Skip to content

Instantly share code, notes, and snippets.

@brickpop
Created July 6, 2017 00:31
Show Gist options
  • Save brickpop/fed1b8ee98a72a2caa5e8f816048c057 to your computer and use it in GitHub Desktop.
Save brickpop/fed1b8ee98a72a2caa5e8f816048c057 to your computer and use it in GitHub Desktop.
Ethereum utilities
// Generate a private key => address from a passphrase
const passphrase = process.argv[2];
if(!passphrase) {
console.log("Usage: node app 'your-passphrase-here'");
process.exit();
}
const EthUtil = require("ethereumjs-util")
const Web3 = require('web3')
const web3 = new Web3();
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c+=2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
function privateKeyToAddress(privateKey) {
return `0x${EthUtil.privateToAddress(hexToBytes(privateKey)).toString('hex')}`
}
function passphraseToPrivateKey(text){
return web3.sha3(text).substr(2);
}
const privKey = passphraseToPrivateKey(passphrase);
const address = privateKeyToAddress(privKey);
console.log("Address for the given passphrase:", address);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment