Transaction Records. How Do Transactions Work in Bitcoin?

blockchain coinsfera.com

Bitcoin is one of the cryptocurrencies that leads the market. The market capitalization of the Bitcoin exceeds 50% of the whole sector. The attraction to the Bitcoin caused its value to hit its highest price in late 2017. However, people do not know the details of Bitcoin. Here we will examine the transactions in the Bitcoin blockchain.

Transactions are one of the central parts of the Bitcoin blockchain. Since Bitcoin is the currency, then it should have ledgers and transaction details, at least. Those transactions are grouped and put into blocks by the system. Because transactions are necessary, this type of record is called a transaction-based ledger. It is required to know the difference between account-based and transaction-based bookkeeping.

Difference Between Transaction and Account-based Accounting

Account-based ledger is the ledger that main focus of records is the accounts. Each transaction refers to the accounts of people and determines whether there is an adequate amount of coins to do the transactions. We know that blockchain consists of hundreds of blocks. If we try to determine that the sending account has the right amount of money, then we have to analyze all blocks and calculate if it is correct. This process is very cumbersome. If we look at the first table, it is hard to identify Alice has the right amount of money to transfer at the end of the table. It is a short list we can calculate, but in real blockchain, it is hard to do. After calculation, we will see that it is not a valid transaction.

ledger coinsfera.com

A transaction-based ledger is a ledger that main focus of the records is the transactions itself. Each transaction refers to another transaction to check its validity. The number in the input line refers to another transaction and the exact person. In reality, those index numbers are the hash pointers to those transactions. Each transaction has its hash value. So just finding the previous transaction, we can identify that Alice has a specified amount of money in her account. Since you have to spend all of your coins once, you have to send another part of the money back to yourself, usually to your other account. If you want to transfer to two separate accounts, then you can do it by adding two different people to the output of the transaction. On the other hand, if you want to send from two separate transactions, then you can merge them in one input.

Deep Into Transaction

We examined how the transaction works. Now it is time to delve into more programming side of the transaction. I am sure that it will be interesting for many people since they think of bitcoin as an abstract system. Here we will try to look close to the transaction.

We know that transaction has input and output, and it is also valid for the script. Additionally, there is a metadata part that gives us some information about the transaction. In the metadata, there are some important keywords to analyze. Firstly, “hash” is the hash value of the transaction. It is the hash value that can be used as a hash pointer to this transaction in the future. Secondly, “vin_sz” is the number of inputs in the transaction. Here there are two inputs. Next, “vout_sz” is the number of outputs in the transaction. Here there is one output. “size” is the size of the transaction.

After the metadata, the inputs section comes to the scene. We have two inputs, and we will have two similar coding pieces. Firstly, “hash” is the hash value of the previous transaction, and “n” shows the which index from the transaction. The index of the output is zero, and zero is the first output from the previous. In the end, there is “scriptSig”, it is our signature that we are able to require those funds from the previous transaction.

metadata coinsfera.com

The last section is the output part of the code. The output consists of two parts: value and script. “value” is the coins that you want to send. “scriptPubKey” does not just include public key but also other things. As its name suggests, it is a script. It is the Bitcoin scripting language. This language is simple, compact, and specially made for cryptography. It is a stack-based language. In this programming language, there is no loop function. It means every command is executed once in a linear way. Let’s examine the scripts in the output.

<sig> <pubKey> OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

In this script, <sig> and <pubKey> are specified by the recipient who will receive the payment. OP_DUP is the operation that takes and duplicates the last stacked value (in this case <pubkey>), which is at the top of the stack. OP_HASH160 finds the hash value of the last stacked value (<pubkey>). Then the algorithm adds <pubKeyHash>, which is the public key provided by the sender. Next, OP_EQUALVERIFY is the operation that verifies that the hash of the public key provided by the recipient and the sender are the same. If they are true, then the last two stacks will disappear. Finally, we already find that the public key is correct, and OP_CHECKSIG is the command that verifies the signature again and approves that signature is true for the whole transaction. It is the power of Bitcoin scripting language that it does not call any library. It is all in the Bitcoin scripting language. If the error occurs, then the whole process stops, and the transaction will not add to the blockchain.

In most cases, it is hard to understand the working principles of Bitcoin. In this blog, we tried to give you information about the transactions to help you visualize it. With that visualization, we are sure that your trust in Bitcoin will improve.