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?

4
  • 2
    Does this answer your question? How are ethereum addresses generated?
    – Ismael
    Apr 30, 2020 at 17:20
  • @Ismael It does not answer. This question is specifically about easily callable APIs. I am not interested in math behind it. Apr 30, 2020 at 19:36
  • @MikkoOhtamaa that is not clear from your question, if you are looking for easily callable APIs (e.g. web3.js / ethers.js) then your question should state that.
    – SCBuergel
    May 8, 2020 at 9:09
  • @SCBuergel.eth Thank you. I edited the question. May 8, 2020 at 9:36

2 Answers 2

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;
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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