Bitcoinjs-lib: Can't get address from random key nor get private from specific mnemonic

Created on 31 Oct 2018  路  1Comment  路  Source: bitcoinjs/bitcoinjs-lib

Hi there,

It's been a few days since I try to do this but it seems I can't get the 3 infos I want :

  • 1 address
  • 1 public key
  • 1 private key (WIF style)

From a mnemonic, I try to get these informations from a specific derived path, but I can't get the WIF key.
Here is my code :

const seed = bip39.mnemonicToSeed( "muscle skate dawn remember pumpkin foil vacuum such brass grass bullet shoulder" ).toString( 'hex' );

const root = HDKey.fromMasterSeed(new Buffer(seed, 'hex'));

const path = "m/44'/0'/0'/0/0";

const publicKey = root.derive(path).publicKey.toString('hex');

const { address } = bitcoin.payments.p2pkh({ pubkey: root.derive(path).publicKey });

const privKey = root.derive(path).privateKey.toString('hex');

Any help about the WIF syntax from this private key? I've read that I need to use SHA-256 twice then base58 encode but it doesn't seem to work.

And from a random keyPair, I get an undefined address with this code :

const bitcoinNetwork = bitcoin.networks.bitcoin;

const keyPair = bitcoin.ECPair.makeRandom(bitcoinNetwork, new Buffer(seed, 'hex'));

const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey });

const publicKey = keyPair.publicKey.toString('hex');

const privKey = keyPair.toWIF();

Any help would be appreciated, thanks !

how to / question / docs

Most helpful comment

Read the examples please.

For the HD stuff

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

const seed = bip39.mnemonicToSeed( "muscle skate dawn remember pumpkin foil vacuum such brass grass bullet shoulder" ).toString( 'hex' );

const root = HDKey.fromSeed(new Buffer(seed, 'hex'));

const path = "m/44'/0'/0'/0/0";

const derived = root.derivePath(path);

const publicKey = derived.publicKey.toString('hex');

const { address } = bitcoin.payments.p2pkh({ pubkey: derived.publicKey });

const privKey = bitcoin.ECPair.fromPrivateKey(derived.privateKey).toWIF();

For the single key stuff.

const bitcoin = require('bitcoinjs-lib');

// the default is bitcoin... so this is useless though.
const bitcoinNetwork = bitcoin.networks.bitcoin;

const keyPair = bitcoin.ECPair.makeRandom({ network: bitcoinNetwork });

const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey });

const publicKey = keyPair.publicKey.toString('hex');

const privKey = keyPair.toWIF();

>All comments

Read the examples please.

For the HD stuff

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

const seed = bip39.mnemonicToSeed( "muscle skate dawn remember pumpkin foil vacuum such brass grass bullet shoulder" ).toString( 'hex' );

const root = HDKey.fromSeed(new Buffer(seed, 'hex'));

const path = "m/44'/0'/0'/0/0";

const derived = root.derivePath(path);

const publicKey = derived.publicKey.toString('hex');

const { address } = bitcoin.payments.p2pkh({ pubkey: derived.publicKey });

const privKey = bitcoin.ECPair.fromPrivateKey(derived.privateKey).toWIF();

For the single key stuff.

const bitcoin = require('bitcoinjs-lib');

// the default is bitcoin... so this is useless though.
const bitcoinNetwork = bitcoin.networks.bitcoin;

const keyPair = bitcoin.ECPair.makeRandom({ network: bitcoinNetwork });

const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey });

const publicKey = keyPair.publicKey.toString('hex');

const privKey = keyPair.toWIF();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

thrastarson picture thrastarson  路  3Comments

Beardcoding picture Beardcoding  路  3Comments

coingeek picture coingeek  路  4Comments

yakitorifoodie picture yakitorifoodie  路  3Comments

dakk picture dakk  路  3Comments