Skip to content

Commit 0ebea7b

Browse files
committed
Fix converting fixed bytes to string
(cherry picked from commit a89e988)
1 parent a0df753 commit 0ebea7b

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

accounts/abi/unpack.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,14 @@ func toString(index int, t Type, output []byte) (interface{}, error) {
426426
if err != nil {
427427
return nil, fmt.Errorf("abi: cannot convert value as fixed bytes array: %v", returnOutput)
428428
}
429-
return common.Bytes2HexWithPrefix(b.([]byte)), nil
429+
return common.Bytes2HexWithPrefix(fixedBytesToSlice(b)), nil
430430
case FunctionTy:
431-
var f interface{}
432-
f, err = ReadFixedBytes(t, returnOutput)
431+
var b interface{}
432+
b, err = ReadFixedBytes(t, returnOutput)
433433
if err != nil {
434434
return nil, fmt.Errorf("abi: cannot convert value as function: %v", returnOutput)
435435
}
436-
return common.Bytes2HexWithPrefix(f.([]byte)), nil
436+
return common.Bytes2HexWithPrefix(fixedBytesToSlice(b)), nil
437437
default:
438438
return nil, fmt.Errorf("abi: unknown type %v", t.T)
439439
}
@@ -482,3 +482,14 @@ func tuplePointsTo(index int, output []byte) (start int, err error) {
482482
}
483483
return int(offset.Uint64()), nil
484484
}
485+
486+
func fixedBytesToSlice(b interface{}) (res []byte) {
487+
arr := reflect.ValueOf(b)
488+
length := arr.Len()
489+
res = make([]byte, length)
490+
for i := 0; i < length; i++ {
491+
res[i] = arr.Index(i).Interface().(byte)
492+
}
493+
494+
return res
495+
}

accounts/abi/unpack_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ var unpackTests = []unpackTest{
243243
IntOne *big.Int
244244
}{big.NewInt(1)},
245245
},
246+
{
247+
def: `[{"name":"one","type":"string"},{"name":"two","type":"bytes32"},{"name":"three","type":"bytes"}]`,
248+
enc: "0000000000000000000000000000000000000000000000000000000000000060972ed6e068cd66d9a90b0e300ccf142da753a68f9c75c85b1aff5b85cce09c3e00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000002465303532313337362d613332312d346633372d393161632d3331376330623765353038350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000414341b07dad558e91104ac08d3e939b73ea8d723e9cfb8eb9dc7c532154dbb5df2e5e93514338ecf8c512ba87546238353ff4d28366bb5d91e2938b692ce725cf1c00000000000000000000000000000000000000000000000000000000000000",
249+
want: struct {
250+
One string
251+
Two [32]uint8
252+
Three []uint8
253+
}{"e0521376-a321-4f37-91ac-317c0b7e5085", [32]uint8{151, 46, 214, 224, 104, 205, 102, 217, 169, 11, 14, 48, 12, 207, 20, 45, 167, 83, 166, 143, 156, 117, 200, 91, 26, 255, 91, 133, 204, 224, 156, 62}, []uint8{67, 65, 176, 125, 173, 85, 142, 145, 16, 74, 192, 141, 62, 147, 155, 115, 234, 141, 114, 62, 156, 251, 142, 185, 220, 124, 83, 33, 84, 219, 181, 223, 46, 94, 147, 81, 67, 56, 236, 248, 197, 18, 186, 135, 84, 98, 56, 53, 63, 244, 210, 131, 102, 187, 93, 145, 226, 147, 139, 105, 44, 231, 37, 207, 28}},
254+
},
246255
{
247256
def: `[{"type":"bool"}]`,
248257
enc: "",

0 commit comments

Comments
 (0)