简体   繁体   中英

Simple 1-to-1 Bitcoin Transaction with Bitcore

I try to understand Bitcoin Transactions. I use the Bitcore Javascript Library.

I have a Source Wallet1 (Address1/PublicKey1 and PrivateKey1) - with 10 Bitcoins (simplified).

Now a friend gives me his Wallet2 (Address2/PublicKey2), and wants to receive 1 Bitcoin.

When I read the documentation then for a simple Transaction (1-to-1) the code looks like this:

var transaction = new Transaction()
.from(utxos)          // Feed information about what unspent outputs one can use
.to(address, amount)  // Add an output with the given amount of satoshis
.change(address)      // Sets up a change address where the rest of the funds will go
.fee(5430) // Minimum non-dust amount
.sign(privkeySet)     // Signs all the inputs it can

But I have these Questions:

  1. What is the argument utxos int the .from(utxos) function is. Is this the PublicKey1 from my Wallet1?
  2. The argument address for the .to(address) function is the PublicKey2 of my Friends Wallet2?
  3. The argument address for the change(address) function is a new Address3, which belongs to MY Wallet3, which I have to create just before I make the transaction? => This means I need to know the PrivateKey3 of this Wallet3 and this is the Wallet3 which I will get my rest of the 9 Bitcoins? => Is it possible to do the Transaction without this .change(address) function? If I say, I don't want to transfer the rest of my 9 Bitcoins to the new Address? They should just stay in the original Wallet1?
  4. The .fee(5430) means that I will spend 5430 Satoshi = USD $0.2337424950 for this Transaction?
  5. The privkeySet in the .sign(privkeySet) function is the PrivateKey1 from my original Wallet1 right? After this .sign() function the Transaction will be 'fired' and the job is done?

    Thank you very much for your support.

What is the argument utxos int the .from(utxos) function is. Is this the PublicKey1 from my Wallet1?

These are outpoints (ie unspent outputs from prior transactions) that you can spend.

https://bitcore.io/api/lib/unspent-output

The argument address for the .to(address) function is the PublicKey2 of my Friends Wallet2?

An address is not necessarily derived from just a destination public key. You will need to read up on how p2sh/p2pkh/p2pk addresses are generated.

But in general, if you just want to pay someone with simple spend conditions, the canonical address to pay to is simply a p2pkh address, which is derived from the recipient's public key.

https://bitcore.io/api/lib/address

// recipientPublicKey should be provided
var address = new Address(recipientPublicKey);
// alternative interface
var address = Address.fromPublicKey(recipientPublicKey);

Usually though, your friend should just provide you with an address to pay to, so you don't have to worry about address generation. The example above assumes that your friend has provided you with their public key (for whatever reason).

The argument address for the change(address) function is a new Address3, which belongs to MY Wallet3, which I have to create just before I make the transaction? => This means I need to know the PrivateKey3 of this Wallet3 and this is the Wallet3 which I will get my rest of the 9 Bitcoins? => Is it possible to do the Transaction without this .change(address) function? If I say, I don't want to transfer the rest of my 9 Bitcoins to the new Address? They should just stay in the original Wallet1?

You will need a change address unless the UTXO(s) you are spending is equal to the amount you are sending to your friend + fees. Typically, it is good practice to generate a new keypair and address to use as a change address as address re-use is generally considered bad. But it is also OK to reuse your address for wallet1 .

The .fee(5430) means that I will spend 5430 Satoshi = USD $0.2337424950 for this Transaction?

Yes.

The privkeySet in the .sign(privkeySet) function is the PrivateKey1 from my original Wallet1 right? After this .sign() function the Transaction will be 'fired' and the job is done?

After you sign, you need to serialize your transaction. After serialization, you should get an hexadecimal ASCII string which you can use to broadcast to the bitcoin network using a 3rd party provider or your own Bitcoin node.

bitcoin-cli sendrawtransaction <serialized transaction>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM