forked from syscoin/syscoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcevo.cpp
1109 lines (960 loc) · 54.8 KB
/
rpcevo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018-2020 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <consensus/validation.h>
#include <core_io.h>
#include <init.h>
#include <messagesigner.h>
#include <rpc/server.h>
#include <util/moneystr.h>
#include <validation.h>
#include <wallet/coincontrol.h>
#include <wallet/spend.h>
#include <wallet/rpcwallet.h>
#include <netbase.h>
#include <evo/specialtx.h>
#include <evo/providertx.h>
#include <evo/deterministicmns.h>
#include <evo/simplifiedmns.h>
#include <bls/bls.h>
#include <masternode/masternodemeta.h>
#include <rpc/util.h>
#include <rpc/blockchain.h>
#include <util/translation.h>
#include <node/context.h>
#include <node/transaction.h>
static CKeyID ParsePubKeyIDFromAddress(const std::string& strAddress, const std::string& paramName)
{
CTxDestination dest = DecodeDestination(strAddress);
const WitnessV0KeyHash *keyID = std::get_if<WitnessV0KeyHash>(&dest);
if (!keyID) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be a valid P2PWKH address, not %s", paramName, strAddress));
}
return ToKeyID(*keyID);
}
static CBLSPublicKey ParseBLSPubKey(const std::string& hexKey, const std::string& paramName)
{
CBLSPublicKey pubKey;
if (!pubKey.SetHexStr(hexKey)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be a valid BLS public key, not %s", paramName, hexKey));
}
return pubKey;
}
static CBLSSecretKey ParseBLSSecretKey(const std::string& hexKey, const std::string& paramName)
{
CBLSSecretKey secKey;
if (!secKey.SetHexStr(hexKey)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be a valid BLS secret key", paramName));
}
return secKey;
}
template<typename SpecialTxPayload>
static void FundSpecialTx(CWallet& pwallet, CMutableTransaction& tx, const SpecialTxPayload& payload, const CTxDestination& fundDest)
{
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet.BlockUntilSyncedToCurrentChain();
{
LOCK(pwallet.cs_wallet);
CTxDestination nodest = CNoDestination();
if (fundDest == nodest) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "No source of funds specified");
}
SetTxPayload(tx, payload);
std::vector<CRecipient> vecSend;
for (const auto& txOut : tx.vout) {
CRecipient recipient = {txOut.scriptPubKey, txOut.nValue, false};
vecSend.push_back(recipient);
}
CCoinControl coinControl;
coinControl.destChange = fundDest;
std::vector<COutput> vecOutputs;
AvailableCoins(pwallet, vecOutputs);
for (const auto& out : vecOutputs) {
CTxDestination txDest;
if (ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, txDest) && txDest == fundDest) {
coinControl.Select(COutPoint(out.tx->tx->GetHash(), out.i));
}
}
if (!coinControl.HasSelected()) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "No funds at specified address");
}
CAmount nFeeRequired = 0;
int nChangePosRet = -1;
bilingual_str error;
CTransactionRef wtx;
FeeCalculation fee_calc_out;
bool fCreated = CreateTransaction(pwallet, vecSend, wtx, nFeeRequired, nChangePosRet, error, coinControl, fee_calc_out);
if (!fCreated)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, error.original);
tx.vin = wtx->vin;
tx.vout = wtx->vout;
}
}
template<typename SpecialTxPayload>
static void UpdateSpecialTxInputsHash(const CMutableTransaction& tx, SpecialTxPayload& payload)
{
payload.inputsHash = CalcTxInputsHash(CTransaction(tx));
}
template<typename SpecialTxPayload>
static void SignSpecialTxPayloadByHash(const CMutableTransaction& tx, SpecialTxPayload& payload, const CKey& key)
{
UpdateSpecialTxInputsHash(tx, payload);
payload.vchSig.clear();
uint256 hash = ::SerializeHash(payload);
if (!CHashSigner::SignHash(hash, key, payload.vchSig)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "failed to sign special tx");
}
}
template<typename SpecialTxPayload>
static void SignSpecialTxPayloadByString(const CMutableTransaction& tx, SpecialTxPayload& payload, const CKey& key)
{
UpdateSpecialTxInputsHash(tx, payload);
payload.vchSig.clear();
std::string m = payload.MakeSignString();
if (!CMessageSigner::SignMessage(m, payload.vchSig, key)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "failed to sign special tx");
}
}
template<typename SpecialTxPayload>
static void SignSpecialTxPayloadByHash(const CMutableTransaction& tx, SpecialTxPayload& payload, const CBLSSecretKey& key)
{
UpdateSpecialTxInputsHash(tx, payload);
uint256 hash = ::SerializeHash(payload);
payload.sig = key.Sign(hash);
}
static UniValue SignAndSendSpecialTx(const JSONRPCRequest& request, const CWallet& pwallet, const CMutableTransaction& tx, bool fSubmit = true)
{
CDataStream ds(SER_NETWORK, PROTOCOL_VERSION);
ds << tx;
JSONRPCRequest signRequest;
signRequest.context = request.context;
signRequest.URI = request.URI;
signRequest.params.setArray();
signRequest.params.push_back(HexStr(ds));
UniValue signResult = signrawtransactionwithwallet().HandleRequest(signRequest);
if (!fSubmit) {
return signResult["hex"].get_str();
}
CMutableTransaction mtx;
if(!DecodeHexTx(mtx, signResult["hex"].get_str())) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
}
CTransactionRef txRef(MakeTransactionRef(std::move(mtx)));
int64_t virtual_size = GetVirtualTransactionSize(*txRef);
CAmount max_raw_tx_fee = DEFAULT_MAX_RAW_TX_FEE_RATE.GetFee(virtual_size);
std::string err_string;
if (!pwallet.chain().broadcastTransaction(txRef, max_raw_tx_fee, true, err_string)) {
throw JSONRPCError(RPC_WALLET_ERROR, err_string);
}
return txRef->GetHash().GetHex();
}
// handles register, register_prepare and register_fund
static RPCHelpMan protx_register()
{
return RPCHelpMan{"protx_register",
"\nSame as \"protx_register_fund\", but with an externally referenced collateral.\n"
"The collateral is specified through \"collateralHash\" and \"collateralIndex\" and must be an unspent\n"
"transaction output spendable by this wallet. It must also not be used by any other masternode.\n",
{
{"collateralHash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The collateral transaction hash."},
{"collateralIndex", RPCArg::Type::NUM, RPCArg::Optional::NO, "The collateral transaction output index."},
{"ipAndPort", RPCArg::Type::STR, RPCArg::Optional::NO, "IP and port in the form \"IP:PORT\".\n"
"Must be unique on the network. Can be set to 0, which will require a ProUpServTx afterwards."},
{"ownerAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The Syscoin address to use for payee updates and proposal voting.\n"
"The corresponding private key does not have to be known by your wallet.\n"
"The address must be unused and must differ from the collateralAddress."},
{"operatorPubKey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The operator BLS public key. The BLS private key does not have to be known.\n"
"It has to match the BLS private key which is later used when operating the masternode."},
{"votingAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The voting key address. The private key does not have to be known by your wallet.\n"
"It has to match the private key which is later used when voting on proposals.\n"
"If set to an empty string, ownerAddress will be used.\n"},
{"operatorReward", RPCArg::Type::NUM, RPCArg::Optional::NO, "The fraction in %% to share with the operator. The value must be\n"
"between 0.00 and 100.00."},
{"payoutAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The Syscoin address to use for masternode reward payments."},
{"fundAddress", RPCArg::Type::STR, RPCArg::Default{""}, "If specified wallet will only use coins from this address to fund ProTx.\n"
"If not specified, payoutAddress is the one that is going to be used.\n"
"The private key belonging to this address must be known in your wallet."},
{"submit", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, "If true, the resulting transaction is sent to the network."},
},
RPCResult{RPCResult::Type::STR_HEX, "", "The transaction hash in hex"},
RPCExamples{
HelpExampleCli("protx_register", "1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d 0 173.249.49.9:18369 tsys1q2j57a4rtserh9022a63pvk3jqmg7un55stux0v 003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63 tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r 5 tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r")
+ HelpExampleRpc("protx_register", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\", 0, \"173.249.49.9:18369\", \"tsys1q2j57a4rtserh9022a63pvk3jqmg7un55stux0v\", \"003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63\", \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\", 5, \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\"")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
if (!pwallet) return NullUniValue;
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet->BlockUntilSyncedToCurrentChain();
EnsureWalletIsUnlocked(*pwallet);
size_t paramIdx = 0;
CMutableTransaction tx;
tx.nVersion = SYSCOIN_TX_VERSION_MN_REGISTER;
CProRegTx ptx;
ptx.nVersion = CProRegTx::CURRENT_VERSION;
uint256 collateralHash = ParseHashV(request.params[paramIdx], "collateralHash");
int32_t collateralIndex = request.params[paramIdx + 1].get_int();
if (collateralHash.IsNull() || collateralIndex < 0) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid hash or index: %s-%d", collateralHash.ToString(), collateralIndex));
}
ptx.collateralOutpoint = COutPoint(collateralHash, (uint32_t)collateralIndex);
paramIdx += 2;
CTxDestination fundDest;
{
// TODO unlock on failure
LOCK(pwallet->cs_wallet);
pwallet->LockCoin(ptx.collateralOutpoint);
if (request.params[paramIdx].get_str() != "") {
if (!Lookup(request.params[paramIdx].get_str().c_str(), ptx.addr, Params().GetDefaultPort(), false)) {
throw std::runtime_error(strprintf("invalid network address %s", request.params[paramIdx].get_str()));
}
}
ptx.keyIDOwner = ParsePubKeyIDFromAddress(request.params[paramIdx + 1].get_str(), "owner address");
CBLSPublicKey pubKeyOperator = ParseBLSPubKey(request.params[paramIdx + 2].get_str(), "operator BLS address");
CKeyID keyIDVoting = ptx.keyIDOwner;
if (request.params[paramIdx + 3].get_str() != "") {
keyIDVoting = ParsePubKeyIDFromAddress(request.params[paramIdx + 3].get_str(), "voting address");
}
int64_t operatorReward;
if (!ParseFixedPoint(request.params[paramIdx + 4].getValStr(), 2, &operatorReward)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "operatorReward must be a number");
}
if (operatorReward < 0 || operatorReward > 10000) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "operatorReward must be between 0.00 and 100.00");
}
ptx.nOperatorReward = operatorReward;
CTxDestination payoutDest = DecodeDestination(request.params[paramIdx + 5].get_str());
if (!IsValidDestination(payoutDest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid payout address: %s", request.params[paramIdx + 5].get_str()));
}
ptx.pubKeyOperator = pubKeyOperator;
ptx.keyIDVoting = keyIDVoting;
ptx.scriptPayout = GetScriptForDestination(payoutDest);
// make sure fee calculation works
ptx.vchSig.resize(65);
fundDest = payoutDest;
if (!request.params[paramIdx + 6].isNull()) {
fundDest = DecodeDestination(request.params[paramIdx + 6].get_str());
if (!IsValidDestination(fundDest))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Syscoin address: ") + request.params[paramIdx + 6].get_str());
}
}
bool fSubmit{true};
if (!request.params[paramIdx + 7].isNull()) {
fSubmit = request.params[paramIdx + 7].get_bool();
}
FundSpecialTx(*pwallet, tx, ptx, fundDest);
UpdateSpecialTxInputsHash(tx, ptx);
// referencing external collateral
std::map<COutPoint, Coin> coins;
coins[ptx.collateralOutpoint];
pwallet->chain().findCoins(coins);
const Coin &coin = coins.at(ptx.collateralOutpoint);
if(coin.IsSpent()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral not found: %s", ptx.collateralOutpoint.ToStringShort()));
}
CTxDestination txDest;
ExtractDestination(coin.out.scriptPubKey, txDest);
CKeyID keyID;
if (auto witness_id = std::get_if<WitnessV0KeyHash>(&txDest)) {
keyID = ToKeyID(*witness_id);
}
else if (auto key_id = std::get_if<PKHash>(&txDest)) {
keyID = ToKeyID(*key_id);
}
if (keyID.IsNull()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral type not supported: %s", ptx.collateralOutpoint.ToStringShort()));
}
CKey key;
{
// lets prove we own the collateral
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
if (!spk_man.GetKey(keyID, key)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral key not in wallet: %s", EncodeDestination(txDest)));
}
}
SignSpecialTxPayloadByString(tx, ptx, key);
SetTxPayload(tx, ptx);
return SignAndSendSpecialTx(request, *pwallet, tx, fSubmit);
},
};
}
static RPCHelpMan protx_register_fund()
{
return RPCHelpMan{"protx_register_fund",
"\nCreates, funds and sends a ProTx to the network. The resulting transaction will move 100000 Syscoin\n"
"to the address specified by collateralAddress and will then function as the collateral of your\n"
"masternode.\n",
{
{"collateralAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The Syscoin address to send the collateral to."},
{"ipAndPort", RPCArg::Type::STR, RPCArg::Optional::NO, "IP and port in the form \"IP:PORT\".\n"
"Must be unique on the network. Can be set to 0, which will require a ProUpServTx afterwards."},
{"ownerAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The Syscoin address to use for payee updates and proposal voting.\n"
"The corresponding private key does not have to be known by your wallet.\n"
"The address must be unused and must differ from the collateralAddress."},
{"operatorPubKey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The operator BLS public key. The BLS private key does not have to be known.\n"
"It has to match the BLS private key which is later used when operating the masternode."},
{"votingAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The voting key address. The private key does not have to be known by your wallet.\n"
"It has to match the private key which is later used when voting on proposals.\n"
"If set to an empty string, ownerAddress will be used.\n"},
{"operatorReward", RPCArg::Type::NUM, RPCArg::Optional::NO, "The fraction in %% to share with the operator. The value must be\n"
"between 0.00 and 100.00."},
{"payoutAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The Syscoin address to use for masternode reward payments."},
{"fundAddress", RPCArg::Type::STR, RPCArg::Default{""}, "If specified wallet will only use coins from this address to fund ProTx.\n"
"If not specified, payoutAddress is the one that is going to be used.\n"
"The private key belonging to this address must be known in your wallet."},
{"submit", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, "If true, the resulting transaction is sent to the network."},
},
RPCResult{RPCResult::Type::STR_HEX, "", "The transaction hash in hex"},
RPCExamples{
HelpExampleCli("protx_register_fund", "\"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\" 173.249.49.9:18369 tsys1q2j57a4rtserh9022a63pvk3jqmg7un55stux0v 003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63 tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r 5 tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r")
+ HelpExampleRpc("protx_register_fund", "\"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\", \"173.249.49.9:18369\", \"tsys1q2j57a4rtserh9022a63pvk3jqmg7un55stux0v\", \"003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63\", \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\", 5, \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\"")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
if (!pwallet) return NullUniValue;
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet->BlockUntilSyncedToCurrentChain();
EnsureWalletIsUnlocked(*pwallet);
size_t paramIdx = 0;
CMutableTransaction tx;
tx.nVersion = SYSCOIN_TX_VERSION_MN_REGISTER;
CProRegTx ptx;
ptx.nVersion = CProRegTx::CURRENT_VERSION;
CTxDestination collateralDest = DecodeDestination(request.params[paramIdx].get_str());
if (!IsValidDestination(collateralDest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid collaterall address: %s", request.params[paramIdx].get_str()));
}
CScript collateralScript = GetScriptForDestination(collateralDest);
CTxOut collateralTxOut(nMNCollateralRequired, collateralScript);
tx.vout.emplace_back(collateralTxOut);
paramIdx++;
if (request.params[paramIdx].get_str() != "") {
if (!Lookup(request.params[paramIdx].get_str().c_str(), ptx.addr, Params().GetDefaultPort(), false)) {
throw std::runtime_error(strprintf("invalid network address %s", request.params[paramIdx].get_str()));
}
}
ptx.keyIDOwner = ParsePubKeyIDFromAddress(request.params[paramIdx + 1].get_str(), "owner address");
CBLSPublicKey pubKeyOperator = ParseBLSPubKey(request.params[paramIdx + 2].get_str(), "operator BLS address");
CKeyID keyIDVoting = ptx.keyIDOwner;
if (request.params[paramIdx + 3].get_str() != "") {
keyIDVoting = ParsePubKeyIDFromAddress(request.params[paramIdx + 3].get_str(), "voting address");
}
int64_t operatorReward;
if (!ParseFixedPoint(request.params[paramIdx + 4].getValStr(), 2, &operatorReward)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "operatorReward must be a number");
}
if (operatorReward < 0 || operatorReward > 10000) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "operatorReward must be between 0.00 and 100.00");
}
ptx.nOperatorReward = operatorReward;
CTxDestination payoutDest = DecodeDestination(request.params[paramIdx + 5].get_str());
if (!IsValidDestination(payoutDest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid payout address: %s", request.params[paramIdx + 5].get_str()));
}
ptx.pubKeyOperator = pubKeyOperator;
ptx.keyIDVoting = keyIDVoting;
ptx.scriptPayout = GetScriptForDestination(payoutDest);
CTxDestination fundDest = payoutDest;
if (!request.params[paramIdx + 6].isNull()) {
fundDest = DecodeDestination(request.params[paramIdx + 6].get_str());
if (!IsValidDestination(fundDest))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Syscoin address: ") + request.params[paramIdx + 6].get_str());
}
FundSpecialTx(*pwallet, tx, ptx, fundDest);
UpdateSpecialTxInputsHash(tx, ptx);
bool fSubmit{true};
if (!request.params[paramIdx + 7].isNull()) {
fSubmit = request.params[paramIdx + 7].get_bool();
}
uint32_t collateralIndex = (uint32_t) -1;
for (uint32_t i = 0; i < tx.vout.size(); i++) {
if (tx.vout[i].nValue == nMNCollateralRequired) {
collateralIndex = i;
break;
}
}
CHECK_NONFATAL(collateralIndex != (uint32_t) -1);
ptx.collateralOutpoint.n = collateralIndex;
SetTxPayload(tx, ptx);
return SignAndSendSpecialTx(request, *pwallet, tx, fSubmit);
},
};
}
static RPCHelpMan protx_register_prepare()
{
return RPCHelpMan{"protx_register_prepare",
"\nCreates an unsigned ProTx and returns it. The ProTx must be signed externally with the collateral\n"
"key and then passed to \"protx_register_submit\". The prepared transaction will also contain inputs\n"
"and outputs to cover fees.\n",
{
{"collateralHash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The collateral transaction hash."},
{"collateralIndex", RPCArg::Type::NUM, RPCArg::Optional::NO, "The collateral transaction output index."},
{"ipAndPort", RPCArg::Type::STR, RPCArg::Optional::NO, "IP and port in the form \"IP:PORT\".\n"
"Must be unique on the network. Can be set to 0, which will require a ProUpServTx afterwards."},
{"ownerAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The Syscoin address to use for payee updates and proposal voting.\n"
"The corresponding private key does not have to be known by your wallet.\n"
"The address must be unused and must differ from the collateralAddress."},
{"operatorPubKey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The operator BLS public key. The BLS private key does not have to be known.\n"
"It has to match the BLS private key which is later used when operating the masternode."},
{"votingAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The voting key address. The private key does not have to be known by your wallet.\n"
"It has to match the private key which is later used when voting on proposals.\n"
"If set to an empty string, ownerAddress will be used.\n"},
{"operatorReward", RPCArg::Type::NUM, RPCArg::Optional::NO, "The fraction in %% to share with the operator. The value must be\n"
"between 0.00 and 100.00."},
{"payoutAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The Syscoin address to use for masternode reward payments."},
{"fundAddress", RPCArg::Type::STR, RPCArg::Default{""}, "If specified wallet will only use coins from this address to fund ProTx.\n"
"If not specified, payoutAddress is the one that is going to be used.\n"
"The private key belonging to this address must be known in your wallet."},
},
RPCResult{RPCResult::Type::ANY, "", "Unsigned ProTX transaction object"},
RPCExamples{
HelpExampleCli("protx_register_prepare", "1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d 0 173.249.49.9:18369 tsys1q2j57a4rtserh9022a63pvk3jqmg7un55stux0v 003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63 tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r 5 tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r")
+ HelpExampleRpc("protx_register_prepare", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\", 0, \"173.249.49.9:18369\", \"tsys1q2j57a4rtserh9022a63pvk3jqmg7un55stux0v\", \"003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63\", \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\", 5, \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\"")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
if (!pwallet) return NullUniValue;
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet->BlockUntilSyncedToCurrentChain();
size_t paramIdx = 0;
CMutableTransaction tx;
tx.nVersion = SYSCOIN_TX_VERSION_MN_REGISTER;
CProRegTx ptx;
ptx.nVersion = CProRegTx::CURRENT_VERSION;
uint256 collateralHash = ParseHashV(request.params[paramIdx], "collateralHash");
int32_t collateralIndex = request.params[paramIdx + 1].get_int();
if (collateralHash.IsNull() || collateralIndex < 0) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid hash or index: %s-%d", collateralHash.ToString(), collateralIndex));
}
ptx.collateralOutpoint = COutPoint(collateralHash, (uint32_t)collateralIndex);
paramIdx += 2;
CTxDestination fundDest;
{
// TODO unlock on failure
LOCK(pwallet->cs_wallet);
pwallet->LockCoin(ptx.collateralOutpoint);
if (request.params[paramIdx].get_str() != "") {
if (!Lookup(request.params[paramIdx].get_str().c_str(), ptx.addr, Params().GetDefaultPort(), false)) {
throw std::runtime_error(strprintf("invalid network address %s", request.params[paramIdx].get_str()));
}
}
ptx.keyIDOwner = ParsePubKeyIDFromAddress(request.params[paramIdx + 1].get_str(), "owner address");
CBLSPublicKey pubKeyOperator = ParseBLSPubKey(request.params[paramIdx + 2].get_str(), "operator BLS address");
CKeyID keyIDVoting = ptx.keyIDOwner;
if (request.params[paramIdx + 3].get_str() != "") {
keyIDVoting = ParsePubKeyIDFromAddress(request.params[paramIdx + 3].get_str(), "voting address");
}
int64_t operatorReward;
if (!ParseFixedPoint(request.params[paramIdx + 4].getValStr(), 2, &operatorReward)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "operatorReward must be a number");
}
if (operatorReward < 0 || operatorReward > 10000) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "operatorReward must be between 0.00 and 100.00");
}
ptx.nOperatorReward = operatorReward;
CTxDestination payoutDest = DecodeDestination(request.params[paramIdx + 5].get_str());
if (!IsValidDestination(payoutDest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid payout address: %s", request.params[paramIdx + 5].get_str()));
}
ptx.pubKeyOperator = pubKeyOperator;
ptx.keyIDVoting = keyIDVoting;
ptx.scriptPayout = GetScriptForDestination(payoutDest);
// make sure fee calculation works
ptx.vchSig.resize(65);
fundDest = payoutDest;
if (!request.params[paramIdx + 6].isNull()) {
fundDest = DecodeDestination(request.params[paramIdx + 6].get_str());
if (!IsValidDestination(fundDest))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Syscoin address: ") + request.params[paramIdx + 6].get_str());
}
}
FundSpecialTx(*pwallet, tx, ptx, fundDest);
UpdateSpecialTxInputsHash(tx, ptx);
// referencing external collateral
std::map<COutPoint, Coin> coins;
coins[ptx.collateralOutpoint];
pwallet->chain().findCoins(coins);
const Coin &coin = coins.at(ptx.collateralOutpoint);
if(coin.IsSpent()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral not found: %s", ptx.collateralOutpoint.ToStringShort()));
}
CTxDestination txDest;
ExtractDestination(coin.out.scriptPubKey, txDest);
CKeyID keyID;
if (auto witness_id = std::get_if<WitnessV0KeyHash>(&txDest)) {
keyID = ToKeyID(*witness_id);
}
else if (auto key_id = std::get_if<PKHash>(&txDest)) {
keyID = ToKeyID(*key_id);
}
if (keyID.IsNull()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral type not supported: %s", ptx.collateralOutpoint.ToStringShort()));
}
// external signing with collateral key
ptx.vchSig.clear();
SetTxPayload(tx, ptx);
UniValue ret(UniValue::VOBJ);
ret.pushKV("tx", EncodeHexTx(CTransaction(tx)));
ret.pushKV("collateralAddress", EncodeDestination(txDest));
ret.pushKV("signMessage", ptx.MakeSignString());
return ret;
},
};
}
static RPCHelpMan protx_register_submit()
{
return RPCHelpMan{"protx_register_submit",
"\nSubmits the specified ProTx to the network. This command will also sign the inputs of the transaction\n"
"which were previously added by \"protx_register_prepare\" to cover transaction fees\n"
"and outputs to cover fees.\n",
{
{"tx", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The serialized transaction previously returned by \"protx_register_prepare\"."},
{"sig", RPCArg::Type::STR, RPCArg::Optional::NO, "The signature signed with the collateral key. Must be in base64 format."},
},
RPCResult{RPCResult::Type::STR_HEX, "", "The transaction hash in hex"},
RPCExamples{
HelpExampleCli("protx_register_submit", "")
+ HelpExampleRpc("protx_register_submit", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
if (!pwallet) return NullUniValue;
EnsureWalletIsUnlocked(*pwallet);
CMutableTransaction tx;
if (!DecodeHexTx(tx, request.params[0].get_str())) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "transaction not deserializable");
}
if (tx.nVersion != SYSCOIN_TX_VERSION_MN_REGISTER) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "transaction not a ProRegTx");
}
CProRegTx ptx;
if (!GetTxPayload(tx, ptx)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "transaction payload not deserializable");
}
if (!ptx.vchSig.empty()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "payload signature not empty");
}
ptx.vchSig = DecodeBase64(request.params[1].get_str().c_str());
SetTxPayload(tx, ptx);
return SignAndSendSpecialTx(request, *pwallet, tx);
},
};
}
static RPCHelpMan protx_update_service()
{
return RPCHelpMan{"protx_update_service",
"\nCreates and sends a ProUpServTx to the network. This will update the IP address\n"
"of a masternode.\n"
"If this is done for a masternode that got PoSe-banned, the ProUpServTx will also revive this masternode.\n",
{
{"proTxHash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hash of the initial ProRegTx."},
{"ipAndPort", RPCArg::Type::STR, RPCArg::Optional::NO, "IP and port in the form \"IP:PORT\".\n"
"Must be unique on the network. Can be set to 0, which will require a ProUpServTx afterwards."},
{"operatorKey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The operator BLS private key associated with the\n"
"registered operator public key."},
{"operatorPayoutAddress", RPCArg::Type::STR_HEX, RPCArg::Default{""}, "The address used for operator reward payments.\n"
"Only allowed when the ProRegTx had a non-zero operatorReward value.\n"
"If set to an empty string, the currently active payout address is reused."},
{"feeSourceAddress", RPCArg::Type::STR, RPCArg::Default{""}, "If specified wallet will only use coins from this address to fund ProTx.\n"
"If not specified, payoutAddress is the one that is going to be used.\n"
"The private key belonging to this address must be known in your wallet."},
},
RPCResult{RPCResult::Type::STR_HEX, "", "The transaction hash in hex"},
RPCExamples{
HelpExampleCli("protx_update_service", "1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d 173.249.49.9:18369 003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63 tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r")
+ HelpExampleRpc("protx_update_service", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\", \"173.249.49.9:18369\", \"003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63\", \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\", \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\"")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
if (!pwallet) return NullUniValue;
EnsureWalletIsUnlocked(*pwallet);
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet->BlockUntilSyncedToCurrentChain();
CProUpServTx ptx;
ptx.nVersion = CProUpServTx::CURRENT_VERSION;
ptx.proTxHash = ParseHashV(request.params[0], "proTxHash");
if (!Lookup(request.params[1].get_str().c_str(), ptx.addr, Params().GetDefaultPort(), false)) {
throw std::runtime_error(strprintf("invalid network address %s", request.params[1].get_str()));
}
CBLSSecretKey keyOperator = ParseBLSSecretKey(request.params[2].get_str(), "operatorKey");
auto mnList = deterministicMNManager->GetListAtChainTip();
auto dmn = mnList.GetMN(ptx.proTxHash);
if (!dmn) {
throw std::runtime_error(strprintf("masternode with proTxHash %s not found", ptx.proTxHash.ToString()));
}
if (keyOperator.GetPublicKey() != dmn->pdmnState->pubKeyOperator.Get()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("the operator key does not belong to the registered public key"));
}
CMutableTransaction tx;
tx.nVersion = SYSCOIN_TX_VERSION_MN_UPDATE_SERVICE;
// param operatorPayoutAddress
if (!request.params[3].isNull()) {
if (request.params[3].get_str().empty()) {
ptx.scriptOperatorPayout = dmn->pdmnState->scriptOperatorPayout;
} else {
CTxDestination payoutDest = DecodeDestination(request.params[3].get_str());
if (!IsValidDestination(payoutDest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid operator payout address: %s", request.params[3].get_str()));
}
ptx.scriptOperatorPayout = GetScriptForDestination(payoutDest);
}
} else {
ptx.scriptOperatorPayout = dmn->pdmnState->scriptOperatorPayout;
}
CTxDestination feeSource;
// param feeSourceAddress
if (!request.params[4].isNull()) {
feeSource = DecodeDestination(request.params[4].get_str());
if (!IsValidDestination(feeSource))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Syscoin address: ") + request.params[4].get_str());
} else {
if (ptx.scriptOperatorPayout != CScript()) {
// use operator reward address as default source for fees
ExtractDestination(ptx.scriptOperatorPayout, feeSource);
} else {
// use payout address as default source for fees
ExtractDestination(dmn->pdmnState->scriptPayout, feeSource);
}
}
FundSpecialTx(*pwallet, tx, ptx, feeSource);
SignSpecialTxPayloadByHash(tx, ptx, keyOperator);
SetTxPayload(tx, ptx);
return SignAndSendSpecialTx(request, *pwallet, tx);
},
};
}
static RPCHelpMan protx_update_registrar()
{
return RPCHelpMan{"protx_update_registrar",
"\nCreates and sends a ProUpRegTx to the network. This will update the operator key, voting key and payout\n"
"address of the masternode specified by \"proTxHash\".\n"
"The owner key of the masternode must be known to your wallet.\n",
{
{"proTxHash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hash of the initial ProRegTx."},
{"operatorPubKey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The operator BLS public key. The BLS private key does not have to be known.\n"
"It has to match the BLS private key which is later used when operating the masternode.\n"
"If set to an empty string, the currently active operator BLS public key is reused."},
{"votingAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The voting key address. The private key does not have to be known by your wallet.\n"
"It has to match the private key which is later used when voting on proposals.\n"
"If set to an empty string, the currently active voting key address is reused."},
{"payoutAddress", RPCArg::Type::STR, RPCArg::Optional::NO, "The Syscoin address to use for masternode reward payments.\n"
"If set to an empty string, the currently active payout address is reused."},
{"feeSourceAddress", RPCArg::Type::STR, RPCArg::Default{""}, "If specified wallet will only use coins from this address to fund ProTx.\n"
"If not specified, payoutAddress is the one that is going to be used.\n"
"The private key belonging to this address must be known in your wallet."},
},
RPCResult{RPCResult::Type::STR_HEX, "", "The transaction hash in hex"},
RPCExamples{
HelpExampleCli("protx_update_registrar", "1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d 003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63 tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r")
+ HelpExampleRpc("protx_update_registrar", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\", \"003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63\", \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\", \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\"")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
if (!pwallet) return NullUniValue;
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet->BlockUntilSyncedToCurrentChain();
EnsureWalletIsUnlocked(*pwallet);
CProUpRegTx ptx;
ptx.nVersion = CProUpRegTx::CURRENT_VERSION;
ptx.proTxHash = ParseHashV(request.params[0], "proTxHash");
auto mnList = deterministicMNManager->GetListAtChainTip();
auto dmn = mnList.GetMN(ptx.proTxHash);
if (!dmn) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
}
ptx.pubKeyOperator = dmn->pdmnState->pubKeyOperator.Get();
ptx.keyIDVoting = dmn->pdmnState->keyIDVoting;
ptx.scriptPayout = dmn->pdmnState->scriptPayout;
if (request.params[1].get_str() != "") {
ptx.pubKeyOperator = ParseBLSPubKey(request.params[1].get_str(), "operator BLS address");
}
if (request.params[2].get_str() != "") {
ptx.keyIDVoting = ParsePubKeyIDFromAddress(request.params[2].get_str(), "voting address");
}
CTxDestination payoutDest;
ExtractDestination(ptx.scriptPayout, payoutDest);
if (request.params[3].get_str() != "") {
payoutDest = DecodeDestination(request.params[3].get_str());
if (!IsValidDestination(payoutDest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid payout address: %s", request.params[3].get_str()));
}
ptx.scriptPayout = GetScriptForDestination(payoutDest);
}
CKey keyOwner;
{
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
if (!spk_man.GetKey(dmn->pdmnState->keyIDOwner, keyOwner)) {
throw std::runtime_error(strprintf("Private key for owner address %s not found in your wallet", EncodeDestination(WitnessV0KeyHash(dmn->pdmnState->keyIDOwner))));
}
}
CMutableTransaction tx;
tx.nVersion = SYSCOIN_TX_VERSION_MN_UPDATE_REGISTRAR;
// make sure we get anough fees added
ptx.vchSig.resize(65);
CTxDestination feeSourceDest = payoutDest;
if (!request.params[4].isNull()) {
feeSourceDest = DecodeDestination(request.params[4].get_str());
if (!IsValidDestination(feeSourceDest))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Syscoin address: ") + request.params[5].get_str());
}
FundSpecialTx(*pwallet, tx, ptx, feeSourceDest);
SignSpecialTxPayloadByHash(tx, ptx, keyOwner);
SetTxPayload(tx, ptx);
return SignAndSendSpecialTx(request, *pwallet, tx);
},
};
}
static RPCHelpMan protx_revoke()
{
return RPCHelpMan{"protx_revoke",
"\nCreates and sends a ProUpRevTx to the network. This will revoke the operator key of the masternode and\n"
"put it into the PoSe-banned state. It will also set the service field of the masternode\n"
"to zero. Use this in case your operator key got compromised or you want to stop providing your service\n"
"to the masternode owner.\n",
{
{"proTxHash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hash of the initial ProRegTx."},
{"operatorKey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The operator BLS private key associated with the\n"
"registered operator public key."},
{"reason", RPCArg::Type::NUM, RPCArg::Default{0}, "The reason for masternode service revocation."},
{"feeSourceAddress", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "If specified wallet will only use coins from this address to fund ProTx.\n"
"If not specified, payoutAddress is the one that is going to be used.\n"
"The private key belonging to this address must be known in your wallet."},
},
RPCResult{RPCResult::Type::STR_HEX, "", "The transaction hash in hex"},
RPCExamples{
HelpExampleCli("protx_update_registrar", "1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d 003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63 0 tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r")
+ HelpExampleRpc("protx_update_registrar", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\", \"003bc97fcd6023996f8703b4da34dedd1641bd45ed12ac7a4d74a529dd533ecb99d4fb8ddb04853bb110f0d747ee8e63\", 0, \"tsys1qxh8am0c9w0q9kv7h7f9q2c4jrfjg63yawrgm0r\"")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
if (!pwallet) return NullUniValue;
EnsureWalletIsUnlocked(*pwallet);
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet->BlockUntilSyncedToCurrentChain();
CProUpRevTx ptx;
ptx.nVersion = CProUpRevTx::CURRENT_VERSION;
ptx.proTxHash = ParseHashV(request.params[0], "proTxHash");
CBLSSecretKey keyOperator = ParseBLSSecretKey(request.params[1].get_str(), "operatorKey");
if (!request.params[2].isNull()) {
int32_t nReason = request.params[2].get_int();
if (nReason < 0 || nReason > CProUpRevTx::REASON_LAST) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("invalid reason %d, must be between 0 and %d", nReason, CProUpRevTx::REASON_LAST));
}
ptx.nReason = (uint16_t)nReason;
}
auto mnList = deterministicMNManager->GetListAtChainTip();
auto dmn = mnList.GetMN(ptx.proTxHash);
if (!dmn) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
}
if (keyOperator.GetPublicKey() != dmn->pdmnState->pubKeyOperator.Get()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("the operator key does not belong to the registered public key"));
}
CMutableTransaction tx;
tx.nVersion = SYSCOIN_TX_VERSION_MN_UPDATE_REVOKE;
if (!request.params[3].isNull()) {
CTxDestination feeSourceDest = DecodeDestination(request.params[3].get_str());
if (!IsValidDestination(feeSourceDest))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Syscoin address: ") + request.params[3].get_str());
FundSpecialTx(*pwallet, tx, ptx, feeSourceDest);
} else if (dmn->pdmnState->scriptOperatorPayout != CScript()) {
// Using funds from previousely specified operator payout address
CTxDestination txDest;
ExtractDestination(dmn->pdmnState->scriptOperatorPayout, txDest);
FundSpecialTx(*pwallet, tx, ptx, txDest);
} else if (dmn->pdmnState->scriptPayout != CScript()) {
// Using funds from previousely specified masternode payout address
CTxDestination txDest;
ExtractDestination(dmn->pdmnState->scriptPayout, txDest);
FundSpecialTx(*pwallet, tx, ptx, txDest);
} else {
throw JSONRPCError(RPC_INTERNAL_ERROR, "No payout or fee source addresses found, can't revoke");
}
SignSpecialTxPayloadByHash(tx, ptx, keyOperator);
SetTxPayload(tx, ptx);
return SignAndSendSpecialTx(request, *pwallet, tx);
},
};
}
static bool CheckWalletOwnsKey(CWallet* pwallet, const CKeyID& keyID) {
if (!pwallet) {
return false;
}
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
return spk_man.IsMine(GetScriptForDestination(CTxDestination(WitnessV0KeyHash(keyID))));
}
static bool CheckWalletOwnsScript(CWallet* pwallet, const CScript& script) {
if (!pwallet) {
return false;
}
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
return spk_man.IsMine(script);
}
UniValue BuildDMNListEntry(CWallet* pwallet, const CDeterministicMNCPtr& dmn, int detailed)
{
if (!detailed) {
return dmn->proTxHash.ToString();
}
UniValue o(UniValue::VOBJ);
if(detailed == 1) {
const CTxDestination &voteDest = WitnessV0KeyHash(dmn->pdmnState->keyIDVoting);
o.pushKV("collateralHash", dmn->collateralOutpoint.hash.ToString());
o.pushKV("collateralIndex", (int)dmn->collateralOutpoint.n);
o.pushKV("collateralHeight", dmn->pdmnState->nCollateralHeight);
o.pushKV("votingAddress", EncodeDestination(voteDest));
if(pwallet) {
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
CKey keyVoting;
spk_man.GetKey(dmn->pdmnState->keyIDVoting, keyVoting);
o.pushKV("votingKey", EncodeSecret(keyVoting));
const auto* address_book_entry = pwallet->FindAddressBookEntry(voteDest);
if (address_book_entry) {
o.pushKV("label", address_book_entry->GetLabel());
}
}
return o;
} else if(detailed >= 2 && pwallet) {
dmn->ToJson(pwallet->chain(), o);
std::map<COutPoint, Coin> coins;
coins[dmn->collateralOutpoint];
pwallet->chain().findCoins(coins);
int confirmations = 0;
const Coin &coin = coins.at(dmn->collateralOutpoint);
if(!coin.IsSpent()) {
confirmations = *pwallet->chain().getHeight() - coin.nHeight;
}
o.pushKV("confirmations", confirmations);
if (pwallet) {
LOCK2(pwallet->cs_wallet, cs_main);
bool hasOwnerKey = CheckWalletOwnsKey(pwallet, dmn->pdmnState->keyIDOwner);
bool hasVotingKey = CheckWalletOwnsKey(pwallet, dmn->pdmnState->keyIDVoting);
UniValue walletObj(UniValue::VOBJ);
walletObj.pushKV("hasOwnerKey", hasOwnerKey);
walletObj.pushKV("hasOperatorKey", false);
walletObj.pushKV("hasVotingKey", hasVotingKey);
walletObj.pushKV("ownsPayeeScript", CheckWalletOwnsScript(pwallet, dmn->pdmnState->scriptPayout));
walletObj.pushKV("ownsOperatorRewardScript", CheckWalletOwnsScript(pwallet, dmn->pdmnState->scriptOperatorPayout));
o.pushKV("wallet", walletObj);
}
auto metaInfo = mmetaman.GetMetaInfo(dmn->proTxHash);
o.pushKV("metaInfo", metaInfo->ToJson());