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