Bitcoinjs-lib: Question

Created on 17 Oct 2018  路  14Comments  路  Source: bitcoinjs/bitcoinjs-lib

I want to create a nodejs webAPI which does 3 things: 1) Create private/public keys and address 2) Gets balance of any bitcoin address 3) Takes in senders address, pvt key and amount, and transfer it to a given destination address, and return the change back to senders address.

I wanted to know if bitcoin-js can help do it. Also can it does above transaction using a full running bitcoin node?

THanks,
Shrish

how to / question / docs

All 14 comments

Here's what you can do with this library:

  1. Yes
  2. No
  3. Yes (without broadcasting)

So you will need to provide:

  • A method for retrieving the UTXOs for any given address and their amounts.
  • A method for broadcasting the completed transaction to the Bitcoin network.

@junderw Can you provide example code for creating an offline transaction which can be broadcast?

I am sorry. Here are some codes that bother me. I hope you will take the time to answer them. Thank you very much.
txb.setVersion(1)
txb.addInput('61d520ccb74288c96bc1a2b20ea1c0d5a704776dd0164a396efec3ea7040349d', 0)
// Alice's previous transaction output, has 15000 satoshis
txb.addOutput('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP', 12000)
// (in)15000 - (out)12000 = (fee)3000, this is the miner fee
txb.sign(0, alice)

txb.addInput('61d520ccb74288c96bc1a2b20ea1c0d5a704776dd0164a396efec3ea7040349d', 0)
"61d520ccb74288c96bc1a2b20ea1c0d5a704776dd0164a396efec3ea7040349d" is the txid what I need to extract from uxtos?
What if I have multiple txids?
Can't I set the value of fees?
Will the remaining amount of the transaction all be used as fees ?
This shows that it is create a 1-to-1 Transaction, the code of the address with the change set is the original address is "txb.sign(0, alice)".

Because I don't have a test dash coin at the moment, I can't verify it, so I am very sorry to bring you the harassment. @junderw

... is the txid what I need to extract from uxtos?

Yes.

What if I have multiple txids?

Call txb.addInput multiple times.

Can't I set the value of fees?

Bitcoin fees are implicit. You can not set them explicitly. You set them indirectly by calculating the total inputs value, subtracting the fee you want to spend, and making sure the total outputs value equals the inputs value minus the fee.

Will the remaining amount of the transaction all be used as fees ?

Yes.

Isn't this a deal so troublesome? If I want to transfer you 0.5btc, but I only have a 3btc input, the remaining 2.5btc will be treated as a fee.... It鈥檚 terrible.

Why is this library different from other libraries? Other libraries like bitcore_lib set the value of fee when signing the transaction, instead of relying on the total outputs value equals the inputs value minus the fee.

If I want to transfer you 0.5btc, but I only have a 3btc input, the remaining 2.5btc will be treated as a fee.... It鈥檚 terrible.

This is how Bitcoin works. It has nothing to do with bitcoinjs-lib.

Other libraries like bitcore_lib set the value of fee when signing the transaction, instead of relying on the total outputs value equals the inputs value minus the fee.

When you set a fee in bitcore-lib it automatically creates a new output sending to your own address.

So if I say send 1 BTC to Alice with a 0.001 BTC fee, and I only have a 2 BTC input, bitcore-lib will automatically add a second output to send to your own address 0.999 BTC.

There is no explicit fee in Bitcoin. Trying to hide that fact from developers may be easy for them to understand, but it can hurt you. (ie. if you keep sending change to the same address you run risk of loss of fungibility, and coin selection optimization is not possible with bitcore-lib logic automatically deciding your second output.

I got it锛乼hanks

@junderw Can this library support the fourth version of zcash? I use the sign_hex generated by other libraries to broadcast return an error occured:16: tx-overwinter-active. Code:-26

Can this library support the fourth version of zcash?

We only officially support Bitcoin. If it works for zcash then that's great, but we will not explicitly support it.

However, you are more than welcome to fork us and create your own zcash supported library if you would like (hooray open source :-D )

So if I say send 1 BTC to Alice with a 0.001 BTC fee, and I only have a 2 BTC input, bitcore-lib will automatically add a second output to send to your own address 0.999 BTC.

@junderw How i can send 0.999BTC change to special address?

Hello sorry im new to this library so my question may seem a bit silly. Im slightly confused on what the line means txb.addInput('61d520ccb74288c96bc1a2b20ea1c0d5a704776dd0164a396efec3ea7040349d', 0)
how would i get the balance? and to confirm that a transaction has been made. even on testnet?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Beardcoding picture Beardcoding  路  3Comments

ishwarchandratiwari picture ishwarchandratiwari  路  3Comments

tuyennvtb picture tuyennvtb  路  3Comments

dakk picture dakk  路  3Comments

itsMikeLowrey picture itsMikeLowrey  路  3Comments