From 8a8213894b81bc318471f273d4d10ff45c1b0823 Mon Sep 17 00:00:00 2001 From: Alejandro Date: Fri, 15 Mar 2024 11:08:26 -0300 Subject: [PATCH] Progress with validation on challenges package --- packages/ethernaut-ai/src/tasks/config.js | 2 +- packages/ethernaut-ai/src/tasks/interpret.js | 2 +- packages/ethernaut-challenges/src/tasks/check.js | 4 ++-- packages/ethernaut-challenges/src/tasks/info.js | 4 ++-- packages/ethernaut-challenges/src/tasks/instance.js | 4 ++-- packages/ethernaut-challenges/src/tasks/submit.js | 4 ++-- packages/ethernaut-common/src/type-address.js | 4 ++++ packages/ethernaut-common/src/type-ens.js | 4 ++++ packages/ethernaut-common/src/type-int.js | 10 ++++++++-- packages/ethernaut-common/src/type-string.js | 4 ++++ 10 files changed, 30 insertions(+), 12 deletions(-) diff --git a/packages/ethernaut-ai/src/tasks/config.js b/packages/ethernaut-ai/src/tasks/config.js index 10f863cb..41d53b72 100644 --- a/packages/ethernaut-ai/src/tasks/config.js +++ b/packages/ethernaut-ai/src/tasks/config.js @@ -1,4 +1,4 @@ -const { types } = require('hardhat/config') +const types = require('ethernaut-common/src/types') const output = require('ethernaut-common/src/output') const storage = require('ethernaut-common/src/storage') diff --git a/packages/ethernaut-ai/src/tasks/interpret.js b/packages/ethernaut-ai/src/tasks/interpret.js index 5f3b4c94..9deb0b68 100644 --- a/packages/ethernaut-ai/src/tasks/interpret.js +++ b/packages/ethernaut-ai/src/tasks/interpret.js @@ -1,4 +1,4 @@ -const { types } = require('hardhat/config') +const types = require('ethernaut-common/src/types') const Interpreter = require('../internal/assistants/Interpreter') const Explainer = require('../internal/assistants/Explainer') const Thread = require('../internal/threads/Thread') diff --git a/packages/ethernaut-challenges/src/tasks/check.js b/packages/ethernaut-challenges/src/tasks/check.js index e4969960..9d9d1811 100644 --- a/packages/ethernaut-challenges/src/tasks/check.js +++ b/packages/ethernaut-challenges/src/tasks/check.js @@ -1,4 +1,4 @@ -const { types } = require('hardhat/config') +const types = require('ethernaut-common/src/types') const helper = require('../internal/helper') const output = require('ethernaut-common/src/output') const findLevelCompletedEvents = require('../internal/level-completed-logs') @@ -9,7 +9,7 @@ require('../scopes/challenges') 'check', 'Checks if the player has completed the specified level by submitting an instance modified as per the levels requirements', ) - .addPositionalParam('level', 'The level number', undefined, types.string) + .addPositionalParam('level', 'The level number', undefined, types.int) .setAction(async ({ level }, hre) => { try { const completed = await checkLevel(level, hre) diff --git a/packages/ethernaut-challenges/src/tasks/info.js b/packages/ethernaut-challenges/src/tasks/info.js index f15c1e40..315e0c07 100644 --- a/packages/ethernaut-challenges/src/tasks/info.js +++ b/packages/ethernaut-challenges/src/tasks/info.js @@ -1,4 +1,4 @@ -const { types } = require('hardhat/config') +const types = require('ethernaut-common/src/types') const helper = require('../internal/helper') const fs = require('fs') const path = require('path') @@ -11,7 +11,7 @@ require('../scopes/challenges') 'info', 'Shows information about an open zeppelin challenges level. The info includes the level name, contract name, ABI path, address, and description. The ABI path can be used with the interact package call task to interact with the contract.', ) - .addPositionalParam('level', 'The level number', undefined, types.string) + .addPositionalParam('level', 'The level number', undefined, types.int) .setAction(async ({ level }) => { try { const info = await getLevelInfo(level) diff --git a/packages/ethernaut-challenges/src/tasks/instance.js b/packages/ethernaut-challenges/src/tasks/instance.js index 292f7bcb..dcc658e2 100644 --- a/packages/ethernaut-challenges/src/tasks/instance.js +++ b/packages/ethernaut-challenges/src/tasks/instance.js @@ -1,4 +1,4 @@ -const { types } = require('hardhat/config') +const types = require('ethernaut-common/src/types') const helper = require('../internal/helper') const output = require('ethernaut-common/src/output') const { getNetworkName } = require('ethernaut-common/src/network') @@ -11,7 +11,7 @@ require('../scopes/challenges') 'instance', 'Creates an instance of a level, so that it can be played. The address of the instance is printed to the console. Use this address to interact with the contract using the ethernaut-cli contract command. Make sure to use the info command to get instructions on how to complete the level.', ) - .addPositionalParam('level', 'The level number', undefined, types.string) + .addPositionalParam('level', 'The level number', undefined, types.int) .setAction(async ({ level }, hre) => { try { const instanceAddress = await createInstance(level, hre) diff --git a/packages/ethernaut-challenges/src/tasks/submit.js b/packages/ethernaut-challenges/src/tasks/submit.js index b7ea1f54..c093ce95 100644 --- a/packages/ethernaut-challenges/src/tasks/submit.js +++ b/packages/ethernaut-challenges/src/tasks/submit.js @@ -1,4 +1,4 @@ -const { types } = require('hardhat/config') +const types = require('ethernaut-common/src/types') const output = require('ethernaut-common/src/output') const debug = require('ethernaut-common/src/debug') const getEthernautContract = require('../internal/ethernaut-contract') @@ -12,7 +12,7 @@ require('../scopes/challenges') 'address', 'The address of the instance to submit', undefined, - types.string, + types.address, ) .setAction(async ({ address }, hre) => { try { diff --git a/packages/ethernaut-common/src/type-address.js b/packages/ethernaut-common/src/type-address.js index 37975793..d22440dd 100644 --- a/packages/ethernaut-common/src/type-address.js +++ b/packages/ethernaut-common/src/type-address.js @@ -16,6 +16,10 @@ module.exports = { `"${argValue}" is not an address`, `Invalid ${argName}`, ) + + if (typeof describe === 'function') { + throw err + } } }, } diff --git a/packages/ethernaut-common/src/type-ens.js b/packages/ethernaut-common/src/type-ens.js index 40aca330..43f3b181 100644 --- a/packages/ethernaut-common/src/type-ens.js +++ b/packages/ethernaut-common/src/type-ens.js @@ -14,6 +14,10 @@ module.exports = { } } catch (err) { output.errorBoxStr(`"${argValue}" is not an ens`, `Invalid ${argName}`) + + if (typeof describe === 'function') { + throw err + } } }, } diff --git a/packages/ethernaut-common/src/type-int.js b/packages/ethernaut-common/src/type-int.js index c6ffe9d5..db66d99a 100644 --- a/packages/ethernaut-common/src/type-int.js +++ b/packages/ethernaut-common/src/type-int.js @@ -3,12 +3,18 @@ const output = require('./output') module.exports = { name: 'int', - parse: types.int.parse, + parse: function (argName, argValue) { + return argValue + }, validate: (argName, argValue) => { try { types.int.validate(argName, argValue) } catch (err) { - output.errorBoxStr(`"${argValue}" is an int`, `Invalid ${argName}`) + output.errorBoxStr(`"${argValue}" is not an int`, `Invalid ${argName}`) + + if (typeof describe === 'function') { + throw err + } } }, } diff --git a/packages/ethernaut-common/src/type-string.js b/packages/ethernaut-common/src/type-string.js index cd720236..f3c674b8 100644 --- a/packages/ethernaut-common/src/type-string.js +++ b/packages/ethernaut-common/src/type-string.js @@ -9,6 +9,10 @@ module.exports = { types.string.validate(argName, argValue) } catch (err) { output.errorBoxStr(`"${argValue}" is not string`, `Invalid ${argName}`) + + if (typeof describe === 'function') { + throw err + } } }, }