From ff0bdf8c7ffe7ae210a30651153473a4aac0582a Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Tue, 9 Apr 2024 12:06:03 -0700 Subject: [PATCH] updates & verification for electrum currencies --- cw_bitcoin/lib/electrum_wallet.dart | 9 +++++++++ cw_bitcoin_cash/lib/src/bitcoin_cash_wallet.dart | 11 +++++++++++ cw_core/lib/wallet_base.dart | 7 +++++-- cw_ethereum/lib/ethereum_wallet.dart | 5 +++++ cw_solana/lib/solana_wallet.dart | 5 +++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 4d1cfefde..e3b33a661 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1229,6 +1229,15 @@ abstract class ElectrumWalletBase return base64Encode(HD.signMessage(message)); } + @override + Future verifyMessage(String message, String signature, {String? address = null}) async { + final index = address != null + ? walletAddresses.allAddresses.firstWhere((element) => element.address == address).index + : null; + final HD = index == null ? hd : hd.derive(index); + return HD.verify(message: message, signature: base64Decode(signature)); + } + static BasedUtxoNetwork _getNetwork(bitcoin.NetworkType networkType, CryptoCurrency? currency) { if (networkType == bitcoin.bitcoin && currency == CryptoCurrency.bch) { return BitcoinCashNetwork.mainnet; diff --git a/cw_bitcoin_cash/lib/src/bitcoin_cash_wallet.dart b/cw_bitcoin_cash/lib/src/bitcoin_cash_wallet.dart index b7b60a31d..efa914702 100644 --- a/cw_bitcoin_cash/lib/src/bitcoin_cash_wallet.dart +++ b/cw_bitcoin_cash/lib/src/bitcoin_cash_wallet.dart @@ -175,4 +175,15 @@ abstract class BitcoinCashWalletBase extends ElectrumWallet with Store { final HD = index == null ? hd : hd.derive(index); return base64Encode(HD.signMessage(message)); } + + @override + Future verifyMessage(String message, String signature, {String? address = null}) async { + final index = address != null + ? walletAddresses.allAddresses + .firstWhere((element) => element.address == AddressUtils.toLegacyAddress(address)) + .index + : null; + final HD = index == null ? hd : hd.derive(index); + return HD.verify(message: message, signature: base64Decode(signature)); + } } diff --git a/cw_core/lib/wallet_base.dart b/cw_core/lib/wallet_base.dart index 41d19c6c4..e9bf36020 100644 --- a/cw_core/lib/wallet_base.dart +++ b/cw_core/lib/wallet_base.dart @@ -67,7 +67,6 @@ abstract class WalletBase renameWalletFiles(String newWalletName); - Future signMessage(String message, {String? address = null}) => throw UnimplementedError(); + Future signMessage(String message, {String? address = null}) => + throw UnimplementedError(); + + Future verifyMessage(String message, String signature, {String? address = null}) => + throw UnimplementedError(); bool? isTestnet; } diff --git a/cw_ethereum/lib/ethereum_wallet.dart b/cw_ethereum/lib/ethereum_wallet.dart index 4604db662..f9234ebc8 100644 --- a/cw_ethereum/lib/ethereum_wallet.dart +++ b/cw_ethereum/lib/ethereum_wallet.dart @@ -119,6 +119,11 @@ class EthereumWallet extends EVMChainWallet { return EthereumTransactionHistory(walletInfo: walletInfo, password: password); } + @override + Future signMessage(String message, {String? address}) async { + throw UnimplementedError(); + } + static Future open( {required String name, required String password, required WalletInfo walletInfo}) async { final path = await pathForWallet(name: name, type: walletInfo.type); diff --git a/cw_solana/lib/solana_wallet.dart b/cw_solana/lib/solana_wallet.dart index b01fac9ad..d1e6da897 100644 --- a/cw_solana/lib/solana_wallet.dart +++ b/cw_solana/lib/solana_wallet.dart @@ -527,5 +527,10 @@ abstract class SolanaWalletBase return signSolanaMessage(message); } + @override + Future verifyMessage(String message, String signature, {String? address}) async { + throw UnimplementedError(); + } + SolanaClient? get solanaClient => _client.getSolanaClient; }