Skip to content

Commit

Permalink
feat(MerkleProof2019): return verification status
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Fraichot committed Nov 18, 2020
1 parent 0fbe051 commit bd22bc9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
29 changes: 20 additions & 9 deletions src/MerkleProof2019.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface MerkleProof2019API {
document: VCDocument;
}

export interface MerkleProof2019VerificationResult {
verified: boolean;
error?: string;
}

export class MerkleProof2019 extends LinkedDataProof {
/**
* @param type {string} Provided by subclass.
Expand Down Expand Up @@ -91,17 +96,23 @@ export class MerkleProof2019 extends LinkedDataProof {
this.proof = base58Decoder.decode();
}

async verifyProof (): Promise<any> { // TODO: define return type
this.validateTransactionId();
await this.computeLocalHash();
await this.fetchTransactionData();
this.compareHashes();
this.confirmMerkleRoot();
const verificationStatus = {} as any;
const verified = verificationStatus.status === 'success';
async verifyProof (): Promise<MerkleProof2019VerificationResult> { // TODO: define return type
let verified: boolean = false;
let error: string = '';
try {
this.validateTransactionId();
await this.computeLocalHash();
await this.fetchTransactionData();
this.compareHashes();
this.confirmMerkleRoot();
verified = true;
} catch (e) {
verified = false;
error = e.message;
}
return {
verified,
verificationStatus
error
};
}

Expand Down
14 changes: 12 additions & 2 deletions tests/MerkleProof2019.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sinon from 'sinon';
import { MerkleProof2019, MerkleProof2019Options } from '../src/MerkleProof2019';
import { MerkleProof2019, MerkleProof2019Options, MerkleProof2019VerificationResult } from '../src/MerkleProof2019';
import decodedProof, { assertionTransactionId } from './assertions/proof';
import { TransactionData } from '../src/models/TransactionData';
import { BLOCKCHAINS } from '../src/constants/blockchains';
Expand Down Expand Up @@ -86,13 +86,16 @@ describe('MerkleProof2019 test suite', function () {

describe('verifyProof method', function () {
describe('when the process is successful', function () {
let result: MerkleProof2019VerificationResult;

beforeEach(async function () {
sinon.stub(lookForTxFunctions, 'default').resolves(fixtureTransactionData);
await instance.verifyProof();
result = await instance.verifyProof();
});

afterEach(function () {
sinon.restore();
result = null;
});

it('should retrieve the transaction id', function () {
Expand All @@ -106,6 +109,13 @@ describe('MerkleProof2019 test suite', function () {
it('should compute the local document\'s hash', function () {
expect(instance.localDocumentHash).toBe(documentHash);
});

it('should return the result object', function () {
expect(result).toEqual({
verified: true,
error: ''
});
});
});
});

Expand Down
12 changes: 7 additions & 5 deletions tests/verification/failing-altered-document.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sinon from 'sinon';
import alteredBlockcertsV3Fixture from '../fixtures/altered-blockcerts-v3';
import { MerkleProof2019 } from '../../src/MerkleProof2019';
import { MerkleProof2019, MerkleProof2019VerificationResult } from '../../src/MerkleProof2019';
import * as lookForTxFunctions from '../../src/helpers/lookForTx';
import fixtureTransactionData from '../fixtures/transactionData';

Expand All @@ -18,10 +18,12 @@ describe('when the process fails', function () {
});

describe('given the local hash does not match the remote hash', function () {
it('should throw', async function () {
await expect(async () => {
await instance.verifyProof();
}).rejects.toThrow('Remote hash does not match verified document.');
it('should return the error', async function () {
const result: MerkleProof2019VerificationResult = await instance.verifyProof();
expect(result).toEqual({
verified: false,
error: 'Remote hash does not match verified document.'
});
});
});
});

0 comments on commit bd22bc9

Please sign in to comment.