diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index a45bd6cc0f..f41a2b510c 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -112,6 +112,13 @@ func TestSimpleMethodUnpack(t *testing.T) { outVar string // the output variable (e.g. uint32, *big.Int, etc) err string // empty or error if expected }{ + { + `[ { "type": "bool" } ]`, + pad([]byte{1}, 32, true), + bool(true), + "bool", + "", + }, { `[ { "type": "uint32" } ]`, pad([]byte{1}, 32, true), @@ -215,6 +222,10 @@ func TestSimpleMethodUnpack(t *testing.T) { var outvar interface{} switch test.outVar { + case "bool": + var v bool + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v case "uint8": var v uint8 err = abi.Unpack(&v, "method", test.marshalledOutput) diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index b6c41de682..a220705e19 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -118,7 +118,7 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { case IntTy, UintTy: inter = readInteger(t.Type.Kind, returnOutput) case BoolTy: - inter, err = getBoolValue(returnOutput) + inter, err = readBool(returnOutput) if err != nil { return nil, err } @@ -160,7 +160,10 @@ func readInteger(kind reflect.Kind, b []byte) interface{} { } } -func getBoolValue(word []byte) (bool, error) { +func readBool(word []byte) (bool, error) { + if len(word) != 32 { + panic("abi: fatal error. Incorrect word length.") + } improperEncoding := "abi: improperly encoded boolean value" for i, b := range word { if b != 0 && i != 31 { @@ -218,7 +221,7 @@ func toGoType(i int, t Argument, output []byte) (interface{}, error) { case IntTy, UintTy: return readInteger(t.Type.Kind, returnOutput), nil case BoolTy: - return getBoolValue(returnOutput) + return readBool(returnOutput) case AddressTy: return common.BytesToAddress(returnOutput), nil case HashTy: