Interacting with Ethernaut through node.js in VS Code

I recently completed the Ethernaut challenges throught Level 10 using the browser developer console and Remix. Everything went smoothly with that, and since I'm currently learning ethers.js, I decided to see if I could interact with Ethernaut using node.js and VS Code.

But here's where I think I'm running into a disconnect. If my thinking is correct, in order to interact with Ethernaut, I have to have my MetaMask connected. And since MetaMask is a browser extension, node.js isn't going to work for this if I'm using it solely as a back-end component.

So my question is, if I don't write a page to run in the browser to host my script (basically a minimal dApp?), is there any way to connect to MetaMask and interact with the Ethernaut challenges strictly through node.js on the back end?

you could definitely do everything in the backend. Everytime metamask pops up its just asking you to accept a transaction with specific data to a certain contract. you could perform all the necessary transactions with ethers.js as well.

The webinterface just helps you avoid some specific tasks such as bookkeeping. For example when you click on "get new instance" at the beginning of a level, the main ethernauts contract will create a new contract that you need to hack and will emit an event that contains the address. The webinterface will extract the address and provide you the object in the console. In your backend-only you would need to search for the specific event yourself to get the address and connect to the contract.

1 Like

When I use the boilerplate code that ethers.js shows for connecting to MetaMask, it throws an error saying that "window is not defined", which is to be expected without a browser instance:

Do you know what I can use here for the provider instead of window.ethereum?

"window" is an object that is only present when you load the js code in a browser tab.

you need to connect to rinkeby directly by doing something like this:

  const provider = new ethers.providers.JsonRpcProvider(url)

If I go that route, I've got a connection to Rinkeby, but then I bypass MetaMask completely, right? I think that for Ethernaut to recognize "me" I have to have my MetaMask connected to it. This is my dilemma.

I could always just throw together a simple html page and load my ethers.js calls inside of that to avoid this, but was hoping to find a way to just access Ethernaut directly through node.js on the back-end.

not sure what you mean with "recognize"? you can definitely interact with the Ethernaut contracts without the webinterface

If I go to the Ethernaut site when I'm not connected to MetaMask, it treats me like I'm a new participant and takes me to the start page of the challenges. Even if I've already completed 10 challenges, it doesn't "see" my completed instances. As soon as I connect to MetaMask, it shows all my current progress and gives me access to all the level instances I've created. (Edit: I just tested it again without being connected to MetaMask and it won't even let me create any level instances unless I'm connected to MetaMask)

Maybe I'm missing something really simple, this is all a bit of a paradigm shift for me from the traditional software development I'm used to. Definitely a learning process!

ah now I understood what you are trying to do. You were just trying to replace metamask with ethers.js while still using the ethernaut interface. yea no, that won't work :sweat_smile:

I was assuming you wanted to play the game purely from the backend, which you could with ethers.js

So it's possible from the back end to have Ethernaut generate the new level instances for me? Or for things like that I'll still need to visit the UI, then I can jump into the back end to do the rest?

Thanks for your replies, by the way!

So it's possible from the back end to have Ethernaut generate the new level instances for me?

yes, all that happens when you press on the blue button and accept on metamask is that a transaction is send to a "main contract" that generates a new contract. You can all do this with ethers.js.

Since you are coming from a software dev background, imagine it like this:
blockchain = server
contract = API for the server
Ethernaut + Metamask = GUI that makes calls to the API and presents the data returned in a user friendly way
ethers.js = script, that makes calls to the API, can be used instead of a GUI for the more tech savy people

Ahhh...yes! So there's nothing preventing me from interacting directly with the main contract in exactly the same way the UI is doing it when I click the blue "Get new instance" button. Starting to see it now!