-
Notifications
You must be signed in to change notification settings - Fork 23
/
diddoc_validation.go
371 lines (323 loc) · 9.61 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
package types
import (
"fmt"
"regexp"
"strings"
)
// isValidDidDoc checks if the DID Id is valid
func isValidDidDocId(id string) error {
// check the number of elements in DID Document
idElements := strings.Split(id, ":")
if !(len(idElements) == 3 || len(idElements) == 4) {
return fmt.Errorf(
"number of elements in DID Id %s should be either 3 or 4",
id,
)
}
// check if the first element is valid document identifier
if idElements[0] != DocumentIdentifierDid {
return fmt.Errorf(
"document identifier should be %s",
DocumentIdentifierDid,
)
}
// check if the second element is the correct DID method
if idElements[1] != DidMethod {
return fmt.Errorf(
"DID method should be %s",
DidMethod,
)
}
// check proper method specific id
// TODO: need to define a specification for method-specific-id
methodSpecificId := idElements[len(idElements)-1]
isProperMethodSpecificId, err := regexp.MatchString(
"^[a-zA-Z0-9]{32,}$",
methodSpecificId,
)
if err != nil {
return fmt.Errorf("error in parsing regular expression for method-specific-id: %s", err.Error())
}
if !isProperMethodSpecificId {
return fmt.Errorf(
"method-specific-id should be an alphanumeric string with minimum of 32 characters, received: %s",
methodSpecificId,
)
}
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,
)
}
default:
return fmt.Errorf("unsupported verification method type: %v", 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
}
// checkDuplicateItems return a duplicate Id from the list, if found
func checkDuplicateItems(list []string) string {
presentMap := map[string]bool{}
for idx := range list {
if _, present := presentMap[list[idx]]; !present {
presentMap[list[idx]] = true
} else {
return list[idx]
}
}
return ""
}
// 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 *Did) error {
// make verificationMethods map
vmMap := map[string]bool{}
for _, vm := range didDoc.VerificationMethod {
vmMap[vm.Id] = true
}
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 _, element := range vmRelationship {
err := isDidUrl(element)
if err != nil {
return fmt.Errorf("%s: %s", field, err)
}
if _, found := vmMap[element]; !found {
return fmt.Errorf(
"%s: verification method id %s not found in verificationMethod list",
field,
element,
)
}
}
}
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
}
// ValidateDidDocument validates the DID Document
func (didDoc *Did) 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
}
return nil
}