|
//npm install -S bn.js elliptic js-sha3 |
|
|
|
var EC = require('elliptic').ec; |
|
var BN = require('bn.js'); |
|
var ec = new EC('secp256k1'); |
|
const keccak256 = require('js-sha3').keccak256; |
|
|
|
|
|
// console.log("PK::"+pk); |
|
pk = '00000000000000000000000000000001'; |
|
pk = '0000000000000000000000000000000000000000000000000000000000000001'; |
|
pk = '1'; |
|
pk = new BN('1'); |
|
|
|
var privateKey=Buffer.alloc(32, 0); |
|
privateKey[31]=1; |
|
pk = privateKey.toString('hex'); |
|
|
|
//example with private key generated with openssl lib |
|
//openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout > Key |
|
//cat Key | grep priv -A 3 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^00//' > priv |
|
//(priv = '868f70c2c5760aaffd559157c792816e3ef10fc8610a567abbba6c4a9bc7f1c6') |
|
pk = '868f70c2c5760aaffd559157c792816e3ef10fc8610a567abbba6c4a9bc7f1c6'; |
|
|
|
var G = ec.g; // Generator point (fixed) |
|
//<EC Point x: 79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 y: 483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8> |
|
|
|
var pubPoint = G.mul(pk); // EC multiplication to determine public point |
|
|
|
var x = pubPoint.getX().toBuffer(); //32 bit x co-ordinate of public point |
|
var y = pubPoint.getY().toBuffer(); //32 bit y co-ordinate of public point |
|
|
|
var publicKey = Buffer.concat([x,y]); |
|
console.log("pub key::" + publicKey.toString('hex')); |
|
|
|
|
|
const address = keccak256(publicKey); // keccak256 hash of publicKey |
|
|
|
const buf2 = Buffer.from(address, 'hex'); |
|
console.log("Ethereum Adress:::"+"0x"+buf2.slice(-20).toString('hex')); // take last 20 bytes as ethereum adress |
|
|
|
//Understanding the concept of Private Key, Public Key and Address in Ethereum Blockchain |
|
//https://etherworld.co/2017/11/17/understanding-the-concept-of-private-key-public-key-and-address-in-ethereum-blockchain/ |