From 999a5d5e22aeb2d3b23da7af6b67a285faa04dbb Mon Sep 17 00:00:00 2001 From: dapplion Date: Mon, 30 Nov 2020 11:29:24 +0000 Subject: [PATCH 1/6] Proxy named exports in root index --- src/index.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index bcf1267..6179fa0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,7 +31,20 @@ export async function init(impl: Implementation): Promise { // Using Object.assign instead of just bls = getImplementation() // because otherwise the default import breaks. The reference is lost // and the imported object is still undefined after calling init() - Object.assign(bls, await getImplementation(impl)); + const blsImpl = await getImplementation(impl); + Object.assign(bls, blsImpl); + Object.assign(exports, blsImpl); } +// Proxy named exports, will get set by `Object.assign(exports, blsImpl)` +export let sign: IBls["sign"]; +export let aggregateSignatures: IBls["aggregateSignatures"]; +export let aggregatePubkeys: IBls["aggregatePubkeys"]; +export let verify: IBls["verify"]; +export let verifyAggregate: IBls["verifyAggregate"]; +export let verifyMultiple: IBls["verifyMultiple"]; +export let PrivateKey: IBls["PrivateKey"]; +export let PublicKey: IBls["PublicKey"]; +export let Signature: IBls["Signature"]; + export default bls; From 0a3f6f56591d5ad30bba0a80549bc7f5b6d6f9e9 Mon Sep 17 00:00:00 2001 From: dapplion Date: Mon, 30 Nov 2020 11:32:36 +0000 Subject: [PATCH 2/6] Use declare only --- src/index.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6179fa0..9b55c9a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,14 +37,14 @@ export async function init(impl: Implementation): Promise { } // Proxy named exports, will get set by `Object.assign(exports, blsImpl)` -export let sign: IBls["sign"]; -export let aggregateSignatures: IBls["aggregateSignatures"]; -export let aggregatePubkeys: IBls["aggregatePubkeys"]; -export let verify: IBls["verify"]; -export let verifyAggregate: IBls["verifyAggregate"]; -export let verifyMultiple: IBls["verifyMultiple"]; -export let PrivateKey: IBls["PrivateKey"]; -export let PublicKey: IBls["PublicKey"]; -export let Signature: IBls["Signature"]; +export declare let sign: IBls["sign"]; +export declare let aggregateSignatures: IBls["aggregateSignatures"]; +export declare let aggregatePubkeys: IBls["aggregatePubkeys"]; +export declare let verify: IBls["verify"]; +export declare let verifyAggregate: IBls["verifyAggregate"]; +export declare let verifyMultiple: IBls["verifyMultiple"]; +export declare let PrivateKey: IBls["PrivateKey"]; +export declare let PublicKey: IBls["PublicKey"]; +export declare let Signature: IBls["Signature"]; export default bls; From 9b7e5f5f95aed97b8e517bfe4db5fd88a6a13348 Mon Sep 17 00:00:00 2001 From: dapplion Date: Mon, 30 Nov 2020 11:43:47 +0000 Subject: [PATCH 3/6] Export class interfaces too --- src/index.ts | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9b55c9a..cad6199 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import {IBls} from "./interface"; +import {IBls, IPrivateKey, IPublicKey, ISignature} from "./interface"; import {bls as blsHerumi} from "./herumi"; export type Implementation = "herumi" | "blst-native"; @@ -7,6 +7,7 @@ export * from "./interface"; // TODO: Use a Proxy for example to throw an error if it's not initialized yet export const bls: IBls = {} as IBls; +export default bls; async function getImplementation(impl: Implementation = "herumi"): Promise { switch (impl) { @@ -43,8 +44,32 @@ export declare let aggregatePubkeys: IBls["aggregatePubkeys"]; export declare let verify: IBls["verify"]; export declare let verifyAggregate: IBls["verifyAggregate"]; export declare let verifyMultiple: IBls["verifyMultiple"]; -export declare let PrivateKey: IBls["PrivateKey"]; -export declare let PublicKey: IBls["PublicKey"]; -export declare let Signature: IBls["Signature"]; -export default bls; +export declare class PrivateKey implements IPrivateKey { + static fromBytes(bytes: Uint8Array): PrivateKey; + static fromHex(hex: string): PrivateKey; + static fromKeygen(entropy?: Uint8Array): PrivateKey; + sign(message: Uint8Array): Signature; + toPublicKey(): PublicKey; + toBytes(): Uint8Array; + toHex(): string; +} + +export declare class PublicKey implements IPublicKey { + static fromBytes(bytes: Uint8Array): PublicKey; + static fromHex(hex: string): PublicKey; + static aggregate(pubkeys: PublicKey[]): PublicKey; + toBytes(): Uint8Array; + toHex(): string; +} + +export declare class Signature implements ISignature { + static fromBytes(bytes: Uint8Array): Signature; + static fromHex(hex: string): Signature; + static aggregate(signatures: Signature[]): Signature; + verify(publicKey: PublicKey, message: Uint8Array): boolean; + verifyAggregate(publicKeys: PublicKey[], message: Uint8Array): boolean; + verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[]): boolean; + toBytes(): Uint8Array; + toHex(): string; +} From 2c938ddeed7e61e84cdf78e307631fa166e8c5a7 Mon Sep 17 00:00:00 2001 From: dapplion Date: Mon, 30 Nov 2020 11:49:37 +0000 Subject: [PATCH 4/6] Test named exports --- test/unit/index-named-exports.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 test/unit/index-named-exports.test.ts diff --git a/test/unit/index-named-exports.test.ts b/test/unit/index-named-exports.test.ts new file mode 100644 index 0000000..e383ebf --- /dev/null +++ b/test/unit/index-named-exports.test.ts @@ -0,0 +1,21 @@ +import {expect} from "chai"; +import {PrivateKey, PublicKey, Signature, init} from "../../src"; + +describe("index named exports", () => { + it("Classes and methods should be defined", async () => { + await init("herumi"); + + const sk = PrivateKey.fromKeygen(); + const msg = new Uint8Array(32); + const sig = sk.sign(msg); + const pk = sk.toPublicKey(); + expect(verifyHelper(pk, sig, msg)).to.be.true; + }); + + /** + * Sample helper to test argument typing + */ + function verifyHelper(pk: PublicKey, sig: Signature, msg: Uint8Array): boolean { + return sig.verify(pk, msg); + } +}); From 3004076a4a4ffb13449eac661af5ff295d22bca6 Mon Sep 17 00:00:00 2001 From: dapplion Date: Mon, 30 Nov 2020 11:49:47 +0000 Subject: [PATCH 5/6] Remove console.log --- test/unit/helpers/bytes.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/helpers/bytes.test.ts b/test/unit/helpers/bytes.test.ts index 1958185..2b14df7 100644 --- a/test/unit/helpers/bytes.test.ts +++ b/test/unit/helpers/bytes.test.ts @@ -17,7 +17,6 @@ describe("helpers / bytes", () => { for (const {hex, isZero} of testCases) { it(`${hex} isZero = ${isZero}`, () => { const bytes = hexToBytesNode(hex); - console.log(bytes); expect(isZeroUint8Array(bytes)).to.equal(isZero); }); } From a44d1bf2d0ce342bb7217ebd81392e0941fcbfcc Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 30 Nov 2020 10:12:18 -0600 Subject: [PATCH 6/6] Add named export test to browser test cases --- karma.conf.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 4599b2a..1423525 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -6,7 +6,7 @@ module.exports = function(config) { basePath: "", frameworks: ["mocha", "chai"], - files: ["test/unit/run-web-implementation.test.ts"], + files: ["test/unit/run-web-implementation.test.ts", "test/unit/index-named-exports.test.ts"], exclude: [], preprocessors: { "test/**/*.ts": ["webpack"] @@ -23,4 +23,4 @@ module.exports = function(config) { singleRun: true }); -}; \ No newline at end of file +};