Merge #11590: [Wallet] always show help-line of wallet encryption calls
[bitcoinplatinum.git] / src / primitives / transaction.cpp
blobe0a106adb90bfbce7cfd45bc9249c383c9ee3505
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "primitives/transaction.h"
8 #include "hash.h"
9 #include "tinyformat.h"
10 #include "utilstrencodings.h"
12 std::string COutPoint::ToString() const
14 return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
17 CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
19 prevout = prevoutIn;
20 scriptSig = scriptSigIn;
21 nSequence = nSequenceIn;
24 CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
26 prevout = COutPoint(hashPrevTx, nOut);
27 scriptSig = scriptSigIn;
28 nSequence = nSequenceIn;
31 std::string CTxIn::ToString() const
33 std::string str;
34 str += "CTxIn(";
35 str += prevout.ToString();
36 if (prevout.IsNull())
37 str += strprintf(", coinbase %s", HexStr(scriptSig));
38 else
39 str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
40 if (nSequence != SEQUENCE_FINAL)
41 str += strprintf(", nSequence=%u", nSequence);
42 str += ")";
43 return str;
46 CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
48 nValue = nValueIn;
49 scriptPubKey = scriptPubKeyIn;
52 std::string CTxOut::ToString() const
54 return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
57 CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
58 CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}
60 uint256 CMutableTransaction::GetHash() const
62 return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
65 uint256 CTransaction::ComputeHash() const
67 return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
70 uint256 CTransaction::GetWitnessHash() const
72 if (!HasWitness()) {
73 return GetHash();
75 return SerializeHash(*this, SER_GETHASH, 0);
78 /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
79 CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash() {}
80 CTransaction::CTransaction(const CMutableTransaction &tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash(ComputeHash()) {}
81 CTransaction::CTransaction(CMutableTransaction &&tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash(ComputeHash()) {}
83 CAmount CTransaction::GetValueOut() const
85 CAmount nValueOut = 0;
86 for (const auto& tx_out : vout) {
87 nValueOut += tx_out.nValue;
88 if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut))
89 throw std::runtime_error(std::string(__func__) + ": value out of range");
91 return nValueOut;
94 unsigned int CTransaction::GetTotalSize() const
96 return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
99 std::string CTransaction::ToString() const
101 std::string str;
102 str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
103 GetHash().ToString().substr(0,10),
104 nVersion,
105 vin.size(),
106 vout.size(),
107 nLockTime);
108 for (const auto& tx_in : vin)
109 str += " " + tx_in.ToString() + "\n";
110 for (const auto& tx_in : vin)
111 str += " " + tx_in.scriptWitness.ToString() + "\n";
112 for (const auto& tx_out : vout)
113 str += " " + tx_out.ToString() + "\n";
114 return str;