cake_wallet/lib/bitcoin/bitcoin_wallet_service.dart

91 lines
3 KiB
Dart
Raw Normal View History

2020-06-20 07:10:00 +00:00
import 'dart:io';
import 'package:bip39/bip39.dart' as bip39;
import 'package:cake_wallet/bitcoin/file.dart';
import 'package:cake_wallet/bitcoin/bitcoin_wallet_creation_credentials.dart';
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';
import 'package:cake_wallet/entities/wallet_info.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/wallet_type.dart';
import 'package:hive/hive.dart';
2020-06-20 07:10:00 +00:00
class BitcoinWalletService extends WalletService<
BitcoinNewWalletCredentials,
BitcoinRestoreWalletFromSeedCredentials,
BitcoinRestoreWalletFromWIFCredentials> {
BitcoinWalletService(this.walletInfoSource);
final Box<WalletInfo> walletInfoSource;
2020-06-20 07:10:00 +00:00
@override
Future<BitcoinWallet> create(BitcoinNewWalletCredentials credentials) async {
final dirPath = await pathForWalletDir(
type: WalletType.bitcoin, name: credentials.name);
final wallet = BitcoinWalletBase.build(
dirPath: dirPath,
mnemonic: bip39.generateMnemonic(),
password: credentials.password,
name: credentials.name,
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 =>
File(await pathForWallet(name: name, type: WalletType.bitcoin))
.existsSync();
@override
Future<BitcoinWallet> openWallet(String name, String password) async {
final walletDirPath =
await pathForWalletDir(name: name, type: WalletType.bitcoin);
final walletPath = '$walletDirPath/$name';
final walletJSONRaw = await read(path: walletPath, password: password);
final walletInfo = walletInfoSource.values.firstWhere(
(info) => info.id == WalletBase.idFor(name, WalletType.bitcoin),
orElse: () => null);
2020-06-20 07:10:00 +00:00
final wallet = BitcoinWalletBase.fromJSON(
password: password,
name: name,
dirPath: walletDirPath,
jsonSource: walletJSONRaw,
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(
BitcoinRestoreWalletFromWIFCredentials credentials) async {
// TODO: implement restoreFromKeys
throw UnimplementedError();
}
@override
Future<BitcoinWallet> restoreFromSeed(
BitcoinRestoreWalletFromSeedCredentials credentials) async {
final dirPath = await pathForWalletDir(
type: WalletType.bitcoin, name: credentials.name);
final wallet = BitcoinWalletBase.build(
dirPath: dirPath,
name: credentials.name,
password: credentials.password,
mnemonic: credentials.mnemonic,
walletInfo: credentials.walletInfo);
2020-06-20 07:10:00 +00:00
await wallet.save();
await wallet.init();
return wallet;
}
}