Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
chore: updates samples to also show how to use multiple proofs
Browse files Browse the repository at this point in the history
This additional sample is used to show how the deriveProof API can be used to derive proofs from
multiple signatures.

re #79
  • Loading branch information
kdenhartog-mattr committed Oct 5, 2020
1 parent 97c1f07 commit 5f4efc2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
9 changes: 8 additions & 1 deletion sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
3 changes: 2 additions & 1 deletion sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
113 changes: 113 additions & 0 deletions sample/src/demo_multi.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
//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();
4 changes: 2 additions & 2 deletions sample/src/index.ts → sample/src/demo_single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const main = async (): Promise<void> => {
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
Expand All @@ -82,7 +82,7 @@ const main = async (): Promise<void> => {
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
});
Expand Down

0 comments on commit 5f4efc2

Please sign in to comment.