cake_wallet/lib/view_model/wallet_creation_vm.dart

85 lines
2.8 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/core/wallet_base.dart';
import 'package:cake_wallet/core/wallet_credentials.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:cake_wallet/store/app_store.dart';
import 'package:flutter/services.dart';
import 'dart:math';
part 'wallet_creation_vm.g.dart';
class WalletCreationVM = WalletCreationVMBase with _$WalletCreationVM;
abstract class WalletCreationVMBase with Store {
WalletCreationVMBase(this._appStore, this._walletInfoSource,
{@required this.type, @required this.isRecovery}) {
state = InitialExecutionState();
name = '';
}
@observable
String name;
@observable
ExecutionState state;
final WalletType type;
final bool isRecovery;
final Box<WalletInfo> _walletInfoSource;
final AppStore _appStore;
Future<String> generateName() async {
final adjectiveStringRaw =
await rootBundle.loadString('assets/text/Wallet_Adjectives.txt');
final nounStringRaw =
await rootBundle.loadString('assets/text/Wallet_Nouns.txt');
final randomThing = new Random();
final adjectives = new List<String>.from(adjectiveStringRaw.split("\n"));
final nouns = new List<String>.from(nounStringRaw.split("\n"));
final chosenAdjective = adjectives[randomThing.nextInt(adjectives.length)];
final chosenNoun = nouns[randomThing.nextInt(nouns.length)];
final returnString = chosenAdjective + " " + chosenNoun;
return returnString;
}
Future<void> create({dynamic options}) async {
try {
state = IsExecutingState();
final dirPath = await pathForWalletDir(name: name, type: type);
final path = await pathForWallet(name: name, type: type);
final credentials = getCredentials(options);
final walletInfo = WalletInfo.external(
id: WalletBase.idFor(name, type),
name: name,
type: type,
isRecovery: isRecovery,
restoreHeight: credentials.height ?? 0,
date: DateTime.now(),
path: path,
dirPath: dirPath);
credentials.walletInfo = walletInfo;
final wallet = await process(credentials);
await _walletInfoSource.add(walletInfo);
_appStore.wallet = wallet;
_appStore.authenticationStore.allowed();
state = ExecutedSuccessfullyState();
} catch (e) {
state = FailureState(e.toString());
}
}
WalletCredentials getCredentials(dynamic options) =>
throw UnimplementedError();
Future<WalletBase> process(WalletCredentials credentials) =>
throw UnimplementedError();
}