Bitcoin Core  22.99.0
P2P Digital Currency
transaction_utils.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 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>
8 
9 CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey, int nValue)
10 {
11  CMutableTransaction txCredit;
12  txCredit.nVersion = 1;
13  txCredit.nLockTime = 0;
14  txCredit.vin.resize(1);
15  txCredit.vout.resize(1);
16  txCredit.vin[0].prevout.SetNull();
17  txCredit.vin[0].scriptSig = CScript() << CScriptNum(0) << CScriptNum(0);
18  txCredit.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
19  txCredit.vout[0].scriptPubKey = scriptPubKey;
20  txCredit.vout[0].nValue = nValue;
21 
22  return txCredit;
23 }
24 
25 CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CScriptWitness& scriptWitness, const CTransaction& txCredit)
26 {
27  CMutableTransaction txSpend;
28  txSpend.nVersion = 1;
29  txSpend.nLockTime = 0;
30  txSpend.vin.resize(1);
31  txSpend.vout.resize(1);
32  txSpend.vin[0].scriptWitness = scriptWitness;
33  txSpend.vin[0].prevout.hash = txCredit.GetHash();
34  txSpend.vin[0].prevout.n = 0;
35  txSpend.vin[0].scriptSig = scriptSig;
36  txSpend.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
37  txSpend.vout[0].scriptPubKey = CScript();
38  txSpend.vout[0].nValue = txCredit.vout[0].nValue;
39 
40  return txSpend;
41 }
42 
43 std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet, const std::array<CAmount,4>& nValues)
44 {
45  std::vector<CMutableTransaction> dummyTransactions;
46  dummyTransactions.resize(2);
47 
48  // Add some keys to the keystore:
49  CKey key[4];
50  for (int i = 0; i < 4; i++) {
51  key[i].MakeNewKey(i % 2);
52  keystoreRet.AddKey(key[i]);
53  }
54 
55  // Create some dummy input transactions
56  dummyTransactions[0].vout.resize(2);
57  dummyTransactions[0].vout[0].nValue = nValues[0];
58  dummyTransactions[0].vout[0].scriptPubKey << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
59  dummyTransactions[0].vout[1].nValue = nValues[1];
60  dummyTransactions[0].vout[1].scriptPubKey << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
61  AddCoins(coinsRet, CTransaction(dummyTransactions[0]), 0);
62 
63  dummyTransactions[1].vout.resize(2);
64  dummyTransactions[1].vout[0].nValue = nValues[2];
65  dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(PKHash(key[2].GetPubKey()));
66  dummyTransactions[1].vout[1].nValue = nValues[3];
67  dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(PKHash(key[3].GetPubKey()));
68  AddCoins(coinsRet, CTransaction(dummyTransactions[1]), 0);
69 
70  return dummyTransactions;
71 }
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
CKey::MakeNewKey
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
FillableSigningProvider
Fillable signing provider that keeps keys in an address->secret map.
Definition: signingprovider.h:91
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:351
CMutableTransaction::nVersion
int32_t nVersion
Definition: transaction.h:348
SetupDummyInputs
std::vector< CMutableTransaction > SetupDummyInputs(FillableSigningProvider &keystoreRet, CCoinsViewCache &coinsRet, const std::array< CAmount, 4 > &nValues)
Definition: transaction_utils.cpp:43
FillableSigningProvider::AddKey
virtual bool AddKey(const CKey &key)
Definition: signingprovider.h:152
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
CTxIn::SEQUENCE_FINAL
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:75
CScriptWitness
Definition: script.h:557
CScriptNum
Definition: script.h:219
AddCoins
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:108
signingprovider.h
CMutableTransaction::nLockTime
uint32_t nLockTime
Definition: transaction.h:349
ToByteVector
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:60
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:271
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
coins.h
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
PKHash
Definition: standard.h:79
CKey
An encapsulated private key.
Definition: key.h:26
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
CTransaction::GetHash
const uint256 & GetHash() const
Definition: transaction.h:302
transaction_utils.h
BuildSpendingTransaction
CMutableTransaction BuildSpendingTransaction(const CScript &scriptSig, const CScriptWitness &scriptWitness, const CTransaction &txCredit)
Definition: transaction_utils.cpp:25
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
OP_CHECKSIG
@ OP_CHECKSIG
Definition: script.h:183
GetPubKey
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition: sign.cpp:104
BuildCreditingTransaction
CMutableTransaction BuildCreditingTransaction(const CScript &scriptPubKey, int nValue)
Definition: transaction_utils.cpp:9