2021-12-24 12:52:08 +00:00
|
|
|
import 'dart:convert';
|
2023-12-02 01:02:55 +00:00
|
|
|
|
|
|
|
import 'package:cw_bitcoin/electrum_transaction_info.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_core/pathForWallet.dart';
|
2023-12-02 01:02:55 +00:00
|
|
|
import 'package:cw_core/transaction_history.dart';
|
|
|
|
import 'package:cw_core/utils/file.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_core/wallet_info.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
|
|
|
part 'electrum_transaction_history.g.dart';
|
|
|
|
|
2023-07-12 23:20:11 +00:00
|
|
|
const transactionsHistoryFileName = 'transactions.json';
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
class ElectrumTransactionHistory = ElectrumTransactionHistoryBase
|
|
|
|
with _$ElectrumTransactionHistory;
|
|
|
|
|
|
|
|
abstract class ElectrumTransactionHistoryBase
|
|
|
|
extends TransactionHistoryBase<ElectrumTransactionInfo> with Store {
|
|
|
|
ElectrumTransactionHistoryBase(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required this.walletInfo, required String password})
|
2021-12-24 12:52:08 +00:00
|
|
|
: _password = password,
|
|
|
|
_height = 0 {
|
|
|
|
transactions = ObservableMap<String, ElectrumTransactionInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
final WalletInfo walletInfo;
|
2022-07-19 14:29:28 +00:00
|
|
|
String _password;
|
2021-12-24 12:52:08 +00:00
|
|
|
int _height;
|
|
|
|
|
|
|
|
Future<void> init() async => await _load();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void addOne(ElectrumTransactionInfo transaction) =>
|
|
|
|
transactions[transaction.id] = transaction;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void addMany(Map<String, ElectrumTransactionInfo> transactions) =>
|
2023-06-14 20:53:58 +00:00
|
|
|
transactions.forEach((_, tx) => _update(tx));
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> save() async {
|
|
|
|
try {
|
|
|
|
final dirPath =
|
|
|
|
await pathForWalletDir(name: walletInfo.name, type: walletInfo.type);
|
2023-07-12 23:20:11 +00:00
|
|
|
final path = '$dirPath/$transactionsHistoryFileName';
|
2021-12-24 12:52:08 +00:00
|
|
|
final data =
|
|
|
|
json.encode({'height': _height, 'transactions': transactions});
|
|
|
|
await writeData(path: path, password: _password, data: data);
|
|
|
|
} catch (e) {
|
|
|
|
print('Error while save bitcoin transaction history: ${e.toString()}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:29:28 +00:00
|
|
|
Future<void> changePassword(String password) async {
|
|
|
|
_password = password;
|
|
|
|
await save();
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Map<String, dynamic>> _read() async {
|
2021-12-24 12:52:08 +00:00
|
|
|
final dirPath =
|
|
|
|
await pathForWalletDir(name: walletInfo.name, type: walletInfo.type);
|
2023-07-12 23:20:11 +00:00
|
|
|
final path = '$dirPath/$transactionsHistoryFileName';
|
2021-12-24 12:52:08 +00:00
|
|
|
final content = await read(path: path, password: _password);
|
2022-10-12 17:09:57 +00:00
|
|
|
return json.decode(content) as Map<String, dynamic>;
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _load() async {
|
|
|
|
try {
|
|
|
|
final content = await _read();
|
2023-08-04 17:01:49 +00:00
|
|
|
final txs = content['transactions'] as Map<String, dynamic>? ?? {};
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
txs.entries.forEach((entry) {
|
|
|
|
final val = entry.value;
|
|
|
|
|
2023-01-03 21:59:08 +00:00
|
|
|
if (val is Map<String, dynamic>) {
|
2021-12-24 12:52:08 +00:00
|
|
|
final tx = ElectrumTransactionInfo.fromJson(val, walletInfo.type);
|
2023-06-14 20:53:58 +00:00
|
|
|
_update(tx);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_height = content['height'] as int;
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 20:53:58 +00:00
|
|
|
void _update(ElectrumTransactionInfo transaction) =>
|
2021-12-24 12:52:08 +00:00
|
|
|
transactions[transaction.id] = transaction;
|
2023-07-11 17:56:01 +00:00
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|