Skip to content

Can BitcoinJS generate Taproot addresses? #1746

Closed
@SuperHenkie

Description

@SuperHenkie

Given a private key (in Hex of WIF format) is there a way to get BitcoinJS to generate the corresponding Taproot (P2TR) address for that?

I sortof extracted this from the Taproot Key Spend example:

const dummyExampleWif = 'L4m2jm8B2vnN5c3FCaCRtbk7Ao9tavipfKKJZTecwLKAQzpGWBT7';
const keyPair = ECPair.fromWIF( dummyExampleWif );
// or: const keyPair = ECPair.makeRandom();

const output = Buffer.concat([
  // witness v1, PUSH_DATA 32 bytes
  Buffer.from([0x51, 0x20]),
  // x-only pubkey (remove 1 byte y parity)
  keyPair.publicKey.slice(1, 33),
]);

const p2trAddress = address.fromOutputScript( output );

However this results in an error:

Uncaught Error: OP_1 9902b7ca58529074db3c0e98f6101753f5978be6ff9ab9acdf8aee7539468048 has no matching Address

What am I doing wrong here?

P.S. I'm using bitcoinjs-lib 4.0.3.

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