Bitcoin Trading (2)


In the last article, we focus on digital signature. In this article, we explore the source code of bitcoin transactions and answer the following questions:

  • How to give e-money a unit.
  • How to divide an electronic currency into several electronic currencies.
  • How to integrate multiple e-money into one e-money.
  • How to represent an unused electronic currency.
  • What is the transaction process of electronic currency.


Some key words, translated into Chinese, completely lost their original meaning. The key words are as follows:

  • Inputs: a set of transaction inputs, consisting of one or more inputs
  • Input: a transaction input. A transaction input is usually composed of information such as a utxo and a digital signature
  • Outputs: a set of transaction outputs
  • Output: a new e-currency for transaction output. It usually consists of the value of the e-money and the script that verifies the digital signature
  • Double spend: double flower. An electronic currency may be twice or more than once
  • Utxo: abbreviation for unspent transaction output. Usually, an output is referenced
  • TX: abbreviation for bitcoin transaction. Trading.
  • Txid: abbreviation for transaction ID. Refers to the hash value of the transaction.
  • satoshi: 1 BTC = 100,000,000 satoshi

electronic money

The white paper on bitcoin defines e-money

An electronic currency is defined as a chain of digital signatures. E-money is transferred from the owner to the next person. The owner uses the private key to digitally sign transactions and the public key of the next person, and the digital signature is attached to the back of the electronic currency (digital signature chain). The receiver can verify whether he has received the electronic currency by checking the digital signature.

An electronic currency, ever since it was created, is constantly being traded and attached with a digital signature. We can think of an electronic currency as a digital signature chain. For example:

  1. Owner 0’s appends his digital signature and owner 1’s public key at the end of the first transaction (from left to right)
  2. After the verification of bitcoin system, the electronic currency belongs to owner 1’s.
  3. Owner 1’s attached his digital signature and owner 2’s public key at the end of the second transaction
  4. After the verification of bitcoin system, the electronic currency belongs to owner 2’s.
  5. The third deal is similar

Bitcoin Trading (2)

Trading with electronic money

In bitcoin, the value of an electronic currency is measured by value in Satoshi. 1 BTC = 100,000,000 satoshi。 The essence of a transaction is the transfer of value, that is, the owner signs one or more of his electronic currencies, namely inputs. After the verification of bitcoin system, one or more electronic currencies belonging to the payee, namely outputs, are created. Note that an electronic currency, like a RMB, cannot be split into two parts, but it can be converted into several equivalent electronic currencies.

For example, Alice pays Bob 0.2 BTC of e-money, Bob receives an e-money of 0.15 BTC, and gives Alice an e-money of 0.05 BTC.

  1. Alice creates a transaction.
  2. Alice chooses an electronic currency with a value of 0.2 BTC as the input of this transaction.
  3. Alice pays Bob an e-currency with a value of 0.15 BTC, and gives her own change of 0.05 BTC. This part is the output of this transaction.
  4. After the bitcoin system was verified, Bob received an e-currency of 0.15 BTC.

Bitcoin Trading (2)

After the transaction, Alice’s 0.2 BTC electronic currency was abolished by the “MINT” of bitcoin system, and the “MINT” issued two new electronic currencies at the same time, one 0.15 BTC electronic currency was given to Bob, and one 0.05 BTC electronic currency was given to Alice. Only electronic money that is not voided by the mint can be used for trading, thus preventing double spending. The electronic currency issued by the “MINT” is called output, and the electronic currency without invalidation is called unspent transaction output, abbreviated as utxo.

Data format of the transaction

Here’s an early bitcoin deal.

Bitcoin Trading (2)

It consists of four independent parts

  • The yellow part shows: hash – txid (transaction ID)
  • The blue curly brackets indicate the description information and metadata of the transaction
  • The pink part shows: inputs
  • The green part shows: outputs

General format of transaction

name describe
version Version number of bitcoin system
hash Hash value of this transaction
inputs An array of inputs
outputs An array of outputs
lockTime If the value is 0, the transaction is executed immediately; if the value is not 0, the transaction is executed at the specified block height or timestamp.

Data format of output

name describe
value The value of e-money in BTC
scriptPubKey It is usually a lock script composed of payee’s public key, etc

Data format of input

name describe
prevTxId Hash value of last transaction
outputIndex Index of the output of the last transaction
scriptSig It is usually composed of the digital signature of the payer and the public key of the payee.

Notice that input is actually a reference. When calculating the transaction, the output of the last transaction is found as the actual input of this transaction through the prevtxid and outputindex attributes.

Reference articles:

Bitcoin: A Peer-to-Peer Electronic Cash System


How a Bitcoin Transaction Works