Skip to content

Commit a89e988

Browse files
committed
Fix converting fixed bytes to string
1 parent bd04917 commit a89e988

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
@@ -425,14 +425,14 @@ func toString(index int, t Type, output []byte) (interface{}, error) {
425425
if err != nil {
426426
return nil, fmt.Errorf("abi: cannot convert value as fixed bytes array: %v", returnOutput)
427427
}
428-
return common.Bytes2HexWithPrefix(b.([]byte)), nil
428+
return common.Bytes2HexWithPrefix(fixedBytesToSlice(b)), nil
429429
case FunctionTy:
430-
var f interface{}
431-
f, err = ReadFixedBytes(t, returnOutput)
430+
var b interface{}
431+
b, err = ReadFixedBytes(t, returnOutput)
432432
if err != nil {
433433
return nil, fmt.Errorf("abi: cannot convert value as function: %v", returnOutput)
434434
}
435-
return common.Bytes2HexWithPrefix(f.([]byte)), nil
435+
return common.Bytes2HexWithPrefix(fixedBytesToSlice(b)), nil
436436
default:
437437
return nil, fmt.Errorf("abi: unknown type %v", t.T)
438438
}
@@ -481,3 +481,14 @@ func tuplePointsTo(index int, output []byte) (start int, err error) {
481481
}
482482
return int(offset.Uint64()), nil
483483
}
484+
485+
func fixedBytesToSlice(b interface{}) (res []byte) {
486+
arr := reflect.ValueOf(b)
487+
length := arr.Len()
488+
res = make([]byte, length)
489+
for i := 0; i < length; i++ {
490+
res[i] = arr.Index(i).Interface().(byte)
491+
}
492+
493+
return res
494+
}

accounts/abi/unpack_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ var unpackTests = []unpackTest{
202202
IntOne *big.Int
203203
}{big.NewInt(1)},
204204
},
205+
{
206+
def: `[{"name":"one","type":"string"},{"name":"two","type":"bytes32"},{"name":"three","type":"bytes"}]`,
207+
enc: "0000000000000000000000000000000000000000000000000000000000000060972ed6e068cd66d9a90b0e300ccf142da753a68f9c75c85b1aff5b85cce09c3e00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000002465303532313337362d613332312d346633372d393161632d3331376330623765353038350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000414341b07dad558e91104ac08d3e939b73ea8d723e9cfb8eb9dc7c532154dbb5df2e5e93514338ecf8c512ba87546238353ff4d28366bb5d91e2938b692ce725cf1c00000000000000000000000000000000000000000000000000000000000000",
208+
want: struct {
209+
One string
210+
Two [32]uint8
211+
Three []uint8
212+
}{"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}},
213+
},
205214
{
206215
def: `[{"type":"bool"}]`,
207216
enc: "",

0 commit comments

Comments
 (0)