UNPKG

navio-blsct

Version:

TypeScript bindings for the `libblsct` library used by the [Navio](https://nav.io/) blockchain to construct confidential transactions based on the BLS12-381 curve.

59 lines (58 loc) 2.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Signature = void 0; const blsct_1 = require("./blsct"); const managedObj_1 = require("./managedObj"); /** Represents the signature of a transaction. * * Examples: * ```ts * const { PublicKey, Scalar, Signature } = require('navio-blsct') * const sk = Scalar.random() * const pk = PublicKey.fromScalar(sk) * const sig = Signature.generate(sk, 'navio') * sig.verify(pk, 'navio') * const ser = sig.serialize() * const deser = Signature.deserialize(ser) * ser === deser.serialize() // true * ``` */ class Signature extends managedObj_1.ManagedObj { constructor(obj) { super(obj); } /** Generates a new `Signature` instance. * * @param privKey - The private key used to sign the message. * @param msg - The message to be signed. * @return A new `Signature` instance containing the signature of the message. */ static generate(privKey, msg) { const obj = (0, blsct_1.signMessage)(privKey.value(), msg); return new Signature(obj); } /** Verifies the signature against a public key and message. * * @param pubKey - The public key used to verify the signature. * @param msg - The message that was signed. * @return `true` if the signature is valid for the given public key and message, otherwise `false`. */ verify(pubKey, msg) { return (0, blsct_1.verifyMsgSig)(pubKey.value(), msg, this.value()); } value() { return (0, blsct_1.castToSignature)(this.obj); } serialize() { return (0, blsct_1.serializeSignature)(this.value()); } /** Serializes the signature to a hexadecimal string. * * @param hex - The hexadecimal string to serialize. * @returns A hexadecimal string representation of the signature. */ static deserialize(hex) { return Signature._deserialize(hex, blsct_1.deserializeSignature); } } exports.Signature = Signature;