mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-10 21:04:53 +00:00
d106e14e74
Conflicts: cw_bitcoin/pubspec.yaml cw_core/lib/wallet_base.dart cw_monero/ios/Classes/monero_api.cpp lib/core/backup_service.dart lib/core/wallet_loading_service.dart lib/di.dart lib/main.dart lib/router.dart lib/src/screens/exchange/exchange_page.dart lib/src/screens/send/send_page.dart lib/src/screens/settings/security_backup_page.dart lib/src/screens/setup_2fa/setup_2fa_enter_code_page.dart lib/src/screens/wallet_list/wallet_list_page.dart lib/src/widgets/address_text_field.dart lib/store/settings_store.dart lib/view_model/exchange/exchange_view_model.dart lib/view_model/wallet_creation_vm.dart lib/view_model/wallet_new_vm.dart res/values/strings_yo.arb tool/configure.dart
89 lines
2.8 KiB
Dart
89 lines
2.8 KiB
Dart
import 'dart:convert';
|
|
import 'package:cw_bitcoin/encryption_file_utils.dart';
|
|
import 'package:cw_core/pathForWallet.dart';
|
|
import 'package:cw_core/wallet_info.dart';
|
|
import 'package:mobx/mobx.dart';
|
|
import 'package:cw_core/transaction_history.dart';
|
|
import 'package:cw_bitcoin/electrum_transaction_info.dart';
|
|
|
|
part 'electrum_transaction_history.g.dart';
|
|
|
|
const transactionsHistoryFileName = 'transactions.json';
|
|
|
|
class ElectrumTransactionHistory = ElectrumTransactionHistoryBase
|
|
with _$ElectrumTransactionHistory;
|
|
|
|
abstract class ElectrumTransactionHistoryBase
|
|
extends TransactionHistoryBase<ElectrumTransactionInfo> with Store {
|
|
ElectrumTransactionHistoryBase(
|
|
{required this.walletInfo, required String password, required this.encryptionFileUtils})
|
|
: _password = password,
|
|
_height = 0 {
|
|
transactions = ObservableMap<String, ElectrumTransactionInfo>();
|
|
}
|
|
|
|
final WalletInfo walletInfo;
|
|
final EncryptionFileUtils encryptionFileUtils;
|
|
String _password;
|
|
int _height;
|
|
|
|
Future<void> init() async => await _load();
|
|
|
|
@override
|
|
void addOne(ElectrumTransactionInfo transaction) =>
|
|
transactions[transaction.id] = transaction;
|
|
|
|
@override
|
|
void addMany(Map<String, ElectrumTransactionInfo> transactions) =>
|
|
transactions.forEach((_, tx) => _update(tx));
|
|
|
|
@override
|
|
Future<void> save() async {
|
|
try {
|
|
final dirPath =
|
|
await pathForWalletDir(name: walletInfo.name, type: walletInfo.type);
|
|
final path = '$dirPath/$transactionsHistoryFileName';
|
|
final data =
|
|
json.encode({'height': _height, 'transactions': transactions});
|
|
await encryptionFileUtils.write(path: path, password: _password, data: data);
|
|
} catch (e) {
|
|
print('Error while save bitcoin transaction history: ${e.toString()}');
|
|
}
|
|
}
|
|
|
|
Future<void> changePassword(String password) async {
|
|
_password = password;
|
|
await save();
|
|
}
|
|
|
|
Future<Map<String, dynamic>> _read() async {
|
|
final dirPath =
|
|
await pathForWalletDir(name: walletInfo.name, type: walletInfo.type);
|
|
final path = '$dirPath/$transactionsHistoryFileName';
|
|
final content = await encryptionFileUtils.read(path: path, password: _password);
|
|
return json.decode(content) as Map<String, dynamic>;
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
try {
|
|
final content = await _read();
|
|
final txs = content['transactions'] as Map<String, dynamic>? ?? {};
|
|
|
|
txs.entries.forEach((entry) {
|
|
final val = entry.value;
|
|
|
|
if (val is Map<String, dynamic>) {
|
|
final tx = ElectrumTransactionInfo.fromJson(val, walletInfo.type);
|
|
_update(tx);
|
|
}
|
|
});
|
|
|
|
_height = content['height'] as int;
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
void _update(ElectrumTransactionInfo transaction) =>
|
|
transactions[transaction.id] = transaction;
|
|
}
|