jsonld-signatures-merkleproof2019
Version:
A jsonld signature implementation to support MerkleProof2019 verification in Verifiable Credential context
49 lines (48 loc) • 2.51 kB
JavaScript
import VerifierError from '../models/VerifierError';
import getText from '../helpers/getText';
function assertProofPurposeValidity({ expectedProofPurpose, proof, issuer }) {
var _a;
if (proof.proofPurpose !== expectedProofPurpose) {
throw new VerifierError('assertProofValidity', getText('errors', 'assertProofValidityPurposeVerifier')
.replace('${expectedProofPurpose}', expectedProofPurpose)
.replace('${proof.proofPurpose}', proof.proofPurpose));
}
if (issuer && !((_a = issuer[expectedProofPurpose]) === null || _a === void 0 ? void 0 : _a.includes(proof.verificationMethod))) {
throw new VerifierError('assertProofValidity', getText('errors', 'assertProofValidityPurposeIssuerKey')
.replace('${proof.verificationMethod}', proof.verificationMethod)
.replace('${expectedProofPurpose}', expectedProofPurpose));
}
if (expectedProofPurpose === 'authentication' && !proof.domain) {
// TODO: return actual warning
console.warn('No domain found in proof, but it is recommended for authentication purposes');
}
}
function assertProofDomain({ expectedDomain, proof, expectedChallenge }) {
if (!expectedDomain.includes(proof.domain)) {
throw new VerifierError('assertProofValidity', getText('errors', 'assertProofValidityDomainVerifier')
.replace('${expectedDomain}', expectedDomain.join(', '))
.replace('${proof.domain}', proof.domain));
}
if (proof.domain && !proof.challenge) {
// TODO: return actual warning
console.warn('No challenge found in proof, but it is recommended for domain verification');
}
if (proof.challenge && proof.challenge !== expectedChallenge) {
throw new VerifierError('assertProofValidity', getText('errors', 'assertProofValidityInvalidChallenge'));
}
}
export default function assertProofValidity({ expectedProofPurpose, expectedDomain, expectedChallenge, proof, issuer }) {
if (!proof.proofPurpose) {
throw new VerifierError('assertProofValidity', getText('errors', 'assertProofValidityNoProofPurpose'));
}
if (!proof.created) {
throw new VerifierError('assertProofValidity', getText('errors', 'assertProofValidityNoCreated'));
}
if (proof.proofPurpose) {
assertProofPurposeValidity({ expectedProofPurpose, proof, issuer });
}
if (proof.domain) {
assertProofDomain({ expectedDomain, proof, expectedChallenge });
}
return true;
}