Skip to content

Commit

Permalink
Remove expansionMap (deprecated).
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrizagidulin committed Dec 30, 2023
1 parent b124892 commit 0750cd9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 86 deletions.
41 changes: 12 additions & 29 deletions lib/ProofSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const constants = require('./constants');
const jsonld = require('jsonld');
const {extendContextLoader, strictDocumentLoader} = require('./documentLoader');
const {serializeError} = require('serialize-error');
const strictExpansionMap = require('./expansionMap');

module.exports = class ProofSet {
/**
Expand Down Expand Up @@ -35,16 +34,11 @@ module.exports = class ProofSet {
*
* @param [documentLoader] {function} a custom document loader,
* `Promise<RemoteDocument> documentLoader(url)`.
* @param [expansionMap] {function} A custom expansion map that is
* passed to the JSON-LD processor; by default a function that will throw
* an error when unmapped properties are detected in the input, use `false`
* to turn this off and allow unmapped properties to be dropped or use a
* custom function.
*
* @return {Promise<object>} resolves with the signed document, with
* the signature in the top-level `proof` property.
*/
async add(document, {suite, purpose, documentLoader, expansionMap} = {}) {
async add(document, {suite, purpose, documentLoader} = {}) {
if(!suite) {
throw new TypeError('"options.suite" is required.');
}
Expand All @@ -57,9 +51,6 @@ module.exports = class ProofSet {
} else {
documentLoader = strictDocumentLoader;
}
if(expansionMap !== false) {
expansionMap = strictExpansionMap;
}

// preprocess document to prepare to remove existing proofs
// let input;
Expand All @@ -71,7 +62,7 @@ module.exports = class ProofSet {
// create the new proof (suites MUST output a proof using the security-v2
// `@context`)
const proof = await suite.createProof({
document: input, purpose, documentLoader, expansionMap
document: input, purpose, documentLoader
});

jsonld.addValue(document, 'proof', proof);
Expand Down Expand Up @@ -101,19 +92,14 @@ module.exports = class ProofSet {
*
* @param {function} [documentLoader] a custom document loader,
* `Promise<RemoteDocument> documentLoader(url)`.
* @param {function} [expansionMap] - A custom expansion map that is
* passed to the JSON-LD processor; by default a function that will throw
* an error when unmapped properties are detected in the input, use `false`
* to turn this off and allow unmapped properties to be dropped or use a
* custom function.
*
* @return {Promise<{verified: boolean, results: Array, error: *}>} resolves
* with an object with a `verified`boolean property that is `true` if at
* least one proof matching the given purpose and suite verifies and `false`
* otherwise; a `results` property with an array of detailed results;
* if `false` an `error` property will be present.
*/
async verify(document, {suite, purpose, documentLoader, expansionMap} = {}) {
async verify(document, {suite, purpose, documentLoader} = {}) {
if(!suite) {
throw new TypeError('"options.suite" is required.');
}
Expand All @@ -130,23 +116,20 @@ module.exports = class ProofSet {
} else {
documentLoader = strictDocumentLoader;
}
if(expansionMap !== false) {
expansionMap = strictExpansionMap;
}

try {
// shallow copy to allow for removal of proof set prior to canonize
document = {...document};

// get proofs from document
const {proofSet, document: doc} = await _getProofs({
document, documentLoader, expansionMap
document, documentLoader
});
document = doc;

// verify proofs
const results = await _verify({
document, suites, proofSet, purpose, documentLoader, expansionMap
document, suites, proofSet, purpose, documentLoader
});
if(results.length === 0) {
const error = new Error(
Expand Down Expand Up @@ -197,7 +180,7 @@ async function _getProofs({document}) {
}

async function _verify({
document, suites, proofSet, purpose, documentLoader, expansionMap
document, suites, proofSet, purpose, documentLoader
}) {
// map each purpose to at least one proof to verify
const purposes = Array.isArray(purpose) ? purpose : [purpose];
Expand All @@ -206,7 +189,7 @@ async function _verify({
const suiteMatchQueue = new Map();
await Promise.all(purposes.map(purpose => _matchProofSet({
purposeToProofs, proofToSuite, purpose, proofSet, suites,
suiteMatchQueue, document, documentLoader, expansionMap
suiteMatchQueue, document, documentLoader
})));

// every purpose must have at least one matching proof or verify will fail
Expand All @@ -230,7 +213,7 @@ async function _verify({
}
};
const {verified, verificationMethod, error} = await suite.verifyProof({
proof, document, purpose, documentLoader, expansionMap
proof, document, purpose, documentLoader
});
if(!vm) {
vm = verificationMethod;
Expand Down Expand Up @@ -264,7 +247,7 @@ async function _verify({
let purposeResult;
try {
purposeResult = await purpose.validate(proof, {
document, suite, verificationMethod, documentLoader, expansionMap
document, suite, verificationMethod, documentLoader
});
} catch(error) {
purposeResult = {valid: false, error};
Expand Down Expand Up @@ -312,11 +295,11 @@ function _makeSerializable(error) {

async function _matchProofSet({
purposeToProofs, proofToSuite, purpose, proofSet, suites,
suiteMatchQueue, document, documentLoader, expansionMap
suiteMatchQueue, document, documentLoader
}) {
for(const proof of proofSet) {
// first check if the proof matches the purpose; if it doesn't continue
if(!await purpose.match(proof, {document, documentLoader, expansionMap})) {
if(!await purpose.match(proof, {document, documentLoader})) {
continue;
}

Expand All @@ -335,7 +318,7 @@ async function _matchProofSet({
}
let promise = matchingProofs.get(proof);
if(!promise) {
promise = s.matchProof({proof, document, documentLoader, expansionMap});
promise = s.matchProof({proof, document, documentLoader});
matchingProofs.set(proof, promise);
}
if(await promise) {
Expand Down
20 changes: 4 additions & 16 deletions lib/jsonld-signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,14 @@ const VerificationError = require('./VerificationError');
* and other relevant URLs needed for the proof.
*
* Advanced optional parameters and overrides:
*
* @param {function} [options.expansionMap] - A custom expansion map that is
* passed to the JSON-LD processor; by default a function that will throw
* an error when unmapped properties are detected in the input, use `false`
* to turn this off and allow unmapped properties to be dropped or use a
* custom function.
* @param {boolean} [options.addSuiteContext=true] - Toggles the default
* behavior of each signature suite enforcing the presence of its own
* `@context` (if it is not present, it's added to the context list).
*
* @returns {Promise<object>} Resolves with signed document.
*/
api.sign = async function sign(document, {
suite, purpose, documentLoader, expansionMap, addSuiteContext = true
suite, purpose, documentLoader, addSuiteContext = true
} = {}) {
if(typeof document !== 'object') {
throw new TypeError('The "document" parameter must be an object.');
Expand All @@ -61,7 +55,7 @@ api.sign = async function sign(document, {

try {
return await new ProofSet().add(
document, {suite, purpose, documentLoader, expansionMap});
document, {suite, purpose, documentLoader});
} catch(e) {
if(!documentLoader && e.name === 'jsonld.InvalidUrl') {
const {details: {url}} = e;
Expand Down Expand Up @@ -92,11 +86,6 @@ api.sign = async function sign(document, {
*
* @param {function} [documentLoader] - A custom document loader,
* `Promise<RemoteDocument> documentLoader(url)`.
* @param {function} [expansionMap] - A custom expansion map that is
* passed to the JSON-LD processor; by default a function that will throw
* an error when unmapped properties are detected in the input, use `false`
* to turn this off and allow unmapped properties to be dropped or use a
* custom function.
*
* @return {Promise<{verified: boolean, results: Array,
* error: VerificationError}>}
Expand All @@ -107,12 +96,12 @@ api.sign = async function sign(document, {
* containing all of the errors that occurred during the verification process.
*/
api.verify = async function verify(document, {
suite, purpose, documentLoader, expansionMap} = {}) {
suite, purpose, documentLoader} = {}) {
if(typeof document !== 'object') {
throw new TypeError('The "document" parameter must be an object.');
}
const result = await new ProofSet().verify(
document, {suite, purpose, documentLoader, expansionMap});
document, {suite, purpose, documentLoader});
const {error} = result;
if(error) {
if(!documentLoader && error.name === 'jsonld.InvalidUrl') {
Expand All @@ -136,4 +125,3 @@ api.purposes = require('./purposes').purposes;

// expose document loader helpers
Object.assign(api, require('./documentLoader'));

8 changes: 4 additions & 4 deletions lib/purposes/AuthenticationProofPurpose.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = class AuthenticationProofPurpose extends
this.domain = domain;
}

async validate(proof, {verificationMethod, documentLoader, expansionMap}) {
async validate(proof, {verificationMethod, documentLoader}) {
try {
// check challenge
if(proof.challenge !== this.challenge) {
Expand All @@ -36,15 +36,15 @@ module.exports = class AuthenticationProofPurpose extends
}

return super.validate(
proof, {verificationMethod, documentLoader, expansionMap});
proof, {verificationMethod, documentLoader});
} catch(error) {
return {valid: false, error};
}
}

async update(proof, {document, suite, documentLoader, expansionMap}) {
async update(proof, {document, suite, documentLoader}) {
proof = await super.update(
proof, {document, suite, documentLoader, expansionMap});
proof, {document, suite, documentLoader});
proof.challenge = this.challenge;
if(this.domain !== undefined) {
proof.domain = this.domain;
Expand Down
7 changes: 2 additions & 5 deletions lib/purposes/ControllerProofPurpose.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,16 @@ module.exports = class ControllerProofPurpose extends ProofPurpose {
* @param proof
* @param verificationMethod
* @param documentLoader
* @param expansionMap
*
* @throws {Error} If verification method not authorized by controller
* @throws {Error} If proof's created timestamp is out of range
*
* @returns {Promise<{valid: boolean, error: Error}>}
*/
async validate(proof, {verificationMethod, documentLoader, expansionMap}) {
async validate(proof, {verificationMethod, documentLoader}) {
try {
const result = await super.validate(
proof, {verificationMethod, documentLoader, expansionMap});
proof, {verificationMethod, documentLoader});
if(!result.valid) {
throw result.error;
}
Expand Down Expand Up @@ -97,8 +96,6 @@ module.exports = class ControllerProofPurpose extends ProofPurpose {
(Array.isArray(document['@context']) &&
document['@context'][0] === DID_CONTEXT_V1));
if(mustFrame) {
// Note: `expansionMap` is intentionally not passed; we can safely
// drop properties here and must allow for it
document = await jsonld.frame(document, {
'@context': constants.SECURITY_CONTEXT_URL,
id: controllerId,
Expand Down
6 changes: 3 additions & 3 deletions lib/purposes/ProofPurpose.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = class ProofPurpose {
*/
async validate(
proof, {/*document, suite, verificationMethod,
documentLoader, expansionMap*/}) {
documentLoader*/}) {
try {
// check expiration
if(this.maxTimestampDelta !== Infinity) {
Expand Down Expand Up @@ -73,7 +73,7 @@ module.exports = class ProofPurpose {
* @return {Promise<object>} resolves to the proof instance (in the
* `constants.SECURITY_CONTEXT_URL`.
*/
async update(proof, {/*document, suite, documentLoader, expansionMap */}) {
async update(proof, {/*document, suite, documentLoader*/}) {
proof.proofPurpose = this.term;
return proof;
}
Expand All @@ -87,7 +87,7 @@ module.exports = class ProofPurpose {
*
* @return {Promise<boolean>} `true` if there's a match, `false` if not.
*/
async match(proof, {/* document, documentLoader, expansionMap */}) {
async match(proof, {/* document, documentLoader */}) {
return proof.proofPurpose === this.term;
}
};
8 changes: 3 additions & 5 deletions lib/suites/LinkedDataProof.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ module.exports = class LinkedDataProof {
* @param document {object} to be signed.
* @param purpose {ProofPurpose}
* @param documentLoader {function}
* @param expansionMap {function}
*
* @returns {Promise<object>} Resolves with the created proof object.
*/
async createProof({
/* document, purpose, documentLoader, expansionMap */
/* document, purpose, documentLoader */
}) {
throw new Error('"createProof" must be implemented in a derived class.');
}
Expand All @@ -30,12 +29,11 @@ module.exports = class LinkedDataProof {
* @param document {object} the document the proof applies to.
* @param purpose {ProofPurpose}
* @param documentLoader {function}
* @param expansionMap {function}
*
* @returns {Promise<{object}>} Resolves with the verification result.
*/
async verifyProof({
/* proof, document, purpose, documentLoader, expansionMap */
/* proof, document, purpose, documentLoader */
}) {
throw new Error('"verifyProof" must be implemented in a derived class.');
}
Expand All @@ -49,7 +47,7 @@ module.exports = class LinkedDataProof {
* @returns {Promise<boolean>} Whether a match for the proof was found.
*/
async matchProof({
proof /*, document, purpose, documentLoader, expansionMap */
proof /*, document, purpose, documentLoader */
}) {
return proof.type === this.type;
}
Expand Down
Loading

0 comments on commit 0750cd9

Please sign in to comment.