Remove code duplication in the functional interface

This commit is contained in:
dapplion 2020-11-25 10:41:52 +00:00
parent 2073d31a15
commit 4da10180d9
3 changed files with 116 additions and 210 deletions

View File

@ -1,11 +1,10 @@
import assert from "assert";
import {Keypair} from "./keypair";
import {PrivateKey} from "./privateKey";
import {PublicKey} from "./publicKey";
import {Signature} from "./signature";
import {toBuffer} from "../helpers/utils";
import {IBls} from "../interface";
export * from "../constants";
import {functionalInterfaceFactory} from "../functional";
export {Keypair, PrivateKey, PublicKey, Signature};
@ -16,113 +15,13 @@ export function destroy(): void {
// Native bindings require no destroy() call
}
/**
* Signs given message using secret key.
* @param secretKey
* @param messageHash
*/
export function sign(secretKey: Uint8Array, messageHash: Uint8Array): Buffer {
assert(secretKey, "secretKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
const privateKey = PrivateKey.fromBytes(toBuffer(secretKey));
return privateKey.signMessage(toBuffer(messageHash)).toBytes();
}
/**
* Compines all given signature into one.
* @param signatures
*/
export function aggregateSignatures(signatures: Uint8Array[]): Buffer {
const agg = Signature.aggregate(signatures.map((p) => Signature.fromBytes(p)));
return agg.toBytes();
}
/**
* Combines all given public keys into single one
* @param publicKeys
*/
export function aggregatePubkeys(publicKeys: Uint8Array[]): Buffer {
const agg = PublicKey.aggregate(publicKeys.map((p) => PublicKey.fromBytes(p)));
return agg.toBytes();
}
/**
* Verifies if signature is message signed with given public key.
* @param publicKey
* @param messageHash
* @param signature
*/
export function verify(publicKey: Uint8Array, messageHash: Uint8Array, signature: Uint8Array): boolean {
assert(publicKey, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
try {
return PublicKey.fromBytes(publicKey).verifyMessage(
Signature.fromBytes(toBuffer(signature)),
toBuffer(messageHash)
);
} catch (e) {
return false;
}
}
/**
* Verifies if aggregated signature is same message signed with given public keys.
* @param publicKeys
* @param messageHash
* @param signature
*/
export function verifyAggregate(publicKeys: Uint8Array[], messageHash: Uint8Array, signature: Uint8Array): boolean {
assert(publicKeys, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
try {
return Signature.fromBytes(signature).verifyAggregate(
publicKeys.map((pubkey) => PublicKey.fromBytes(pubkey)),
messageHash
);
} catch (e) {
return false;
}
}
/**
* Verifies if signature is list of message signed with corresponding public key.
* @param publicKeys
* @param messageHashes
* @param signature
* @param fast Check if all messages are different
*/
export function verifyMultiple(publicKeys: Uint8Array[], messageHashes: Uint8Array[], signature: Uint8Array): boolean {
assert(publicKeys, "publicKey is null or undefined");
assert(messageHashes, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
if (publicKeys.length === 0 || publicKeys.length != messageHashes.length) {
return false;
}
try {
return Signature.fromBytes(toBuffer(signature)).verifyMultiple(
publicKeys.map((key) => PublicKey.fromBytes(toBuffer(key))),
messageHashes.map((m) => toBuffer(m))
);
} catch (e) {
return false;
}
}
const bls: IBls = {
sign,
aggregateSignatures,
aggregatePubkeys,
verify,
verifyAggregate,
verifyMultiple,
export const bls: IBls = {
// Keypair,
PrivateKey,
PublicKey,
Signature,
...functionalInterfaceFactory({PrivateKey, PublicKey, Signature}),
initBLS,
destroy,
};

109
src/functional.ts Normal file
View File

@ -0,0 +1,109 @@
import assert from "assert";
import {IBls} from "./interface";
export function functionalInterfaceFactory({
PrivateKey,
PublicKey,
Signature,
}: Pick<IBls, "PrivateKey" | "PublicKey" | "Signature">) {
/**
* Signs given message using secret key.
* @param secretKey
* @param messageHash
*/
function sign(secretKey: Uint8Array, messageHash: Uint8Array): Buffer {
assert(secretKey, "secretKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
const privateKey = PrivateKey.fromBytes(secretKey);
return privateKey.signMessage(messageHash).toBytes();
}
/**
* Compines all given signature into one.
* @param signatures
*/
function aggregateSignatures(signatures: Uint8Array[]): Buffer {
const agg = Signature.aggregate(signatures.map((p) => Signature.fromBytes(p)));
return agg.toBytes();
}
/**
* Combines all given public keys into single one
* @param publicKeys
*/
function aggregatePubkeys(publicKeys: Uint8Array[]): Buffer {
const agg = PublicKey.aggregate(publicKeys.map((p) => PublicKey.fromBytes(p)));
return agg.toBytes();
}
/**
* Verifies if signature is message signed with given public key.
* @param publicKey
* @param messageHash
* @param signature
*/
function verify(publicKey: Uint8Array, messageHash: Uint8Array, signature: Uint8Array): boolean {
assert(publicKey, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
try {
return Signature.fromBytes(signature).verify(PublicKey.fromBytes(publicKey), messageHash);
} catch (e) {
return false;
}
}
/**
* Verifies if aggregated signature is same message signed with given public keys.
* @param publicKeys
* @param messageHash
* @param signature
*/
function verifyAggregate(publicKeys: Uint8Array[], messageHash: Uint8Array, signature: Uint8Array): boolean {
assert(publicKeys, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
try {
return Signature.fromBytes(signature).verifyAggregate(
publicKeys.map((pubkey) => PublicKey.fromBytes(pubkey)),
messageHash
);
} catch (e) {
return false;
}
}
/**
* Verifies if signature is list of message signed with corresponding public key.
* @param publicKeys
* @param messageHashes
* @param signature
* @param fast Check if all messages are different
*/
function verifyMultiple(publicKeys: Uint8Array[], messageHashes: Uint8Array[], signature: Uint8Array): boolean {
assert(publicKeys, "publicKey is null or undefined");
assert(messageHashes, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
if (publicKeys.length === 0 || publicKeys.length != messageHashes.length) {
return false;
}
try {
return Signature.fromBytes(signature).verifyMultiple(
publicKeys.map((publicKey) => PublicKey.fromBytes(publicKey)),
messageHashes.map((msg) => msg)
);
} catch (e) {
return false;
}
}
return {
sign,
aggregateSignatures,
aggregatePubkeys,
verify,
verifyAggregate,
verifyMultiple,
};
}

View File

@ -3,120 +3,18 @@ import {PrivateKey} from "./privateKey";
import {PublicKey} from "./publicKey";
import {Signature} from "./signature";
import {initBLS, destroy} from "./context";
import assert from "assert";
import {toBuffer} from "../helpers/utils";
import {IBls} from "../interface";
import {functionalInterfaceFactory} from "../functional";
export {Keypair, PrivateKey, PublicKey, Signature, initBLS, destroy};
/**
* Signs given message using secret key.
* @param secretKey
* @param messageHash
*/
export function sign(secretKey: Uint8Array, messageHash: Uint8Array): Buffer {
assert(secretKey, "secretKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
const privateKey = PrivateKey.fromBytes(toBuffer(secretKey));
return privateKey.signMessage(toBuffer(messageHash)).toBytes();
}
/**
* Compines all given signature into one.
* @param signatures
*/
export function aggregateSignatures(signatures: Uint8Array[]): Buffer {
assert(signatures, "signatures is null or undefined");
const agg = Signature.aggregate(signatures.map((signature): Signature => Signature.fromBytes(signature)));
return agg.toBytes();
}
/**
* Combines all given public keys into single one
* @param publicKeys
*/
export function aggregatePubkeys(publicKeys: Uint8Array[]): Buffer {
assert(publicKeys, "publicKeys is null or undefined");
const agg = PublicKey.aggregate(publicKeys.map((pk) => PublicKey.fromBytes(pk)));
return agg.toBytes();
}
/**
* Verifies if signature is message signed with given public key.
* @param publicKey
* @param messageHash
* @param signature
*/
export function verify(publicKey: Uint8Array, messageHash: Uint8Array, signature: Uint8Array): boolean {
assert(publicKey, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
try {
return PublicKey.fromBytes(publicKey).verifyMessage(
Signature.fromBytes(toBuffer(signature)),
toBuffer(messageHash)
);
} catch (e) {
return false;
}
}
/**
* Verifies if aggregated signature is same message signed with given public keys.
* @param publicKeys
* @param messageHash
* @param signature
*/
export function verifyAggregate(publicKeys: Uint8Array[], messageHash: Uint8Array, signature: Uint8Array): boolean {
assert(publicKeys, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
try {
return Signature.fromBytes(signature).verifyAggregate(
publicKeys.map((pubkey) => PublicKey.fromBytes(pubkey)),
messageHash
);
} catch (e) {
return false;
}
}
/**
* Verifies if signature is list of message signed with corresponding public key.
* @param publicKeys
* @param messageHashes
* @param signature
*/
export function verifyMultiple(publicKeys: Uint8Array[], messageHashes: Uint8Array[], signature: Uint8Array): boolean {
assert(publicKeys, "publicKey is null or undefined");
assert(messageHashes, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
if (publicKeys.length === 0 || publicKeys.length != messageHashes.length) {
return false;
}
try {
return Signature.fromBytes(toBuffer(signature)).verifyMultiple(
publicKeys.map((key) => PublicKey.fromBytes(toBuffer(key))),
messageHashes.map((m) => toBuffer(m))
);
} catch (e) {
return false;
}
}
const bls: IBls = {
sign,
aggregateSignatures,
aggregatePubkeys,
verify,
verifyAggregate,
verifyMultiple,
// Keypair,
PrivateKey,
PublicKey,
Signature,
...functionalInterfaceFactory({PrivateKey, PublicKey, Signature}),
initBLS,
destroy,
};