Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 69x 69x 51x 51x 51x 69x 69x 451x 42x 27x 81x 67x 16x 51x 51x 51x 102x 51x 175x 153x 153x 51x 51x 51x 51x 71x 71x 69x 34x 34x 28x 43x 43x 43x 71x 142x 116x 48x 11x 11x 11x 11x 11x 60x 71x 22x 22x 38x 19x 16x 19x 16x | import { LDMerkleProof2019 } from 'jsonld-signatures-merkleproof2019';
import * as inspectors from '../inspectors';
import domain from '../domain';
import { Suite } from '../models/Suite';
import { isDidUri } from '../domain/verifier/useCases/getIssuerProfile';
import { getVCProofVerificationMethod } from '../models/BlockcertsV3';
import { removeEntry } from '../helpers/array';
import type { ExplorerAPI, TransactionData, IBlockchainObject } from '@blockcerts/explorer-lookup';
import type { Receipt } from '../models/Receipt';
import type { Issuer, IssuerPublicKeyList } from '../models/Issuer';
import type { BlockcertsV3, VCProof } from '../models/BlockcertsV3';
import type VerificationSubstep from '../domain/verifier/valueObjects/VerificationSubstep';
import type { SuiteAPI } from '../models/Suite';
import type { ITransactionLink } from '../domain/certificates/useCases/getTransactionLink';
import type { IDidDocument } from '../models/DidDocument';
import type IVerificationMethod from '../models/VerificationMethod';
enum SUB_STEPS {
getTransactionId = 'getTransactionId',
computeLocalHash = 'computeLocalHash',
fetchRemoteHash = 'fetchRemoteHash',
parseIssuerKeys = 'parseIssuerKeys',
compareHashes = 'compareHashes',
checkImagesIntegrity = 'checkImagesIntegrity',
checkMerkleRoot = 'checkMerkleRoot',
checkReceipt = 'checkReceipt',
retrieveVerificationMethodPublicKey = 'retrieveVerificationMethodPublicKey',
ensureVerificationMethodValidity = 'ensureVerificationMethodValidity',
deriveIssuingAddressFromPublicKey = 'deriveIssuingAddressFromPublicKey',
compareIssuingAddress = 'compareIssuingAddress',
checkAuthenticity = 'checkAuthenticity'
}
export function parseReceipt (proof: VCProof): Receipt {
return LDMerkleProof2019.decodeMerkleProof2019(proof);
}
export default class MerkleProof2019 extends Suite {
public proofVerificationProcess = [
SUB_STEPS.parseIssuerKeys,
SUB_STEPS.checkAuthenticity
];
public transactionId: string;
public localHash: string;
public documentToVerify: BlockcertsV3;
public txData: TransactionData;
public chain: IBlockchainObject;
public explorerAPIs: ExplorerAPI[];
public receipt: Receipt;
public issuerPublicKeyList: IssuerPublicKeyList;
public issuer: Issuer;
public verificationMethod: IVerificationMethod;
public derivedIssuingAddress: string;
public hasDid: boolean;
public proof: VCProof;
public type = 'MerkleProof2019';
public cryptosuite = 'merkle-proof-2019';
public suite: LDMerkleProof2019;
public proofPurpose?: string;
public proofDomain?: string | string[];
public proofChallenge?: string;
constructor (props: SuiteAPI) {
super(props);
this.executeStep = props.executeStep;
this.documentToVerify = props.document as BlockcertsV3;
this.explorerAPIs = props.explorerAPIs;
this.proof = props.proof as VCProof;
this.issuer = props.issuer;
this.proofPurpose = props.proofPurpose;
this.proofDomain = props.proofDomain;
this.proofChallenge = props.proofChallenge;
this.validateProofType();
this.receipt = parseReceipt(this.proof);
this.transactionId = domain.certificates.getTransactionId(this.receipt);
this.setHasDid();
}
async init (): Promise<void> {
await this.setVerificationSuite();
this.chain = this.suite.getChain();
this.adaptVerificationProcessToChain();
}
async verifyProof (): Promise<void> {
await this.suite.verifyProof({
verifyIdentity: false
});
await this.verifyProcess(this.proofVerificationProcess);
}
async verifyIdentity (): Promise<void> {
await this.suite.verifyIdentity();
}
getProofVerificationSteps (parentStepKey: string): VerificationSubstep[] {
const proofVerificationProcess = [
...this.suite.getProofVerificationProcess(),
...this.proofVerificationProcess
];
return proofVerificationProcess.map(childStepKey =>
domain.verifier.convertToVerificationSubsteps(parentStepKey, childStepKey)
);
}
getIdentityVerificationSteps (parentStepKey: string): VerificationSubstep[] {
if (!this.hasDid) {
return [];
}
return this.suite.getIdentityVerificationProcess().map(childStepKey =>
domain.verifier.convertToVerificationSubsteps(parentStepKey, childStepKey)
);
}
getIssuerPublicKey (): string {
return this.suite.getIssuerPublicKey();
}
getIssuanceTime (): string {
return this.suite.getIssuanceTime();
}
getIssuerName (): string {
return this.issuer.name;
}
getIssuerProfileDomain (): string {
const issuerProfileUrl = new URL(this.getIssuerProfileUrl());
return issuerProfileUrl?.hostname;
}
getIssuerProfileUrl (): string {
return this.issuer.id;
}
getSigningDate (): string {
return this.proof.created;
}
getChain (): IBlockchainObject {
return this.suite.getChain();
}
getReceipt (): Receipt {
return this.receipt;
}
// TODO: rename inspector method to make this function `getTransactionId`
getTransactionIdString (): string {
return domain.certificates.getTransactionId(this.getReceipt());
}
getTransactionLink (): string {
const transactionLinks: ITransactionLink = domain.certificates.getTransactionLink(this.getTransactionIdString(), this.getChain());
return transactionLinks.transactionLink;
}
getRawTransactionLink (): string {
const transactionLinks: ITransactionLink = domain.certificates.getTransactionLink(this.getTransactionIdString(), this.getChain());
return transactionLinks.rawTransactionLink;
}
async executeStep (step: string, action, verificationSuite: string): Promise<any> {
throw new Error('executeStep method needs to be overwritten by injecting from Verifier');
}
private async setVerificationSuite (): Promise<void> {
await this.setIssuerFromProofVerificationMethod();
this.verificationMethod = inspectors
.retrieveVerificationMethodPublicKey(
this.getTargetVerificationMethodContainer(),
getVCProofVerificationMethod(this.proof)
);
this.suite = new LDMerkleProof2019({
document: this.documentToVerify,
proof: this.proof,
verificationMethod: this.verificationMethod,
proofPurpose: this.proofPurpose,
domain: this.proofDomain,
challenge: this.proofChallenge,
options: {
explorerAPIs: this.explorerAPIs,
executeStepMethod: this.executeStep
}
});
}
private adaptVerificationProcessToChain (): void {
if (domain.chains.isMockChain(this.chain)) { // TODO: maybe refactor? Is it opportunistic to read this from suite?
removeEntry(this.proofVerificationProcess, SUB_STEPS.parseIssuerKeys);
removeEntry(this.proofVerificationProcess, SUB_STEPS.checkAuthenticity);
}
}
private getTargetVerificationMethodContainer (): Issuer | IDidDocument {
if (this.issuer.didDocument) {
return this.issuer.didDocument;
}
// let's assume the verification method is in the issuer profile and further checks will fail
// if that's not the case
const controller = {
...this.issuer
};
delete controller.didDocument; // not defined in JSONLD for verification
return controller;
}
private findVerificationMethod (verificationMethods: IVerificationMethod[], controller: string): IVerificationMethod {
return verificationMethods?.find(
verificationMethod => {
return verificationMethod.id === this.proof.verificationMethod ||
controller + verificationMethod.id === this.proof.verificationMethod;
}) ?? null;
}
private isProofChain (): boolean {
return this.proof.type === 'ChainedProof2021';
}
private isMultipleProof (): boolean {
return Array.isArray(this.documentToVerify.proof);
}
private getProofIndex (): number {
if (!this.isMultipleProof()) {
return -1;
}
return (this.documentToVerify.proof as VCProof[]).findIndex(proof => proof.proofValue === this.proof.proofValue);
}
private async setIssuerFromProofVerificationMethod (): Promise<void> {
if (this.getProofIndex() > 0) {
const issuerProfileUrl = this.proof.verificationMethod.split('#')[0];
this.issuer = await domain.verifier.getIssuerProfile(issuerProfileUrl);
}
}
private setHasDid (): void {
if (this.getProofIndex() > 0) {
const issuerProfileUrl = this.proof.verificationMethod.split('#')[0];
this.hasDid = isDidUri(issuerProfileUrl);
return;
}
this.hasDid = !!this.issuer.didDocument;
}
private validateProofType (): void {
const proofType = this.isProofChain() ? this.proof.chainedProofType : this.proof.type;
if (proofType === 'DataIntegrityProof') {
const proofCryptoSuite = this.proof.cryptosuite;
if (!proofCryptoSuite) {
throw new Error(`Malformed proof passed. With DataIntegrityProof a cryptosuite must be defined. Expected: ${this.cryptosuite}`);
}
if (proofCryptoSuite !== this.cryptosuite) {
throw new Error(`Incompatible proof cryptosuite passed. Expected: ${this.cryptosuite}, Got: ${proofCryptoSuite}`);
}
return;
}
if (proofType !== this.type) {
throw new Error(`Incompatible proof type passed. Expected: ${this.type}, Got: ${proofType}`);
}
}
private async verifyProcess (process: SUB_STEPS[]): Promise<void> {
for (const verificationStep of process) {
if (!this[verificationStep]) {
console.error('verification logic for', verificationStep, 'not implemented');
return;
}
await this[verificationStep]();
}
}
private async parseIssuerKeys (): Promise<void> {
this.issuerPublicKeyList = await this.executeStep(
SUB_STEPS.parseIssuerKeys,
() => domain.verifier.parseIssuerKeys(this.issuer),
this.type
);
}
private async checkAuthenticity (): Promise<void> {
await this.executeStep(
SUB_STEPS.checkAuthenticity,
() => { inspectors.ensureValidIssuingKey(this.issuerPublicKeyList, this.getIssuerPublicKey(), this.getIssuanceTime()); },
this.type
);
}
}
|