fix: refuse Bitcoin message signing for an address the wallet cannot derive (#3826)
What changed, and why it matters
This commit fixes a bug in Cake Wallet's Bitcoin message signing. Previously, if a user chose a silent payment address (a newer privacy-focused Bitcoin address type), the app would still sign the message using the wallet's main key. That signature would not actually prove ownership of the silent payment address shown on screen, which could mislead someone verifying the signature. Now the app refuses to sign for any address the wallet cannot directly derive, including silent payment and Taproot addresses, instead of silently falling back to the main key.
Review whether any other wallet operations (e.g., message verification display, export of signed messages, or similar flows in other coin modules) assume the signing address matches the displayed address. Ensure the new validation is consistently applied across all Bitcoin message signing entry points and that user-facing error messages explain why signing was refused.
Security signals we found
Cryptographic signature produced for a different identifier than the one presented to the user
Silent fallback to main wallet key when selected address cannot be derived
Message signing bypass for silent payment / unheld addresses
New explicit validation function with unit tests
Evidence from the diff
The change introduces assertCanSignBitcoinMessage() in cw_bitcoin/lib/bitcoin_message_sign.dart and calls it from ElectrumWalletBase.signMessage(). Before signing, it now checks that a non-blank address is held by the wallet and is not a Taproot address. Previously, an unheld address (such as a silent payment address) would cause addressRecord to be null, and signing would fall back to the wallet’s main HD key. The new guard throws UnsupportedError for unheld or Taproot addresses. Unit tests cover blank address fallback, unheld address rejection, Taproot rejection, and held non-Taproot allowance.
Changed components
cw_bitcoin/lib/bitcoin_message_sign.dartcw_bitcoin/lib/electrum_wallet.dartcw_bitcoin/test/bitcoin_message_sign_test.dartInspect captured patch +72 / −4
### cw_bitcoin/lib/bitcoin_message_sign.dart
@@ -0,0 +1,19 @@
+/// A blank address uses the wallet's main key. Any other address has to be
+/// one this wallet can derive. Silent payment addresses are not in that set,
+/// and signing them with the main key would look like proof for the wrong key.
+void assertCanSignBitcoinMessage({
+ required String? address,
+ required bool heldByWallet,
+ required bool isTaproot,
+}) {
+ final requested = address != null && address.trim().isNotEmpty;
+ if (!requested) {
+ return;
+ }
+ if (!heldByWallet) {
+ throw UnsupportedError('Cannot sign a message for an address this wallet does not hold');
+ }
+ if (isTaproot) {
+ throw UnsupportedError('Cannot sign message with Taproot address');
+ }
+}
### cw_bitcoin/lib/electrum_wallet.dart
@@ -21,6 +21,7 @@ import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:collection/collection.dart';
import 'package:cw_bitcoin/address_from_output.dart';
import 'package:cw_bitcoin/bitcoin_address_record.dart';
+import 'package:cw_bitcoin/bitcoin_message_sign.dart';
import 'package:cw_bitcoin/bitcoin_transaction_credentials.dart';
import 'package:cw_bitcoin/bitcoin_transaction_priority.dart';
import 'package:cw_bitcoin/bitcoin_unspent.dart';
@@ -4279,13 +4280,16 @@ abstract class ElectrumWalletBase
@override
Future<String> signMessage(String message, {String? address = null}) async {
- final addressRecord = address != null
+ final requested = address != null && address.trim().isNotEmpty;
+ final addressRecord = requested
? walletAddresses.allAddresses.firstWhereOrNull((addr) => addr.address == address)
: null;
- if (addressRecord != null && addressRecord.type == SegwitAddresType.p2tr) {
- throw UnsupportedError("Cannot sign message with Taproot address");
- }
+ assertCanSignBitcoinMessage(
+ address: address,
+ heldByWallet: addressRecord != null,
+ isTaproot: addressRecord?.type == SegwitAddresType.p2tr,
+ );
final hd = addressRecord != null
? _hdForAddressRecord(record: addressRecord).childKey(Bip32KeyIndex(addressRecord.index))
### cw_bitcoin/test/bitcoin_message_sign_test.dart
@@ -0,0 +1,45 @@
+import 'package:cw_bitcoin/bitcoin_message_sign.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+ test('blank address still uses the main key', () {
+ expect(
+ () => assertCanSignBitcoinMessage(address: '', heldByWallet: false, isTaproot: false),
+ returnsNormally,
+ );
+ expect(
+ () => assertCanSignBitcoinMessage(address: null, heldByWallet: false, isTaproot: false),
+ returnsNormally,
+ );
+ });
+
+ test('refuses an address the wallet does not hold', () {
+ expect(
+ () => assertCanSignBitcoinMessage(
+ address: 'sp1qqexample',
+ heldByWallet: false,
+ isTaproot: false,
+ ),
+ throwsA(isA<UnsupportedError>()),
+ );
+ });
+
+ test('refuses taproot and allows a held receive address', () {
+ expect(
+ () => assertCanSignBitcoinMessage(
+ address: 'bc1pexample',
+ heldByWallet: true,
+ isTaproot: true,
+ ),
+ throwsA(isA<UnsupportedError>()),
+ );
+ expect(
+ () => assertCanSignBitcoinMessage(
+ address: 'bc1qexample',
+ heldByWallet: true,
+ isTaproot: false,
+ ),
+ returnsNormally,
+ );
+ });
+}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.