Skip to content

Commit

Permalink
Merge pull request #19 from blockchain-certificates/feat/support-mocknet
Browse files Browse the repository at this point in the history
Feat/support mocknet
  • Loading branch information
lemoustachiste authored Nov 17, 2022
2 parents 969d3e7 + 1c943f6 commit 6cc1f57
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 10 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dependencies": {
"@blockcerts/explorer-lookup": "^1.3.0",
"@trust/keyto": "^1.0.1",
"@vaultie/lds-merkle-proof-2019": "^0.0.9",
"@vaultie/lds-merkle-proof-2019": "^0.0.10",
"bitcoinjs-lib": "^6.0.2",
"bs58": "^4.0.1",
"core-js": "^3.7.0",
Expand Down
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { lookForTx, ExplorerAPI, TransactionData } from '@blockcerts/explorer-lo
import { DecodedProof, VCProof } from './models/Proof';
import getTransactionId from './helpers/getTransactionId';
import getChain from './helpers/getChain';
import { removeEntry } from './utils/array';
import {
isTransactionIdValid,
computeLocalHash,
Expand Down Expand Up @@ -44,6 +45,7 @@ export interface MerkleProof2019VerificationResult {
export interface MerkleProof2019VerifyProofAPI {
documentLoader?: (url: string) => any; // jsonld document loader hook
verifyIdentity?: boolean; // allow splitting verification process for more control
isMocknet?: boolean; // limit amount of checks for mocknet specificity
}

export class LDMerkleProof2019 extends LinkedDataProof {
Expand Down Expand Up @@ -115,13 +117,17 @@ export class LDMerkleProof2019 extends LinkedDataProof {
this.proofValue = base58Decoder.decode();
}

async verifyProof ({ documentLoader, verifyIdentity }: MerkleProof2019VerifyProofAPI = {
async verifyProof ({ documentLoader, verifyIdentity, isMocknet }: MerkleProof2019VerifyProofAPI = {
documentLoader: (url): any => {},
verifyIdentity: true
verifyIdentity: true,
isMocknet: false
}): Promise<MerkleProof2019VerificationResult> {
this.documentLoader = documentLoader;
let verified: boolean;
let error: string = '';
if (isMocknet) {
this.adaptProofVerificationProcessToMocknet();
}
try {
await this.verifyProcess(this.proofVerificationProcess);
if (verifyIdentity) {
Expand Down Expand Up @@ -180,6 +186,12 @@ export class LDMerkleProof2019 extends LinkedDataProof {
this.chain = getChain(this.proofValue);
}

private adaptProofVerificationProcessToMocknet (): void {
removeEntry(this.proofVerificationProcess, 'getTransactionId');
removeEntry(this.proofVerificationProcess, 'fetchRemoteHash');
removeEntry(this.proofVerificationProcess, 'checkMerkleRoot');
}

private setOptions (options: MerkleProof2019Options): void {
this.explorerAPIs = options.explorerAPIs ?? [];
if (options.executeStepMethod && typeof options.executeStepMethod === 'function') {
Expand Down
7 changes: 7 additions & 0 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function removeEntry (map: any[], key: any): void {
const stepIndex = map.findIndex(entry => entry === key);
if (stepIndex > -1) {
// delete by reference
map.splice(stepIndex, 1);
}
}
40 changes: 40 additions & 0 deletions tests/fixtures/mocknet-v3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export default {
'@context': ['https://www.w3.org/2018/credentials/v1', 'https://w3id.org/blockcerts/v3.1'],
id: 'urn:uuid:e3351aa6-61e2-4d13-bd96-e848aa0c75cd',
type: ['VerifiableCredential', 'BlockcertsCredential'],
issuer: 'https://issuer.dev.hylandcredentials.com/93d02af5-8683-452a-a79c-959deae72495/issuer_profile.json',
issuanceDate: '2022-11-03T17:08:34Z',
credentialSubject: {
type: 'email',
name: 'Piotr Nalewajka',
publicKey: '[email protected]',
id: 'urn:uuid:7d3d512e-ba58-4d7c-886e-acb5f4ee20d9',
email: '[email protected]'
},
metadata: '{}',
display: {
contentMediaType: 'text/html',
content: '<div style="background-color:transparent;padding:6px;display:inline-flex;align-items:center;flex-direction:column"><style>*{line-height:initial}</style><div style="background-color:#FFFFFF;margin-top:0px;height:842px;width:595px;overflow:hidden;box-sizing:border-box;box-shadow:0 0 6px rgba(0,0,0,0.08);border:1px solid #eee"><div style="height:100%;position:relative;width:100%;box-sizing:border-box"><section class="image" style="align-items:flex-end;clear:both;display:flex;flex-direction:column;flex-wrap:wrap;justify-content:flex-start;width:100%"><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCABhAGQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD0qkNLTc15xZyuvuU1fcP4VUiuXvdQH2pjMhQHGGA4NdH4jbbqsmeyj+VclNdF8hBhffmppr3m2Z0KblOV0WoGV2DIwI9jXR26jyl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SLI2MbhyF/xrm2yWJJySeTXTCPLE6ILlppIToKfDIY5Ac8d6jOaSqFezujbNvODzDJ/3yaKt6d4tmtbNILiDz2TgPvwcds0Vg51b/D+Jf1qp/L+J6/XC+Pf+Puz/wBxv5iiivOof7xH5nRhv4qOYTpTj0oor1Xuex0J0+4KbJ1X60UVBwv4icf6v8KhPSiiuqhscrOaX/V1HRRUvoc7GmkooqCGFFFFAj//2Q==" style="max-width:100%;height:auto;width:inherit;display:block;box-sizing:border-box;display:block;margin:10px;width:80px"/></section><section class="text" style="align-items:center;clear:both;display:flex;flex-direction:column;flex-wrap:wrap;justify-content:flex-start;width:100%"><div style="box-sizing:border-box;text-transform:none;color:#3A3A3A;font-family:Helvetica, sans-serif;font-size:15px;font-weight:normal;line-height:normal;margin:10px;overflow-wrap:break-word;text-align:left;white-space:normal;width:calc(100% - 20px)"><div>This is a me, Piotr Nalewajka</div></div></section><section class="text" style="align-items:center;clear:both;display:flex;flex-direction:column;flex-wrap:wrap;justify-content:flex-start;width:100%"><div style="box-sizing:border-box;text-transform:none;color:#3A3A3A;font-family:Helvetica, sans-serif;font-size:13px;font-weight:normal;line-height:normal;margin:10px;overflow-wrap:break-word;text-align:left;white-space:normal;width:calc(100% - 20px)">Miskatonic University, Department of Ancient History<br></div></section></div></div></div>'
},
proof: [{
type: 'EcdsaSecp256k1Signature2019',
created: '2022-11-03T17:12:32Z',
verificationMethod: 'https://issuer.dev.hylandcredentials.com/93d02af5-8683-452a-a79c-959deae72495/issuer_profile.json#ckKpEaMGujwETK7Zpp8Rw6w9gTjmSbR5pZomLdxStq6j',
proofPurpose: 'assertionMethod',
jws: 'eyJhbGciOiJFUzI1NksiLCJiNjQiOmZhbHNlLCJjcml0IjpbImI2NCJdfQ..d3Eh5cX2WyoW8dYR8eiD7iCnAKdqAntLgA_sdrC3Op9wCdw7toC3USbf5YdEDJnFXivnWX05cSJxE0atzLYItw'
}, {
type: 'ChainedProof2021',
verificationMethod: 'https://issuer.dev.hylandcredentials.com/61e6c3fc-5779-4a6d-b05f-fdd10fdac5f0/issuer_profile.json#mocknet-public-key',
chainedProofType: 'MerkleProof2019',
created: '2022-11-15T13:57:49.216Z',
proofPurpose: 'assertionMethod',
proofValue: 'z52dKXiCeigz15WBp2vdftTWVgPqc33rrZDc7iVhzfhYh4y5irhV8AVPpGyP9BGZGCx7GDQKVBk8Lqj69tZKMHVGAmLtQ8qHkRBkxZsy8w4qBXgirTtNt56QWAJ7yzMmCDmtyWrbjvhZWd1fZn6SAtLbd5xn858tC3hvxLsrbnPhvtpCK1jiSCLfX2pBrqpLLNuKaP6PUVnEN2AkXwqv2gvkp35c',
previousProof: {
type: 'EcdsaSecp256k1Signature2019',
created: '2022-11-03T17:12:32Z',
verificationMethod: 'https://issuer.dev.hylandcredentials.com/93d02af5-8683-452a-a79c-959deae72495/issuer_profile.json#ckKpEaMGujwETK7Zpp8Rw6w9gTjmSbR5pZomLdxStq6j',
proofPurpose: 'assertionMethod',
jws: 'eyJhbGciOiJFUzI1NksiLCJiNjQiOmZhbHNlLCJjcml0IjpbImI2NCJdfQ..d3Eh5cX2WyoW8dYR8eiD7iCnAKdqAntLgA_sdrC3Op9wCdw7toC3USbf5YdEDJnFXivnWX05cSJxE0atzLYItw'
}
}]
};
19 changes: 19 additions & 0 deletions tests/verification/mocknet-v3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fixture from '../fixtures/mocknet-v3';
import { LDMerkleProof2019 } from '../../src';

describe('Given the test chain is Mocknet', function () {
describe('when the certificate is valid', function () {
it('should verify successfully', async function () {
const instance = new LDMerkleProof2019({
document: fixture,
proof: fixture.proof[1] // merkle proof
});
const result = await instance.verifyProof({ isMocknet: true });
expect(result).toEqual({
verified: true,
verificationMethod: null,
error: ''
});
});
});
});

0 comments on commit 6cc1f57

Please sign in to comment.