how to generate bitcoin adddres using seed?

0

Hi i am new to bitcoin all i want to know is how to generate address using mnemonic seed?

let testnet =Bitcoin.networks.testnet;
let keypair =Bitcoin.ECPair.makeRandom({network:testnet});
let address=keypair.getAddress();
let privateKey=keypair.toWIF();
res.json("Public Address  "+ address+ "  "+privateKey)

syeda Roushan Arshid

Posted 2019-03-28T04:38:23.310

Reputation: 1

Answers

0

skaht

Posted 2019-03-28T04:38:23.310

Reputation: 2 588

0

Using bitcoinjs-lib and bip39. You can install those with npm install bitcoinjs-lib bip39 --save

const Bitcoin = require('bitcoinjs-lib');
const Bip39 = require('bip39');
const Bip32 = require('bip32');

function getAddress (node, network) {
  return Bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address
}

const mnemonic = `entire taste skull already invest view turtle surge razor key next \
buffalo venue canoe sheriff winner wash ten subject hamster scrap unit shield garden`;

const seed = Bip39.mnemonicToSeed(mnemonic);

const root = Bip32.fromSeed(seed, Bitcoin.networks.bitcoin);

const child1 = root.derivePath("m/44'/0'/0'/0/0");
const child2 = root.deriveHardened(44).deriveHardened(0).deriveHardened(0).derive(0).derive(0);

console.log(getAddress(child1)); //1ENQm8nEP7sd6dqXbAMYZ4AuqcP8Y7AtR
console.log(getAddress(child2)); //1Hb6Z1uZ1RuZ6GXTvedQ2ETYKYsMc5qynN

You can check the code is working using this website. You might also want to read up a little about BIP32, BIP39 and BIP44. An executive summary below:

  • The BIP32 describes how you can generate new child keys programmatically from a single key and a few other properties.

  • BIP39 defines a set of standard words used to mimic a key and how to transform them back to the original "seed" (the key from BIP32).

  • BIP44 specifies how you should compute the child keys from the parent one. You can see from my code that I used derivePath("m/44'/0'/0'/0/0"); in conformance of its specification.

Tiago Loriato Simões

Posted 2019-03-28T04:38:23.310

Reputation: 116