/
verifier.ts
313 lines (275 loc) · 9.64 KB
/
verifier.ts
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { STEPS, SUB_STEPS, VERIFICATION_STATUSES } from './constants';
import debug from 'debug';
import Versions, { isV3 } from './constants/certificateVersions';
import domain from './domain';
import * as inspectors from './inspectors';
import { Blockcerts } from './models/Blockcerts';
import { IBlockchainObject } from './constants/blockchains';
import { ExplorerAPI, TransactionData } from '@blockcerts/explorer-lookup';
import { Issuer, IssuerPublicKeyList } from './models/Issuer';
const log = debug('Verifier');
export interface IVerificationStepCallbackAPI {
code: string;
label: string;
status: string; // TODO: use enum
errorMessage?: string;
}
export type IVerificationStepCallbackFn = (update: IVerificationStepCallbackAPI) => any;
export interface IFinalVerificationStatus {
code: STEPS.final;
status: string; // TODO: use enum
message: string;
}
export default class Verifier {
public chain: IBlockchainObject;
public expires: string;
public id: string;
public issuer: Issuer;
public receipt: any; // TODO: define receipt interface
public revocationKey: string;
public version: Versions;
public transactionId: string;
public documentToVerify: Blockcerts; // TODO: confirm this
public explorerAPIs: ExplorerAPI[];
public txData: TransactionData;
private readonly _stepsStatuses: any[]; // TODO: define stepStatus interface
private localHash: string;
private issuerPublicKeyList: IssuerPublicKeyList;
constructor (
{ certificateJson, chain, expires, id, issuer, receipt, revocationKey, transactionId, version, explorerAPIs }: {
certificateJson: Blockcerts;
chain: IBlockchainObject;
expires: string;
id: string;
issuer: Issuer;
receipt: any;
revocationKey: string;
transactionId: string;
version: Versions;
explorerAPIs?: ExplorerAPI[];
}
) {
this.chain = chain;
this.expires = expires;
this.id = id;
this.issuer = issuer;
this.receipt = receipt;
this.revocationKey = revocationKey;
this.version = version;
this.transactionId = transactionId;
this.explorerAPIs = explorerAPIs;
let document = certificateJson.document;
if (!document) {
document = this._retrieveDocumentBeforeIssuance(certificateJson);
}
this.documentToVerify = Object.assign({}, document);
// Final verification result
// Init status as success, we will update the final status at the end
this._stepsStatuses = [];
}
getIssuingAddress (): string {
if (!this.txData) {
console.error('Trying to access issuing address when txData not available yet. Did you run the `verify` method yet?');
}
return this.txData?.issuingAddress;
}
async verify (stepCallback: IVerificationStepCallbackFn = () => {}): Promise<IFinalVerificationStatus> {
this._stepCallback = stepCallback;
if (domain.chains.isMockChain(this.chain)) {
await this._verifyV2Mock();
} else {
await this._verifyMain();
}
// Send final callback update for global verification status
const erroredStep = this._stepsStatuses.find(step => step.status === VERIFICATION_STATUSES.FAILURE);
return erroredStep ? this._failed(erroredStep) : this._succeed();
}
_getRevocationListUrl (distantIssuerProfile: Issuer): any { // TODO: define revocationList type
if (this.issuer?.revocationList) {
return this.issuer.revocationList;
}
return distantIssuerProfile.revocationList;
}
private async _doAction (step: string, action: () => any): Promise<any> {
// If not failing already
if (this._isFailing()) {
return;
}
let label: string;
if (step) {
label = domain.i18n.getText('subSteps', `${step}LabelPending`);
log(label);
this._updateStatusCallback(step, label, VERIFICATION_STATUSES.STARTING);
}
try {
const res: any = await action();
if (step) {
this._updateStatusCallback(step, label, VERIFICATION_STATUSES.SUCCESS);
this._stepsStatuses.push({ step, label, status: VERIFICATION_STATUSES.SUCCESS });
}
return res;
} catch (err) {
if (step) {
this._updateStatusCallback(step, label, VERIFICATION_STATUSES.FAILURE, err.message);
this._stepsStatuses.push({
code: step,
label,
status: VERIFICATION_STATUSES.FAILURE,
errorMessage: err.message
});
}
}
}
private _stepCallback (update: IVerificationStepCallbackAPI): any { // TODO: unsure type is indeed any
// defined by this.verify interface
}
async _verifyMain (): Promise<void> {
await this.getTransactionId();
await this.computeLocalHash();
await this.fetchRemoteHash();
await this.getIssuerProfile();
await this.parseIssuerKeys();
await this.compareHashes();
await this.checkMerkleRoot();
await this.checkReceipt();
await this.checkRevokedStatus();
await this.checkAuthenticity();
await this.checkExpiresDate();
}
async _verifyV2Mock (): Promise<void> {
await this.computeLocalHash();
await this.compareHashes();
await this.checkReceipt();
await this.checkExpiresDate();
}
private async getTransactionId (): Promise<void> {
await this._doAction(
SUB_STEPS.getTransactionId,
() => inspectors.isTransactionIdValid(this.transactionId)
);
}
private async computeLocalHash (): Promise<void> {
this.localHash = await this._doAction(
SUB_STEPS.computeLocalHash,
async () => await inspectors.computeLocalHash(this.documentToVerify, this.version)
);
}
private async fetchRemoteHash (): Promise<void> {
this.txData = await this._doAction(
SUB_STEPS.fetchRemoteHash,
async () => await domain.verifier.lookForTx({
transactionId: this.transactionId,
chain: this.chain.code,
explorerAPIs: this.explorerAPIs
})
);
}
private async getIssuerProfile (): Promise<void> {
if (!isV3(this.version)) { // with v3 this is done at certificate parsing level (parseV3)
this.issuer = await this._doAction(
SUB_STEPS.getIssuerProfile,
async () => await domain.verifier.getIssuerProfile(this.issuer)
);
}
}
private async parseIssuerKeys (): Promise<void> {
this.issuerPublicKeyList = await this._doAction(
SUB_STEPS.parseIssuerKeys,
() => domain.verifier.parseIssuerKeys(this.issuer)
);
}
private async compareHashes (): Promise<void> {
await this._doAction(SUB_STEPS.compareHashes, () => {
inspectors.ensureHashesEqual(this.localHash, this.receipt.targetHash);
});
}
private async checkMerkleRoot (): Promise<void> {
await this._doAction(SUB_STEPS.checkMerkleRoot, () =>
inspectors.ensureMerkleRootEqual(this.receipt.merkleRoot, this.txData.remoteHash)
);
}
private async checkReceipt (): Promise<void> {
await this._doAction(SUB_STEPS.checkReceipt, () =>
inspectors.ensureValidReceipt(this.receipt, this.version)
);
}
private async checkRevokedStatus (): Promise<void> {
let keys;
let revokedAddresses;
if (this.version === Versions.V1_2) {
revokedAddresses = this.txData.revokedAddresses;
keys = [
domain.verifier.parseRevocationKey(this.issuer),
this.revocationKey
];
} else {
// Get revoked assertions
revokedAddresses = await this._doAction(
null,
async () => await domain.verifier.getRevokedAssertions(this._getRevocationListUrl(this.issuer), this.id)
);
keys = this.id;
}
await this._doAction(SUB_STEPS.checkRevokedStatus, () =>
inspectors.ensureNotRevoked(revokedAddresses, keys)
);
}
private async checkAuthenticity (): Promise<void> {
await this._doAction(SUB_STEPS.checkAuthenticity, () =>
inspectors.ensureValidIssuingKey(this.issuerPublicKeyList, this.txData.issuingAddress, this.txData.time)
);
}
private async checkExpiresDate (): Promise<void> {
await this._doAction(SUB_STEPS.checkExpiresDate, () =>
inspectors.ensureNotExpired(this.expires)
);
}
/**
* Returns a failure final step message
*/
_failed (errorStep): IFinalVerificationStatus { // TODO: define errorStep interface
const message: string = errorStep.errorMessage;
log(`failure:${message}`);
return this._setFinalStep({ status: VERIFICATION_STATUSES.FAILURE, message });
}
/**
* whether or not the current verification is failing
*/
_isFailing (): boolean {
return this._stepsStatuses.some(step => step.status === VERIFICATION_STATUSES.FAILURE);
}
_retrieveDocumentBeforeIssuance (certificateJson): any { // TODO: define certificate object
const certificateCopy = Object.assign({}, certificateJson);
if (isV3(this.version)) {
delete certificateCopy.proof;
} else {
delete certificateCopy.signature;
}
return certificateCopy;
}
/**
* Returns a final success message
*/
_succeed (): IFinalVerificationStatus {
const message = domain.chains.isMockChain(this.chain)
? domain.i18n.getText('success', 'mocknet')
: domain.i18n.getText('success', 'blockchain');
log(message);
return this._setFinalStep({ status: VERIFICATION_STATUSES.SUCCESS, message });
}
_setFinalStep ({ status, message }: { status: string; message: string }): IFinalVerificationStatus {
return { code: STEPS.final, status, message };
}
/**
* calls the origin callback to update on a step status
*/
private _updateStatusCallback (code: string, label: string, status: string, errorMessage = ''): void {
if (code != null) {
const update: IVerificationStepCallbackAPI = { code, label, status };
if (errorMessage) {
update.errorMessage = errorMessage;
}
this._stepCallback(update);
}
}
}