A transaction is a transfer of ownership

Time:2020-11-13

If you want to get to the top, start at the lowest level.

When it comes to bitcoin, we hear various descriptions: “bitcoin is a database in which transactions are stored.” “Bitcoin is a virtual machine that can execute scripts.” “Bitcoin is a distributed ledger” and so on. In fact, when describing a new thing, people usually use the old concept to make analogy, reflecting some characteristics of the new thing. So let’s take a look at what bitcoin’s “trading” looks like today.

For ordinary users, our observation of bitcoin is as follows:

Transfer: open wallet, input amount, address, click send
Collect money: open wallet > wait for network synchronization

The wallet seems to be uploading and downloading data from a remote server. In fact, bitcoin network can be regarded as a database with only “add” and “search” functions. So how can we store transaction data into the bitcoin network? Generally speaking, bitcoin network will verify the received data as follows:

def receive_data(data) do
  data
  |> deserialize()
  |> valid?()
  |> save()
end

Deserialization

Therefore, the first step is to deserialize the received binary data to get the transaction content. Here is the bitcoin transaction data of raw:

01000000011a6dae526d3c88630a97bcfe84895e6802ea367324e57a70ecd1e30fcbbb9a88000000006b483045022100fde8236c63aa891850216f736d1efaecbca2582ac9027016e4341075dc086f38022044f7aa43c33e81018c642e5dffe545e3f3ad610f8bcdd2671026592c7eadee18012102d6a0f589e7fa321c5ec21aefde732ede2d7e2a16998dda19965256b2fb3523c2feffffff030000000000000000166a146f6d6e6900000000000000020000000002faf08022020000000000001976a91442352b8a2b42a31b63ed67363a5ac5c3463c3b5a88ac42d16800000000001976a9145d3c5a1aac4c6182d987c68055d63a75ba62e22d88ac65a91300

referencehttps://bitcoin.org/en/develo…It can be translated as:

version: 1,
tx_in_count: 1,
tx_in: 
  [
    {
      previous_output: 
        {
          hash: '1a6dae526d3c88630a97bcfe84895e6802ea367324e57a70ecd1e30fcbbb9a88',
          index: 0
        },
      script_bytes: 107,
      signature_script: 'PUSH48 3045022100fde8236c63aa891850216f736d1efaecbca2582ac9027016e4341075dc086f38022044f7aa43c33e81018c PUSH64 2e5dffe545e3f3ad610f8bcdd2671026592c7eadee18012102d6a0f589e7fa321c5ec21aefde732ede2d7e2a16998dda19965256b2fb3523c2',
      sequence: 4278190079
    }
  ],
tx_out_count: 3,
tx_out:
  [
    {
      value: 0,
      pk_script_bytes: 22,
      pk_script: 'OP_RETURN 6f6d6e6900000000000000020000000002faf080'
    },
    {
      value: 0.00000546,
      pk_script_bytes: 25,
      pk_script: 'OP_DUP OP_HASH160 42352b8a2b42a31b63ed67363a5ac5c3463c3b5a OP_EQUALVERIFY OP_CHECKSIG'
    },
    {
      value: 0.06869314,
      pk_script_bytes: 25,
      pk_script: 'OP_DUP OP_HASH160 5d3c5a1aac4c6182d987c68055d63a75ba62e22d OP_EQUALVERIFY OP_CHECKSIG',
    }
  ]   

Note that this is a transaction with version 1, and the transaction format of version 2 can refer tohttps://github.com/bitcoin/bi…。 The specific functions added in version 2 will be specially understood in the following articles.

verification

After the deserialization is completed, the information in the transaction is extracted. The next step is to verify the legitimacy of the transaction. We only need to use thetx_in, and from the previous transactiontx_outTo run the two scripts together and get the result of true, which proves that the new transaction can transfer the value in the corresponding output of the old transaction. Of course, the sum of values in the output of a new transaction cannot be greater than the value obtained in all inputs.

How to run bitcoin scripts will be mentioned in later articles.