diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index aadf87572..f9437e668 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -431,6 +431,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance, await transactionHistory.save(); } + @override Future<void> renameWalletFiles(String newWalletName) async { final currentWalletPath = await pathForWallet(name: walletInfo.name, type: type); final currentWalletFile = File(currentWalletPath); diff --git a/cw_core/lib/wallet_base.dart b/cw_core/lib/wallet_base.dart index e5f84f467..019f87631 100644 --- a/cw_core/lib/wallet_base.dart +++ b/cw_core/lib/wallet_base.dart @@ -75,4 +75,6 @@ abstract class WalletBase< Future<void>? updateBalance(); void setExceptionHandler(void Function(FlutterErrorDetails) onError) => null; + + Future<void> renameWalletFiles(String newWalletName); } diff --git a/cw_core/lib/wallet_service.dart b/cw_core/lib/wallet_service.dart index 5e216e225..f95bc1a44 100644 --- a/cw_core/lib/wallet_service.dart +++ b/cw_core/lib/wallet_service.dart @@ -18,5 +18,5 @@ abstract class WalletService<N extends WalletCredentials, Future<void> remove(String wallet); - Future<void> rename(String name, String password, String newName); + Future<void> rename(String currentName, String password, String newName); } diff --git a/cw_ethereum/lib/ethereum_wallet.dart b/cw_ethereum/lib/ethereum_wallet.dart index 988f596fb..05052fc99 100644 --- a/cw_ethereum/lib/ethereum_wallet.dart +++ b/cw_ethereum/lib/ethereum_wallet.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io'; import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/node.dart'; @@ -347,4 +348,29 @@ abstract class EthereumWalletBase initialErc20Tokens.forEach((token) => erc20TokensBox.put(token.contractAddress, token)); } + + @override + Future<void> renameWalletFiles(String newWalletName) async { + final currentWalletPath = await pathForWallet(name: walletInfo.name, type: type); + final currentWalletFile = File(currentWalletPath); + + final currentDirPath = + await pathForWalletDir(name: walletInfo.name, type: type); + // TODO: un-hash when transactions flow is implemented + // final currentTransactionsFile = File('$currentDirPath/$transactionsHistoryFileName'); + + // Copies current wallet files into new wallet name's dir and files + if (currentWalletFile.existsSync()) { + final newWalletPath = await pathForWallet(name: newWalletName, type: type); + await currentWalletFile.copy(newWalletPath); + } + // TODO: un-hash when transactions flow is implemented + // if (currentTransactionsFile.existsSync()) { + // final newDirPath = await pathForWalletDir(name: newWalletName, type: type); + // await currentTransactionsFile.copy('$newDirPath/$transactionsHistoryFileName'); + // } + + // Delete old name's dir and files + await Directory(currentDirPath).delete(recursive: true); + } } diff --git a/cw_ethereum/lib/ethereum_wallet_service.dart b/cw_ethereum/lib/ethereum_wallet_service.dart index d050b7bfc..16cf86ab6 100644 --- a/cw_ethereum/lib/ethereum_wallet_service.dart +++ b/cw_ethereum/lib/ethereum_wallet_service.dart @@ -84,4 +84,20 @@ class EthereumWalletService extends WalletService<EthereumNewWalletCredentials, return wallet; } + + @override + Future<void> rename(String currentName, String password, String newName) async { + final currentWalletInfo = walletInfoSource.values + .firstWhere((info) => info.id == WalletBase.idFor(currentName, getType())); + final currentWallet = await EthereumWalletBase.open( + password: password, name: currentName, walletInfo: currentWalletInfo); + + await currentWallet.renameWalletFiles(newName); + + final newWalletInfo = currentWalletInfo; + newWalletInfo.id = WalletBase.idFor(newName, getType()); + newWalletInfo.name = newName; + + await walletInfoSource.put(currentWalletInfo.key, newWalletInfo); + } } diff --git a/cw_haven/lib/haven_wallet.dart b/cw_haven/lib/haven_wallet.dart index ecd2a4b73..c1e505823 100644 --- a/cw_haven/lib/haven_wallet.dart +++ b/cw_haven/lib/haven_wallet.dart @@ -253,6 +253,7 @@ abstract class HavenWalletBase extends WalletBase<MoneroBalance, await haven_wallet.store(); } + @override Future<void> renameWalletFiles(String newWalletName) async { final currentWalletPath = await pathForWallet(name: name, type: type); final currentCacheFile = File(currentWalletPath); diff --git a/cw_monero/lib/monero_wallet.dart b/cw_monero/lib/monero_wallet.dart index 7592001b8..32ba20c8e 100644 --- a/cw_monero/lib/monero_wallet.dart +++ b/cw_monero/lib/monero_wallet.dart @@ -268,6 +268,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance, await monero_wallet.store(); } + @override Future<void> renameWalletFiles(String newWalletName) async { final currentWalletDirPath = await pathForWalletDir(name: name, type: type);