Skip to content

Commit 4999853

Browse files
committed
Add optimization for controller docs that are DID docs.
1 parent 68b274c commit 4999853

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# jsonld-signatures ChangeLog
22

3+
## 9.3.0 - 2021-07-xx
4+
5+
### Added
6+
- Add optimization for JSON-LD controller documents that are DID documents. When
7+
a controller ID resolves to a JSON-LD DID Document, then JSON-LD framing can
8+
be skipped when verifying verification method relationships if the verification
9+
relationship is one that is defined by the DID context.
10+
311
## 9.2.1 - 2021-07-08
412

513
### Fixed

lib/purposes/ControllerProofPurpose.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ const constants = require('../constants');
77
const jsonld = require('jsonld');
88
const ProofPurpose = require('./ProofPurpose');
99

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+
1022
module.exports = class ControllerProofPurpose extends ProofPurpose {
1123
/**
1224
* Creates a proof purpose that will validate whether or not the verification
@@ -30,6 +42,7 @@ module.exports = class ControllerProofPurpose extends ProofPurpose {
3042
}
3143
this.controller = controller;
3244
}
45+
this._termDefinedByDIDContext = DID_VR_TERMS.includes(term);
3346
}
3447

3548
/**
@@ -57,6 +70,7 @@ module.exports = class ControllerProofPurpose extends ProofPurpose {
5770
}
5871

5972
const {id: verificationId} = verificationMethod;
73+
const {term, _termDefinedByDIDContext} = this;
6074

6175
// if no `controller` specified, use verification method's
6276
if(this.controller) {
@@ -75,23 +89,31 @@ module.exports = class ControllerProofPurpose extends ProofPurpose {
7589
}
7690
}
7791

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;
91114
}
92115

93-
const verificationMethods = jsonld.getValues(
94-
result.controller, this.term);
116+
const verificationMethods = jsonld.getValues(result.controller, term);
95117
result.valid = verificationMethods.some(vm =>
96118
vm === verificationId ||
97119
(typeof vm === 'object' && vm.id === verificationId));

0 commit comments

Comments
 (0)