Refactor and improve address extraction code.

This commit significantly changes the address extraction code.  The
original code was written before some of the other newer code was written
and as a result essentially duplicated some of the logic for handling
standard scripts which is used elsewhere in the package.

The following is a summary of what has changed:

- CalcPkScriptAddrHashes, ScriptToAddrHash, and ScriptToAddrHashes have
  been replaced by ExtractPkScriptAddresses
- The ScriptType type has been removed in favor of the existing
  ScriptClass type
- The new function returns a slice of btcutil.Addresses instead of raw
  hashes that the caller then needs to figure out what to do with to
  convert them to proper addressses
- The new function makes use of the existing ScriptClass instead of an
  nearly duplicate ScriptType
- The new function hooks into the existing infrastructure for parsing
  scripts and identifying scripts of standard forms
- The new function only works with pkscripts to match the behavior of the
  reference implementation - do note that the redeeming script from a p2sh
  script is still considered a pkscript
- The logic combines extraction for all script types instead of using a
  separate function for multi-signature transactions
- The new function ignores addresses which are invalid for some reason
  such as invalid public keys
This commit is contained in:
Dave Collins 2014-01-06 20:49:01 -06:00
parent 126991b60b
commit 6c8003b064
4 changed files with 75 additions and 770 deletions

View file

@ -4,197 +4,87 @@
package btcscript
// ScriptType is an enum type that represents the type of a script. It is
// returned from ScriptToAddrHash as part of the metadata about the script.
// It implements the Stringer interface for nice printing.
type ScriptType int
// String Converts the enumeration to a nice string value instead of a number.
func (t ScriptType) String() string {
if int(t) > len(scriptTypeToName) || int(t) < 0 {
return "Invalid"
}
return scriptTypeToName[t]
}
// Constant types representing known types of script found in the wild
const (
ScriptUnknown ScriptType = iota
ScriptAddr
ScriptPubKey
ScriptPayToScriptHash
ScriptMultiSig
ScriptStrange
ScriptGeneration
import (
"github.com/conformal/btcutil"
"github.com/conformal/btcwire"
)
var scriptTypeToName = []string{
ScriptUnknown: "Unknown",
ScriptAddr: "Addr",
ScriptPubKey: "Pubkey",
ScriptPayToScriptHash: "PayToScriptHash",
ScriptMultiSig: "MultiSig",
ScriptStrange: "Strange",
ScriptGeneration: "Generation", // ScriptToAddrHash does not recieve enough information to identify Generation scripts.
}
// ExtractPkScriptAddrs returns the type of script, addresses and required
// signatures associated with the passed PkScript. Note that it only works for
// 'standard' transaction script types. Any data such as public keys which are
// invalid are omitted from the results.
func ExtractPkScriptAddrs(pkScript []byte, net btcwire.BitcoinNet) (ScriptClass, []btcutil.Address, int, error) {
var addrs []btcutil.Address
var requiredSigs int
type pkformat struct {
addrtype ScriptType
parsetype int
length int
databytes []pkbytes
allowmore bool
}
type pkbytes struct {
off int
val byte
}
const (
scrPayAddr = iota
scrCollectAddr
scrCollectAddrComp
scrGeneratePubkeyAddr
scrPubkeyAddr
scrPubkeyAddrComp
scrPayToScriptHash
scrNoAddr
)
// ScriptToAddrHash extracts a 20-byte public key hash and the type out of a PkScript
func ScriptToAddrHash(script []byte) (ScriptType, []byte, error) {
// Currently this only understands one form of PkScript
validformats := []pkformat{
{ScriptAddr, scrPayAddr, 25, []pkbytes{{0, OP_DUP}, {1, OP_HASH160}, {2, OP_DATA_20}, {23, OP_EQUALVERIFY}, {24, OP_CHECKSIG}}, true},
{ScriptAddr, scrCollectAddr, 142, []pkbytes{{0, OP_DATA_75}, {76, OP_DATA_65}}, false},
{ScriptAddr, scrCollectAddr, 141, []pkbytes{{0, OP_DATA_74}, {75, OP_DATA_65}}, false},
{ScriptAddr, scrCollectAddr, 140, []pkbytes{{0, OP_DATA_73}, {74, OP_DATA_65}}, false},
{ScriptAddr, scrCollectAddr, 139, []pkbytes{{0, OP_DATA_72}, {73, OP_DATA_65}}, false},
{ScriptAddr, scrCollectAddr, 138, []pkbytes{{0, OP_DATA_71}, {72, OP_DATA_65}}, false},
{ScriptAddr, scrCollectAddr, 137, []pkbytes{{0, OP_DATA_70}, {71, OP_DATA_65}}, false},
{ScriptAddr, scrCollectAddr, 136, []pkbytes{{0, OP_DATA_69}, {70, OP_DATA_65}}, false},
{ScriptAddr, scrCollectAddrComp, 110, []pkbytes{{0, OP_DATA_75}, {76, OP_DATA_33}}, false},
{ScriptAddr, scrCollectAddrComp, 109, []pkbytes{{0, OP_DATA_74}, {75, OP_DATA_33}}, false},
{ScriptAddr, scrCollectAddrComp, 108, []pkbytes{{0, OP_DATA_73}, {74, OP_DATA_33}}, false},
{ScriptAddr, scrCollectAddrComp, 107, []pkbytes{{0, OP_DATA_72}, {73, OP_DATA_33}}, false},
{ScriptAddr, scrCollectAddrComp, 106, []pkbytes{{0, OP_DATA_71}, {72, OP_DATA_33}}, false},
{ScriptAddr, scrCollectAddrComp, 105, []pkbytes{{0, OP_DATA_70}, {71, OP_DATA_33}}, false},
{ScriptAddr, scrCollectAddrComp, 104, []pkbytes{{0, OP_DATA_69}, {70, OP_DATA_33}}, false},
{ScriptPubKey, scrGeneratePubkeyAddr, 74, []pkbytes{{0, OP_DATA_73}}, false},
{ScriptPubKey, scrGeneratePubkeyAddr, 73, []pkbytes{{0, OP_DATA_72}}, false},
{ScriptPubKey, scrGeneratePubkeyAddr, 72, []pkbytes{{0, OP_DATA_71}}, false},
{ScriptPubKey, scrGeneratePubkeyAddr, 71, []pkbytes{{0, OP_DATA_70}}, false},
{ScriptPubKey, scrGeneratePubkeyAddr, 70, []pkbytes{{0, OP_DATA_69}}, false},
{ScriptPubKey, scrPubkeyAddr, 67, []pkbytes{{0, OP_DATA_65}, {66, OP_CHECKSIG}}, true},
{ScriptPubKey, scrPubkeyAddrComp, 35, []pkbytes{{0, OP_DATA_33}, {34, OP_CHECKSIG}}, true},
{ScriptPayToScriptHash, scrPayToScriptHash, 23, []pkbytes{{0, OP_HASH160}, {1, OP_DATA_20}, {22, OP_EQUAL}}, false},
{ScriptStrange, scrNoAddr, 33, []pkbytes{{0, OP_DATA_32}}, false},
}
return scriptToAddrHashTemplate(script, validformats)
}
func scriptToAddrHashTemplate(script []byte, validformats []pkformat) (ScriptType, []byte, error) {
var format pkformat
var success bool
for _, format = range validformats {
if format.length != len(script) {
if len(script) < format.length {
continue
}
if !format.allowmore {
continue
}
}
success = true
for _, pkbyte := range format.databytes {
if pkbyte.off >= len(script) {
return ScriptUnknown, nil,
StackErrInvalidAddrOffset
}
if script[pkbyte.off] != pkbyte.val {
log.Tracef("off at byte %v %v %v", pkbyte.off, script[pkbyte.off], pkbyte.val)
success = false
break
} else {
log.Tracef("match at byte %v: ok", pkbyte.off)
}
}
if success == true {
break
}
}
if success == false {
if len(script) > 1 {
// check for a few special case
if script[len(script)-1] == OP_CHECKMULTISIG {
return ScriptStrange, nil, nil
}
if script[0] == OP_0 && (len(script) <= 75 && byte(len(script)) == script[1]+2) {
return ScriptStrange, nil, nil
}
if script[0] == OP_DATA_36 && len(script) == 37 {
// Multisig ScriptSig
return ScriptStrange, nil, nil
}
}
return ScriptUnknown, nil, StackErrUnknownAddress
}
var addrhash []byte
switch format.parsetype {
case scrPayAddr:
addrhash = script[3:23]
case scrCollectAddr:
// script is replaced with the md160 of the pubkey
slen := len(script)
pubkey := script[slen-65:]
addrhash = calcHash160(pubkey)
case scrCollectAddrComp:
// script is replaced with the md160 of the pubkey
slen := len(script)
pubkey := script[slen-33:]
addrhash = calcHash160(pubkey)
case scrGeneratePubkeyAddr:
// unable to determine address hash from script
case scrNoAddr:
// unable to determine address hash from script
case scrPubkeyAddr:
pubkey := script[1:66]
addrhash = calcHash160(pubkey)
case scrPubkeyAddrComp:
pubkey := script[1:34]
addrhash = calcHash160(pubkey)
case scrPayToScriptHash:
addrhash = script[2:22]
default:
return ScriptUnknown, nil, StackErrInvalidParseType
}
return format.addrtype, addrhash, nil
}
// ScriptToAddrHashes extracts multiple 20-byte public key hashes
// from a MultiSig script.
func ScriptToAddrHashes(script []byte) (ScriptType, int, [][]byte, error) {
pops, err := parseScript(script)
// No valid addresses or required signatures if the script doesn't
// parse.
pops, err := parseScript(pkScript)
if err != nil {
return ScriptUnknown, 0, nil, StackErrUnknownAddress
return NonStandardTy, nil, 0, err
}
if !isMultiSig(pops) {
return ScriptUnknown, 0, nil, StackErrUnknownAddress
scriptClass := typeOfScript(pops)
switch scriptClass {
case PubKeyHashTy:
// A pay-to-pubkey-hash script is of the form:
// OP_DUP OP_HASH160 <hash> OP_EQUALVERIFY OP_CHECKSIG
// Therefore the pubkey hash is the 3rd item on the stack.
// Skip the pubkey hash if it's invalid for some reason.
requiredSigs = 1
addr, err := btcutil.NewAddressPubKeyHash(pops[2].data, net)
if err == nil {
addrs = append(addrs, addr)
}
case PubKeyTy:
// A pay-to-pubkey script is of the form:
// <pubkey> OP_CHECKSIG
// Therefore the pubkey is the item on the stack.
// Skip the pubkey if it's invalid for some reason.
requiredSigs = 1
addr, err := btcutil.NewAddressPubKey(pops[0].data, net)
if err == nil {
addrs = append(addrs, addr)
}
case ScriptHashTy:
// A pay-to-script-hash script is of the form:
// OP_HASH160 <scripthash> OP_EQUAL
// Therefore the script hash is the 2nd item on the stack.
// Skip the script hash if it's invalid for some reason.
requiredSigs = 1
addr, err := btcutil.NewAddressScriptHashFromHash(pops[1].data, net)
if err == nil {
addrs = append(addrs, addr)
}
case MultiSigTy:
// A multi-signature script is of the form:
// <numsigs> <pubkey> <pubkey> <pubkey>... <numpubkeys> OP_CHECKMULTISIG
// Therefore the number of required signatures is the 1st item
// on the stack and the number of public keys is the 2nd to last
// item on the stack.
requiredSigs = int(pops[0].opcode.value - (OP_1 - 1))
numPubKeys := int(pops[len(pops)-2].opcode.value - (OP_1 - 1))
// Extract the public keys while skipping any that are invalid.
addrs = make([]btcutil.Address, 0, numPubKeys)
for i := 0; i < numPubKeys; i++ {
addr, err := btcutil.NewAddressPubKey(pops[i+1].data, net)
if err == nil {
addrs = append(addrs, addr)
}
}
case NullDataTy:
// Null data transactions have no addresses or required
// signatures.
case NonStandardTy:
// Don't attempt to extract addresses or required signatures for
// nonstandard transactions.
}
l := len(pops)
addrHashes := make([][]byte, l-3)
for i, pop := range pops[1 : l-2] {
addrHashes[i] = calcHash160(pop.data)
}
reqSigs := int(pops[0].opcode.value - (OP_1 - 1))
return ScriptMultiSig, reqSigs, addrHashes, nil
return scriptClass, addrs, requiredSigs, nil
}

View file

@ -1,490 +0,0 @@
// Copyright (c) 2013 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcscript_test
import (
"bytes"
"github.com/conformal/btcscript"
"testing"
)
type addressTest struct {
script []byte
addrhash []byte
shouldFail error
class btcscript.ScriptType
}
var addressTests = []addressTest{
{script: []byte{btcscript.OP_DATA_65,
0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc,
0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48,
0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a,
0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc,
0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b,
0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f,
0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12,
0xa3, btcscript.OP_CHECKSIG},
addrhash: []byte{0x11, 0xb3, 0x66, 0xed, 0xfc, 0x0a,
0x8b, 0x66, 0xfe, 0xeb, 0xae, 0x5c, 0x2e,
0x25, 0xa7, 0xb6, 0xa5, 0xd1, 0xcf, 0x31},
class: btcscript.ScriptPubKey,
},
{script: []byte{btcscript.OP_DATA_65,
0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc,
0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48,
0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a,
0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc,
0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b,
0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f,
0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12,
0xa3},
shouldFail: btcscript.StackErrUnknownAddress,
},
{script: []byte{btcscript.OP_DATA_71,
0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3,
0xa1, 0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32,
0xe9, 0xd6, 0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab,
0x5f, 0xb8, 0xcd, 0x41, 0x02, 0x20, 0x18, 0x15,
0x22, 0xec, 0x8e, 0xca, 0x07, 0xde, 0x48, 0x60,
0xa4, 0xac, 0xdd, 0x12, 0x90, 0x9d, 0x83, 0x1c,
0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22, 0x08, 0x22,
0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09, 0x01},
addrhash: nil,
class: btcscript.ScriptPubKey,
},
{script: []byte{btcscript.OP_DUP, btcscript.OP_HASH160,
btcscript.OP_DATA_20,
0xad, 0x06, 0xdd, 0x6d, 0xde, 0xe5, 0x5c, 0xbc,
0xa9, 0xa9, 0xe3, 0x71, 0x3b, 0xd7, 0x58, 0x75,
0x09, 0xa3, 0x05, 0x64,
btcscript.OP_EQUALVERIFY, btcscript.OP_CHECKSIG,
},
addrhash: []byte{0xad, 0x06, 0xdd, 0x6d, 0xde, 0xe5,
0x5c, 0xbc, 0xa9, 0xa9, 0xe3, 0x71, 0x3b,
0xd7, 0x58, 0x75, 0x09, 0xa3, 0x05, 0x64},
class: btcscript.ScriptAddr,
},
{script: []byte{btcscript.OP_DATA_73,
0x30, 0x46, 0x02, 0x21, 0x00, 0xdd, 0xc6, 0x97,
0x38, 0xbf, 0x23, 0x36, 0x31, 0x8e, 0x4e, 0x04,
0x1a, 0x5a, 0x77, 0xf3, 0x05, 0xda, 0x87, 0x42,
0x8a, 0xb1, 0x60, 0x6f, 0x02, 0x32, 0x60, 0x01,
0x78, 0x54, 0x35, 0x0d, 0xdc, 0x02, 0x21, 0x00,
0x81, 0x7a, 0xf0, 0x9d, 0x2e, 0xec, 0x36, 0x86,
0x2d, 0x16, 0x00, 0x98, 0x52, 0xb7, 0xe3, 0xa0,
0xf6, 0xdd, 0x76, 0x59, 0x82, 0x90, 0xb7, 0x83,
0x4e, 0x14, 0x53, 0x66, 0x03, 0x67, 0xe0, 0x7a,
0x01,
btcscript.OP_DATA_65,
0x04, 0xcd, 0x42, 0x40, 0xc1, 0x98, 0xe1, 0x25,
0x23, 0xb6, 0xf9, 0xcb, 0x9f, 0x5b, 0xed, 0x06,
0xde, 0x1b, 0xa3, 0x7e, 0x96, 0xa1, 0xbb, 0xd1,
0x37, 0x45, 0xfc, 0xf9, 0xd1, 0x1c, 0x25, 0xb1,
0xdf, 0xf9, 0xa5, 0x19, 0x67, 0x5d, 0x19, 0x88,
0x04, 0xba, 0x99, 0x62, 0xd3, 0xec, 0xa2, 0xd5,
0x93, 0x7d, 0x58, 0xe5, 0xa7, 0x5a, 0x71, 0x04,
0x2d, 0x40, 0x38, 0x8a, 0x4d, 0x30, 0x7f, 0x88,
0x7d},
addrhash: []byte{0x40, 0x92, 0x08, 0xf3, 0x87, 0xf4,
0x7f, 0xd2, 0x3a, 0x9f, 0x44, 0x5e, 0x14,
0xdc, 0x1f, 0x99, 0xbb, 0xb8, 0x0d, 0xaa},
class: btcscript.ScriptAddr,
},
{script: []byte{btcscript.OP_DATA_73,
0x30, 0x46, 0x02, 0x21, 0x00, 0xac, 0x7e, 0x4e,
0x27, 0xf2, 0xb1, 0x1c, 0xb8, 0x6f, 0xb5, 0xaa,
0x87, 0x2a, 0xb9, 0xd3, 0x2c, 0xdc, 0x08, 0x33,
0x80, 0x73, 0x3e, 0x3e, 0x98, 0x47, 0xff, 0x77,
0xa0, 0x69, 0xcd, 0xdf, 0xab, 0x02, 0x21, 0x00,
0xc0, 0x4c, 0x3e, 0x6f, 0xfe, 0x88, 0xa1, 0x5b,
0xc5, 0x07, 0xb8, 0xe5, 0x71, 0xaa, 0x35, 0x92,
0x8a, 0xcf, 0xe1, 0x5a, 0x4a, 0x23, 0x20, 0x1b,
0x08, 0xfe, 0x3c, 0x7b, 0x3c, 0x97, 0xc8, 0x8f,
0x01,
btcscript.OP_DATA_33,
0x02, 0x40, 0x05, 0xc9, 0x45, 0xd8, 0x6a, 0xc6,
0xb0, 0x1f, 0xb0, 0x42, 0x58, 0x34, 0x5a, 0xbe,
0xa7, 0xa8, 0x45, 0xbd, 0x25, 0x68, 0x9e, 0xdb,
0x72, 0x3d, 0x5a, 0xd4, 0x06, 0x8d, 0xdd, 0x30,
0x36,
},
addrhash: []byte{0x0c, 0x1b, 0x83, 0xd0, 0x1d, 0x0f,
0xfb, 0x2b, 0xcc, 0xae, 0x60, 0x69, 0x63,
0x37, 0x6c, 0xca, 0x38, 0x63, 0xa7, 0xce},
class: btcscript.ScriptAddr,
},
{script: []byte{btcscript.OP_DATA_32,
0x30, 0x46, 0x02, 0x21, 0x00, 0xac, 0x7e, 0x4e,
0x27, 0xf2, 0xb1, 0x1c, 0xb8, 0x6f, 0xb5, 0xaa,
0x87, 0x2a, 0xb9, 0xd3, 0x2c, 0xdc, 0x08, 0x33,
0x80, 0x73, 0x3e, 0x3e, 0x98, 0x47, 0xff, 0x77,
},
addrhash: nil,
class: btcscript.ScriptStrange,
},
{script: []byte{btcscript.OP_DATA_33,
0x02, 0x40, 0x05, 0xc9, 0x45, 0xd8, 0x6a, 0xc6,
0xb0, 0x1f, 0xb0, 0x42, 0x58, 0x34, 0x5a, 0xbe,
0xa7, 0xa8, 0x45, 0xbd, 0x25, 0x68, 0x9e, 0xdb,
0x72, 0x3d, 0x5a, 0xd4, 0x06, 0x8d, 0xdd, 0x30,
0x36,
btcscript.OP_CHECKSIG,
},
addrhash: []byte{0x0c, 0x1b, 0x83, 0xd0, 0x1d, 0x0f,
0xfb, 0x2b, 0xcc, 0xae, 0x60, 0x69, 0x63,
0x37, 0x6c, 0xca, 0x38, 0x63, 0xa7, 0xce},
class: btcscript.ScriptPubKey,
},
{script: []byte{btcscript.OP_DATA_33,
0x02, 0x40, 0x05, 0xc9, 0x45, 0xd8, 0x6a, 0xc6,
0xb0, 0x1f, 0xb0, 0x42, 0x58, 0x34, 0x5a, 0xbe,
0xa7, 0xa8, 0x45, 0xbd, 0x25, 0x68, 0x9e, 0xdb,
0x72, 0x3d, 0x5a, 0xd4, 0x06, 0x8d, 0xdd, 0x30,
0x36,
btcscript.OP_CHECKMULTISIG, // note this isn't a real tx
},
addrhash: nil,
class: btcscript.ScriptStrange,
},
{script: []byte{btcscript.OP_0, btcscript.OP_DATA_33,
0x02, 0x40, 0x05, 0xc9, 0x45, 0xd8, 0x6a, 0xc6,
0xb0, 0x1f, 0xb0, 0x42, 0x58, 0x34, 0x5a, 0xbe,
0xa7, 0xa8, 0x45, 0xbd, 0x25, 0x68, 0x9e, 0xdb,
0x72, 0x3d, 0x5a, 0xd4, 0x06, 0x8d, 0xdd, 0x30,
0x36, // note this isn't a real tx
},
addrhash: nil,
class: btcscript.ScriptStrange,
},
{script: []byte{btcscript.OP_HASH160, btcscript.OP_DATA_20,
0x63, 0xbc, 0xc5, 0x65, 0xf9, 0xe6, 0x8e, 0xe0,
0x18, 0x9d, 0xd5, 0xcc, 0x67, 0xf1, 0xb0, 0xe5,
0xf0, 0x2f, 0x45, 0xcb,
btcscript.OP_EQUAL,
},
addrhash: []byte{0x63, 0xbc, 0xc5, 0x65, 0xf9, 0xe6, 0x8e, 0xe0,
0x18, 0x9d, 0xd5, 0xcc, 0x67, 0xf1, 0xb0, 0xe5,
0xf0, 0x2f, 0x45, 0xcb},
class: btcscript.ScriptPayToScriptHash,
},
{script: []byte{btcscript.OP_DATA_36,
0x02, 0x40, 0x05, 0xc9, 0x45, 0xd8, 0x6a, 0xc6,
0xb0, 0x1f, 0xb0, 0x42, 0x58, 0x34, 0x5a, 0xbe,
0xb0, 0x1f, 0xb0, 0x42, 0x58, 0x34, 0x5a, 0xbe,
0xb0, 0x1f, 0xb0, 0x42, 0x58, 0x34, 0x5a, 0xbe,
0xa7, 0xa8, 0x45, 0xbd,
// note this isn't a real tx
},
addrhash: nil,
class: btcscript.ScriptStrange,
},
{script: []byte{},
shouldFail: btcscript.StackErrUnknownAddress,
},
}
func TestAddresses(t *testing.T) {
for i, s := range addressTests {
class, addrhash, err := btcscript.ScriptToAddrHash(s.script)
if s.shouldFail != nil {
if err != s.shouldFail {
t.Errorf("Address test %v failed is err [%v] should be [%v]", i, err, s.shouldFail)
}
} else {
if err != nil {
t.Errorf("Address test %v failed err %v", i, err)
} else {
if !bytes.Equal(s.addrhash, addrhash) {
t.Errorf("Address test %v mismatch is [%v] want [%v]", i, addrhash, s.addrhash)
}
if s.class != class {
t.Errorf("Address test %v class mismatch is [%v] want [%v]", i, class, s.class)
}
}
}
}
}
type stringifyTest struct {
name string
scripttype btcscript.ScriptType
stringed string
}
var stringifyTests = []stringifyTest{
stringifyTest{
name: "unknown",
scripttype: btcscript.ScriptUnknown,
stringed: "Unknown",
},
stringifyTest{
name: "addr",
scripttype: btcscript.ScriptAddr,
stringed: "Addr",
},
stringifyTest{
name: "pubkey",
scripttype: btcscript.ScriptPubKey,
stringed: "Pubkey",
},
stringifyTest{
name: "strange",
scripttype: btcscript.ScriptStrange,
stringed: "Strange",
},
stringifyTest{
name: "generation",
scripttype: btcscript.ScriptGeneration,
stringed: "Generation",
},
stringifyTest{
name: "not in enum",
scripttype: btcscript.ScriptType(255),
stringed: "Invalid",
},
}
func TestStringify(t *testing.T) {
for _, test := range stringifyTests {
typeString := test.scripttype.String()
if typeString != test.stringed {
t.Errorf("%s: got \"%s\" expected \"%s\"", test.name,
typeString, test.stringed)
}
}
}
type multiSigTest struct {
script []byte
reqSigs int
addrhashes [][]byte
shouldFail error
class btcscript.ScriptType
}
var multiSigTests = []multiSigTest{
{script: []byte{},
class: btcscript.ScriptUnknown,
shouldFail: btcscript.StackErrUnknownAddress,
},
{script: []byte{btcscript.OP_DATA_65,
0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc,
0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48,
0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a,
0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc,
0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b,
0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f,
0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12,
0xa3, btcscript.OP_CHECKSIG},
class: btcscript.ScriptUnknown,
shouldFail: btcscript.StackErrUnknownAddress,
},
{script: []byte{
btcscript.OP_1, btcscript.OP_DATA_65,
0x04, 0xcc, 0x71, 0xeb, 0x30, 0xd6, 0x53, 0xc0,
0xc3, 0x16, 0x39, 0x90, 0xc4, 0x7b, 0x97, 0x6f,
0x3f, 0xb3, 0xf3, 0x7c, 0xcc, 0xdc, 0xbe, 0xdb,
0x16, 0x9a, 0x1d, 0xfe, 0xf5, 0x8b, 0xbf, 0xbf,
0xaf, 0xf7, 0xd8, 0xa4, 0x73, 0xe7, 0xe2, 0xe6,
0xd3, 0x17, 0xb8, 0x7b, 0xaf, 0xe8, 0xbd, 0xe9,
0x7e, 0x3c, 0xf8, 0xf0, 0x65, 0xde, 0xc0, 0x22,
0xb5, 0x1d, 0x11, 0xfc, 0xdd, 0x0d, 0x34, 0x8a,
0xc4, btcscript.OP_DATA_65,
0x04, 0x61, 0xcb, 0xdc, 0xc5, 0x40, 0x9f, 0xb4,
0xb4, 0xd4, 0x2b, 0x51, 0xd3, 0x33, 0x81, 0x35,
0x4d, 0x80, 0xe5, 0x50, 0x07, 0x8c, 0xb5, 0x32,
0xa3, 0x4b, 0xfa, 0x2f, 0xcf, 0xde, 0xb7, 0xd7,
0x65, 0x19, 0xae, 0xcc, 0x62, 0x77, 0x0f, 0x5b,
0x0e, 0x4e, 0xf8, 0x55, 0x19, 0x46, 0xd8, 0xa5,
0x40, 0x91, 0x1a, 0xbe, 0x3e, 0x78, 0x54, 0xa2,
0x6f, 0x39, 0xf5, 0x8b, 0x25, 0xc1, 0x53, 0x42,
0xaf, btcscript.OP_2, btcscript.OP_CHECKMULTISIG},
class: btcscript.ScriptMultiSig,
reqSigs: 1,
addrhashes: [][]byte{
[]byte{
0x66, 0x0d, 0x4e, 0xf3, 0xa7, 0x43, 0xe3, 0xe6,
0x96, 0xad, 0x99, 0x03, 0x64, 0xe5, 0x55, 0xc2,
0x71, 0xad, 0x50, 0x4b,
},
[]byte{
0x64, 0x1a, 0xd5, 0x05, 0x1e, 0xdd, 0x97, 0x02,
0x9a, 0x00, 0x3f, 0xe9, 0xef, 0xb2, 0x93, 0x59,
0xfc, 0xee, 0x40, 0x9d,
},
},
},
// from real tx 60a20bd93aa49ab4b28d514ec10b06e1829ce6818ec06cd3aabd013ebcdc4bb1, vout 0
{script: []byte{
btcscript.OP_1, btcscript.OP_DATA_65,
0x04, 0xcc, 0x71, 0xeb, 0x30, 0xd6, 0x53, 0xc0,
0xc3, 0x16, 0x39, 0x90, 0xc4, 0x7b, 0x97, 0x6f,
0x3f, 0xb3, 0xf3, 0x7c, 0xcc, 0xdc, 0xbe, 0xdb,
0x16, 0x9a, 0x1d, 0xfe, 0xf5, 0x8b, 0xbf, 0xbf,
0xaf, 0xf7, 0xd8, 0xa4, 0x73, 0xe7, 0xe2, 0xe6,
0xd3, 0x17, 0xb8, 0x7b, 0xaf, 0xe8, 0xbd, 0xe9,
0x7e, 0x3c, 0xf8, 0xf0, 0x65, 0xde, 0xc0, 0x22,
0xb5, 0x1d, 0x11, 0xfc, 0xdd, 0x0d, 0x34, 0x8a,
0xc4, btcscript.OP_DATA_65,
0x04, 0x61, 0xcb, 0xdc, 0xc5, 0x40, 0x9f, 0xb4,
0xb4, 0xd4, 0x2b, 0x51, 0xd3, 0x33, 0x81, 0x35,
0x4d, 0x80, 0xe5, 0x50, 0x07, 0x8c, 0xb5, 0x32,
0xa3, 0x4b, 0xfa, 0x2f, 0xcf, 0xde, 0xb7, 0xd7,
0x65, 0x19, 0xae, 0xcc, 0x62, 0x77, 0x0f, 0x5b,
0x0e, 0x4e, 0xf8, 0x55, 0x19, 0x46, 0xd8, 0xa5,
0x40, 0x91, 0x1a, 0xbe, 0x3e, 0x78, 0x54, 0xa2,
0x6f, 0x39, 0xf5, 0x8b, 0x25, 0xc1, 0x53, 0x42,
0xaf, btcscript.OP_2, btcscript.OP_CHECKMULTISIG},
class: btcscript.ScriptMultiSig,
reqSigs: 1,
addrhashes: [][]byte{
[]byte{
0x66, 0x0d, 0x4e, 0xf3, 0xa7, 0x43, 0xe3, 0xe6,
0x96, 0xad, 0x99, 0x03, 0x64, 0xe5, 0x55, 0xc2,
0x71, 0xad, 0x50, 0x4b,
},
[]byte{
0x64, 0x1a, 0xd5, 0x05, 0x1e, 0xdd, 0x97, 0x02,
0x9a, 0x00, 0x3f, 0xe9, 0xef, 0xb2, 0x93, 0x59,
0xfc, 0xee, 0x40, 0x9d,
},
},
},
// from real tx: 691dd277dc0e90a462a3d652a1171686de49cf19067cd33c7df0392833fb986a, vout 0
{script: []byte{
btcscript.OP_1, btcscript.OP_DATA_65,
0x1c, 0x22, 0x00, 0x00, 0x73, 0x53, 0x45, 0x58,
0x57, 0x69, 0x6b, 0x69, 0x6c, 0x65, 0x61, 0x6b,
0x73, 0x20, 0x43, 0x61, 0x62, 0x6c, 0x65, 0x67,
0x61, 0x74, 0x65, 0x20, 0x42, 0x61, 0x63, 0x6b,
0x75, 0x70, 0x0a, 0x0a, 0x63, 0x61, 0x62, 0x6c,
0x65, 0x67, 0x61, 0x74, 0x65, 0x2d, 0x32, 0x30,
0x31, 0x30, 0x31, 0x32, 0x30, 0x34, 0x31, 0x38,
0x31, 0x31, 0x2e, 0x37, 0x7a, 0x0a, 0x0a, 0x44,
0x6f, btcscript.OP_DATA_65,
0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x20, 0x74,
0x68, 0x65, 0x20, 0x66, 0x6f, 0x6c, 0x6c, 0x6f,
0x77, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x72, 0x61,
0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e,
0x73, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x53,
0x61, 0x74, 0x6f, 0x73, 0x68, 0x69, 0x20, 0x4e,
0x61, 0x6b, 0x61, 0x6d, 0x6f, 0x74, 0x6f, 0x27,
0x73, 0x20, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f,
0x61, btcscript.OP_DATA_65,
0x64, 0x20, 0x74, 0x6f, 0x6f, 0x6c, 0x20, 0x77,
0x68, 0x69, 0x63, 0x68, 0x0a, 0x63, 0x61, 0x6e,
0x20, 0x62, 0x65, 0x20, 0x66, 0x6f, 0x75, 0x6e,
0x64, 0x20, 0x69, 0x6e, 0x20, 0x74, 0x72, 0x61,
0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e,
0x20, 0x36, 0x63, 0x35, 0x33, 0x63, 0x64, 0x39,
0x38, 0x37, 0x31, 0x31, 0x39, 0x65, 0x66, 0x37,
0x39, 0x37, 0x64, 0x35, 0x61, 0x64, 0x63, 0x63,
0x64, btcscript.OP_3, btcscript.OP_CHECKMULTISIG},
class: btcscript.ScriptMultiSig,
reqSigs: 1,
addrhashes: [][]byte{
[]byte{
0x7c, 0xe9, 0x23, 0x01, 0x00, 0x75, 0x98, 0xc8,
0xb9, 0x31, 0x5b, 0x49, 0x71, 0x21, 0xa0, 0x18,
0x20, 0x90, 0xa3, 0x0c,
},
[]byte{
0x76, 0xf6, 0x95, 0x05, 0xb3, 0x9f, 0xba, 0x56,
0x6d, 0xe8, 0x40, 0x89, 0xda, 0xd8, 0xbd, 0x35,
0x01, 0xec, 0x47, 0xed,
},
[]byte{
0x7b, 0xfb, 0xfe, 0x5d, 0x66, 0x80, 0x22, 0x2e,
0xe6, 0x38, 0xf9, 0x41, 0xdc, 0xfa, 0x61, 0x6d,
0xd4, 0x5f, 0x11, 0xee,
},
},
},
// from real tx: 691dd277dc0e90a462a3d652a1171686de49cf19067cd33c7df0392833fb986a, vout 44
{script: []byte{
btcscript.OP_1, btcscript.OP_DATA_65,
0x34, 0x63, 0x33, 0x65, 0x63, 0x32, 0x35, 0x39,
0x63, 0x37, 0x34, 0x64, 0x61, 0x63, 0x65, 0x36,
0x66, 0x64, 0x30, 0x38, 0x38, 0x62, 0x34, 0x34,
0x63, 0x65, 0x66, 0x38, 0x63, 0x0a, 0x63, 0x36,
0x36, 0x62, 0x63, 0x31, 0x39, 0x39, 0x36, 0x63,
0x38, 0x62, 0x39, 0x34, 0x61, 0x33, 0x38, 0x31,
0x31, 0x62, 0x33, 0x36, 0x35, 0x36, 0x31, 0x38,
0x66, 0x65, 0x31, 0x65, 0x39, 0x62, 0x31, 0x62,
0x35, btcscript.OP_DATA_65,
0x36, 0x63, 0x61, 0x63, 0x63, 0x65, 0x39, 0x39,
0x33, 0x61, 0x33, 0x39, 0x38, 0x38, 0x61, 0x34,
0x36, 0x39, 0x66, 0x63, 0x63, 0x36, 0x64, 0x36,
0x64, 0x61, 0x62, 0x66, 0x64, 0x0a, 0x32, 0x36,
0x36, 0x33, 0x63, 0x66, 0x61, 0x39, 0x63, 0x66,
0x34, 0x63, 0x30, 0x33, 0x63, 0x36, 0x30, 0x39,
0x63, 0x35, 0x39, 0x33, 0x63, 0x33, 0x65, 0x39,
0x31, 0x66, 0x65, 0x64, 0x65, 0x37, 0x30, 0x32,
0x39, btcscript.OP_DATA_33,
0x31, 0x32, 0x33, 0x64, 0x64, 0x34, 0x32, 0x64,
0x32, 0x35, 0x36, 0x33, 0x39, 0x64, 0x33, 0x38,
0x61, 0x36, 0x63, 0x66, 0x35, 0x30, 0x61, 0x62,
0x34, 0x63, 0x64, 0x34, 0x34, 0x0a, 0x00, 0x00,
0x00, btcscript.OP_3, btcscript.OP_CHECKMULTISIG},
class: btcscript.ScriptMultiSig,
reqSigs: 1,
addrhashes: [][]byte{
[]byte{
0xfb, 0xf0, 0x08, 0x0b, 0xc5, 0xf9, 0xd7, 0x2a,
0x9e, 0x64, 0x6f, 0x16, 0x46, 0x46, 0x1c, 0x43,
0x19, 0xc3, 0xb6, 0xd4,
},
[]byte{
0xc6, 0x00, 0xe7, 0x69, 0xc3, 0xae, 0x20, 0xd4,
0xa0, 0x50, 0x08, 0xd1, 0xe3, 0xad, 0x06, 0x33,
0xf2, 0x7b, 0x77, 0xa2,
},
[]byte{
0xad, 0x34, 0x62, 0xcb, 0xa3, 0x5b, 0xee, 0x04,
0xef, 0xd4, 0x20, 0x8c, 0xcd, 0x7f, 0x41, 0xf4,
0xc8, 0x55, 0xf2, 0x73,
},
},
},
}
func TestMultiSigs(t *testing.T) {
for i, s := range multiSigTests {
class, reqSigs, addrhashes, err := btcscript.ScriptToAddrHashes(s.script)
if s.shouldFail != nil {
if err != s.shouldFail {
t.Errorf("MultiSig test %v failed is err [%v] should be [%v]", i, err, s.shouldFail)
}
} else {
if err != nil {
t.Errorf("MultiSig test %v failed err %v", i, err)
} else {
if s.class != class {
t.Errorf("MultiSig test %v class mismatch is [%v] want [%v]", i, class, s.class)
}
if len(addrhashes) != len(s.addrhashes) {
t.Errorf("MultiSig test %v num addrhashes expected is [%d] want [%d]", i, len(addrhashes), len(s.addrhashes))
}
if reqSigs != s.reqSigs {
t.Errorf("MultiSig test %v reqSigs expected is [%d] want [%d]", i, reqSigs, s.reqSigs)
}
for j := 0; j < len(addrhashes); j++ {
if !bytes.Equal(s.addrhashes[j], addrhashes[j]) {
t.Errorf("MultiSig test %v addrhash %d mismatch is [%v] want [%v]", i, j, addrhashes[j], s.addrhashes[j])
}
}
}
}
}
}

View file

@ -49,69 +49,6 @@ func TstSignatureScriptCustomReader(reader io.Reader, tx *btcwire.MsgTx, idx int
hashType, privkey, compress)
}
// Tests for internal error cases in ScriptToAddrHash.
// We pass bad format definitions to ScriptToAddrHash to make sure the internal
// checks work correctly. This is located in internal_test.go and not address.go
// because of the ridiculous amount of internal types/constants that would
// otherwise need to be exported here.
type pkformatTest struct {
name string
format pkformat
script []byte
ty ScriptType
err error
}
var TstPkFormats = []pkformatTest{
pkformatTest{
name: "bad offset",
format: pkformat{
addrtype: ScriptAddr,
parsetype: scrNoAddr,
length: 4,
databytes: []pkbytes{{0, OP_1}, {1, OP_2}, {2,
OP_3}, /* wrong - too long */ {9, OP_4}},
allowmore: true,
},
script: []byte{OP_1, OP_2, OP_3, OP_4},
err: StackErrInvalidAddrOffset,
},
pkformatTest{
name: "Bad parsetype",
format: pkformat{
addrtype: ScriptAddr,
parsetype: 8, // invalid type
length: 4,
databytes: []pkbytes{{0, OP_1}, {1, OP_2}, {2,
OP_3}, /* wrong - too long */ {3, OP_4}},
allowmore: true,
},
script: []byte{OP_1, OP_2, OP_3, OP_4},
err: StackErrInvalidParseType,
},
}
func TestBadPkFormat(t *testing.T) {
for _, test := range TstPkFormats {
ty, addr, err := scriptToAddrHashTemplate(test.script,
[]pkformat{test.format})
if err != nil {
if err != test.err {
t.Errorf("%s got error \"%v\". Was expecrting "+
"\"%v\"", test.name, err, test.err)
}
continue
}
if ty != test.ty {
t.Errorf("%s: unexpected type \"%s\". Wanted \"%s\" (addr %v)",
test.name, ty, test.ty, addr)
continue
}
}
}
// Internal tests for opcodde parsing with bad data templates.
func TestParseOpcode(t *testing.T) {
fakemap := make(map[byte]*opcode)

View file

@ -1200,35 +1200,3 @@ func CalcMultiSigStats(script []byte) (int, int, error) {
numPubKeys := int(pops[len(pops)-2].opcode.value - (OP_1 - 1))
return numPubKeys, numSigs, nil
}
// CalcPkScriptAddrHashes returns the type of script as well as the required
// number of signatures and the hashes of the addresses.
func CalcPkScriptAddrHashes(script []byte) (ScriptClass, int, [][]byte) {
c := GetScriptClass(script)
switch c {
case ScriptHashTy:
fallthrough
case PubKeyTy:
fallthrough
case PubKeyHashTy:
_, addrHash, err := ScriptToAddrHash(script)
if err != nil {
return NonStandardTy, 0, nil
}
return c, 1, [][]byte{addrHash}
case MultiSigTy:
_, reqSigs, addrHashes, err := ScriptToAddrHashes(script)
if err != nil {
return NonStandardTy, 0, nil
}
return c, reqSigs, addrHashes
case NonStandardTy:
fallthrough
case NullDataTy:
fallthrough
default:
return NonStandardTy, 0, nil
}
}