Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <coins.h>6#include <consensus/validation.h>7#include <script/signingprovider.h>8#include <test/util/transaction_utils.h>10CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey, CAmount nValue)11{12CMutableTransaction txCredit;13txCredit.version = 1;14txCredit.nLockTime = 0;15txCredit.vin.resize(1);16txCredit.vout.resize(1);17txCredit.vin[0].prevout.SetNull();18txCredit.vin[0].scriptSig = CScript() << CScriptNum(0) << CScriptNum(0);19txCredit.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;20txCredit.vout[0].scriptPubKey = scriptPubKey;21txCredit.vout[0].nValue = nValue;23return txCredit;24}26CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CScriptWitness& scriptWitness, const CTransaction& txCredit)27{28CMutableTransaction txSpend;29txSpend.version = 1;30txSpend.nLockTime = 0;31txSpend.vin.resize(1);32txSpend.vout.resize(1);33txSpend.vin[0].scriptWitness = scriptWitness;34txSpend.vin[0].prevout.hash = txCredit.GetHash();35txSpend.vin[0].prevout.n = 0;36txSpend.vin[0].scriptSig = scriptSig;37txSpend.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;38txSpend.vout[0].scriptPubKey = CScript();39txSpend.vout[0].nValue = txCredit.vout[0].nValue;41return txSpend;42}44std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet, const std::array<CAmount,4>& nValues)45{46std::vector<CMutableTransaction> dummyTransactions;47dummyTransactions.resize(2);49// Add some keys to the keystore:50CKey key[4];51for (int i = 0; i < 4; i++) {52key[i].MakeNewKey(i % 2);53keystoreRet.AddKey(key[i]);54}56// Create some dummy input transactions57dummyTransactions[0].vout.resize(2);58dummyTransactions[0].vout[0].nValue = nValues[0];59dummyTransactions[0].vout[0].scriptPubKey << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;60dummyTransactions[0].vout[1].nValue = nValues[1];61dummyTransactions[0].vout[1].scriptPubKey << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;62AddCoins(coinsRet, CTransaction(dummyTransactions[0]), 0);64dummyTransactions[1].vout.resize(2);65dummyTransactions[1].vout[0].nValue = nValues[2];66dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(PKHash(key[2].GetPubKey()));67dummyTransactions[1].vout[1].nValue = nValues[3];68dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(PKHash(key[3].GetPubKey()));69AddCoins(coinsRet, CTransaction(dummyTransactions[1]), 0);71return dummyTransactions;72}74void BulkTransaction(CMutableTransaction& tx, int32_t target_weight)75{76tx.vout.emplace_back(0, CScript() << OP_RETURN);77auto unpadded_weight{GetTransactionWeight(CTransaction(tx))};78assert(target_weight >= unpadded_weight);80// determine number of needed padding bytes by converting weight difference to vbytes81auto dummy_vbytes = (target_weight - unpadded_weight + (WITNESS_SCALE_FACTOR - 1)) / WITNESS_SCALE_FACTOR;82// compensate for the increase of the compact-size encoded script length83// (note that the length encoding of the unpadded output script needs one byte)84dummy_vbytes -= GetSizeOfCompactSize(dummy_vbytes) - 1;86// pad transaction by repeatedly appending a dummy opcode to the output script87tx.vout[0].scriptPubKey.insert(tx.vout[0].scriptPubKey.end(), dummy_vbytes, OP_1);89// actual weight should be at most 3 higher than target weight90assert(GetTransactionWeight(CTransaction(tx)) >= target_weight);91assert(GetTransactionWeight(CTransaction(tx)) <= target_weight + 3);92}94bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType, SignatureData& sig_data)95{96assert(nIn < txTo.vin.size());98MutableTransactionSignatureCreator creator(txTo, nIn, amount, {.sighash_type = nHashType});100bool ret = ProduceSignature(provider, creator, fromPubKey, sig_data);101UpdateInput(txTo.vin.at(nIn), sig_data);102return ret;103}105bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType, SignatureData& sig_data)106{107assert(nIn < txTo.vin.size());108const CTxIn& txin = txTo.vin[nIn];109assert(txin.prevout.n < txFrom.vout.size());110const CTxOut& txout = txFrom.vout[txin.prevout.n];112return SignSignature(provider, txout.scriptPubKey, txTo, nIn, txout.nValue, nHashType, sig_data);113}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.