0

I am trying to set up a private blockchain with geth, using truffle to deploy a smart contract to it. My goal is to get a private blockchain running with several accounts, deploy some contracts to it and connect some peers. It is supposed to be learning exercise.

I am running:

  • geth 1.9.15
  • truffle 5.1.30
  • Ubuntu 18.04

Here are the steps I took to set up the private blockchain:

  1. Create 5 accounts: geth account new --datadir private-chain --password pw.txt
  2. Initialize blockchain: geth --datadir private-chain init private-chain/genesis-block.json
  3. Start the console: geth --datadir private-chain --http --http.addr 127.0.0.1 --http.port 8545 --http.api eth,net,web3,personal --networkid 3456 console 2> private-chain/private-chain.log

My genesis-block.json file funded the new accounts with 120 ethers each.

When I try to deploy the contract with truffle, this is what I get:

Starting migrations...
======================
> Network name:    'geth'
> Network id:      3456
> Block gas limit: 2000000 (0x1e8480)


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------

Error:  *** Deployment Failed ***

"Migrations" -- Returned error: authentication needed: password or unlock.

So I tried to unlock the account by changing 1_initial_migration.js to the following:

const web3_lib = require ("web3");
const Migrations = artifacts.require ("Migrations.sol");

module.exports = function (deployer, network, addresses)
{
    const web3 = new web3_lib (new web3_lib.providers.HttpProvider ('http://127.0.0.1:8545'));
    console.log ('>>>>> Attempting to unlock account');
    console.log ('>>>>> Using password ' + process.env.GETH_PW);
    web3.eth.personal.unlockAccount ('0x1234567890abcdef1234567890abcdef12345678', 'my-pw', 36000).then (console.log ('Account unlocked!'));

    console.log ('>>>>> Deploying migration');
    deployer.deploy (Migrations);
};

The "Account unlocked!" message was printed to the screen, but I got the same error as before.

Then I went to the geth console and tried to unlock the account from there and this was the result:

> web3.personal.unlockAccount ("0x1234567890abcdef1234567890abcdef12345678", "my-pw", 36000)
GoError: Error: account unlock with HTTP access is forbidden at web3.js:6347:37(47)
    at native
    at <eval>:1:91(6)

Is this even possible? Is there something I am forgetting, or not understanding about how this is supposed to work? What would be the correct way to do what I am trying to do?

  • Sign the transaction and stop trying to unlock account that is the wrong way. – Majd TL Jun 19 at 7:48
0

As said by MajdTL unlocking an account is not a good idea from a security perpective.

Usually the approach is to create a mnemonic account and use @truffle/hdwallet-provider to sign transactions without having to unlock it.

If you are just practicing the you will have to launch geth with an extra parameter --allow-insecure-unlock to indicate that you will use the unlock function. To get more geth's parameters look at their documentation.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.