2020-11-25 10:41:52 +00:00
|
|
|
import {IBls} from "./interface";
|
2020-11-29 14:28:30 +00:00
|
|
|
import {validateBytes} from "./helpers";
|
2020-11-30 00:20:52 +00:00
|
|
|
import {NotInitializedError} from "./errors";
|
2020-11-25 10:41:52 +00:00
|
|
|
|
2020-11-25 11:50:47 +00:00
|
|
|
// Returned type is enforced at each implementation's index
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
2020-11-25 10:41:52 +00:00
|
|
|
export function functionalInterfaceFactory({
|
2020-11-30 18:01:13 +00:00
|
|
|
SecretKey,
|
2020-11-25 10:41:52 +00:00
|
|
|
PublicKey,
|
|
|
|
Signature,
|
2020-11-30 18:01:13 +00:00
|
|
|
}: Pick<IBls, "SecretKey" | "PublicKey" | "Signature">) {
|
2020-11-25 10:41:52 +00:00
|
|
|
/**
|
|
|
|
* Signs given message using secret key.
|
|
|
|
* @param secretKey
|
2020-11-28 19:11:21 +00:00
|
|
|
* @param message
|
2020-11-25 10:41:52 +00:00
|
|
|
*/
|
2020-11-28 19:11:21 +00:00
|
|
|
function sign(secretKey: Uint8Array, message: Uint8Array): Uint8Array {
|
2020-11-29 14:06:09 +00:00
|
|
|
validateBytes(secretKey, "secretKey");
|
|
|
|
validateBytes(message, "message");
|
|
|
|
|
2020-11-30 18:01:13 +00:00
|
|
|
return SecretKey.fromBytes(secretKey).sign(message).toBytes();
|
2020-11-25 10:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compines all given signature into one.
|
|
|
|
* @param signatures
|
|
|
|
*/
|
2020-11-25 16:09:44 +00:00
|
|
|
function aggregateSignatures(signatures: Uint8Array[]): Uint8Array {
|
2020-11-25 10:41:52 +00:00
|
|
|
const agg = Signature.aggregate(signatures.map((p) => Signature.fromBytes(p)));
|
|
|
|
return agg.toBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Combines all given public keys into single one
|
|
|
|
* @param publicKeys
|
|
|
|
*/
|
2020-11-30 18:01:13 +00:00
|
|
|
function aggregatePublicKeys(publicKeys: Uint8Array[]): Uint8Array {
|
2020-11-25 10:41:52 +00:00
|
|
|
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
|
2020-11-28 19:11:21 +00:00
|
|
|
* @param message
|
2020-11-25 10:41:52 +00:00
|
|
|
* @param signature
|
|
|
|
*/
|
2020-11-28 19:11:21 +00:00
|
|
|
function verify(publicKey: Uint8Array, message: Uint8Array, signature: Uint8Array): boolean {
|
2020-11-29 14:06:09 +00:00
|
|
|
validateBytes(publicKey, "publicKey");
|
|
|
|
validateBytes(message, "message");
|
|
|
|
validateBytes(signature, "signature");
|
|
|
|
|
2020-11-25 10:41:52 +00:00
|
|
|
try {
|
2020-11-28 19:11:21 +00:00
|
|
|
return Signature.fromBytes(signature).verify(PublicKey.fromBytes(publicKey), message);
|
2020-11-25 10:41:52 +00:00
|
|
|
} catch (e) {
|
2020-11-30 00:20:52 +00:00
|
|
|
if (e instanceof NotInitializedError) throw e;
|
2020-11-25 10:41:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies if aggregated signature is same message signed with given public keys.
|
|
|
|
* @param publicKeys
|
2020-11-28 19:11:21 +00:00
|
|
|
* @param message
|
2020-11-25 10:41:52 +00:00
|
|
|
* @param signature
|
|
|
|
*/
|
2020-11-28 19:11:21 +00:00
|
|
|
function verifyAggregate(publicKeys: Uint8Array[], message: Uint8Array, signature: Uint8Array): boolean {
|
2020-11-29 14:06:09 +00:00
|
|
|
validateBytes(publicKeys, "publicKey");
|
|
|
|
validateBytes(message, "message");
|
|
|
|
validateBytes(signature, "signature");
|
|
|
|
|
2020-11-25 10:41:52 +00:00
|
|
|
try {
|
|
|
|
return Signature.fromBytes(signature).verifyAggregate(
|
2020-11-30 18:01:13 +00:00
|
|
|
publicKeys.map((publicKey) => PublicKey.fromBytes(publicKey)),
|
2020-11-28 19:11:21 +00:00
|
|
|
message
|
2020-11-25 10:41:52 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2020-11-30 00:20:52 +00:00
|
|
|
if (e instanceof NotInitializedError) throw e;
|
2020-11-25 10:41:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies if signature is list of message signed with corresponding public key.
|
|
|
|
* @param publicKeys
|
2020-11-28 19:11:21 +00:00
|
|
|
* @param messages
|
2020-11-25 10:41:52 +00:00
|
|
|
* @param signature
|
|
|
|
* @param fast Check if all messages are different
|
|
|
|
*/
|
2020-11-28 19:11:21 +00:00
|
|
|
function verifyMultiple(publicKeys: Uint8Array[], messages: Uint8Array[], signature: Uint8Array): boolean {
|
2020-11-29 14:06:09 +00:00
|
|
|
validateBytes(publicKeys, "publicKey");
|
|
|
|
validateBytes(messages, "message");
|
|
|
|
validateBytes(signature, "signature");
|
2020-11-25 10:41:52 +00:00
|
|
|
|
2020-11-28 19:11:21 +00:00
|
|
|
if (publicKeys.length === 0 || publicKeys.length != messages.length) {
|
2020-11-25 10:41:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return Signature.fromBytes(signature).verifyMultiple(
|
|
|
|
publicKeys.map((publicKey) => PublicKey.fromBytes(publicKey)),
|
2020-11-28 19:11:21 +00:00
|
|
|
messages.map((msg) => msg)
|
2020-11-25 10:41:52 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2020-11-30 00:20:52 +00:00
|
|
|
if (e instanceof NotInitializedError) throw e;
|
2020-11-25 10:41:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
sign,
|
|
|
|
aggregateSignatures,
|
2020-11-30 18:01:13 +00:00
|
|
|
aggregatePublicKeys,
|
2020-11-25 10:41:52 +00:00
|
|
|
verify,
|
|
|
|
verifyAggregate,
|
|
|
|
verifyMultiple,
|
|
|
|
};
|
|
|
|
}
|