From 3f52f559eb6aa87e87e0249d471b1f70c88dcc20 Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Thu, 2 Jan 2014 13:31:00 -0500 Subject: [PATCH] Added tests for multisig scripts and fixed comments Fixed up bad function comment headers Added a small set of tests for the ScriptToAddrHashes function to test functionality of a couple real multisig cases as well as error checking --- address.go | 7 +- address_test.go | 196 ++++++++++++++++++++++++++++++++++++++++++++++-- script.go | 2 +- 3 files changed, 195 insertions(+), 10 deletions(-) diff --git a/address.go b/address.go index 14b4fa78..5407ac12 100644 --- a/address.go +++ b/address.go @@ -176,11 +176,12 @@ func scriptToAddrHashTemplate(script []byte, validformats []pkformat) (ScriptTyp return format.addrtype, addrhash, nil } -// ScriptToAddrHashes extracts multiply 20-byte public key hash and the type out of a PkScript -func ScriptToMultiSigAddresses(script []byte) (ScriptType, int, [][]byte, error) { +// ScriptToAddrHashes extracts multiple 20-byte public key hashes +// from a MultiSig script. +func ScriptToAddrHashes(script []byte) (ScriptType, int, [][]byte, error) { pops, err := parseScript(script) if err != nil { - return ScriptUnknown, 0, nil, err + return ScriptUnknown, 0, nil, StackErrUnknownAddress } if !isMultiSig(pops) { diff --git a/address_test.go b/address_test.go index 048f8a9f..d8da48d1 100644 --- a/address_test.go +++ b/address_test.go @@ -162,13 +162,15 @@ var addressTests = []addressTest{ class: btcscript.ScriptStrange, }, {script: []byte{btcscript.OP_HASH160, btcscript.OP_DATA_20, - 0x02, 0x40, 0x05, 0xc9, 0x45, 0xd8, 0x6a, 0xc6, - 0xb0, 0x1f, 0xb0, 0x42, 0x58, 0x34, 0x5a, 0xbe, - 0xa7, 0xa8, 0x45, 0xbd, - btcscript.OP_EQUAL, // note this isn't a real tx + 0x63, 0xbc, 0xc5, 0x65, 0xf9, 0xe6, 0x8e, 0xe0, + 0x18, 0x9d, 0xd5, 0xcc, 0x67, 0xf1, 0xb0, 0xe5, + 0xf0, 0x2f, 0x45, 0xcb, + btcscript.OP_EQUAL, }, - addrhash: nil, - class: btcscript.ScriptStrange, + 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, @@ -257,3 +259,185 @@ func TestStringify(t *testing.T) { } } } + +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_CHECK_MULTISIG}, + 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_CHECK_MULTISIG}, + 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_CHECK_MULTISIG}, + 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, + }, + }, + }, +} + +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]) + } + } + } + } + + } +} diff --git a/script.go b/script.go index bd7dcf9a..0e06e00d 100644 --- a/script.go +++ b/script.go @@ -1165,7 +1165,7 @@ func CalcPkScriptAddrHashes(script []byte) (ScriptClass, int, [][]byte) { return c, 1, [][]byte{addrHash} case MultiSigTy: - _, reqSigs, addrHashes, err := ScriptToMultiSigAddresses(script) + _, reqSigs, addrHashes, err := ScriptToAddrHashes(script) if err != nil { return NonStandardTy, 0, nil }