cake_wallet/cw_bitcoin/lib/electrum_wallet_snapshot.dart

71 lines
2.4 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:cw_bitcoin/bitcoin_address_record.dart';
import 'package:cw_bitcoin/electrum_balance.dart';
import 'package:cw_bitcoin/file.dart';
import 'package:cw_core/pathForWallet.dart';
2023-08-24 22:06:58 +00:00
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
class ElectrumWallletSnapshot {
2022-10-12 17:09:57 +00:00
ElectrumWallletSnapshot({
required this.name,
required this.type,
required this.password,
required this.mnemonic,
required this.addresses,
required this.balance,
required this.regularAddressIndex,
2023-08-24 22:06:58 +00:00
required this.changeAddressIndex,
this.derivationType,
this.derivationPath,
});
final String name;
final String password;
final WalletType type;
String mnemonic;
List<BitcoinAddressRecord> addresses;
ElectrumBalance balance;
int regularAddressIndex;
int changeAddressIndex;
2023-08-24 22:06:58 +00:00
DerivationType? derivationType;
String? derivationPath;
2022-10-12 17:09:57 +00:00
static Future<ElectrumWallletSnapshot> load(String name, WalletType type, String password) async {
final path = await pathForWallet(name: name, type: type);
final jsonSource = await read(path: path, password: password);
final data = json.decode(jsonSource) as Map;
final addressesTmp = data['addresses'] as List? ?? <Object>[];
final mnemonic = data['mnemonic'] as String;
final addresses = addressesTmp
.whereType<String>()
.map((addr) => BitcoinAddressRecord.fromJSON(addr))
.toList();
final balance = ElectrumBalance.fromJSON(data['balance'] as String) ??
ElectrumBalance(confirmed: 0, unconfirmed: 0, frozen: 0);
2022-10-12 17:09:57 +00:00
var regularAddressIndex = 0;
var changeAddressIndex = 0;
2023-08-25 15:12:07 +00:00
final derivationType = data['derivationType'] as DerivationType? ?? DerivationType.bip39;
final derivationPath = data['derivationPath'] as String? ?? "m/0'/1";
2023-08-24 22:06:58 +00:00
try {
2022-10-12 17:09:57 +00:00
regularAddressIndex = int.parse(data['account_index'] as String? ?? '0');
changeAddressIndex = int.parse(data['change_address_index'] as String? ?? '0');
} catch (_) {}
2022-10-12 17:09:57 +00:00
return ElectrumWallletSnapshot(
2023-08-24 22:06:58 +00:00
name: name,
type: type,
password: password,
mnemonic: mnemonic,
addresses: addresses,
balance: balance,
regularAddressIndex: regularAddressIndex,
changeAddressIndex: changeAddressIndex,
derivationType: derivationType,
derivationPath: derivationPath);
}
}