btcsuite / btcd

An alternative full node bitcoin implementation written in Go (golang)
https://github.com/btcsuite/btcd/blob/master/README.md
ISC License
6.1k stars 2.31k forks source link

Generating Taproot Address #2068

Closed developer2346 closed 6 months ago

developer2346 commented 6 months ago

Hi I am trying to generate Taproot address:

entropy, err := bip39.NewEntropy(128)
if err != nil {
return nil, err
}
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return nil, err
}

seed := bip39.NewSeed(mnemonic, "")

hdprvkey := bip32.NewHDKey(seed)
privateKey := hdprvkey.PrivateKey()

p2trAddress, err := btcutil.NewAddressWitnessPubKeyHash(btcutil.Hash160(privateKey.PubKey().SerializeCompressed()), &chaincfg.MainNetParams)
if err != nil {
panic(err)
}

addr, err := btcutil.NewAddressTaproot(p2trAddress.WitnessProgram(), &chaincfg.MainNetParams)
if err != nil {
panic(err)
// panic here: witness program must be 32 bytes for p2tr
}

But I am unable to generate as I get: panic: witness program must be 32 bytes for p2tr

guggero commented 6 months ago

Taproot works a bit different to the previous address types. I assume you'll want to have a single-sig only (BIP-0086) address? Then you'd need to use this code:

    taprootKey := txscript.ComputeTaprootKeyNoScript(internalKey)
    tapScriptAddr, err := btcutil.NewAddressTaproot(
        schnorr.SerializePubKey(taprootKey), harnessNetParams,
    )

Feel free to take a look at our integration tests in lnd for more advanced Taproot related example code in Go.