Skip to content

Commit 13b4117

Browse files
feat(governance): refactor vote signing logic into a helper function
1 parent 778a0b1 commit 13b4117

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

src/governance/vote.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,34 @@ bool operator<(const CGovernanceVote& vote1, const CGovernanceVote& vote2)
266266

267267
return fResult;
268268
}
269+
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class CGovernanceVote;
1515
class CMasternodeSync;
1616
class CKey;
1717
class CKeyID;
18+
class CWallet;
1819
class PeerManager;
1920

2021
// INTENTION OF MASTERNODES REGARDING ITEM
@@ -126,4 +127,10 @@ class CGovernanceVote
126127
}
127128
};
128129

130+
/**
131+
* Sign a governance vote using wallet signing methods
132+
* Handles different signing approaches for different networks
133+
*/
134+
bool SignGovernanceVote(const CWallet& wallet, const CKeyID& keyID, CGovernanceVote& vote);
135+
129136
#endif // BITCOIN_GOVERNANCE_VOTE_H

src/qt/governancelist.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <script/standard.h>
2424
#include <util/message.h>
2525
#include <util/strencodings.h>
26+
#include <wallet/wallet.h>
2627

2728
#include <univalue.h>
2829

@@ -557,32 +558,8 @@ void GovernanceList::voteForProposal(vote_outcome_enum_t outcome)
557558
// Create vote
558559
CGovernanceVote vote(dmn->collateralOutpoint, proposalHash, VOTE_SIGNAL_FUNDING, outcome);
559560

560-
// Sign vote
561-
bool signSuccess = false;
562-
563-
// Special implementation for testnet (same as RPC SignVote)
564-
if (Params().NetworkIDString() == CBaseChainParams::TESTNET) {
565-
// Testnet uses SignSpecialTxPayload
566-
std::vector<unsigned char> vchSig;
567-
if (walletModel->wallet().signSpecialTxPayload(vote.GetSignatureHash(), votingKeyID, vchSig)) {
568-
vote.SetSignature(vchSig);
569-
signSuccess = true;
570-
}
571-
} else {
572-
// Other networks use SignMessage
573-
std::string strMessage = vote.GetSignatureString();
574-
std::string signature;
575-
SigningResult result = walletModel->wallet().signMessage(strMessage, PKHash(votingKeyID), signature);
576-
if (result == SigningResult::OK) {
577-
const auto decoded = DecodeBase64(signature);
578-
if (decoded) {
579-
vote.SetSignature(std::vector<unsigned char>(decoded->begin(), decoded->end()));
580-
signSuccess = true;
581-
}
582-
}
583-
}
584-
585-
if (!signSuccess) {
561+
// Sign vote using shared helper function
562+
if (!SignGovernanceVote(*walletModel->wallet().wallet(), votingKeyID, vote)) {
586563
nFailed++;
587564
failedMessages.append(
588565
tr("Failed to sign vote for masternode %1").arg(QString::fromStdString(proTxHash.ToString())));

0 commit comments

Comments
 (0)