How does Bitcoin work – Part 3 | The “Billcoin” Transactions

bitcoin
Sat Mar 15 2014

Introduction

In the two previous posts we went over the following:

1) The logo of the coin (important)
2) The algorithm for the creation of the “Wallet”.
3) What is a Hash. And the SHA256 hash.
4) What is the Block Chain. Its importance and role.

In this part we’ll create together the algorithm necessary for a “Transaction”.

“Billcoin” Transactions

Now that we have wallets, it is time to create the process to send money from one wallet to another. This process is called a transaction. At a first though to make a transaction we would need:

1) Sender Wallet Address
2) Receiver Wallet Address
3) Amount to send

transaction

 

However, in the Billcoin (and of course Bitcoin) world there is no place where your money is “stored”. The whole Billcoin economy consists of one file. This file is called the Block Chain which you can see exactly what it is in Part 2. So basically we want to broadcast a request to the network to add our transaction in the Block Chain.

Our transaction needs a sender, a receiver and an amount of money. The Bitcoin algorithm is a bit more advanced and allows a sender to send money to multiple destinations. However, we’ll keep the Billcoin economy simple and have only one destination. Also, there is a parameter in the Bitcoin transaction which is a “fee”. This is an amount of money offered to a “miner” if the sender wants to. “Miner” is the PC/wallet that will find the magic number we mentioned in the Part 2 post so we can add the block with our transaction in the Block Chain. We’ll talk about “miners” in depth in Part 4.

Let us take a glimpse of a real block in the Bitcoin Block Chain.

We can see some key elements in the block.

Hash:  This works as the unique key for this block. The final Hash after running SHA256 on the block’s data. You can see that it has 16 zeros at the beginning. To generate this number it took a lot of work!!
Previous block:  The previous block.
Next block:  The next block. Since this is an older block we have this information.
Time:  The timestamp when this was created
Difficulty:  The difficulty level (hash has to start with certain number of zeros)
Transactions:  How many transactions this block contains
Total BTC:  Total Bitcoins transferred in this block
Size:  Size of the data in bytes
Merkle root:  This is a hash generated by all of the transactions in this block chain. We hash each transaction hash by two and then by two again, until we have one number. The hash of all hashes is the merkle root
Nonce:  This is the magic number found after a lot of brute force tries to generate the Hash (the first line of this table)

 

In this block now we notice the first line is a transaction from “Generation”. When someone in the network finds this magic number (the nonce) that can generate a hash with 16 zeros. Then we can add the block in the block chain and he can add his address on the first line to receive 25 Bitcoins as a reward.

Now let us see the second line which is an actual transaction of a user so we can dive deep in the properties of a transaction.

In the transaction above we can notice the following properties

Hash:  This works as the unique key for this transaction. It is a combination of data included in the transaction that create this key.
Appeared in:  In which block this transaction is located.
Number of inputs:  The number of inputs. Inputs are references to transactions where the wallet, that wants to send the money, has received money from.
Total BTC in:  The total number of Bitcoins the wallet has received.
Number of outputs:  The number of senders.
Total BTC out:  The total number of Bitcoins the wallet sends. It has to be equal to the “Total BTC in”. If you want to send just an portion of your wallet’s amount, you can send the portion to the destination and the rest back to yourself.
Size:  Size of the data in bytes
Fee:  If you want to give a small reward to the good fellow that took your transaction, added it in a block and found a nonce for that block so it could be added in the block chain.

 

The out as we can see has a ScriptSig which is a script signature. This is a signature generated by the address and the private key of the wallet seen in Part 1. With this signature we can verify that the owner of the wallet has made this request.

Time to create our own snippet to generate a transaction to send to the network and hopefully someone will add it in the block chain.

JSON structure:
– root
— transaction hash
— hash (same as above)
— version
— number of inputs
— number of outputs
— lock time
— size (bytes)
— inputs
— previous output
—– hash of previous transaction
—– index of previous output
— scriptsig (replaced by “coinbase” on generation inputs)
— sequence (only when the sequence is non-default)
— address (on address transactions only!)
— outputs
— value
— scriptpubkey
— address (on address transactions only!)
— block hash
— block number
— block time

Our Transcript JSON object that we will broadcast to the network could be the following example.

{
  "hash":"927ff709c118ecd748142b05bdf47cb496643fd11c9fa94a164895d00580c6d6",
  "ver":1,
  "vin_sz":1,
  "vout_sz":1,
  "lock_time":0,
  "size":193,
  "in":[
    {
      "prev_out":{
        "hash":"c6f19e4ea56d46b1595966840360e178aea1f9af24432bb6ac5a9b1e443333d9",
        "n":0
      },
      "scriptSig":"3046022100dcac0b642fbdfcadb9f6bd2d948e46bf0021b37dcb0381f76d4bc0560dee6a35022100a5142920494a337820b1ddcaf505d58c5ec5d1fad38618d708b502e70dfa15df01 0345d9ff27e06aeef66bc2d8df328d8e1335a78a3737867559de60f32bd8996dff"
    }
  ],
  "out":[
    {
      "value":"0.00040000",
      "scriptPubKey":"OP_DUP OP_HASH160 7f66b7e6ddbeb31f787b919634066bdae324b7cb OP_EQUALVERIFY OP_CHECKSIG"
    }
  ]
}

So we have the “hash” property which will be the unique id of our transaction.

In the Billcoin economy we said that we’ll have one wallet send money to another. And not one to multiple, this will make it simpler.
We have a property “in” which has a “prev_out” property with a “hash”. This hash points which transactions were responsible for the money that arrived in the wallet. This way we can see where did all of the money in this wallet come from. So the “in” can have multiple prev_out blocks if the wallet has received money from multiple transactions.

The “out” has the value we want to send, and the “scriptPubKey” the hashed public key of the address. So it will be

scriptPubKey: OP_DUP OP_HASH160 [pubKeyHash where we send the money] OP_EQUALVERIFY OP_CHECKSIG

Also we see a “scriptSig” property. This is a signature to verify that the sender really sent the money specified. It has two big strings which are the [signed_message] [public_key].

Billcoin Transaction

You can checkout now Step 3 in the Billcoin economy over here => http://bstavroulakis.com/demos/billcoin/ and get an overview of the Billcoin Pending Transactions! You can also generate a transaction by yourself by generating some wallets and sending 0 amount of Billcoins from one wallet to another.

transactions_withSteps

References

http://bitcore.io/blog/articles/transactionbuilder/

https://en.bitcoin.it/w/images/en/7/70/Bitcoin_OpCheckSig_InDetail.png

http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html

http://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx

https://en.bitcoin.it/wiki/Transactions

http://www.math.brown.edu/~jhs/Presentations/WyomingEllipticCurve.pdf

https://www.strongcoin.com/en/blog/the_easiest_way_to_create_secure_offline_bitcoin_transactions

Full Stack Weekly Newsletter

A free weekly newsletter for full stack web developers!