diff --git a/sample/README.md b/sample/README.md index 4d0e401..75dbb49 100644 --- a/sample/README.md +++ b/sample/README.md @@ -2,9 +2,16 @@ A simple runnable sample demoing how to use the API. -Run the following and observe the console output +Run the following and observe the console output for a JSON-LD document with a single proof ``` yarn install --frozen-lockfile yarn demo ``` + +Run the following and observe the console output for a JSON-LD document with multiple proofs + +``` +yarn install --frozen-lockfile +yarn demo:multi +``` diff --git a/sample/package.json b/sample/package.json index 63a4adf..a222dc2 100644 --- a/sample/package.json +++ b/sample/package.json @@ -8,7 +8,8 @@ "license": "Apache 2.0", "private": false, "scripts": { - "demo": "ts-node --files src/index.ts" + "demo": "ts-node --files src/demo_single.ts", + "demo:multi": "ts-node --files src/demo_multi.ts" }, "devDependencies": { "@types/node": "12.7.2", diff --git a/sample/src/demo_multi.ts b/sample/src/demo_multi.ts new file mode 100644 index 0000000..cc13d3d --- /dev/null +++ b/sample/src/demo_multi.ts @@ -0,0 +1,113 @@ +/* + * Copyright 2020 - MATTR Limited + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + Bls12381G2KeyPair, + BbsBlsSignature2020, + BbsBlsSignatureProof2020, + deriveProof +} from "@mattrglobal/jsonld-signatures-bbs"; +import { extendContextLoader, sign, verify, purposes } from "jsonld-signatures"; +import { documentLoaders } from "jsonld"; + +import inputDocument from "./data/inputDocument.json"; +import keyPairOptions from "./data/keyPair.json"; +import exampleControllerDoc from "./data/controllerDocument.json"; +import bbsContext from "./data/lds-bbsbls2020-v0.0.json"; +import revealDocument from "./data/deriveProofFrame.json"; +import citizenVocab from "./data/citizenVocab.json"; + +/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ +const documents: any = { + "did:example:489398593#test": keyPairOptions, + "did:example:489398593": exampleControllerDoc, + "https://w3c-ccg.github.io/ldp-bbs2020/context/v1": bbsContext, + "https://w3id.org/citizenship/v1": citizenVocab +}; + +/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ +const customDocLoader = (url: string): any => { + const context = documents[url]; + + if (context) { + return { + contextUrl: null, // this is for a context via a link header + document: context, // this is the actual document that was loaded + documentUrl: url // this is the actual context URL after redirects + }; + } + + return documentLoaders.node()(url); +}; + +//Extended document load that uses local contexts +/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ +const documentLoader: any = extendContextLoader(customDocLoader); + +const main = async (): Promise => { + //Import the example key pair + const keyPair = await new Bls12381G2KeyPair(keyPairOptions); + + console.log("Input document"); + console.log(JSON.stringify(inputDocument, null, 2)); + + //Sign the input document + const signedDocument = await sign(inputDocument, { + suite: new BbsBlsSignature2020({ key: keyPair }), + purpose: new purposes.AssertionProofPurpose(), + documentLoader + }); + + console.log("Input document with proof"); + console.log(JSON.stringify(signedDocument, null, 2)); + + const multiSignedDocument = await sign(signedDocument, { + suite: new BbsBlsSignature2020({ key: keyPair }), + purpose: new purposes.AssertionProofPurpose(), + documentLoader + }); + + console.log("Input document with multiple proofs"); + console.log(JSON.stringify(multiSignedDocument, null, 2)); + + //Verify the proof + const verified = await verify(multiSignedDocument, { + suite: new BbsBlsSignature2020(), + purpose: new purposes.AssertionProofPurpose(), + documentLoader + }); + + console.log("Verify the signed proof"); + console.log(JSON.stringify(verified, null, 2)); + + //Derive a proof + const derivedProof = await deriveProof(multiSignedDocument, revealDocument, { + suite: new BbsBlsSignatureProof2020(), + documentLoader + }); + + console.log("Derived Proof Result"); + console.log(JSON.stringify(derivedProof, null, 2)); + + //Verify the derived proof + const derivedProofVerified = await verify(derivedProof, { + suite: new BbsBlsSignatureProof2020(), + purpose: new purposes.AssertionProofPurpose(), + documentLoader + }); + + console.log("Derived Proof Verification result"); + console.log(JSON.stringify(derivedProofVerified, null, 2)); +}; + +main(); diff --git a/sample/src/index.ts b/sample/src/demo_single.ts similarity index 96% rename from sample/src/index.ts rename to sample/src/demo_single.ts index 8e4b38a..a43b904 100644 --- a/sample/src/index.ts +++ b/sample/src/demo_single.ts @@ -72,7 +72,7 @@ const main = async (): Promise => { console.log(JSON.stringify(inputDocument, null, 2)); //Verify the proof - let verified = await verify(inputDocument, { + let verified = await verify(signedDocument, { suite: new BbsBlsSignature2020(), purpose: new purposes.AssertionProofPurpose(), documentLoader @@ -82,7 +82,7 @@ const main = async (): Promise => { console.log(JSON.stringify(verified, null, 2)); //Derive a proof - const derivedProof = await deriveProof(inputDocument, revealDocument, { + const derivedProof = await deriveProof(signedDocument, revealDocument, { suite: new BbsBlsSignatureProof2020(), documentLoader });