cake_wallet/cw_lightning/lib/lightning_wallet_service.dart

121 lines
4.3 KiB
Dart
Raw Normal View History

2024-02-08 19:45:21 +00:00
import 'dart:io';
2024-02-23 17:29:11 +00:00
import 'package:bitcoin_base/bitcoin_base.dart';
2024-02-08 19:59:06 +00:00
import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
2024-02-23 17:46:17 +00:00
import 'package:cw_bitcoin/mnemonic_is_incorrect_exception.dart';
2024-02-08 19:59:06 +00:00
import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart';
2024-02-08 19:45:21 +00:00
import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_service.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
2024-02-08 19:59:06 +00:00
import 'package:cw_lightning/lightning_wallet.dart';
2024-02-08 19:45:21 +00:00
import 'package:hive/hive.dart';
import 'package:collection/collection.dart';
2024-02-13 18:13:17 +00:00
class LightningWalletService extends WalletService<BitcoinNewWalletCredentials,
BitcoinRestoreWalletFromSeedCredentials, BitcoinRestoreWalletFromWIFCredentials> {
2024-02-08 19:59:06 +00:00
LightningWalletService(this.walletInfoSource, this.unspentCoinsInfoSource);
2024-02-08 19:45:21 +00:00
final Box<WalletInfo> walletInfoSource;
final Box<UnspentCoinsInfo> unspentCoinsInfoSource;
@override
2024-02-08 19:59:06 +00:00
WalletType getType() => WalletType.lightning;
2024-02-08 19:45:21 +00:00
@override
2024-02-23 17:46:17 +00:00
Future<LightningWallet> create(BitcoinNewWalletCredentials credentials, {bool? isTestnet}) async {
2024-02-08 19:45:21 +00:00
final wallet = await LightningWalletBase.create(
mnemonic: await generateMnemonic(),
password: credentials.password!,
walletInfo: credentials.walletInfo!,
unspentCoinsInfo: unspentCoinsInfoSource);
await wallet.save();
await wallet.init();
return wallet;
}
@override
Future<bool> isWalletExit(String name) async =>
File(await pathForWallet(name: name, type: getType())).existsSync();
@override
2024-02-08 19:59:06 +00:00
Future<LightningWallet> openWallet(String name, String password) async {
2024-02-13 18:13:17 +00:00
final walletInfo = walletInfoSource.values
.firstWhereOrNull((info) => info.id == WalletBase.idFor(name, getType()))!;
try {
final wallet = await LightningWalletBase.open(
password: password,
name: name,
walletInfo: walletInfo,
unspentCoinsInfo: unspentCoinsInfoSource);
await wallet.init();
saveBackup(name);
return wallet;
} catch (_) {
await restoreWalletFilesFromBackup(name);
final wallet = await LightningWalletBase.open(
password: password,
name: name,
walletInfo: walletInfo,
unspentCoinsInfo: unspentCoinsInfoSource);
await wallet.init();
return wallet;
}
2024-02-08 19:45:21 +00:00
}
@override
Future<void> remove(String wallet) async {
2024-02-13 18:13:17 +00:00
File(await pathForWalletDir(name: wallet, type: getType())).delete(recursive: true);
final walletInfo = walletInfoSource.values
.firstWhereOrNull((info) => info.id == WalletBase.idFor(wallet, getType()))!;
2024-02-08 19:45:21 +00:00
await walletInfoSource.delete(walletInfo.key);
}
@override
Future<void> rename(String currentName, String password, String newName) async {
2024-02-13 18:13:17 +00:00
final currentWalletInfo = walletInfoSource.values
.firstWhereOrNull((info) => info.id == WalletBase.idFor(currentName, getType()))!;
2024-02-08 19:59:06 +00:00
final currentWallet = await LightningWalletBase.open(
2024-02-08 19:45:21 +00:00
password: password,
name: currentName,
walletInfo: currentWalletInfo,
unspentCoinsInfo: unspentCoinsInfoSource);
await currentWallet.renameWalletFiles(newName);
2024-02-13 18:13:17 +00:00
await saveBackup(newName);
2024-02-08 19:45:21 +00:00
final newWalletInfo = currentWalletInfo;
newWalletInfo.id = WalletBase.idFor(newName, getType());
newWalletInfo.name = newName;
await walletInfoSource.put(currentWalletInfo.key, newWalletInfo);
}
@override
2024-02-23 17:46:17 +00:00
Future<LightningWallet> restoreFromKeys(BitcoinRestoreWalletFromWIFCredentials credentials, {bool? isTestnet}) async =>
2024-02-08 19:45:21 +00:00
throw UnimplementedError();
@override
2024-02-23 17:29:11 +00:00
Future<LightningWallet> restoreFromSeed(BitcoinRestoreWalletFromSeedCredentials credentials,
{bool? isTestnet}) async {
2024-02-08 19:45:21 +00:00
if (!validateMnemonic(credentials.mnemonic)) {
throw BitcoinMnemonicIsIncorrectException();
}
2024-02-23 17:29:11 +00:00
final network = isTestnet == true ? BitcoinNetwork.testnet : BitcoinNetwork.mainnet;
credentials.walletInfo?.network = network.value;
2024-02-08 19:59:06 +00:00
final wallet = await LightningWalletBase.create(
2024-02-23 17:29:11 +00:00
password: credentials.password!,
mnemonic: credentials.mnemonic,
walletInfo: credentials.walletInfo!,
unspentCoinsInfo: unspentCoinsInfoSource,
network: network,
);
2024-02-08 19:45:21 +00:00
await wallet.save();
await wallet.init();
return wallet;
}
2024-02-13 18:13:17 +00:00
}