Transaction id
Protocol and consensus12.4.15+

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:

SizeFieldMeaning
4nVersionTransaction version, little-endian
32inputs rootMerkle root of the inputs, without their scriptSig
1inputs heightNumber of levels of the inputs tree
32outputs rootMerkle root of the outputs
1outputs heightNumber of levels of the outputs tree
4nLockTimeLock time, little-endian

Leaves #

TreeLeaf
Inputsdouble SHA-256 of prevout (32-byte txid, 4-byte index) || nSequence (4 bytes)
Outputsdouble SHA-256 of the serialized output: amount (8 bytes) || script length || script

Merkle root and height #

  1. With no leaf, the root is 32 zero bytes and the height is 0.
  2. With one leaf, the root is that leaf and the height is 1.
  3. 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 1 to 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:

C++
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:

IdentifierComputed fromUsed for
Transaction id (txid)The preimage aboveOutpoints: an input names the txid and output index it spends.
Transaction hashDouble SHA-256 of the whole serialized transaction, scriptSig includedCommitting 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.

Source share/www/docs/txid.md in the lotusd repository
Checked against src/primitives/transaction.cpp src/consensus/merkle.cpp