-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
118 lines (104 loc) · 3.86 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package types
import (
"bytes"
"fmt"
"math/big"
"github.com/defiweb/go-eth/hexutil"
)
// bytesMarshalJSON encodes the given bytes as a JSON string where each byte is
// represented by a two-digit hex number. The hex string is always even-length
// and prefixed with "0x".
func bytesMarshalJSON(input []byte) []byte {
return naiveQuote(bytesMarshalText(input))
}
// bytesMarshalText encodes the given bytes as a string where each byte is
// represented by a two-digit hex number. The hex string is always even-length
// and prefixed with "0x".
func bytesMarshalText(input []byte) []byte {
return []byte(hexutil.BytesToHex(input))
}
// bytesUnmarshalJSON decodes the given JSON string where each byte is
// represented by a two-digit hex number. The hex string may be prefixed with
// "0x". If the hex string is odd-length, it is padded with a leading zero.
func bytesUnmarshalJSON(input []byte, output *[]byte) error {
if bytes.Equal(input, []byte("null")) {
return nil
}
return bytesUnmarshalText(naiveUnquote(input), output)
}
// bytesUnmarshalText decodes the given string where each byte is represented by
// a two-digit hex number. The hex string may be prefixed with "0x". If the hex
// string is odd-length, it is padded with a leading zero.
func bytesUnmarshalText(input []byte, output *[]byte) error {
var err error
*output, err = hexutil.HexToBytes(string(input))
return err
}
// fixedBytesUnmarshalJSON works like bytesUnmarshalJSON, but it is designed to
// be used with fixed-size byte arrays. The given byte array must be large
// enough to hold the decoded data.
func fixedBytesUnmarshalJSON(input, output []byte) error {
if bytes.Equal(input, []byte("null")) {
return nil
}
return fixedBytesUnmarshalText(naiveUnquote(input), output)
}
// fixedBytesUnmarshalText works like bytesUnmarshalText, but it is designed to
// be used with fixed-size byte arrays. The given byte array must be large
// enough to hold the decoded data.
func fixedBytesUnmarshalText(input, output []byte) error {
data, err := hexutil.HexToBytes(string(input))
if err != nil {
return err
}
if len(data) != len(output) {
return fmt.Errorf("invalid length %d, want %d", len(data), len(output))
}
copy(output, data)
return nil
}
// numberMarshalJSON encodes the given big integer as JSON string where number
// is resented in hexadecimal format. The hex string is prefixed with "0x".
// Negative numbers are prefixed with "-0x".
func numberMarshalJSON(input *big.Int) []byte {
return naiveQuote(numberMarshalText(input))
}
// numberMarshalText encodes the given big integer as string where number is
// resented in hexadecimal format. The hex string is prefixed with "0x".
// Negative numbers are prefixed with "-0x".
func numberMarshalText(input *big.Int) []byte {
return []byte(hexutil.BigIntToHex(input))
}
// numberUnmarshalJSON decodes the given JSON string where number is resented in
// hexadecimal format. The hex string may be prefixed with "0x". Negative numbers
// must start with minus sign.
func numberUnmarshalJSON(input []byte, output *big.Int) error {
return numberUnmarshalText(naiveUnquote(input), output)
}
// numberUnmarshalText decodes the given string where number is resented in
// hexadecimal format. The hex string may be prefixed with "0x". Negative numbers
// must start with minus sign.
func numberUnmarshalText(input []byte, output *big.Int) error {
data, err := hexutil.HexToBigInt(string(input))
if err != nil {
return err
}
output.Set(data)
return nil
}
// naiveQuote returns a double-quoted string. It does not perform any escaping.
func naiveQuote(i []byte) []byte {
b := make([]byte, len(i)+2)
b[0] = '"'
b[len(b)-1] = '"'
copy(b[1:], i)
return b
}
// naiveUnquote returns the string inside the quotes. It does not perform any
// unescaping.
func naiveUnquote(i []byte) []byte {
if len(i) >= 2 && i[0] == '"' && i[len(i)-1] == '"' {
return i[1 : len(i)-1]
}
return i
}