/* * 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 type { BbsCreateProofRequest, BbsSignRequest, BlsBbsSignRequest, BbsVerifyProofRequest, BlsBbsVerifyRequest, BbsVerifyRequest, BbsVerifyResult, } from "./types"; import { UInt8ArrayToArray } from "./utilities"; import { getNativeRnBbsSignatures } from "./getNativeRnBbsSignatures"; const RnBbsSignatures = getNativeRnBbsSignatures(); /** * Default BBS Signature Length */ export const BBS_SIGNATURE_LENGTH = 112; /** * Signs a set of messages with a BBS key pair and produces a BBS signature * @param request Request for the sign operation * * @returns The raw signature value */ export const sign = async (request: BbsSignRequest): Promise => { const { keyPair, messages } = request; try { return new Uint8Array( await RnBbsSignatures.sign({ publicKey: UInt8ArrayToArray(keyPair.publicKey), secretKey: UInt8ArrayToArray(keyPair.secretKey), messageCount: keyPair.messageCount, messages: messages.map((_) => UInt8ArrayToArray(_)), }) ); } catch { throw new Error("Failed to sign"); } }; /** * Signs a set of messages with a BLS 12-381 key pair and produces a BBS signature * @param request Request for the sign operation * * @returns The raw signature value */ export const blsSign = async (request: BlsBbsSignRequest): Promise => { const { keyPair, messages } = request; try { return new Uint8Array( await RnBbsSignatures.blsSign({ publicKey: UInt8ArrayToArray(keyPair.publicKey), secretKey: UInt8ArrayToArray(keyPair.secretKey), messages: messages.map((_) => UInt8ArrayToArray(_)), }) ); } catch { throw new Error("Failed to sign"); } }; /** * Verifies a BBS+ signature for a set of messages with a BBS public key * @param request Request for the signature verification operation * * @returns A result indicating if the signature was verified */ export const verify = async (request: BbsVerifyRequest): Promise => { const { publicKey, signature, messages } = request; try { const result = await RnBbsSignatures.verify({ publicKey: UInt8ArrayToArray(publicKey), signature: UInt8ArrayToArray(signature), messageCount: messages.length, messages: messages.map((_) => UInt8ArrayToArray(_)), }); return { verified: result }; } catch (ex) { return { verified: false, error: ex }; } }; /** * Verifies a BBS+ signature for a set of messages with a with a BLS 12-381 public key * @param request Request for the signature verification operation * * @returns A result indicating if the signature was verified */ export const blsVerify = async (request: BlsBbsVerifyRequest): Promise => { const { publicKey, signature, messages } = request; try { const result = await RnBbsSignatures.blsVerify({ publicKey: UInt8ArrayToArray(publicKey), signature: UInt8ArrayToArray(signature), messages: messages.map((_) => UInt8ArrayToArray(_)), }); return { verified: result }; } catch (ex) { return { verified: false, error: ex }; } }; /** * Creates a BBS+ proof for a set of messages from a BBS public key and a BBS signature * @param request Request for the create proof operation * * @returns The raw proof value */ export const createProof = async (request: BbsCreateProofRequest): Promise => { const { publicKey, signature, messages, nonce, revealed } = request; try { return new Uint8Array( await RnBbsSignatures.createProof({ nonce: UInt8ArrayToArray(nonce), revealed, publicKey: UInt8ArrayToArray(publicKey), signature: UInt8ArrayToArray(signature), messages: messages.map((_) => UInt8ArrayToArray(_)), }) ); } catch (ex) { throw new Error("Failed to create proof"); } }; /** * Creates a BBS+ proof for a set of messages from a BLS12-381 public key and a BBS signature * @param request Request for the create proof operation * * @returns The raw proof value */ export const blsCreateProof = async (request: BbsCreateProofRequest): Promise => { const { publicKey, signature, messages, nonce, revealed } = request; try { return new Uint8Array( await RnBbsSignatures.blsCreateProof({ nonce: UInt8ArrayToArray(nonce), revealed, publicKey: UInt8ArrayToArray(publicKey), signature: UInt8ArrayToArray(signature), messages: messages.map((_) => UInt8ArrayToArray(_)), }) ); } catch (ex) { throw new Error("Failed to create proof"); } }; /** * Verifies a BBS+ proof with a BBS public key * @param request Request for the verify proof operation * * @returns A result indicating if the proof was verified */ export const verifyProof = async (request: BbsVerifyProofRequest): Promise => { const { publicKey, proof, messages, nonce } = request; try { const result = await RnBbsSignatures.verifyProof({ nonce: UInt8ArrayToArray(nonce), publicKey: UInt8ArrayToArray(publicKey), proof: UInt8ArrayToArray(proof), messages: messages.map((_) => UInt8ArrayToArray(_)), }); return { verified: result }; } catch (ex) { return { verified: false, error: ex }; } }; /** * Verifies a BBS+ proof with a BLS12-381 public key * @param request Request for the verify proof operation * * @returns A result indicating if the proof was verified */ export const blsVerifyProof = async (request: BbsVerifyProofRequest): Promise => { const { publicKey, proof, messages, nonce } = request; try { const result = await RnBbsSignatures.blsVerifyProof({ nonce: UInt8ArrayToArray(nonce), publicKey: UInt8ArrayToArray(publicKey), proof: UInt8ArrayToArray(proof), messages: messages.map((_) => UInt8ArrayToArray(_)), }); return { verified: result }; } catch (ex) { return { verified: false, error: ex }; } };