diff --git a/script_test.go b/script_test.go index d5f45679..690c15f1 100644 --- a/script_test.go +++ b/script_test.go @@ -9,6 +9,7 @@ import ( "crypto/ecdsa" "github.com/conformal/btcec" "github.com/conformal/btcscript" + "github.com/conformal/btcutil" "github.com/conformal/btcwire" "math/big" "testing" @@ -2786,3 +2787,93 @@ func TestStringifyClass(t *testing.T) { } } } + +// bogusAddress implements the btcutil.Address interface so the tests can ensure +// unsupported address types are handled properly. +type bogusAddress struct{} + +// EncodeAddress simply returns an empty string. It exists to satsify the +// btcutil.Address interface. +func (b *bogusAddress) EncodeAddress() string { + return "" +} + +// ScriptAddress simply returns an empty byte slice. It exists to satsify the +// btcutil.Address interface. +func (b *bogusAddress) ScriptAddress() []byte { + return []byte{} +} + +func TestPayToAddrScript(t *testing.T) { + // 1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX + p2pkhMain, err := btcutil.NewAddressPubKeyHash([]byte{ + 0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, + 0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84, + }, btcwire.MainNet) + if err != nil { + t.Errorf("Unable to create public key hash address: %v", err) + return + } + + // Taken from transaction: + // b0539a45de13b3e0403909b8bd1a555b8cbe45fd4e3f3fda76f3a5f52835c29d + p2shMain, _ := btcutil.NewAddressScriptHashFromHash([]byte{ + 0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37, + 0xc0, 0x51, 0x99, 0x29, 0x01, 0x9e, 0xf8, 0x6e, 0xb5, 0xb4, + }, btcwire.MainNet) + if err != nil { + t.Errorf("Unable to create script hash address: %v", err) + return + } + + tests := []struct { + in btcutil.Address + expected []byte + err error + }{ + // pay-to-pubkey-hash address on mainnet + { + p2pkhMain, + []byte{ + 0x76, 0xa9, 0x14, 0xe3, 0x4c, 0xce, 0x70, 0xc8, + 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xc5, 0x4c, 0xe7, + 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84, 0x88, + 0xac, + }, + nil, + }, + // pay-to-script-hash address on mainnet + { + p2shMain, + []byte{ + 0xa9, 0x14, 0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, + 0xef, 0xa8, 0x4c, 0x37, 0xc0, 0x51, 0x99, 0x29, + 0x01, 0x9e, 0xf8, 0x6e, 0xb5, 0xb4, 0x87, + }, + nil, + }, + + // Supported address types with nil pointers. + {(*btcutil.AddressPubKeyHash)(nil), []byte{}, btcscript.ErrUnsupportedAddress}, + {(*btcutil.AddressScriptHash)(nil), []byte{}, btcscript.ErrUnsupportedAddress}, + + // Unsupported address type. + {&bogusAddress{}, []byte{}, btcscript.ErrUnsupportedAddress}, + } + + t.Logf("Running %d tests", len(tests)) + for i, test := range tests { + pkScript, err := btcscript.PayToAddrScript(test.in) + if err != test.err { + t.Errorf("PayToAddrScript #%d unexpected error - "+ + "got %v, want %v", i, err, test.err) + continue + } + + if !bytes.Equal(pkScript, test.expected) { + t.Errorf("PayToAddrScript #%d got: %x\nwant: %x", + i, pkScript, test.expected) + continue + } + } +} diff --git a/test_coverage.txt b/test_coverage.txt index b57291c4..8ec4a9c4 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -1,25 +1,25 @@ -github.com/conformal/btcscript/address.go scriptToAddrHashTemplate 100.00% (45/45) +github.com/conformal/btcscript/address.go scriptToAddrHashTemplate 100.00% (44/44) github.com/conformal/btcscript/script.go Script.Step 100.00% (37/37) github.com/conformal/btcscript/script.go parseScriptTemplate 100.00% (30/30) github.com/conformal/btcscript/script.go CalcScriptInfo 100.00% (25/25) github.com/conformal/btcscript/opcode.go parsedOpcode.bytes 100.00% (23/23) -github.com/conformal/btcscript/script.go sigDER 100.00% (22/22) github.com/conformal/btcscript/script.go NewScript 100.00% (21/21) github.com/conformal/btcscript/stack.go asInt 100.00% (21/21) -github.com/conformal/btcscript/script.go signatureScriptCustomReader 100.00% (15/15) +github.com/conformal/btcscript/script.go signatureScriptCustomReader 100.00% (16/16) github.com/conformal/btcscript/stack.go Stack.nipN 100.00% (14/14) github.com/conformal/btcscript/stack.go fromInt 100.00% (14/14) -github.com/conformal/btcscript/script.go isMultiSig 100.00% (13/13) -github.com/conformal/btcscript/opcode.go opcodeWithin 100.00% (13/13) github.com/conformal/btcscript/script.go GetPreciseSigOpCount 100.00% (13/13) +github.com/conformal/btcscript/opcode.go opcodeWithin 100.00% (13/13) +github.com/conformal/btcscript/script.go isMultiSig 100.00% (13/13) github.com/conformal/btcscript/opcode.go parsedOpcode.print 100.00% (12/12) -github.com/conformal/btcscript/opcode.go opcodeNotIf 100.00% (11/11) github.com/conformal/btcscript/opcode.go opcodeIf 100.00% (11/11) -github.com/conformal/btcscript/opcode.go opcodeMin 100.00% (10/10) +github.com/conformal/btcscript/opcode.go opcodeNotIf 100.00% (11/11) +github.com/conformal/btcscript/opcode.go opcodeGreaterThanOrEqual 100.00% (10/10) +github.com/conformal/btcscript/opcode.go opcodeMax 100.00% (10/10) github.com/conformal/btcscript/script.go getSigOpCount 100.00% (10/10) github.com/conformal/btcscript/stack.go Stack.Tuck 100.00% (10/10) -github.com/conformal/btcscript/opcode.go opcodeMax 100.00% (10/10) +github.com/conformal/btcscript/opcode.go opcodeMin 100.00% (10/10) github.com/conformal/btcscript/opcode.go opcodeBoolAnd 100.00% (10/10) github.com/conformal/btcscript/opcode.go opcodeBoolOr 100.00% (10/10) github.com/conformal/btcscript/opcode.go opcodeNumEqual 100.00% (10/10) @@ -27,47 +27,47 @@ github.com/conformal/btcscript/opcode.go opcodeNumNotEqual 100.00% (10/10) github.com/conformal/btcscript/opcode.go opcodeLessThan 100.00% (10/10) github.com/conformal/btcscript/opcode.go opcodeGreaterThan 100.00% (10/10) github.com/conformal/btcscript/opcode.go opcodeLessThanOrEqual 100.00% (10/10) -github.com/conformal/btcscript/opcode.go opcodeGreaterThanOrEqual 100.00% (10/10) -github.com/conformal/btcscript/stack.go Stack.RotN 100.00% (9/9) -github.com/conformal/btcscript/stack.go Stack.OverN 100.00% (9/9) github.com/conformal/btcscript/stack.go Stack.SwapN 100.00% (9/9) github.com/conformal/btcscript/script.go DisasmString 100.00% (9/9) -github.com/conformal/btcscript/opcode.go opcodeEqual 100.00% (8/8) -github.com/conformal/btcscript/opcode.go opcodeAdd 100.00% (8/8) -github.com/conformal/btcscript/opcode.go opcodeSub 100.00% (8/8) +github.com/conformal/btcscript/stack.go Stack.OverN 100.00% (9/9) +github.com/conformal/btcscript/stack.go Stack.RotN 100.00% (9/9) github.com/conformal/btcscript/stack.go Stack.DupN 100.00% (8/8) -github.com/conformal/btcscript/script.go unparseScript 100.00% (7/7) +github.com/conformal/btcscript/opcode.go opcodeSub 100.00% (8/8) +github.com/conformal/btcscript/opcode.go opcodeEqual 100.00% (8/8) +github.com/conformal/btcscript/script.go PayToAddrScript 100.00% (8/8) +github.com/conformal/btcscript/opcode.go opcodeAdd 100.00% (8/8) github.com/conformal/btcscript/opcode.go opcodeRoll 100.00% (7/7) -github.com/conformal/btcscript/opcode.go opcodeNot 100.00% (7/7) -github.com/conformal/btcscript/opcode.go opcode0NotEqual 100.00% (7/7) -github.com/conformal/btcscript/opcode.go opcodePick 100.00% (7/7) -github.com/conformal/btcscript/script.go typeOfScript 100.00% (7/7) github.com/conformal/btcscript/stack.go Stack.DropN 100.00% (7/7) -github.com/conformal/btcscript/opcode.go opcodeElse 100.00% (6/6) +github.com/conformal/btcscript/script.go unparseScript 100.00% (7/7) +github.com/conformal/btcscript/opcode.go opcode0NotEqual 100.00% (7/7) +github.com/conformal/btcscript/script.go typeOfScript 100.00% (7/7) +github.com/conformal/btcscript/opcode.go opcodeNot 100.00% (7/7) +github.com/conformal/btcscript/opcode.go opcodePick 100.00% (7/7) github.com/conformal/btcscript/opcode.go opcodeVerify 100.00% (6/6) -github.com/conformal/btcscript/opcode.go opcodeEndif 100.00% (6/6) -github.com/conformal/btcscript/opcode.go parsedOpcode.conditional 100.00% (6/6) github.com/conformal/btcscript/opcode.go opcodeIfDup 100.00% (6/6) -github.com/conformal/btcscript/opcode.go opcodeFromAltStack 100.00% (5/5) -github.com/conformal/btcscript/stack.go Stack.RollN 100.00% (5/5) -github.com/conformal/btcscript/stack.go Stack.PickN 100.00% (5/5) -github.com/conformal/btcscript/opcode.go opcodeSha1 100.00% (5/5) -github.com/conformal/btcscript/script.go Script.validPC 100.00% (5/5) -github.com/conformal/btcscript/opcode.go opcodeHash256 100.00% (5/5) -github.com/conformal/btcscript/opcode.go opcodeHash160 100.00% (5/5) +github.com/conformal/btcscript/opcode.go parsedOpcode.conditional 100.00% (6/6) +github.com/conformal/btcscript/opcode.go opcodeEndif 100.00% (6/6) +github.com/conformal/btcscript/opcode.go opcodeElse 100.00% (6/6) github.com/conformal/btcscript/opcode.go opcodeSha256 100.00% (5/5) -github.com/conformal/btcscript/opcode.go opcodeSize 100.00% (5/5) -github.com/conformal/btcscript/opcode.go opcodeRipemd160 100.00% (5/5) +github.com/conformal/btcscript/opcode.go opcodeHash160 100.00% (5/5) github.com/conformal/btcscript/opcode.go opcode1Add 100.00% (5/5) github.com/conformal/btcscript/opcode.go opcode1Sub 100.00% (5/5) github.com/conformal/btcscript/opcode.go opcodeNegate 100.00% (5/5) github.com/conformal/btcscript/opcode.go opcodeAbs 100.00% (5/5) +github.com/conformal/btcscript/opcode.go opcodeHash256 100.00% (5/5) +github.com/conformal/btcscript/script.go Script.validPC 100.00% (5/5) +github.com/conformal/btcscript/opcode.go opcodeRipemd160 100.00% (5/5) +github.com/conformal/btcscript/stack.go Stack.PickN 100.00% (5/5) +github.com/conformal/btcscript/stack.go Stack.RollN 100.00% (5/5) +github.com/conformal/btcscript/opcode.go opcodeFromAltStack 100.00% (5/5) github.com/conformal/btcscript/script.go removeOpcode 100.00% (5/5) +github.com/conformal/btcscript/script.go removeOpcodeByData 100.00% (5/5) github.com/conformal/btcscript/script.go Script.DisasmScript 100.00% (5/5) github.com/conformal/btcscript/opcode.go parsedOpcode.exec 100.00% (5/5) -github.com/conformal/btcscript/script.go removeOpcodeByData 100.00% (5/5) +github.com/conformal/btcscript/opcode.go opcodeSize 100.00% (5/5) github.com/conformal/btcscript/opcode.go opcodeToAltStack 100.00% (5/5) -github.com/conformal/btcscript/stack.go Stack.PeekBool 100.00% (4/4) +github.com/conformal/btcscript/opcode.go opcodeSha1 100.00% (5/5) +github.com/conformal/btcscript/script.go isNullData 100.00% (4/4) github.com/conformal/btcscript/opcode.go opcodePushData 100.00% (4/4) github.com/conformal/btcscript/opcode.go opcodeEqualVerify 100.00% (4/4) github.com/conformal/btcscript/opcode.go opcodeNumEqualVerify 100.00% (4/4) @@ -78,78 +78,82 @@ github.com/conformal/btcscript/stack.go Stack.PeekByteArray 100.00% (4/4) github.com/conformal/btcscript/stack.go Stack.PeekInt 100.00% (4/4) github.com/conformal/btcscript/stack.go Stack.PopInt 100.00% (4/4) github.com/conformal/btcscript/script.go IsPayToScriptHash 100.00% (4/4) -github.com/conformal/btcscript/script.go isNullData 100.00% (4/4) github.com/conformal/btcscript/script.go isPushOnly 100.00% (4/4) github.com/conformal/btcscript/script.go GetScriptClass 100.00% (4/4) github.com/conformal/btcscript/script.go Script.curPC 100.00% (4/4) github.com/conformal/btcscript/script.go Script.DisasmPC 100.00% (4/4) +github.com/conformal/btcscript/stack.go Stack.PeekBool 100.00% (4/4) github.com/conformal/btcscript/script.go getStack 100.00% (4/4) -github.com/conformal/btcscript/script.go scriptUInt8 100.00% (3/3) github.com/conformal/btcscript/script.go scriptUInt32 100.00% (3/3) -github.com/conformal/btcscript/script.go ScriptClass.String 100.00% (3/3) -github.com/conformal/btcscript/address.go ScriptType.String 100.00% (3/3) -github.com/conformal/btcscript/script.go setStack 100.00% (3/3) -github.com/conformal/btcscript/script.go scriptUInt16 100.00% (3/3) github.com/conformal/btcscript/stack.go fromBool 100.00% (3/3) -github.com/conformal/btcscript/stack.go Stack.NipN 100.00% (2/2) -github.com/conformal/btcscript/stack.go Stack.Depth 100.00% (2/2) -github.com/conformal/btcscript/opcode.go calcHash 100.00% (2/2) -github.com/conformal/btcscript/address.go ScriptToAddrHash 100.00% (2/2) -github.com/conformal/btcscript/opcode.go opcode1Negate 100.00% (2/2) -github.com/conformal/btcscript/opcode.go opcodeDepth 100.00% (2/2) +github.com/conformal/btcscript/script.go setStack 100.00% (3/3) +github.com/conformal/btcscript/address.go ScriptType.String 100.00% (3/3) +github.com/conformal/btcscript/script.go scriptUInt16 100.00% (3/3) +github.com/conformal/btcscript/script.go scriptUInt8 100.00% (3/3) +github.com/conformal/btcscript/script.go ScriptClass.String 100.00% (3/3) +github.com/conformal/btcscript/script.go PayToScriptHashScript 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeCodeSeparator 100.00% (2/2) -github.com/conformal/btcscript/script.go PayToPubKeyHashScript 100.00% (2/2) -github.com/conformal/btcscript/script.go GetSigOpCount 100.00% (2/2) -github.com/conformal/btcscript/opcode.go opcodeFalse 100.00% (2/2) +github.com/conformal/btcscript/stack.go Stack.Depth 100.00% (2/2) +github.com/conformal/btcscript/stack.go Stack.NipN 100.00% (2/2) +github.com/conformal/btcscript/opcode.go calcHash 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeN 100.00% (2/2) -github.com/conformal/btcscript/script.go isPubkeyHash 100.00% (1/1) -github.com/conformal/btcscript/script.go isScriptHash 100.00% (1/1) -github.com/conformal/btcscript/script.go SignatureScript 100.00% (1/1) -github.com/conformal/btcscript/log.go DisableLog 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeDisabled 100.00% (1/1) -github.com/conformal/btcscript/log.go init 100.00% (1/1) -github.com/conformal/btcscript/script.go parseScript 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcode1Negate 100.00% (2/2) +github.com/conformal/btcscript/address.go ScriptToAddrHash 100.00% (2/2) +github.com/conformal/btcscript/opcode.go opcodeDepth 100.00% (2/2) +github.com/conformal/btcscript/script.go GetSigOpCount 100.00% (2/2) +github.com/conformal/btcscript/script.go PayToPubKeyHashScript 100.00% (2/2) +github.com/conformal/btcscript/opcode.go opcodeFalse 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeInvalid 100.00% (1/1) +github.com/conformal/btcscript/script.go parseScript 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeReserved 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeDisabled 100.00% (1/1) github.com/conformal/btcscript/opcode.go init 100.00% (1/1) -github.com/conformal/btcscript/log.go newLogClosure 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.disasm 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.GetAltStack 100.00% (1/1) -github.com/conformal/btcscript/stack.go Stack.PopByteArray 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeNip 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.SetAltStack 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeDup 100.00% (1/1) +github.com/conformal/btcscript/stack.go Stack.PopByteArray 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeDrop 100.00% (1/1) +github.com/conformal/btcscript/log.go DisableLog 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode2Swap 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode2Rot 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode2Over 100.00% (1/1) -github.com/conformal/btcscript/log.go UseLogger 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode3Dup 100.00% (1/1) +github.com/conformal/btcscript/log.go init 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode2Dup 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode2Drop 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeReturn 100.00% (1/1) github.com/conformal/btcscript/stack.go Stack.PushBool 100.00% (1/1) github.com/conformal/btcscript/script.go Script.subScript 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeReturn 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeNop 100.00% (1/1) +github.com/conformal/btcscript/script.go isPubkeyHash 100.00% (1/1) +github.com/conformal/btcscript/script.go isScriptHash 100.00% (1/1) +github.com/conformal/btcscript/script.go SignatureScript 100.00% (1/1) github.com/conformal/btcscript/script.go isPubkey 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeOver 100.00% (1/1) +github.com/conformal/btcscript/log.go newLogClosure 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.disasm 100.00% (1/1) github.com/conformal/btcscript/script.go Script.GetStack 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.SetStack 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.GetAltStack 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.SetAltStack 100.00% (1/1) github.com/conformal/btcscript/opcode.go calcHash160 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeTuck 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeSwap 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.SetStack 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeRot 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeOver 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeNip 100.00% (1/1) github.com/conformal/btcscript/stack.go Stack.PushByteArray 100.00% (1/1) github.com/conformal/btcscript/stack.go Stack.PushInt 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeRot 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeCheckMultiSig 98.28% (57/58) +github.com/conformal/btcscript/address.go ScriptToAddrHashes 90.91% (10/11) github.com/conformal/btcscript/opcode.go opcodeCheckSig 90.62% (29/32) github.com/conformal/btcscript/script.go expectedInputs 85.71% (6/7) github.com/conformal/btcscript/script.go calcScriptHash 82.05% (32/39) github.com/conformal/btcscript/script.go Script.CheckErrorCondition 78.57% (11/14) github.com/conformal/btcscript/opcode.go opcodeCheckSigVerify 75.00% (3/4) github.com/conformal/btcscript/script.go Script.Execute 44.44% (8/18) -github.com/conformal/btcscript/log.go SetLogWriter 0.00% (0/7) +github.com/conformal/btcscript/script.go CalcPkScriptAddrHashes 0.00% (0/15) +github.com/conformal/btcscript/log.go SetLogWriter 0.00% (0/10) +github.com/conformal/btcscript/script.go CalcMultiSigStats 0.00% (0/8) github.com/conformal/btcscript/script.go IsPushOnlyScript 0.00% (0/4) github.com/conformal/btcscript/log.go logClosure.String 0.00% (0/1) -github.com/conformal/btcscript --------------------------- 96.33% (998/1036) +github.com/conformal/btcscript/log.go UseLogger 0.00% (0/1) +github.com/conformal/btcscript --------------------------- 93.78% (995/1061)