Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
transaction_utils.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <coins.h>
9
11{
13 txCredit.version = 1;
14 txCredit.nLockTime = 0;
15 txCredit.vin = {
17 };
18 txCredit.vout = {
19 CTxOut{nValue, scriptPubKey},
20 };
21
22 return txCredit;
23}
24
26{
28 txSpend.version = 1;
29 txSpend.nLockTime = 0;
30 txSpend.vin = {
31 CTxIn{COutPoint{txCredit.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL},
32 };
33 txSpend.vin[0].scriptWitness = scriptWitness;
34 txSpend.vout = {
35 CTxOut{txCredit.vout[0].nValue, CScript{}},
36 };
37
38 return txSpend;
39}
40
41std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet, const std::array<CAmount,4>& nValues)
42{
43 std::vector<CMutableTransaction> dummyTransactions;
45
46 // Add some keys to the keystore:
47 CKey key[4];
48 for (int i = 0; i < 4; i++) {
49 key[i].MakeNewKey(i % 2);
50 keystoreRet.AddKey(key[i]);
51 }
52
53 // Create some dummy input transactions
54 dummyTransactions[0].vout = {
57 };
59
60 dummyTransactions[1].vout = {
63 };
65
66 return dummyTransactions;
67}
68
70{
71 tx.vout.emplace_back(0, CScript() << OP_RETURN);
74
75 // determine number of needed padding bytes by converting weight difference to vbytes
77 // compensate for the increase of the compact-size encoded script length
78 // (note that the length encoding of the unpadded output script needs one byte)
80
81 // pad transaction by repeatedly appending a dummy opcode to the output script
82 tx.vout[0].scriptPubKey.insert(tx.vout[0].scriptPubKey.end(), dummy_vbytes, OP_1);
83
84 // actual weight should be at most 3 higher than target weight
87}
88
89bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType, SignatureData& sig_data)
90{
91 assert(nIn < txTo.vin.size());
92
93 MutableTransactionSignatureCreator creator(txTo, nIn, amount, {.sighash_type = nHashType});
94
96 UpdateInput(txTo.vin.at(nIn), sig_data);
97 return ret;
98}
99
101{
102 assert(nIn < txTo.vin.size());
103 const CTxIn& txin = txTo.vin[nIn];
104 assert(txin.prevout.n < txFrom.vout.size());
105 const CTxOut& txout = txFrom.vout[txin.prevout.n];
106
107 return SignSignature(provider, txout.scriptPubKey, txTo, nIn, txout.nValue, nHashType, sig_data);
108}
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
int64_t CAmount
Amount in satoshis (Can be negative)
Definition amount.h:12
int ret
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition coins.h:437
An encapsulated private key.
Definition key.h:46
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition key.cpp:163
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:30
Serialized script, used inside transaction inputs and outputs.
Definition script.h:406
The basic transaction that is broadcasted on the network and contained in blocks.
An input of a transaction.
Definition transaction.h:63
static constexpr uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime/IsFinalTx().
Definition transaction.h:77
An output of a transaction.
CScript scriptPubKey
CAmount nValue
Fillable signing provider that keeps keys in an address->secret map.
A signature creator for transactions.
Definition sign.h:54
An interface to be implemented by keystores that support signing.
void resize(size_type new_size)
Definition prevector.h:276
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
Definition coins.cpp:133
static int32_t GetTransactionWeight(const CTransaction &tx)
Definition validation.h:139
constexpr int WITNESS_SCALE_FACTOR
Definition consensus.h:21
@ OP_CHECKSIG
Definition script.h:191
@ OP_1
Definition script.h:84
@ OP_RETURN
Definition script.h:112
std::vector< unsigned char > ToByteVector(const T &in)
Definition script.h:68
constexpr unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 byt...
Definition serialize.h:291
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
Definition sign.cpp:750
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition sign.cpp:923
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition sign.cpp:243
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< CTxIn > vin
FuzzedDataProvider provider
CMutableTransaction BuildSpendingTransaction(const CScript &scriptSig, const CScriptWitness &scriptWitness, const CTransaction &txCredit)
void BulkTransaction(CMutableTransaction &tx, int32_t target_weight)
CMutableTransaction BuildCreditingTransaction(const CScript &scriptPubKey, CAmount nValue)
std::vector< CMutableTransaction > SetupDummyInputs(FillableSigningProvider &keystoreRet, CCoinsViewCache &coinsRet, const std::array< CAmount, 4 > &nValues)
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType, SignatureData &sig_data)
Produce a satisfying script (scriptSig or witness).
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())