This repository has been archived on 2023-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
chainsafe-bls/src/index.ts

151 lines
4.1 KiB
TypeScript
Raw Normal View History

2019-08-05 15:48:26 +00:00
import {Keypair} from "./keypair";
import {PrivateKey} from "./privateKey";
import {PublicKey} from "./publicKey";
import {Signature} from "./signature";
2019-11-27 20:58:41 +00:00
import {PUBLIC_KEY_LENGTH} from "./constants";
2020-01-08 06:49:51 +00:00
import assert from "assert";
2019-11-28 11:13:20 +00:00
2019-08-12 07:51:18 +00:00
export {Keypair, PrivateKey, PublicKey, Signature};
2019-08-05 15:48:26 +00:00
2019-12-02 12:44:45 +00:00
export {init as initBLS} from "./context";
2019-11-27 20:58:41 +00:00
2020-02-12 15:39:40 +00:00
function toBuffer(input: Uint8Array): Buffer {
return Buffer.from(input.buffer, input.byteOffset, input.length);
}
2019-08-05 15:48:26 +00:00
/**
* Generates new secret and public key
*/
2019-08-12 07:51:18 +00:00
export function generateKeyPair(): Keypair {
2019-08-05 15:48:26 +00:00
return Keypair.generate();
}
/**
* Generates public key from given secret.
* @param {BLSSecretKey} secretKey
*/
2020-02-12 15:39:40 +00:00
export function generatePublicKey(secretKey: Uint8Array): Buffer {
2020-01-08 06:49:51 +00:00
assert(secretKey, "secretKey is null or undefined");
2020-02-12 15:39:40 +00:00
const keypair = new Keypair(PrivateKey.fromBytes(toBuffer(secretKey)));
2019-08-05 15:48:26 +00:00
return keypair.publicKey.toBytesCompressed();
}
/**
* Signs given message using secret key.
* @param secretKey
* @param messageHash
* @param domain
*/
2020-02-12 15:39:40 +00:00
export function sign(secretKey: Uint8Array, messageHash: Uint8Array, domain: Uint8Array): Buffer {
2020-01-08 06:49:51 +00:00
assert(secretKey, "secretKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(domain, "domain is null or undefined");
2020-02-12 15:39:40 +00:00
const privateKey = PrivateKey.fromBytes(toBuffer(secretKey));
2020-02-04 22:30:10 +00:00
return privateKey.signMessage(
2020-02-12 15:39:40 +00:00
toBuffer(messageHash),
toBuffer(domain),
2020-02-04 22:30:10 +00:00
).toBytesCompressed();
2019-08-05 15:48:26 +00:00
}
/**
* Compines all given signature into one.
* @param signatures
*/
2020-02-12 15:39:40 +00:00
export function aggregateSignatures(signatures: Uint8Array[]): Buffer {
2020-01-08 06:49:51 +00:00
assert(signatures, "signatures is null or undefined");
2019-08-05 15:48:26 +00:00
return signatures.map((signature): Signature => {
2020-02-12 15:39:40 +00:00
return Signature.fromCompressedBytes(toBuffer(signature));
2019-08-05 15:48:26 +00:00
}).reduce((previousValue, currentValue): Signature => {
return previousValue.add(currentValue);
}).toBytesCompressed();
}
/**
* Combines all given public keys into single one
* @param publicKeys
*/
2020-02-12 15:39:40 +00:00
export function aggregatePubkeys(publicKeys: Uint8Array[]): Buffer {
2020-01-08 06:49:51 +00:00
assert(publicKeys, "publicKeys is null or undefined");
2019-08-05 15:48:26 +00:00
if(publicKeys.length === 0) {
2019-11-27 20:58:41 +00:00
return Buffer.alloc(PUBLIC_KEY_LENGTH);
}
2020-02-12 15:39:40 +00:00
return publicKeys
.map((p) => PublicKey.fromBytes(toBuffer(p)))
.reduce((agg, pubKey) => agg.add(pubKey))
.toBytesCompressed();
2019-08-05 15:48:26 +00:00
}
/**
* Verifies if signature is message signed with given public key.
* @param publicKey
* @param messageHash
* @param signature
* @param domain
*/
2020-02-12 15:39:40 +00:00
export function verify(
publicKey: Uint8Array,
messageHash: Uint8Array,
signature: Uint8Array,
domain: Uint8Array
): boolean {
2020-01-08 06:49:51 +00:00
assert(publicKey, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
assert(domain, "domain is null or undefined");
2019-08-05 15:48:26 +00:00
try {
2019-11-27 20:58:41 +00:00
return PublicKey
2020-02-12 15:39:40 +00:00
.fromBytes(toBuffer(publicKey))
2020-02-04 22:30:10 +00:00
.verifyMessage(
2020-02-12 15:39:40 +00:00
Signature.fromCompressedBytes(toBuffer(signature)),
toBuffer(messageHash),
toBuffer(domain)
2020-02-04 22:30:10 +00:00
);
2019-08-05 15:48:26 +00:00
} catch (e) {
return false;
}
}
/**
* Verifies if signature is list of message signed with corresponding public key.
* @param publicKeys
* @param messageHashes
* @param signature
* @param domain
*/
2019-09-17 19:05:13 +00:00
export function verifyMultiple(
2020-02-12 15:39:40 +00:00
publicKeys: Uint8Array[],
messageHashes: Uint8Array[],
signature: Uint8Array,
domain: Uint8Array
2019-09-17 19:05:13 +00:00
): boolean {
2020-01-09 06:55:26 +00:00
assert(publicKeys, "publicKey is null or undefined");
assert(messageHashes, "messageHash is null or undefined");
2020-01-08 06:49:51 +00:00
assert(signature, "signature is null or undefined");
assert(domain, "domain is null or undefined");
2019-08-05 15:48:26 +00:00
if(publicKeys.length === 0 || publicKeys.length != messageHashes.length) {
return false;
}
try {
2019-11-27 20:58:41 +00:00
return Signature
2020-02-12 15:39:40 +00:00
.fromCompressedBytes(toBuffer(signature))
2019-11-27 20:58:41 +00:00
.verifyMultiple(
2020-02-12 15:39:40 +00:00
publicKeys.map((key) => PublicKey.fromBytes(toBuffer(key))),
messageHashes.map((m) => toBuffer(m)),
toBuffer(domain),
2019-11-27 20:58:41 +00:00
);
2019-08-05 15:48:26 +00:00
} catch (e) {
return false;
}
}
export default {
generateKeyPair,
generatePublicKey,
sign,
aggregateSignatures,
aggregatePubkeys,
verify,
verifyMultiple
2019-08-12 07:51:18 +00:00
};