Cannot read property 'toWIF' of undefined

0

I am working on a cryptocurrency site in which each user will have there own individual deposit address to put currency on site. I am using bitcoin-core, bitcoinjs-lib, along with quite a few other modules.

Currently I am receiving an error after running the following code via node generate_wallet.js

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

var privKey = process.env.BIP32_PRIV;
var hdNode = bitcoinjs.HDNode.fromBase58(privKey);

var count = process.env.GENERATE_ADDRESSES ? parseInt(process.env.GENERATE_ADDRESSES) : 1000; // how many addresses to watch


var rescan = 'false';

for (var i = 1; i <= count; ++i) {
  console.log('bitcoin-cli importprivkey ' +  hdNode.derive(i).keyPair.toWIF() + " '' " + rescan)
}

The error:

console.log('bitcoin-cli importprivkey ' +  hdNode.derive(i).keyPair.toWIF(L4tvCJoE9nxFnmZ4znCDvpqMLo9m8ybpAzxdE4ZKWDAfBqeM8JsR) + " '' " + rescan)
                                                                      ^

TypeError: Cannot read property 'toWIF' of undefined
    at Object.<anonymous> (/root/bustabit-depositor/src/generate_wallet.js:13:71)
    at Module._compile (internal/modules/cjs/loader.js:736:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:568:12)
    at Function.Module._load (internal/modules/cjs/loader.js:560:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
    at executeUserCode (internal/bootstrap/node.js:526:15)
    at startMainThreadExecution (internal/bootstrap/node.js:439:3)
root@ubuntu:~/bustabit-depositor/src#

Any suggestions would be greatly appreciated. I don't even know how to work around this as there's no results online about this. Maybe i'm just dumb haha

Mason

Posted 2019-01-27T02:13:12.290

Reputation: 1

Answers

1

First, note that HDNode was removed in 4.0.0, so you should use the BIP32 module you included. Second, you don't need the extra keyPair property, so your code becomes:

var bip32 = require('bip32')

var privKey = process.env.BIP32_PRIV;
var hdNode = bip32.fromBase58(privKey);

var count = process.env.GENERATE_ADDRESSES ? parseInt(process.env.GENERATE_ADDRESSES) : 1000; // how many addresses to watch


var rescan = 'false';

for (var i = 1; i <= count; ++i) {
  console.log('bitcoin-cli importprivkey ' +  hdNode.derive(i).toWIF() + " '' " + rescan)
}

JBaczuk

Posted 2019-01-27T02:13:12.290

Reputation: 6 172