Bitcoinjs-lib: "Error.captureStackTrace is not a function" when creating keypair from privatekey on different network

Created on 4 Sep 2018  路  17Comments  路  Source: bitcoinjs/bitcoinjs-lib

        const hash = bitcoin.crypto.sha256(Buffer.from("my seed here"))
        const network = coininfo(`DASH-TEST`).toBitcoinJS();
        const keyPair = bitcoin.ECPair.fromPrivateKey(hash,{network})
        console.log(keyPair)


When there is no network option it works fine

external how to / question / docs

Most helpful comment

Just insert the network info needed for DASH.

Then just count up the keys / accounts according to BIP44.

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

const DASH = { // DASH
  "messagePrefix": '\u0018Dash Signed Message:\n',
  "bip32": {
    "public": 0x0488b21e,
    "private": 0x0488ade4
  },
  "pubKeyHash": 0x4c,
  "scriptHash": 0x10,
  "wif": 0xcc
}

let phrase = bip39.generateMnemonic() // generate a random phrase
//  phrase = 'device lady dose anger fire catch junk turtle elite possible correct venue'

let seed = bip39.mnemonicToSeed('device lady dose anger fire catch junk turtle elite possible correct venue')
//  seed (hex) = '4a9b302fbbcf4165575c4bf234a4346a6295301d3f79986eff34514ff217e74468f236ba26a70181d90e1bdfb628f652d75beb9b061d325343d8e8e38f031429'

let rootNode = bip32.fromSeed(seed, DASH)

// 5' is DASH BIP44 coin value
let firstAccount = rootNode.derivePath("m/44'/5'/0'")

let xpub = firstAccount.neutered().toBase58()
// xpub6DX8xdrSMpep9No44GiHTBrXS8spKXUSypS11qtUxRVmBNkyAi9BX1FWn3tLUWeHM6JKiPMyTWMfndkp6oAKp9UAXvcreFVhtbhEE47hZD1

let firstKey = firstAccount.derivePath("0/0")

let address = bitcoin.payments.p2pkh({ pubkey: firstKey.publicKey, network: DASH }).address
// Xw6f5rFzLLAu2d9o8xaEfscuFXbqfMxckg

let firstKeyECPair = bitcoin.ECPair.fromPrivateKey(firstKey.privateKey, { network: DASH })

let wif = firstKeyECPair.toWIF()
// XHXPznM8PEFnkman8aYqG2uRDPT6jxQxKsVmGqxgRdEPha8DPNCG

All 17 comments

Are you using node js? Or?

const hash = bitcoin.crypto.sha256(Buffer.from("my seed here"))

Don't do this, ever. You will lose your Dash coins.

@dcousens yes

@junderw then how else would i create private key from a bip39 mnemonic for a different network, like for ltc or any other network besides btc

@imerkle what version of Node?

node --version

If you want to use BIP39... use it. Don't make up your own.

Just insert the network info needed for DASH.

Then just count up the keys / accounts according to BIP44.

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

const DASH = { // DASH
  "messagePrefix": '\u0018Dash Signed Message:\n',
  "bip32": {
    "public": 0x0488b21e,
    "private": 0x0488ade4
  },
  "pubKeyHash": 0x4c,
  "scriptHash": 0x10,
  "wif": 0xcc
}

let phrase = bip39.generateMnemonic() // generate a random phrase
//  phrase = 'device lady dose anger fire catch junk turtle elite possible correct venue'

let seed = bip39.mnemonicToSeed('device lady dose anger fire catch junk turtle elite possible correct venue')
//  seed (hex) = '4a9b302fbbcf4165575c4bf234a4346a6295301d3f79986eff34514ff217e74468f236ba26a70181d90e1bdfb628f652d75beb9b061d325343d8e8e38f031429'

let rootNode = bip32.fromSeed(seed, DASH)

// 5' is DASH BIP44 coin value
let firstAccount = rootNode.derivePath("m/44'/5'/0'")

let xpub = firstAccount.neutered().toBase58()
// xpub6DX8xdrSMpep9No44GiHTBrXS8spKXUSypS11qtUxRVmBNkyAi9BX1FWn3tLUWeHM6JKiPMyTWMfndkp6oAKp9UAXvcreFVhtbhEE47hZD1

let firstKey = firstAccount.derivePath("0/0")

let address = bitcoin.payments.p2pkh({ pubkey: firstKey.publicKey, network: DASH }).address
// Xw6f5rFzLLAu2d9o8xaEfscuFXbqfMxckg

let firstKeyECPair = bitcoin.ECPair.fromPrivateKey(firstKey.privateKey, { network: DASH })

let wif = firstKeyECPair.toWIF()
// XHXPznM8PEFnkman8aYqG2uRDPT6jxQxKsVmGqxgRdEPha8DPNCG

The error still occured. I found out the error was only happening when using https://github.com/cryptocoinjs/coininfo 's data reason was that coininfo had messagePrefix: null. When changing it to "messagePrefix": '\u0018Dash Signed Message:\n' it worked.

Error was caused by some typechecking in typeforce lib

My followup question is that is messagePrefix required or optional ?

So it's either needs to be fixed here or that library depending on whether its optional or not.

code: https://codesandbox.io/s/pw8yk195yq ln 25

@fanatid

This isn't a bug, or really our problem.

However, I am not against removing the strict need for messagePrefix from our network object - as it is not needed by this module.

@imerkle, if you could please provide reproduction as to how you received the error in the title, that would be appreciated.

@dcousens it's provided in the codesandbox above. Just comment the line 25 to see the error

messagePrifix should be defined -- https://github.com/bitcoinjs/bitcoinjs-lib/blob/v4.0.1/src/types.js#L23
@imerkle is messagePrefix definition in coininfo for DASH will solve you problem?

Yeah that's what I said

Thanks, can you point on source of

"messagePrefix": '\u0018Dash Signed Message:\n'

Thanks, can you point on source

No source, I just made it up because it is irrelevant to the error, and typeforce was yelling at me.

@fanatid it is probably OK, but may need the length prefix reflected to the actual length...

'Bitcoin Signed Message:\n'.length === 0x18
'Dash Signed Message:\n'.length === 0x15

@Alex-Werner @codablock @UdjinM6 do you have any preferred options for message prefix? Is Dash Signed Message:\n will works for DASH?

From bitcoinjs, I see that it wants...

    messagePrefix: '\x18Bitcoin Signed Message:\n',
    messagePrefix: '\x19Litecoin Signed Message:\n',
    messagePrefix: '\x19DarkCoin Signed Message:\n',

Honestly not sure where these values come from!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhaozhiming picture zhaozhiming  路  3Comments

Beardcoding picture Beardcoding  路  3Comments

namnv04 picture namnv04  路  3Comments

panpan2 picture panpan2  路  3Comments

itsMikeLowrey picture itsMikeLowrey  路  3Comments