| 1 | import 'dart:convert'; |
| 2 | |
| 3 | import 'package:cw_bitcoin/electrum_transaction_info.dart'; |
| 4 | import 'package:cw_core/encryption_file_utils.dart'; |
| 5 | import 'package:cw_core/pathForWallet.dart'; |
| 6 | import 'package:cw_core/transaction_history.dart'; |
| 7 | import 'package:cw_core/utils/print_verbose.dart'; |
| 8 | import 'package:cw_core/wallet_info.dart'; |
| 9 | import 'package:mobx/mobx.dart'; |
| 10 | |
| 11 | part 'electrum_transaction_history.g.dart'; |
| 12 | |
| 13 | const transactionsHistoryFileName = 'transactions.json'; |
| 14 | |
| 15 | class ElectrumTransactionHistory = ElectrumTransactionHistoryBase with _$ElectrumTransactionHistory; |
| 16 | |
| 17 | abstract class ElectrumTransactionHistoryBase |
| 18 | extends TransactionHistoryBase<ElectrumTransactionInfo> with Store { |
| 19 | ElectrumTransactionHistoryBase( |
| 20 | {required this.walletInfo, required String password, required this.encryptionFileUtils}) |
| 21 | : _password = password, |
| 22 | _height = 0 { |
| 23 | transactions = ObservableMap<String, ElectrumTransactionInfo>(); |
| 24 | } |
| 25 | |
| 26 | final WalletInfo walletInfo; |
| 27 | final EncryptionFileUtils encryptionFileUtils; |
| 28 | String _password; |
| 29 | int _height; |
| 30 | |
| 31 | Future<void> init() async { |
| 32 | clear(); |
| 33 | await _load(); |
| 34 | } |
| 35 | |
| 36 | @override |
| 37 | void addOne(ElectrumTransactionInfo transaction) => transactions[transaction.id] = transaction; |
| 38 | |
| 39 | @override |
| 40 | void addMany(Map<String, ElectrumTransactionInfo> transactions) => |
| 41 | transactions.forEach((_, tx) => _update(tx)); |
| 42 | |
| 43 | @override |
| 44 | Future<void> save() async { |
| 45 | try { |
| 46 | final dirPath = await pathForWalletDir(name: walletInfo.name, type: walletInfo.type); |
| 47 | final path = '$dirPath/$transactionsHistoryFileName'; |
| 48 | final txjson = {}; |
| 49 | for (final tx in transactions.entries) { |
| 50 | txjson[tx.key] = tx.value.toJson(); |
| 51 | } |
| 52 | final data = json.encode({'height': _height, 'transactions': txjson}); |
| 53 | await encryptionFileUtils.write(path: path, password: _password, data: data); |
| 54 | } catch (e) { |
| 55 | printV('Error while save bitcoin transaction history: ${e.toString()}'); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | Future<void> changePassword(String password) async { |
| 60 | _password = password; |
| 61 | await save(); |
| 62 | } |
| 63 | |
| 64 | Future<Map<String, dynamic>> _read() async { |
| 65 | final dirPath = await pathForWalletDir(name: walletInfo.name, type: walletInfo.type); |
| 66 | final path = '$dirPath/$transactionsHistoryFileName'; |
| 67 | final content = await encryptionFileUtils.read(path: path, password: _password); |
| 68 | return json.decode(content) as Map<String, dynamic>; |
| 69 | } |
| 70 | |
| 71 | Future<void> _load() async { |
| 72 | try { |
| 73 | final content = await _read(); |
| 74 | final txs = content['transactions'] as Map<String, dynamic>? ?? {}; |
| 75 | |
| 76 | txs.entries.forEach((entry) { |
| 77 | final val = entry.value; |
| 78 | |
| 79 | if (val is Map<String, dynamic>) { |
| 80 | // removing transactions with invalid date |
| 81 | if (val['date'] == 1168650000) { |
| 82 | transactions.remove(entry.key); |
| 83 | } else { |
| 84 | final tx = ElectrumTransactionInfo.fromJson(val, walletInfo.type); |
| 85 | _update(tx); |
| 86 | } |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | _height = content['height'] as int; |
| 91 | } catch (e) { |
| 92 | printV(e); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void _update(ElectrumTransactionInfo transaction) => transactions[transaction.id] = transaction; |
| 97 | } |