Skip to content

Commit

Permalink
Add 0x prefix when converting to string
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolev-igor committed May 26, 2023
1 parent 23aa994 commit bd04917
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 5 additions & 6 deletions accounts/abi/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package abi

import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math"
Expand Down Expand Up @@ -415,25 +414,25 @@ func toString(index int, t Type, output []byte) (interface{}, error) {
}
return strconv.FormatBool(b), nil
case AddressTy:
return hex.EncodeToString(common.BytesToAddress(returnOutput).Bytes()), nil
return common.Bytes2HexWithPrefix(common.BytesToAddress(returnOutput).Bytes()), nil
case HashTy:
return hex.EncodeToString(common.BytesToHash(returnOutput).Bytes()), nil
return common.Bytes2HexWithPrefix(common.BytesToHash(returnOutput).Bytes()), nil
case BytesTy:
return hex.EncodeToString(output[begin : begin+length]), nil
return common.Bytes2HexWithPrefix(output[begin : begin+length]), nil
case FixedBytesTy:
var b interface{}
b, err = ReadFixedBytes(t, returnOutput)
if err != nil {
return nil, fmt.Errorf("abi: cannot convert value as fixed bytes array: %v", returnOutput)
}
return hex.EncodeToString(b.([]byte)), nil
return common.Bytes2HexWithPrefix(b.([]byte)), nil
case FunctionTy:
var f interface{}
f, err = ReadFixedBytes(t, returnOutput)
if err != nil {
return nil, fmt.Errorf("abi: cannot convert value as function: %v", returnOutput)
}
return hex.EncodeToString(f.([]byte)), nil
return common.Bytes2HexWithPrefix(f.([]byte)), nil
default:
return nil, fmt.Errorf("abi: unknown type %v", t.T)
}
Expand Down
5 changes: 5 additions & 0 deletions common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func Bytes2Hex(d []byte) string {
return hex.EncodeToString(d)
}

// Bytes2Hex returns the hexadecimal encoding of d.
func Bytes2HexWithPrefix(d []byte) string {
return "0x" + Bytes2Hex(d)
}

// Hex2Bytes returns the bytes represented by the hexadecimal string str.
func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str)
Expand Down

0 comments on commit bd04917

Please sign in to comment.