-
Notifications
You must be signed in to change notification settings - Fork 2
/
ThresholdSha256.go
183 lines (149 loc) · 3.61 KB
/
ThresholdSha256.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
package CryptoConditions
import (
"encoding/binary"
"errors"
"github.com/jtremback/crypto-conditions/encoding"
)
type WeightedString struct {
Weight uint32
String []byte
}
type WeightedStrings []WeightedString
func ParseWeightedStrings(b []byte) (WeightedStrings, error) {
bs := encoding.ParseVarray(b)
ws := WeightedStrings{}
for _, b := range bs {
w, b, err := encoding.GetUvarint(b)
if err != nil {
return nil, err
}
s, _, err := encoding.GetVarbyte(b)
if err != nil {
return nil, err
}
ws = append(ws, WeightedString{
Weight: uint32(w),
String: s,
})
}
return ws, nil
}
func (a WeightedStrings) Len() int { return len(a) }
func (a WeightedStrings) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a WeightedStrings) Less(i, j int) bool {
// Sort lexicographically if the lengths are equal
if len(a[i].String) == len(a[j].String) {
return string(a[i].String) < string(a[j].String)
}
// Sort by length otherwise
return len(a[i].String) < len(a[j].String)
}
func (wss *WeightedStrings) WriteTo(b []byte) {
i := 0
for _, ws := range *wss {
// write weight
i = binary.PutUvarint(b[i:], uint64(ws.Weight))
// prefix length
i = binary.PutUvarint(b[i:], uint64(len(ws.String)))
// write fulfillment or condition
b = append(b[i:], ws.String...)
}
}
func ParseFulfillment(b []byte) (uint16, []byte, error) {
typ, b, err := encoding.GetUvarint(b)
if err != nil {
return 0, []byte{}, err
}
payload, b, err := encoding.GetVarbyte(b)
if err != nil {
return 0, []byte{}, err
}
return uint16(typ), payload, nil
}
type ThresholdSha256Fulfillment struct {
Threshold uint32
SubFulfillments WeightedStrings
}
func ParseThresholdSha256Fulfillment(payload []byte) (*ThresholdSha256Fulfillment, error) {
threshold, b, err := encoding.GetUvarint(payload)
if err != nil {
return nil, err
}
f, b, err := encoding.GetVarbyte(b)
if err != nil {
return nil, err
}
subFulfillments, err := ParseWeightedStrings(f)
if err != nil {
return nil, err
}
ful := &ThresholdSha256Fulfillment{
Threshold: uint32(threshold),
SubFulfillments: subFulfillments,
}
return ful, nil
}
func (ful *ThresholdSha256Fulfillment) Serialize() []byte {
b := []byte{}
// write threshold
i := binary.PutUvarint(b, uint64(ful.Threshold))
// write subfulfillments
ful.SubFulfillments.WriteTo(b[i:])
return b
}
func Validate(fulfillment []byte, message []byte) error {
typ, payload, err := ParseFulfillment(fulfillment)
if err != nil {
return err
}
switch typ {
case 2:
err := ThresholdSha256Validate(payload, message)
if err != nil {
return err
}
return nil
case 4:
err := Ed25519Validate(payload, message)
if err != nil {
return err
}
return nil
default:
return errors.New("Unrecognized fulfillment type")
}
}
func ThresholdSha256Validate(payload []byte, message []byte) error {
ful, err := ParseThresholdSha256Fulfillment(payload)
if err != nil {
return err
}
var fulfilled uint32
for _, sf := range ful.SubFulfillments {
err := Validate(sf.String, message)
if err != nil {
return err
}
fulfilled += sf.Weight
}
if fulfilled < ful.Threshold {
return errors.New("Not enough fulfillments")
}
return nil
}
func (ful *ThresholdSha256Fulfillment) Condition() Condition {
subconditions := make(WeightedStrings, len(ful.SubFulfillments))
for i, sf := range ful.SubFulfillments {
subconditions[i] = WeightedString{
Weight: sf.Weight,
String: sf.String,
}
}
// Still need to sort
return Condition{
Type: 2,
FeatureBitmask: []byte{0x09},
// Fingerprint: sha256.Sum256()[:],
MaxFulfillmentLength: 96,
}
}