Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 5b7acd4

Browse files
authored
feat: add simple runnable sample (#94)
1 parent 0a9efeb commit 5b7acd4

File tree

7 files changed

+2031
-1009
lines changed

7 files changed

+2031
-1009
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ yarn add @mattrglobal/node-bbs-signatures
4040

4141
## Usage
4242

43+
See the [sample](./sample) directory for a runnable demo.
44+
4345
The following is a short sample on how to use the API
4446

4547
```typescript

sample/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# node-bbs-signatures-sample
2+
3+
A simple runnable sample demoing how to use the API.
4+
5+
Run the following and observe the console output
6+
7+
```
8+
yarn install --frozen-lockfile
9+
yarn demo
10+
```

sample/package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "node-bbs-signatures-sample",
3+
"version": "1.0.0",
4+
"description": "A sample for using node-bbs-signatures",
5+
"main": "lib/index.js",
6+
"repository": "https://github.com/mattrglobal/node-bbs-signatures",
7+
"author": "Mattr Limited",
8+
"license": "Apache 2.0",
9+
"private": false,
10+
"scripts": {
11+
"demo": "ts-node ./src/index.ts"
12+
},
13+
"devDependencies": {
14+
"@types/node": "12.7.2",
15+
"ts-node": "8.4.1",
16+
"typescript": "3.7.5"
17+
},
18+
"dependencies": {
19+
"@mattrglobal/node-bbs-signatures": "0.5.0"
20+
}
21+
}

sample/src/index.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2020 - MATTR Limited
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
import {
15+
generateBls12381KeyPair,
16+
blsSign,
17+
blsVerify,
18+
blsCreateProof,
19+
blsVerifyProof,
20+
} from "@mattrglobal/node-bbs-signatures";
21+
22+
const main = (): void => {
23+
//Generate a new key pair
24+
const keyPair = generateBls12381KeyPair();
25+
26+
console.log("Key pair generated");
27+
console.log(`Public key base64 = ${Buffer.from(keyPair.publicKey).toString("base64")}`);
28+
29+
//Set of messages we wish to sign
30+
const messages = ["message1", "message2"];
31+
32+
console.log("Signing a message set of " + messages);
33+
34+
//Create the signature
35+
const signature = blsSign({
36+
keyPair,
37+
messages: messages,
38+
});
39+
40+
console.log(`Output signature base64 = ${Buffer.from(signature).toString("base64")}`);
41+
42+
//Verify the signature
43+
const isVerified = blsVerify({
44+
publicKey: keyPair.publicKey,
45+
messages: messages,
46+
signature,
47+
});
48+
49+
const isVerifiedString = JSON.stringify(isVerified);
50+
console.log(`Signature verified ? ${isVerifiedString}`);
51+
52+
//Derive a proof from the signature revealing the first message
53+
const proof = blsCreateProof({
54+
signature,
55+
publicKey: keyPair.publicKey,
56+
messages,
57+
nonce: "nonce",
58+
revealed: [0],
59+
});
60+
61+
console.log(`Output proof base64 = ${Buffer.from(proof).toString("base64")}`);
62+
63+
//Verify the created proof
64+
const isProofVerified = blsVerifyProof({
65+
proof,
66+
publicKey: keyPair.publicKey,
67+
messageCount: messages.length,
68+
messages: messages.slice(0, 1),
69+
nonce: "nonce",
70+
revealed: [0],
71+
});
72+
73+
const isProofVerifiedString = JSON.stringify(isProofVerified);
74+
console.log(`Proof verified ? ${isProofVerifiedString}`);
75+
};
76+
77+
main();

sample/tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"sourceMap": true,
6+
"allowJs": false,
7+
"moduleResolution": "node",
8+
"strict": true,
9+
"declaration": true,
10+
"downlevelIteration": true,
11+
"baseUrl": ".",
12+
"esModuleInterop": true,
13+
"resolveJsonModule": true,
14+
"outDir": "./lib",
15+
"types": ["jest", "node"]
16+
},
17+
"include": ["./src"]
18+
}

0 commit comments

Comments
 (0)