From c5cecec09b58ce1128da43855db976f36d4746d9 Mon Sep 17 00:00:00 2001 From: Cayman Date: Tue, 4 Feb 2020 16:30:10 -0600 Subject: [PATCH] Fix w new vector types --- src/index.ts | 29 ++++++++++++++++++----------- src/privateKey.ts | 5 ++--- src/publicKey.ts | 9 ++++----- src/signature.ts | 11 +++++------ 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/index.ts b/src/index.ts index 97840ed..2126b10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,7 @@ export function generateKeyPair(): Keypair { */ export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey { assert(secretKey, "secretKey is null or undefined"); - const keypair = new Keypair(PrivateKey.fromBytes(secretKey)); + const keypair = new Keypair(PrivateKey.fromBytes(Buffer.from(secretKey as Uint8Array))); return keypair.publicKey.toBytesCompressed(); } @@ -37,8 +37,11 @@ export function sign(secretKey: BLSSecretKey, messageHash: Bytes32, domain: Doma 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(secretKey); - return privateKey.signMessage(messageHash, domain).toBytesCompressed(); + const privateKey = PrivateKey.fromBytes(Buffer.from(secretKey as Uint8Array)); + return privateKey.signMessage( + Buffer.from(messageHash as Uint8Array), + Buffer.from(domain as Uint8Array) + ).toBytesCompressed(); } /** @@ -48,7 +51,7 @@ export function sign(secretKey: BLSSecretKey, messageHash: Bytes32, domain: Doma export function aggregateSignatures(signatures: BLSSignature[]): BLSSignature { assert(signatures, "signatures is null or undefined"); return signatures.map((signature): Signature => { - return Signature.fromCompressedBytes(signature); + return Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array)); }).reduce((previousValue, currentValue): Signature => { return previousValue.add(currentValue); }).toBytesCompressed(); @@ -63,7 +66,7 @@ export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey { if(publicKeys.length === 0) { return Buffer.alloc(PUBLIC_KEY_LENGTH); } - return publicKeys.map(PublicKey.fromBytes).reduce((agg, pubKey) => { + return publicKeys.map((p) => PublicKey.fromBytes(Buffer.from(p as Uint8Array))).reduce((agg, pubKey) => { if(agg) { return agg.add(pubKey); } else { @@ -87,8 +90,12 @@ export function verify(publicKey: BLSPubkey, messageHash: Bytes32, signature: BL assert(domain, "domain is null or undefined"); try { return PublicKey - .fromBytes(publicKey) - .verifyMessage(Signature.fromCompressedBytes(signature), messageHash, domain); + .fromBytes(Buffer.from(publicKey as Uint8Array)) + .verifyMessage( + Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array)), + Buffer.from(messageHash as Uint8Array), + Buffer.from(domain as Uint8Array) + ); } catch (e) { return false; } @@ -117,11 +124,11 @@ export function verifyMultiple( } try { return Signature - .fromCompressedBytes(signature) + .fromCompressedBytes(Buffer.from(signature as Uint8Array)) .verifyMultiple( - publicKeys.map((key) => PublicKey.fromBytes(key)), - messageHashes, - domain + publicKeys.map((key) => PublicKey.fromBytes(Buffer.from(key as Uint8Array))), + messageHashes.map((m) => Buffer.from(m as Uint8Array)), + Buffer.from(domain as Uint8Array), ); } catch (e) { return false; diff --git a/src/privateKey.ts b/src/privateKey.ts index 065d096..d4e5051 100644 --- a/src/privateKey.ts +++ b/src/privateKey.ts @@ -1,6 +1,5 @@ import {SECRET_KEY_LENGTH} from "./constants"; import assert from "assert"; -import {BLSSecretKey, Bytes32, Domain} from "@chainsafe/eth2.0-types"; import {SecretKeyType} from "@chainsafe/eth2-bls-wasm"; import {generateRandomSecretKey} from "@chainsafe/bls-keygen"; import {getContext} from "./context"; @@ -51,7 +50,7 @@ export class PrivateKey { // return Signature.fromValue(this.value.sign(message)); // } - public signMessage(message: Bytes32, domain: Domain): Signature { + public signMessage(message: Uint8Array, domain: Uint8Array): Signature { domain = padLeft(domain, 8); return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain]))); } @@ -60,7 +59,7 @@ export class PrivateKey { return PublicKey.fromPublicKeyType(this.value.getPublicKey()); } - public toBytes(): BLSSecretKey { + public toBytes(): Buffer { return Buffer.from(this.value.serialize()); } diff --git a/src/publicKey.ts b/src/publicKey.ts index 9023339..6babd16 100644 --- a/src/publicKey.ts +++ b/src/publicKey.ts @@ -1,5 +1,4 @@ import {PrivateKey} from "./privateKey"; -import {BLSPubkey, Bytes32, Domain} from "@chainsafe/eth2.0-types"; import {PublicKeyType} from "@chainsafe/eth2-bls-wasm"; import {getContext} from "./context"; import {PUBLIC_KEY_LENGTH} from "./constants"; @@ -14,12 +13,12 @@ export class PublicKey { protected constructor(value: PublicKeyType) { this.value = value; } - + public static fromPrivateKey(privateKey: PrivateKey): PublicKey { return privateKey.toPublicKey(); } - public static fromBytes(bytes: BLSPubkey): PublicKey { + public static fromBytes(bytes: Uint8Array): PublicKey { const context = getContext(); const publicKey = new context.PublicKey(); if(!EMPTY_PUBLIC_KEY.equals(bytes)) { @@ -49,11 +48,11 @@ export class PublicKey { return agg; } - public verifyMessage(signature: Signature, messageHash: Bytes32, domain: Domain): boolean { + public verifyMessage(signature: Signature, messageHash: Uint8Array, domain: Uint8Array): boolean { return this.value.verifyHashWithDomain(signature.getValue(), Buffer.concat([messageHash, domain])); } - public toBytesCompressed(): BLSPubkey { + public toBytesCompressed(): Buffer { return Buffer.from(this.value.serialize()); } diff --git a/src/signature.ts b/src/signature.ts index 3e028bc..7d8786e 100644 --- a/src/signature.ts +++ b/src/signature.ts @@ -1,6 +1,5 @@ import assert from "assert"; import {FP_POINT_LENGTH} from "./constants"; -import {BLSSignature, Bytes32, Domain} from "@chainsafe/eth2.0-types"; import {SignatureType} from "@chainsafe/eth2-bls-wasm"; import {getContext} from "./context"; import {PublicKey} from "./publicKey"; @@ -14,7 +13,7 @@ export class Signature { this.value = value; } - public static fromCompressedBytes(value: BLSSignature): Signature { + public static fromCompressedBytes(value: Uint8Array): Signature { assert( value.length === 2 * FP_POINT_LENGTH, `Signature must have ${2 * FP_POINT_LENGTH} bytes` @@ -43,20 +42,20 @@ export class Signature { return this.value; } - public verify(publicKey: PublicKey, message: Bytes32, domain: Domain): boolean { + public verify(publicKey: PublicKey, message: Uint8Array, domain: Uint8Array): boolean { domain = padLeft(domain, 8); return publicKey.verifyMessage(this, message, domain); } - public verifyMultiple(publicKeys: PublicKey[], messages: Bytes32[], domain: Domain): boolean { + public verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[], domain: Uint8Array): boolean { domain = padLeft(domain, 8); return this.value.verifyAggregatedHashWithDomain( publicKeys.map((key) => key.getValue()), - messages.map((message) => Buffer.concat([message, domain])) + messages.map((message) => Buffer.concat([Buffer.from(message as Uint8Array), domain as Uint8Array])) ); } - public toBytesCompressed(): BLSSignature { + public toBytesCompressed(): Buffer { return Buffer.from(this.value.serialize()); }