common/hexutil: new package for 0x hex encoding
The new package is purpose-built to handle the encoding consumed and produced by the RPC API.
This commit is contained in:
parent
801a13f791
commit
ec75953f50
232
common/hexutil/hexutil.go
Normal file
232
common/hexutil/hexutil.go
Normal file
@ -0,0 +1,232 @@
|
|||||||
|
// Copyright 2016 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package hexutil implements hex encoding with 0x prefix.
|
||||||
|
This encoding is used by the Ethereum RPC API to transport binary data in JSON payloads.
|
||||||
|
|
||||||
|
Encoding Rules
|
||||||
|
|
||||||
|
All hex data must have prefix "0x".
|
||||||
|
|
||||||
|
For byte slices, the hex data must be of even length. An empty byte slice
|
||||||
|
encodes as "0x".
|
||||||
|
|
||||||
|
Integers are encoded using the least amount of digits (no leading zero digits). Their
|
||||||
|
encoding may be of uneven length. The number zero encodes as "0x0".
|
||||||
|
*/
|
||||||
|
package hexutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const uintBits = 32 << (uint64(^uint(0)) >> 63)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrEmptyString = errors.New("empty hex string")
|
||||||
|
ErrMissingPrefix = errors.New("missing 0x prefix for hex data")
|
||||||
|
ErrSyntax = errors.New("invalid hex")
|
||||||
|
ErrEmptyNumber = errors.New("hex number has no digits after 0x")
|
||||||
|
ErrLeadingZero = errors.New("hex number has leading zero digits after 0x")
|
||||||
|
ErrOddLength = errors.New("hex string has odd length")
|
||||||
|
ErrUint64Range = errors.New("hex number does not fit into 64 bits")
|
||||||
|
ErrUintRange = fmt.Errorf("hex number does not fit into %d bits", uintBits)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Decode decodes a hex string with 0x prefix.
|
||||||
|
func Decode(input string) ([]byte, error) {
|
||||||
|
if len(input) == 0 {
|
||||||
|
return nil, ErrEmptyString
|
||||||
|
}
|
||||||
|
if !has0xPrefix(input) {
|
||||||
|
return nil, ErrMissingPrefix
|
||||||
|
}
|
||||||
|
return hex.DecodeString(input[2:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustDecode decodes a hex string with 0x prefix. It panics for invalid input.
|
||||||
|
func MustDecode(input string) []byte {
|
||||||
|
dec, err := Decode(input)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return dec
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode encodes b as a hex string with 0x prefix.
|
||||||
|
func Encode(b []byte) string {
|
||||||
|
enc := make([]byte, len(b)*2+2)
|
||||||
|
copy(enc, "0x")
|
||||||
|
hex.Encode(enc[2:], b)
|
||||||
|
return string(enc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeUint64 decodes a hex string with 0x prefix as a quantity.
|
||||||
|
func DecodeUint64(input string) (uint64, error) {
|
||||||
|
raw, err := checkNumber(input)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
dec, err := strconv.ParseUint(raw, 16, 64)
|
||||||
|
if err != nil {
|
||||||
|
err = mapError(err)
|
||||||
|
}
|
||||||
|
return dec, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustDecodeUint64 decodes a hex string with 0x prefix as a quantity.
|
||||||
|
// It panics for invalid input.
|
||||||
|
func MustDecodeUint64(input string) uint64 {
|
||||||
|
dec, err := DecodeUint64(input)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return dec
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncodeUint64 encodes i as a hex string with 0x prefix.
|
||||||
|
func EncodeUint64(i uint64) string {
|
||||||
|
enc := make([]byte, 2, 10)
|
||||||
|
copy(enc, "0x")
|
||||||
|
return string(strconv.AppendUint(enc, i, 16))
|
||||||
|
}
|
||||||
|
|
||||||
|
var bigWordNibbles int
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// This is a weird way to compute the number of nibbles required for big.Word.
|
||||||
|
// The usual way would be to use constant arithmetic but go vet can't handle that.
|
||||||
|
b, _ := new(big.Int).SetString("FFFFFFFFFF", 16)
|
||||||
|
switch len(b.Bits()) {
|
||||||
|
case 1:
|
||||||
|
bigWordNibbles = 16
|
||||||
|
case 2:
|
||||||
|
bigWordNibbles = 8
|
||||||
|
default:
|
||||||
|
panic("weird big.Word size")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeBig decodes a hex string with 0x prefix as a quantity.
|
||||||
|
func DecodeBig(input string) (*big.Int, error) {
|
||||||
|
raw, err := checkNumber(input)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
words := make([]big.Word, len(raw)/bigWordNibbles+1)
|
||||||
|
end := len(raw)
|
||||||
|
for i := range words {
|
||||||
|
start := end - bigWordNibbles
|
||||||
|
if start < 0 {
|
||||||
|
start = 0
|
||||||
|
}
|
||||||
|
for ri := start; ri < end; ri++ {
|
||||||
|
nib := decodeNibble(raw[ri])
|
||||||
|
if nib == badNibble {
|
||||||
|
return nil, ErrSyntax
|
||||||
|
}
|
||||||
|
words[i] *= 16
|
||||||
|
words[i] += big.Word(nib)
|
||||||
|
}
|
||||||
|
end = start
|
||||||
|
}
|
||||||
|
dec := new(big.Int).SetBits(words)
|
||||||
|
return dec, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustDecodeBig decodes a hex string with 0x prefix as a quantity.
|
||||||
|
// It panics for invalid input.
|
||||||
|
func MustDecodeBig(input string) *big.Int {
|
||||||
|
dec, err := DecodeBig(input)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return dec
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncodeBig encodes bigint as a hex string with 0x prefix.
|
||||||
|
// The sign of the integer is ignored.
|
||||||
|
func EncodeBig(bigint *big.Int) string {
|
||||||
|
nbits := bigint.BitLen()
|
||||||
|
if nbits == 0 {
|
||||||
|
return "0x0"
|
||||||
|
}
|
||||||
|
enc := make([]byte, 2, (nbits/8)*2+2)
|
||||||
|
copy(enc, "0x")
|
||||||
|
for i := len(bigint.Bits()) - 1; i >= 0; i-- {
|
||||||
|
enc = strconv.AppendUint(enc, uint64(bigint.Bits()[i]), 16)
|
||||||
|
}
|
||||||
|
return string(enc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func has0xPrefix(input string) bool {
|
||||||
|
return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkNumber(input string) (raw string, err error) {
|
||||||
|
if len(input) == 0 {
|
||||||
|
return "", ErrEmptyString
|
||||||
|
}
|
||||||
|
if !has0xPrefix(input) {
|
||||||
|
return "", ErrMissingPrefix
|
||||||
|
}
|
||||||
|
input = input[2:]
|
||||||
|
if len(input) == 0 {
|
||||||
|
return "", ErrEmptyNumber
|
||||||
|
}
|
||||||
|
if len(input) > 1 && input[0] == '0' {
|
||||||
|
return "", ErrLeadingZero
|
||||||
|
}
|
||||||
|
return input, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const badNibble = ^uint64(0)
|
||||||
|
|
||||||
|
func decodeNibble(in byte) uint64 {
|
||||||
|
switch {
|
||||||
|
case in >= '0' && in <= '9':
|
||||||
|
return uint64(in - '0')
|
||||||
|
case in >= 'A' && in <= 'F':
|
||||||
|
return uint64(in - 'A' + 10)
|
||||||
|
case in >= 'a' && in <= 'f':
|
||||||
|
return uint64(in - 'a' + 10)
|
||||||
|
default:
|
||||||
|
return badNibble
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapError(err error) error {
|
||||||
|
if err, ok := err.(*strconv.NumError); ok {
|
||||||
|
switch err.Err {
|
||||||
|
case strconv.ErrRange:
|
||||||
|
return ErrUint64Range
|
||||||
|
case strconv.ErrSyntax:
|
||||||
|
return ErrSyntax
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, ok := err.(hex.InvalidByteError); ok {
|
||||||
|
return ErrSyntax
|
||||||
|
}
|
||||||
|
if err == hex.ErrLength {
|
||||||
|
return ErrOddLength
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
186
common/hexutil/hexutil_test.go
Normal file
186
common/hexutil/hexutil_test.go
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
// Copyright 2016 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package hexutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
|
"math/big"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type marshalTest struct {
|
||||||
|
input interface{}
|
||||||
|
want string
|
||||||
|
}
|
||||||
|
|
||||||
|
type unmarshalTest struct {
|
||||||
|
input string
|
||||||
|
want interface{}
|
||||||
|
wantErr error
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
encodeBytesTests = []marshalTest{
|
||||||
|
{[]byte{}, "0x"},
|
||||||
|
{[]byte{0}, "0x00"},
|
||||||
|
{[]byte{0, 0, 1, 2}, "0x00000102"},
|
||||||
|
}
|
||||||
|
|
||||||
|
encodeBigTests = []marshalTest{
|
||||||
|
{referenceBig("0"), "0x0"},
|
||||||
|
{referenceBig("1"), "0x1"},
|
||||||
|
{referenceBig("ff"), "0xff"},
|
||||||
|
{referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"},
|
||||||
|
}
|
||||||
|
|
||||||
|
encodeUint64Tests = []marshalTest{
|
||||||
|
{uint64(0), "0x0"},
|
||||||
|
{uint64(1), "0x1"},
|
||||||
|
{uint64(0xff), "0xff"},
|
||||||
|
{uint64(0x1122334455667788), "0x1122334455667788"},
|
||||||
|
}
|
||||||
|
|
||||||
|
decodeBytesTests = []unmarshalTest{
|
||||||
|
// invalid
|
||||||
|
{input: ``, wantErr: ErrEmptyString},
|
||||||
|
{input: `0`, wantErr: ErrMissingPrefix},
|
||||||
|
{input: `0x0`, wantErr: hex.ErrLength},
|
||||||
|
{input: `0x023`, wantErr: hex.ErrLength},
|
||||||
|
{input: `0xxx`, wantErr: hex.InvalidByteError('x')},
|
||||||
|
{input: `0x01zz01`, wantErr: hex.InvalidByteError('z')},
|
||||||
|
// valid
|
||||||
|
{input: `0x`, want: []byte{}},
|
||||||
|
{input: `0X`, want: []byte{}},
|
||||||
|
{input: `0x02`, want: []byte{0x02}},
|
||||||
|
{input: `0X02`, want: []byte{0x02}},
|
||||||
|
{input: `0xffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}},
|
||||||
|
{
|
||||||
|
input: `0xffffffffffffffffffffffffffffffffffff`,
|
||||||
|
want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
decodeBigTests = []unmarshalTest{
|
||||||
|
// invalid
|
||||||
|
{input: `0`, wantErr: ErrMissingPrefix},
|
||||||
|
{input: `0x`, wantErr: ErrEmptyNumber},
|
||||||
|
{input: `0x01`, wantErr: ErrLeadingZero},
|
||||||
|
{input: `0xx`, wantErr: ErrSyntax},
|
||||||
|
{input: `0x1zz01`, wantErr: ErrSyntax},
|
||||||
|
// valid
|
||||||
|
{input: `0x0`, want: big.NewInt(0)},
|
||||||
|
{input: `0x2`, want: big.NewInt(0x2)},
|
||||||
|
{input: `0x2F2`, want: big.NewInt(0x2f2)},
|
||||||
|
{input: `0X2F2`, want: big.NewInt(0x2f2)},
|
||||||
|
{input: `0x1122aaff`, want: big.NewInt(0x1122aaff)},
|
||||||
|
{input: `0xbBb`, want: big.NewInt(0xbbb)},
|
||||||
|
{input: `0xfffffffff`, want: big.NewInt(0xfffffffff)},
|
||||||
|
{
|
||||||
|
input: `0x112233445566778899aabbccddeeff`,
|
||||||
|
want: referenceBig("112233445566778899aabbccddeeff"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `0xffffffffffffffffffffffffffffffffffff`,
|
||||||
|
want: referenceBig("ffffffffffffffffffffffffffffffffffff"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
decodeUint64Tests = []unmarshalTest{
|
||||||
|
// invalid
|
||||||
|
{input: `0`, wantErr: ErrMissingPrefix},
|
||||||
|
{input: `0x`, wantErr: ErrEmptyNumber},
|
||||||
|
{input: `0x01`, wantErr: ErrLeadingZero},
|
||||||
|
{input: `0xfffffffffffffffff`, wantErr: ErrUintRange},
|
||||||
|
{input: `0xx`, wantErr: ErrSyntax},
|
||||||
|
{input: `0x1zz01`, wantErr: ErrSyntax},
|
||||||
|
// valid
|
||||||
|
{input: `0x0`, want: uint64(0)},
|
||||||
|
{input: `0x2`, want: uint64(0x2)},
|
||||||
|
{input: `0x2F2`, want: uint64(0x2f2)},
|
||||||
|
{input: `0X2F2`, want: uint64(0x2f2)},
|
||||||
|
{input: `0x1122aaff`, want: uint64(0x1122aaff)},
|
||||||
|
{input: `0xbbb`, want: uint64(0xbbb)},
|
||||||
|
{input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEncode(t *testing.T) {
|
||||||
|
for _, test := range encodeBytesTests {
|
||||||
|
enc := Encode(test.input.([]byte))
|
||||||
|
if enc != test.want {
|
||||||
|
t.Errorf("input %x: wrong encoding %s", test.input, enc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecode(t *testing.T) {
|
||||||
|
for _, test := range decodeBytesTests {
|
||||||
|
dec, err := Decode(test.input)
|
||||||
|
if !checkError(t, test.input, err, test.wantErr) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !bytes.Equal(test.want.([]byte), dec) {
|
||||||
|
t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncodeBig(t *testing.T) {
|
||||||
|
for _, test := range encodeBigTests {
|
||||||
|
enc := EncodeBig(test.input.(*big.Int))
|
||||||
|
if enc != test.want {
|
||||||
|
t.Errorf("input %x: wrong encoding %s", test.input, enc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeBig(t *testing.T) {
|
||||||
|
for _, test := range decodeBigTests {
|
||||||
|
dec, err := DecodeBig(test.input)
|
||||||
|
if !checkError(t, test.input, err, test.wantErr) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if dec.Cmp(test.want.(*big.Int)) != 0 {
|
||||||
|
t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncodeUint64(t *testing.T) {
|
||||||
|
for _, test := range encodeUint64Tests {
|
||||||
|
enc := EncodeUint64(test.input.(uint64))
|
||||||
|
if enc != test.want {
|
||||||
|
t.Errorf("input %x: wrong encoding %s", test.input, enc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeUint64(t *testing.T) {
|
||||||
|
for _, test := range decodeUint64Tests {
|
||||||
|
dec, err := DecodeUint64(test.input)
|
||||||
|
if !checkError(t, test.input, err, test.wantErr) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if dec != test.want.(uint64) {
|
||||||
|
t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
271
common/hexutil/json.go
Normal file
271
common/hexutil/json.go
Normal file
@ -0,0 +1,271 @@
|
|||||||
|
// Copyright 2016 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package hexutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
jsonNull = []byte("null")
|
||||||
|
jsonZero = []byte(`"0x0"`)
|
||||||
|
errNonString = errors.New("cannot unmarshal non-string as hex data")
|
||||||
|
errNegativeBigInt = errors.New("hexutil.Big: can't marshal negative integer")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Bytes marshals/unmarshals as a JSON string with 0x prefix.
|
||||||
|
// The empty slice marshals as "0x".
|
||||||
|
type Bytes []byte
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaler.
|
||||||
|
func (b Bytes) MarshalJSON() ([]byte, error) {
|
||||||
|
result := make([]byte, len(b)*2+4)
|
||||||
|
copy(result, `"0x`)
|
||||||
|
hex.Encode(result[3:], b)
|
||||||
|
result[len(result)-1] = '"'
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements json.Unmarshaler.
|
||||||
|
func (b *Bytes) UnmarshalJSON(input []byte) error {
|
||||||
|
raw, err := checkJSON(input)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dec := make([]byte, len(raw)/2)
|
||||||
|
if _, err = hex.Decode(dec, raw); err != nil {
|
||||||
|
err = mapError(err)
|
||||||
|
} else {
|
||||||
|
*b = dec
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the hex encoding of b.
|
||||||
|
func (b Bytes) String() string {
|
||||||
|
return Encode(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON decodes input as a JSON string with 0x prefix. The length of out
|
||||||
|
// determines the required input length. This function is commonly used to implement the
|
||||||
|
// UnmarshalJSON method for fixed-size types:
|
||||||
|
//
|
||||||
|
// type Foo [8]byte
|
||||||
|
//
|
||||||
|
// func (f *Foo) UnmarshalJSON(input []byte) error {
|
||||||
|
// return hexutil.UnmarshalJSON("Foo", input, f[:])
|
||||||
|
// }
|
||||||
|
func UnmarshalJSON(typname string, input, out []byte) error {
|
||||||
|
raw, err := checkJSON(input)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(raw)/2 != len(out) {
|
||||||
|
return fmt.Errorf("hex string has length %d, want %d for %s", len(raw), len(out)*2, typname)
|
||||||
|
}
|
||||||
|
// Pre-verify syntax before modifying out.
|
||||||
|
for _, b := range raw {
|
||||||
|
if decodeNibble(b) == badNibble {
|
||||||
|
return ErrSyntax
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hex.Decode(out, raw)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Big marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as
|
||||||
|
// "0x0". Negative integers are not supported at this time. Attempting to marshal them
|
||||||
|
// will return an error.
|
||||||
|
type Big big.Int
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaler.
|
||||||
|
func (b *Big) MarshalJSON() ([]byte, error) {
|
||||||
|
if b == nil {
|
||||||
|
return jsonNull, nil
|
||||||
|
}
|
||||||
|
bigint := (*big.Int)(b)
|
||||||
|
if bigint.Sign() == -1 {
|
||||||
|
return nil, errNegativeBigInt
|
||||||
|
}
|
||||||
|
nbits := bigint.BitLen()
|
||||||
|
if nbits == 0 {
|
||||||
|
return jsonZero, nil
|
||||||
|
}
|
||||||
|
enc := make([]byte, 3, (nbits/8)*2+4)
|
||||||
|
copy(enc, `"0x`)
|
||||||
|
for i := len(bigint.Bits()) - 1; i >= 0; i-- {
|
||||||
|
enc = strconv.AppendUint(enc, uint64(bigint.Bits()[i]), 16)
|
||||||
|
}
|
||||||
|
enc = append(enc, '"')
|
||||||
|
return enc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements json.Unmarshaler.
|
||||||
|
func (b *Big) UnmarshalJSON(input []byte) error {
|
||||||
|
raw, err := checkNumberJSON(input)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
words := make([]big.Word, len(raw)/bigWordNibbles+1)
|
||||||
|
end := len(raw)
|
||||||
|
for i := range words {
|
||||||
|
start := end - bigWordNibbles
|
||||||
|
if start < 0 {
|
||||||
|
start = 0
|
||||||
|
}
|
||||||
|
for ri := start; ri < end; ri++ {
|
||||||
|
nib := decodeNibble(raw[ri])
|
||||||
|
if nib == badNibble {
|
||||||
|
return ErrSyntax
|
||||||
|
}
|
||||||
|
words[i] *= 16
|
||||||
|
words[i] += big.Word(nib)
|
||||||
|
}
|
||||||
|
end = start
|
||||||
|
}
|
||||||
|
var dec big.Int
|
||||||
|
dec.SetBits(words)
|
||||||
|
*b = (Big)(dec)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToInt converts b to a big.Int.
|
||||||
|
func (b *Big) ToInt() *big.Int {
|
||||||
|
return (*big.Int)(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the hex encoding of b.
|
||||||
|
func (b *Big) String() string {
|
||||||
|
return EncodeBig(b.ToInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uint64 marshals/unmarshals as a JSON string with 0x prefix.
|
||||||
|
// The zero value marshals as "0x0".
|
||||||
|
type Uint64 uint64
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaler.
|
||||||
|
func (b Uint64) MarshalJSON() ([]byte, error) {
|
||||||
|
buf := make([]byte, 3, 12)
|
||||||
|
copy(buf, `"0x`)
|
||||||
|
buf = strconv.AppendUint(buf, uint64(b), 16)
|
||||||
|
buf = append(buf, '"')
|
||||||
|
return buf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements json.Unmarshaler.
|
||||||
|
func (b *Uint64) UnmarshalJSON(input []byte) error {
|
||||||
|
raw, err := checkNumberJSON(input)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(raw) > 16 {
|
||||||
|
return ErrUint64Range
|
||||||
|
}
|
||||||
|
var dec uint64
|
||||||
|
for _, byte := range raw {
|
||||||
|
nib := decodeNibble(byte)
|
||||||
|
if nib == badNibble {
|
||||||
|
return ErrSyntax
|
||||||
|
}
|
||||||
|
dec *= 16
|
||||||
|
dec += uint64(nib)
|
||||||
|
}
|
||||||
|
*b = Uint64(dec)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the hex encoding of b.
|
||||||
|
func (b Uint64) String() string {
|
||||||
|
return EncodeUint64(uint64(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uint marshals/unmarshals as a JSON string with 0x prefix.
|
||||||
|
// The zero value marshals as "0x0".
|
||||||
|
type Uint uint
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaler.
|
||||||
|
func (b Uint) MarshalJSON() ([]byte, error) {
|
||||||
|
return Uint64(b).MarshalJSON()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements json.Unmarshaler.
|
||||||
|
func (b *Uint) UnmarshalJSON(input []byte) error {
|
||||||
|
var u64 Uint64
|
||||||
|
err := u64.UnmarshalJSON(input)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if u64 > Uint64(^uint(0)) {
|
||||||
|
return ErrUintRange
|
||||||
|
}
|
||||||
|
*b = Uint(u64)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the hex encoding of b.
|
||||||
|
func (b Uint) String() string {
|
||||||
|
return EncodeUint64(uint64(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
func isString(input []byte) bool {
|
||||||
|
return len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"'
|
||||||
|
}
|
||||||
|
|
||||||
|
func bytesHave0xPrefix(input []byte) bool {
|
||||||
|
return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkJSON(input []byte) (raw []byte, err error) {
|
||||||
|
if !isString(input) {
|
||||||
|
return nil, errNonString
|
||||||
|
}
|
||||||
|
if len(input) == 2 {
|
||||||
|
return nil, ErrEmptyString
|
||||||
|
}
|
||||||
|
if !bytesHave0xPrefix(input[1:]) {
|
||||||
|
return nil, ErrMissingPrefix
|
||||||
|
}
|
||||||
|
input = input[3 : len(input)-1]
|
||||||
|
if len(input)%2 != 0 {
|
||||||
|
return nil, ErrOddLength
|
||||||
|
}
|
||||||
|
return input, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkNumberJSON(input []byte) (raw []byte, err error) {
|
||||||
|
if !isString(input) {
|
||||||
|
return nil, errNonString
|
||||||
|
}
|
||||||
|
input = input[1 : len(input)-1]
|
||||||
|
if len(input) == 0 {
|
||||||
|
return nil, ErrEmptyString
|
||||||
|
}
|
||||||
|
if !bytesHave0xPrefix(input) {
|
||||||
|
return nil, ErrMissingPrefix
|
||||||
|
}
|
||||||
|
input = input[2:]
|
||||||
|
if len(input) == 0 {
|
||||||
|
return nil, ErrEmptyNumber
|
||||||
|
}
|
||||||
|
if len(input) > 1 && input[0] == '0' {
|
||||||
|
return nil, ErrLeadingZero
|
||||||
|
}
|
||||||
|
return input, nil
|
||||||
|
}
|
258
common/hexutil/json_test.go
Normal file
258
common/hexutil/json_test.go
Normal file
@ -0,0 +1,258 @@
|
|||||||
|
// Copyright 2016 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package hexutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
|
"math/big"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkError(t *testing.T, input string, got, want error) bool {
|
||||||
|
if got == nil {
|
||||||
|
if want != nil {
|
||||||
|
t.Errorf("input %s: got no error, want %q", input, want)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if want == nil {
|
||||||
|
t.Errorf("input %s: unexpected error %q", input, got)
|
||||||
|
} else if got.Error() != want.Error() {
|
||||||
|
t.Errorf("input %s: got error %q, want %q", input, got, want)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func referenceBig(s string) *big.Int {
|
||||||
|
b, ok := new(big.Int).SetString(s, 16)
|
||||||
|
if !ok {
|
||||||
|
panic("invalid")
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func referenceBytes(s string) []byte {
|
||||||
|
b, err := hex.DecodeString(s)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
var unmarshalBytesTests = []unmarshalTest{
|
||||||
|
// invalid encoding
|
||||||
|
{input: "", wantErr: errNonString},
|
||||||
|
{input: "null", wantErr: errNonString},
|
||||||
|
{input: "10", wantErr: errNonString},
|
||||||
|
{input: `""`, wantErr: ErrEmptyString},
|
||||||
|
{input: `"0"`, wantErr: ErrMissingPrefix},
|
||||||
|
{input: `"0x0"`, wantErr: ErrOddLength},
|
||||||
|
{input: `"0xxx"`, wantErr: ErrSyntax},
|
||||||
|
{input: `"0x01zz01"`, wantErr: ErrSyntax},
|
||||||
|
|
||||||
|
// valid encoding
|
||||||
|
{input: `"0x"`, want: referenceBytes("")},
|
||||||
|
{input: `"0x02"`, want: referenceBytes("02")},
|
||||||
|
{input: `"0X02"`, want: referenceBytes("02")},
|
||||||
|
{input: `"0xffffffffff"`, want: referenceBytes("ffffffffff")},
|
||||||
|
{
|
||||||
|
input: `"0xffffffffffffffffffffffffffffffffffff"`,
|
||||||
|
want: referenceBytes("ffffffffffffffffffffffffffffffffffff"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalBytes(t *testing.T) {
|
||||||
|
for _, test := range unmarshalBytesTests {
|
||||||
|
var v Bytes
|
||||||
|
err := v.UnmarshalJSON([]byte(test.input))
|
||||||
|
if !checkError(t, test.input, err, test.wantErr) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !bytes.Equal(test.want.([]byte), []byte(v)) {
|
||||||
|
t.Errorf("input %s: value mismatch: got %x, want %x", test.input, &v, test.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkUnmarshalBytes(b *testing.B) {
|
||||||
|
input := []byte(`"0x123456789abcdef123456789abcdef"`)
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
var v Bytes
|
||||||
|
if err := v.UnmarshalJSON(input); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalBytes(t *testing.T) {
|
||||||
|
for _, test := range encodeBytesTests {
|
||||||
|
in := test.input.([]byte)
|
||||||
|
out, err := Bytes(in).MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%x: %v", in, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if want := `"` + test.want + `"`; string(out) != want {
|
||||||
|
t.Errorf("%x: MarshalJSON output mismatch: got %q, want %q", in, out, want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if out := Bytes(in).String(); out != test.want {
|
||||||
|
t.Errorf("%x: String mismatch: got %q, want %q", in, out, test.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var unmarshalBigTests = []unmarshalTest{
|
||||||
|
// invalid encoding
|
||||||
|
{input: "", wantErr: errNonString},
|
||||||
|
{input: "null", wantErr: errNonString},
|
||||||
|
{input: "10", wantErr: errNonString},
|
||||||
|
{input: `""`, wantErr: ErrEmptyString},
|
||||||
|
{input: `"0"`, wantErr: ErrMissingPrefix},
|
||||||
|
{input: `"0x"`, wantErr: ErrEmptyNumber},
|
||||||
|
{input: `"0x01"`, wantErr: ErrLeadingZero},
|
||||||
|
{input: `"0xx"`, wantErr: ErrSyntax},
|
||||||
|
{input: `"0x1zz01"`, wantErr: ErrSyntax},
|
||||||
|
|
||||||
|
// valid encoding
|
||||||
|
{input: `"0x0"`, want: big.NewInt(0)},
|
||||||
|
{input: `"0x2"`, want: big.NewInt(0x2)},
|
||||||
|
{input: `"0x2F2"`, want: big.NewInt(0x2f2)},
|
||||||
|
{input: `"0X2F2"`, want: big.NewInt(0x2f2)},
|
||||||
|
{input: `"0x1122aaff"`, want: big.NewInt(0x1122aaff)},
|
||||||
|
{input: `"0xbBb"`, want: big.NewInt(0xbbb)},
|
||||||
|
{input: `"0xfffffffff"`, want: big.NewInt(0xfffffffff)},
|
||||||
|
{
|
||||||
|
input: `"0x112233445566778899aabbccddeeff"`,
|
||||||
|
want: referenceBig("112233445566778899aabbccddeeff"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `"0xffffffffffffffffffffffffffffffffffff"`,
|
||||||
|
want: referenceBig("ffffffffffffffffffffffffffffffffffff"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalBig(t *testing.T) {
|
||||||
|
for _, test := range unmarshalBigTests {
|
||||||
|
var v Big
|
||||||
|
err := v.UnmarshalJSON([]byte(test.input))
|
||||||
|
if !checkError(t, test.input, err, test.wantErr) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if test.want != nil && test.want.(*big.Int).Cmp((*big.Int)(&v)) != 0 {
|
||||||
|
t.Errorf("input %s: value mismatch: got %x, want %x", test.input, (*big.Int)(&v), test.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkUnmarshalBig(b *testing.B) {
|
||||||
|
input := []byte(`"0x123456789abcdef123456789abcdef"`)
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
var v Big
|
||||||
|
if err := v.UnmarshalJSON(input); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalBig(t *testing.T) {
|
||||||
|
for _, test := range encodeBigTests {
|
||||||
|
in := test.input.(*big.Int)
|
||||||
|
out, err := (*Big)(in).MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%d: %v", in, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if want := `"` + test.want + `"`; string(out) != want {
|
||||||
|
t.Errorf("%d: MarshalJSON output mismatch: got %q, want %q", in, out, want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if out := (*Big)(in).String(); out != test.want {
|
||||||
|
t.Errorf("%x: String mismatch: got %q, want %q", in, out, test.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var unmarshalUint64Tests = []unmarshalTest{
|
||||||
|
// invalid encoding
|
||||||
|
{input: "", wantErr: errNonString},
|
||||||
|
{input: "null", wantErr: errNonString},
|
||||||
|
{input: "10", wantErr: errNonString},
|
||||||
|
{input: `""`, wantErr: ErrEmptyString},
|
||||||
|
{input: `"0"`, wantErr: ErrMissingPrefix},
|
||||||
|
{input: `"0x"`, wantErr: ErrEmptyNumber},
|
||||||
|
{input: `"0x01"`, wantErr: ErrLeadingZero},
|
||||||
|
{input: `"0xfffffffffffffffff"`, wantErr: ErrUintRange},
|
||||||
|
{input: `"0xx"`, wantErr: ErrSyntax},
|
||||||
|
{input: `"0x1zz01"`, wantErr: ErrSyntax},
|
||||||
|
|
||||||
|
// valid encoding
|
||||||
|
{input: `"0x0"`, want: uint64(0)},
|
||||||
|
{input: `"0x2"`, want: uint64(0x2)},
|
||||||
|
{input: `"0x2F2"`, want: uint64(0x2f2)},
|
||||||
|
{input: `"0X2F2"`, want: uint64(0x2f2)},
|
||||||
|
{input: `"0x1122aaff"`, want: uint64(0x1122aaff)},
|
||||||
|
{input: `"0xbbb"`, want: uint64(0xbbb)},
|
||||||
|
{input: `"0xffffffffffffffff"`, want: uint64(0xffffffffffffffff)},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalUint64(t *testing.T) {
|
||||||
|
for _, test := range unmarshalUint64Tests {
|
||||||
|
var v Uint64
|
||||||
|
err := v.UnmarshalJSON([]byte(test.input))
|
||||||
|
if !checkError(t, test.input, err, test.wantErr) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if uint64(v) != test.want.(uint64) {
|
||||||
|
t.Errorf("input %s: value mismatch: got %d, want %d", test.input, v, test.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkUnmarshalUint64(b *testing.B) {
|
||||||
|
input := []byte(`"0x123456789abcdf"`)
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
var v Uint64
|
||||||
|
v.UnmarshalJSON(input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalUint64(t *testing.T) {
|
||||||
|
for _, test := range encodeUint64Tests {
|
||||||
|
in := test.input.(uint64)
|
||||||
|
out, err := Uint64(in).MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%d: %v", in, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if want := `"` + test.want + `"`; string(out) != want {
|
||||||
|
t.Errorf("%d: MarshalJSON output mismatch: got %q, want %q", in, out, want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if out := (Uint64)(in).String(); out != test.want {
|
||||||
|
t.Errorf("%x: String mismatch: got %q, want %q", in, out, test.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user