MENU

Connecting Geth to MetaMask and Mist

February 8, 2018
0
7415
0

If you have geth, the Go Ethereum client running and you want to connect Mist or MetaMask to you get a nice front end, here is how you do it.

Start geth with:

geth --datadir ./chaindata init ./myGenesisFile.json

Note: If you don’t have a genesis file, one is provided at the end.

The run

geth --datadir ./chaindata --networkid 1994

to spin up the private Ethereum blockchain

Mist

The easiest way to connect Mist to this private blockchain is to start it via command line and passing in the location of the ipc file with the rpc flag.

> ./Mist --rpc /Users/cloudnthings/privatechain/chaindata/geth.ipc

You should get the word PRIVATE-NET at the top. Click Launch Application and you should be away.

If you get this error:

NCAUGHT EXCEPTION { Error: connect ENOENT Users/cloudnthings/privatechain/chaindata/geth.ipc

Then it means you have your syntax wrong. The most common mistake is not having a double dash before rpc (- – rpc)

MetaMask

To connect to MetaMask, you need to start the private Ethereum blockchain with an extra rpc flag.

geth --datadir ./chaindata --networkid 1994 --rpc

You can connect to the blockchain as per normal via geth attach like so:

geth attach http://127.0.0.1:8545

or in MetaMask, simple choose localhost

The balance will be zero so be sure to import an account

and find the JSON keystore file which you can locate in the folder where your private chain data is sitting.

You should then be able to run

web3.eth.accounts

to confirm the public address is the same and then

> eth.getBalance(web3.eth.accounts[0])

where the 2 balances should match.

A neat extension to this is to try mining and seeing the balance increase. Unfortunately Web3 does not expose the miner object via rpc so you’ll have to connect via ipc like so:

geth attach ipc:/Users/cloudnthings/privatechain/chaindata/geth.ipc

and then run miner.start() and miner.stop(). If you watch MetaMask you’ll see the balance jump up in real time!

Genesis file sample

{
   "config": {
      "chainId": 1994,
      "homesteadBlock": 0,
      "eip155Block": 0,
      "eip158Block": 0,
      "byzantiumBlock": 0
   },
   "difficulty": "400",
   "gasLimit": "2000000",
   "alloc": {
      "7b684d27167d208c66584ece7f09d8bc8f86ffff": { 
          "balance": "100000000000000000000000" 
      }
   }
}

Leave a Comment!

Your email address will not be published. Required fields are marked *