Bitcoin Transactions
Transaction is an interesting and fundamental concept in blockchain, which conceal the secret of blockchain.
Transaction
Here is an example of bitcoin transaction, which also could be viewed in blockchain.info
{
"version": 1,
"locktime": 0,
"vin": [
{
"txid": "7957a35fe64f80d234d76d83a2a8f1a0d8149a41d81de548f0a65a8a999f6f18",
"vout": 0,
"scriptSig": "3045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204 b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e3813[ALL] 0484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d1 72787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf",
"sequence": 4294967295
}
],
"vout": [
{
"value": 0.015,
"scriptPubKey": "OP_DUP OP_HASH160 ab68025513c3dbd2f7b92a94e0581f5d50f654e7 OP_EQUALVERIFY OP_CHECKSIG"
},
{
"value": 0.0845,
"scriptPubKey": "OP_DUP OP_HASH160 7f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a8 OP_EQUALVERIFY OP_CHECKSIG"
}
]
}
Based on the mechanism of redeeming the asset in a transaction, transaction could be classified into following categories.
- P2PKH
Unlock Script: <Cafe Signature> <Cafe Public Key>
Lock Script: OP_DUP OP_HASH160 <Cafe Public Key Hash> OP_EQUALVERIFY OP_CHECKSIG
- MultiSignature
A 2-of-3 multisignature example
Unlock Script: <Signature B> <Signature C>
Lock Script: 2 <Public Key A> <Public Key B> <Public Key C> 3 CHECKMULTISIG
- P2SH
Introduced in 2012. This shifts the burden in fees and complexity from the sender to the recipient (spender) of the transaction.
Redeem Script: 2 PubKey1 PubKey2 PubKey3 PubKey4 PubKey5 5 CHECKMULTISIG
Locking Script: HASH160 <20-byte hash of redeem script> EQUAL
Unlocking Script: Sig1 Sig2 <redeem script>
- RETURN
No bitcoin, extra 80 byte storage, not in UTXO.
SegWitness
Introduced in 2017. Here is an example from current chain
Reference Implementation
BIP141
BIP143
BIP147
{
"ver":1,
"inputs":[
{
"sequence":4294967295,
"witness":"0400483045022100c4c482edbf5b3a74ee65a61ea9380a469ca18fda5f5623998e8e1368aa4250fe022053ee2a52eb23810735518aebfa11f7ae6baf98c63fc42260b5a159a9e404524301473044022076ec695519510462a8c1a3bffc41163ce14462a36dcf52e4811a2c662152afc102200a1635763c3e7b90f5580c29d1badfa7fc771c69a36392f634788a870dd3ea7d016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae",
"prev_out":{
"spent":true,
"tx_index":356442070,
"type":0,
"addr":"bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej",
"value":224182264,
"n":3,
"script":"0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d"
},
"script":""
}
],
"weight":767,
"block_height":529137,
"relayed_by":"0.0.0.0",
"out":[
{
"spent":false,
"tx_index":356445337,
"type":0,
"addr":"1J1LXp3UF8YcJcrHERo4V4zoqr5YguLtf9",
"value":5887000,
"n":0,
"script":"76a914ba8a71321122cbc5d6e70a150d801420ee60d45988ac"
},
{
"spent":true,
"tx_index":356445337,
"type":0,
"addr":"bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej",
"value":218205264,
"n":1,
"script":"0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d"
}
],
"lock_time":0,
"size":383,
"double_spend":false,
"time":1529908501,
"tx_index":356445337,
"vin_sz":1,
"hash":"a63aee6fdd500b0da4667212f1468452201451e34c9bdcfae953713dd20c680e",
"vout_sz":2
}
The original purpose of this is to take care of Bitcoin's transaction malleability.
Timelocks
Besides the redeeming method, transactions could specify the "Time" when the asset in transaction could be redeemed.
- nLockTime, Absolute/Transaction level
Transaction Locktime, defines the earliest time that a transaction is valid and can be relayed on the network or added to the blockchain. If a transaction is transmitted to the network before the specified nLocktime, the transaction will be rejected by the first node as invalid and will not be relayed to other nodes.
- Check Lock Time Verify (CLTV), Absolute/Script level
This is a new script operator introduced in Dec 2015.
Rather than a per-transaction timelock as nLockTime, CLTV is a per-output timelock. By adding the CLTV opcode in the redeem script of an output it restrict the output.ample of bitcoin transaction, which also could be viewed in
An example of redeem script: bitcoin to Bob until 3 month.
<now + 3 months> CHECKLOCKTIMEVERIFY DROP DUP HASH160 <Bob's Public Key Hash> EQUALVERIFY CHECKSIG
- nSequence, Relative/Transaction level
Transaction inputs with nSequence values less than 2^31 are interpreted as having a relative timelock. For example, a transaction with one input with an nSequence relative timelock of 30 blocks is only valid when at least 30 blocks have elapsed from the time the UTXO referenced in the input was mined.
- Check Sequence Verify (CSV), Relative/Script level
Works similar as CLTV, while check the sequence by script.