Added selection of new wallet type. Returned support of bitcoin wallet.

This commit is contained in:
M
2020-11-12 18:31:53 +02:00
parent 37d3bf3fa9
commit e8f53766a0
10 changed files with 110 additions and 57 deletions
+6 -2
View File
@@ -58,6 +58,7 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
{@required String password,
@required String name,
@required String dirPath,
@required WalletInfo walletInfo,
String jsonSource}) {
final data = json.decode(jsonSource) as Map;
final mnemonic = data['mnemonic'] as String;
@@ -83,7 +84,8 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
name: name,
accountIndex: accountIndex,
initialAddresses: addresses,
initialBalance: balance);
initialBalance: balance,
walletInfo: walletInfo);
}
static BitcoinWallet build(
@@ -91,6 +93,7 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
@required String password,
@required String name,
@required String dirPath,
@required WalletInfo walletInfo,
List<BitcoinAddressRecord> initialAddresses,
BitcoinBalance initialBalance,
int accountIndex = 0}) {
@@ -107,7 +110,8 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
accountIndex: accountIndex,
initialAddresses: initialAddresses,
initialBalance: initialBalance,
transactionHistory: history);
transactionHistory: history,
walletInfo: walletInfo);
}
@override
@@ -1,21 +1,23 @@
import 'package:cake_wallet/core/wallet_credentials.dart';
import 'package:cake_wallet/entities/wallet_info.dart';
class BitcoinNewWalletCredentials extends WalletCredentials {
BitcoinNewWalletCredentials({String name}) : super(name: name);
BitcoinNewWalletCredentials({String name, WalletInfo walletInfo})
: super(name: name, walletInfo: walletInfo);
}
class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials {
BitcoinRestoreWalletFromSeedCredentials(
{String name, String password, this.mnemonic})
: super(name: name, password: password);
{String name, String password, this.mnemonic, WalletInfo walletInfo})
: super(name: name, password: password, walletInfo: walletInfo);
final String mnemonic;
}
class BitcoinRestoreWalletFromWIFCredentials extends WalletCredentials {
BitcoinRestoreWalletFromWIFCredentials(
{String name, String password, this.wif})
: super(name: name, password: password);
{String name, String password, this.wif, WalletInfo walletInfo})
: super(name: name, password: password, walletInfo: walletInfo);
final String wif;
}
+16 -3
View File
@@ -2,15 +2,22 @@ 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';
import 'package:cake_wallet/core/wallet_service.dart';
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
import 'package:cake_wallet/entities/pathForWallet.dart';
import 'package:cake_wallet/entities/wallet_info.dart';
import 'package:cake_wallet/entities/wallet_type.dart';
import 'package:hive/hive.dart';
class BitcoinWalletService extends WalletService<
BitcoinNewWalletCredentials,
BitcoinRestoreWalletFromSeedCredentials,
BitcoinRestoreWalletFromWIFCredentials> {
BitcoinWalletService(this.walletInfoSource);
final Box<WalletInfo> walletInfoSource;
@override
Future<BitcoinWallet> create(BitcoinNewWalletCredentials credentials) async {
final dirPath = await pathForWalletDir(
@@ -19,7 +26,8 @@ class BitcoinWalletService extends WalletService<
dirPath: dirPath,
mnemonic: bip39.generateMnemonic(),
password: credentials.password,
name: credentials.name);
name: credentials.name,
walletInfo: credentials.walletInfo);
await wallet.save();
await wallet.init();
@@ -37,11 +45,15 @@ class BitcoinWalletService extends WalletService<
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);
final wallet = BitcoinWalletBase.fromJSON(
password: password,
name: name,
dirPath: walletDirPath,
jsonSource: walletJSONRaw);
jsonSource: walletJSONRaw,
walletInfo: walletInfo);
await wallet.init();
return wallet;
@@ -68,7 +80,8 @@ class BitcoinWalletService extends WalletService<
dirPath: dirPath,
name: credentials.name,
password: credentials.password,
mnemonic: credentials.mnemonic);
mnemonic: credentials.mnemonic,
walletInfo: credentials.walletInfo);
await wallet.save();
await wallet.init();