txscript: add some txscript support for parsing taproot scripts

This commit is contained in:
Brian Stafford 2021-11-08 08:25:44 -06:00 committed by Olaoluwa Osuntokun
parent bfd0f4a492
commit 37964e550b
No known key found for this signature in database
GPG Key ID: 3BBD59E99B280306
2 changed files with 34 additions and 3 deletions

View File

@ -1001,6 +1001,15 @@ func ExtractPkScriptAddrs(pkScript []byte,
return WitnessV0ScriptHashTy, addrs, 1, nil
}
if hash := extractWitnessV1ScriptHash(pkScript); hash != nil {
var addrs []btcutil.Address
addr, err := btcutil.NewAddressTaproot(hash, chainParams)
if err == nil {
addrs = append(addrs, addr)
}
return WitnessV1TaprootTy, addrs, 1, nil
}
// If none of the above passed, then the address must be non-standard.
return NonStandardTy, nil, 0, nil
}

View File

@ -11,9 +11,9 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcd/btcutil"
)
// mustParseShortForm parses the passed short form script and returns the
@ -71,6 +71,20 @@ func newAddressScriptHash(scriptHash []byte) btcutil.Address {
return addr
}
// newAddressTaproot returns a new btcutil.AddressTaproot from the
// provided hash. It panics if an error occurs. This is only used in the tests
// as a helper since the only way it can fail is if there is an error in the
// test source code.
func newAddressTaproot(scriptHash []byte) btcutil.Address {
addr, err := btcutil.NewAddressTaproot(scriptHash,
&chaincfg.MainNetParams)
if err != nil {
panic("invalid script hash in test source")
}
return addr
}
// TestExtractPkScriptAddrs ensures that extracting the type, addresses, and
// number of required signatures from PkScripts works as intended.
func TestExtractPkScriptAddrs(t *testing.T) {
@ -311,8 +325,16 @@ func TestExtractPkScriptAddrs(t *testing.T) {
reqSigs: 1,
class: MultiSigTy,
},
// from real tx: 691dd277dc0e90a462a3d652a1171686de49cf19067cd33c7df0392833fb986a, vout 44
// invalid public keys
{
name: "v1 p2tr witness-script-hash",
script: hexToBytes("51201a82f7457a9ba6ab1074e9f50" +
"053eefc637f8b046e389b636766bdc7d1f676f8"),
addrs: []btcutil.Address{newAddressTaproot(
hexToBytes("1a82f7457a9ba6ab1074e9f50053eefc6" +
"37f8b046e389b636766bdc7d1f676f8"))},
reqSigs: 1,
class: WitnessV1TaprootTy,
},
{
name: "1 of 3 multisig with invalid pubkeys 2",
script: hexToBytes("514134633365633235396337346461636" +