bitcoinjs-lib icon indicating copy to clipboard operation
bitcoinjs-lib copied to clipboard

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

Open imerkle opened this issue 7 years ago • 17 comments
trafficstars

        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

imerkle avatar Sep 03 '18 23:09 imerkle

Are you using node js? Or?

dcousens avatar Sep 03 '18 23:09 dcousens

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

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

junderw avatar Sep 04 '18 01:09 junderw

@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 avatar Sep 04 '18 23:09 imerkle

@imerkle what version of Node?

node --version

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

dcousens avatar Sep 05 '18 00:09 dcousens

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

junderw avatar Sep 05 '18 02:09 junderw

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

imerkle avatar Sep 05 '18 16:09 imerkle

@fanatid

dcousens avatar Sep 05 '18 21:09 dcousens

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.

dcousens avatar Sep 06 '18 07:09 dcousens

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

dcousens avatar Sep 06 '18 07:09 dcousens

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

imerkle avatar Sep 06 '18 07:09 imerkle

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?

fanatid avatar Sep 06 '18 07:09 fanatid

Yeah that's what I said

imerkle avatar Sep 06 '18 08:09 imerkle

Thanks, can you point on source of

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

fanatid avatar Sep 06 '18 08:09 fanatid

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.

junderw avatar Sep 06 '18 08:09 junderw

@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

dcousens avatar Sep 06 '18 08:09 dcousens

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

fanatid avatar Sep 06 '18 08:09 fanatid

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!

dbolser avatar Feb 05 '19 15:02 dbolser