|
| 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(); |
0 commit comments