Skip to content

Commit a0df753

Browse files
committed
Add 0x prefix when converting to string
(cherry picked from commit bd04917)
1 parent 98614fc commit a0df753

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"
@@ -416,25 +415,25 @@ func toString(index int, t Type, output []byte) (interface{}, error) {
416415
}
417416
return strconv.FormatBool(b), nil
418417
case AddressTy:
419-
return hex.EncodeToString(common.BytesToAddress(returnOutput).Bytes()), nil
418+
return common.Bytes2HexWithPrefix(common.BytesToAddress(returnOutput).Bytes()), nil
420419
case HashTy:
421-
return hex.EncodeToString(common.BytesToHash(returnOutput).Bytes()), nil
420+
return common.Bytes2HexWithPrefix(common.BytesToHash(returnOutput).Bytes()), nil
422421
case BytesTy:
423-
return hex.EncodeToString(output[begin : begin+length]), nil
422+
return common.Bytes2HexWithPrefix(output[begin : begin+length]), nil
424423
case FixedBytesTy:
425424
var b interface{}
426425
b, err = ReadFixedBytes(t, returnOutput)
427426
if err != nil {
428427
return nil, fmt.Errorf("abi: cannot convert value as fixed bytes array: %v", returnOutput)
429428
}
430-
return hex.EncodeToString(b.([]byte)), nil
429+
return common.Bytes2HexWithPrefix(b.([]byte)), nil
431430
case FunctionTy:
432431
var f interface{}
433432
f, err = ReadFixedBytes(t, returnOutput)
434433
if err != nil {
435434
return nil, fmt.Errorf("abi: cannot convert value as function: %v", returnOutput)
436435
}
437-
return hex.EncodeToString(f.([]byte)), nil
436+
return common.Bytes2HexWithPrefix(f.([]byte)), nil
438437
default:
439438
return nil, fmt.Errorf("abi: unknown type %v", t.T)
440439
}

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)