Skip to content

Commit bd0f59e

Browse files
feat(governance): implement SignGovernanceVote method in CWallet for vote signing
1 parent 13b4117 commit bd0f59e

File tree

6 files changed

+38
-61
lines changed

6 files changed

+38
-61
lines changed

src/governance/vote.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -267,33 +267,3 @@ bool operator<(const CGovernanceVote& vote1, const CGovernanceVote& vote2)
267267
return fResult;
268268
}
269269

270-
#include <wallet/wallet.h>
271-
#include <util/strencodings.h>
272-
#include <util/message.h>
273-
274-
bool SignGovernanceVote(const CWallet& wallet, const CKeyID& keyID, CGovernanceVote& vote)
275-
{
276-
// Special implementation for testnet (Harden Spork6 that has not been deployed to other networks)
277-
if (Params().NetworkIDString() == CBaseChainParams::TESTNET) {
278-
std::vector<unsigned char> signature;
279-
if (!wallet.SignSpecialTxPayload(vote.GetSignatureHash(), keyID, signature)) {
280-
LogPrintf("SignGovernanceVote -- SignHash() failed\n");
281-
return false;
282-
}
283-
vote.SetSignature(signature);
284-
return true;
285-
} // end of testnet implementation
286-
287-
std::string strMessage{vote.GetSignatureString()};
288-
std::string signature;
289-
SigningResult err = wallet.SignMessage(strMessage, PKHash{keyID}, signature);
290-
if (err != SigningResult::OK) {
291-
LogPrintf("SignGovernanceVote failed due to: %s\n", SigningResultString(err));
292-
return false;
293-
}
294-
const auto opt_decoded = DecodeBase64(signature);
295-
CHECK_NONFATAL(opt_decoded.has_value()); // DecodeBase64 should not fail
296-
297-
vote.SetSignature(std::vector<unsigned char>(opt_decoded->data(), opt_decoded->data() + opt_decoded->size()));
298-
return true;
299-
}

src/governance/vote.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,5 @@ class CGovernanceVote
131131
* Sign a governance vote using wallet signing methods
132132
* Handles different signing approaches for different networks
133133
*/
134-
bool SignGovernanceVote(const CWallet& wallet, const CKeyID& keyID, CGovernanceVote& vote);
135134

136135
#endif // BITCOIN_GOVERNANCE_VOTE_H

src/qt/governancelist.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ void GovernanceList::voteForProposal(vote_outcome_enum_t outcome)
558558
// Create vote
559559
CGovernanceVote vote(dmn->collateralOutpoint, proposalHash, VOTE_SIGNAL_FUNDING, outcome);
560560

