Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
haxwell opened this issue Jan 14, 2023 · 1 comment

Comments

@haxwell
Copy link

haxwell commented Jan 14, 2023

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.

@iamhardikat11
Copy link

Can i take up this Issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants