Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions cw_bitcoin/lib/bitcoin_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_core/payjoin_session.dart';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/utils/zpub.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_service.dart';
import 'package:cw_bitcoin/bitcoin_wallet.dart';
import 'package:cw_core/pathForWallet.dart';
Expand Down Expand Up @@ -130,31 +129,6 @@ class BitcoinWalletService extends WalletService<
}
}

@override
Future<void> rename(String currentName, String password, String newName) async {
final currentWalletInfo = await WalletInfo.get(currentName, getType());
if (currentWalletInfo == null) {
throw Exception('Wallet not found');
}
final currentWallet = await BitcoinWalletBase.open(
password: password,
name: currentName,
walletInfo: currentWalletInfo,
unspentCoinsInfo: unspentCoinsInfoSource,
payjoinBox: payjoinSessionSource,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);

await currentWallet.renameWalletFiles(newName);
await saveBackup(newName);

final newWalletInfo = currentWalletInfo;
newWalletInfo.id = WalletBase.idFor(newName, getType());
newWalletInfo.name = newName;

await newWalletInfo.save();
}

@override
Future<BitcoinWallet> restoreFromHardwareWallet(BitcoinRestoreWalletFromHardware credentials,
{bool? isTestnet}) async {
Expand Down
33 changes: 0 additions & 33 deletions cw_bitcoin/lib/electrum_wallet.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'package:bitcoin_base/bitcoin_base.dart';
Expand Down Expand Up @@ -34,7 +33,6 @@ import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_core/get_height_by_date.dart';
import 'package:cw_core/node.dart';
import 'package:cw_core/output_info.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/pending_transaction.dart';
import 'package:cw_core/sync_status.dart';
import 'package:cw_core/transaction_direction.dart';
Expand Down Expand Up @@ -1728,37 +1726,6 @@ abstract class ElectrumWalletBase
await transactionHistory.save();
}

@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);
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);
}
if (currentTransactionsFile.existsSync()) {
final newDirPath = await pathForWalletDir(name: newWalletName, type: type);
await currentTransactionsFile.copy('$newDirPath/$transactionsHistoryFileName');
}

// Delete old name's dir and files
final dir = Directory(currentDirPath);
for (var attempt = 0; attempt < 3; attempt++) {
try {
await dir.delete(recursive: true);
break;
} on FileSystemException {
if (attempt == 2) rethrow;
await Future<void>.delayed(const Duration(milliseconds: 200));
}
}
}

@override
Future<void> changePassword(String password) async {
_password = password;
Expand Down
35 changes: 14 additions & 21 deletions cw_bitcoin/lib/litecoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:bitcoin_base/src/crypto/keypair/sign_utils.dart';
import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:blockchain_utils/signer/ecdsa_signing_key.dart';
import 'package:collection/collection.dart';
import 'package:convert/convert.dart' as convert;
import 'package:crypto/crypto.dart';
import 'package:cw_bitcoin/bitcoin_address_record.dart';
import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
import 'package:cw_bitcoin/bitcoin_transaction_credentials.dart';
import 'package:cw_bitcoin/bitcoin_transaction_priority.dart';
import 'package:cw_bitcoin/bitcoin_unspent.dart';
import 'package:cw_bitcoin/electrum_balance.dart';
Expand All @@ -35,25 +32,18 @@ import 'package:cw_bitcoin/litecoin_wallet_addresses.dart';
import 'package:cw_bitcoin/pending_bitcoin_transaction.dart';
import 'package:cw_bitcoin/psbt/transaction_builder.dart';
import 'package:cw_bitcoin/utils.dart';
import 'package:cw_core/cake_hive.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_core/mweb_utxo.dart';
import 'package:cw_core/node.dart';
import 'package:cw_core/output_info.dart';
import 'package:cw_core/pending_transaction.dart';
import 'package:cw_core/sync_status.dart';
import 'package:cw_core/transaction_direction.dart';
import 'package:cw_core/transaction_priority.dart';
import 'package:cw_core/unspent_coin_type.dart';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_keys_file.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_mweb/cw_mweb.dart';
import 'package:cw_mweb/mwebd.pbgrpc.dart';
import 'package:fixnum/fixnum.dart';
import 'package:flutter/foundation.dart';
import 'package:grpc/grpc.dart';
import 'package:hive/hive.dart';
Expand Down Expand Up @@ -506,22 +496,25 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
mwebUtxosBox = await CakeHive.openBox<MwebUtxo>(boxName);
}

@override
Future<void> renameWalletFiles(String newWalletName) async {
// rename the hive box:
final oldBoxName = "${walletInfo.name.replaceAll(" ", "_")}_${MwebUtxo.boxName}";
final newBoxName = "${newWalletName.replaceAll(" ", "_")}_${MwebUtxo.boxName}";
static Future<void> copyMwebBox({
required String fromName,
required String toName,
}) async {
final oldBoxName = "${fromName.replaceAll(" ", "_")}_${MwebUtxo.boxName}";
final newBoxName = "${toName.replaceAll(" ", "_")}_${MwebUtxo.boxName}";
if (oldBoxName == newBoxName) return;

final oldBox = await CakeHive.openBox<MwebUtxo>(oldBoxName);
mwebUtxosBox = await CakeHive.openBox<MwebUtxo>(newBoxName);
final newBox = await CakeHive.openBox<MwebUtxo>(newBoxName);
for (final key in oldBox.keys) {
final value = oldBox.get(key);
await oldBox.delete(key);
await mwebUtxosBox.put(key, value!);
await newBox.put(key, oldBox.get(key)!);
}
oldBox.deleteFromDisk();
}

await super.renameWalletFiles(newWalletName);
static Future<void> deleteMwebBox(String name) async {
final boxName = "${name.replaceAll(" ", "_")}_${MwebUtxo.boxName}";
final box = await CakeHive.openBox<MwebUtxo>(boxName);
await box.deleteFromDisk();
}

@action
Expand Down
25 changes: 4 additions & 21 deletions cw_bitcoin/lib/litecoin_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import 'package:cw_core/wallet_service.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:collection/collection.dart';
import 'package:bip39/bip39.dart' as bip39;
import 'package:path_provider/path_provider.dart';

Expand Down Expand Up @@ -139,26 +137,11 @@ class LitecoinWalletService extends WalletService<

@override
Future<void> rename(String currentName, String password, String newName) async {
final currentWalletInfo = await WalletInfo.get(currentName, getType());
if (currentWalletInfo == null) {
throw Exception('Wallet not found');
}
final currentWallet = await LitecoinWalletBase.open(
password: password,
name: currentName,
walletInfo: currentWalletInfo,
unspentCoinsInfo: unspentCoinsInfoSource,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);

await currentWallet.renameWalletFiles(newName);
await saveBackup(newName);

final newWalletInfo = currentWalletInfo;
newWalletInfo.id = WalletBase.idFor(newName, getType());
newWalletInfo.name = newName;
if (currentName == newName) return;

await newWalletInfo.save();
await LitecoinWalletBase.copyMwebBox(fromName: currentName, toName: newName);
await super.rename(currentName, password, newName);
await LitecoinWalletBase.deleteMwebBox(currentName);
}

@override
Expand Down
25 changes: 0 additions & 25 deletions cw_bitcoin_cash/lib/src/bitcoin_cash_wallet_service.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import 'dart:io';

import 'package:bip39/bip39.dart';
import 'package:collection/collection.dart';
import 'package:cw_bitcoin/bitcoin_mnemonics_bip39.dart';
import 'package:cw_bitcoin_cash/cw_bitcoin_cash.dart';
import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_service.dart';
import 'package:cw_core/wallet_type.dart';
Expand Down Expand Up @@ -99,29 +97,6 @@ class BitcoinCashWalletService extends WalletService<
}
}

@override
Future<void> rename(String currentName, String password, String newName) async {
final currentWalletInfo = await WalletInfo.get(currentName, getType());
if (currentWalletInfo == null) {
throw Exception('Wallet not found');
}
final currentWallet = await BitcoinCashWalletBase.open(
password: password,
name: currentName,
walletInfo: currentWalletInfo,
unspentCoinsInfo: unspentCoinsInfoSource,
encryptionFileUtils: encryptionFileUtilsFor(isDirect));

await currentWallet.renameWalletFiles(newName);
await saveBackup(newName);

final newWalletInfo = currentWalletInfo;
newWalletInfo.id = WalletBase.idFor(newName, getType());
newWalletInfo.name = newName;

await newWalletInfo.save();
}

