PIVX Core  5.6.99
P2P Digital Currency
providertx.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2021 The Dash Core developers
2 // Copyright (c) 2021-2022 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "evo/providertx.h"
7 
8 #include "bls/key_io.h"
9 #include "key_io.h"
10 
11 std::string ProRegPL::MakeSignString() const
12 {
13  std::ostringstream ss;
14 
15  ss << HexStr(scriptPayout) << "|";
16  ss << strprintf("%d", nOperatorReward) << "|";
17  ss << EncodeDestination(keyIDOwner) << "|";
18  ss << EncodeDestination(keyIDVoting) << "|";
19 
20  // ... and also the full hash of the payload as a protection against malleability and replays
21  ss << ::SerializeHash(*this).ToString();
22 
23  return ss.str();
24 }
25 
26 std::string ProRegPL::ToString() const
27 {
28  CTxDestination dest;
29  std::string payee = ExtractDestination(scriptPayout, dest) ?
30  EncodeDestination(dest) : "unknown";
31  return strprintf("ProRegPL(nVersion=%d, collateralOutpoint=%s, addr=%s, nOperatorReward=%f, ownerAddress=%s, operatorPubKey=%s, votingAddress=%s, scriptPayout=%s)",
33 }
34 
35 void ProRegPL::ToJson(UniValue& obj) const
36 {
37  obj.clear();
38  obj.setObject();
39  obj.pushKV("version", nVersion);
40  obj.pushKV("collateralHash", collateralOutpoint.hash.ToString());
41  obj.pushKV("collateralIndex", (int)collateralOutpoint.n);
42  obj.pushKV("service", addr.ToString());
43  obj.pushKV("ownerAddress", EncodeDestination(keyIDOwner));
44  obj.pushKV("operatorPubKey", bls::EncodePublic(Params(), pubKeyOperator));
45  obj.pushKV("votingAddress", EncodeDestination(keyIDVoting));
46 
47  CTxDestination dest1;
48  if (ExtractDestination(scriptPayout, dest1)) {
49  obj.pushKV("payoutAddress", EncodeDestination(dest1));
50  }
51  CTxDestination dest2;
53  obj.pushKV("operatorPayoutAddress", EncodeDestination(dest2));
54  }
55  obj.pushKV("operatorReward", (double)nOperatorReward / 100);
56  obj.pushKV("inputsHash", inputsHash.ToString());
57 }
58 
60 {
62  return state.DoS(100, false, REJECT_INVALID, "bad-protx-version");
63  }
64  if (nType != 0) {
65  return state.DoS(100, false, REJECT_INVALID, "bad-protx-type");
66  }
67  if (nMode != 0) {
68  return state.DoS(100, false, REJECT_INVALID, "bad-protx-mode");
69  }
70 
71  if (keyIDOwner.IsNull() || keyIDVoting.IsNull()) {
72  return state.DoS(10, false, REJECT_INVALID, "bad-protx-key-null");
73  }
74  if (!pubKeyOperator.IsValid()) {
75  return state.DoS(10, false, REJECT_INVALID, "bad-protx-operator-key-invalid");
76  }
77  // we may support other kinds of scripts later, but restrict it for now
79  return state.DoS(10, false, REJECT_INVALID, "bad-protx-payee");
80  }
82  return state.DoS(10, false, REJECT_INVALID, "bad-protx-operator-payee");
83  }
84 
85  CTxDestination payoutDest;
86  if (!ExtractDestination(scriptPayout, payoutDest)) {
87  // should not happen as we checked script types before
88  return state.DoS(10, false, REJECT_INVALID, "bad-protx-payee-dest");
89  }
90  // don't allow reuse of payout key for other keys (don't allow people to put the payee key onto an online server)
91  if (payoutDest == CTxDestination(keyIDOwner) ||
92  payoutDest == CTxDestination(keyIDVoting)) {
93  return state.DoS(10, false, REJECT_INVALID, "bad-protx-payee-reuse");
94  }
95 
96  if (nOperatorReward > 10000) {
97  return state.DoS(10, false, REJECT_INVALID, "bad-protx-operator-reward");
98  }
99  return true;
100 }
101 
102 std::string ProUpServPL::ToString() const
103 {
104  CTxDestination dest;
105  std::string payee = ExtractDestination(scriptOperatorPayout, dest) ?
106  EncodeDestination(dest) : "unknown";
107  return strprintf("ProUpServPL(nVersion=%d, proTxHash=%s, addr=%s, operatorPayoutAddress=%s)",
108  nVersion, proTxHash.ToString(), addr.ToString(), payee);
109 }
110 
112 {
113  obj.clear();
114  obj.setObject();
115  obj.pushKV("version", nVersion);
116  obj.pushKV("proTxHash", proTxHash.ToString());
117  obj.pushKV("service", addr.ToString());
118  CTxDestination dest;
120  obj.pushKV("operatorPayoutAddress", EncodeDestination(dest));
121  }
122  obj.pushKV("inputsHash", inputsHash.ToString());
123 }
124 
126 {
128  return state.DoS(100, false, REJECT_INVALID, "bad-protx-version");
129  }
130  return true;
131 }
132 
133 std::string ProUpRegPL::ToString() const
134 {
135  CTxDestination dest;
136  std::string payee = ExtractDestination(scriptPayout, dest) ?
137  EncodeDestination(dest) : "unknown";
138  return strprintf("ProUpRegPL(nVersion=%d, proTxHash=%s, operatorPubKey=%s, votingAddress=%s, payoutAddress=%s)",
140 }
141 
142 void ProUpRegPL::ToJson(UniValue& obj) const
143 {
144  obj.clear();
145  obj.setObject();
146  obj.pushKV("version", nVersion);
147  obj.pushKV("proTxHash", proTxHash.ToString());
148  obj.pushKV("votingAddress", EncodeDestination(keyIDVoting));
149  CTxDestination dest;
150  if (ExtractDestination(scriptPayout, dest)) {
151  obj.pushKV("payoutAddress", EncodeDestination(dest));
152  }
153  obj.pushKV("operatorPubKey", bls::EncodePublic(Params(), pubKeyOperator));
154  obj.pushKV("inputsHash", inputsHash.ToString());
155 }
156 
158 {
160  return state.DoS(100, false, REJECT_INVALID, "bad-protx-version");
161  }
162  if (nMode != 0) {
163  return state.DoS(100, false, REJECT_INVALID, "bad-protx-mode");
164  }
165 
166  if (!pubKeyOperator.IsValid()) {
167  return state.DoS(10, false, REJECT_INVALID, "bad-protx-operator-key-invalid");
168  }
169  if (keyIDVoting.IsNull()) {
170  return state.DoS(10, false, REJECT_INVALID, "bad-protx-voting-key-null");
171  }
172  // !TODO: enable other scripts
174  return state.DoS(10, false, REJECT_INVALID, "bad-protx-payee");
175  }
176 
177  return true;
178 }
179 
180 std::string ProUpRevPL::ToString() const
181 {
182  return strprintf("ProUpRevPL(nVersion=%d, proTxHash=%s, nReason=%d)",
184 }
185 
186 void ProUpRevPL::ToJson(UniValue& obj) const
187 {
188  obj.clear();
189  obj.setObject();
190  obj.pushKV("version", nVersion);
191  obj.pushKV("proTxHash", proTxHash.ToString());
192  obj.pushKV("reason", (int)nReason);
193  obj.pushKV("inputsHash", inputsHash.ToString());
194 }
195 
197 {
199  return state.DoS(100, false, REJECT_INVALID, "bad-protx-version");
200  }
201 
202  // pl.nReason < ProUpRevPL::REASON_NOT_SPECIFIED is always `false` since
203  // pl.nReason is unsigned and ProUpRevPL::REASON_NOT_SPECIFIED == 0
205  return state.DoS(100, false, REJECT_INVALID, "bad-protx-reason");
206  }
207  return true;
208 }
209 
211 {
212  if (tx == nullptr) {
213  return false;
214  }
215  if (!tx->IsSpecialTx() || tx->nType != CTransaction::TxType::PROREG) {
216  return false;
217  }
218  ProRegPL pl;
219  if (!GetTxPayload(*tx, pl)) {
220  return false;
221  }
222  outRet = pl.collateralOutpoint.hash.IsNull() ? COutPoint(tx->GetHash(), pl.collateralOutpoint.n)
223  : pl.collateralOutpoint;
224  return true;
225 }
226 
227 
const CChainParams & Params()
Return the currently selected parameters.
uint256 hash
Definition: transaction.h:35
std::string ToStringShort() const
Definition: transaction.cpp:13
uint32_t n
Definition: transaction.h:36
bool IsValid() const
Definition: bls_wrapper.h:87
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:72
bool IsPayToPublicKeyHash() const
Definition: script.cpp:212
std::string ToString() const
Definition: netaddress.cpp:954
Capture information about block/transaction validation.
Definition: validation.h:24
bool DoS(int level, bool ret=false, unsigned int chRejectCodeIn=0, std::string strRejectReasonIn="", bool corruptionIn=false, const std::string &strDebugMessageIn="")
Definition: validation.h:39
static const uint16_t CURRENT_VERSION
Definition: providertx.h:22
std::string ToString() const
Definition: providertx.cpp:26
uint16_t nMode
Definition: providertx.h:28
CScript scriptOperatorPayout
Definition: providertx.h:36
CKeyID keyIDVoting
Definition: providertx.h:33
uint16_t nType
Definition: providertx.h:27
COutPoint collateralOutpoint
Definition: providertx.h:29
uint16_t nVersion
Definition: providertx.h:26
void ToJson(UniValue &obj) const
Definition: providertx.cpp:35
uint16_t nOperatorReward
Definition: providertx.h:35
CScript scriptPayout
Definition: providertx.h:34
CBLSPublicKey pubKeyOperator
Definition: providertx.h:32
uint256 inputsHash
Definition: providertx.h:37
std::string MakeSignString() const
Definition: providertx.cpp:11
CKeyID keyIDOwner
Definition: providertx.h:31
bool IsTriviallyValid(CValidationState &state) const
Definition: providertx.cpp:59
CService addr
Definition: providertx.h:30
CKeyID keyIDVoting
Definition: providertx.h:113
CScript scriptPayout
Definition: providertx.h:114
static const uint16_t CURRENT_VERSION
Definition: providertx.h:105
std::string ToString() const
Definition: providertx.cpp:133
uint16_t nVersion
Definition: providertx.h:109
uint256 proTxHash
Definition: providertx.h:110
bool IsTriviallyValid(CValidationState &state) const
Definition: providertx.cpp:157
uint16_t nMode
Definition: providertx.h:111
CBLSPublicKey pubKeyOperator
Definition: providertx.h:112
uint256 inputsHash
Definition: providertx.h:115
void ToJson(UniValue &obj) const
Definition: providertx.cpp:142
void ToJson(UniValue &obj) const
Definition: providertx.cpp:186
bool IsTriviallyValid(CValidationState &state) const
Definition: providertx.cpp:196
uint16_t nReason
Definition: providertx.h:152
static const uint16_t CURRENT_VERSION
Definition: providertx.h:137
uint256 inputsHash
Definition: providertx.h:153
uint256 proTxHash
Definition: providertx.h:151
std::string ToString() const
Definition: providertx.cpp:180
uint16_t nVersion
Definition: providertx.h:150
std::string ToString() const
Definition: providertx.cpp:102
uint256 inputsHash
Definition: providertx.h:83
uint16_t nVersion
Definition: providertx.h:79
CService addr
Definition: providertx.h:81
static const uint16_t CURRENT_VERSION
Definition: providertx.h:75
uint256 proTxHash
Definition: providertx.h:80
void ToJson(UniValue &obj) const
Definition: providertx.cpp:111
CScript scriptOperatorPayout
Definition: providertx.h:82
bool IsTriviallyValid(CValidationState &state) const
Definition: providertx.cpp:125
bool setObject()
Definition: univalue.cpp:101
void clear()
Definition: univalue.cpp:15
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
std::string ToString() const
Definition: uint256.cpp:65
bool IsNull() const
Definition: uint256.h:36
bool empty() const
Definition: prevector.h:281
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:289
std::string EncodeDestination(const CWDestination &address, const CChainParams::Base58Type addrType)
std::string EncodePublic(const CChainParams &params, const CBLSPublicKey &pk)
Definition: key_io.cpp:55
bool GetProRegCollateral(const CTransactionRef &tx, COutPoint &outRet)
Definition: providertx.cpp:210
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet, bool fColdStake)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:162
boost::variant< CNoDestination, CKeyID, CScriptID, CExchangeKeyID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:72
#define strprintf
Definition: tinyformat.h:1056
bool GetTxPayload(const std::vector< unsigned char > &payload, T &obj)
Definition: transaction.h:464
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:456
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.