The Draftsmanship of Truth: Building the Transaction Template
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
-
vecSend(The Recipients): This is the "Intent" of the payment. It is a list of "Who gets what." You can send to one person or to a hundred people in a single transaction (Batching).CreateTransactiontakes this "List of Names" and turns it into the "Outputs" of the draft. It is the "Master Intent" of the bank. -
"The Loop" (The 100 Tries): Wait, why does it loop 100 times? Because there is a "Circular Dependency." The "Fee" depends on the "Size" of the transaction. But the "Size" depends on how many "Coins" you pick. And if you pick more coins to pay a higher fee, the "Size" goes up again! The computer "Iterates" through this puzzle until it finds a "Steady State" where the math is perfect. It is the "Calibration of the Draft."
-
AttemptSelection: This is where the strategies we studied earlier (BnB, Knapsack) are actually called into action.CreateTransactiondoes not care how the coins are picked; it just asks the "Selector" for the best result. This modularity is what allows Bitcoin Core to be so flexible. It is the "Separation of Concerns."
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."
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: