// 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_CHECK_MULTISIG, // 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, 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 }, addrhash: nil, class: btcscript.ScriptStrange, }, {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) } } }