From bc89e86fca7004d2dfc1b73c7abdd9e9e5a195a5 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Sat, 3 Aug 2024 15:49:20 +0200 Subject: [PATCH 1/4] evm: export EOFContainer / validateEOF --- packages/evm/src/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/evm/src/index.ts b/packages/evm/src/index.ts index dc79d5c626..94ab69aff6 100644 --- a/packages/evm/src/index.ts +++ b/packages/evm/src/index.ts @@ -1,3 +1,4 @@ +import { EOFContainer, validateEOF } from './eof/container.js' import { EVM } from './evm.js' import { ERROR as EVMErrorMessage, EvmError } from './exceptions.js' import { Message } from './message.js' @@ -36,6 +37,7 @@ export type { } export { + EOFContainer, EVM, EvmError, EVMErrorMessage, @@ -44,6 +46,7 @@ export { MCLBLS, Message, NobleBLS, + validateEOF, } export * from './constructors.js' From e74c1ab5b94c0959594d896d69bab9cb35baa8fc Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Sat, 3 Aug 2024 15:49:31 +0200 Subject: [PATCH 2/4] evm: add eof container validator script --- packages/evm/scripts/eofContainerValidator.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 packages/evm/scripts/eofContainerValidator.ts diff --git a/packages/evm/scripts/eofContainerValidator.ts b/packages/evm/scripts/eofContainerValidator.ts new file mode 100644 index 0000000000..cacdff97d1 --- /dev/null +++ b/packages/evm/scripts/eofContainerValidator.ts @@ -0,0 +1,40 @@ +import { Common, Hardfork, Mainnet } from '@ethereumjs/common' +import { unprefixedHexToBytes } from '@ethereumjs/util' +import * as readline from 'readline' + +import { createEVM, validateEOF } from '../src/index.js' + +/** + * This script reads hex strings (either prefixed or non-prefixed with 0x) from stdin + * It tries to validate the EOF container, if it is valid, it will print "OK" + * If there is a validation error, it will print "err: " + * If the input is emtpy, the program will exit + */ + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}) + +const common = new Common({ chain: Mainnet }) +common.setHardfork(Hardfork.Prague) +common.setEIPs([663, 3540, 3670, 4200, 4750, 5450, 6206, 7069, 7480, 7620, 7692, 7698]) +const evm = await createEVM({ common }) + +rl.on('line', async (line) => { + if (line.length === 0) { + rl.close() + return + } + let trimmed = line + if (line.startsWith('0x')) { + trimmed = line.slice(2) + } + const bytes = unprefixedHexToBytes(trimmed) + try { + validateEOF(bytes, evm) + console.log('OK') + } catch (e: any) { + console.log('err: ' + e.message) + } +}) From d647a0fc4d864bd5b1a0772e62adb2d2d5c72361 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Sat, 3 Aug 2024 18:14:34 +0200 Subject: [PATCH 3/4] validator: terminal: false --- packages/evm/scripts/eofContainerValidator.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/evm/scripts/eofContainerValidator.ts b/packages/evm/scripts/eofContainerValidator.ts index cacdff97d1..aea8428271 100644 --- a/packages/evm/scripts/eofContainerValidator.ts +++ b/packages/evm/scripts/eofContainerValidator.ts @@ -14,6 +14,7 @@ import { createEVM, validateEOF } from '../src/index.js' const rl = readline.createInterface({ input: process.stdin, output: process.stdout, + terminal: false, }) const common = new Common({ chain: Mainnet }) From 1973edfa633fffe893441fd740f1ea48f3c785e3 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Sat, 3 Aug 2024 18:56:41 +0200 Subject: [PATCH 4/4] evm: read from pipe (?) --- package-lock.json | 15 ++++++++++++++- packages/evm/package.json | 5 +++-- packages/evm/scripts/eofContainerValidator.ts | 17 ++++++----------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index efe96a9f05..2cc6aea911 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14309,6 +14309,18 @@ "semver": "bin/semver.js" } }, + "node_modules/split": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "dev": true, + "dependencies": { + "through": "2" + }, + "engines": { + "node": "*" + } + }, "node_modules/split2": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz", @@ -16842,7 +16854,8 @@ "minimist": "^1.2.5", "node-dir": "^0.1.17", "rollup-plugin-visualizer": "^5.12.0", - "solc": "^0.8.1" + "solc": "^0.8.1", + "split": "^1.0.1" }, "engines": { "node": ">=18" diff --git a/packages/evm/package.json b/packages/evm/package.json index bdd1015585..b136324d2d 100644 --- a/packages/evm/package.json +++ b/packages/evm/package.json @@ -60,10 +60,10 @@ "@ethereumjs/statemanager": "^2.3.0", "@ethereumjs/tx": "^5.3.0", "@ethereumjs/util": "^9.0.3", + "@noble/curves": "^1.4.2", "@types/debug": "^4.1.9", "debug": "^4.3.3", "ethereum-cryptography": "^2.2.1", - "@noble/curves": "^1.4.2", "rustbn-wasm": "^0.4.0" }, "devDependencies": { @@ -81,7 +81,8 @@ "minimist": "^1.2.5", "node-dir": "^0.1.17", "rollup-plugin-visualizer": "^5.12.0", - "solc": "^0.8.1" + "solc": "^0.8.1", + "split": "^1.0.1" }, "engines": { "node": ">=18" diff --git a/packages/evm/scripts/eofContainerValidator.ts b/packages/evm/scripts/eofContainerValidator.ts index aea8428271..a1265db084 100644 --- a/packages/evm/scripts/eofContainerValidator.ts +++ b/packages/evm/scripts/eofContainerValidator.ts @@ -1,6 +1,6 @@ import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import { unprefixedHexToBytes } from '@ethereumjs/util' -import * as readline from 'readline' +import split from 'split' import { createEVM, validateEOF } from '../src/index.js' @@ -11,21 +11,14 @@ import { createEVM, validateEOF } from '../src/index.js' * If the input is emtpy, the program will exit */ -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, - terminal: false, -}) - const common = new Common({ chain: Mainnet }) common.setHardfork(Hardfork.Prague) common.setEIPs([663, 3540, 3670, 4200, 4750, 5450, 6206, 7069, 7480, 7620, 7692, 7698]) const evm = await createEVM({ common }) -rl.on('line', async (line) => { +function processLine(line) { if (line.length === 0) { - rl.close() - return + process.exit() } let trimmed = line if (line.startsWith('0x')) { @@ -38,4 +31,6 @@ rl.on('line', async (line) => { } catch (e: any) { console.log('err: ' + e.message) } -}) +} + +process.stdin.pipe(split()).on('data', processLine)