Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (4.1k points)

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:

What is the argument utxos int the .from(utxos) function is. Is this the PublicKey1 from my Wallet1?
The argument address for the .to(address) function is the PublicKey2 of my Friends Wallet2?
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?
The .fee(5430) means that I will spend 5430 Satoshi = USD $0.2337424950 for this Transaction?
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.

1 Answer

0 votes
by (14.4k points)

What is the argument utxos int the .from(utxos) function is. 

Is this the PublicKey1 from my Wallet1?

These are out-points 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, 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 a 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>

Browse Categories

...