So, as a real blockchain developer, you want to test your smart contract not only in virtual Ganache (which is in-memory blockchain emulator), but also – with environment which is close to real blockchain. In that case, you going to use tool names Go Ethereum, and, mostly possible, tool called Truffle for writing, testing and deploying your smart contracts.
And, as using Ganache + Truffle is really easy and looks like they come together out of a box, with Truffle + Geth you can have some problems and errors. In this article I will show you how to solve problems that I’ve met.
Environment
I have installed Go Ethereum version 1.10.19-stabl for arm64 + project with Truffle version 5.5.18. Task is to deploy contracts through Truffle to Geth and be able to manipulate them.
Command for running Geth is:
1 2 3 4 |
geth --networkid 4224 --mine --miner.threads 1 --datadir "./private" --nodiscover -port "30303" --nat "any" \ --http --http.api personal,eth,net,web3 \ --ws --ws.api personal,eth,net,web3 \ --password ./private/password.sec --ipcpath "~/Library/Ethereum/geth.ipc" |
As you can see, flag password is specified (security stuff).
In truffle project config file, I have a network for that:
1 2 3 4 5 6 |
geth: { host: "127.0.0.1", port: 8545, network_id: 4224, from: '0x6db7f74b66403832fc2cfa40d6ee79f1b6915e37', } |
From – address of account, made without password through command in Geth private data directory:
1 |
* geth --datadir . account new |
And press inter through the dialog, or you can specify password – it doesn’t matter.
So, if you would try to do typical
1 |
truffle migrate --networg geth |
command for compiling and deploying smart contracts to blockchain, first, you will receive error:
So, how could we authenticate in this case?
You have 2 ways, but first things first – you should add flag –allow-insecure-unlock for Geth run command.
So, in my case final run command is:
1 2 3 4 5 |
geth --networkid 4224 --mine --miner.threads 1 --datadir "./private" --nodiscover -port "30303" --nat "any" \ --http --http.api personal,eth,net,web3 \ --ws --ws.api personal,eth,net,web3 \ --password ./private/password.sec --ipcpath "~/Library/Ethereum/geth.ipc" \ --allow-insecure-unlock |
Modify initial migrations script
Go to migrations/1_initial_migration.js in your truffle project and add code from topic on Stackoverflow (don’t forget to replace your account credentials).
Final code would be like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
const Migrations = artifacts.require("Migrations"); const TruffleConfig = require('../truffle-config'); const Web3 = require('web3'); module.exports = function (deployer, network, address) { const config = TruffleConfig.networks[network]; if (process.env.ACCOUNT_PASSWORD) { const web3 = new Web3(new Web3.providers.HttpProvider(`http://${config.host}:${config.port}`)); web3.eth.personal.getAccounts().then(accounts => { const defaultAccount = accounts[0]; console.log(`[x] Unlocking account ${defaultAccount}`) web3.eth.personal.unlockAccount(defaultAccount, process.env.ACCOUNT_PASSWORD); }) } console.log('[x] Making migrations') deployer.deploy(Migrations); }; |
Use Geth console to unlock password manually
Start Geth in one terminal tab, in another run command
1 |
geth attach |
And after that – find and unlock your account by similar command:
Now, try migration command for truffle again and auth error will disappear:
(I have another error on a screen, but it is completely another story :D)
Now, you can go further, gentleman!