diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 7bd61e5..7430d7d 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -7,12 +7,12 @@ import {PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH} from "../constants"; * @param source * @param length */ -export function padLeft(source: Buffer, length: number): Buffer { +export function padLeft(source: Uint8Array, length: number): Buffer { assert(source.length <= length, "Given array must be smaller or equal to desired array size"); const result = Buffer.alloc(length, 0); - source.copy(result, length - source.length); + result.set(source, length - source.length); return result; } export const EMPTY_PUBLIC_KEY = Buffer.alloc(PUBLIC_KEY_LENGTH); -export const EMPTY_SIGNATURE = Buffer.alloc(SIGNATURE_LENGTH); \ No newline at end of file +export const EMPTY_SIGNATURE = Buffer.alloc(SIGNATURE_LENGTH); diff --git a/src/index.ts b/src/index.ts index fcab0dd..97840ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import {Keypair} from "./keypair"; import {PrivateKey} from "./privateKey"; import {PublicKey} from "./publicKey"; import {Signature} from "./signature"; -import {BLSPubkey, BLSSecretKey, BLSSignature, Domain, bytes32} from "@chainsafe/eth2.0-types"; +import {BLSPubkey, BLSSecretKey, BLSSignature, Bytes32, Domain} from "@chainsafe/eth2.0-types"; import {PUBLIC_KEY_LENGTH} from "./constants"; import assert from "assert"; @@ -33,7 +33,7 @@ export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey { * @param messageHash * @param domain */ -export function sign(secretKey: BLSSecretKey, messageHash: bytes32, domain: Domain): BLSSignature { +export function sign(secretKey: BLSSecretKey, messageHash: Bytes32, domain: Domain): BLSSignature { assert(secretKey, "secretKey is null or undefined"); assert(messageHash, "messageHash is null or undefined"); assert(domain, "domain is null or undefined"); @@ -80,7 +80,7 @@ export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey { * @param signature * @param domain */ -export function verify(publicKey: BLSPubkey, messageHash: bytes32, signature: BLSSignature, domain: Domain): boolean { +export function verify(publicKey: BLSPubkey, messageHash: Bytes32, signature: BLSSignature, domain: Domain): boolean { assert(publicKey, "publicKey is null or undefined"); assert(messageHash, "messageHash is null or undefined"); assert(signature, "signature is null or undefined"); @@ -103,7 +103,7 @@ export function verify(publicKey: BLSPubkey, messageHash: bytes32, signature: BL */ export function verifyMultiple( publicKeys: BLSPubkey[], - messageHashes: bytes32[], + messageHashes: Bytes32[], signature: BLSSignature, domain: Domain ): boolean { diff --git a/src/privateKey.ts b/src/privateKey.ts index 03367d9..065d096 100644 --- a/src/privateKey.ts +++ b/src/privateKey.ts @@ -1,6 +1,6 @@ import {SECRET_KEY_LENGTH} from "./constants"; import assert from "assert"; -import {BLSSecretKey, Domain, bytes32} from "@chainsafe/eth2.0-types"; +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 +51,7 @@ export class PrivateKey { // return Signature.fromValue(this.value.sign(message)); // } - public signMessage(message: bytes32, domain: Domain): Signature { + public signMessage(message: Bytes32, domain: Domain): Signature { domain = padLeft(domain, 8); return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain]))); } diff --git a/src/publicKey.ts b/src/publicKey.ts index c74a415..9023339 100644 --- a/src/publicKey.ts +++ b/src/publicKey.ts @@ -1,5 +1,5 @@ import {PrivateKey} from "./privateKey"; -import {BLSPubkey, Domain, bytes32} from "@chainsafe/eth2.0-types"; +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"; @@ -22,7 +22,7 @@ export class PublicKey { public static fromBytes(bytes: BLSPubkey): PublicKey { const context = getContext(); const publicKey = new context.PublicKey(); - if(!bytes.equals(EMPTY_PUBLIC_KEY)) { + if(!EMPTY_PUBLIC_KEY.equals(bytes)) { publicKey.deserialize(bytes); } return new PublicKey( @@ -49,7 +49,7 @@ export class PublicKey { return agg; } - public verifyMessage(signature: Signature, messageHash: bytes32, domain: Domain): boolean { + public verifyMessage(signature: Signature, messageHash: Bytes32, domain: Domain): boolean { return this.value.verifyHashWithDomain(signature.getValue(), Buffer.concat([messageHash, domain])); } @@ -58,7 +58,7 @@ export class PublicKey { } public toHexString(): string { - return `0x${this.toBytesCompressed().toString("hex")}`; + return `0x${Buffer.from(this.toBytesCompressed()).toString("hex")}`; } public getValue(): PublicKeyType { diff --git a/src/signature.ts b/src/signature.ts index 399da02..3e028bc 100644 --- a/src/signature.ts +++ b/src/signature.ts @@ -1,6 +1,6 @@ import assert from "assert"; import {FP_POINT_LENGTH} from "./constants"; -import {BLSSignature, Domain, bytes32} from "@chainsafe/eth2.0-types"; +import {BLSSignature, Bytes32, Domain} from "@chainsafe/eth2.0-types"; import {SignatureType} from "@chainsafe/eth2-bls-wasm"; import {getContext} from "./context"; import {PublicKey} from "./publicKey"; @@ -21,7 +21,7 @@ export class Signature { ); const context = getContext(); const signature = new context.Signature(); - if(!value.equals(EMPTY_SIGNATURE)) { + if(!EMPTY_SIGNATURE.equals(value)) { signature.deserialize(value); } return new Signature(signature); @@ -43,12 +43,12 @@ export class Signature { return this.value; } - public verify(publicKey: PublicKey, message: bytes32, domain: Domain): boolean { + public verify(publicKey: PublicKey, message: Bytes32, domain: Domain): 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: Bytes32[], domain: Domain): boolean { domain = padLeft(domain, 8); return this.value.verifyAggregatedHashWithDomain( publicKeys.map((key) => key.getValue()),