paulmillr / scure-btc-signer

Audited & minimal library for creating, signing & decoding Bitcoin transactions.
https://paulmillr.com/noble/#scure
MIT License
151 stars 39 forks source link

Question about generating taproot addresses #60

Closed radicleart closed 4 months ago

radicleart commented 11 months ago

Hey there,

wondering why these two methods yield different bech32m addresses?

    const script = btc.p2tr(xOnlyPubKey, undefined, net)
    const addr = btc.Address(net).encode({type: 'tr', pubkey: xOnlyPubKey})
    expect(script.address).equals(addr)

this test fails but we expected it to pass - how are the two different addresses connected ?

finian commented 4 months ago

Not the same.

const script = btc.p2tr(xOnlyPubKey, undefined, net) script.address

By default, p2tr uses the tweaked public key to generate the address.

const addr = btc.Address(net).encode({type: 'tr', pubkey: xOnlyPubKey})

And here you use xOnlyPubKey AS the tweaked public key to generate the address.

You should tweak the xOnlyPubKey first to generate the same address:

const addr = btc.Address(net).encode({type: 'tr', pubkey:btc.taprootTweakPubkey(xOnlyPubKey, new Uint8Array())[0]})