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-09-03 18:17:55 +00:00
|
|
|
import {BLSPubkey, BLSSecretKey, BLSSignature, Domain, Hash} from "@chainsafe/eth2.0-types";
|
2019-11-27 20:58:41 +00:00
|
|
|
import {init} from "./context";
|
|
|
|
import {PUBLIC_KEY_LENGTH} from "./constants";
|
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-11-27 20:58:41 +00:00
|
|
|
|
|
|
|
export async function initLibrary(): Promise<void> {
|
|
|
|
await init();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2019-08-12 07:51:18 +00:00
|
|
|
export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey {
|
2019-08-05 15:48:26 +00:00
|
|
|
const keypair = new Keypair(PrivateKey.fromBytes(secretKey));
|
|
|
|
return keypair.publicKey.toBytesCompressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signs given message using secret key.
|
|
|
|
* @param secretKey
|
|
|
|
* @param messageHash
|
|
|
|
* @param domain
|
|
|
|
*/
|
2019-09-03 18:17:55 +00:00
|
|
|
export function sign(secretKey: BLSSecretKey, messageHash: Hash, domain: Domain): BLSSignature {
|
2019-08-05 15:48:26 +00:00
|
|
|
const privateKey = PrivateKey.fromBytes(secretKey);
|
2019-11-27 20:58:41 +00:00
|
|
|
return privateKey.signMessage(messageHash, domain).toBytesCompressed();
|
2019-08-05 15:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compines all given signature into one.
|
|
|
|
* @param signatures
|
|
|
|
*/
|
2019-08-12 07:51:18 +00:00
|
|
|
export function aggregateSignatures(signatures: BLSSignature[]): BLSSignature {
|
2019-08-05 15:48:26 +00:00
|
|
|
return signatures.map((signature): Signature => {
|
2019-08-12 07:51:18 +00:00
|
|
|
return Signature.fromCompressedBytes(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
|
|
|
|
*/
|
2019-08-12 07:51:18 +00:00
|
|
|
export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey {
|
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);
|
|
|
|
}
|
|
|
|
return publicKeys.map(PublicKey.fromBytes).reduce((agg, pubKey) => {
|
|
|
|
if(agg) {
|
|
|
|
return agg.add(pubKey);
|
|
|
|
} else {
|
|
|
|
return pubKey;
|
|
|
|
}
|
2019-08-05 15:48:26 +00:00
|
|
|
}
|
2019-11-27 20:58:41 +00:00
|
|
|
).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
|
|
|
|
*/
|
2019-09-03 18:17:55 +00:00
|
|
|
export function verify(publicKey: BLSPubkey, messageHash: Hash, signature: BLSSignature, domain: Domain): boolean {
|
2019-08-05 15:48:26 +00:00
|
|
|
try {
|
2019-11-27 20:58:41 +00:00
|
|
|
return PublicKey
|
|
|
|
.fromBytes(publicKey)
|
|
|
|
.verifyMessage(Signature.fromCompressedBytes(signature), messageHash, domain);
|
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(
|
|
|
|
publicKeys: BLSPubkey[],
|
|
|
|
messageHashes: Hash[],
|
|
|
|
signature: BLSSignature,
|
|
|
|
domain: Domain
|
|
|
|
): boolean {
|
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
|
|
|
|
.fromCompressedBytes(signature)
|
|
|
|
.verifyMultiple(
|
|
|
|
publicKeys.map((key) => PublicKey.fromBytes(key)),
|
|
|
|
messageHashes,
|
|
|
|
domain
|
|
|
|
);
|
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
|
|
|
};
|