diff options
author | Péter Szilágyi <peterke@gmail.com> | 2016-08-25 21:20:38 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-25 21:20:38 +0800 |
commit | 5fc032a9d16ac6ea1dc020f06e44c24c94a361a7 (patch) | |
tree | 363e7e1a3439424e00a4e7e72a701b97f737e6aa /rpc | |
parent | 806e3cd0751e7769bddbb2d4250b4520d912df42 (diff) | |
parent | c97df052a96820742fffdbb3ef5e77dbf1397637 (diff) | |
download | go-tangerine-5fc032a9d16ac6ea1dc020f06e44c24c94a361a7.tar go-tangerine-5fc032a9d16ac6ea1dc020f06e44c24c94a361a7.tar.gz go-tangerine-5fc032a9d16ac6ea1dc020f06e44c24c94a361a7.tar.bz2 go-tangerine-5fc032a9d16ac6ea1dc020f06e44c24c94a361a7.tar.lz go-tangerine-5fc032a9d16ac6ea1dc020f06e44c24c94a361a7.tar.xz go-tangerine-5fc032a9d16ac6ea1dc020f06e44c24c94a361a7.tar.zst go-tangerine-5fc032a9d16ac6ea1dc020f06e44c24c94a361a7.zip |
Merge pull request #2930 from fjl/ethclient
Stable Go API, part 1
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/types.go | 30 | ||||
-rw-r--r-- | rpc/types_test.go | 22 |
2 files changed, 52 insertions, 0 deletions
diff --git a/rpc/types.go b/rpc/types.go index 89c5b5bc9..ebe388373 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -17,6 +17,8 @@ package rpc import ( + "bytes" + "encoding/hex" "fmt" "math" "math/big" @@ -272,3 +274,31 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error { func (bn BlockNumber) Int64() int64 { return (int64)(bn) } + +// HexBytes JSON-encodes as hex with 0x prefix. +type HexBytes []byte + +func (b HexBytes) 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 +} + +func (b *HexBytes) UnmarshalJSON(input []byte) error { + if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { + input = input[1 : len(input)-1] + } + if !bytes.HasPrefix(input, []byte("0x")) { + return fmt.Errorf("missing 0x prefix for hex byte array") + } + input = input[2:] + if len(input) == 0 { + *b = nil + return nil + } + *b = make([]byte, len(input)/2) + _, err := hex.Decode(*b, input) + return err +} diff --git a/rpc/types_test.go b/rpc/types_test.go index c2c5c6db6..5482557b8 100644 --- a/rpc/types_test.go +++ b/rpc/types_test.go @@ -71,3 +71,25 @@ func TestHexNumberMarshalJSON(t *testing.T) { t.Fatalf("Invalid json.Marshal, expected '%s', got '%s'", exp, got) } } + +var hexBytesTests = []struct{ in, out []byte }{ + {in: []byte(`"0x"`), out: []byte{}}, + {in: []byte(`"0x00"`), out: []byte{0}}, + {in: []byte(`"0x01ff"`), out: []byte{0x01, 0xFF}}, +} + +func TestHexBytes(t *testing.T) { + for i, test := range hexBytesTests { + var dec HexBytes + if err := json.Unmarshal(test.in, &dec); err != nil { + t.Fatalf("test %d: can't decode: %v", i, err) + } + enc, _ := json.Marshal(HexBytes(test.out)) + if !bytes.Equal(dec, test.out) { + t.Errorf("test %d: wrong decoded value 0x%x", i, dec) + } + if !bytes.Equal(enc, test.in) { + t.Errorf("test %d: wrong encoded value %#q", i, enc) + } + } +} |