17
17
package common
18
18
19
19
import (
20
- "encoding/hex"
21
- "encoding/json"
22
- "errors"
23
20
"fmt"
24
21
"math/big"
25
22
"math/rand"
26
23
"reflect"
27
- "strings"
24
+
25
+ "github.com/ethereum/go-ethereum/common/hexutil"
28
26
)
29
27
30
28
const (
31
29
HashLength = 32
32
30
AddressLength = 20
33
31
)
34
32
35
- var hashJsonLengthErr = errors .New ("common: unmarshalJSON failed: hash must be exactly 32 bytes" )
36
-
37
33
type (
38
34
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
39
35
Hash [HashLength ]byte
@@ -57,30 +53,16 @@ func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
57
53
func (h Hash ) Str () string { return string (h [:]) }
58
54
func (h Hash ) Bytes () []byte { return h [:] }
59
55
func (h Hash ) Big () * big.Int { return Bytes2Big (h [:]) }
60
- func (h Hash ) Hex () string { return "0x" + Bytes2Hex (h [:]) }
56
+ func (h Hash ) Hex () string { return hexutil . Encode (h [:]) }
61
57
62
58
// UnmarshalJSON parses a hash in its hex from to a hash.
63
59
func (h * Hash ) UnmarshalJSON (input []byte ) error {
64
- length := len (input )
65
- if length >= 2 && input [0 ] == '"' && input [length - 1 ] == '"' {
66
- input = input [1 : length - 1 ]
67
- }
68
- // strip "0x" for length check
69
- if len (input ) > 1 && strings .ToLower (string (input [:2 ])) == "0x" {
70
- input = input [2 :]
71
- }
72
-
73
- // validate the length of the input hash
74
- if len (input ) != HashLength * 2 {
75
- return hashJsonLengthErr
76
- }
77
- h .SetBytes (FromHex (string (input )))
78
- return nil
60
+ return hexutil .UnmarshalJSON ("Hash" , input , h [:])
79
61
}
80
62
81
63
// Serialize given hash to JSON
82
64
func (h Hash ) MarshalJSON () ([]byte , error ) {
83
- return json . Marshal ( h . Hex () )
65
+ return hexutil . Bytes ( h [:]). MarshalJSON ( )
84
66
}
85
67
86
68
// Sets the hash to the value of b. If b is larger than len(h) it will panic
@@ -142,7 +124,7 @@ func (a Address) Str() string { return string(a[:]) }
142
124
func (a Address ) Bytes () []byte { return a [:] }
143
125
func (a Address ) Big () * big.Int { return Bytes2Big (a [:]) }
144
126
func (a Address ) Hash () Hash { return BytesToHash (a [:]) }
145
- func (a Address ) Hex () string { return "0x" + Bytes2Hex (a [:]) }
127
+ func (a Address ) Hex () string { return hexutil . Encode (a [:]) }
146
128
147
129
// Sets the address to the value of b. If b is larger than len(a) it will panic
148
130
func (a * Address ) SetBytes (b []byte ) {
@@ -164,34 +146,12 @@ func (a *Address) Set(other Address) {
164
146
165
147
// Serialize given address to JSON
166
148
func (a Address ) MarshalJSON () ([]byte , error ) {
167
- return json . Marshal ( a . Hex () )
149
+ return hexutil . Bytes ( a [:]). MarshalJSON ( )
168
150
}
169
151
170
152
// Parse address from raw json data
171
- func (a * Address ) UnmarshalJSON (data []byte ) error {
172
- if len (data ) > 2 && data [0 ] == '"' && data [len (data )- 1 ] == '"' {
173
- data = data [1 : len (data )- 1 ]
174
- }
175
-
176
- if len (data ) > 2 && data [0 ] == '0' && data [1 ] == 'x' {
177
- data = data [2 :]
178
- }
179
-
180
- if len (data ) != 2 * AddressLength {
181
- return fmt .Errorf ("Invalid address length, expected %d got %d bytes" , 2 * AddressLength , len (data ))
182
- }
183
-
184
- n , err := hex .Decode (a [:], data )
185
- if err != nil {
186
- return err
187
- }
188
-
189
- if n != AddressLength {
190
- return fmt .Errorf ("Invalid address" )
191
- }
192
-
193
- a .Set (HexToAddress (string (data )))
194
- return nil
153
+ func (a * Address ) UnmarshalJSON (input []byte ) error {
154
+ return hexutil .UnmarshalJSON ("Address" , input , a [:])
195
155
}
196
156
197
157
// PP Pretty Prints a byte slice in the following format:
0 commit comments