From 2245559e0e631fe5388bfbd9269f8775feaf2c3f Mon Sep 17 00:00:00 2001 From: dapplion Date: Sun, 29 Nov 2020 14:28:30 +0000 Subject: [PATCH] Prevent "BLS not initialized" error from causing a false negative --- src/errors.ts | 5 +++++ src/functional.ts | 6 +++++- src/herumi/context.ts | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/errors.ts diff --git a/src/errors.ts b/src/errors.ts new file mode 100644 index 0000000..8666707 --- /dev/null +++ b/src/errors.ts @@ -0,0 +1,5 @@ +/** + * Indicate that this error is expected and should not be ignored + * by the functional interface try / catch blocks + */ +export class ExpectedError extends Error {} diff --git a/src/functional.ts b/src/functional.ts index a1287cb..da7ad41 100644 --- a/src/functional.ts +++ b/src/functional.ts @@ -1,5 +1,6 @@ -import {validateBytes} from "./helpers"; import {IBls} from "./interface"; +import {validateBytes} from "./helpers"; +import {ExpectedError} from "./errors"; // Returned type is enforced at each implementation's index // eslint-disable-next-line @typescript-eslint/explicit-function-return-type @@ -53,6 +54,7 @@ export function functionalInterfaceFactory({ try { return Signature.fromBytes(signature).verify(PublicKey.fromBytes(publicKey), message); } catch (e) { + if (e instanceof ExpectedError) throw e; return false; } } @@ -74,6 +76,7 @@ export function functionalInterfaceFactory({ message ); } catch (e) { + if (e instanceof ExpectedError) throw e; return false; } } @@ -99,6 +102,7 @@ export function functionalInterfaceFactory({ messages.map((msg) => msg) ); } catch (e) { + if (e instanceof ExpectedError) throw e; return false; } } diff --git a/src/herumi/context.ts b/src/herumi/context.ts index a35a59f..b4da52e 100644 --- a/src/herumi/context.ts +++ b/src/herumi/context.ts @@ -1,5 +1,6 @@ /* eslint-disable require-atomic-updates */ import bls from "bls-eth-wasm"; +import {ExpectedError} from "../errors"; type Bls = typeof bls; let blsGlobal: Bls | null = null; @@ -27,7 +28,7 @@ export function destroy(): void { export function getContext(): Bls { if (!blsGlobal) { - throw new Error("BLS not initialized"); + throw new ExpectedError("BLS not initialized"); } return blsGlobal; }