Skip to content

Commit 23aa994

Browse files
committed
Improve bytes conversion to string
1 parent 4dc3ce7 commit 23aa994

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

accounts/abi/unpack.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package abi
1818

1919
import (
2020
"encoding/binary"
21+
"encoding/hex"
2122
"errors"
2223
"fmt"
2324
"math"
@@ -400,7 +401,12 @@ func toString(index int, t Type, output []byte) (interface{}, error) {
400401
case StringTy: // variable arrays are written at the end of the return bytes
401402
return string(output[begin : begin+length]), nil
402403
case IntTy, UintTy:
403-
return ReadInteger(t, returnOutput)
404+
var n interface{}
405+
n, err = ReadInteger(t, returnOutput)
406+
if err != nil {
407+
return nil, fmt.Errorf("abi: cannot convert value as integer: %v", returnOutput)
408+
}
409+
return fmt.Sprintf("%d", n), nil
404410
case BoolTy:
405411
var b bool
406412
b, err = readBool(returnOutput)
@@ -409,25 +415,25 @@ func toString(index int, t Type, output []byte) (interface{}, error) {
409415
}
410416
return strconv.FormatBool(b), nil
411417
case AddressTy:
412-
return string(common.BytesToAddress(returnOutput).Bytes()), nil
418+
return hex.EncodeToString(common.BytesToAddress(returnOutput).Bytes()), nil
413419
case HashTy:
414-
return string(common.BytesToHash(returnOutput).Bytes()), nil
420+
return hex.EncodeToString(common.BytesToHash(returnOutput).Bytes()), nil
415421
case BytesTy:
416-
return string(output[begin : begin+length]), nil
422+
return hex.EncodeToString(output[begin : begin+length]), nil
417423
case FixedBytesTy:
418424
var b interface{}
419425
b, err = ReadFixedBytes(t, returnOutput)
420426
if err != nil {
421427
return nil, fmt.Errorf("abi: cannot convert value as fixed bytes array: %v", returnOutput)
422428
}
423-
return string(b.([]byte)), nil
429+
return hex.EncodeToString(b.([]byte)), nil
424430
case FunctionTy:
425431
var f interface{}
426432
f, err = ReadFixedBytes(t, returnOutput)
427433
if err != nil {
428434
return nil, fmt.Errorf("abi: cannot convert value as function: %v", returnOutput)
429435
}
430-
return string(f.([]byte)), nil
436+
return hex.EncodeToString(f.([]byte)), nil
431437
default:
432438
return nil, fmt.Errorf("abi: unknown type %v", t.T)
433439
}

0 commit comments

Comments
 (0)