diff --git a/internal_test.go b/internal_test.go index 3240abb0..d957ebb8 100644 --- a/internal_test.go +++ b/internal_test.go @@ -29,24 +29,6 @@ func TstRemoveOpcodeByData(pkscript []byte, data []byte) ([]byte, error) { return unparseScript(pops), nil } -type TstScriptType scriptType - -const ( - TstPubKeyTy TstScriptType = TstScriptType(pubKeyTy) - TstPubKeyHashTy = TstScriptType(pubKeyHashTy) - TstScriptHashTy = TstScriptType(scriptHashTy) - TstMultiSigTy = TstScriptType(multiSigTy) - TstNonStandardTy = TstScriptType(nonStandardTy) -) - -func TstTypeOfScript(script []byte) TstScriptType { - pops, err := parseScript(script) - if err != nil { - return TstNonStandardTy - } - return TstScriptType(typeOfScript(pops)) -} - // TestSetPC allows the test modules to set the program counter to whatever they // want. func (s *Script) TstSetPC(script, off int) { diff --git a/script.go b/script.go index 110154d6..f4e6f555 100644 --- a/script.go +++ b/script.go @@ -125,16 +125,16 @@ const ( MaxScriptElementSize = 520 // Max bytes pushable to the stack. ) -// ScriptType is an enumeration for the list of standard types of script. -type scriptType byte +// ScriptClass is an enumeration for the list of standard types of script. +type ScriptClass byte -// Types of script payment known about in the blockchain. +// Classes of script payment known about in the blockchain. const ( - pubKeyTy scriptType = iota // Pay pubkey. - pubKeyHashTy // Pay pubkey hash. - scriptHashTy // Pay to script hash. - multiSigTy // Multi signature. - nonStandardTy // None of the above. + PubKeyTy ScriptClass = iota // Pay pubkey. + PubKeyHashTy // Pay pubkey hash. + ScriptHashTy // Pay to script hash. + MultiSigTy // Multi signature. + NonStandardTy // None of the above. ) // Script is the virtual machine that executes btcscripts. @@ -237,21 +237,31 @@ func isPushOnly(pops []parsedOpcode) bool { return true } +// GetScriptClass returns the class of the script passed. If the script does not +// parse then NonStandardTy will be returned. +func GetScriptClass(script []byte) ScriptClass { + pops, err := parseScript(script) + if err != nil { + return NonStandardTy + } + return typeOfScript(pops) +} + // scriptType returns the type of the script being inspected from the known // standard types. -func typeOfScript(pops []parsedOpcode) scriptType { +func typeOfScript(pops []parsedOpcode) ScriptClass { // XXX dubious optimisation: order these in order of popularity in the // blockchain if isPubkey(pops) { - return pubKeyTy + return PubKeyTy } else if isPubkeyHash(pops) { - return pubKeyHashTy + return PubKeyHashTy } else if isScriptHash(pops) { - return scriptHashTy + return ScriptHashTy } else if isMultiSig(pops) { - return multiSigTy + return MultiSigTy } - return nonStandardTy + return NonStandardTy } diff --git a/script_test.go b/script_test.go index 3797c9df..0e2751fc 100644 --- a/script_test.go +++ b/script_test.go @@ -1536,7 +1536,7 @@ func TestRemoveOpcodeByDatas(t *testing.T) { type scriptTypeTest struct { name string script []byte - scripttype btcscript.TstScriptType + scripttype btcscript.ScriptClass } var scriptTypeTests = []scriptTypeTest{ @@ -1555,7 +1555,7 @@ var scriptTypeTests = []scriptTypeTest{ 0x12, 0xa3, btcscript.OP_CHECKSIG, }, - scripttype: btcscript.TstPubKeyTy, + scripttype: btcscript.PubKeyTy, }, // tx 599e47a8114fe098103663029548811d2651991b62397e057f0c863c2bc9f9ea scriptTypeTest{ @@ -1570,7 +1570,7 @@ var scriptTypeTests = []scriptTypeTest{ btcscript.OP_EQUALVERIFY, btcscript.OP_CHECKSIG, }, - scripttype: btcscript.TstPubKeyHashTy, + scripttype: btcscript.PubKeyHashTy, }, // part of tx 6d36bc17e947ce00bb6f12f8e7a56a1585c5a36188ffa2b05e10b4743273a74b // codeseparator parts have been elided. (bitcoind's checks for multisig @@ -1587,7 +1587,7 @@ var scriptTypeTests = []scriptTypeTest{ btcscript.OP_TRUE, btcscript.OP_CHECK_MULTISIG, }, - scripttype: btcscript.TstMultiSigTy, + scripttype: btcscript.MultiSigTy, }, // tx e5779b9e78f9650debc2893fd9636d827b26b4ddfa6a8172fe8708c924f5c39d // P2SH @@ -1601,7 +1601,7 @@ var scriptTypeTests = []scriptTypeTest{ 0xae, 0x88, btcscript.OP_EQUAL, }, - scripttype: btcscript.TstScriptHashTy, + scripttype: btcscript.ScriptHashTy, }, // The next few are almost multisig (it is the more complex script type) // but with various changes to make it fail. @@ -1618,7 +1618,7 @@ var scriptTypeTests = []scriptTypeTest{ btcscript.OP_TRUE, btcscript.OP_CHECK_MULTISIG, }, - scripttype: btcscript.TstNonStandardTy, + scripttype: btcscript.NonStandardTy, }, scriptTypeTest{ name: "strange 2", @@ -1629,7 +1629,7 @@ var scriptTypeTests = []scriptTypeTest{ btcscript.OP_TRUE, btcscript.OP_CHECK_MULTISIG, }, - scripttype: btcscript.TstNonStandardTy, + scripttype: btcscript.NonStandardTy, }, scriptTypeTest{ name: "strange 3", @@ -1649,7 +1649,7 @@ var scriptTypeTests = []scriptTypeTest{ // No number. btcscript.OP_CHECK_MULTISIG, }, - scripttype: btcscript.TstNonStandardTy, + scripttype: btcscript.NonStandardTy, }, scriptTypeTest{ name: "strange 4", @@ -1664,7 +1664,7 @@ var scriptTypeTests = []scriptTypeTest{ btcscript.OP_TRUE, btcscript.OP_CHECKMULTISIGVERIFY, }, - scripttype: btcscript.TstNonStandardTy, + scripttype: btcscript.NonStandardTy, }, scriptTypeTest{ name: "strange 5", @@ -1673,19 +1673,19 @@ var scriptTypeTests = []scriptTypeTest{ btcscript.OP_TRUE, btcscript.OP_CHECK_MULTISIG, }, - scripttype: btcscript.TstNonStandardTy, + scripttype: btcscript.NonStandardTy, }, scriptTypeTest{ name: "doesn't parse", script: []byte{ btcscript.OP_DATA_5, 0x1, 0x2, 0x3, 0x4, }, - scripttype: btcscript.TstNonStandardTy, + scripttype: btcscript.NonStandardTy, }, } func testScriptType(t *testing.T, test *scriptTypeTest) { - scripttype := btcscript.TstTypeOfScript(test.script) + scripttype := btcscript.GetScriptClass(test.script) if scripttype != test.scripttype { t.Errorf("%s: expected %s got %s", test.name, test.scripttype, scripttype) @@ -1700,7 +1700,7 @@ func TestScriptTypes(t *testing.T) { func TestIsPayToScriptHash(t *testing.T) { for _, test := range scriptTypeTests { - shouldBe := (test.scripttype == btcscript.TstScriptHashTy) + shouldBe := (test.scripttype == btcscript.ScriptHashTy) p2sh := btcscript.IsPayToScriptHash(test.script) if p2sh != shouldBe { t.Errorf("%s: epxected p2sh %v, got %v", test.name, diff --git a/test_coverage.txt b/test_coverage.txt index 9556b1bd..cb1626db 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -8,131 +8,132 @@ github.com/conformal/btcscript/script.go GetPreciseSigOpCount 100.00% (17/17) github.com/conformal/btcscript/stack.go fromInt 100.00% (14/14) 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.bytes 100.00% (12/12) github.com/conformal/btcscript/stack.go Stack.nipN 100.00% (12/12) github.com/conformal/btcscript/opcode.go parsedOpcode.print 100.00% (12/12) +github.com/conformal/btcscript/opcode.go parsedOpcode.bytes 100.00% (12/12) github.com/conformal/btcscript/opcode.go opcodeIf 100.00% (11/11) github.com/conformal/btcscript/opcode.go opcodeNotIf 100.00% (11/11) -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/opcode.go opcodeNumEqual 100.00% (10/10) -github.com/conformal/btcscript/opcode.go opcodeBoolOr 100.00% (10/10) +github.com/conformal/btcscript/stack.go Stack.Tuck 100.00% (10/10) +github.com/conformal/btcscript/opcode.go opcodeMin 100.00% (10/10) +github.com/conformal/btcscript/opcode.go opcodeMax 100.00% (10/10) +github.com/conformal/btcscript/opcode.go opcodeGreaterThanOrEqual 100.00% (10/10) +github.com/conformal/btcscript/opcode.go opcodeLessThan 100.00% (10/10) github.com/conformal/btcscript/opcode.go opcodeBoolAnd 100.00% (10/10) github.com/conformal/btcscript/opcode.go opcodeNumNotEqual 100.00% (10/10) -github.com/conformal/btcscript/opcode.go opcodeMin 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 opcodeGreaterThan 100.00% (10/10) github.com/conformal/btcscript/script.go getSigOpCount 100.00% (10/10) +github.com/conformal/btcscript/opcode.go opcodeLessThanOrEqual 100.00% (10/10) +github.com/conformal/btcscript/opcode.go opcodeBoolOr 100.00% (10/10) +github.com/conformal/btcscript/script.go DisasmString 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/stack.go Stack.RotN 100.00% (9/9) github.com/conformal/btcscript/opcode.go opcodeEqual 100.00% (8/8) github.com/conformal/btcscript/stack.go Stack.DupN 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.DropN 100.00% (7/7) -github.com/conformal/btcscript/opcode.go opcodePick 100.00% (7/7) github.com/conformal/btcscript/opcode.go opcodeRoll 100.00% (7/7) -github.com/conformal/btcscript/opcode.go opcode0NotEqual 100.00% (7/7) github.com/conformal/btcscript/opcode.go opcodeNot 100.00% (7/7) -github.com/conformal/btcscript/script.go typeOfScript 100.00% (6/6) -github.com/conformal/btcscript/opcode.go opcodeVerify 100.00% (6/6) +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/stack.go Stack.DropN 100.00% (7/7) github.com/conformal/btcscript/opcode.go opcodeIfDup 100.00% (6/6) github.com/conformal/btcscript/opcode.go parsedOpcode.conditional 100.00% (6/6) +github.com/conformal/btcscript/script.go typeOfScript 100.00% (6/6) +github.com/conformal/btcscript/opcode.go opcodeVerify 100.00% (6/6) github.com/conformal/btcscript/opcode.go opcodeElse 100.00% (6/6) -github.com/conformal/btcscript/stack.go Stack.RollN 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/script.go Script.validPC 100.00% (5/5) -github.com/conformal/btcscript/script.go removeOpcode 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 opcodeToAltStack 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 Script.validPC 100.00% (5/5) +github.com/conformal/btcscript/opcode.go opcodeFromAltStack 100.00% (5/5) +github.com/conformal/btcscript/opcode.go opcodeToAltStack 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 opcodeSha256 100.00% (5/5) github.com/conformal/btcscript/opcode.go opcodeSha1 100.00% (5/5) github.com/conformal/btcscript/opcode.go opcodeRipemd160 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/stack.go Stack.PickN 100.00% (5/5) -github.com/conformal/btcscript/stack.go Stack.PeekBool 100.00% (4/4) +github.com/conformal/btcscript/stack.go Stack.RollN 100.00% (5/5) +github.com/conformal/btcscript/opcode.go opcodeSize 100.00% (5/5) +github.com/conformal/btcscript/stack.go asBool 100.00% (4/4) github.com/conformal/btcscript/opcode.go opcodeEndif 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) -github.com/conformal/btcscript/stack.go asBool 100.00% (4/4) github.com/conformal/btcscript/opcode.go opcodeCheckMultiSigVerify 100.00% (4/4) github.com/conformal/btcscript/stack.go Stack.PopBool 100.00% (4/4) 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/stack.go Stack.PeekBool 100.00% (4/4) github.com/conformal/btcscript/script.go unparseScript 100.00% (4/4) github.com/conformal/btcscript/script.go IsPayToScriptHash 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/script.go getStack 100.00% (4/4) github.com/conformal/btcscript/script.go GetSigOpCount 100.00% (4/4) -github.com/conformal/btcscript/stack.go fromBool 100.00% (3/3) -github.com/conformal/btcscript/script.go scriptUInt32 100.00% (3/3) -github.com/conformal/btcscript/address.go ScriptType.String 100.00% (3/3) -github.com/conformal/btcscript/script.go scriptUInt8 100.00% (3/3) 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 scriptUInt32 100.00% (3/3) +github.com/conformal/btcscript/stack.go fromBool 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/opcode.go opcodePushData 100.00% (2/2) -github.com/conformal/btcscript/opcode.go opcodeCodeSeparator 100.00% (2/2) -github.com/conformal/btcscript/opcode.go calcHash 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeFalse 100.00% (2/2) +github.com/conformal/btcscript/opcode.go opcodeCodeSeparator 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeDepth 100.00% (2/2) -github.com/conformal/btcscript/address.go ScriptToAddress 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/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 opcodeN 100.00% (2/2) +github.com/conformal/btcscript/address.go ScriptToAddress 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcode1Negate 100.00% (2/2) -github.com/conformal/btcscript/script.go Script.SetStack 100.00% (1/1) github.com/conformal/btcscript/script.go isPubkey 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 Script.GetAltStack 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.SetStack 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeReserved 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 opcodeDisabled 100.00% (1/1) github.com/conformal/btcscript/script.go parseScript 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/opcode.go opcodeReturn 100.00% (1/1) +github.com/conformal/btcscript/opcode.go calcHash160 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 opcode2Swap 100.00% (1/1) -github.com/conformal/btcscript/stack.go Stack.PopByteArray 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode2Rot 100.00% (1/1) -github.com/conformal/btcscript/opcode.go calcHash160 100.00% (1/1) +github.com/conformal/btcscript/stack.go Stack.PopByteArray 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode2Over 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode3Dup 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/opcode.go opcodeNop 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.GetStack 100.00% (1/1) +github.com/conformal/btcscript/log.go DisableLog 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 opcodeInvalid 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeTuck 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.GetStack 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeSwap 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/opcode.go opcodeDup 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeDrop 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcode2Swap 100.00% (1/1) github.com/conformal/btcscript/script.go Script.disasm 100.00% (1/1) github.com/conformal/btcscript/log.go UseLogger 100.00% (1/1) github.com/conformal/btcscript/log.go init 100.00% (1/1) -github.com/conformal/btcscript/log.go DisableLog 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeTuck 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeCheckMultiSig 98.21% (55/56) github.com/conformal/btcscript/opcode.go opcodeCheckSig 96.15% (25/26) github.com/conformal/btcscript/script.go Script.CheckErrorCondition 78.57% (11/14) @@ -141,5 +142,5 @@ github.com/conformal/btcscript/script.go Script.calcScriptHash 71.43% (25/35) 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/log.go logClosure.String 0.00% (0/1) -github.com/conformal/btcscript -------------------------- 96.35% (898/932) +github.com/conformal/btcscript -------------------------- 96.37% (902/936)