-
Notifications
You must be signed in to change notification settings - Fork 75
/
mdstream.go
420 lines (364 loc) · 12.6 KB
/
mdstream.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright (c) 2019-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package mdstream
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"strings"
pd "github.com/decred/politeia/politeiad/api/v1"
"github.com/decred/politeia/politeiad/api/v1/identity"
cms "github.com/decred/politeia/politeiawww/api/cms/v1"
"github.com/decred/politeia/util"
)
const (
// mdstream IDs
IDInvalid = 0
IDRecordStatusChange = 2
IDInvoiceGeneral = 3
IDInvoiceStatusChange = 4
IDInvoicePayment = 5
IDDCCGeneral = 6
IDDCCStatusChange = 7
IDDCCSupportOpposition = 8
// mdstream current supported versions
VersionRecordStatusChange = 2
VersionInvoiceGeneral = 1
VersionInvoiceStatusChange = 1
VersionInvoicePayment = 1
VersionDCCGeneral = 1
VersionDCCStatusChange = 1
VersionDCCSupposeOpposition = 1
)
// DecodeVersion returns the version of the provided mstream payload. This
// function should only be used when the payload contains a single struct with
// a version field.
func DecodeVersion(payload []byte) (uint, error) {
data := make(map[string]interface{}, 32)
err := json.Unmarshal(payload, &data)
if err != nil {
return 0, err
}
version := uint(data["version"].(float64))
if version == 0 {
return 0, fmt.Errorf("version not found")
}
return version, nil
}
// RecordStatusChangeV1 represents a politeiad record status change and is used
// to store additional status change metadata that would not otherwise be
// captured by the politeiad status change routes.
//
// This mdstream is used by both pi and cms.
type RecordStatusChangeV1 struct {
Version uint `json:"version"` // Version of the struct
AdminPubKey string `json:"adminpubkey"` // Identity of the administrator
NewStatus pd.RecordStatusT `json:"newstatus"` // New status
StatusChangeMessage string `json:"statuschangemessage,omitempty"` // Change message
Timestamp int64 `json:"timestamp"` // UNIX timestamp
}
// EncodeRecordStatusChangeV1 encodes an RecordStatusChangeV1 into a JSON byte
// slice.
func EncodeRecordStatusChangeV1(rsc RecordStatusChangeV1) ([]byte, error) {
b, err := json.Marshal(rsc)
if err != nil {
return nil, err
}
return b, nil
}
// RecordStatusChangeV2 represents a politeiad record status change and is used
// to store additional status change metadata that would not otherwise be
// captured by the politeiad status change routes.
//
// V2 adds the Signature field, which was erroneously left out of V1.
//
// This mdstream is used by both pi and cms.
type RecordStatusChangeV2 struct {
Version uint `json:"version"` // Struct version
NewStatus pd.RecordStatusT `json:"newstatus"` // New status
StatusChangeMessage string `json:"statuschangemessage,omitempty"` // Change message
Signature string `json:"signature"` // Signature of (Token + NewStatus + StatusChangeMessage)
AdminPubKey string `json:"adminpubkey"` // Signature pubkey
Timestamp int64 `json:"timestamp"` // UNIX timestamp
}
// VerifySignature verifies that the RecordStatusChangeV2 signature is correct.
func (r *RecordStatusChangeV2) VerifySignature(token string) error {
sig, err := util.ConvertSignature(r.Signature)
if err != nil {
return err
}
b, err := hex.DecodeString(r.AdminPubKey)
if err != nil {
return err
}
pk, err := identity.PublicIdentityFromBytes(b)
if err != nil {
return err
}
msg := token + strconv.Itoa(int(r.NewStatus)) + r.StatusChangeMessage
if !pk.VerifyMessage([]byte(msg), sig) {
return fmt.Errorf("invalid signature")
}
return nil
}
// EncodeRecordStatusChangeV2 encodes an RecordStatusChangeV2 into a JSON byte
// slice.
func EncodeRecordStatusChangeV2(rsc RecordStatusChangeV2) ([]byte, error) {
b, err := json.Marshal(rsc)
if err != nil {
return nil, err
}
return b, nil
}
// DecodeRecordStatusChanges decodes a JSON byte slice into a slice of
// RecordStatusChangeV1 and a slice of RecordStatusChangeV2.
func DecodeRecordStatusChanges(payload []byte) ([]RecordStatusChangeV1, []RecordStatusChangeV2, error) {
statusesV1 := make([]RecordStatusChangeV1, 0, 16)
statusesV2 := make([]RecordStatusChangeV2, 0, 16)
d := json.NewDecoder(strings.NewReader(string(payload)))
for {
// Decode json into a map so we can determine the version.
statusChange := make(map[string]interface{}, 6)
err := d.Decode(&statusChange)
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return nil, nil, err
}
// These fields exist in all status change versions so they
// can be converted to their appropriate types outside the
// switch statement.
// Note: a JSON number has to first be type cast to float64
var (
version = uint(statusChange["version"].(float64))
newStatus = pd.RecordStatusT(int(statusChange["newstatus"].(float64)))
pubkey = statusChange["adminpubkey"].(string)
ts = int64(statusChange["timestamp"].(float64))
)
// The status change message is optional
var msg string
m, ok := statusChange["statuschangemessage"]
if ok {
msg = m.(string)
}
// Handle different versions
switch version {
case 1:
statusesV1 = append(statusesV1, RecordStatusChangeV1{
Version: version,
NewStatus: newStatus,
StatusChangeMessage: msg,
AdminPubKey: pubkey,
Timestamp: ts,
})
case 2:
statusesV2 = append(statusesV2, RecordStatusChangeV2{
Version: version,
NewStatus: newStatus,
StatusChangeMessage: msg,
AdminPubKey: pubkey,
Signature: statusChange["signature"].(string),
Timestamp: ts,
})
default:
return nil, nil, fmt.Errorf("invalid status change version: %v",
statusChange["version"])
}
}
return statusesV1, statusesV2, nil
}
// InvoiceGeneral represents the general metadata for an invoice and is
// stored in the metadata IDInvoiceGeneral in politeiad.
type InvoiceGeneral struct {
Version uint64 `json:"version"` // Version of the struct
Timestamp int64 `json:"timestamp"` // Last update of invoice
PublicKey string `json:"publickey"` // Key used for signature
Signature string `json:"signature"` // Signature of merkle root
}
// EncodeInvoiceGeneral encodes a InvoiceGeneral into a JSON
// byte slice.
func EncodeInvoiceGeneral(md InvoiceGeneral) ([]byte, error) {
b, err := json.Marshal(md)
if err != nil {
return nil, err
}
return b, nil
}
// DecodeInvoiceGeneral decodes a JSON byte slice into an InvoiceGeneral.
func DecodeInvoiceGeneral(payload []byte) (*InvoiceGeneral, error) {
var md InvoiceGeneral
err := json.Unmarshal(payload, &md)
if err != nil {
return nil, err
}
return &md, nil
}
// InvoiceStatusChange represents an invoice status change and is stored
// in the metadata IDInvoiceStatusChange in politeiad.
type InvoiceStatusChange struct {
Version uint `json:"version"` // Version of the struct
AdminPublicKey string `json:"adminpublickey"` // Identity of the administrator
NewStatus cms.InvoiceStatusT `json:"newstatus"` // Status
Reason string `json:"reason"` // Reason
Timestamp int64 `json:"timestamp"` // Timestamp of the change
}
// EncodeInvoiceStatusChange encodes a InvoiceStatusChange into a
// JSON byte slice.
func EncodeInvoiceStatusChange(md InvoiceStatusChange) ([]byte, error) {
b, err := json.Marshal(md)
if err != nil {
return nil, err
}
return b, nil
}
// DecodeInvoiceStatusChange decodes a JSON byte slice into a slice of
// InvoiceStatusChanges.
func DecodeInvoiceStatusChange(payload []byte) ([]InvoiceStatusChange, error) {
var md []InvoiceStatusChange
d := json.NewDecoder(strings.NewReader(string(payload)))
for {
var m InvoiceStatusChange
err := d.Decode(&m)
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return nil, err
}
md = append(md, m)
}
return md, nil
}
// InvoicePayment represents an invoice payment and is stored
// in the metadata IDInvoicePayment in politeiad.
type InvoicePayment struct {
Version uint `json:"version"` // Version of the struct
TxIDs string `json:"txids"` // TxIDs captured from the payment, separated by commas
Timestamp int64 `json:"timeupdated"` // Time of last payment update
AmountReceived int64 `json:"amountreceived"` // Amount of DCR payment currently received
}
// EncodeInvoicePayment encodes a InvoicePayment into a JSON byte slice.
func EncodeInvoicePayment(md InvoicePayment) ([]byte, error) {
b, err := json.Marshal(md)
if err != nil {
return nil, err
}
return b, nil
}
// DecodeInvoicePayment decodes a JSON byte slice into an InvoicePayment.
func DecodeInvoicePayment(payload []byte) ([]InvoicePayment, error) {
var md []InvoicePayment
d := json.NewDecoder(strings.NewReader(string(payload)))
for {
var m InvoicePayment
err := d.Decode(&m)
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return nil, err
}
md = append(md, m)
}
return md, nil
}
// DCCGeneral represents the general metadata for a DCC and is
// stored in the metadata stream IDDCCGeneral in politeiad.
type DCCGeneral struct {
Version uint64 `json:"version"` // Version of the struct
Timestamp int64 `json:"timestamp"` // Last update of invoice
PublicKey string `json:"publickey"` // Key used for signature
Signature string `json:"signature"` // Signature of merkle root
}
// EncodeDCCGeneral encodes a DCCGeneral into a JSON
// byte slice.
func EncodeDCCGeneral(md DCCGeneral) ([]byte, error) {
b, err := json.Marshal(md)
if err != nil {
return nil, err
}
return b, nil
}
// DecodeDCCGeneral decodes a JSON byte slice into a
// DCCGeneral.
func DecodeDCCGeneral(payload []byte) (*DCCGeneral, error) {
var md DCCGeneral
err := json.Unmarshal(payload, &md)
if err != nil {
return nil, err
}
return &md, nil
}
// DCCStatusChange represents the metadata for any status change that
// occurs to a patricular DCC issuance or revocation.
type DCCStatusChange struct {
Version uint `json:"version"` // Version of the struct
AdminPublicKey string `json:"adminpublickey"` // Identity of the administrator
NewStatus cms.DCCStatusT `json:"newstatus"` // Status
Reason string `json:"reason"` // Reason
Timestamp int64 `json:"timestamp"` // Timestamp of the change
Signature string `json:"signature"` // Signature of Token + NewStatus + Reason
}
// EncodeDCCStatusChange encodes a DCCStatusChange into a
// JSON byte slice.
func EncodeDCCStatusChange(md DCCStatusChange) ([]byte, error) {
b, err := json.Marshal(md)
if err != nil {
return nil, err
}
return b, nil
}
// DecodeDCCStatusChange decodes a JSON byte slice into a slice of
// DCCStatusChange.
func DecodeDCCStatusChange(payload []byte) ([]DCCStatusChange, error) {
var md []DCCStatusChange
d := json.NewDecoder(strings.NewReader(string(payload)))
for {
var m DCCStatusChange
err := d.Decode(&m)
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return nil, err
}
md = append(md, m)
}
return md, nil
}
// DCCSupportOpposition represents the general metadata for a DCC
// Support/Opposition 'vote' for a given DCC proposal.
type DCCSupportOpposition struct {
Version uint64 `json:"version"` // Version of the struct
Timestamp int64 `json:"timestamp"` // Last update of invoice
PublicKey string `json:"publickey"` // Key used for signature
Vote string `json:"vote"` // Vote for support/opposition
Signature string `json:"signature"` // Signature of Token + Vote
}
// EncodeDCCSupportOpposition encodes a DCCSupportOpposition into a JSON
// byte slice.
func EncodeDCCSupportOpposition(md DCCSupportOpposition) ([]byte, error) {
b, err := json.Marshal(md)
if err != nil {
return nil, err
}
return b, nil
}
// DecodeDCCSupportOpposition decodes a JSON byte slice into a
// DCCSupportOpposition.
func DecodeDCCSupportOpposition(payload []byte) ([]DCCSupportOpposition, error) {
var md []DCCSupportOpposition
d := json.NewDecoder(strings.NewReader(string(payload)))
for {
var m DCCSupportOpposition
err := d.Decode(&m)
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return nil, err
}
md = append(md, m)
}
return md, nil
}