Skip to content

Instantly share code, notes, and snippets.

@seoh
Created April 15, 2019 11:30
Show Gist options
  • Save seoh/5b7d117a2ce36b03d896ea484d1f17e1 to your computer and use it in GitHub Desktop.
Save seoh/5b7d117a2ce36b03d896ea484d1f17e1 to your computer and use it in GitHub Desktop.
address from pkey
// from https://etherworld.co/2017/11/17/understanding-the-concept-of-private-key-public-key-and-address-in-ethereum-blockchain/
const EC = require('elliptic').ec
const BN = require('bn.js')
const ec = new EC('secp256k1')
const keccak256 = require('js-sha3').keccak256
const G = ec.g
const privateKey = process.argv[2]
const pk = new BN(privateKey)
const pub = G.mul(pk)
const x = pub.getX().toBuffer()
const y = pub.getY().toBuffer()
const key = Buffer.concat([x, y])
console.log(key.toString('hex'))
const address = keccak256(key)
const buf = Buffer.from(address, 'hex')
console.log('0x' + buf.slice(-20).toString('hex'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment