Fix w new vector types
This commit is contained in:
parent
825726b5cc
commit
c5cecec09b
29
src/index.ts
29
src/index.ts
|
@ -23,7 +23,7 @@ export function generateKeyPair(): Keypair {
|
||||||
*/
|
*/
|
||||||
export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey {
|
export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey {
|
||||||
assert(secretKey, "secretKey is null or undefined");
|
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();
|
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(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(secretKey);
|
const privateKey = PrivateKey.fromBytes(Buffer.from(secretKey as Uint8Array));
|
||||||
return privateKey.signMessage(messageHash, domain).toBytesCompressed();
|
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 {
|
export function aggregateSignatures(signatures: BLSSignature[]): BLSSignature {
|
||||||
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(signature);
|
return Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array));
|
||||||
}).reduce((previousValue, currentValue): Signature => {
|
}).reduce((previousValue, currentValue): Signature => {
|
||||||
return previousValue.add(currentValue);
|
return previousValue.add(currentValue);
|
||||||
}).toBytesCompressed();
|
}).toBytesCompressed();
|
||||||
|
@ -63,7 +66,7 @@ export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey {
|
||||||
if(publicKeys.length === 0) {
|
if(publicKeys.length === 0) {
|
||||||
return Buffer.alloc(PUBLIC_KEY_LENGTH);
|
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) {
|
if(agg) {
|
||||||
return agg.add(pubKey);
|
return agg.add(pubKey);
|
||||||
} else {
|
} else {
|
||||||
|
@ -87,8 +90,12 @@ export function verify(publicKey: BLSPubkey, messageHash: Bytes32, signature: BL
|
||||||
assert(domain, "domain is null or undefined");
|
assert(domain, "domain is null or undefined");
|
||||||
try {
|
try {
|
||||||
return PublicKey
|
return PublicKey
|
||||||
.fromBytes(publicKey)
|
.fromBytes(Buffer.from(publicKey as Uint8Array))
|
||||||
.verifyMessage(Signature.fromCompressedBytes(signature), messageHash, domain);
|
.verifyMessage(
|
||||||
|
Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array)),
|
||||||
|
Buffer.from(messageHash as Uint8Array),
|
||||||
|
Buffer.from(domain as Uint8Array)
|
||||||
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -117,11 +124,11 @@ export function verifyMultiple(
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return Signature
|
return Signature
|
||||||
.fromCompressedBytes(signature)
|
.fromCompressedBytes(Buffer.from(signature as Uint8Array))
|
||||||
.verifyMultiple(
|
.verifyMultiple(
|
||||||
publicKeys.map((key) => PublicKey.fromBytes(key)),
|
publicKeys.map((key) => PublicKey.fromBytes(Buffer.from(key as Uint8Array))),
|
||||||
messageHashes,
|
messageHashes.map((m) => Buffer.from(m as Uint8Array)),
|
||||||
domain
|
Buffer.from(domain as Uint8Array),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import {SECRET_KEY_LENGTH} from "./constants";
|
import {SECRET_KEY_LENGTH} from "./constants";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import {BLSSecretKey, Bytes32, Domain} from "@chainsafe/eth2.0-types";
|
|
||||||
import {SecretKeyType} from "@chainsafe/eth2-bls-wasm";
|
import {SecretKeyType} from "@chainsafe/eth2-bls-wasm";
|
||||||
import {generateRandomSecretKey} from "@chainsafe/bls-keygen";
|
import {generateRandomSecretKey} from "@chainsafe/bls-keygen";
|
||||||
import {getContext} from "./context";
|
import {getContext} from "./context";
|
||||||
|
@ -51,7 +50,7 @@ export class PrivateKey {
|
||||||
// return Signature.fromValue(this.value.sign(message));
|
// 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);
|
domain = padLeft(domain, 8);
|
||||||
return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain])));
|
return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain])));
|
||||||
}
|
}
|
||||||
|
@ -60,7 +59,7 @@ export class PrivateKey {
|
||||||
return PublicKey.fromPublicKeyType(this.value.getPublicKey());
|
return PublicKey.fromPublicKeyType(this.value.getPublicKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
public toBytes(): BLSSecretKey {
|
public toBytes(): Buffer {
|
||||||
return Buffer.from(this.value.serialize());
|
return Buffer.from(this.value.serialize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import {PrivateKey} from "./privateKey";
|
import {PrivateKey} from "./privateKey";
|
||||||
import {BLSPubkey, Bytes32, Domain} from "@chainsafe/eth2.0-types";
|
|
||||||
import {PublicKeyType} from "@chainsafe/eth2-bls-wasm";
|
import {PublicKeyType} from "@chainsafe/eth2-bls-wasm";
|
||||||
import {getContext} from "./context";
|
import {getContext} from "./context";
|
||||||
import {PUBLIC_KEY_LENGTH} from "./constants";
|
import {PUBLIC_KEY_LENGTH} from "./constants";
|
||||||
|
@ -14,12 +13,12 @@ export class PublicKey {
|
||||||
protected constructor(value: PublicKeyType) {
|
protected constructor(value: PublicKeyType) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static fromPrivateKey(privateKey: PrivateKey): PublicKey {
|
public static fromPrivateKey(privateKey: PrivateKey): PublicKey {
|
||||||
return privateKey.toPublicKey();
|
return privateKey.toPublicKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static fromBytes(bytes: BLSPubkey): PublicKey {
|
public static fromBytes(bytes: Uint8Array): PublicKey {
|
||||||
const context = getContext();
|
const context = getContext();
|
||||||
const publicKey = new context.PublicKey();
|
const publicKey = new context.PublicKey();
|
||||||
if(!EMPTY_PUBLIC_KEY.equals(bytes)) {
|
if(!EMPTY_PUBLIC_KEY.equals(bytes)) {
|
||||||
|
@ -49,11 +48,11 @@ export class PublicKey {
|
||||||
return agg;
|
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]));
|
return this.value.verifyHashWithDomain(signature.getValue(), Buffer.concat([messageHash, domain]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public toBytesCompressed(): BLSPubkey {
|
public toBytesCompressed(): Buffer {
|
||||||
return Buffer.from(this.value.serialize());
|
return Buffer.from(this.value.serialize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import {FP_POINT_LENGTH} from "./constants";
|
import {FP_POINT_LENGTH} from "./constants";
|
||||||
import {BLSSignature, Bytes32, Domain} from "@chainsafe/eth2.0-types";
|
|
||||||
import {SignatureType} from "@chainsafe/eth2-bls-wasm";
|
import {SignatureType} from "@chainsafe/eth2-bls-wasm";
|
||||||
import {getContext} from "./context";
|
import {getContext} from "./context";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
|
@ -14,7 +13,7 @@ export class Signature {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static fromCompressedBytes(value: BLSSignature): Signature {
|
public static fromCompressedBytes(value: Uint8Array): Signature {
|
||||||
assert(
|
assert(
|
||||||
value.length === 2 * FP_POINT_LENGTH,
|
value.length === 2 * FP_POINT_LENGTH,
|
||||||
`Signature must have ${2 * FP_POINT_LENGTH} bytes`
|
`Signature must have ${2 * FP_POINT_LENGTH} bytes`
|
||||||
|
@ -43,20 +42,20 @@ export class Signature {
|
||||||
return this.value;
|
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);
|
domain = padLeft(domain, 8);
|
||||||
return publicKey.verifyMessage(this, message, domain);
|
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);
|
domain = padLeft(domain, 8);
|
||||||
return this.value.verifyAggregatedHashWithDomain(
|
return this.value.verifyAggregatedHashWithDomain(
|
||||||
publicKeys.map((key) => key.getValue()),
|
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());
|
return Buffer.from(this.value.serialize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue