From 1d90668845f229948ab27a863cc3f5acd55b1bca Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Wed, 17 Jan 2024 21:48:04 -0500 Subject: [PATCH] Add ecrecover function --- packages/evm/test/customCrypto.spec.ts | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/evm/test/customCrypto.spec.ts b/packages/evm/test/customCrypto.spec.ts index b1c2632272..815331039f 100644 --- a/packages/evm/test/customCrypto.spec.ts +++ b/packages/evm/test/customCrypto.spec.ts @@ -1,4 +1,12 @@ import { Chain, Common } from '@ethereumjs/common' +import { + bytesToHex, + concatBytes, + hexToBytes, + intToBytes, + randomBytes, + setLengthLeft, +} from '@ethereumjs/util' import { assert, describe, it } from 'vitest' import { EVM } from '../src/evm.js' @@ -26,4 +34,31 @@ describe('custom crypto', () => { }) assert.equal(result.returnValue[0], 0xff, 'used custom sha256 hashing function') }) + + it('should use custom ecrecover function', async () => { + const customEcrecover = (_msg: Uint8Array) => { + return hexToBytes( + '0x84b2586da9b582d3cb260e8fd136129c734f3c80453f48a68e8217ea0b81e08342520f318d202f27a548ad8d3f814ca76d0ee621de2cc510c29e2db4d4f39418' + ) + } + const customCrypto = { + ecrecover: customEcrecover, + } + const msg = concatBytes(randomBytes(32), setLengthLeft(intToBytes(27), 32), randomBytes(32)) + const common = new Common({ chain: Chain.Mainnet, customCrypto }) + const evm = new EVM({ common }) + const addressStr = '0000000000000000000000000000000000000001' + const ECRECOVER = getActivePrecompiles(common).get(addressStr)! + const result = await ECRECOVER({ + data: msg, + gasLimit: 0xfffffn, + common, + _EVM: evm, + }) + assert.equal( + bytesToHex(result.returnValue), + '0x00000000000000000000000063304c5c6884567b84b18f5bc5774d829a32d25d', + 'used custom ecrecover hashing function' + ) + }) })