@@ -7,6 +7,18 @@ const constants = require('../constants');
7
7
const jsonld = require ( 'jsonld' ) ;
8
8
const ProofPurpose = require ( './ProofPurpose' ) ;
9
9
10
+ // DID documents can be specially optimized
11
+ const DID_CONTEXT_V1 = 'https://www.w3.org/ns/did/v1' ;
12
+ // verification relationship terms that are known to appear in DID documents
13
+ const DID_VR_TERMS = [
14
+ 'assertionMethod' ,
15
+ 'authentication' ,
16
+ 'capabilityInvocation' ,
17
+ 'capabilityDelegation' ,
18
+ 'keyAgreement' ,
19
+ 'verificationMethod'
20
+ ] ;
21
+
10
22
module . exports = class ControllerProofPurpose extends ProofPurpose {
11
23
/**
12
24
* Creates a proof purpose that will validate whether or not the verification
@@ -30,6 +42,7 @@ module.exports = class ControllerProofPurpose extends ProofPurpose {
30
42
}
31
43
this . controller = controller ;
32
44
}
45
+ this . _termDefinedByDIDContext = DID_VR_TERMS . includes ( term ) ;
33
46
}
34
47
35
48
/**
@@ -57,6 +70,7 @@ module.exports = class ControllerProofPurpose extends ProofPurpose {
57
70
}
58
71
59
72
const { id : verificationId } = verificationMethod ;
73
+ const { term, _termDefinedByDIDContext} = this ;
60
74
61
75
// if no `controller` specified, use verification method's
62
76
if ( this . controller ) {
@@ -75,23 +89,31 @@ module.exports = class ControllerProofPurpose extends ProofPurpose {
75
89
}
76
90
}
77
91
78
- // Note: `expansionMap` is intentionally not passed; we can safely drop
79
- // properties here and must allow for it
80
- const framed = await jsonld . frame ( controllerId , {
81
- '@context' : constants . SECURITY_CONTEXT_URL ,
82
- id : controllerId ,
83
- // the term should be in the json-ld object the controllerId resolves
84
- // to.
85
- [ this . term ] : {
86
- '@embed' : '@never' ,
87
- id : verificationId
88
- }
89
- } , { documentLoader, compactToRelative : false } ) ;
90
- result . controller = framed ;
92
+ // apply optimization to controller documents that are DID documents;
93
+ // if `term` is one of those defined by the DID context
94
+ let { document} = await documentLoader ( controllerId ) ;
95
+ const mustFrame = ! ( _termDefinedByDIDContext &&
96
+ document [ '@context' ] === DID_CONTEXT_V1 ||
97
+ ( Array . isArray ( document [ '@context' ] ) &&
98
+ document [ '@context' ] [ 0 ] === DID_CONTEXT_V1 ) ) ;
99
+ if ( mustFrame ) {
100
+ // Note: `expansionMap` is intentionally not passed; we can safely
101
+ // drop properties here and must allow for it
102
+ document = await jsonld . frame ( document , {
103
+ '@context' : constants . SECURITY_CONTEXT_URL ,
104
+ id : controllerId ,
105
+ // this term must be in the JSON-LD controller document or
106
+ // verification will fail
107
+ [ term ] : {
108
+ '@embed' : '@never' ,
109
+ id : verificationId
110
+ }
111
+ } , { documentLoader, compactToRelative : false } ) ;
112
+ }
113
+ result . controller = document ;
91
114
}
92
115
93
- const verificationMethods = jsonld . getValues (
94
- result . controller , this . term ) ;
116
+ const verificationMethods = jsonld . getValues ( result . controller , term ) ;
95
117
result . valid = verificationMethods . some ( vm =>
96
118
vm === verificationId ||
97
119
( typeof vm === 'object' && vm . id === verificationId ) ) ;
0 commit comments