bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.62k stars 2.09k forks source link

What should i do for getting privatekey? #1249

Closed ishwarchandratiwari closed 5 years ago

ishwarchandratiwari commented 5 years ago

I have generated address see the below code..

'use strict'

var bip39 = require('bip39');
var bip32 = require('bip32');
var bitcoin = require('bitcoinjs-lib');

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

var mnemonic = bip39.generateMnemonic();
if (bip39.validateMnemonic(mnemonic)) {
    console.log('\nThe mnemonic is \n' + mnemonic);
    var seed = bip39.mnemonicToSeed(mnemonic);
    var root = bip32.fromSeed(seed);
    var dp = root.derivePath("m/140'/0'/0'/0/5");
    console.log(dp.privateKey);  // I used dp.privateKey but this not returning privatekey
    console.log('\nThe Address is \n' + getAddress(dp));    
} else {

}
Output:

$ node index.js

The mnemonic is
assume victory kiwi swarm furnace firm thumb exact dizzy crucial neglect certain

The Address is
1FYtPnUZZ2ZJL2fZ6XehGtkHfujUHkqK3T

Help me to get privatekey for this address 1FYtPnUZZ2ZJL2fZ6XehGtkHfujUHkqK3T tell me what should I do for getting privatekey for the above address I have generated I used dp.privateKey but this not returning privatekey

junderw commented 5 years ago

It works for me. I just copy pasted your code and it gives me the private key.

What version?

What environment?

junderw commented 5 years ago

By the way, your private key for 1FYtPnUZZ2ZJL2fZ6XehGtkHfujUHkqK3T is below.

Buffer.from('4dc8579ae9cb65a8afe146325007caec44a39997f248f9c9579d90937e783737', 'hex')

ishwarchandratiwari commented 5 years ago

yeah thanks its working