This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
/
conditions.go
593 lines (533 loc) · 13 KB
/
conditions.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package conditions
import (
"bytes"
. "github.com/Envoke-org/envoke-api/common"
"github.com/Envoke-org/envoke-api/crypto/crypto"
"github.com/Envoke-org/envoke-api/crypto/ed25519"
"github.com/Envoke-org/envoke-api/crypto/rsa"
"github.com/Envoke-org/envoke-api/regex"
)
// ILP crypto-conditions
const (
// Params
HASH_SIZE = 32
MAX_PAYLOAD_SIZE = 0xfff
SUPPORTED_BITMASK = 0x3f
// Types
PREIMAGE_ID = 0
PREIMAGE_BITMASK = 0x03
PREFIX_ID = 1
PREFIX_BITMASK = 0x05
THRESHOLD_ID = 2
THRESHOLD_BITMASK = 0x09
RSA_ID = 3
RSA_BITMASK = 0x11
RSA_SIZE = rsa.KEY_SIZE + rsa.SIGNATURE_SIZE
ED25519_ID = 4
ED25519_BITMASK = 0x20
ED25519_SIZE = ed25519.PUBKEY_SIZE + ed25519.SIGNATURE_SIZE
TIMEOUT_ID = 5
TIMEOUT_BITMASK = 0x00
)
// Fulfillment
type Fulfillment interface {
Bitmask() int
Data() Data
FromString(string) error
Hash() []byte
Id() int
Init()
IsCondition() bool
MarshalBinary() ([]byte, error)
PublicKey() crypto.PublicKey
Signature() crypto.Signature
Size() int
String() string
Subfulfillments() Fulfillments
UnmarshalBinary([]byte) error
Validate([]byte) bool
Weight() int
}
// Fufillment from key
func DefaultFulfillmentsFromPrivkeys(msgs [][]byte, privkeys []crypto.PrivateKey) (_ Fulfillments, err error) {
n := len(privkeys)
if n != len(msgs) {
return nil, Error("different number of privkeys and msgs")
}
fulfillments := make(Fulfillments, n)
for i, privkey := range privkeys {
fulfillments[i], err = DefaultFulfillmentFromPrivkey(msgs[i], privkey)
if err != nil {
return nil, err
}
}
return fulfillments, nil
}
func FulfillmentsFromPrivkeys(msgs [][]byte, privkeys []crypto.PrivateKey, weights []int) (_ Fulfillments, err error) {
n := len(privkeys)
if n != len(msgs) {
return nil, Error("different number of privkeys and msgs")
}
if n != len(weights) {
return nil, Error("different number of privkeys and weights")
}
fulfillments := make(Fulfillments, n)
for i, privkey := range privkeys {
fulfillments[i], err = FulfillmentFromPrivkey(msgs[i], privkey, weights[i])
if err != nil {
return nil, err
}
}
return fulfillments, nil
}
func DefaultFulfillmentFromPrivkey(msg []byte, privkey crypto.PrivateKey) (Fulfillment, error) {
return FulfillmentFromPrivkey(msg, privkey, 1)
}
func FulfillmentFromPrivkey(msg []byte, privkey crypto.PrivateKey, weight int) (Fulfillment, error) {
switch privkey.(type) {
case *ed25519.PrivateKey:
privEd25519 := privkey.(*ed25519.PrivateKey)
pubEd25519 := privEd25519.Public().(*ed25519.PublicKey)
sigEd25519 := privEd25519.Sign(msg).(*ed25519.Signature)
return NewFulfillmentEd25519(pubEd25519, sigEd25519, weight), nil
case *rsa.PrivateKey:
privRSA := privkey.(*rsa.PrivateKey)
pubRSA := privRSA.Public().(*rsa.PublicKey)
sigRSA := privRSA.Sign(msg).(*rsa.Signature)
return NewFulfillmentRSA(pubRSA, sigRSA, weight), nil
}
return nil, ErrInvalidType
}
func DefaultFulfillmentsFromPubkeys(pubkeys []crypto.PublicKey) (_ Fulfillments, err error) {
fulfillments := make(Fulfillments, len(pubkeys))
for i, pubkey := range pubkeys {
fulfillments[i], err = DefaultFulfillmentFromPubkey(pubkey)
if err != nil {
return nil, err
}
}
return fulfillments, nil
}
func FulfillmentsFromPubkeys(pubkeys []crypto.PublicKey, weights []int) (_ Fulfillments, err error) {
n := len(pubkeys)
if n != len(weights) {
return nil, Error("different number of pubkeys and weights")
}
fulfillments := make(Fulfillments, n)
for i, pubkey := range pubkeys {
fulfillments[i], err = FulfillmentFromPubKey(pubkey, weights[i])
if err != nil {
return nil, err
}
}
return fulfillments, nil
}
func DefaultFulfillmentFromPubkey(pubkey crypto.PublicKey) (Fulfillment, error) {
return FulfillmentFromPubKey(pubkey, 1)
}
func FulfillmentFromPubKey(pubkey crypto.PublicKey, weight int) (Fulfillment, error) {
switch pubkey.(type) {
case *ed25519.PublicKey:
pubEd25519 := pubkey.(*ed25519.PublicKey)
return NewFulfillmentEd25519(pubEd25519, nil, weight), nil
case *rsa.PublicKey:
pubRSA := pubkey.(*rsa.PublicKey)
return NewFulfillmentRSA(pubRSA, nil, weight), nil
}
return nil, ErrInvalidType
}
func FulfillWithPrivkey(f Fulfillment, msg []byte, privkey crypto.PrivateKey) error {
sig := privkey.Sign(msg)
if !f.PublicKey().Verify(msg, sig) {
return ErrInvalidSignature
}
switch sig.(type) {
case *ed25519.Signature:
ful := f.(*fulfillmentEd25519)
ful.payload = append(ful.payload, sig.Bytes()...)
ful.sig = sig.(*ed25519.Signature)
case *rsa.Signature:
ful := f.(*fulfillmentRSA)
ful.payload = append(ful.payload, sig.Bytes()...)
ful.sig = sig.(*rsa.Signature)
default:
return ErrInvalidType
}
return nil
}
type Fulfillments []Fulfillment
func (fs Fulfillments) Len() int {
return len(fs)
}
// sort in `descending` order by weights, then lexicographically
func (fs Fulfillments) Less(i, j int) bool {
if fs[i].Weight() > fs[j].Weight() {
return true
}
if fs[i].Weight() == fs[j].Weight() {
pi, _ := fs[i].MarshalBinary()
pj, _ := fs[j].MarshalBinary()
return bytes.Compare(pi, pj) == -1
}
return false
}
func (fs Fulfillments) Swap(i, j int) {
fs[i], fs[j] = fs[j], fs[i]
}
func GetCondition(f Fulfillment) *Condition {
if f.IsCondition() {
return f.(*Condition)
}
return NewCondition(f.Bitmask(), f.Hash(), f.Id(), f.PublicKey(), f.Size(), f.Weight())
}
func FulfillmentURI(p []byte) (string, error) {
buf := bytes.NewBuffer(p)
id, err := ReadUint16(buf)
if err != nil {
return "", err
}
hash, err := ReadVarOctet(buf)
if err != nil {
return "", err
}
payload64 := Base64UrlEncode(hash)
return Sprintf("cf:%x:%s", id, payload64), nil
}
func ConditionURI(p []byte) (string, error) {
buf := bytes.NewBuffer(p)
id, err := ReadUint16(buf)
if err != nil {
return "", err
}
bitmask, err := ReadVarUint(buf)
if err != nil {
return "", err
}
hash, err := ReadVarOctet(buf)
if err != nil {
return "", err
}
hash64 := Base64UrlEncode(hash)
size, err := ReadVarUint(buf)
if err != nil {
return "", err
}
return Sprintf("cc:%x:%x:%s:%d", id, bitmask, hash64, size), nil
}
func DefaultUnmarshalBinary(p []byte) (Fulfillment, error) {
return UnmarshalBinary(p, 1)
}
func UnmarshalBinary(p []byte, weight int) (f Fulfillment, err error) {
c := NilCondition()
if err := c.UnmarshalBinary(p); err == nil {
c.weight = weight
return c, nil
}
ful := new(fulfillment)
if err := ful.UnmarshalBinary(p); err != nil {
return nil, err
}
ful.weight = weight
switch ful.id {
case PREIMAGE_ID:
f = &fulfillmentPreImage{ful}
case PREFIX_ID:
f = &fulfillmentPrefix{
fulfillment: ful,
}
case ED25519_ID:
f = &fulfillmentEd25519{
fulfillment: ful,
}
case RSA_ID:
f = &fulfillmentRSA{
fulfillment: ful,
}
case THRESHOLD_ID:
f = &fulfillmentThreshold{
fulfillment: ful,
}
}
f.Init()
if !ful.Validate(nil) {
return nil, ErrInvalidFulfillment
}
return f, nil
}
func DefaultUnmarshalURI(uri string) (Fulfillment, error) {
return UnmarshalURI(uri, 1)
}
func UnmarshalURI(uri string, weight int) (f Fulfillment, err error) {
if MatchStr(regex.CONDITION, uri) {
// Try to parse condition
parts := SplitStr(uri, ":")
c := NilCondition()
c.id, err = ParseUint16(parts[1], 16)
if err != nil {
return nil, err
}
c.bitmask, err = ParseUint32(parts[2], 16)
if err != nil {
return nil, err
}
c.hash, err = Base64UrlDecode(parts[3])
if err != nil {
return nil, err
}
c.size, err = ParseUint16(parts[4], 10)
if err != nil {
return nil, err
}
c.weight = weight
if !c.Validate(nil) {
return nil, ErrInvalidCondition
}
return c, nil
}
if MatchStr(regex.FULFILLMENT, uri) {
// Try to parse non-condition fulfillment
ful := new(fulfillment)
parts := SplitStr(uri, ":")
ful.id, err = ParseUint16(parts[1], 16)
if err != nil {
return nil, err
}
ful.payload, err = Base64UrlDecode(parts[2])
if err != nil {
return nil, err
}
ful.weight = weight
switch ful.id {
case PREIMAGE_ID:
f = &fulfillmentPreImage{ful}
case PREFIX_ID:
f = &fulfillmentPrefix{
fulfillment: ful,
}
case ED25519_ID:
f = &fulfillmentEd25519{
fulfillment: ful,
}
case RSA_ID:
f = &fulfillmentRSA{
fulfillment: ful,
}
case THRESHOLD_ID:
f = &fulfillmentThreshold{
fulfillment: ful,
}
}
f.Init()
if !ful.Validate(nil) {
return nil, ErrInvalidFulfillment
}
return f, nil
}
return nil, ErrInvalidFulfillment
}
type fulfillment struct {
bitmask int
hash []byte
id int
outer Fulfillment
payload []byte
size int
weight int
}
func NewFulfillment(id int, outer Fulfillment, payload []byte, weight int) *fulfillment {
switch id {
case PREIMAGE_ID, PREFIX_ID, ED25519_ID, RSA_ID, THRESHOLD_ID:
//..
default:
Panicf("Unexpected id=%d\n", id)
}
if len(payload) > MAX_PAYLOAD_SIZE {
panic("Exceeds max payload size")
}
if weight < 1 {
panic("Weight cannot be less than 1")
}
return &fulfillment{
id: id,
outer: outer,
payload: payload,
weight: weight,
}
}
func (f *fulfillment) Bitmask() int { return f.bitmask }
func (f *fulfillment) Data() Data { return nil }
func (f *fulfillment) FromString(uri string) (err error) {
if !MatchStr(regex.FULFILLMENT, uri) {
return ErrInvalidFulfillment
}
parts := SplitStr(uri, ":")
f.id, err = ParseUint16(parts[1], 16)
if err != nil {
return err
}
f.payload, err = Base64UrlDecode(parts[2])
if err != nil {
return err
}
if f.outer != nil {
f.outer.Init()
if !f.Validate(nil) {
return ErrInvalidFulfillment
}
}
return nil
}
func (f *fulfillment) Hash() []byte { return f.hash }
func (f *fulfillment) Id() int { return f.id }
func (f *fulfillment) Init() { /* no op */ }
func (f *fulfillment) IsCondition() bool { return false }
func (f *fulfillment) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
WriteUint16(buf, f.id)
WriteVarOctet(buf, f.payload)
return buf.Bytes(), nil
}
func (f *fulfillment) PublicKey() crypto.PublicKey { return nil }
func (f *fulfillment) Signature() crypto.Signature { return nil }
func (f *fulfillment) Size() int { return f.size }
func (f *fulfillment) String() string {
payload64 := Base64UrlEncode(f.payload)
return Sprintf("cf:%x:%s", f.id, payload64)
}
func (f *fulfillment) Subfulfillments() Fulfillments { return nil }
func (f *fulfillment) UnmarshalBinary(p []byte) (err error) {
c := NilCondition()
c.UnmarshalBinary(p)
buf := bytes.NewBuffer(p)
f.id, err = ReadUint16(buf)
if err != nil {
return err
}
f.payload, err = ReadVarOctet(buf)
if err != nil {
return err
}
if f.outer != nil {
f.outer.Init()
if !f.Validate(nil) {
return ErrInvalidFulfillment
}
}
return nil
}
func (f *fulfillment) Validate(p []byte) bool {
switch {
case
f.id == PREIMAGE_ID && f.bitmask == PREIMAGE_BITMASK,
f.id == PREFIX_ID && f.bitmask == PREFIX_BITMASK,
f.id == THRESHOLD_ID && f.bitmask >= THRESHOLD_BITMASK:
//..
case f.id == ED25519_ID && f.bitmask == ED25519_BITMASK:
if f.size != ED25519_SIZE {
return false
}
case f.id == RSA_ID && f.bitmask == RSA_BITMASK:
if f.size != RSA_SIZE {
return false
}
default:
return false
}
switch {
case
f.size > MAX_PAYLOAD_SIZE,
len(f.hash) != HASH_SIZE:
return false
}
return true
}
func (f *fulfillment) Weight() int {
return f.weight
}
// Condition
type Condition struct {
*fulfillment
pubkey crypto.PublicKey
}
func NilCondition() *Condition {
return &Condition{
fulfillment: &fulfillment{},
}
}
func NewCondition(bitmask int, hash []byte, id int, pubkey crypto.PublicKey, size, weight int) *Condition {
c := &Condition{
&fulfillment{
bitmask: bitmask,
hash: hash,
id: id,
size: size,
weight: weight,
}, pubkey,
}
if !c.Validate(nil) {
panic(ErrInvalidCondition)
}
return c
}
func (c *Condition) FromString(uri string) (err error) {
if !MatchStr(regex.CONDITION, uri) {
return ErrInvalidCondition
}
parts := SplitStr(uri, ":")
c.id, err = ParseUint16(parts[1], 16)
if err != nil {
return err
}
c.bitmask, err = ParseUint32(parts[2], 16)
if err != nil {
return err
}
c.hash, err = Base64UrlDecode(parts[3])
if err != nil {
return err
}
c.size, err = ParseUint16(parts[4], 10)
if err != nil {
return err
}
if !c.Validate(nil) {
return ErrInvalidCondition
}
return nil
}
func (c *Condition) IsCondition() bool { return true }
func (c *Condition) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
WriteUint16(buf, c.id)
WriteVarUint(buf, c.bitmask)
WriteVarOctet(buf, c.hash)
WriteVarUint(buf, c.size)
return buf.Bytes(), nil
}
func (c *Condition) String() string {
hash64 := Base64UrlEncode(c.hash)
return Sprintf("cc:%x:%x:%s:%d", c.id, c.bitmask, hash64, c.size)
}
func (c *Condition) UnmarshalBinary(p []byte) (err error) {
buf := bytes.NewBuffer(p)
c.id, err = ReadUint16(buf)
if err != nil {
return err
}
c.bitmask, err = ReadVarUint(buf)
if err != nil {
return err
}
c.hash, err = ReadVarOctet(buf)
if err != nil {
return err
}
c.size, err = ReadUint16(buf)
if err != nil {
return err
}
if !c.Validate(nil) {
return ErrInvalidCondition
}
return nil
}