bitcoinjs / bitcoinjs-lib

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

How to send & receive bitcoin using bitcoinjs-lib? #1434

Closed MonarthS closed 3 years ago

MonarthS commented 5 years ago

First I create HD Wallet using BIP32 using mnemonics.

Now I want to generate new child addresses with private key using xpub & xpriv for every receive.

Then for example, I received BTC in 2 child addresses, i.e. 1 BTC & 2 BTC.

Now, I have to send 3 BTC to someone, then How can I send it as I have 3 BTC but in separate account with utxo.

How can I manage these accounts & transaction, and also how can I show the total balance to the user?

I thing it is insufficient to call the api to get balance for all accounts, as for HD wallet user can have thousands of addresses.

So, I understood this theoretically, but don't know how to implement this using bitcoinjs-lib, BIP32, HD-key.

junderw commented 5 years ago

call the api

Why is this insufficient? Which API are you using?

MonarthS commented 5 years ago

Assume that there is a HD wallet, in which I received BTC in first 3 child accounts, How can I get total balance of the Wallet? I've to call the api for 3 times and also I don't know that which child account has balance and which has not.

I'm using Bitpay insight api.

junderw commented 5 years ago

Insight doesn't support HD, but you can query 20 addresses in one call.

Use that.

MonarthS commented 5 years ago

Here api doesn't matter, please read my question again, I want code for sending & receiving derived addresses, and how to derive addresses with private key.

junderw commented 5 years ago

https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.js

and

https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js

are good places to start reading.

They are linked in the README

MonarthS commented 5 years ago

I am using this:

const path = "m/0'/0/0"
const mnemonic = 'word word word word word word word word word word word word'
const seed = bip39.mnemonicToSeed(mnemonic)
const root = bip32.fromSeed(seed)

const child1 = root.derivePath("m/0'/0")
const child2 = root.derivePath("m/0'/1")

const child1Address = bitcoin.payments.p2pkh({ pubkey: child1.publicKey }).address
const child2Address = bitcoin.payments.p2pkh({ pubkey: child2.publicKey }).address

can I get privatekey of child addresses? If yes, then how?

junderw commented 5 years ago

child1.privateKey is the private key.

supertestnet commented 3 years ago

https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.js

and

https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js

are good places to start reading.

They are linked in the README

I believe the links have changed, I think the corresponding links are:

https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts

and

https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts

I am trying to use the createPayment() function which is inside https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts and I am trying to do so in a browser context. The readme says that for use in the browser you need to use browserify and it links here for a web ui example of a project that uses bitcoinjs-lib. I went there to grab the browserified bitcoinjs-lib and got it here, but the createPayment() function does not seem to be in that file.

I tried adding it manually in a new Githubissues.

  • Githubissues is a development platform for aggregating issues.