mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-28 12:29:51 +00:00
TMP 4
This commit is contained in:
parent
719842964b
commit
81cee186db
94 changed files with 3786 additions and 3001 deletions
|
@ -1,34 +1,47 @@
|
|||
import 'package:cake_wallet/core/wallet_creation_state.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
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';
|
||||
import 'package:cake_wallet/core/wallet_credentials.dart';
|
||||
import 'package:cake_wallet/core/bitcoin_wallet_list_service.dart';
|
||||
import 'package:cake_wallet/core/monero_wallet_list_service.dart';
|
||||
import 'package:cake_wallet/core/wallet_list_service.dart';
|
||||
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';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/src/domain/common/secret_store_key.dart';
|
||||
import 'package:cake_wallet/src/domain/common/encrypt.dart';
|
||||
|
||||
part 'wallet_creation_service.g.dart';
|
||||
|
||||
class WalletCreationService = WalletCreationServiceBase
|
||||
with _$WalletCreationService;
|
||||
|
||||
abstract class WalletCreationServiceBase with Store {
|
||||
@observable
|
||||
WalletCreationState state;
|
||||
class WalletCreationService {
|
||||
WalletCreationService(
|
||||
{WalletType initialType,
|
||||
this.appStore,
|
||||
this.secureStorage,
|
||||
this.sharedPreferences})
|
||||
: type = initialType {
|
||||
if (type != null) {
|
||||
changeWalletType(type: type);
|
||||
}
|
||||
}
|
||||
|
||||
WalletType type;
|
||||
final AppStore appStore;
|
||||
final FlutterSecureStorage secureStorage;
|
||||
final SharedPreferences sharedPreferences;
|
||||
|
||||
WalletListService _service;
|
||||
// final WalletService walletService;
|
||||
// final Box<WalletInfo> walletInfoSource;
|
||||
|
||||
WalletService _service;
|
||||
|
||||
void changeWalletType({@required WalletType type}) {
|
||||
this.type = type;
|
||||
|
||||
switch (type) {
|
||||
case WalletType.monero:
|
||||
_service = MoneroWalletListService();
|
||||
_service = MoneroWalletService();
|
||||
break;
|
||||
case WalletType.bitcoin:
|
||||
_service = BitcoinWalletListService();
|
||||
_service = BitcoinWalletService();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@ -36,32 +49,45 @@ abstract class WalletCreationServiceBase with Store {
|
|||
}
|
||||
|
||||
Future<void> create(WalletCredentials credentials) async {
|
||||
try {
|
||||
state = WalletCreating();
|
||||
await _service.create(credentials);
|
||||
state = WalletCreatedSuccessfully();
|
||||
} catch (e) {
|
||||
state = WalletCreationFailure(error: e.toString());
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
Future<void> restoreFromKeys(WalletCredentials credentials) async {
|
||||
try {
|
||||
state = WalletCreating();
|
||||
await _service.restoreFromKeys(credentials);
|
||||
state = WalletCreatedSuccessfully();
|
||||
} catch (e) {
|
||||
state = WalletCreationFailure(error: e.toString());
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
Future<void> restoreFromSeed(WalletCredentials credentials) async {
|
||||
try {
|
||||
state = WalletCreating();
|
||||
await _service.restoreFromSeed(credentials);
|
||||
state = WalletCreatedSuccessfully();
|
||||
} catch (e) {
|
||||
state = WalletCreationFailure(error: e.toString());
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue