Skip to content

Commit bd04917

Browse files
committed
Add 0x prefix when converting to string
1 parent 23aa994 commit bd04917

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

accounts/abi/unpack.go

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

1919
import (
2020
"encoding/binary"
21-
"encoding/hex"
2221
"errors"
2322
"fmt"
2423
"math"
@@ -415,25 +414,25 @@ func toString(index int, t Type, output []byte) (interface{}, error) {
415414
}
416415
return strconv.FormatBool(b), nil
417416
case AddressTy:
418-
return hex.EncodeToString(common.BytesToAddress(returnOutput).Bytes()), nil
417+
return common.Bytes2HexWithPrefix(common.BytesToAddress(returnOutput).Bytes()), nil
419418
case HashTy:
420-
return hex.EncodeToString(common.BytesToHash(returnOutput).Bytes()), nil
419+
return common.Bytes2HexWithPrefix(common.BytesToHash(returnOutput).Bytes()), nil
421420
case BytesTy:
422-
return hex.EncodeToString(output[begin : begin+length]), nil
421+
return common.Bytes2HexWithPrefix(output[begin : begin+length]), nil
423422
case FixedBytesTy:
424423
var b interface{}
425424
b, err = ReadFixedBytes(t, returnOutput)
426425
if err != nil {
427426
return nil, fmt.Errorf("abi: cannot convert value as fixed bytes array: %v", returnOutput)
428427
}
429-
return hex.EncodeToString(b.([]byte)), nil
428+
return common.Bytes2HexWithPrefix(b.([]byte)), nil
430429
case FunctionTy:
431430
var f interface{}
432431
f, err = ReadFixedBytes(t, returnOutput)
433432
if err != nil {
434433
return nil, fmt.Errorf("abi: cannot convert value as function: %v", returnOutput)
435434
}
436-
return hex.EncodeToString(f.([]byte)), nil
435+
return common.Bytes2HexWithPrefix(f.([]byte)), nil
437436
default:
438437
return nil, fmt.Errorf("abi: unknown type %v", t.T)
439438
}

common/bytes.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ func Bytes2Hex(d []byte) string {
7575
return hex.EncodeToString(d)
7676
}
7777

78+
// Bytes2Hex returns the hexadecimal encoding of d.
79+
func Bytes2HexWithPrefix(d []byte) string {
80+
return "0x" + Bytes2Hex(d)
81+
}
82+
7883
// Hex2Bytes returns the bytes represented by the hexadecimal string str.
7984
func Hex2Bytes(str string) []byte {
8085
h, _ := hex.DecodeString(str)

0 commit comments

Comments
 (0)