2020-06-20 07:10:00 +00:00
|
|
|
import 'dart:io';
|
2020-11-27 09:52:05 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_mnemonic.dart';
|
2020-12-03 19:34:56 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_mnemonic_is_incorrect_exception.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_wallet_creation_credentials.dart';
|
2020-11-12 16:31:53 +00:00
|
|
|
import 'package:cake_wallet/core/wallet_base.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:cake_wallet/core/wallet_service.dart';
|
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/pathForWallet.dart';
|
2020-11-12 16:31:53 +00:00
|
|
|
import 'package:cake_wallet/entities/wallet_info.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/wallet_type.dart';
|
2020-11-12 16:31:53 +00:00
|
|
|
import 'package:hive/hive.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
class BitcoinWalletService extends WalletService<
|
|
|
|
BitcoinNewWalletCredentials,
|
|
|
|
BitcoinRestoreWalletFromSeedCredentials,
|
|
|
|
BitcoinRestoreWalletFromWIFCredentials> {
|
2020-11-12 16:31:53 +00:00
|
|
|
BitcoinWalletService(this.walletInfoSource);
|
|
|
|
|
|
|
|
final Box<WalletInfo> walletInfoSource;
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
@override
|
|
|
|
WalletType getType() => WalletType.bitcoin;
|
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
@override
|
|
|
|
Future<BitcoinWallet> create(BitcoinNewWalletCredentials credentials) async {
|
2021-05-07 07:36:38 +00:00
|
|
|
final wallet = BitcoinWallet(
|
2021-05-10 10:06:57 +00:00
|
|
|
mnemonic: await generateMnemonic(),
|
2020-06-20 07:10:00 +00:00
|
|
|
password: credentials.password,
|
2020-11-12 16:31:53 +00:00
|
|
|
walletInfo: credentials.walletInfo);
|
2020-06-20 07:10:00 +00:00
|
|
|
await wallet.save();
|
|
|
|
await wallet.init();
|
|
|
|
return wallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> isWalletExit(String name) async =>
|
2021-05-07 07:36:38 +00:00
|
|
|
File(await pathForWallet(name: name, type: getType())).existsSync();
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<BitcoinWallet> openWallet(String name, String password) async {
|
2020-11-12 16:31:53 +00:00
|
|
|
final walletInfo = walletInfoSource.values.firstWhere(
|
2021-05-07 07:36:38 +00:00
|
|
|
(info) => info.id == WalletBase.idFor(name, getType()),
|
2020-11-12 16:31:53 +00:00
|
|
|
orElse: () => null);
|
2021-05-07 07:36:38 +00:00
|
|
|
final wallet = await BitcoinWalletBase.open(
|
|
|
|
password: password, name: name, walletInfo: walletInfo);
|
2020-06-20 07:10:00 +00:00
|
|
|
await wallet.init();
|
|
|
|
return wallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-08-27 16:54:34 +00:00
|
|
|
Future<void> remove(String wallet) async =>
|
|
|
|
File(await pathForWalletDir(name: wallet, type: WalletType.bitcoin))
|
|
|
|
.delete(recursive: true);
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<BitcoinWallet> restoreFromKeys(
|
2021-05-07 07:36:38 +00:00
|
|
|
BitcoinRestoreWalletFromWIFCredentials credentials) async =>
|
|
|
|
throw UnimplementedError();
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<BitcoinWallet> restoreFromSeed(
|
|
|
|
BitcoinRestoreWalletFromSeedCredentials credentials) async {
|
2020-12-03 19:34:56 +00:00
|
|
|
if (!validateMnemonic(credentials.mnemonic)) {
|
|
|
|
throw BitcoinMnemonicIsIncorrectException();
|
|
|
|
}
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
final wallet = BitcoinWallet(
|
2020-06-20 07:10:00 +00:00
|
|
|
password: credentials.password,
|
2020-11-12 16:31:53 +00:00
|
|
|
mnemonic: credentials.mnemonic,
|
|
|
|
walletInfo: credentials.walletInfo);
|
2020-06-20 07:10:00 +00:00
|
|
|
await wallet.save();
|
|
|
|
await wallet.init();
|
|
|
|
return wallet;
|
|
|
|
}
|
|
|
|
}
|