forked from sero-cash/go-sero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
350 lines (299 loc) · 6.83 KB
/
json.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package ethapi
import (
"bytes"
"math/big"
"github.com/sero-cash/go-sero/common/address"
"github.com/sero-cash/go-sero/common"
"github.com/sero-cash/go-sero/zero/txtool"
"github.com/btcsuite/btcutil/base58"
"github.com/sero-cash/go-czero-import/c_type"
"github.com/sero-cash/go-czero-import/superzk"
"github.com/pkg/errors"
"github.com/sero-cash/go-czero-import/seroparam"
)
type decError struct{ msg string }
func (err decError) Error() string { return err.msg }
var (
ErrEmptyString = &decError{"empty input string"}
ErrSyntax = &decError{"invalid hex string"}
ErrMissingPrefix = &decError{"hex string without 0x prefix"}
ErrOddLength = &decError{"hex string of odd length"}
ErrUint64Range = &decError{"hex number > 64 bits"}
)
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')
}
type Big big.Int
func (b Big) MarshalJSON() ([]byte, error) {
i := big.Int(b)
by, err := i.MarshalJSON()
if err != nil {
return nil, err
}
if seroparam.IsExchangeValueStr() {
bytes := make([]byte, len(by)+2)
copy(bytes[1:len(bytes)-1], by[:])
bytes[0] = '"'
bytes[len(bytes)-1] = '"'
return bytes, nil
} else {
return by, err
}
}
// UnmarshalJSON implements json.Unmarshaler.
func (b *Big) UnmarshalJSON(input []byte) error {
if isString(input) {
input = input[1 : len(input)-1]
}
i := big.Int{}
if e := i.UnmarshalText(input); e != nil {
return e
} else {
*b = Big(i)
return nil
}
}
func (b *Big) ToInt() *big.Int {
return (*big.Int)(b)
}
type PKrAddress [96]byte
func (b PKrAddress) ToPKr() *c_type.PKr {
result := &c_type.PKr{}
copy(result[:], b[:])
return result
}
func (b PKrAddress) MarshalText() ([]byte, error) {
return []byte(b.String()), nil
}
func (b PKrAddress) String() string {
return base58.Encode(b[:])
}
func (b PKrAddress) Base58() string {
return base58.Encode(b[:])
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *PKrAddress) UnmarshalText(input []byte) error {
if len(input) == 0 {
return nil
}
out, err := address.DecodeAddr(input)
if err != nil {
return err
}
err = address.ValidPkr(out)
if err != nil {
return err
}
copy(b[:], out)
return nil
}
type MixAdrress []byte
func (b MixAdrress) MarshalText() ([]byte, error) {
return []byte(base58.Encode(b)), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *MixAdrress) UnmarshalText(input []byte) error {
if len(input) == 0 {
return nil
}
out, err := address.DecodeAddr(input)
if err != nil {
return err
}
if len(out) == 96 {
err := address.ValidPkr(out)
if err != nil {
return err
}
*b = out[:]
return nil
} else if len(out) == 64 {
err := address.ValidPk(out)
if err != nil {
return err
}
*b = out[:]
return nil
} else {
return errors.New("invalid mix address")
}
}
type AllMixedAddress [96]byte
func (b *AllMixedAddress) setBytes(bs []byte) {
copy(b[:], bs)
}
func (b AllMixedAddress) ToPKrAddress() (ret PKrAddress) {
copy(ret[:], b[:])
return
}
func (b AllMixedAddress) IsContract() bool {
empty := common.Hash{}
if len(b) == 96 {
if bytes.Compare(b[64:], empty[:]) == 0 {
return true
}
}
return false
}
func (b AllMixedAddress) ToPKr() (ret c_type.PKr) {
copy(ret[:], b[:])
return
}
func (b AllMixedAddress) MarshalText() ([]byte, error) {
if b.IsContract() {
return []byte(base58.Encode(b[:64])), nil
} else {
return []byte(base58.Encode(b[:])), nil
}
}
func IsContract(b []byte) (bool, error) {
addr := common.Address{}
copy(addr[:], b)
return txtool.Ref_inst.Bc.IsContract(addr)
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *AllMixedAddress) UnmarshalText(input []byte) error {
if len(input) == 0 {
return ErrEmptyString
}
out, err := address.DecodeAddr(input)
if err != nil {
return err
}
if len(out) == 96 {
if isContract, err := IsContract(out); err != nil {
return err
} else {
if isContract {
copy(b[:], out)
return nil
} else {
err := address.ValidPkr(out)
if err != nil {
return err
}
copy(b[:], out)
return nil
}
}
} else if len(out) == 64 {
contract_addr := common.Address{}
copy(contract_addr[:], out)
if isContract, err := txtool.Ref_inst.Bc.IsContract(contract_addr); err != nil {
return err
} else {
if isContract {
copy(b[:], out)
return nil
} else {
err := address.ValidPk(out)
if err != nil {
return err
}
pk := c_type.Uint512{}
copy(pk[:], out[:])
pkr := superzk.Pk2PKr(&pk, nil)
copy(b[:], pkr[:])
}
}
} else {
return errors.New("AllMixedAddress must be length 64 or 96")
}
return nil
}
type ContractAddress c_type.PKr
func (b *ContractAddress) SetBytes(bs []byte) {
copy(b[:], bs)
}
func (b ContractAddress) MarshalText() ([]byte, error) {
return []byte(base58.Encode(b[:64])), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *ContractAddress) UnmarshalText(input []byte) error {
if len(input) == 0 {
return ErrEmptyString
}
out, err := address.DecodeAddr(input)
if err != nil {
return err
}
if len(out) == 96 {
addr := common.Address{}
copy(addr[:], out)
if isContract, err := txtool.Ref_inst.Bc.IsContract(addr); err != nil {
return err
} else {
if isContract {
copy(b[:], out)
return nil
} else {
return errors.New("this 96 bytes not contract address")
}
}
} else if len(out) == 64 {
contract_addr := common.Address{}
copy(contract_addr[:], out)
if isContract, err := txtool.Ref_inst.Bc.IsContract(contract_addr); err != nil {
return err
} else {
if isContract {
copy(b[:], contract_addr[:])
return nil
} else {
return errors.New("this 64 bytes not contract address")
}
}
} else {
return errors.New("ContractAddress must be length 64 or 96")
}
}
type AllBase58Adrress []byte
func (b AllBase58Adrress) MarshalText() ([]byte, error) {
return []byte(base58.Encode(b)), nil
}
func (b AllBase58Adrress) String() string {
return base58.Encode(b)
}
func (b AllBase58Adrress) Bytes() []byte {
return []byte(b)
}
func (b AllBase58Adrress) ToPkr(isContract bool) (ret c_type.PKr) {
if len(b) == 96 {
copy(ret[:], b[:])
} else {
if isContract {
copy(ret[:], b[:])
} else {
var pk c_type.Uint512
copy(pk[:], b[:])
ret = superzk.Pk2PKr(&pk, nil)
}
}
return
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *AllBase58Adrress) UnmarshalText(input []byte) error {
if len(input) == 0 {
return nil
}
out, err := address.DecodeAddr(input)
if err != nil {
return err
}
if len(out) == 96 {
err := address.ValidPkr(out)
if err != nil {
return err
}
*b = out[:]
return nil
} else if len(out) == 64 {
*b = out[:]
return nil
} else {
return errors.New("invalid base58 address")
}
}