Merge pull request #37 from ChainSafe/expected-errors

Prevent "BLS not initialized" error from causing a false negative
This commit is contained in:
Cayman 2020-11-29 09:51:06 -07:00 committed by GitHub
commit 3a34ccbacc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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;
} }