This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
276 lines (236 loc) · 6.6 KB
/
main.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
package event
import (
"errors"
"fmt"
"github.com/decentralized-identity/kerigo/pkg/derivation"
"github.com/decentralized-identity/kerigo/pkg/prefix"
"github.com/decentralized-identity/kerigo/pkg/version"
)
type EventOption func(*Event) error
// WithKeys sets the keys that are applicable for the event
func WithKeys(keys ...prefix.Prefix) EventOption {
return func(e *Event) error {
for i := 0; i < len(keys); i++ {
k := keys[i].String()
e.Keys = append(e.Keys, k)
}
return nil
}
}
// WithWitnesses sets the witness keys for the event
func WithWitnesses(keys ...prefix.Prefix) EventOption {
return func(e *Event) error {
for i := 0; i < len(keys); i++ {
k := keys[i].String()
e.Witnesses = append(e.Keys, k)
}
return nil
}
}
// WithNext keys must be self adressing prefixs. Do not use a basic prefix
// otherwise the public key data will be exposed in the log breaking post-quantum
// security.
// To support multi-sig, next is a prefix of the commitment to a signing threshold
// along with all of the keys to be rotated to, combined using XOR.
// Each of the provided keys, along with the derivation to use for the next,
// must use the same derivaiton code.
func WithNext(threshold string, code derivation.Code, keys ...prefix.Prefix) EventOption {
return func(e *Event) error {
next, err := NextDigest(threshold, code, keys...)
if err != nil {
return err
}
e.Next = next
return nil
}
}
// WithThreshold sets the key threshold
func WithThreshold(threshold int64) EventOption {
return func(e *Event) error {
st, err := NewSigThreshold(threshold)
if err != nil {
return err
}
e.SigThreshold = st
return nil
}
}
// WithWeightedTheshold sets a weighted signing threshold using provided
// string int or fraction values. The total for all conditions must be
// >= 1 otherwise the threshold can not be met. The order in which
// the conditions are provided is important: they map to the specific
// key index in the keys list, e.g, the second condition provided to this
// configuration function would be the weight of a signature by the second key
// in the keys list.
func WithWeightedTheshold(conditions ...string) EventOption {
return func(e *Event) error {
st, err := NewWeighted(conditions...)
if err != nil {
return err
}
e.SigThreshold = st
return nil
}
}
// WithMultiWeightedThesholds sets multiple weighted signing thresholds using provided
// string values.
func WithMultiWeightedThesholds(thresholds ...[]string) EventOption {
return func(e *Event) error {
st, err := NewMultiWeighted(thresholds...)
if err != nil {
return err
}
e.SigThreshold = st
return nil
}
}
// WithWitnessThreshold sets the witness duplicity threshold for the event
func WithWitnessThreshold(threshold int) EventOption {
return func(e *Event) error {
e.WitnessThreshold = fmt.Sprintf("%x", threshold)
return nil
}
}
// WithType specifies the event type
func WithType(eventType ILK) EventOption {
return func(e *Event) error {
e.EventType = ilkString[eventType]
return nil
}
}
/// WithSequence sets the sequence number for this event
func WithSequence(sequence int) EventOption {
return func(e *Event) error {
e.Sequence = fmt.Sprintf("%x", sequence)
return nil
}
}
// WithDigest sets the digest for the event
func WithDigest(digest string) EventOption {
return func(e *Event) error {
e.PriorEventDigest = digest
return nil
}
}
func WithDefaultVersion(in FORMAT) EventOption {
return func(e *Event) error {
e.Version = DefaultVersionString(in)
return nil
}
}
func WithPrefix(prefix string) EventOption {
return func(e *Event) error {
e.Prefix = prefix
return nil
}
}
func WithSeals(seals SealArray) EventOption {
return func(e *Event) error {
e.Seals = seals
return nil
}
}
// NewEvent returns a new event with the specified options applied
func NewEvent(opts ...EventOption) (*Event, error) {
st, _ := NewSigThreshold(1)
e := &Event{
Sequence: "0",
SigThreshold: st,
WitnessThreshold: "0",
Witnesses: []string{},
Config: []prefix.Trait{},
}
for _, o := range opts {
err := o(e)
if err != nil {
return nil, err
}
}
if e.EventType == "" {
return nil, errors.New("must specify an event type")
}
if (e.EventType != ilkString[ICP] && e.EventType != ilkString[VRC] && e.EventType != ilkString[RCT]) && e.Sequence == "0" {
return nil, errors.New("only inception events may have a sequence of 0")
}
return e, nil
}
// NewInceptionEvent returns and incpetion configured with the provided parameters
// New Inception Events will have empty 'v' and 'i' strings
func NewInceptionEvent(opts ...EventOption) (*Event, error) {
st, _ := NewSigThreshold(1)
e := &Event{
EventType: ilkString[ICP],
Sequence: "0",
SigThreshold: st,
WitnessThreshold: "0",
Witnesses: []string{},
Config: []prefix.Trait{},
}
for _, o := range opts {
err := o(e)
if err != nil {
return nil, err
}
}
return e, nil
}
// NewRotationEvent returns and incpetion configured with the provided parameters
// New Rotation Events will have empty 'v' and 'i' strings
func NewRotationEvent(opts ...EventOption) (*Event, error) {
sith, _ := NewSigThreshold(1)
rot := &Event{
EventType: ilkString[ROT],
Sequence: "0",
SigThreshold: sith,
WitnessThreshold: "0",
Witnesses: []string{},
Config: []prefix.Trait{},
}
for _, o := range opts {
err := o(rot)
if err != nil {
return nil, err
}
}
if rot.Prefix == "" {
return nil, errors.New("prefix required for rot")
}
if rot.Next == "" {
return nil, errors.New("next commitment required for rot")
}
// Serialize with defaults to get correct length for version string
if rot.Version == "" {
rot.Version = DefaultVersionString(JSON)
}
eventBytes, err := Serialize(rot, JSON)
if err != nil {
return nil, err
}
rot.Version = VersionString(JSON, version.Code(), len(eventBytes))
return rot, nil
}
// NewRotationEvent returns and incpetion configured with the provided parameters
// New Rotation Events will have empty 'v' and 'i' strings
func NewInteractionEvent(opts ...EventOption) (*Event, error) {
rot := &Event{
EventType: ilkString[IXN],
Sequence: "0",
}
for _, o := range opts {
err := o(rot)
if err != nil {
return nil, err
}
}
if rot.Prefix == "" {
return nil, errors.New("prefix required for ixn")
}
// Serialize with defaults to get correct length for version string
rot.Version = DefaultVersionString(JSON)
eventBytes, err := Serialize(rot, JSON)
if err != nil {
return nil, err
}
rot.Version = VersionString(JSON, version.Code(), len(eventBytes))
return rot, nil
}