561-
// Sign vote using shared helper function
562-
if (!SignGovernanceVote(*walletModel->wallet().wallet(), votingKeyID, vote)) {
561+
// Sign vote using CWallet member function
562+
if (!walletModel->wallet().wallet()->SignGovernanceVote(votingKeyID, vote)) {
563563
nFailed++;
564564
failedMessages.append(
565565
tr("Failed to sign vote for masternode %1").arg(QString::fromStdString(proTxHash.ToString())));

src/rpc/governance.cpp

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -411,33 +411,6 @@ static RPCHelpMan gobject_submit()
411411
}
412412

413413
#ifdef ENABLE_WALLET
414-
static bool SignVote(const CWallet& wallet, const CKeyID& keyID, CGovernanceVote& vote)
415-
{
416-
// Special implementation for testnet (Harden Spork6 that has not been deployed to other networks)
417-
if (Params().NetworkIDString() == CBaseChainParams::TESTNET) {
418-
std::vector<unsigned char> signature;
419-
if (!wallet.SignSpecialTxPayload(vote.GetSignatureHash(), keyID, signature)) {
420-
LogPrintf("SignVote -- SignHash() failed\n");
421-
return false;
422-
}
423-
vote.SetSignature(signature);
424-
return true;
425-
} // end of testnet implementation
426-
427-
std::string strMessage{vote.GetSignatureString()};
428-
std::string signature;
429-
SigningResult err = wallet.SignMessage(strMessage, PKHash{keyID}, signature);
430-
if (err != SigningResult::OK) {
431-
LogPrintf("SignVote failed due to: %s\n", SigningResultString(err));
432-
return false;
433-
}
434-
const auto opt_decoded = DecodeBase64(signature);
435-
CHECK_NONFATAL(opt_decoded.has_value()); // DecodeBase64 should not fail
436-
437-
vote.SetSignature(std::vector<unsigned char>(opt_decoded->data(), opt_decoded->data() + opt_decoded->size()));
438-
return true;
439-
}
440-
441414
static UniValue VoteWithMasternodes(const JSONRPCRequest& request, const CWallet& wallet,
442415
const std::map<uint256, CKeyID>& votingKeys,
443416
const uint256& hash, vote_signal_enum_t eVoteSignal,
@@ -477,7 +450,7 @@ static UniValue VoteWithMasternodes(const JSONRPCRequest& request, const CWallet
477450

478451
CGovernanceVote vote(dmn->collateralOutpoint, hash, eVoteSignal, eVoteOutcome);
479452

480-
if (!SignVote(wallet, keyID, vote) || !vote.CheckSignature(keyID)) {
453+
if (!wallet.SignGovernanceVote(keyID, vote) || !vote.CheckSignature(keyID)) {
481454
nFailed++;
482455
statusObj.pushKV("result", "failed");
483456
statusObj.pushKV("errorMessage", "Failure to sign.");

src/wallet/wallet.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,33 @@ bool CWallet::SignSpecialTxPayload(const uint256& hash, const CKeyID& keyid, std
20402040
return false;
20412041
}
20422042

2043+
bool CWallet::SignGovernanceVote(const CKeyID& keyID, CGovernanceVote& vote) const
2044+
{
2045+
// Special implementation for testnet (Harden Spork6 that has not been deployed to other networks)
2046+
if (Params().NetworkIDString() == CBaseChainParams::TESTNET) {
2047+
std::vector<unsigned char> signature;
2048+
if (!SignSpecialTxPayload(vote.GetSignatureHash(), keyID, signature)) {
2049+
LogPrintf("SignGovernanceVote -- SignHash() failed\n");
2050+
return false;
2051+
}
2052+
vote.SetSignature(signature);
2053+
return true;
2054+
} // end of testnet implementation
2055+
2056+
std::string strMessage{vote.GetSignatureString()};
2057+
std::string signature;
2058+
SigningResult err = SignMessage(strMessage, PKHash{keyID}, signature);
2059+
if (err != SigningResult::OK) {
2060+
LogPrintf("SignGovernanceVote failed due to: %s\n", SigningResultString(err));
2061+
return false;
2062+
}
2063+
const auto opt_decoded = DecodeBase64(signature);
2064+
CHECK_NONFATAL(opt_decoded.has_value()); // DecodeBase64 should not fail
2065+
2066+
vote.SetSignature(std::vector<unsigned char>(opt_decoded->data(), opt_decoded->data() + opt_decoded->size()));
2067+
return true;
2068+
}
2069+
20432070
void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm)
20442071
{
20452072
LOCK(cs_wallet);

src/wallet/wallet.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,14 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
658658
* but it should work for any its type, we pass there directly a hash of payload.
659659
*/
660660
bool SignSpecialTxPayload(const uint256& hash, const CKeyID& keyid, std::vector<unsigned char>& vchSig) const;
661+
/**
662+
* Sign a governance vote using wallet signing methods.
663+
*
664+
* @param[in] keyID The key ID to use for signing
665+
* @param[in,out] vote The governance vote to sign (signature is set on success)
666+
* @return true if signing succeeded, false otherwise
667+
*/
668+
bool SignGovernanceVote(const CKeyID& keyID, CGovernanceVote& vote) const;
661669

662670
/**
663671
* Fills out a PSBT with information from the wallet. Fills in UTXOs if we have

0 commit comments

Comments
 (0)