Created
September 4, 2021 19:24
-
-
Save andyjsbell/84810e49040b3c1ac1c2e5e51caafe2c to your computer and use it in GitHub Desktop.
ethereum address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BN = require('bn.js'); | |
const EC = require('elliptic').ec; | |
const keccak256 = require('js-sha3').keccak256; | |
const sk = new BN('616E6769652E6A6A706572657A616775696E6167612E6574682E6C696E6B0D0A', 16); | |
const ec = new EC('secp256k1'); // Elliptic curve used by Ethereum | |
const G = ec.g; // Elliptic curve generator point | |
// console.log(G.getX()); | |
const qa = G.mul(sk); // EC multiplication for public point | |
const x = qa.getX().toBuffer(); // 32bit x coord of public point | |
const y = qa.getY().toBuffer(); // 32bit y coord of public point | |
const pk = Buffer.concat([x,y]); // 64 bit concatenation of EC multiplication | |
const kh = keccak256(pk); // keccak256 hash of ECDSA public key | |
const ethereumAddress = Buffer.from(kh, 'hex').slice(-20).toString('hex'); | |
console.log('address is', ethereumAddress); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment