From 20acc303fdf0ba8ae63cf111b141b8afb6143423 Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 30 Aug 2023 10:29:18 +0300 Subject: [PATCH] add ability to rename and delete --- .../lib/src/bitcoin_cash_base.dart | 3 - .../lib/src/bitcoin_cash_client.dart | 1 - .../src/bitcoin_cash_pending_transaction.dart | 25 ------- .../src/bitcoin_cash_transaction_history.dart | 38 ----------- .../src/bitcoin_cash_transaction_info.dart | 68 ------------------- .../lib/src/bitcoin_cash_wallet_service.dart | 28 ++++++-- 6 files changed, 22 insertions(+), 141 deletions(-) delete mode 100644 cw_bitcoin_cash/lib/src/bitcoin_cash_client.dart delete mode 100644 cw_bitcoin_cash/lib/src/bitcoin_cash_pending_transaction.dart delete mode 100644 cw_bitcoin_cash/lib/src/bitcoin_cash_transaction_history.dart delete mode 100644 cw_bitcoin_cash/lib/src/bitcoin_cash_transaction_info.dart diff --git a/cw_bitcoin_cash/lib/src/bitcoin_cash_base.dart b/cw_bitcoin_cash/lib/src/bitcoin_cash_base.dart index 7c6038f9b..10040eb02 100644 --- a/cw_bitcoin_cash/lib/src/bitcoin_cash_base.dart +++ b/cw_bitcoin_cash/lib/src/bitcoin_cash_base.dart @@ -1,7 +1,4 @@ export 'bitcoin_cash_balance.dart'; -export 'bitcoin_cash_client.dart'; -export 'bitcoin_cash_transaction_history.dart'; -export 'bitcoin_cash_transaction_info.dart'; export 'bitcoin_cash_wallet.dart'; export 'bitcoin_cash_wallet_addresses.dart'; export 'bitcoin_cash_wallet_creation_credentials.dart'; diff --git a/cw_bitcoin_cash/lib/src/bitcoin_cash_client.dart b/cw_bitcoin_cash/lib/src/bitcoin_cash_client.dart deleted file mode 100644 index 8632c7fba..000000000 --- a/cw_bitcoin_cash/lib/src/bitcoin_cash_client.dart +++ /dev/null @@ -1 +0,0 @@ -class BitcoinCashClient {} diff --git a/cw_bitcoin_cash/lib/src/bitcoin_cash_pending_transaction.dart b/cw_bitcoin_cash/lib/src/bitcoin_cash_pending_transaction.dart deleted file mode 100644 index 417f8d19a..000000000 --- a/cw_bitcoin_cash/lib/src/bitcoin_cash_pending_transaction.dart +++ /dev/null @@ -1,25 +0,0 @@ -import 'package:cw_core/pending_transaction.dart'; - -class BitcoinCashPendingTransaction with PendingTransaction { - @override - // TODO: implement amountFormatted - String get amountFormatted => throw UnimplementedError('amountFormatted is not implemented'); - - @override - Future commit() { - // TODO: implement commit - throw UnimplementedError('commit is not implemented'); - } - - @override - // TODO: implement feeFormatted - String get feeFormatted => throw UnimplementedError('feeFormatted is not implemented'); - - @override - // TODO: implement hex - String get hex => throw UnimplementedError('hex is not implemented'); - - @override - // TODO: implement id - String get id => throw UnimplementedError('id is not implemented'); -} diff --git a/cw_bitcoin_cash/lib/src/bitcoin_cash_transaction_history.dart b/cw_bitcoin_cash/lib/src/bitcoin_cash_transaction_history.dart deleted file mode 100644 index ec5873e59..000000000 --- a/cw_bitcoin_cash/lib/src/bitcoin_cash_transaction_history.dart +++ /dev/null @@ -1,38 +0,0 @@ -import 'dart:core'; - -import 'package:cw_core/transaction_history.dart'; -import 'package:cw_core/wallet_info.dart'; -import 'package:mobx/mobx.dart'; - -import 'bitcoin_cash_transaction_info.dart'; - -part 'bitcoin_cash_transaction_history.g.dart'; - -class BitcoinCashTransactionHistory = BitcoinCashTransactionHistoryBase - with _$BitcoinCashTransactionHistory; - -abstract class BitcoinCashTransactionHistoryBase - extends TransactionHistoryBase with Store { - BitcoinCashTransactionHistoryBase({required this.walletInfo, required String password}) - : _password = password, - _height = 0 { - transactions = ObservableMap(); - } - - final WalletInfo walletInfo; - String _password; - int _height; - - @override - Future save() async { - // TODO: implement - UnimplementedError('save() is not implemented'); - } - - @override - void addOne(BitcoinCashTransactionInfo transaction) => transactions[transaction.id] = transaction; - - @override - void addMany(Map transactions) => - this.transactions.addAll(transactions); -} diff --git a/cw_bitcoin_cash/lib/src/bitcoin_cash_transaction_info.dart b/cw_bitcoin_cash/lib/src/bitcoin_cash_transaction_info.dart deleted file mode 100644 index fc2cd85dc..000000000 --- a/cw_bitcoin_cash/lib/src/bitcoin_cash_transaction_info.dart +++ /dev/null @@ -1,68 +0,0 @@ -import 'package:cw_core/format_amount.dart'; -import 'package:cw_core/transaction_direction.dart'; -import 'package:cw_core/transaction_info.dart'; - -class BitcoinCashTransactionInfo extends TransactionInfo { - BitcoinCashTransactionInfo({ - required this.id, - required this.height, - required this.amount, - required this.fee, - this.tokenSymbol = "BCH", - this.exponent = 18, - required this.direction, - required this.isPending, - required this.date, - required this.confirmations, - }); - - final String id; - final int height; - final int amount; - final int exponent; - final TransactionDirection direction; - final DateTime date; - final bool isPending; - final int fee; - final int confirmations; - final String tokenSymbol; - String? _fiatAmount; - - @override - String amountFormatted() => - '${formatAmount((BigInt.from(amount) / BigInt.from(10).pow(exponent)).toString())} $tokenSymbol'; - - @override - String fiatAmount() => _fiatAmount ?? ''; - - @override - void changeFiatAmount(String amount) => _fiatAmount = formatAmount(amount); - - @override - String feeFormatted() => '${(BigInt.from(fee) / BigInt.from(10).pow(exponent)).toString()} BCH'; - - factory BitcoinCashTransactionInfo.fromJson(Map data) { - return BitcoinCashTransactionInfo( - id: data['id'] as String, - height: data['height'] as int, - amount: data['amount'] as int, - fee: data['fee'] as int, - direction: parseTransactionDirectionFromInt(data['direction'] as int), - date: DateTime.fromMillisecondsSinceEpoch(data['date'] as int), - isPending: data['isPending'] as bool, - confirmations: data['confirmations'] as int); - } - - Map toJson() { - final m = {}; - m['id'] = id; - m['height'] = height; - m['amount'] = amount; - m['direction'] = direction.index; - m['date'] = date.millisecondsSinceEpoch; - m['isPending'] = isPending; - m['confirmations'] = confirmations; - m['fee'] = fee; - return m; - } -} diff --git a/cw_bitcoin_cash/lib/src/bitcoin_cash_wallet_service.dart b/cw_bitcoin_cash/lib/src/bitcoin_cash_wallet_service.dart index 0906df73b..fa572b38b 100644 --- a/cw_bitcoin_cash/lib/src/bitcoin_cash_wallet_service.dart +++ b/cw_bitcoin_cash/lib/src/bitcoin_cash_wallet_service.dart @@ -54,15 +54,31 @@ class BitcoinCashWalletService extends WalletService remove(String wallet) { - // TODO: implement remove - throw UnimplementedError('remove() is not implemented'); + Future remove(String wallet) async { + File(await pathForWalletDir(name: wallet, type: getType())) + .delete(recursive: true); + final walletInfo = walletInfoSource.values.firstWhereOrNull( + (info) => info.id == WalletBase.idFor(wallet, getType()))!; + await walletInfoSource.delete(walletInfo.key); } @override - Future rename(String currentName, String password, String newName) { - // TODO: implement rename - throw UnimplementedError('rename() is not implemented'); + Future rename(String currentName, String password, String newName) async { + final currentWalletInfo = walletInfoSource.values.firstWhereOrNull( + (info) => info.id == WalletBase.idFor(currentName, getType()))!; + final currentWallet = await BitcoinCashWalletBase.open( + password: password, + name: currentName, + walletInfo: currentWalletInfo, + unspentCoinsInfo: unspentCoinsInfoSource); + + await currentWallet.renameWalletFiles(newName); + + final newWalletInfo = currentWalletInfo; + newWalletInfo.id = WalletBase.idFor(newName, getType()); + newWalletInfo.name = newName; + + await walletInfoSource.put(currentWalletInfo.key, newWalletInfo); } @override