What changed, and why it matters
This update fixes a flaw in Monero's wallet message-signing feature. Previously, a wallet that only held a view key (a 'view-only' wallet) could be asked to sign a message as if it held the spend key, producing a signature that looked valid but was actually created with the wrong private key. The fix makes the wallet verify it actually possesses the requested signing key before creating any signature, and it also adds checks that the requested address truly belongs to the wallet. This prevents misleading or unverifiable signatures from being generated.
Apply the patch and run the included functional, API, and unit tests. Users and integrators relying on message signing should upgrade, especially those exposing wallet RPC or view-only wallets, to ensure only verifiable signatures are produced.
Security signals we found
Prevents generation of unverifiable message signatures from wallets lacking the requested private key
Adds cryptographic key-ownership check inside wallet2::sign before signing
Adds address-belongs-to-wallet validation in WalletImpl::signMessage
Adds exception handling in wallet RPC signing path
Includes regression tests for view-only, subaddress, and wrong-address signing scenarios
Evidence from the diff
The patch hardens wallet2::sign() by deriving the public key from the selected secret key and comparing it to the expected public key (spend or view) for the requested address/subaddress. If the wallet lacks the requested key, it now throws. The wallet API and RPC layers catch these exceptions and report errors instead of silently returning invalid signatures. Additional API-level checks reject addresses that do not belong to the wallet. New tests cover view-only wallets, subaddress signing, and tampered addresses.
Changed components
src/wallet/api/wallet.cppsrc/wallet/wallet2.cppsrc/wallet/wallet_rpc_server.cpptests/functional_tests/sign_message.pytests/libwallet_api_tests/main.cpptests/unit_tests/wallet_storage.cppInspect captured patch +142 / −13
### src/wallet/api/wallet.cpp
@@ -2344,22 +2344,37 @@ std::string WalletImpl::signMessage(const std::string &message, const std::strin
if (checkBackgroundSync("cannot sign message"))
return "";
- if (address.empty()) {
- return m_wallet->sign(message, tools::wallet2::sign_with_spend_key);
- }
+ try
+ {
+ clearStatus();
+ if (address.empty()) {
+ return m_wallet->sign(message, tools::wallet2::sign_with_spend_key);
+ }
- cryptonote::address_parse_info info;
- if (!cryptonote::get_account_address_from_str(info, m_wallet->nettype(), address)) {
- setStatusError(tr("Failed to parse address"));
- return "";
+ cryptonote::address_parse_info info;
+ if (!cryptonote::get_account_address_from_str(info, m_wallet->nettype(), address)) {
+ setStatusError(tr("Failed to parse address"));
+ return "";
+ }
+ auto index = m_wallet->get_subaddress_index(info.address);
+ if (!index) {
+ setStatusError(tr("Address doesn't belong to the wallet"));
+ return "";
+ }
+ const cryptonote::account_public_address wallet_address = index->is_zero()
+ ? m_wallet->get_account().get_keys().m_account_address : m_wallet->get_subaddress(*index);
+ if (wallet_address != info.address) {
+ setStatusError(tr("Address doesn't belong to the wallet"));
+ return "";
+ }
+
+ return m_wallet->sign(message, tools::wallet2::sign_with_spend_key, *index);
}
- auto index = m_wallet->get_subaddress_index(info.address);
- if (!index) {
- setStatusError(tr("Address doesn't belong to the wallet"));
+ catch (const std::exception &e)
+ {
+ setStatusError(e.what());
return "";
}
-
- return m_wallet->sign(message, tools::wallet2::sign_with_spend_key, *index);
}
bool WalletImpl::verifySignedMessage(const std::string &message, const std::string &address, const std::string &signature) const
### src/wallet/wallet2.cpp
@@ -13765,6 +13765,15 @@ std::string wallet2::sign(const std::string &data, message_signature_type_t sign
secret_key_to_public_key(skey, pkey);
hash = get_message_hash(data,pkey_spend,pkey_view,mode);
}
+
+ const cryptonote::account_public_address address = index.is_zero()
+ ? keys.m_account_address : get_subaddress(index);
+ const crypto::public_key &expected_key = signature_type == sign_with_spend_key
+ ? address.m_spend_public_key : address.m_view_public_key;
+ crypto::public_key derived_key;
+ CHECK_AND_ASSERT_THROW_MES(crypto::secret_key_to_public_key(skey, derived_key) && derived_key == expected_key,
+ "Wallet does not have the requested message signing key");
+
crypto::generate_signature(hash, pkey, skey, signature);
return std::string("SigV2") + tools::base58::encode(std::string((const char *)&signature, sizeof(signature)));
}
### src/wallet/wallet_rpc_server.cpp
@@ -2692,7 +2692,15 @@ namespace tools
er.message = "Invalid signature type requested";
return false;
}
- res.signature = m_wallet->sign(req.data, signature_type, {req.account_index, req.address_index});
+ try
+ {
+ res.signature = m_wallet->sign(req.data, signature_type, {req.account_index, req.address_index});
+ }
+ catch (...)
+ {
+ handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR);
+ return false;
+ }
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
### tests/functional_tests/sign_message.py
@@ -45,6 +45,7 @@ def run_test(self):
self.check_signing(False, True)
self.check_signing(True, False)
self.check_signing(True, True)
+ self.check_view_only_signing()
def create(self):
print('Creating wallets')
@@ -101,5 +102,24 @@ def check_signing(self, subaddress, spend_key):
res = self.wallet[i].verify(message, address[0], signature.replace('SigV2','SigV1'))
assert not res.good
+ def check_view_only_signing(self):
+ view_only = Wallet(idx = 2)
+ try: view_only.close_wallet()
+ except: pass
+ view_key = self.wallet[0].query_key('view_key').key
+ view_only.generate_from_keys(address = self.address[0], viewkey = view_key)
+ subaddress_index = view_only.create_address().address_index
+
+ for mode, index in [('', 0), ('spend', 0), ('spend', subaddress_index), ('view', subaddress_index)]:
+ try:
+ view_only.sign('foo', address_index = index, signature_type = mode)
+ except AssertionError as error:
+ assert 'message signing key' in str(error)
+ else:
+ assert False, 'signing without the requested private key succeeded'
+
+ signature = view_only.sign('foo', signature_type = 'view').signature
+ assert self.wallet[0].verify('foo', self.address[0], signature).good
+
if __name__ == '__main__':
MessageSigningTest().run_test()
### tests/libwallet_api_tests/main.cpp
@@ -511,6 +511,51 @@ TEST_F(WalletManagerTest, FailedKeyRecoveryDoesNotOverwriteExistingWallet)
ASSERT_TRUE(wmgr->closeWallet(wallet));
}
+TEST_F(WalletManagerTest, WatchOnlyMessageSigningReportsError)
+{
+ const std::string message = "message to sign";
+ Monero::Wallet *full = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET);
+ ASSERT_EQ(Monero::Wallet::Status_Ok, full->status());
+ const std::string address = full->mainAddress();
+ const std::string view_key = full->secretViewKey();
+ const std::string signature = full->signMessage(message);
+ EXPECT_TRUE(full->verifySignedMessage(message, address, signature));
+ ASSERT_TRUE(wmgr->closeWallet(full));
+ Utils::deleteWallet(WALLET_NAME);
+
+ Monero::Wallet *view_only = wmgr->createWalletFromKeys(WALLET_NAME, WALLET_PASS, WALLET_LANG,
+ Monero::NetworkType::MAINNET, 0, address, view_key, "");
+ ASSERT_EQ(Monero::Wallet::Status_Ok, view_only->status());
+ ASSERT_TRUE(view_only->watchOnly());
+ EXPECT_TRUE(view_only->signMessage(message).empty());
+ EXPECT_EQ(Monero::Wallet::Status_Error, view_only->status());
+ EXPECT_NE(std::string::npos, view_only->errorString().find("message signing key"));
+ ASSERT_TRUE(wmgr->closeWallet(view_only));
+}
+
+TEST_F(WalletManagerTest, MessageSigningChecksRequestedAddress)
+{
+ Monero::Wallet *wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET);
+ ASSERT_EQ(Monero::Wallet::Status_Ok, wallet->status());
+ const std::string message = "message to sign";
+ const std::string address = wallet->mainAddress();
+ const std::string signature = wallet->signMessage(message, address);
+ ASSERT_FALSE(signature.empty());
+ EXPECT_TRUE(wallet->verifySignedMessage(message, address, signature));
+
+ cryptonote::address_parse_info info;
+ ASSERT_TRUE(cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, address));
+ cryptonote::account_base other;
+ other.generate();
+ info.address.m_view_public_key = other.get_keys().m_account_address.m_view_public_key;
+ const std::string wrong_address = cryptonote::get_account_address_as_str(cryptonote::MAINNET, false, info.address);
+
+ EXPECT_TRUE(wallet->signMessage(message, wrong_address).empty());
+ EXPECT_EQ(Monero::Wallet::Status_Error, wallet->status());
+ EXPECT_NE(std::string::npos, wallet->errorString().find("Address doesn't belong"));
+ ASSERT_TRUE(wmgr->closeWallet(wallet));
+}
+
TEST_F(WalletManagerTest, WalletManagerStoresWallet1)
{
### tests/unit_tests/wallet_storage.cpp
@@ -185,6 +185,38 @@ TEST(wallet_balance, batched_account_balances_match_individual_balances)
}
}
+TEST(wallet_sign, requires_the_selected_private_key)
+{
+ const epee::wipeable_string password("test-password");
+ const std::string message("message to sign");
+ const cryptonote::subaddress_index subaddress{0, 1};
+
+ tools::wallet2 full(cryptonote::MAINNET, 1, true);
+ full.generate("", password);
+ tools::wallet2 view_only(cryptonote::MAINNET, 1, true);
+ view_only.generate("", password, full.get_address(), full.get_account().get_keys().m_view_secret_key);
+ ASSERT_TRUE(view_only.watch_only());
+
+ for (const auto index : {cryptonote::subaddress_index{0, 0}, subaddress})
+ {
+ for (const auto type : {tools::wallet2::sign_with_spend_key, tools::wallet2::sign_with_view_key})
+ {
+ const std::string signature = full.sign(message, type, index);
+ const auto result = full.verify(message, full.get_subaddress(index), signature);
+ EXPECT_TRUE(result.valid);
+ EXPECT_EQ(type, result.type);
+ }
+ }
+
+ const std::string view_signature = view_only.sign(message, tools::wallet2::sign_with_view_key);
+ const auto view_result = full.verify(message, full.get_address(), view_signature);
+ EXPECT_TRUE(view_result.valid);
+ EXPECT_EQ(tools::wallet2::sign_with_view_key, view_result.type);
+ EXPECT_THROW(view_only.sign(message, tools::wallet2::sign_with_spend_key), std::runtime_error);
+ EXPECT_THROW(view_only.sign(message, tools::wallet2::sign_with_spend_key, subaddress), std::runtime_error);
+ EXPECT_THROW(view_only.sign(message, tools::wallet2::sign_with_view_key, subaddress), std::runtime_error);
+}
+
TEST(wallet_storage, upgrade_legacy_unencrypted_keys)
{
const path test_dir = temp_directory_path() / unique_path("monero-legacy-keys-%%%%-%%%%");Why this scored 60/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.