bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.69k stars 2.11k forks source link

Wrong derivation address #1670

Closed maxidev closed 3 years ago

maxidev commented 3 years ago

Hi there, I think this is not a problem with a library itself but rather with my implementation, but not sure what I'm doing wroing. I'm writing a simple script to derive multisig addresses based on 2 xpub (2 of 2 scheme).

xpub1 = "xpub6B7t__c2y"; xpub2 = "xpub6B1TUPxg";

I'm deriving first public key from each xpub to then create the multisig address

const pub1 = bitcoinjs.payments.p2pkh({
        pubkey: bip32.fromBase58(xpub1).derive(0).derive(0).publicKey,
  });

const pub2 = bitcoinjs.payments.p2pkh({
      pubkey: bip32.fromBase58(xpub2).derive(0).derive(0).publicKey,
});

and finally:

const pubkeys = [
  pub1.pubkey.toString('hex'),
  pub2.pubkey.toString('hex'),
].map(hex => Buffer.from(hex, 'hex'));

const {address} = bitcoinjs.payments.p2sh({
  redeem: bitcoinjs.payments.p2ms({ m: 2, pubkeys }),
});

Now, it works and outputs an address but weird thing that is DIFFERENT from Electrum and Bluewallet address, so I guess it is something wrong with derivation paths but I'm not sure.

Weird thing is that if i set the .derive(0).derive(1) on both public keys I got the SECOND address generated by Electrum and Bluewallet.

Can someone point me in the right direction?

Thanks in advance.

maxidev commented 3 years ago

[Solved] I will close the issue but if someone stumbles with the same problem, key thing is to SORT lexicographically the pubkeys buffer array as described in: https://github.com/bitcoin/bips/blob/master/bip-0067.mediawiki

junderw commented 3 years ago

duplicate of #1667