@override
Future<BitcoinCashWallet> restoreFromHardwareWallet(BitcoinCashNewWalletCredentials credentials) {
throw UnimplementedError(
Expand Down
41 changes: 41 additions & 0 deletions cw_core/lib/pathForWallet.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';
import 'package:cw_core/root_dir.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:path/path.dart' as p;

Future<String> pathForWalletTypeDir({required WalletType type}) async {
final root = await getAppDir();
Expand Down Expand Up @@ -36,3 +37,43 @@ Future<String> outdatedAndroidPathForWalletDir({required String name}) async {

return pathDir;
}

Future<void> copyWalletFilesTo({
required String fromName,
required String toName,
required WalletType type,
}) async {
if (fromName == toName) return;

final typeRoot = await pathForWalletTypeDir(type: type);
final sourceDir = Directory(p.join(typeRoot, fromName));
if (!sourceDir.existsSync()) {
throw "Source wallet not found: $fromName $type";
}

if (Directory(p.join(typeRoot, toName)).existsSync()) {
throw Exception('Cannot rename wallet: "$toName" already exists');
}

final destinationDir = Directory(p.join(typeRoot, toName));
await _copyDirectory(sourceDir, destinationDir);

for (final suffix in const ['', '.keys', '.keys.backup']) {
final file = File(p.join(destinationDir.path, '$fromName$suffix'));
if (file.existsSync()) {
await file.rename(p.join(destinationDir.path, '$toName$suffix'));
}
}
}

Future<void> _copyDirectory(Directory source, Directory destination) async {
await destination.create(recursive: true);
await for (final entity in source.list(followLinks: false)) {
final name = p.basename(entity.path);
if (entity is File) {
await entity.copy(p.join(destination.path, name));
} else if (entity is Directory) {
await _copyDirectory(entity, Directory(p.join(destination.path, name)));
}
}
}
4 changes: 3 additions & 1 deletion cw_core/lib/wallet_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/sync_status.dart';
import 'package:cw_core/node.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_core/pathForWallet.dart';

abstract class WalletBase<BalanceType extends Balance, HistoryType extends TransactionHistoryBase,
TransactionType extends TransactionInfo> {
Expand Down Expand Up @@ -117,7 +118,8 @@ abstract class WalletBase<BalanceType extends Balance, HistoryType extends Trans

void setExceptionHandler(void Function(FlutterErrorDetails) onError) => null;

Future<void> renameWalletFiles(String newWalletName);
Future<void> renameWalletFiles(String newWalletName) =>
copyWalletFilesTo(fromName: walletInfo.name, toName: newWalletName, type: type);

Future<String> signMessage(String message, {String? address = null});

Expand Down
28 changes: 27 additions & 1 deletion cw_core/lib/wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import 'dart:io';

import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/utils/file.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_credentials.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:path/path.dart' as p;

abstract class WalletService<N extends WalletCredentials, RFS extends WalletCredentials,
RFK extends WalletCredentials, RFH extends WalletCredentials> {
Expand All @@ -25,7 +28,30 @@ abstract class WalletService<N extends WalletCredentials, RFS extends WalletCred

Future<void> remove(String wallet);

Future<void> rename(String currentName, String password, String newName);
Future<void> rename(String currentName, String password, String newName) async {
if (currentName == newName) return;

final currentWalletInfo = await WalletInfo.get(currentName, getType());
if (currentWalletInfo == null) {
throw Exception('Wallet not found');
}

await copyWalletFilesTo(fromName: currentName, toName: newName, type: getType());
await saveBackup(newName);

currentWalletInfo.id = WalletBase.idFor(newName, getType());
currentWalletInfo.name = newName;
await currentWalletInfo.save();

final oldDir = Directory(p.join(await pathForWalletTypeDir(type: getType()), currentName));
if (oldDir.existsSync()) {
try {
await oldDir.delete(recursive: true);
Comment thread
OmarHatem28 marked this conversation as resolved.
} catch (e) {
printV('rename: failed to delete old wallet dir "$currentName": $e');
}
}
}

Future<void> restoreWalletFilesFromBackup(String name) async {
final backupWalletDirPath = await pathForWalletDir(name: "$name.backup", type: getType());
Expand Down
Loading
Loading