-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.go
532 lines (479 loc) · 13 KB
/
actions.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// ami actions implements the authentication functionality on top of breeze
// void action.
package attorney
import (
"encoding/json"
"fmt"
"github.com/freehandle/breeze/crypto"
"github.com/freehandle/breeze/protocol/actions"
"github.com/freehandle/breeze/util"
)
const voidTailSize = 2*crypto.TokenSize + 2*crypto.SignatureSize + 8
var AxeProtocolCode = [4]byte{1, 0, 0, 0}
type ActionValidator interface {
Epoch() uint64
HasCaption(crypto.Hash) bool
HasMember(crypto.Hash) bool
SetNewMember(crypto.Hash, crypto.Hash) bool
PowerOfAttorney(crypto.Hash) bool
SetNewGrantPower(crypto.Hash) bool
SetNewRevokePower(crypto.Hash) bool
}
func GetHashes(data []byte) []crypto.Hash {
tokens := GetTokens(data)
hashes := make([]crypto.Hash, len(tokens))
for n, token := range tokens {
hashes[n] = crypto.HashToken(token)
}
return hashes
}
func GetTokens(data []byte) []crypto.Token {
kind := Kind(data)
switch kind {
case JoinNetworkType:
if join := ParseJoinNetwork(data); join != nil {
return join.Tokens()
}
case UpdateInfoType:
if update := ParseUpdateInfo(data); update != nil {
return update.Tokens()
}
case GrantPowerOfAttorneyType:
if grant := ParseGrantPowerOfAttorney(data); grant != nil {
return grant.Tokens()
}
case RevokePowerOfAttorneyType:
if revoke := ParseRevokePowerOfAttorney(data); revoke != nil {
return revoke.Tokens()
}
case VoidType:
if void := ParseVoid(data); void != nil {
return void.Tokens()
}
}
return nil
}
const (
VoidType byte = iota
JoinNetworkType
UpdateInfoType
GrantPowerOfAttorneyType
RevokePowerOfAttorneyType
Invalid
)
func Kind(data []byte) byte {
if len(data) < 14 {
return Invalid
}
if data[0] != 0 || data[1] != actions.IVoid || data[10] != 1 {
return Invalid
}
return data[14]
}
type JoinNetwork struct {
Epoch uint64
Author crypto.Token
Handle string
Details string
Signature crypto.Signature
}
func (j *JoinNetwork) Tokens() []crypto.Token {
return []crypto.Token{j.Author}
}
func (j *JoinNetwork) Validate(v ActionValidator) bool {
captionHash := crypto.Hasher([]byte(j.Handle))
if v.HasCaption(captionHash) {
return false
}
authorHash := crypto.Hasher(j.Author[:])
if v.HasMember(authorHash) {
return false
}
if !json.Valid([]byte(j.Details)) {
return false
}
return v.SetNewMember(authorHash, captionHash)
}
func (j *JoinNetwork) Kind() byte {
return JoinNetworkType
}
func (j *JoinNetwork) serializeToSign() []byte {
bytes := []byte{0, actions.IVoid} // breeze (version 0) void action
util.PutUint64(j.Epoch, &bytes)
util.PutByte(1, &bytes)
util.PutByte(0, &bytes)
util.PutByte(0, &bytes)
util.PutByte(0, &bytes)
util.PutByte(JoinNetworkType, &bytes)
util.PutToken(j.Author, &bytes)
util.PutString(j.Handle, &bytes)
util.PutString(j.Details, &bytes)
return bytes
}
func (j *JoinNetwork) Serialize() []byte {
bytes := j.serializeToSign()
util.PutSignature(j.Signature, &bytes)
return bytes
}
func (j *JoinNetwork) Sign(key crypto.PrivateKey) {
bytes := j.serializeToSign()
j.Signature = key.Sign(bytes)
}
func ParseJoinNetwork(data []byte) *JoinNetwork {
if data[0] != 0 || data[1] != actions.IVoid || len(data) < 14 {
fmt.Println(3)
return nil
}
join := JoinNetwork{}
position := 2
join.Epoch, position = util.ParseUint64(data, position)
// check if it is pure axe protocol
if data[position] != 1 || data[position+1] != 0 || data[position+2] != 0 || data[position+3] != 0 {
return nil
}
if data[position+4] != JoinNetworkType {
return nil
}
position = position + 5
join.Author, position = util.ParseToken(data, position)
join.Handle, position = util.ParseString(data, position)
join.Details, position = util.ParseString(data, position)
if len(join.Details) > 0 && !json.Valid([]byte(join.Details)) {
return nil
}
hashPosition := position
join.Signature, position = util.ParseSignature(data, position)
if position > len(data) {
return nil
}
if !join.Author.Verify(data[0:hashPosition], join.Signature) {
return nil
}
return &join
}
type UpdateInfo struct {
Epoch uint64
Author crypto.Token
Details string
Signer crypto.Token
Signature crypto.Signature
}
func (u *UpdateInfo) Tokens() []crypto.Token {
if u.Signer.Equal(u.Author) {
return []crypto.Token{u.Author}
} else {
return []crypto.Token{u.Author, u.Signer}
}
}
func (u *UpdateInfo) Validate(v ActionValidator) bool {
if !v.HasMember(crypto.HashToken(u.Author)) {
return false
}
if !u.Signer.Equal(u.Author) {
hash := crypto.Hasher(append(u.Author[:], u.Signer[:]...))
if !v.PowerOfAttorney(hash) {
return false
}
}
return json.Valid([]byte(u.Details))
}
func (u *UpdateInfo) Kind() byte {
return UpdateInfoType
}
func (u *UpdateInfo) Serialize() []byte {
bytes := u.serializeToSign()
util.PutSignature(u.Signature, &bytes)
return bytes
}
func (u *UpdateInfo) serializeToSign() []byte {
bytes := []byte{0, actions.IVoid}
util.PutUint64(u.Epoch, &bytes)
util.PutByte(1, &bytes)
util.PutByte(0, &bytes)
util.PutByte(0, &bytes)
util.PutByte(0, &bytes)
util.PutByte(UpdateInfoType, &bytes)
util.PutToken(u.Author, &bytes)
util.PutString(u.Details, &bytes)
util.PutToken(u.Signer, &bytes)
return bytes
}
func (u *UpdateInfo) Sign(pk crypto.PrivateKey) {
bytes := u.serializeToSign()
u.Signature = pk.Sign(bytes)
}
func ParseUpdateInfo(data []byte) *UpdateInfo {
if data[0] != 0 || data[1] != actions.IVoid || len(data) < 14 {
return nil
}
update := UpdateInfo{}
position := 2
update.Epoch, position = util.ParseUint64(data, position)
// check if it is pure axe protocol
if data[position] != 1 || data[position+1] != 0 || data[position+2] != 0 || data[position+3] != 0 {
return nil
}
if data[position+4] != UpdateInfoType {
return nil
}
position = position + 5
update.Author, position = util.ParseToken(data, position)
update.Details, position = util.ParseString(data, position)
if !json.Valid([]byte(update.Details)) {
return nil
}
update.Signer, position = util.ParseToken(data, position)
hashPosition := position
update.Signature, position = util.ParseSignature(data, position)
if position > len(data) {
return nil
}
if !update.Author.Verify(data[0:hashPosition], update.Signature) {
return nil
}
return &update
}
type GrantPowerOfAttorney struct {
Epoch uint64
Author crypto.Token
Attorney crypto.Token
Fingerprint []byte
Signature crypto.Signature
}
func (g *GrantPowerOfAttorney) Tokens() []crypto.Token {
return []crypto.Token{g.Author, g.Attorney}
}
func (g *GrantPowerOfAttorney) Validate(v ActionValidator) bool {
if !v.HasMember(crypto.HashToken(g.Author)) {
return false
}
hash := crypto.Hasher(append(g.Author[:], g.Attorney[:]...))
if v.PowerOfAttorney(hash) {
return false
}
return v.SetNewGrantPower(hash)
}
func (g *GrantPowerOfAttorney) Kind() byte {
return GrantPowerOfAttorneyType
}
func (g *GrantPowerOfAttorney) Serialize() []byte {
bytes := g.serializeToSign()
util.PutSignature(g.Signature, &bytes)
return bytes
}
func (g *GrantPowerOfAttorney) serializeToSign() []byte {
bytes := []byte{0, actions.IVoid}
util.PutUint64(g.Epoch, &bytes)
util.PutByte(1, &bytes)
util.PutByte(0, &bytes)
util.PutByte(0, &bytes)
util.PutByte(0, &bytes)
util.PutByte(GrantPowerOfAttorneyType, &bytes)
util.PutToken(g.Author, &bytes)
util.PutByteArray(g.Fingerprint, &bytes)
util.PutToken(g.Attorney, &bytes)
return bytes
}
func (g *GrantPowerOfAttorney) Sign(pk crypto.PrivateKey) {
bytes := g.serializeToSign()
g.Signature = pk.Sign(bytes)
}
func ParseGrantPowerOfAttorney(data []byte) *GrantPowerOfAttorney {
if data[0] != 0 || data[1] != actions.IVoid || len(data) < 14 {
return nil
}
grant := GrantPowerOfAttorney{}
position := 2
grant.Epoch, position = util.ParseUint64(data, position)
// check if it is pure axe protocol
if data[position] != 1 || data[position+1] != 0 || data[position+2] != 0 || data[position+3] != 0 {
return nil
}
if data[position+4] != GrantPowerOfAttorneyType {
return nil
}
position = position + 5
grant.Author, position = util.ParseToken(data, position)
grant.Fingerprint, position = util.ParseByteArray(data, position)
grant.Attorney, position = util.ParseToken(data, position)
hashPosition := position
grant.Signature, position = util.ParseSignature(data, position)
if position > len(data) {
return nil
}
if !grant.Author.Verify(data[0:hashPosition], grant.Signature) {
return nil
}
return &grant
}
type RevokePowerOfAttorney struct {
Epoch uint64
Author crypto.Token
Attorney crypto.Token
Signature crypto.Signature
}
func (r *RevokePowerOfAttorney) Tokens() []crypto.Token {
return []crypto.Token{r.Author, r.Attorney}
}
func (r *RevokePowerOfAttorney) Validate(v ActionValidator) bool {
if !v.HasMember(crypto.HashToken(r.Author)) {
return false
}
hash := crypto.Hasher(append(r.Author[:], r.Attorney[:]...))
if !v.PowerOfAttorney(hash) {
return false
}
return v.SetNewRevokePower(hash)
}
func (r *RevokePowerOfAttorney) Kind() byte {
return RevokePowerOfAttorneyType
}
func (r *RevokePowerOfAttorney) serializeToSign() []byte {
bytes := []byte{0, actions.IVoid}
util.PutUint64(r.Epoch, &bytes)
util.PutByte(1, &bytes)
util.PutByte(0, &bytes)
util.PutByte(0, &bytes)
util.PutByte(0, &bytes)
util.PutByte(RevokePowerOfAttorneyType, &bytes)
util.PutToken(r.Author, &bytes)
util.PutToken(r.Attorney, &bytes)
return bytes
}
func (r *RevokePowerOfAttorney) Serialize() []byte {
bytes := r.serializeToSign()
util.PutSignature(r.Signature, &bytes)
return bytes
}
func (r *RevokePowerOfAttorney) Sign(pk crypto.PrivateKey) {
bytes := r.serializeToSign()
r.Signature = pk.Sign(bytes)
}
func ParseRevokePowerOfAttorney(data []byte) *RevokePowerOfAttorney {
if data[0] != 0 || data[1] != actions.IVoid || len(data) < 14 {
return nil
}
revoke := RevokePowerOfAttorney{}
position := 2
revoke.Epoch, position = util.ParseUint64(data, position)
// check if it is pure axe protocol
if data[position] != 1 || data[position+1] != 0 || data[position+2] != 0 || data[position+3] != 0 {
return nil
}
if data[position+4] != RevokePowerOfAttorneyType {
return nil
}
position = position + 5
revoke.Author, position = util.ParseToken(data, position)
revoke.Attorney, position = util.ParseToken(data, position)
hashPosition := position
revoke.Signature, position = util.ParseSignature(data, position)
if position > len(data) {
return nil
}
if !revoke.Author.Verify(data[0:hashPosition], revoke.Signature) {
return nil
}
return &revoke
}
type Void struct {
Epoch uint64
Protocol uint32
Author crypto.Token
Data []byte
Signer crypto.Token
Signature crypto.Signature
}
func (g *Void) Tokens() []crypto.Token {
if g.Author.Equal(g.Signer) {
return []crypto.Token{g.Author}
} else {
return []crypto.Token{g.Author, g.Signer}
}
}
func (void *Void) Validate(v ActionValidator) bool {
if void.Epoch > v.Epoch() {
return false
}
if !v.HasMember(crypto.HashToken(void.Author)) {
return false
}
if !void.Signer.Equal(void.Author) {
hash := crypto.Hasher(append(void.Author[:], void.Signer[:]...))
if !v.PowerOfAttorney(hash) {
return false
}
}
return true
}
func (v *Void) Kind() byte {
return VoidType
}
func (v *Void) serializeToSign() []byte {
bytes := []byte{0, actions.IVoid}
util.PutUint64(v.Epoch, &bytes)
util.PutUint32(v.Protocol, &bytes)
util.PutByte(VoidType, &bytes)
util.PutToken(v.Author, &bytes)
bytes = append(bytes, v.Data...)
util.PutToken(v.Signer, &bytes)
return bytes
}
func (v *Void) Serialize() []byte {
bytes := v.serializeToSign()
util.PutSignature(v.Signature, &bytes)
return bytes
}
func (v *Void) Sign(pk crypto.PrivateKey) {
bytes := v.serializeToSign()
v.Signature = pk.Sign(bytes)
}
func ParseVoid(data []byte) *Void {
if data[0] != 0 || data[1] != actions.IVoid || len(data) < 14 {
return nil
}
void := Void{}
position := 2
void.Epoch, position = util.ParseUint64(data, position)
void.Protocol, position = util.ParseUint32(data, position)
if data[position] != VoidType {
return nil
}
position = position + 1
void.Author, position = util.ParseToken(data, position)
if len(data)-voidTailSize < position {
return nil
}
void.Data = data[position : len(data)-voidTailSize]
position = len(data) - voidTailSize
void.Signer, position = util.ParseToken(data, position)
hashPosition := position
void.Signature, position = util.ParseSignature(data, position)
if position > len(data) {
return nil
}
if !void.Signer.Verify(data[0:hashPosition], void.Signature) {
return nil
}
return &void
}
type KeyExchange struct {
Epoch uint64
Author crypto.Token
To crypto.Token
Ephemeral crypto.Token
Secret []byte
Attorney crypto.Token
}
// iisAxeNonVoid checks if a byte array has the header of an axé action different from
// the void action. It does not try to parse the instruction, so there is no guarantee
// that the byte array is a valid axé action.
func IsAxeNonVoid(action []byte) bool {
if len(action) < 15 {
return false
}
if action[0] != 0 || action[1] != 0 || action[10] != 1 || action[11] != 0 || action[12] != 0 || action[13] != 0 || action[14] == 0 {
return false
}
return true
}