Skip to content

Commit

Permalink
Add witness taproot address support
Browse files Browse the repository at this point in the history
  • Loading branch information
martinboehm committed Sep 14, 2021
1 parent 86f9dd3 commit 8ece5dc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
24 changes: 17 additions & 7 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ func encodeSegWitAddress(hrp string, witnessVersion byte, witnessProgram []byte)
combined := make([]byte, len(converted)+1)
combined[0] = witnessVersion
copy(combined[1:], converted)
bech, err := bech32.Encode(hrp, combined)
var bech string
if witnessVersion == 0 {
bech, err = bech32.Encode(hrp, combined)
} else {
bech, err = bech32.EncodeM(hrp, combined)
}
if err != nil {
return "", err
}
Expand Down Expand Up @@ -151,8 +156,8 @@ func DecodeAddress(addr string, defaultNet *chaincfg.Params) (Address, error) {
// and we proceed to try decoding as a legacy address below.
if err == nil {
// We currently only support P2WPKH and P2WSH, which is
// witness version 0.
if witnessVer != 0 {
// witness version 0 and taproot version 1.
if witnessVer > 1 {
return nil, UnsupportedWitnessVerError(witnessVer)
}

Expand All @@ -163,7 +168,7 @@ func DecodeAddress(addr string, defaultNet *chaincfg.Params) (Address, error) {
case 20:
return newAddressWitnessPubKeyHash(hrp, witnessProg)
case 32:
return newAddressWitnessScriptHash(hrp, witnessProg)
return newAddressWitnessScriptHash(hrp, witnessVer, witnessProg)
default:
return nil, UnsupportedWitnessProgLenError(len(witnessProg))
}
Expand Down Expand Up @@ -631,13 +636,18 @@ type AddressWitnessScriptHash struct {

// NewAddressWitnessScriptHash returns a new AddressWitnessPubKeyHash.
func NewAddressWitnessScriptHash(witnessProg []byte, net *chaincfg.Params) (*AddressWitnessScriptHash, error) {
return newAddressWitnessScriptHash(net.Bech32HRPSegwit, witnessProg)
return newAddressWitnessScriptHash(net.Bech32HRPSegwit, 0, witnessProg)
}

// NewAddressWitnessTaproot returns a new AddressWitnessPubKeyHash v1.
func NewAddressWitnessTaproot(witnessProg []byte, net *chaincfg.Params) (*AddressWitnessScriptHash, error) {
return newAddressWitnessScriptHash(net.Bech32HRPSegwit, 1, witnessProg)
}

// newAddressWitnessScriptHash is an internal helper function to create an
// AddressWitnessScriptHash with a known human-readable part, rather than
// looking it up through its parameters.
func newAddressWitnessScriptHash(hrp string, witnessProg []byte) (*AddressWitnessScriptHash, error) {
func newAddressWitnessScriptHash(hrp string, witnessVer byte, witnessProg []byte) (*AddressWitnessScriptHash, error) {
// Check for valid program length for witness version 0, which is 32
// for P2WSH.
if len(witnessProg) != 32 {
Expand All @@ -647,7 +657,7 @@ func newAddressWitnessScriptHash(hrp string, witnessProg []byte) (*AddressWitnes

addr := &AddressWitnessScriptHash{
hrp: strings.ToLower(hrp),
witnessVersion: 0x00,
witnessVersion: witnessVer,
}

copy(addr.witnessProgram[:], witnessProg)
Expand Down
27 changes: 25 additions & 2 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,29 @@ func TestAddresses(t *testing.T) {
},
net: &chaincfg.TestNet3Params,
},
{
name: "segwit testnet taproot v1",
addr: "tb1petwqkk8wnk4lgweyy4xvgxk0c7f572mrval39mwaxl34scex8zsqlpfgdd",
encoded: "tb1petwqkk8wnk4lgweyy4xvgxk0c7f572mrval39mwaxl34scex8zsqlpfgdd",
valid: true,
result: btcutil.TstAddressWitnessScriptHash(
1,
[32]byte{
0xca, 0xdc, 0x0b, 0x58, 0xee, 0x9d, 0xab, 0xf4,
0x3b, 0x24, 0x25, 0x4c, 0xc4, 0x1a, 0xcf, 0xc7,
0x93, 0x4f, 0x2b, 0x63, 0x67, 0x7f, 0x12, 0xed,
0xdd, 0x37, 0xe3, 0x58, 0x63, 0x26, 0x38, 0xa0},
chaincfg.TestNet3Params.Bech32HRPSegwit),
f: func() (btcutil.Address, error) {
scriptHash := []byte{
0xca, 0xdc, 0x0b, 0x58, 0xee, 0x9d, 0xab, 0xf4,
0x3b, 0x24, 0x25, 0x4c, 0xc4, 0x1a, 0xcf, 0xc7,
0x93, 0x4f, 0x2b, 0x63, 0x67, 0x7f, 0x12, 0xed,
0xdd, 0x37, 0xe3, 0x58, 0x63, 0x26, 0x38, 0xa0}
return btcutil.NewAddressWitnessTaproot(scriptHash, &chaincfg.TestNet3Params)
},
net: &chaincfg.TestNet3Params,
},
{
name: "segwit litecoin mainnet p2wpkh v0",
addr: "LTC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KGMN4N9",
Expand All @@ -714,9 +737,9 @@ func TestAddresses(t *testing.T) {
},
net: &customParams,
},
// Unsupported witness versions (version 0 only supported at this point)
// Unsupported witness versions (version 0 and 1 only supported at this point)
{
name: "segwit mainnet witness v1",
name: "segwit mainnet witness v1 invalid length",
addr: "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx",
valid: false,
net: &chaincfg.MainNetParams,
Expand Down

0 comments on commit 8ece5dc

Please sign in to comment.