Skip to content

Commit

Permalink
fix(DataIntegrity): respect error contract for external consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
lemoustachiste committed Dec 3, 2024
1 parent e2604e2 commit 65f2cb5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,16 @@ export class LDMerkleProof2019 extends LinkedDataProof {
let verified: boolean;
let error: string = '';

if (this.proof.proofPurpose) {
if (this.proof.proofPurpose !== this.proofPurpose) {
throw new Error(`Invalid proof purpose. Expected ${this.proofPurpose} but received ${this.proof.proofPurpose}`);
}

if (this.issuer && !this.issuer[this.proofPurpose]?.includes(this.proof.verificationMethod)) {
throw new Error(`The verification method ${this.proof.verificationMethod} is not allowed for the proof purpose ${this.proofPurpose}`);
}
}

try {
if (this.proof.proofPurpose) {
if (this.proof.proofPurpose !== this.proofPurpose) {
throw new Error(`Invalid proof purpose. Expected ${this.proofPurpose} but received ${this.proof.proofPurpose}`);
}

if (this.issuer && !this.issuer[this.proofPurpose]?.includes(this.proof.verificationMethod)) {
throw new Error(`The verification method ${this.proof.verificationMethod} is not allowed for the proof purpose ${this.proofPurpose}`);
}
}
await this.verifyProcess(this.proofVerificationProcess);
if (verifyIdentity) {
await this.verifyIdentity();
Expand Down
18 changes: 12 additions & 6 deletions tests/verification/data-integrity-proof-support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ describe('given the document is signed following the DataIntegrityProof spec', f
proofPurpose: 'authentication'
});

await expect(async () => {
await instance.verifyProof();
}).rejects.toThrow('Invalid proof purpose. Expected authentication but received assertionMethod');
const result = await instance.verifyProof();
expect(result).toEqual({
verified: false,
verificationMethod: null,
error: 'Invalid proof purpose. Expected authentication but received assertionMethod'
});
});
});

Expand All @@ -45,9 +48,12 @@ describe('given the document is signed following the DataIntegrityProof spec', f
issuer: fixtureIssuerProfile
});

await expect(async () => {
await instance.verifyProof();
}).rejects.toThrow('The verification method https://www.blockcerts.org/samples/3.0/issuer-blockcerts.json#key-1 is not allowed for the proof purpose authentication');
const result = await instance.verifyProof();
expect(result).toEqual({
verified: false,
verificationMethod: null,
error: 'The verification method https://www.blockcerts.org/samples/3.0/issuer-blockcerts.json#key-1 is not allowed for the proof purpose authentication'
});
});
});
});

0 comments on commit 65f2cb5

Please sign in to comment.