bitcoinjs / bitcoinjs-lib

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

Incorrect extendedPubkey for globalXpub #1504

Closed satsie closed 4 years ago

satsie commented 4 years ago

Hello, I've been experiencing issues with types, specifically Buffers. I keep getting errors similar to this one:

Error: Data for global key globalXpub is incorrect: Expected { masterFingerprint: Buffer; extendedPubkey: Buffer; path: string; } and got [{"masterFingerprint":{"type":"Buffer","data":[218,46,241,23]},"extendedPubkey":{"type":"Buffer","data":[3,81,127,131,184,199,63,219,5,8,53,48,18,86,199,44,24,201,18,91,127,36,7,15,135,138,212,215,223,59,131,21,133]},"path":"m/49/1/0/0"}]
    at throwForUpdateMaker (/vagrant/node_modules/bip174/src/lib/utils.js:58:9)
    at Object.updateGlobal (/vagrant/node_modules/bip174/src/lib/utils.js:84:13)
    at Psbt.updateGlobal (/vagrant/node_modules/bip174/src/lib/psbt.js:41:13)
    at Psbt.updateGlobal (/vagrant/node_modules/bitcoinjs-lib/src/psbt.js:472:15)

I printed the object out before the error was thrown and by all accounts, it appears to meet the requirements of having 2 buffers (masterFingerprint, extendedPubkey) and a String (path).

[{ masterFingerprint: <Buffer 2e e1 f6 01>,
  extendedPubkey: <Buffer 03 51 7f 83 0f 87 8a d4 b8 c7 3f db 05 08 35 30 12 5b 7f 24 07 12 56 c7 2c 18 c9 d7 df 3b 83 15 85>,
  path: 'm/49/1/0/0' }]

This is the method I am calling, psbt.updateGlobal(): https://github.com/bitcoinjs/bitcoinjs-lib/blob/v5.1.6/src/psbt.js#L471 The code looks something like this (some fake values substituted in here):

const masterFingerprint = Buffer.from('some-master-fingerprint', 'hex');
const extendedPubkey = Buffer.from('some-pub-key', 'hex');
const path = 'm/49/1/0/0'
psbt.updateGlobal({globalXpub: [{masterFingerprint, extendedPubkey, path]});

I'm using VS Code and did a number of things to clean up my workspace so now when I right click and look up the definition of Buffer, it takes me to the same place as right clicking for the definition of Buffer in the bitcoinjs-lib and bip174 libraries. For much of my code I was able to get around this error by calling certain libraries directly like bip32 and following the sample code exactly. I am not importing another definition for Buffer anywhere.

The only discrepancy I can find is bitcoinjs-lib is using @types/node version 10.12.18 (https://github.com/bitcoinjs/bitcoinjs-lib/blob/v5.1.6/package.json#L48), while bip174 is using version 12.0.8. Could this cause problems? I see that in September there was a PR where part of the description was "Update @types/node and fix problems" (https://github.com/bitcoinjs/bitcoinjs-lib/pull/1476). Does this PR fix this issue? If so, is there a timeline for release? Thanks very much! 🙏

junderw commented 4 years ago

Hey, thanks for the issue.

extendedPubkey you have given is the compressed pubkey (33 bytes), not an extended one (78 bytes).

The extended pubkey is basically the bytes contained in an xpub but excluding the 4 byte checksum (for base58check).

So just grab your xpub, and run a base58check.decode on it, and use that 78 byte buffer instead.


Also just FYI, TypeScript Errors will always happen at transpile time, so if something errors when you run it, that is not a problem with TypeScript.

Thanks for your report. Let me know if you have any other questions.

satsie commented 4 years ago

Ah! Thank you so much! This has been driving me crazy for the last several days and I finally got it to work using your advice. Really appreciate it, and thanks for the work you do being a maintainer of bitcoinjs.