diff --git a/src/activemasternode.cpp b/src/activemasternode.cpp index 20df68b6e029..b21271707be1 100644 --- a/src/activemasternode.cpp +++ b/src/activemasternode.cpp @@ -33,7 +33,6 @@ OperationResult initMasternode(const std::string& _strMasterNodePrivKey, const s // Global params set strMasterNodeAddr = _strMasterNodeAddr; - strMasterNodePrivKey = _strMasterNodePrivKey; // Address parsing. const CChainParams& params = Params(); @@ -56,11 +55,12 @@ OperationResult initMasternode(const std::string& _strMasterNodePrivKey, const s CKey key; CPubKey pubkey; - - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, key, pubkey)) { + if (!CMessageSigner::GetKeysFromSecret(_strMasterNodePrivKey, key, pubkey)) { return errorOut(_("Invalid masternodeprivkey. Please see the documentation.")); } + activeMasternode.pubKeyMasternode = pubkey; + activeMasternode.privKeyMasternode = key; fMasterNode = true; return OperationResult(true); } @@ -169,11 +169,8 @@ bool CActiveMasternode::SendMasternodePing(std::string& errorMessage) return false; } - CPubKey pubKeyMasternode; - CKey keyMasternode; - - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) { - errorMessage = "Error upon calling GetKeysFromSecret.\n"; + if (!privKeyMasternode.IsValid() || !pubKeyMasternode.IsValid()) { + errorMessage = "Error upon masternode key.\n"; return false; } @@ -181,7 +178,7 @@ bool CActiveMasternode::SendMasternodePing(std::string& errorMessage) const uint256& nBlockHash = mnodeman.GetBlockHashToPing(); CMasternodePing mnp(*vin, nBlockHash); - if (!mnp.Sign(keyMasternode, pubKeyMasternode)) { + if (!mnp.Sign(privKeyMasternode, pubKeyMasternode)) { errorMessage = "Couldn't sign Masternode Ping"; return false; } @@ -234,3 +231,12 @@ bool CActiveMasternode::EnableHotColdMasterNode(CTxIn& newVin, CService& newServ return true; } + +void CActiveMasternode::GetKeys(CKey& _privKeyMasternode, CPubKey& _pubKeyMasternode) +{ + if (!privKeyMasternode.IsValid() || !pubKeyMasternode.IsValid()) { + throw std::runtime_error("Error trying to get masternode keys"); + } + _privKeyMasternode = privKeyMasternode; + _pubKeyMasternode = pubKeyMasternode; +} diff --git a/src/activemasternode.h b/src/activemasternode.h index d05d4844d745..cfa4351d3756 100644 --- a/src/activemasternode.h +++ b/src/activemasternode.h @@ -40,6 +40,7 @@ class CActiveMasternode // Initialized by init.cpp // Keys for the main Masternode CPubKey pubKeyMasternode; + CKey privKeyMasternode; // Initialized while registering Masternode Optional vin; @@ -55,6 +56,8 @@ class CActiveMasternode bool SendMasternodePing(std::string& errorMessage); /// Enable cold wallet mode (run a Masternode with no funds) bool EnableHotColdMasterNode(CTxIn& vin, CService& addr); + + void GetKeys(CKey& privKeyMasternode, CPubKey& pubKeyMasternode); }; #endif diff --git a/src/budget/budgetmanager.cpp b/src/budget/budgetmanager.cpp index cae091d82d09..d04bb2e29ed7 100644 --- a/src/budget/budgetmanager.cpp +++ b/src/budget/budgetmanager.cpp @@ -500,10 +500,8 @@ void CBudgetManager::VoteOnFinalizedBudgets() // Get masternode keys CPubKey pubKeyMasternode; CKey keyMasternode; - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) { - LogPrintf("%s: Unable to get masternode keys\n", __func__); - return; - } + activeMasternode.GetKeys(keyMasternode, pubKeyMasternode); + // Sign finalized budgets for (const uint256& budgetHash: vBudgetHashes) { CFinalizedBudgetVote vote(*(activeMasternode.vin), budgetHash); diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index 8d60ef179261..5c283ece3c80 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -680,14 +680,8 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) } } - std::string errorMessage; - CPubKey pubKeyMasternode; - CKey keyMasternode; - - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) { - LogPrint(BCLog::MASTERNODE,"CMasternodePayments::ProcessBlock() - Error upon calling GetKeysFromSecret.\n"); - return false; - } + CPubKey pubKeyMasternode; CKey keyMasternode; + activeMasternode.GetKeys(keyMasternode, pubKeyMasternode); LogPrint(BCLog::MASTERNODE,"CMasternodePayments::ProcessBlock() - Signing Winner\n"); if (newWinner.Sign(keyMasternode, pubKeyMasternode)) { diff --git a/src/masternodeman.h b/src/masternodeman.h index 9030d2d23865..62b9da2cd0ef 100644 --- a/src/masternodeman.h +++ b/src/masternodeman.h @@ -26,7 +26,6 @@ class CActiveMasternode; extern CMasternodeMan mnodeman; extern CActiveMasternode activeMasternode; -extern std::string strMasterNodePrivKey; void DumpMasternodes(); diff --git a/src/rpc/budget.cpp b/src/rpc/budget.cpp index d1ea19378ac0..9eebc048e571 100644 --- a/src/rpc/budget.cpp +++ b/src/rpc/budget.cpp @@ -273,14 +273,8 @@ UniValue mnbudgetvote(const JSONRPCRequest& request) UniValue statusObj(UniValue::VOBJ); while (true) { - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) { - failed++; - statusObj.pushKV("node", "local"); - statusObj.pushKV("result", "failed"); - statusObj.pushKV("error", "Masternode signing error, GetKeysFromSecret failed."); - resultsObj.push_back(statusObj); - break; - } + // Get MN keys + activeMasternode.GetKeys(keyMasternode, pubKeyMasternode); CMasternode* pmn = mnodeman.Find(activeMasternode.vin->prevout); if (pmn == NULL) { @@ -816,11 +810,8 @@ UniValue mnfinalbudget(const JSONRPCRequest& request) std::string strHash = request.params[1].get_str(); uint256 hash(uint256S(strHash)); - CPubKey pubKeyMasternode; - CKey keyMasternode; - - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) - return "Error upon calling GetKeysFromSecret"; + CPubKey pubKeyMasternode; CKey keyMasternode; + activeMasternode.GetKeys(keyMasternode, pubKeyMasternode); CMasternode* pmn = mnodeman.Find(activeMasternode.vin->prevout); if (pmn == NULL) { diff --git a/src/util.cpp b/src/util.cpp index 6ed16bbafeae..0ef573ca4913 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -90,7 +90,6 @@ const char * const PIVX_MASTERNODE_CONF_FILENAME = "masternode.conf"; // PIVX only features // Masternode std::atomic fMasterNode{false}; -std::string strMasterNodePrivKey = ""; std::string strMasterNodeAddr = ""; bool fLiteMode = false;