From f2dbdaa974f7668f2a27c46db9fd4b08af5fb745 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Mon, 30 May 2022 21:41:49 +0200 Subject: [PATCH] vm: fix ecrecover precompile for v=0 and v=1 --- packages/vm/src/evm/precompiles/01-ecrecover.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/vm/src/evm/precompiles/01-ecrecover.ts b/packages/vm/src/evm/precompiles/01-ecrecover.ts index 0557210d45..8d5a96d249 100644 --- a/packages/vm/src/evm/precompiles/01-ecrecover.ts +++ b/packages/vm/src/evm/precompiles/01-ecrecover.ts @@ -16,6 +16,18 @@ export default function (opts: PrecompileInput): ExecResult { const msgHash = data.slice(0, 32) const v = data.slice(32, 64) + const vBN = new BN(v) + + // Guard against util's `ecrecover`: without providing chainId this will return + // a signature in most of the cases in the cases that `v=0` or `v=1` + // However, this should throw, only 27 and 28 is allowed as input + if (!vBN.eqn(27) && !vBN.eqn(28)) { + return { + gasUsed, + returnValue: Buffer.alloc(0), + } + } + const r = data.slice(64, 96) const s = data.slice(96, 128)