Prevent "BLS not initialized" error from causing a false negative

This commit is contained in:
dapplion 2020-11-29 14:28:30 +00:00
parent 5db911d470
commit 2245559e0e
3 changed files with 12 additions and 2 deletions

5
src/errors.ts Normal file
View File

@ -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 {}

View File

@ -1,5 +1,6 @@
import {validateBytes} from "./helpers";
import {IBls} from "./interface"; import {IBls} from "./interface";
import {validateBytes} from "./helpers";
import {ExpectedError} from "./errors";
// Returned type is enforced at each implementation's index // Returned type is enforced at each implementation's index
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
@ -53,6 +54,7 @@ export function functionalInterfaceFactory({
try { try {
return Signature.fromBytes(signature).verify(PublicKey.fromBytes(publicKey), message); return Signature.fromBytes(signature).verify(PublicKey.fromBytes(publicKey), message);
} catch (e) { } catch (e) {
if (e instanceof ExpectedError) throw e;
return false; return false;
} }
} }
@ -74,6 +76,7 @@ export function functionalInterfaceFactory({
message message
); );
} catch (e) { } catch (e) {
if (e instanceof ExpectedError) throw e;
return false; return false;
} }
} }
@ -99,6 +102,7 @@ export function functionalInterfaceFactory({
messages.map((msg) => msg) messages.map((msg) => msg)
); );
} catch (e) { } catch (e) {
if (e instanceof ExpectedError) throw e;
return false; return false;
} }
} }

View File

@ -1,5 +1,6 @@
/* eslint-disable require-atomic-updates */ /* eslint-disable require-atomic-updates */
import bls from "bls-eth-wasm"; import bls from "bls-eth-wasm";
import {ExpectedError} from "../errors";
type Bls = typeof bls; type Bls = typeof bls;
let blsGlobal: Bls | null = null; let blsGlobal: Bls | null = null;
@ -27,7 +28,7 @@ export function destroy(): void {
export function getContext(): Bls { export function getContext(): Bls {
if (!blsGlobal) { if (!blsGlobal) {
throw new Error("BLS not initialized"); throw new ExpectedError("BLS not initialized");
} }
return blsGlobal; return blsGlobal;
} }