Transaction id
How Lotus computes a transaction id from Merkle roots of the inputs and outputs, leaving the unlocking scripts out so the id cannot be changed by a third party.
The id of a Lotus transaction does not cover the unlocking scripts (scriptSig) of its inputs. Nobody can change the id of a transaction by altering a signature encoding or the data pushed in an input, so chains of unconfirmed transactions stay valid.
The inputs and the outputs enter the id as two Merkle roots. A script that knows the id can therefore be given one input or one output with a short proof instead of the whole transaction.
Preimage #
The id is the double SHA-256 of these 74 bytes:
| Size | Field | Meaning |
|---|---|---|
| 4 | nVersion | Transaction version, little-endian |
| 32 | inputs root | Merkle root of the inputs, without their scriptSig |
| 1 | inputs height | Number of levels of the inputs tree |
| 32 | outputs root | Merkle root of the outputs |
| 1 | outputs height | Number of levels of the outputs tree |
| 4 | nLockTime | Lock time, little-endian |
Leaves #
| Tree | Leaf |
|---|---|
| Inputs | double SHA-256 of prevout (32-byte txid, 4-byte index) || nSequence (4 bytes) |
| Outputs | double SHA-256 of the serialized output: amount (8 bytes) || script length || script |
Merkle root and height #
- With no leaf, the root is 32 zero bytes and the height is
0. - With one leaf, the root is that leaf and the height is
1. - Otherwise, while more than one hash is left: if the count is odd, append a 32-byte zero hash; then replace each pair by the double SHA-256 of the two hashes joined. Each pass adds
1to the height.
The padding is a zero hash, not a copy of the last hash.
Reference code #
From src/primitives/transaction.cpp and src/consensus/merkle.cpp:
uint256 TxInputsMerkleRoot(const std::vector<CTxIn> &vin, size_t &num_layers) {
std::vector<uint256> leaves;
leaves.resize(vin.size());
for (size_t i = 0; i < vin.size(); i++) {
CHashWriter leaf_hash(SER_GETHASH, 0);
leaf_hash << vin[i].prevout;
leaf_hash << vin[i].nSequence;
leaves[i] = leaf_hash.GetHash();
}
return ComputeMerkleRoot(std::move(leaves), num_layers);
}
uint256 TxOutputsMerkleRoot(const std::vector<CTxOut> &vout,
size_t &num_layers) {
std::vector<uint256> leaves;
leaves.resize(vout.size());
for (size_t i = 0; i < vout.size(); i++) {
leaves[i] = SerializeHash(vout[i]);
}
return ComputeMerkleRoot(std::move(leaves), num_layers);
}
static uint256 ComputeTxId(int32_t nVersion, const std::vector<CTxIn> &vin,
const std::vector<CTxOut> &vout,
uint32_t nLockTime) {
CHashWriter txid(SER_GETHASH, 0);
size_t height;
txid << nVersion;
txid << TxInputsMerkleRoot(vin, height);
txid << uint8_t(height);
txid << TxOutputsMerkleRoot(vout, height);
txid << uint8_t(height);
txid << nLockTime;
return txid.GetHash();
}Transaction id and transaction hash #
A transaction has two identifiers:
| Identifier | Computed from | Used for |
|---|---|---|
| Transaction id (txid) | The preimage above | Outpoints: an input names the txid and output index it spends. |
| Transaction hash | Double SHA-256 of the whole serialized transaction, scriptSig included | Committing to the exact bytes of the transaction. |
A block commits to both. Each leaf of the block's Merkle tree is the double SHA-256 of the transaction hash followed by the transaction id. See Block header.
The inputs root and the outputs root are also part of the Lotus signature hash. See Signature hash.
