PIVX Core  5.6.99
P2P Digital Currency
sapling_operation.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 The PIVX Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef PIVX_SAPLING_SAPLING_OPERATION_H
6 #define PIVX_SAPLING_SAPLING_OPERATION_H
7 
8 #include "amount.h"
10 #include "operationresult.h"
11 #include "primitives/transaction.h"
12 #include "wallet/wallet.h"
13 
14 class CCoinControl;
15 struct TxValues;
16 
17 class ShieldedRecipient final : public CRecipientBase
18 {
19 public:
21  const std::string memo;
22  ShieldedRecipient(const libzcash::SaplingPaymentAddress& _address, const CAmount& _amount, const std::string& _memo, bool _fSubtractFeeFromAmount) :
23  CRecipientBase(_amount, _fSubtractFeeFromAmount), address(_address), memo(_memo) {}
24  bool isTransparent() const override { return false; }
26  std::string getMemo() const override { return memo; }
27 };
28 
30 {
31  const std::shared_ptr<CRecipientBase> recipient;
32 
33  bool IsTransparent() const { return recipient->isTransparent(); }
34  bool IsSubtractFee() const { return recipient->fSubtractFeeFromAmount; }
35  CAmount getAmount() const { return recipient->nAmount; };
36  CScript getScript() const { assert(IsTransparent()); return *recipient->getScript(); }
37  libzcash::SaplingPaymentAddress getSapPaymentAddr() const { assert(!IsTransparent()); return *recipient->getSapPaymentAddr(); }
38  std::string getMemo() const { return recipient->getMemo(); }
39 
40  // Prevent default empty initialization
41  SendManyRecipient() = delete;
42 
43  // Shielded recipient
44  SendManyRecipient(const libzcash::SaplingPaymentAddress& address, const CAmount& amount, const std::string& memo, bool fSubtractFeeFromAmount):
45  recipient(new ShieldedRecipient(address, amount, memo, fSubtractFeeFromAmount))
46  {}
47 
48  // Transparent recipient: P2PKH
49  SendManyRecipient(const CTxDestination& dest, const CAmount& amount, bool fSubtractFeeFromAmount):
50  recipient(new CRecipient(GetScriptForDestination(dest), amount, fSubtractFeeFromAmount))
51  {}
52 
53  // Transparent recipient: P2CS
54  SendManyRecipient(const CKeyID& ownerKey, const CKeyID& stakerKey, const CAmount& amount, bool fV6Enforced):
55  recipient(new CRecipient(fV6Enforced ? GetScriptForStakeDelegation(stakerKey, ownerKey)
56  : GetScriptForStakeDelegationLOF(stakerKey, ownerKey), amount, false))
57  {}
58 
59  // Transparent recipient: multisig
60  SendManyRecipient(int nRequired, const std::vector<CPubKey>& keys, const CAmount& amount):
61  recipient(new CRecipient(GetScriptForMultisig(nRequired, keys), amount, false))
62  {}
63 
64  // Transparent recipient: OP_RETURN
65  explicit SendManyRecipient(const uint256& message):
67  {}
68 };
69 
70 class FromAddress {
71 public:
72  explicit FromAddress() {};
73  explicit FromAddress(const CTxDestination& _fromTaddr) : fromTaddr(_fromTaddr) {};
74  explicit FromAddress(const libzcash::SaplingPaymentAddress& _fromSapaddr) : fromSapAddr(_fromSapaddr) {};
75 
76  bool isFromTAddress() const { return IsValidDestination(fromTaddr); }
77  bool isFromSapAddress() const { return fromSapAddr.is_initialized(); }
78 
81 };
82 
84 public:
85  explicit SaplingOperation(const Consensus::Params& consensusParams, CWallet* _wallet);
87 
89  OperationResult send(std::string& retTxHash);
90  OperationResult buildAndSend(std::string& retTxHash);
91 
92  void setFromAddress(const CTxDestination&);
94  void clearTx() { txBuilder.Clear(); }
95  // In case of no addressFrom filter selected, it will accept any utxo in the wallet as input.
96  SaplingOperation* setSelectTransparentCoins(const bool select, const bool _fIncludeDelegated = false);
97  SaplingOperation* setSelectShieldedCoins(const bool select) { selectFromShield = select; return this; };
98  SaplingOperation* setRecipients(std::vector<SendManyRecipient>& vec) { recipients = std::move(vec); return this; };
99  SaplingOperation* setFee(CAmount _fee) { fee = _fee; return this; }
100  SaplingOperation* setMinDepth(int _mindepth) { assert(_mindepth >= 0); mindepth = _mindepth; return this; }
101  SaplingOperation* setTransparentKeyChange(CReserveKey* reserveKey) { tkeyChange = reserveKey; return this; }
102  SaplingOperation* setCoinControl(const CCoinControl* _coinControl) { coinControl = _coinControl; return this; }
103 
104  CAmount getFee() { return fee; }
107 
108 private:
109  /*
110  * Cannot be nullptr. A pointer to the wallet, used to retrieve the inputs to spend, the keys to create the outputs,
111  * sapling notes and nullifiers, as well as to commit transactions.
112  * The same keystore is passed to the transaction builder in order to produce the required signatures.
113  */
114  CWallet* wallet{nullptr};
115 
117  // In case of no addressFrom filter selected, it will accept any utxo in the wallet as input.
118  bool selectFromtaddrs{false};
119  bool selectFromShield{false};
120  bool fIncludeDelegated{false};
121  const CCoinControl* coinControl{nullptr};
122  std::vector<SendManyRecipient> recipients;
123  std::vector<COutput> transInputs;
124  std::vector<SaplingNoteEntry> shieldedInputs;
125  int mindepth{5}; // Min default depth 5.
126  CAmount fee{0}; // User selected fee.
127 
128  // transparent change
130 
131  // Builder
134 
136  OperationResult loadUtxos(TxValues& txValues, const std::vector<COutput>& selectedUTXO, const CAmount selectedUTXOAmount);
138  OperationResult checkTxValues(TxValues& txValues, bool isFromtAddress, bool isFromShielded);
139 };
140 
141 OperationResult GetMemoFromString(const std::string& s, std::array<unsigned char, ZC_MEMO_SIZE>& memoRet);
142 
143 OperationResult CheckTransactionSize(std::vector<SendManyRecipient>& recipients, bool fromTaddr);
144 
145 #endif // PIVX_SAPLING_SAPLING_OPERATION_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
false
Definition: bls_dkg.cpp:151
Coin Control Features.
Definition: coincontrol.h:34
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
A key allocated from the key pool.
Definition: wallet.h:1256
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:577
FromAddress(const CTxDestination &_fromTaddr)
CTxDestination fromTaddr
FromAddress(const libzcash::SaplingPaymentAddress &_fromSapaddr)
bool isFromTAddress() const
bool isFromSapAddress() const
Optional< libzcash::SaplingPaymentAddress > fromSapAddr
std::vector< SaplingNoteEntry > shieldedInputs
FromAddress fromAddress
CTransactionRef finalTx
const CCoinControl * coinControl
SaplingOperation(const Consensus::Params &consensusParams, CWallet *_wallet)
CTransaction getFinalTx()
TransactionBuilder txBuilder
OperationResult loadUnspentNotes(TxValues &txValues, uint256 &ovk)
CReserveKey * tkeyChange
OperationResult loadUtxos(TxValues &values)
OperationResult checkTxValues(TxValues &txValues, bool isFromtAddress, bool isFromShielded)
SaplingOperation * setSelectShieldedCoins(const bool select)
SaplingOperation * setCoinControl(const CCoinControl *_coinControl)
void setFromAddress(const CTxDestination &)
std::vector< COutput > transInputs
OperationResult send(std::string &retTxHash)
SaplingOperation * setRecipients(std::vector< SendManyRecipient > &vec)
SaplingOperation * setMinDepth(int _mindepth)
OperationResult build()
std::vector< SendManyRecipient > recipients
OperationResult buildAndSend(std::string &retTxHash)
SaplingOperation * setFee(CAmount _fee)
SaplingOperation * setSelectTransparentCoins(const bool select, const bool _fIncludeDelegated=false)
CTransactionRef getFinalTxRef()
SaplingOperation * setTransparentKeyChange(CReserveKey *reserveKey)
std::string getMemo() const override
bool isTransparent() const override
const std::string memo
ShieldedRecipient(const libzcash::SaplingPaymentAddress &_address, const CAmount &_amount, const std::string &_memo, bool _fSubtractFeeFromAmount)
const libzcash::SaplingPaymentAddress address
Optional< libzcash::SaplingPaymentAddress > getSapPaymentAddr() const override
Sapling functions.
Definition: address.h:30
256-bit opaque blob.
Definition: uint256.h:138
bool IsValidDestination(const CWDestination &address)
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:12
@ ownerKey
Definition: rpcevo.cpp:49
OperationResult GetMemoFromString(const std::string &s, std::array< unsigned char, ZC_MEMO_SIZE > &memoRet)
OperationResult CheckTransactionSize(std::vector< SendManyRecipient > &recipients, bool fromTaddr)
CScript GetScriptForOpReturn(const uint256 &message)
Generate an OP_RETURN output script with the given data.
Definition: standard.cpp:322
CScript GetScriptForStakeDelegation(const CKeyID &stakingKey, const CKeyID &spendingKey)
Generate a P2CS script for the given staker and owner keys.
Definition: standard.cpp:291
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:311
CScript GetScriptForStakeDelegationLOF(const CKeyID &stakingKey, const CKeyID &spendingKey)
Definition: standard.cpp:301
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a PIVX scriptPubKey for the given CTxDestination.
Definition: standard.cpp:278
boost::variant< CNoDestination, CKeyID, CScriptID, CExchangeKeyID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:72
Parameters that influence chain consensus.
Definition: params.h:171
std::string getMemo() const
libzcash::SaplingPaymentAddress getSapPaymentAddr() const
CAmount getAmount() const
CScript getScript() const
SendManyRecipient(const CTxDestination &dest, const CAmount &amount, bool fSubtractFeeFromAmount)
SendManyRecipient(int nRequired, const std::vector< CPubKey > &keys, const CAmount &amount)
bool IsSubtractFee() const
bool IsTransparent() const
const std::shared_ptr< CRecipientBase > recipient
SendManyRecipient(const libzcash::SaplingPaymentAddress &address, const CAmount &amount, const std::string &memo, bool fSubtractFeeFromAmount)
SendManyRecipient(const CKeyID &ownerKey, const CKeyID &stakerKey, const CAmount &amount, bool fV6Enforced)
SendManyRecipient(const uint256 &message)
SendManyRecipient()=delete
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:456