Connecting MetaMask with a Private Blockchain
Tags: ethereum, blockchain, metamask, wallet
Published: 2022-03-18 14:25:37

This post is the second in a series of posts that demonstrate how to create your own **Private Blockchain**. The previous post shows the step by step to create the private ethereum blockchain, we will connect to that blockchain using the [MetaMask](https://metamask.io/).

Below you can see the current structure of the private blockchain built:
![Old Blockchain Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qegbbqao4h8ek2thtmzf.png) 

You can read the previous post [here](https://dev.to/jeffersonxavier/create-your-own-private-blockchain-using-ethereum-52o5).

## MetaMask

The MetaMask is a crypto wallet to [Ethereum](https://ethereum.org/). Can be used to start and manage private wallets to users in blockchain network. By default is set to the Ethereum Mainnet, but can be used to any others blockchains based on Ethereum.

## Get Start

Follow the next steps to finish this tutorial:

- Install MetaMask and create an account
- Add **My Blockchain** network
- Import the miner account created using [Geth](https://geth.ethereum.org/)
- Transfer funds between accounts

## Install MetaMask and Create the First Account

The MetaMask can be installed like an extension to most popular browsers or a mobile app. In this post, we will use MetaMask like a extension in [Brave](https://brave.com/). This process is similar for other browsers like Chrome or Firefox.

Go to [download page](https://metamask.io/download/) and click at _"Install Metamask For **BROWSER_NAME**"_. Follow the installation steps until finish.

A similar window should be open at the end of installation:
![End Installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ikpdght00zxhhrk30ii.png)

Click at "Get Started" to initialize MetaMask. In next step you can import or create a new wallet. Let's create a new one.

![Get Started MetaMask](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0iz2du28vodrlwm57s2.png)
 
Define the MetaMask password and keep account recovery words in a safe place. Without your private key or that words it's impossible recover your blockchain account.

After that, you should see the inital MetaMask screen with your account created like this:

![MetaMask Init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kla5re4i7e5wakypqbze.png)
 
The account was created on the main ethereum network as shown in the upper right corner of the screen: _Ethereum Mainnet_.

## Add My Blockchain Network

Let's add our Blockchain Network to MetaMask. Click on _Ethereum Mainnet_ and select _Add New Network_ option.

![Add New Network](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1ijdokg2p7i8s9s2xnv.png)
 
Define values to **My Blockchain** and click o _Save_ button.

- **Network Name**: My Blockchain
- **New RPC URL**: http://127.0.0.1:8545
- **Chain ID**: 1234
- **Currency Symbol**: MYBL

![Network Values](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8k9yt7u6pm0urhvxt81.png)
 
**Explanation**:

- **Network Name**: Any string to reference your blockchain name.
- **New RPC URL**: The HTTP Server URL. In our the HTTP Server is running on _Node1_ in default port **8545**.
- **Chain ID**: The chainId defined in _genesis block_.
- **Currency Symbol**: A symbol to reference the coin in your blockchain. If you not define it, the default value is **ETH**.

**All Done!!** Your blockchain network has been added and you should be see your account created in this network without balance.

![MYBL Initial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2dbt5tpl3q9qk9jidrn.png)
 
## Import Account

In [previous post](https://dev.to/jeffersonxavier/create-your-own-private-blockchain-using-ethereum-52o5) we create an account using Geth and we did the mining of some blocks with that account. Let's import that account to MetaMask to see balance and transfer funds to account created in this post.

Select _"Import Account"_ option in MetaMask, choose _"Select Type"_ as **JSON File**, choose _account json file_ in your blockchain, type the _password_ and click on _"Import"_ button.

![Import Account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hd3tqx0lpvrw56hkcyba.png)

![Account JSON](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hidyvti1rjrnvfvwh8ct.png)

The _account json file_ can be found in database of the _Node1_ in your blockchain, the path to this file contains the account address, something like this:

`node1/keystore/UTC--2022-03-17T20-40-36.929865867Z--bd3156b239e2bb8d073406e67eba59a651be18f0`

**All Done!** The account has been imported to MetaMask. You can see that account in inital page selecting the _Account 2_.

![Account Imported](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujt13ql2gh10btr2srw8.png)
 
## Transfer Funds

Let's transfer some value from _Account 2_ to _Account 1_ to validate the structure and finish this post.

First, go to _Account 1_ in MetaMask and copy the _Account Public Address_ and back to _Account 2_. In my case the public address of the _Account 1_ is:

`0xE4f4206F11C249aeCfE5AD8a4DDcfae60343d746`.

In _Account 2_ click on _"Send"_ button, paste the public address of the _Account 1_ and type the value to transfer. Click on _"Next"_ button and then click on _"Confirm"_ button.

![Transfer Funds](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljj9fmfqccwzwnbzm7uu.png)

The transaction will be with status _"Pending"_, waiting for a block to be mined for validation.

![Pending Transaction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaavw7q9ysmkgqzm0o6l.png)

Now, it's necessary start the mining to validate that transaction. Let's do this in our blockchain using Geth in Javascript Console from _Node1_. Type the command:

`miner.start()`

Waiting some seconds and verify your account in MetaMask again.

**Obs**: If you want, you can stop the mining in _Node1_ after the transaction validation. Type command to stop the mining:

`miner.stop()`

**All Done!**. The transaction was validated and the _Account 1_ received the **100 MYBL**.

![Transaction Done](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dqj4u4ppabo9ukg0o3r.png) 

## Blockchain Structure

![Blockchain Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s15nk2fatxi7efz3afzp.png)

## Next Steps

You can create new accounts to your blockchain using MetaMask or Geth to increment your tests. Do more transactions, using other accounts to execute mining in _Node1_ or _Node2_. That's it.

## References

[Go Ethereum](https://geth.ethereum.org/)
[MetaMask](https://metamask.io/)