/
json.go
282 lines (256 loc) · 6.78 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
// Copyright (c) 2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package dcrd
import (
"bytes"
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"
"strings"
"decred.org/dcrwallet/errors"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/gcs/v2"
blockcf "github.com/decred/dcrd/gcs/v2/blockcf2"
"github.com/decred/dcrd/wire"
)
type deserializer interface {
Deserialize(r io.Reader) error
}
type unmarshalFunc func(j []byte) error
func (f *unmarshalFunc) UnmarshalJSON(j []byte) error {
return (*f)(j)
}
// unhex returns a json.Unmarshaler which unmarshals a hex-encoded wire message.
func unhex(msg deserializer) json.Unmarshaler {
f := func(j []byte) error {
if len(j) < 2 || j[0] != '"' || j[len(j)-1] != '"' {
return errors.E(errors.Encoding, "not a string")
}
err := msg.Deserialize(hex.NewDecoder(bytes.NewReader(j[1 : len(j)-1])))
if err != nil {
return errors.E(errors.Encoding, err)
}
return nil
}
return (*unmarshalFunc)(&f)
}
// cfilterV2 implements deserializer to read a version 2 committed filter from
// a io.Reader. Filters are assumed to be serialized as <n filter> with
// consensus-determined B and M values.
//
// Note that this is only the deserializer for the raw filter data and *not*
// for the full response to a cfilterv2 call (see cfilterv2Reply for that).
type cfilterV2 struct {
Filter *gcs.FilterV2
}
func (f *cfilterV2) Deserialize(r io.Reader) error {
b, err := ioutil.ReadAll(r)
if err != nil {
return err
}
f.Filter, err = gcs.FromBytesV2(blockcf.B, blockcf.M, b)
return err
}
func (f *cfilterV2) UnmarshalJSON(j []byte) error {
if len(j)&1 == 1 || j[0] != '"' || j[len(j)-1] != '"' {
return errors.E(errors.Encoding, "cfilterv2 is not an even hex string")
}
b := make([]byte, len(j)/2-1)
_, err := hex.Decode(b, j[1:len(j)-1])
if err != nil {
return errors.E(errors.Encoding, err)
}
f.Filter, err = gcs.FromBytesV2(blockcf.B, blockcf.M, b)
return err
}
// cfilterv2Reply implements the reply to a cfilterv2 query to a dcrd rpc
// server.
type cfilterV2Reply struct {
Filter *cfilterV2 `json:"data"`
ProofIndex uint32 `json:"proofindex"`
ProofHashes *hashes `json:"proofhashes"`
}
// hashes converts the internal []*chainhash.Hash to a []chainhash.Hash used by
// inclusion proofs.
func (r cfilterV2Reply) proofHashes() []chainhash.Hash {
if r.ProofHashes == nil {
return nil
}
h := make([]chainhash.Hash, len(r.ProofHashes.Hashes))
for i, hp := range r.ProofHashes.Hashes {
h[i] = *hp
}
return h
}
// hash implements json.Unmarshaler to decode 32-byte reversed hex hashes.
type hash struct {
Hash *chainhash.Hash
}
func (h *hash) UnmarshalJSON(j []byte) error {
if len(j) != 66 || j[0] != '"' || j[len(j)-1] != '"' {
return errors.E(errors.Encoding, "not a 32-byte hash hex string")
}
h.Hash = new(chainhash.Hash)
_, err := hex.Decode(h.Hash[:], j[1:len(j)-1])
if err != nil {
return errors.E(errors.Encoding, err)
}
// Unreverse hash
for i, j := 0, len(h.Hash)-1; i < j; i, j = i+1, j-1 {
h.Hash[i], h.Hash[j] = h.Hash[j], h.Hash[i]
}
return nil
}
// hashes implements json.Marshaler/Unmarshaler to encode and decode slices of
// hashes as JSON arrays of reversed hex strings.
type hashes struct {
Hashes []*chainhash.Hash
}
func (h *hashes) UnmarshalJSON(j []byte) error {
if bytes.Equal(j, []byte("null")) {
h.Hashes = nil
return nil
}
var array []string
err := json.Unmarshal(j, &array)
if err != nil {
return nil
}
h.Hashes = make([]*chainhash.Hash, len(array))
for i, s := range array {
hash, err := chainhash.NewHashFromStr(s)
if err != nil {
return errors.E(errors.Encoding, err)
}
h.Hashes[i] = hash
}
return nil
}
func (h *hashes) MarshalJSON() ([]byte, error) {
if h.Hashes == nil {
return []byte("null"), nil
}
buf := new(bytes.Buffer)
scratch32 := make([]byte, 32)
scratch64 := make([]byte, 64)
buf.Grow(2 + 67*len(h.Hashes)) // 2 for [], 64+2+1 per hash for strings, quotes, and separator
buf.WriteByte('[')
for i, h := range h.Hashes {
if i != 0 {
buf.WriteByte(',')
}
// Reverse hash into scratch space
for i := 0; i < 32; i++ {
scratch32[31-i] = h[i]
}
// Write hex encoding of reversed hash
hex.Encode(scratch64, scratch32)
buf.WriteByte('"')
buf.Write(scratch64)
buf.WriteByte('"')
}
buf.WriteByte(']')
return buf.Bytes(), nil
}
type hashesContiguous struct {
Hashes []chainhash.Hash
}
func (h *hashesContiguous) UnmarshalJSON(j []byte) error {
if bytes.Equal(j, []byte("null")) {
h.Hashes = nil
return nil
}
var array []string
err := json.Unmarshal(j, &array)
if err != nil {
return nil
}
h.Hashes = make([]chainhash.Hash, len(array))
for i, s := range array {
hash, err := chainhash.NewHashFromStr(s)
if err != nil {
return errors.E(errors.Encoding, err)
}
h.Hashes[i] = *hash
}
return nil
}
func (h *hashesContiguous) MarshalJSON() ([]byte, error) {
if h.Hashes == nil {
return []byte("null"), nil
}
buf := new(bytes.Buffer)
scratch32 := make([]byte, 32)
scratch64 := make([]byte, 64)
buf.Grow(2 + 67*len(h.Hashes)) // 2 for [], 64+2+1 per hash for strings, quotes, and separator
buf.WriteByte('[')
for i, h := range h.Hashes {
if i != 0 {
buf.WriteByte(',')
}
// Reverse hash into scratch space
for i := 0; i < 32; i++ {
scratch32[31-i] = h[i]
}
// Write hex encoding of reversed hash
hex.Encode(scratch64, scratch32)
buf.WriteByte('"')
buf.Write(scratch64)
buf.WriteByte('"')
}
buf.WriteByte(']')
return buf.Bytes(), nil
}
// headers implements json.Unmarshaler to decode JSON arrays of hex-encoded
// serialized block headers.
type headers struct {
Headers []*wire.BlockHeader
}
func (h *headers) UnmarshalJSON(j []byte) error {
if bytes.Equal(j, []byte("null")) {
h.Headers = nil
return nil
}
var array []string
err := json.Unmarshal(j, &array)
if err != nil {
return err
}
h.Headers = make([]*wire.BlockHeader, len(array))
for i, s := range array {
h.Headers[i] = new(wire.BlockHeader)
err = h.Headers[i].Deserialize(hex.NewDecoder(strings.NewReader(s)))
if err != nil {
return errors.E(errors.Encoding, err)
}
}
return nil
}
type transactions struct {
Transactions []*wire.MsgTx
}
func (t *transactions) UnmarshalJSON(j []byte) error {
if bytes.Equal(j, []byte("null")) {
t.Transactions = nil
return nil
}
var array []json.RawMessage
err := json.Unmarshal(j, &array)
if err != nil {
return err
}
t.Transactions = make([]*wire.MsgTx, len(array))
for i, j := range array {
if len(j) < 2 || j[0] != '"' || j[len(j)-1] != '"' {
return errors.E(errors.Encoding, "not a string")
}
t.Transactions[i] = new(wire.MsgTx)
err = t.Transactions[i].Deserialize(hex.NewDecoder(bytes.NewReader(j[1 : len(j)-1])))
if err != nil {
return errors.E(errors.Encoding, err)
}
}
return nil
}