/
diddoc_validation.go
479 lines (423 loc) · 12.7 KB
/
diddoc_validation.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
package types
import (
"fmt"
"regexp"
)
// isValidDidDoc checks if the DID Id is valid
func isValidDidDocId(id string) error {
inputDocumentIdentifier, err := getDocumentIdentifier(id)
if err != nil {
return err
}
inputDidMethod, err := getDocumentMethod(id)
if err != nil {
return err
}
inputMSI, inputMSIType, err := GetMethodSpecificIdAndType(id)
if err != nil {
return err
}
// Validate Document Identifier
if inputDocumentIdentifier != DocumentIdentifierDid {
return fmt.Errorf(
"document identifier should be %s",
DocumentIdentifierDid,
)
}
// Validate DID Method
if inputDidMethod != DidMethod {
return fmt.Errorf(
"DID method should be %s",
DidMethod,
)
}
// Validate Method Specific ID
switch inputMSIType {
case MSIBlockchainAccountId:
if err := validateBlockchainAccountId(inputMSI); err != nil {
return err
}
case MSINonBlockchainAccountId:
// Non Blockchain Account ID should be a string that supports alphanumeric characters,
// and dot (.) and hypen (-). The first character MUST NOT be dot (.) or hyphen (-).
isValidMSI, err := regexp.MatchString(
"^[a-zA-Z0-9][a-zA-Z0-9.-]*$",
inputMSI,
)
if err != nil {
return err
}
if !isValidMSI {
return fmt.Errorf(
"method-specific-id of non BlockchainAccountId type %v should only contain alphanumeric, dot (.) and hyphen (-)",
inputMSI,
)
}
default:
return fmt.Errorf("invalid method specific id type: %v", inputMSIType)
}
return nil
}
// isDidUrl checks if the input is a DID Url or not.
// More on DID Url syntax: https://www.w3.org/TR/did-core/#did-url-syntax
// TODO: add check for path and query
func isDidUrl(id string) error {
didId, fragment := GetElementsFromDidUrl(id)
// check for fragment
if didId == "" || fragment == "" {
return fmt.Errorf(
"invalid didUrl %s",
id,
)
}
// check for DID Id
err := isValidDidDocId(didId)
if err != nil {
return err
}
return nil
}
func isSupportedVmType(typ string) error {
if _, supported := VerificationKeySignatureMap[typ]; !supported {
return fmt.Errorf(
"%v verification method type not supported, supported verification method types: %v ",
typ,
supportedVerificationMethodTypes,
)
}
return nil
}
// verificationKeyCheck validates of publicKeyMultibase and blockchainAccountId.
func verificationKeyCheck(vm *VerificationMethod) error {
switch vm.Type {
case EcdsaSecp256k1VerificationKey2019:
if vm.GetPublicKeyMultibase() == "" {
return fmt.Errorf(
"publicKeyMultibase cannot be empty for verification method %s as it is type %s",
vm.Id,
vm.Type,
)
}
case EcdsaSecp256k1RecoveryMethod2020:
if vm.GetBlockchainAccountId() == "" {
return fmt.Errorf(
"blockchainAccountId cannot be empty for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
if vm.GetPublicKeyMultibase() != "" {
return fmt.Errorf(
"publicKeyMultibase should not be provided for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
case Ed25519VerificationKey2020:
if vm.GetBlockchainAccountId() != "" {
return fmt.Errorf(
"blockchainAccountId is currently not supported for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
if vm.GetPublicKeyMultibase() == "" {
return fmt.Errorf(
"publicKeyMultibase cannot be empty for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
case X25519KeyAgreementKey2020:
if vm.GetPublicKeyMultibase() == "" {
return fmt.Errorf(
"publicKeyMultibase cannot be empty for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
if vm.GetBlockchainAccountId() != "" {
return fmt.Errorf(
"blockchainAccountId must be empty for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
case X25519KeyAgreementKeyEIP5630:
if vm.GetPublicKeyMultibase() == "" {
return fmt.Errorf(
"publicKeyMultibase cannot be empty for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
if vm.GetBlockchainAccountId() != "" {
return fmt.Errorf(
"blockchainAccountId must be empty for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
case Bls12381G2Key2020:
if vm.GetBlockchainAccountId() != "" {
return fmt.Errorf(
"blockchainAccountId is currently not supported for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
if vm.GetPublicKeyMultibase() == "" {
return fmt.Errorf(
"publicKeyMultibase cannot be empty for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
case BabyJubJubKey2021:
if vm.GetBlockchainAccountId() != "" {
return fmt.Errorf(
"blockchainAccountId is currently not supported for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
if vm.GetPublicKeyMultibase() == "" {
return fmt.Errorf(
"publicKeyMultibase cannot be empty for verification method %s as it is of type %s",
vm.Id,
vm.Type,
)
}
default:
return fmt.Errorf("unsupported verification method type: %v. Supported verification method types are: %v", vm.Type, supportedVerificationMethodTypes)
}
// validate blockchainAccountId
if vm.BlockchainAccountId != "" {
err := validateBlockchainAccountId(vm.BlockchainAccountId)
if err != nil {
return fmt.Errorf("invalid blockchainAccount Id %v: "+err.Error(), vm.BlockchainAccountId)
}
}
return nil
}
// validateServices validates the Service attribute of DID Document
func validateServices(services []*Service) error {
for _, service := range services {
var err error
// validate service Id
if err = isDidUrl(service.Id); err != nil {
return fmt.Errorf("service ID %s is Invalid", service.Id)
}
// validate service Type
foundType := false
for _, sType := range SupportedServiceTypes {
if service.Type == sType {
foundType = true
}
}
if !foundType {
return fmt.Errorf("service Type %s is Invalid", service.Type)
}
}
// check if any duplicate service id exists
serviceIdList := []string{}
for _, service := range services {
serviceIdList = append(serviceIdList, service.Id)
}
if duplicateId := checkDuplicateItems(serviceIdList); duplicateId != "" {
return fmt.Errorf("duplicate service found with Id: %s ", duplicateId)
}
return nil
}
// validateVerificationMethods validates all the verification methods present in DID Document
func validateVerificationMethods(vms []*VerificationMethod) error {
for _, vm := range vms {
var err error
// Vm Id check
err = isDidUrl(vm.Id)
if err != nil {
return err
}
// Vm Type check
err = isSupportedVmType(vm.Type)
if err != nil {
return err
}
// Controller check
err = isValidDidDocId(vm.Controller)
if err != nil {
return err
}
// blockchainAccountId and publicKeyMultibase check
err = verificationKeyCheck(vm)
if err != nil {
return err
}
}
// check duplicate Vm Ids, publicKeyMultibase and blockchainAccountId
vmIdList := []string{}
publicKeyMultibaseList := []string{}
blockchainAccountIdList := []string{}
var pubKeyMultibaseBlockchainAccIdMap map[string]bool = map[string]bool{}
for _, vm := range vms {
vmIdList = append(vmIdList, vm.Id)
if vm.Type == EcdsaSecp256k1VerificationKey2019 {
if _, present := pubKeyMultibaseBlockchainAccIdMap[vm.PublicKeyMultibase]; present {
// TODO: Following is a temporary measure, where we will be allowing duplicate publicKeyMultibase values
// for type EcdsaSecp256k1VerificationKey2019, provided if blockchainAccountId field is populated. This is done since
// one secp256k1 key pair from Keplr wallet can have multiple blockchain addresses depending upon the bech32
// prefix. This will eventually be removed once the successful recovery of public key from the `signEthereum()`
// generated signature is figured out.
if vm.BlockchainAccountId == "" {
return fmt.Errorf(
"duplicate publicKeyMultibase of type EcdsaSecp256k1VerificationKey2019 without blockchainAccountId is not allowed: %s ",
vm.PublicKeyMultibase,
)
}
} else {
pubKeyMultibaseBlockchainAccIdMap[vm.PublicKeyMultibase] = true
}
} else {
publicKeyMultibaseList = append(publicKeyMultibaseList, vm.PublicKeyMultibase)
}
blockchainAccountIdList = append(blockchainAccountIdList, vm.BlockchainAccountId)
}
if duplicateId := checkDuplicateItems(vmIdList); duplicateId != "" {
return fmt.Errorf("duplicate verification method Id found: %s ", duplicateId)
}
if duplicateKey := checkDuplicateItems(publicKeyMultibaseList); duplicateKey != "" {
return fmt.Errorf("duplicate publicKeyMultibase found: %s ", duplicateKey)
}
if duplicateKey := checkDuplicateItems(blockchainAccountIdList); duplicateKey != "" {
return fmt.Errorf("duplicate blockchainAccountId found: %s ", duplicateKey)
}
return nil
}
func validateVmRelationships(didDoc *DidDocument) error {
// make verificationMethodType map between VM Id and VM type
vmTypeMap := map[string]string{}
for _, vm := range didDoc.VerificationMethod {
vmTypeMap[vm.Id] = vm.Type
}
vmRelationshipList := map[string][]string{
"authentication": didDoc.Authentication,
"assertionMethod": didDoc.AssertionMethod,
"keyAgreement": didDoc.KeyAgreement,
"capabilityDelegation": didDoc.CapabilityDelegation,
"capabilityInvocation": didDoc.CapabilityInvocation,
}
for field, vmRelationship := range vmRelationshipList {
// didUrl check and presence in verification methods
for _, vmId := range vmRelationship {
err := isDidUrl(vmId)
if err != nil {
return fmt.Errorf("%s: %s", field, err)
}
if _, found := vmTypeMap[vmId]; !found {
return fmt.Errorf(
"%s: verification method id %s not found in verificationMethod list",
field,
vmId,
)
}
// keyAgreement field should harbour only those Verification Methods whose type is either X25519KeyAgreementKey2020
// or X25519KeyAgreementKeyEIP5630
if (vmTypeMap[vmId] == X25519KeyAgreementKey2020) || (vmTypeMap[vmId] == X25519KeyAgreementKeyEIP5630) {
if field != "keyAgreement" {
return fmt.Errorf(
"verification method id %v is of type %v which is not allowed in '%v' attribute",
vmId,
vmTypeMap[vmId],
field,
)
}
} else {
if field == "keyAgreement" {
return fmt.Errorf(
"verification method id %v provided in '%v' attribute must be of type X25519KeyAgreementKey2020 or X25519KeyAgreementKeyEIP5630",
vmId,
field,
)
}
}
}
}
return nil
}
func validateBlockchainAccountId(blockchainAccountId string) error {
blockchainId, err := NewBlockchainId(blockchainAccountId)
if err != nil {
return err
}
var validationErr error
// Check for supported CAIP-10 prefix
validationErr = blockchainId.ValidateSupportedCAIP10Prefix()
if validationErr != nil {
return validationErr
}
// Check for supported CAIP-10 chain-ids
validationErr = blockchainId.ValidateSupportChainId()
if validationErr != nil {
return validationErr
}
// Check for supported CAIP-10 bech32 prefix. Perform this validation
// only when the CAIP-10 prefix is "cosmos"
if blockchainId.CAIP10Prefix == CosmosCAIP10Prefix {
validationErr = blockchainId.ValidateSupportedBech32Prefix()
if validationErr != nil {
return validationErr
}
}
return nil
}
// isBabyJubJubKey2021PresentAlongWithOtherVMTypes checks if both BabyJubJubKey2021 and other VM Types are present at once
func isBabyJubJubKey2021PresentAlongWithOtherVMTypes(verificationMethods []*VerificationMethod) error {
babyJubJubKey2021Count := 0
nonBabyJubJubKey2021Count := 0
for _, vm := range verificationMethods {
if vm.Type == BabyJubJubKey2021 {
babyJubJubKey2021Count += 1
} else {
nonBabyJubJubKey2021Count += 1
}
}
if babyJubJubKey2021Count > 0 && nonBabyJubJubKey2021Count > 0 {
return fmt.Errorf("BabyJubJubKey2021 should not be paired with other VM types in a single DID Document")
}
return nil
}
// ValidateDidDocument validates the DID Document
func (didDoc *DidDocument) ValidateDidDocument() error {
// Id check
err := isValidDidDocId(didDoc.Id)
if err != nil {
return err
}
// Controller check
for _, controller := range didDoc.Controller {
err := isValidDidDocId(controller)
if err != nil {
return err
}
}
// VerificationMethod check
err = validateVerificationMethods(didDoc.VerificationMethod)
if err != nil {
return err
}
// Services check
err = validateServices(didDoc.Service)
if err != nil {
return err
}
// Verification Method Relationships check
err = validateVmRelationships(didDoc)
if err != nil {
return err
}
// TODO: This is a temporary measure due to technical challenges in merklizing DID Document
if err := isBabyJubJubKey2021PresentAlongWithOtherVMTypes(didDoc.VerificationMethod); err != nil {
return err
}
return nil
}