diff --git a/txscript/opcode.go b/txscript/opcode.go index 8ad42ae030..1216772095 100644 --- a/txscript/opcode.go +++ b/txscript/opcode.go @@ -1819,3 +1819,18 @@ func opcodeCheckMultiSigVerify(op *parsedOpcode, vm *Engine) error { } return err } + +// OpcodeByName is a map that can be used to lookup an opcode by its +// human-readable name (OP_CHECKMULTISIG, OP_CHECKSIG, etc). +var OpcodeByName = make(map[string]byte) + +func init() { + // Initialize the opcode name to value map using the contents of the + // opcode array. Also add entries for "OP_FALSE" and "OP_TRUE" since + // they are aliases for "OP_0" and "OP_1", respectively. + for _, op := range opcodeArray { + OpcodeByName[op.name] = op.value + } + OpcodeByName["OP_FALSE"] = OP_FALSE + OpcodeByName["OP_TRUE"] = OP_TRUE +} diff --git a/txscript/reference_test.go b/txscript/reference_test.go index 65704af2f2..db88c38beb 100644 --- a/txscript/reference_test.go +++ b/txscript/reference_test.go @@ -46,7 +46,7 @@ func parseHex(tok string) ([]byte, error) { // parseShortForm parses a string as as used in the Bitcoin Core reference tests // into the script it came from. func parseShortForm(script string) ([]byte, error) { - ops := make(map[string]*opcode) + ops := make(map[string]byte) // the format used for these tests is pretty simple if ad-hoc: // - opcodes other than the push opcodes and unknown are present as @@ -56,16 +56,15 @@ func parseShortForm(script string) ([]byte, error) { // 0x14 is OP_DATA_20) // - single quoted strings are pushed as data. // - anything else is an error. - for i := range opcodeArray { - op := &opcodeArray[i] - if op.value < OP_NOP && op.value != OP_RESERVED { + for opcodeName, opcodeValue := range OpcodeByName { + if opcodeValue < OP_NOP && opcodeValue != OP_RESERVED { continue } - if strings.Contains(op.name, "OP_UNKNOWN") { + if strings.Contains(opcodeName, "OP_UNKNOWN") { continue } - ops[op.name] = op - ops[strings.TrimPrefix(op.name, "OP_")] = op + ops[opcodeName] = opcodeValue + ops[strings.TrimPrefix(opcodeName, "OP_")] = opcodeValue } // do once, build map. @@ -90,7 +89,7 @@ func parseShortForm(script string) ([]byte, error) { tok[0] == '\'' && tok[len(tok)-1] == '\'' { builder.AddFullData([]byte(tok[1 : len(tok)-1])) } else if opcode, ok := ops[tok]; ok { - builder.AddOp(opcode.value) + builder.AddOp(opcode) } else { return nil, fmt.Errorf("bad token \"%s\"", tok) }