TeachMeBitcoin

The Draftsmanship of Truth: Building the Transaction Template

From TeachMeBitcoin, the free encyclopedia Reading time: 4 min

16. The Draftsmanship of Truth: Building the Transaction Template

Before a transaction can be signed and sent to the world, it must be "Drafted." This is the process of building the Transaction Template. This template is a raw, unsigned data structure that defines the "Inputs" (where the money is coming from) and the "Outputs" (where the money is going). In Bitcoin Core, this "Drafting" is the final act of the wallet's logic before it hands the "Message" over to the "Signer." It is the "Scripting of the Truth."

For the Sovereign Architect, the "Draft" is the "Final Blueprint" of your intent. It is your last chance to review the "Total Cost," the "Recipient Address," and the "Privacy Implications" of your payment. In the source code, this is handled by the CreateTransaction function in src/wallet/spend.cpp. This function is the "Architect's Desk," the place where all the different "Plans" (Inputs, Fees, Change) are combined into a single, coherent document.

Analyzing the Architect's Desk: CreateTransaction

In the source code, we see the "Master Loop" of transaction creation. It is a "Refinement Process" that may run several times to find the perfect balance of fees and amounts.

/**
 * This is the master function for building an unsigned transaction draft.
 */
bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, ...)
{
    // 1. Initial "Safety Audit." Are the amounts valid? Is the wallet unlocked?
    if (nTargetValue <= 0) return false;

    // 2. The "Selection Loop." We try to pick coins and calculate fees.
    // If the fee changes, the selection might change, so we loop up to 100 times.
    for (int nTry = 0; nTry < 100; nTry++) {

        // 3. We invoke "Coin Selection" (BnB, Knapsack, etc.).
        auto result = AttemptSelection(...);

        // 4. We calculate the "Draft Fee" based on the size of the selected coins.
        CAmount nFeeRet = CalculateFee(result.GetWeight());

        // 5. If the selection is sufficient to cover the target AND the fee... SUCCESS!
        if (result.TotalValue() >= nTargetValue + nFeeRet) {
            // We have a "Steady State" draft.
            break;
        }
    }

    // 6. Finalize the draft by adding the Change output and sorting the inputs (BIP 69).
    // ...
}

Explaining the Draft: The Legal Document

The "Partially Signed" Future: PSBTs

In modern Bitcoin, the "Draft" doesn't have to be signed immediately. You can export the unsigned draft as a Partially Signed Bitcoin Transaction (PSBT). This allows you to "Review the Blueprint" on a separate, air-gapped computer or to send it to a hardware wallet. This "Separation of Powers"—where one machine drafts the truth and another machine vests the truth—is the ultimate security architecture. You are the "Draftsman of the Ledger," creating the "Plan of Prosperity."


☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!