bitcoinjs / bitcoinjs-lib

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

i am trying to create testnet address using bip39 and bip44 but i am getting p2pkh of undefined error #1336

Closed sukeerthimogallapalli closed 5 years ago

sukeerthimogallapalli commented 5 years ago

/home/ahex/Desktop/tokenize.node/controllers/register-controller.js:171 const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: TESTNET }) ^

TypeError: Cannot read property 'p2pkh' of undefined at Object. (/home/ahex/Desktop/tokenize.node/controllers/register-controller.js:171:38) at Module._compile (module.js:653:30) at Object.Module._extensions..js (module.js:664:10) at Module.load (module.js:566:32) at tryModuleLoad (module.js:506:12) at Function.Module._load (module.js:498:3) at Module.require (module.js:597:17) at require (internal/module.js:11:18) at Object. (/home/ahex/Desktop/tokenize.node/routes/routes.js:82:26) at Module._compile (module.js:653:30) at Object.Module._extensions..js (module.js:664:10) at Module.load (module.js:566:32) at tryModuleLoad (module.js:506:12) at Function.Module._load (module.js:498:3) at Module.require (module.js:597:17) at require (internal/module.js:11:18)

code: const bip39 = require('bip39') const bitcoin = require('bitcoinjs-lib') const TESTNET = bitcoin.networks.testnet

// console.log(bitcoin.networks) // bitcoin.networks= bitcoin.networks.testnet; // console.log(bitcoin.networks) //get a mnemonic, then seed, then root. var mnemonic = bip39.generateMnemonic(); var seed = bip39.mnemonicToSeed(mnemonic); var root = bitcoin.HDNode.fromSeedBuffer(seed);

//root can be transformed to your xpub and xprv var xprv = root.toBase58(); var xpub = root.neutered().toBase58();

//You can then create the address with a derivation of m/0'/0/0 var change = 0; //0 for external (receive), 1 for internal (change) var index = 0; //the index of the address you want to generate // var keypair = root.deriveHardened(44) // .deriveHardened(1) // .deriveHardened(0) // .derive(0) // .derive(0) // root.deriveHardened(0).derive(change).derive(index); //or var keypair = root.derivePath("m/0'/1'/0'/0/0") const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: TESTNET })

console.log(address,xpub,xprv)

junderw commented 5 years ago

You are installing an old version of the library.

bitcoin is holding an old version please use v4 or above.

sukeerthimogallapalli commented 5 years ago

i am using v4.0.3

"bitcoinjs-lib": "^4.0.3"

junderw commented 5 years ago

Then you have a dependency overlap with something.

One of your dependencies must be using v3 as a dependency, and your app is getting confused which one you are using.

sukeerthimogallapalli commented 5 years ago

Now i uninstalled the module and installed again

But now i am getting this error

var root = bitcoin.HDNode.fromSeedBuffer(seed, bitcoin.networks.testnet); ^

TypeError: Cannot read property 'fromSeedBuffer' of undefined at Object. (/home/ahex/Desktop/tokenize.node/controllers/register-controller.js:156:27) at Module._compile (module.js:653:30) at Object.Module._extensions..js (module.js:664:10) at Module.load (module.js:566:32) at tryModuleLoad (module.js:506:12) at Function.Module._load (module.js:498:3) at Module.require (module.js:597:17) at require (internal/module.js:11:18) at Object. (/home/ahex/Desktop/tokenize.node/routes/routes.js:82:26) at Module._compile (module.js:653:30) at Object.Module._extensions..js (module.js:664:10) at Module.load (module.js:566:32) at tryModuleLoad (module.js:506:12) at Function.Module._load (module.js:498:3) at Module.require (module.js:597:17) at require (internal/module.js:11:18)

can u plz help me..

My code:

const bip39 = require('bip39') const bip32 = require('bip32') const bitcoin = require('bitcoinjs-lib') const TESTNET = bitcoin.networks.testnet

var mnemonic = bip39.generateMnemonic(); var seed = bip39.mnemonicToSeed(mnemonic); var root = bitcoin.HDNode.fromSeedBuffer(seed, bitcoin.networks.testnet);

//root can be transformed to your xpub and xprv var xprv = root.toBase58(); var xpub = root.neutered().toBase58();

junderw commented 5 years ago

HDNode is removed in v4. it only exists in v3.

You need to work on your app / your dependencies. it looks like you app is using a mixture of v3 only and v4 only methods.

bitcoin.HDNode was changed to bitcoin.bip32 and it has fromSeed instead of fromSeedBuffer

sukeerthimogallapalli commented 5 years ago

yes i am trying fromSeedBuffer with v3 now my issue is fixed ..

Thank you