CakeWallet/lib/core/wallet_creation_service.dart

94 lines
3.2 KiB
Dart
Raw Normal View History

2020-06-01 21:13:56 +03:00
import 'package:flutter/foundation.dart';
2020-06-20 10:10:00 +03:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:cake_wallet/core/generate_wallet_password.dart';
import 'package:cake_wallet/store/app_store.dart';
2020-06-01 21:13:56 +03:00
import 'package:cake_wallet/core/wallet_credentials.dart';
2020-06-20 10:10:00 +03:00
import 'package:cake_wallet/bitcoin/bitcoin_wallet_service.dart';
import 'package:cake_wallet/monero/monero_wallet_service.dart';
import 'package:cake_wallet/core/wallet_service.dart';
2020-06-01 21:13:56 +03:00
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
2020-06-20 10:10:00 +03:00
import 'package:cake_wallet/src/domain/common/secret_store_key.dart';
import 'package:cake_wallet/src/domain/common/encrypt.dart';
2020-06-01 21:13:56 +03:00
2020-06-20 10:10:00 +03:00
class WalletCreationService {
WalletCreationService(
{WalletType initialType,
this.appStore,
this.secureStorage,
this.sharedPreferences})
: type = initialType {
if (type != null) {
changeWalletType(type: type);
}
}
2020-06-01 21:13:56 +03:00
2020-06-03 12:56:23 +03:00
WalletType type;
2020-06-20 10:10:00 +03:00
final AppStore appStore;
final FlutterSecureStorage secureStorage;
final SharedPreferences sharedPreferences;
2020-06-03 12:56:23 +03:00
2020-06-20 10:10:00 +03:00
// final WalletService walletService;
// final Box<WalletInfo> walletInfoSource;
WalletService _service;
2020-06-01 21:13:56 +03:00
void changeWalletType({@required WalletType type}) {
2020-06-03 12:56:23 +03:00
this.type = type;
2020-06-01 21:13:56 +03:00
switch (type) {
case WalletType.monero:
2020-06-20 10:10:00 +03:00
_service = MoneroWalletService();
2020-06-01 21:13:56 +03:00
break;
case WalletType.bitcoin:
2020-06-20 10:10:00 +03:00
_service = BitcoinWalletService();
2020-06-01 21:13:56 +03:00
break;
default:
break;
}
}
Future<void> create(WalletCredentials credentials) async {
2020-06-20 10:10:00 +03:00
final password = generateWalletPassword(type);
credentials.password = password;
await saveWalletPassword(password: password, walletName: credentials.name);
final wallet = await _service.create(credentials);
appStore.wallet = wallet;
appStore.authenticationStore.allowed();
2020-06-01 21:13:56 +03:00
}
Future<void> restoreFromKeys(WalletCredentials credentials) async {
2020-06-20 10:10:00 +03:00
final password = generateWalletPassword(type);
credentials.password = password;
await saveWalletPassword(password: password, walletName: credentials.name);
final wallet = await _service.restoreFromKeys(credentials);
appStore.wallet = wallet;
appStore.authenticationStore.allowed();
2020-06-01 21:13:56 +03:00
}
Future<void> restoreFromSeed(WalletCredentials credentials) async {
2020-06-20 10:10:00 +03:00
final password = generateWalletPassword(type);
credentials.password = password;
await saveWalletPassword(password: password, walletName: credentials.name);
final wallet = await _service.restoreFromSeed(credentials);
appStore.wallet = wallet;
appStore.authenticationStore.allowed();
}
Future<String> getWalletPassword({String walletName}) async {
final key = generateStoreKeyFor(
key: SecretStoreKey.moneroWalletPassword, walletName: walletName);
final encodedPassword = await secureStorage.read(key: key);
return decodeWalletPassword(password: encodedPassword);
}
Future<void> saveWalletPassword({String walletName, String password}) async {
final key = generateStoreKeyFor(
key: SecretStoreKey.moneroWalletPassword, walletName: walletName);
final encodedPassword = encodeWalletPassword(password: password);
await secureStorage.write(key: key, value: encodedPassword);
2020-06-01 21:13:56 +03:00
}
}