2

Let's assume one has a private key in a raw hexadecimal format:

# Not a real key
ddc64840388bc5f2bc9d40a29b35ae3c41a8cdf9ee1dfdc5a46414e3db3de2db

How one would get the corresponding Ethereum account from a UNIX command line?

Mikko Ohtamaa
  • 22,269
  • 6
  • 62
  • 127

2 Answers2

2

Option #1:

const util = require("ethereumjs-util");
const address = "0x" + util.privateToAddress(privateKey).toString("hex");

Option #2:

const Web3 = require("web3");
const web3 = new Web3();
const address = web3.eth.accounts.privateKeyToAccount(privateKey).address;
goodvibration
  • 26,003
  • 5
  • 46
  • 86
1

Here is an example how to get it with web3.js and Node REPL.

Assuming you know how to set up a Node environment and install eth-lib package. This package is currently being used as a dependency for web3.js functions.

Open node:

node

Then start the multi-line editor with the command

.editor

And paste in and edit the code:

const privateKey = 'ddc64840388bc5f2bc9d40a29b35ae3c41a8cdf9ee1dfdc5a46414e3db3de2db';
const { Account } = require('eth-lib/lib');
// Note it is very important we add a 0x prefix here,
// as the fromPrivate() accepts anything for the input
// and does very little safety checks for the incoming input
const account = Account.fromPrivate('0x' + privateKey);
console.log("Account for", privateKey, "is", account.address); 

This will output:

Account for ddc64840388bc5f2bc9d40a29b35ae3c41a8cdf9ee1dfdc5a46414e3db3de2db is 0x010335E6E38AeACA85DaeeA22F291f86E64d1DaB
Mikko Ohtamaa
  • 22,269
  • 6
  • 62
  • 127