diff --git a/common/hexutil/hexutil.go b/common/hexutil/hexutil.go index b66c0d3fecc41319688223f188e92ef4841becba..6b128ae360d9880b56862238a6cc5aee25428f40 100644 --- a/common/hexutil/hexutil.go +++ b/common/hexutil/hexutil.go @@ -178,7 +178,7 @@ func EncodeBig(bigint *big.Int) string { if nbits == 0 { return "0x0" } - return fmt.Sprintf("0x%x", bigint) + return fmt.Sprintf("%#x", bigint) } func has0xPrefix(input string) bool { diff --git a/common/hexutil/hexutil_test.go b/common/hexutil/hexutil_test.go index 6a92a385cd6b86af230324a532c418387ed3fb63..ed6fccc3ca6f4b96a64e2d23fe6826f1d37e609e 100644 --- a/common/hexutil/hexutil_test.go +++ b/common/hexutil/hexutil_test.go @@ -47,6 +47,7 @@ var ( {referenceBig("ff"), "0xff"}, {referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"}, {referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"}, + {referenceBig("-80a7f2c1bcc396c00"), "-0x80a7f2c1bcc396c00"}, } encodeUint64Tests = []marshalTest{ diff --git a/common/hexutil/json.go b/common/hexutil/json.go index ce6a2b048592418d1d50626eec21ea6925dd98a4..23393ed2c52d64c375c47ec281a8eef0dbb28c27 100644 --- a/common/hexutil/json.go +++ b/common/hexutil/json.go @@ -25,9 +25,8 @@ import ( ) var ( - textZero = []byte(`0x0`) - errNonString = errors.New("cannot unmarshal non-string as hex data") - errNegativeBigInt = errors.New("hexutil.Big: can't marshal negative integer") + textZero = []byte(`0x0`) + errNonString = errors.New("cannot unmarshal non-string as hex data") ) // Bytes marshals/unmarshals as a JSON string with 0x prefix. @@ -101,18 +100,7 @@ type Big big.Int // MarshalText implements encoding.TextMarshaler func (b Big) MarshalText() ([]byte, error) { - bigint := (big.Int)(b) - if bigint.Sign() == -1 { - return nil, errNegativeBigInt - } - nbits := bigint.BitLen() - if nbits == 0 { - return textZero, nil - } - enc := make([]byte, 2, nbits/4+2) - copy(enc, "0x") - enc = bigint.Append(enc, 16) - return enc, nil + return []byte(EncodeBig((*big.Int)(&b))), nil } // UnmarshalJSON implements json.Unmarshaler.