/
did.go
512 lines (427 loc) · 13.6 KB
/
did.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
package types
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"log"
"regexp"
"strings"
"github.com/gogo/protobuf/jsonpb"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/btcsuite/btcutil/base58"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
)
const (
DIDMethod = "re"
Base58Charset = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
)
type JSONStringOrStrings []string
func EmptyDIDs(strings []string) bool {
if len(strings) == 0 {
return true
}
for _, did := range strings {
if !EmptyDID(did) {
return false
}
}
return true
}
func ValidateDIDs(strings []string) bool {
if EmptyDIDs(strings) {
return false
}
for _, did := range strings {
if !ValidateDID(did) {
return false
}
}
return true
}
func (strings JSONStringOrStrings) protoType() *Strings {
values := make([]string, 0, len(strings))
for _, s := range strings {
values = append(values, s)
}
return &Strings{values}
}
func (strings JSONStringOrStrings) Marshal() ([]byte, error) {
return proto.Marshal(strings.protoType())
}
func (strings *JSONStringOrStrings) MarshalTo(data []byte) (n int, err error) {
return strings.protoType().MarshalTo(data)
}
func (strings *JSONStringOrStrings) Unmarshal(data []byte) error {
protoType := &Strings{}
if err := proto.Unmarshal(data, protoType); err != nil {
return err
}
*strings = protoType.Values
return nil
}
func (strings JSONStringOrStrings) Size() int {
return strings.protoType().Size()
}
func (strings JSONStringOrStrings) MarshalJSON() ([]byte, error) {
if len(strings) == 1 { // if only one, treat it as a single string
return json.Marshal(strings[0])
}
return json.Marshal([]string(strings)) // if not, as a list
}
func (strings *JSONStringOrStrings) UnmarshalJSON(data []byte) error {
var single string
err := json.Unmarshal(data, &single)
if err == nil {
*strings = JSONStringOrStrings{single}
return nil
}
var multiple []string
if err := json.Unmarshal(data, &multiple); err != nil {
return err
}
*strings = multiple
return nil
}
var _ sdk.CustomProtobufType = &JSONStringOrStrings{}
func NewDID(pubKey []byte) string {
hash := sha256.New()
_, err := hash.Write(pubKey)
if err != nil {
panic("failed to calculate SHA256 for DID")
}
idStr := base58.Encode(hash.Sum(nil))
return fmt.Sprintf("did:%s:%s", DIDMethod, idStr)
}
func ParseDID(str string) (string, error) {
did := str
if !ValidateDID(did) {
return "", sdkerrors.Wrapf(ErrInvalidDID, "did: %v", str)
}
return did, nil
}
func ValidateDID(did string) bool {
pattern := fmt.Sprintf("^%s$", didRegex())
matched, _ := regexp.MatchString(pattern, did)
return matched
}
func ValidateContexts(contexts []string) bool {
if len(contexts) == 0 || contexts[0] != ContextDIDV1 { // the 1st one must be ContextDIDV1
return false
}
set := make(map[string]struct{}, len(contexts))
for _, context := range contexts {
_, dup := set[context] // check the duplication
if dup || !ValidateContext(context) {
return false
}
set[context] = struct{}{}
}
return true
}
func ValidateContext(context string) bool {
return context != ""
}
func didRegex() string {
// https://www.w3.org/TR/did-core/#did-syntax
return fmt.Sprintf("did:%s:[%s]{32,44}", DIDMethod, Base58Charset)
}
func EmptyDID(did string) bool {
return did == ""
}
func NewDIDDocument(id string, opts ...DIDDocumentOption) DIDDocument {
doc := DIDDocument{
Contexts: &JSONStringOrStrings{ContextDIDV1},
Id: id,
}
for _, opt := range opts {
opt(&doc)
}
return doc
}
type DIDDocumentOption func(opts *DIDDocument)
func WithController(controller string) DIDDocumentOption {
return func(opts *DIDDocument) {
opts.Controller = &JSONStringOrStrings{controller}
}
}
func WithVerificationMethods(verificationMethods []*VerificationMethod) DIDDocumentOption {
return func(opts *DIDDocument) {
opts.VerificationMethods = verificationMethods
}
}
func WithAuthentications(authentications []VerificationRelationship) DIDDocumentOption {
return func(opts *DIDDocument) {
opts.Authentications = authentications
}
}
func WithAssertionMethods(assertionMethods []VerificationRelationship) DIDDocumentOption {
return func(opts *DIDDocument) {
opts.AssertionMethods = assertionMethods
}
}
func WithKeyAgreements(keyAgreements []VerificationRelationship) DIDDocumentOption {
return func(opts *DIDDocument) {
opts.KeyAgreements = keyAgreements
}
}
func WithCapabilityInvocations(capabilityInvocations []VerificationRelationship) DIDDocumentOption {
return func(opts *DIDDocument) {
opts.CapabilityInvocations = capabilityInvocations
}
}
func WithCapabilityDelegations(capabilityDelegations []VerificationRelationship) DIDDocumentOption {
return func(opts *DIDDocument) {
opts.CapabilityDelegations = capabilityDelegations
}
}
func WithServices(services []*Service) DIDDocumentOption {
return func(opts *DIDDocument) {
opts.Services = services
}
}
func (doc DIDDocument) Valid() bool {
if doc.Empty() { // deactivated
return true
}
if !ValidateDID(doc.Id) || doc.VerificationMethods == nil || doc.Authentications == nil {
return false
}
if doc.Controller != nil && !EmptyDIDs(*doc.Controller) && !ValidateDIDs(*doc.Controller) {
return false
}
if doc.Contexts != nil && !ValidateContexts(*doc.Contexts) {
return false
}
for _, verificationMethod := range doc.VerificationMethods {
if !verificationMethod.Valid(doc.Id) {
return false
}
}
if !doc.validVerificationRelationships(doc.Authentications) {
return false
}
if !doc.validVerificationRelationships(doc.AssertionMethods) {
return false
}
if !doc.validVerificationRelationships(doc.KeyAgreements) {
return false
}
if !doc.validVerificationRelationships(doc.CapabilityInvocations) {
return false
}
if !doc.validVerificationRelationships(doc.CapabilityDelegations) {
return false
}
for _, service := range doc.Services {
if !service.Valid() {
return false
}
}
return true
}
func (doc DIDDocument) validVerificationRelationships(relationships []VerificationRelationship) bool {
for _, relationship := range relationships {
if !relationship.Valid(doc.Id) {
return false
}
if !relationship.hasDedicatedMethod() {
// if the relationship isn't a dedicated verification method,
// the referenced verification method must be presented in the 'verificationMethod' property.
if _, ok := doc.VerificationMethodByID(relationship.GetVerificationMethodId()); !ok {
return false
}
}
}
return true
}
func (doc DIDDocument) Empty() bool {
return EmptyDID(doc.Id)
}
// GetSignBytes returns a byte array which is used to generate a signature for verifying DID ownership.
func (doc DIDDocument) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&doc))
}
// VerificationMethodByID finds a VerificationMethod by ID.
// If the corresponding VerificationMethod doesn't exist, it returns a false.
func (doc DIDDocument) VerificationMethodByID(id string) (VerificationMethod, bool) {
for _, verificationMethod := range doc.VerificationMethods {
if verificationMethod.Id == id {
return *verificationMethod, true
}
}
return VerificationMethod{}, false
}
// VerificationMethodFrom finds a VerificationMethod from the slice of VerificationRelationship by its ID.
// There are two types of VerificationRelationship. If it has a dedicated VerificationMethod, it is returned as it is.
// If the relationship has only a ID of VerificationMethod, this function tries to find a corresponding VerificationMethod in the DIDDocument.
func (doc DIDDocument) VerificationMethodFrom(relationships []VerificationRelationship, id string) (VerificationMethod, bool) {
for _, relationship := range relationships {
if relationship.hasDedicatedMethod() {
veriMethod := relationship.GetVerificationMethod()
if veriMethod.Id == id {
return *veriMethod, true
}
} else {
veriMethodID := relationship.GetVerificationMethodId()
if veriMethodID == id {
return doc.VerificationMethodByID(veriMethodID)
}
}
}
return VerificationMethod{}, false
}
const (
ContextDIDV1 = "https://www.w3.org/ns/did/v1"
)
func NewVerificationMethodID(did string, name string) string {
// https://www.w3.org/TR/did-core/#fragment
return fmt.Sprintf("%v#%s", did, name)
}
func ParseVerificationMethodID(id string, did string) (string, error) {
methodID := id
if !ValidateVerificationMethodID(id, did) {
return "", sdkerrors.Wrapf(ErrInvalidVerificationMethodID, "verificationMethodID: %v, did: %v", id, did)
}
return methodID, nil
}
const (
MaxVerificationMethodIDLen = 128
)
func ValidateVerificationMethodID(verificationMethodID string, did string) bool {
prefix := fmt.Sprintf("%v#", did)
if !strings.HasPrefix(verificationMethodID, prefix) {
return false
}
// Limit the length because it can be used for keystore filenames.
// Max filename length on Linux is usually 256 bytes.
if len(verificationMethodID)-len(prefix) > MaxVerificationMethodIDLen {
return false
}
suffix := verificationMethodID[len(prefix):]
matched, _ := regexp.MatchString(`^\S+$`, suffix) // no whitespace
return matched
}
const (
JSONWebKey2020 = "JsonWebKey2020"
EcdsaSecp256k1VerificationKey2019 = "EcdsaSecp256k1VerificationKey2019"
Secp256k1VerificationKey2018 = "Secp256k1VerificationKey2018" // deprecated
Ed25519VerificationKey2018 = "Ed25519VerificationKey2018"
Bls12381G1Key2020 = "Bls12381G1Key2020"
Bls12381G2Key2020 = "Bls12381G2Key2020"
GpgVerificationKey2020 = "GpgVerificationKey2020"
RsaVerificationKey2018 = "RsaVerificationKey2018"
X25519KeyAgreementKey2019 = "X25519KeyAgreementKey2019"
SchnorrSecp256k1VerificationKey2019 = "SchnorrSecp256k1VerificationKey2019"
EcdsaSecp256k1RecoveryMethod2020 = "EcdsaSecp256k1RecoveryMethod2020"
)
func ValidateKeyType(keyType string) bool {
switch keyType {
case JSONWebKey2020,
EcdsaSecp256k1VerificationKey2019,
Secp256k1VerificationKey2018, // deprecated
Ed25519VerificationKey2018,
Bls12381G1Key2020,
Bls12381G2Key2020,
GpgVerificationKey2020,
RsaVerificationKey2018,
X25519KeyAgreementKey2019,
SchnorrSecp256k1VerificationKey2019,
EcdsaSecp256k1RecoveryMethod2020:
return true
}
if keyType == "" {
return false
}
log.Printf("[warn] unknown key type: %s\n", keyType) // TODO: Use tendermint logger
return true
}
func NewVerificationMethod(id string, keyType string, controller string, pubKey []byte) VerificationMethod {
return VerificationMethod{
Id: id,
Type: keyType,
Controller: controller,
PublicKeyBase58: base58.Encode(pubKey),
}
}
func (pk VerificationMethod) Valid(did string) bool {
if !ValidateVerificationMethodID(pk.Id, did) || !ValidateKeyType(pk.Type) {
return false
}
pattern := fmt.Sprintf("^[%s]+$", Base58Charset)
matched, _ := regexp.MatchString(pattern, pk.PublicKeyBase58)
return matched
}
func NewVerificationRelationship(verificationMethodID string) VerificationRelationship {
return VerificationRelationship{
Content: &VerificationRelationship_VerificationMethodId{VerificationMethodId: verificationMethodID},
}
}
func NewVerificationRelationshipDedicated(verificationMethod VerificationMethod) VerificationRelationship {
return VerificationRelationship{
Content: &VerificationRelationship_VerificationMethod{VerificationMethod: &verificationMethod},
}
}
func (v VerificationRelationship) hasDedicatedMethod() bool {
return v.GetVerificationMethod() != nil
}
func (v VerificationRelationship) Valid(did string) bool {
if v.hasDedicatedMethod() {
return v.GetVerificationMethod().Valid(did)
}
return ValidateVerificationMethodID(v.GetVerificationMethodId(), did)
}
func (v VerificationRelationship) MarshalJSON() ([]byte, error) {
if v.hasDedicatedMethod() {
return json.Marshal(v.GetVerificationMethod())
}
return json.Marshal(v.GetVerificationMethodId())
}
func (v *VerificationRelationship) UnmarshalJSON(bz []byte) error {
// if not dedicated
var verificationMethodID string
err := json.Unmarshal(bz, &verificationMethodID)
if err == nil {
*v = NewVerificationRelationship(verificationMethodID)
return nil
}
// if dedicated
// Use jsonpb to handle camelCase as well as snake_case
var verificationMethod VerificationMethod
if err := jsonpb.Unmarshal(bytes.NewReader(bz), &verificationMethod); err != nil {
return err
}
*v = NewVerificationRelationshipDedicated(verificationMethod)
return nil
}
func NewService(id string, serviceType string, serviceEndpoint string) Service {
return Service{Id: id, Type: serviceType, ServiceEndpoint: serviceEndpoint}
}
func (s Service) Valid() bool {
return s.Id != "" && s.Type != "" && s.ServiceEndpoint != ""
}
func NewDIDDocumentWithSeq(doc *DIDDocument, seq uint64, creator string) DIDDocumentWithSeq {
return DIDDocumentWithSeq{
Document: doc,
Sequence: seq,
Creator: creator,
}
}
// Empty returns true if all members in DIDDocumentWithSeq are empty.
// The empty struct means that the entity doesn't exist.
func (d DIDDocumentWithSeq) Empty() bool {
return d.Document == nil || d.Document.Empty() && d.Sequence == InitialSequence
}
func (d DIDDocumentWithSeq) Valid() bool {
return d.Document.Valid()
}
// Deactivate creates a new DIDDocumentWithSeq with an empty DIDDocument (tombstone).
// Note that it requires a new sequence.
func (d DIDDocumentWithSeq) Deactivate(newSeq uint64, creator string) DIDDocumentWithSeq {
return NewDIDDocumentWithSeq(&DIDDocument{}, newSeq, creator)
}
// Deactivated returns true if the DIDDocument has been activated.
func (d DIDDocumentWithSeq) Deactivated() bool {
return d.Document.Empty() && d.Sequence != InitialSequence
}