C 342: MetaMask with Local Testnet (10 pts.)

What You Need


To connect the MetaMask wallet to your local Ethereum testnet.

Verifying that geth is Running

On your Linux machine, with geth running, execute this command:
ss -pant
You should see geth listening on port 8545, as shown below.

If you do not see geth listening, refer to project C 333 to see how to start it.

Adding a Socat Forwarder

geth is listening on the localhost address, which would work if you are running Chrome and MetaMask on the same machine. But I am using a headless Debian server and running Chrome on the host MacOS. So I need to forward that port to an externally visible IP address.

On your Linux machine, with geth running, execute these commands:

sudo apt update
sudo apt install socat -y
socat TCP-LISTEN:18545,fork TCP: &
ss -pant
You should see socat listening on, as shown below.

Finding the Linux Server's IP Address

On your Linux machine,execute this command:
ip a
Find your IP address and make a note of it.

Finding your Chain ID

On your Linux machine,execute this command:
curl  -H "Content-Type: application/json" -X POST \
--data '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' \
Find your chainID. In the image below, it is 0xa in hexadecimal, or 10.

Connecting with MetaMask

On your host machine, launch Chrome and launch MetaMask.

At the top center, click "Ropsten Test Network" and click "Custom RPC", as shown below.

Enter these values, as shown below, replacing the IP address with the address of your Linux server:

Click Save.

Close the Settings box.

Finding your Private Key JSON File

On your Linux server, execute these commands:
ls .ethereum/keystore
You see the name of your private key file, as shown below.

On your Linux server, use cat to print out that file, as shown below.

Higlight the JSON code, as shown below, and copy it.

On your host machine, create a text file, paste the JSON into it, and save it.

Importing your Account

On your host machine, in Chrome, if the MetaMask page is not maximized, at the top right, click the three-dot icon and click "Expand view". MetaMask doesn't work properly unless it is filling a Chrome tab.

A the top right of the MetaMask page, the network name is now local, and your balance is 0 ETH, because the account in your wallet is not the account you used to mine on your local testnet blockchain.

At the top right. click the circular icon and click "Import Account".

Select a Type of "JSON File". Click the "Choose File" button and open the file you created.

Enter the password you specified when creating the account in project C 333, as shown below.

Click the Import button.

You see a frozen page, as shown below.

The import is working, but it is incredibly slow, due to a known bug in MetaMask.

Wait for it to finish. It may take up to 20 minutes.

When it finishes, your account will connect, and you will have a very large amount of ETH, as shown below.

Using your Private Key String

On your Linux machine, execute these commands:
sudo apt install python3-pip
python3 -m pip install web3
ls .ethereum/keystore
You see the name of your keystore, which begins with "UTC", as shown below.

Copy this name and save it.

On your Linux machine, execute these commands, replacing the filename with your correct keystore name, and replacing the password with the correct password for your account. After the last line, press Enter twice.

from web3 import Web3
w3 = Web3(Web3.HTTPProvider(''))
with open('.ethereum/keystore/UTC--2021-11-07T05-59-31.082486227Z--72fae59f701386bc27bdd2855b71e2f246bef2ba') as keyfile:
    encrypted_key = keyfile.read()
    private_key = w3.eth.account.decrypt(encrypted_key, 'P@ssw0rd')
Your private key appears, as shown below.

Copy that private key, excluding the prefix "0x". Paste it in to Metamask, as shown below, to import your account.

Sending Ether to Yourself

At the top center, click your Account number.

A context box pops up saying Copied.

In the center of your screen, click the blue Send button.

Paste in the address, and enter an Amount of "1000 ETH", as shown below.

Click Next. Click Confirm.

Wait a few seconds, while the transaction finishes.

In the lower portion of the window, click the Activity tab, as shown below.

C 342.1: Activity Log (10 pts)

In the lower left of the window, click Send.

The flag is covered by a green rectangle in the image below.


Connecting MyEtherWallet, Mist, and MetaMask to Your Private Blockchain

Posted 5-14-2021
Private key import method added 11-11-21