mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-17 09:46:39 +01:00
TMP 4
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
import 'package:cake_wallet/utils/list_item.dart';
|
||||
|
||||
class AccountListHeader extends ListItem {}
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/core/wallet_base.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
|
||||
import 'package:cake_wallet/monero/monero_wallet.dart';
|
||||
|
||||
part 'address_edit_or_create_view_model.g.dart';
|
||||
|
||||
class AddressEditOrCreateViewModel = AddressEditOrCreateViewModelBase
|
||||
with _$AddressEditOrCreateViewModel;
|
||||
|
||||
abstract class AddressEditOrCreateState {}
|
||||
|
||||
class AddressEditOrCreateStateInitial extends AddressEditOrCreateState {}
|
||||
|
||||
class AddressIsSaving extends AddressEditOrCreateState {}
|
||||
|
||||
class AddressSavedSuccessfully extends AddressEditOrCreateState {}
|
||||
|
||||
class AddressEditOrCreateStateFailure extends AddressEditOrCreateState {
|
||||
AddressEditOrCreateStateFailure({this.error});
|
||||
|
||||
String error;
|
||||
}
|
||||
|
||||
abstract class AddressEditOrCreateViewModelBase with Store {
|
||||
AddressEditOrCreateViewModelBase({@required WalletBase wallet, dynamic item})
|
||||
: isEdit = item != null,
|
||||
state = AddressEditOrCreateStateInitial(),
|
||||
label = item?.name as String,
|
||||
_item = item,
|
||||
_wallet = wallet;
|
||||
|
||||
dynamic _item;
|
||||
|
||||
@observable
|
||||
AddressEditOrCreateState state;
|
||||
|
||||
@observable
|
||||
String label;
|
||||
|
||||
bool isEdit;
|
||||
|
||||
final WalletBase _wallet;
|
||||
|
||||
Future<void> save() async {
|
||||
final wallet = _wallet;
|
||||
|
||||
try {
|
||||
state = AddressIsSaving();
|
||||
|
||||
if (isEdit) {
|
||||
await _update();
|
||||
} else {
|
||||
await _createNew();
|
||||
}
|
||||
|
||||
state = AddressSavedSuccessfully();
|
||||
} catch (e) {
|
||||
state = AddressEditOrCreateStateFailure(error: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _createNew() async {
|
||||
final wallet = _wallet;
|
||||
|
||||
if (wallet is BitcoinWallet) {
|
||||
await wallet.generateNewAddress(label: label);
|
||||
}
|
||||
|
||||
if (wallet is MoneroWallet) {
|
||||
await wallet.subaddressList
|
||||
.addSubaddress(accountIndex: wallet.account.id, label: label);
|
||||
await wallet.save();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _update() async {
|
||||
final wallet = _wallet;
|
||||
|
||||
if (wallet is BitcoinWallet) {
|
||||
await wallet.updateAddress(_item.address as String, label: label);
|
||||
}
|
||||
|
||||
if (wallet is MoneroWallet) {
|
||||
await wallet.subaddressList.setLabelSubaddress(
|
||||
accountIndex: wallet.account.id,
|
||||
addressIndex: _item.id as int,
|
||||
label: label);
|
||||
await wallet.save();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import 'package:cake_wallet/utils/list_item.dart';
|
||||
|
||||
class AddressListHeader extends ListItem {}
|
||||
@@ -0,0 +1,14 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:cake_wallet/utils/list_item.dart';
|
||||
|
||||
class AddressListItem extends ListItem {
|
||||
const AddressListItem({@required this.address, this.name, this.id})
|
||||
: super();
|
||||
|
||||
final int id;
|
||||
final String address;
|
||||
final String name;
|
||||
|
||||
@override
|
||||
String toString() => name ?? address;
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
|
||||
import 'package:cake_wallet/monero/monero_wallet.dart';
|
||||
import 'package:cake_wallet/core/wallet_base.dart';
|
||||
import 'package:cake_wallet/utils/list_item.dart';
|
||||
import 'package:cake_wallet/view_model/address_list/account_list_header.dart';
|
||||
import 'package:cake_wallet/view_model/address_list/address_list_header.dart';
|
||||
import 'package:cake_wallet/view_model/address_list/address_list_item.dart';
|
||||
|
||||
part 'address_list_view_model.g.dart';
|
||||
|
||||
class AddressListViewModel = AddressListViewModelBase
|
||||
with _$AddressListViewModel;
|
||||
|
||||
abstract class PaymentURI {
|
||||
PaymentURI({this.amount, this.address});
|
||||
|
||||
final String amount;
|
||||
final String address;
|
||||
}
|
||||
|
||||
class MoneroURI extends PaymentURI {
|
||||
MoneroURI({String amount, String address})
|
||||
: super(amount: amount, address: address);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
var base = 'monero:' + address;
|
||||
|
||||
if (amount?.isNotEmpty ?? false) {
|
||||
base += '?tx_amount=$amount';
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
}
|
||||
|
||||
class BitcoinURI extends PaymentURI {
|
||||
BitcoinURI({String amount, String address})
|
||||
: super(amount: amount, address: address);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
var base = 'bitcoin:' + address;
|
||||
|
||||
if (amount?.isNotEmpty ?? false) {
|
||||
base += '?amount=$amount';
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
}
|
||||
|
||||
abstract class AddressListViewModelBase with Store {
|
||||
AddressListViewModelBase({@required WalletBase wallet}) {
|
||||
hasAccounts = _wallet is MoneroWallet;
|
||||
_wallet = wallet;
|
||||
_init();
|
||||
}
|
||||
|
||||
@observable
|
||||
String amount;
|
||||
|
||||
@computed
|
||||
AddressListItem get address => AddressListItem(address: _wallet.address);
|
||||
|
||||
@computed
|
||||
PaymentURI get uri {
|
||||
if (_wallet is MoneroWallet) {
|
||||
return MoneroURI(amount: amount, address: address.address);
|
||||
}
|
||||
|
||||
if (_wallet is BitcoinWallet) {
|
||||
return BitcoinURI(amount: amount, address: address.address);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@computed
|
||||
ObservableList<ListItem> get items =>
|
||||
ObservableList<ListItem>()..addAll(_baseItems)..addAll(addressList);
|
||||
|
||||
@computed
|
||||
ObservableList<ListItem> get addressList {
|
||||
final wallet = _wallet;
|
||||
final addressList = ObservableList<ListItem>();
|
||||
|
||||
if (wallet is MoneroWallet) {
|
||||
addressList.addAll(wallet.subaddressList.subaddresses.map((subaddress) =>
|
||||
AddressListItem(
|
||||
id: subaddress.id,
|
||||
name: subaddress.label,
|
||||
address: subaddress.address)));
|
||||
}
|
||||
|
||||
if (wallet is BitcoinWallet) {
|
||||
final bitcoinAddresses = wallet.addresses.map(
|
||||
(addr) => AddressListItem(name: addr.label, address: addr.address));
|
||||
addressList.addAll(bitcoinAddresses);
|
||||
}
|
||||
|
||||
return addressList;
|
||||
}
|
||||
|
||||
set address(AddressListItem address) => _wallet.address = address.address;
|
||||
|
||||
bool hasAccounts;
|
||||
|
||||
WalletBase _wallet;
|
||||
|
||||
List<ListItem> _baseItems;
|
||||
|
||||
@computed
|
||||
String get accountLabel {
|
||||
final wallet = _wallet;
|
||||
|
||||
if (wallet is MoneroWallet) {
|
||||
return wallet.account.label;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void _init() {
|
||||
_baseItems = [];
|
||||
|
||||
if (_wallet is MoneroWallet) {
|
||||
_baseItems.add(AccountListHeader());
|
||||
}
|
||||
|
||||
_baseItems.add(AddressListHeader());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
abstract class AuthState {}
|
||||
|
||||
class AuthenticationStateInitial extends AuthState {}
|
||||
|
||||
class AuthenticationInProgress extends AuthState {}
|
||||
|
||||
class AuthenticatedSuccessfully extends AuthState {}
|
||||
|
||||
class AuthenticationFailure extends AuthState {
|
||||
AuthenticationFailure({this.error});
|
||||
|
||||
final String error;
|
||||
}
|
||||
|
||||
class AuthenticationBanned extends AuthState {
|
||||
AuthenticationBanned({this.error});
|
||||
|
||||
final String error;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/view_model/auth_state.dart';
|
||||
import 'package:cake_wallet/core/auth_service.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
|
||||
part 'auth_view_model.g.dart';
|
||||
|
||||
class AuthViewModel = AuthViewModelBase with _$AuthViewModel;
|
||||
|
||||
abstract class AuthViewModelBase with Store {
|
||||
AuthViewModelBase(
|
||||
{@required this.authService, @required this.sharedPreferences}) {
|
||||
state = AuthenticationStateInitial();
|
||||
_failureCounter = 0;
|
||||
}
|
||||
|
||||
static const maxFailedLogins = 3;
|
||||
static const banTimeout = 180; // 3 mins
|
||||
final banTimeoutKey = S.current.auth_store_ban_timeout;
|
||||
|
||||
final AuthService authService;
|
||||
final SharedPreferences sharedPreferences;
|
||||
|
||||
@observable
|
||||
AuthState state;
|
||||
|
||||
@observable
|
||||
int _failureCounter;
|
||||
|
||||
@action
|
||||
Future<void> auth({String password}) async {
|
||||
state = AuthenticationStateInitial();
|
||||
final _banDuration = banDuration();
|
||||
|
||||
if (_banDuration != null) {
|
||||
state = AuthenticationBanned(
|
||||
error: S.current.auth_store_banned_for +
|
||||
'${_banDuration.inMinutes}' +
|
||||
S.current.auth_store_banned_minutes);
|
||||
return;
|
||||
}
|
||||
|
||||
state = AuthenticationInProgress();
|
||||
final isAuth = await authService.authenticate(password);
|
||||
|
||||
if (isAuth) {
|
||||
state = AuthenticatedSuccessfully();
|
||||
_failureCounter = 0;
|
||||
} else {
|
||||
_failureCounter += 1;
|
||||
|
||||
if (_failureCounter >= maxFailedLogins) {
|
||||
final banDuration = await ban();
|
||||
state = AuthenticationBanned(
|
||||
error: S.current.auth_store_banned_for +
|
||||
'${banDuration.inMinutes}' +
|
||||
S.current.auth_store_banned_minutes);
|
||||
return;
|
||||
}
|
||||
|
||||
state =
|
||||
AuthenticationFailure(error: S.current.auth_store_incorrect_password);
|
||||
}
|
||||
}
|
||||
|
||||
Duration banDuration() {
|
||||
final unbanTimestamp = sharedPreferences.getInt(banTimeoutKey);
|
||||
|
||||
if (unbanTimestamp == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final unbanTime = DateTime.fromMillisecondsSinceEpoch(unbanTimestamp);
|
||||
final now = DateTime.now();
|
||||
|
||||
if (now.isAfter(unbanTime)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Duration(milliseconds: unbanTimestamp - now.millisecondsSinceEpoch);
|
||||
}
|
||||
|
||||
Future<Duration> ban() async {
|
||||
final multiplier = _failureCounter - maxFailedLogins + 1;
|
||||
final timeout = (multiplier * banTimeout) * 1000;
|
||||
final unbanTimestamp = DateTime.now().millisecondsSinceEpoch + timeout;
|
||||
await sharedPreferences.setInt(banTimeoutKey, unbanTimestamp);
|
||||
|
||||
return Duration(milliseconds: timeout);
|
||||
}
|
||||
|
||||
@action
|
||||
void biometricAuth() => state = AuthenticatedSuccessfully();
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_transaction_info.dart';
|
||||
import 'package:cake_wallet/src/domain/common/transaction_direction.dart';
|
||||
import 'package:cake_wallet/src/domain/common/transaction_info.dart';
|
||||
import 'package:cake_wallet/src/stores/action_list/transaction_list_item.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/core/wallet_base.dart';
|
||||
import 'package:cake_wallet/src/domain/common/sync_status.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
|
||||
part 'dashboard_view_model.g.dart';
|
||||
|
||||
class DashboardViewModel = DashboardViewModelBase with _$DashboardViewModel;
|
||||
|
||||
class WalletBalace {
|
||||
WalletBalace({this.unlockedBalance, this.totalBalance});
|
||||
|
||||
final String unlockedBalance;
|
||||
final String totalBalance;
|
||||
}
|
||||
|
||||
abstract class DashboardViewModelBase with Store {
|
||||
DashboardViewModelBase({this.appStore}) {
|
||||
name = appStore.wallet?.name;
|
||||
balance = WalletBalace(unlockedBalance: '0.001', totalBalance: '0.005');
|
||||
status = SyncedSyncStatus();
|
||||
type = WalletType.bitcoin;
|
||||
wallet ??= appStore.wallet;
|
||||
_reaction = reaction((_) => appStore.wallet, _onWalletChange);
|
||||
transactions = ObservableList.of(wallet.transactionHistory.transactions
|
||||
.map((transaction) => TransactionListItem(transaction: transaction)));
|
||||
}
|
||||
|
||||
@observable
|
||||
WalletType type;
|
||||
|
||||
@observable
|
||||
String name;
|
||||
|
||||
@computed
|
||||
String get address => wallet.address;
|
||||
|
||||
@observable
|
||||
WalletBalace balance;
|
||||
|
||||
@observable
|
||||
SyncStatus status;
|
||||
|
||||
@observable
|
||||
ObservableList<Object> transactions;
|
||||
|
||||
@observable
|
||||
String subname;
|
||||
|
||||
WalletBase wallet;
|
||||
|
||||
AppStore appStore;
|
||||
|
||||
ReactionDisposer _reaction;
|
||||
|
||||
void _onWalletChange(WalletBase wallet) {
|
||||
name = wallet.name;
|
||||
transactions.clear();
|
||||
transactions.addAll(wallet.transactionHistory.transactions
|
||||
.map((transaction) => TransactionListItem(transaction: transaction)));
|
||||
balance = WalletBalace(unlockedBalance: '0.001', totalBalance: '0.005');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
abstract class WalletCreationState {}
|
||||
|
||||
class InitialWalletCreationState extends WalletCreationState {}
|
||||
|
||||
class WalletCreating extends WalletCreationState {}
|
||||
|
||||
class WalletCreatedSuccessfully extends WalletCreationState {}
|
||||
|
||||
class WalletCreationFailure extends WalletCreationState {
|
||||
WalletCreationFailure({@required this.error});
|
||||
|
||||
final String error;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/core/wallet_credentials.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/view_model/wallet_creation_state.dart';
|
||||
|
||||
part 'wallet_creation_vm.g.dart';
|
||||
|
||||
class WalletCreationVM = WalletCreationVMBase with _$WalletCreationVM;
|
||||
|
||||
abstract class WalletCreationVMBase with Store {
|
||||
WalletCreationVMBase({@required this.type}) {
|
||||
state = InitialWalletCreationState();
|
||||
name = '';
|
||||
}
|
||||
|
||||
@observable
|
||||
String name;
|
||||
|
||||
@observable
|
||||
WalletCreationState state;
|
||||
|
||||
WalletType type;
|
||||
|
||||
Future<void> create({dynamic options}) async {
|
||||
try {
|
||||
state = WalletCreating();
|
||||
await process(getCredentials(options));
|
||||
state = WalletCreatedSuccessfully();
|
||||
} catch (e) {
|
||||
state = WalletCreationFailure(error: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
WalletCredentials getCredentials(dynamic options) =>
|
||||
throw UnimplementedError();
|
||||
|
||||
Future<void> process(WalletCredentials credentials) =>
|
||||
throw UnimplementedError();
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/monero/monero_wallet_service.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_wallet_creation_credentials.dart';
|
||||
import 'package:cake_wallet/core/wallet_creation_service.dart';
|
||||
import 'package:cake_wallet/core/wallet_credentials.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/view_model/wallet_creation_vm.dart';
|
||||
|
||||
part 'wallet_new_vm.g.dart';
|
||||
|
||||
class WalletNewVM = WalletNewVMBase with _$WalletNewVM;
|
||||
|
||||
abstract class WalletNewVMBase extends WalletCreationVM with Store {
|
||||
WalletNewVMBase(this._walletCreationService, {@required WalletType type})
|
||||
: selectedMnemonicLanguage = '',
|
||||
super(type: type);
|
||||
|
||||
@observable
|
||||
String selectedMnemonicLanguage;
|
||||
|
||||
bool get hasLanguageSelector => type == WalletType.monero;
|
||||
|
||||
final WalletCreationService _walletCreationService;
|
||||
|
||||
@override
|
||||
WalletCredentials getCredentials(dynamic options) {
|
||||
switch (type) {
|
||||
case WalletType.monero:
|
||||
return MoneroNewWalletCredentials(
|
||||
name: name, language: options as String);
|
||||
case WalletType.bitcoin:
|
||||
return BitcoinNewWalletCredentials(name: name);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> process(WalletCredentials credentials) async =>
|
||||
_walletCreationService.create(credentials);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/monero/monero_wallet_service.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin_wallet_creation_credentials.dart';
|
||||
import 'package:cake_wallet/core/generate_wallet_password.dart';
|
||||
import 'package:cake_wallet/core/wallet_creation_service.dart';
|
||||
import 'package:cake_wallet/core/wallet_credentials.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/view_model/wallet_creation_vm.dart';
|
||||
|
||||
part 'wallet_restoration_from_seed_vm.g.dart';
|
||||
|
||||
class WalletRestorationFromSeedVM = WalletRestorationFromSeedVMBase
|
||||
with _$WalletRestorationFromSeedVM;
|
||||
|
||||
abstract class WalletRestorationFromSeedVMBase extends WalletCreationVM
|
||||
with Store {
|
||||
WalletRestorationFromSeedVMBase(this._walletCreationService,
|
||||
{@required WalletType type, @required this.language, this.seed})
|
||||
: super(type: type);
|
||||
|
||||
@observable
|
||||
String seed;
|
||||
|
||||
@observable
|
||||
int height;
|
||||
|
||||
bool get hasRestorationHeight => type == WalletType.monero;
|
||||
|
||||
final String language;
|
||||
final WalletCreationService _walletCreationService;
|
||||
|
||||
@override
|
||||
WalletCredentials getCredentials(dynamic options) {
|
||||
final password = generateWalletPassword(type);
|
||||
|
||||
switch (type) {
|
||||
case WalletType.monero:
|
||||
return MoneroRestoreWalletFromSeedCredentials(
|
||||
name: name, height: height, mnemonic: seed, password: password);
|
||||
case WalletType.bitcoin:
|
||||
return BitcoinRestoreWalletFromSeedCredentials(
|
||||
name: name, mnemonic: seed, password: password);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> process(WalletCredentials credentials) async =>
|
||||
_walletCreationService.restoreFromSeed(credentials);
|
||||
}
|
||||
Reference in New Issue
Block a user