Transfer of ownership in Bitcoin

Bitcoin is a decentralized digital currency that can be transferred on the peer-to-peer bitcoin network. Bitcoin transactions are verified by network nodes through cryptography and recorded in a public distributed ledger called a blockchain.

In Bitcoin, there is no concept of “account balance”, the balance amount is generated on the wallet client. This is possible because of the way transactions were designed for Bitcoin. This blog post will discuss how transactions handle the ownership of Bitcoin in transactions.

Transactions

Every transaction can have multiple inputs and outputs. Of course, a transaction can be based on one input and one output. Inputs belong to the sender of Bitcoin, the outputs will be given to the receiver of Bitcoin. The receiver can use those outputs as input for his/her next transaction.

https://ibrahim-13.github.io/assets/blog/Bitcoin-Transaction-Structure.png

Bitcoin holders will have unspent transaction outputs that the wallet will keep a list of. The wallet can aggregate the amount from those outputs and show the total amount owned by the owner, which can be interpreted as the account balance

https://ibrahim-13.github.io/assets/blog/Bitcoin-UTXO-Sum.png

A full node in the Bitcoin network will examine transactions from the Genesis Block to the latest one and create a database of unspent transaction outputs also known as UTXO. When a new transaction is received from the network, the node will validate that against its database of UTXO.

Now, the real question is, how do Bitcoin nodes make sure that I’m spending my outputs and not someone else’s, or vice versa?

How transfer of ownership works

A transaction will look something like this-

{
  "version": 1,
  "locktime": 0,
  "vin": [
    {
      "txid": "7957a35fe64f80d234d76d83a2a8f1a0d8149a41d81de548f0a65a8a999f6f18",
      "vout": 0,
      "scriptSig" : "3045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e3813[ALL] 0484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf",
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.01500000,
      "scriptPubKey": "OP_DUP OP_HASH160 ab68025513c3dbd2f7b92a94e0581f5d50f654e7 OP_EQUALVERIFY OP_CHECKSIG"
    },
    {
      "value": 0.08450000,
      "scriptPubKey": "OP_DUP OP_HASH160 7f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a8 OP_EQUALVERIFY OP_CHECKSIG",
    }
  ]
}

Note: The data above is shown in JSON object for clarity, the actual transaction data is stored in a different encoding format in the Blockchain.

Here, “vin” is the list of inputs and “vout” is the list of outputs.

In “vin”, “txid” is the transaction id of the sender’s unspent transaction output, “vout” is the output index of the transaction and “scriptSig” (unlocking script) is the proof that the sender owns the output given as the transaction input. Locking scripts generally hold the hash of the receiver’s public key.

In “vout”, “value” is the amount of Bitcoin, and “scriptPubKey” (locking script) is the identity of the receiver of the Bitcoin. Unlocking script generally contains the signature created from the owner’s private key and the public key, along with the public key itself.

The locking and unlocking scripts are combined, becoming the transaction script. The transaction script is then run on Bitcoin’s implemented script engine. A transaction is accepted as valid when all operations defined in the transaction script are successful.

https://ibrahim-13.github.io/assets/blog/Bitcoin-Transaction-Script.png

When a transaction is sent to the network, the node will fetch the outputs of the given “txid”s from Blockchain. Those outputs will have the locking scripts, which will be combined with the current output’s unlocking script and run in the scripting engine. If the sender provides the correct signature (proof of ownership) for input transactions, the output will be validated and sent to the receiver.

This means, the sender needs to show valid proof of ownership to his input transactions and creates output transactions with a locking script that only the receiver of those outputs can show proof of.

https://ibrahim-13.github.io/assets/blog/Bitcoin-Ownership-Transfer.png

So to speak, each transaction will consume the Bitcoin from the inputs and create new spendable transaction outputs with the owner’s identity. The owner can provide the proof of ownership to spend those which in turn will create new spendable outputs with a renewed ownership identity.

Ownership Validation Performance

Validation of transaction ownership is relatively fast, as the only needed data for validation are the given output transactions. Full nodes will index the “txid” of transaction data. So, for example, 50 transactions will only need 50 data read operations on the database, no matter the depth or size of the blockchain. But data size does matter when it comes to indexing the “txid”s as those are primarily stored on the RAM and increasing indexes will require more and more RAMs.

Fractional Transfer

One important thing to remember about Bitcoin transactions is that an unspent transaction output can not be used fractionally. That means, that when the sender of the Bitcoin has an output of 10 BTC but wants to send only 8 BTC, then he/she has to spend the whole of 10 BTC, send 8 BTC to the receiver, and also send 2 BTC to original sender’s address.

Thus, 8 BTC from the 10 BTC of the sender will be spent and a new 2 BTC will be generated for the sender to use in future transactions.

https://ibrahim-13.github.io/assets/blog/Bitcoin-Fractional-Transaction.png

Resources