Skip to content

Instantly share code, notes, and snippets.

@tgoldenberg
Created April 10, 2018 20:34
Show Gist options
  • Save tgoldenberg/47e954b94e768e58c25718c018b5cc2b to your computer and use it in GitHub Desktop.
Save tgoldenberg/47e954b94e768e58c25718c018b5cc2b to your computer and use it in GitHub Desktop.
ECC Addresses
const secureRandom = require('secure-random');
const ec = require('elliptic').ec;
const ecdsa = new ec('secp256k1');
function generateAddress() {
const max = Buffer.from("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140", 'hex');
let isInvalid = true;
let privateKey;
while (isInvalid) {
privateKey = secureRandom.randomBuffer(32);
if (Buffer.compare(max, privateKey) === 1) {
isInvalid = false;
}
}
console.log('> Private key: ', privateKey.toString('hex'));
const keys = ecdsa.keyFromPrivate(privateKey);
const publicKey = keys.getPublic('hex');
console.log('> Public key: ', publicKey);
return {
privateKey: privateKey.toString('hex'),
publicKey,
};
}
generateAddress();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment