Simplify bls functional interface
This commit is contained in:
parent
fa1f393e79
commit
9a52ed5672
|
@ -40,7 +40,6 @@
|
|||
"dependencies": {
|
||||
"@chainsafe/bls-keygen": "^0.0.2",
|
||||
"@chainsafe/eth2-bls-wasm": "^0.2.0",
|
||||
"@chainsafe/eth2.0-types": "^0.2.0",
|
||||
"assert": "^1.4.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
66
src/index.ts
66
src/index.ts
|
@ -2,7 +2,6 @@ import {Keypair} from "./keypair";
|
|||
import {PrivateKey} from "./privateKey";
|
||||
import {PublicKey} from "./publicKey";
|
||||
import {Signature} from "./signature";
|
||||
import {BLSPubkey, BLSSecretKey, BLSSignature, Bytes32, Domain} from "@chainsafe/eth2.0-types";
|
||||
import {PUBLIC_KEY_LENGTH} from "./constants";
|
||||
import assert from "assert";
|
||||
|
||||
|
@ -10,6 +9,10 @@ export {Keypair, PrivateKey, PublicKey, Signature};
|
|||
|
||||
export {init as initBLS} from "./context";
|
||||
|
||||
function toBuffer(input: Uint8Array): Buffer {
|
||||
return Buffer.from(input.buffer, input.byteOffset, input.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates new secret and public key
|
||||
*/
|
||||
|
@ -21,9 +24,9 @@ export function generateKeyPair(): Keypair {
|
|||
* Generates public key from given secret.
|
||||
* @param {BLSSecretKey} secretKey
|
||||
*/
|
||||
export function generatePublicKey(secretKey: BLSSecretKey): Buffer {
|
||||
export function generatePublicKey(secretKey: Uint8Array): Buffer {
|
||||
assert(secretKey, "secretKey is null or undefined");
|
||||
const keypair = new Keypair(PrivateKey.fromBytes(Buffer.from(secretKey as Uint8Array)));
|
||||
const keypair = new Keypair(PrivateKey.fromBytes(toBuffer(secretKey)));
|
||||
return keypair.publicKey.toBytesCompressed();
|
||||
}
|
||||
|
||||
|
@ -33,14 +36,14 @@ export function generatePublicKey(secretKey: BLSSecretKey): Buffer {
|
|||
* @param messageHash
|
||||
* @param domain
|
||||
*/
|
||||
export function sign(secretKey: BLSSecretKey, messageHash: Bytes32, domain: Domain): Buffer {
|
||||
export function sign(secretKey: Uint8Array, messageHash: Uint8Array, domain: Uint8Array): Buffer {
|
||||
assert(secretKey, "secretKey is null or undefined");
|
||||
assert(messageHash, "messageHash is null or undefined");
|
||||
assert(domain, "domain is null or undefined");
|
||||
const privateKey = PrivateKey.fromBytes(Buffer.from(secretKey as Uint8Array));
|
||||
const privateKey = PrivateKey.fromBytes(toBuffer(secretKey));
|
||||
return privateKey.signMessage(
|
||||
Buffer.from(messageHash as Uint8Array),
|
||||
Buffer.from(domain as Uint8Array)
|
||||
toBuffer(messageHash),
|
||||
toBuffer(domain),
|
||||
).toBytesCompressed();
|
||||
}
|
||||
|
||||
|
@ -48,10 +51,10 @@ export function sign(secretKey: BLSSecretKey, messageHash: Bytes32, domain: Doma
|
|||
* Compines all given signature into one.
|
||||
* @param signatures
|
||||
*/
|
||||
export function aggregateSignatures(signatures: BLSSignature[]): Buffer {
|
||||
export function aggregateSignatures(signatures: Uint8Array[]): Buffer {
|
||||
assert(signatures, "signatures is null or undefined");
|
||||
return signatures.map((signature): Signature => {
|
||||
return Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array));
|
||||
return Signature.fromCompressedBytes(toBuffer(signature));
|
||||
}).reduce((previousValue, currentValue): Signature => {
|
||||
return previousValue.add(currentValue);
|
||||
}).toBytesCompressed();
|
||||
|
@ -61,19 +64,15 @@ export function aggregateSignatures(signatures: BLSSignature[]): Buffer {
|
|||
* Combines all given public keys into single one
|
||||
* @param publicKeys
|
||||
*/
|
||||
export function aggregatePubkeys(publicKeys: BLSPubkey[]): Buffer {
|
||||
export function aggregatePubkeys(publicKeys: Uint8Array[]): Buffer {
|
||||
assert(publicKeys, "publicKeys is null or undefined");
|
||||
if(publicKeys.length === 0) {
|
||||
return Buffer.alloc(PUBLIC_KEY_LENGTH);
|
||||
}
|
||||
return publicKeys.map((p) => PublicKey.fromBytes(Buffer.from(p as Uint8Array))).reduce((agg, pubKey) => {
|
||||
if(agg) {
|
||||
return agg.add(pubKey);
|
||||
} else {
|
||||
return pubKey;
|
||||
}
|
||||
}
|
||||
).toBytesCompressed();
|
||||
return publicKeys
|
||||
.map((p) => PublicKey.fromBytes(toBuffer(p)))
|
||||
.reduce((agg, pubKey) => agg.add(pubKey))
|
||||
.toBytesCompressed();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,18 +82,23 @@ export function aggregatePubkeys(publicKeys: BLSPubkey[]): Buffer {
|
|||
* @param signature
|
||||
* @param domain
|
||||
*/
|
||||
export function verify(publicKey: BLSPubkey, messageHash: Bytes32, signature: BLSSignature, domain: Domain): boolean {
|
||||
export function verify(
|
||||
publicKey: Uint8Array,
|
||||
messageHash: Uint8Array,
|
||||
signature: Uint8Array,
|
||||
domain: Uint8Array
|
||||
): boolean {
|
||||
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");
|
||||
try {
|
||||
return PublicKey
|
||||
.fromBytes(Buffer.from(publicKey as Uint8Array))
|
||||
.fromBytes(toBuffer(publicKey))
|
||||
.verifyMessage(
|
||||
Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array)),
|
||||
Buffer.from(messageHash as Uint8Array),
|
||||
Buffer.from(domain as Uint8Array)
|
||||
Signature.fromCompressedBytes(toBuffer(signature)),
|
||||
toBuffer(messageHash),
|
||||
toBuffer(domain)
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
|
@ -109,10 +113,10 @@ export function verify(publicKey: BLSPubkey, messageHash: Bytes32, signature: BL
|
|||
* @param domain
|
||||
*/
|
||||
export function verifyMultiple(
|
||||
publicKeys: BLSPubkey[],
|
||||
messageHashes: Bytes32[],
|
||||
signature: BLSSignature,
|
||||
domain: Domain
|
||||
publicKeys: Uint8Array[],
|
||||
messageHashes: Uint8Array[],
|
||||
signature: Uint8Array,
|
||||
domain: Uint8Array
|
||||
): boolean {
|
||||
assert(publicKeys, "publicKey is null or undefined");
|
||||
assert(messageHashes, "messageHash is null or undefined");
|
||||
|
@ -124,11 +128,11 @@ export function verifyMultiple(
|
|||
}
|
||||
try {
|
||||
return Signature
|
||||
.fromCompressedBytes(Buffer.from(signature as Uint8Array))
|
||||
.fromCompressedBytes(toBuffer(signature))
|
||||
.verifyMultiple(
|
||||
publicKeys.map((key) => PublicKey.fromBytes(Buffer.from(key as Uint8Array))),
|
||||
messageHashes.map((m) => Buffer.from(m as Uint8Array)),
|
||||
Buffer.from(domain as Uint8Array),
|
||||
publicKeys.map((key) => PublicKey.fromBytes(toBuffer(key))),
|
||||
messageHashes.map((m) => toBuffer(m)),
|
||||
toBuffer(domain),
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
|
|
Reference in New Issue