bitcoinjs / bitcoinjs-lib

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

Error while encoding a P2TR address with `address.fromOutputScript()` #1965

Closed DariusParvin closed 1 year ago

DariusParvin commented 1 year ago

I'm experiencing an issue while trying to encode a P2TR address using fromOutputScript. The issue doesn't occur when encoding P2WPKH or P2WSH output scripts.

Here's an example of the error:

const p2trScriptPubkey = Buffer.from("5120d90984bb46b01a27a720255fe7272e358af939f5a3ce746b47387e17da5fdcec", "hex");
console.log(bitcoin.address.fromOutputScript(p2trScriptPubkey));

Returns the error:

  throw new Error(bscript.toASM(output) + ' has no matching Address');
  ^

Error: OP_1 d90984bb46b01a27a720255fe7272e358af939f5a3ce746b47387e17da5fdcec has no matching Address
    at Object.fromOutputScript (/Users/dariuscognac/temp-bitcoinjs/node_modules/bitcoinjs-lib/src/address.js:116:9)
    at Object.<anonymous> (/Users/dariuscognac/temp-bitcoinjs/index.js:305:29)
    at Module._compile (node:internal/modules/cjs/loader:1257:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1311:10)
    at Module.load (node:internal/modules/cjs/loader:1115:32)
    at Module._load (node:internal/modules/cjs/loader:962:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47

Could this be a bug, or am I doing something incorrect? I didn't see this same issue posted here.

Environment:

junderw commented 1 year ago

For p2tr payments (and by extension, any address methods dealing with p2tr) initEccLib must be run at least once.

See the examples for details.

Why is this needed? Because we need an ecc library to verify the point contained in the p2tr output script.

DariusParvin commented 1 year ago

ah okay got it, thanks a lot @junderw !