Skip to content

Are duplicate keyPrefixes causing an incorrect Network object to be returned? #1128

@haxwell

Description

@haxwell

The test...

  it('should return the appropriate Network when calling static fromPublic58() ', () => {
    
    // get a random type from network.types
    const networksjs = require('../lib/protocol/networks');
    const type = networksjs.types[Math.floor(Math.random() * networksjs.types.length)];

// Point A
    const network = Network.get(type);
    const result = Network.fromPublic58(network.keyPrefix.xpubkey58);

    assert.strictEqual(result, network);
  });

then calls

   * Get a network by its xpubkey base58 prefix.
   * @param {String} prefix
   * @param {Network?} network
   * @returns {Network}
   */

  static fromPublic58(prefix, network) {
    return Network.by(prefix, cmpPub58, network, 'xpubkey');
  }

which in turn calls,

  /**
   * Get a network by an associated comparator.
   * @private
   * @param {Object} value
   * @param {Function} compare
   * @param {Network|null} network
   * @param {String} name
   * @returns {Network}
   */

  static by(value, compare, network, name) {
    if (network) {
      network = Network.get(network);
      if (compare(network, value))
        return network;
      throw new Error(`Network mismatch for ${name}.`);
    }

    for (const type of networks.types) {
      network = networks[type];
      if (compare(network, value))
        return Network.get(type);
    }

    throw new Error(`Network not found for ${name}.`);
  }

Assuming at Point A in the unit test, the type equals regtest and so the Network object which is the expected return value is a proper Regtest Network object.

The second file, network.js :: fromPublic58(), calls Network.by() with the 'tpub' prefix defined in Regtest, and the cmpPub58 comparator.

Network.by, the third code block above, when it is called, does not have a network object, so it skips the first section. It then iterates through the networks.types, comparing first for main, and then testnet. Since the prefix for testnet and regtest match, it returns the testnet Network object.

The expected behavior is that it returns a regtest Network object. I think.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions