mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-14 09:14:05 +01:00
Part 1
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import 'package:cake_wallet/core/validator.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
class AddressLabelValidator extends TextValidator {
|
||||
AddressLabelValidator({WalletType type})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/core/validator.dart';
|
||||
import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
|
||||
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
|
||||
class AddressValidator extends TextValidator {
|
||||
AddressValidator({@required CryptoCurrency type})
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
abstract class Amount {
|
||||
Amount(this.value);
|
||||
|
||||
int value;
|
||||
|
||||
int minorDigits;
|
||||
|
||||
String code;
|
||||
|
||||
String formatted();
|
||||
}
|
||||
|
||||
class MoneroAmount extends Amount {
|
||||
MoneroAmount(int value) : super(value) {
|
||||
minorDigits = 12;
|
||||
code = 'XMR';
|
||||
}
|
||||
|
||||
// const moneroAmountLength = 12;
|
||||
// const moneroAmountDivider = 1000000000000;
|
||||
// final moneroAmountFormat = NumberFormat()
|
||||
// ..maximumFractionDigits = moneroAmountLength
|
||||
// ..minimumFractionDigits = 1;
|
||||
|
||||
// String moneroAmountToString({int amount}) =>
|
||||
// moneroAmountFormat.format(cryptoAmountToDouble(amount: amount, divider: moneroAmountDivider));
|
||||
|
||||
// double moneroAmountToDouble({int amount}) => cryptoAmountToDouble(amount: amount, divider: moneroAmountDivider);
|
||||
|
||||
// int moneroParseAmount({String amount}) => moneroAmountFormat.parse(amount).toInt();
|
||||
|
||||
@override
|
||||
String formatted() {
|
||||
// TODO: implement formatted
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
|
||||
class AmountConverter {
|
||||
static const _moneroAmountLength = 12;
|
||||
static const _moneroAmountDivider = 1000000000000;
|
||||
static const _litecoinAmountDivider = 100000000;
|
||||
static const _ethereumAmountDivider = 1000000000000000000;
|
||||
static const _dashAmountDivider = 100000000;
|
||||
static const _bitcoinCashAmountDivider = 100000000;
|
||||
static const _bitcoinAmountDivider = 100000000;
|
||||
static const _bitcoinAmountLength = 8;
|
||||
static final _bitcoinAmountFormat = NumberFormat()
|
||||
..maximumFractionDigits = _bitcoinAmountLength
|
||||
..minimumFractionDigits = 1;
|
||||
static final _moneroAmountFormat = NumberFormat()
|
||||
..maximumFractionDigits = _moneroAmountLength
|
||||
..minimumFractionDigits = 1;
|
||||
|
||||
static double amountIntToDouble(CryptoCurrency cryptoCurrency, int amount) {
|
||||
switch (cryptoCurrency) {
|
||||
case CryptoCurrency.xmr:
|
||||
return _moneroAmountToDouble(amount);
|
||||
case CryptoCurrency.btc:
|
||||
return _bitcoinAmountToDouble(amount);
|
||||
case CryptoCurrency.bch:
|
||||
return _bitcoinCashAmountToDouble(amount);
|
||||
case CryptoCurrency.dash:
|
||||
return _dashAmountToDouble(amount);
|
||||
case CryptoCurrency.eth:
|
||||
return _ethereumAmountToDouble(amount);
|
||||
case CryptoCurrency.ltc:
|
||||
return _litecoinAmountToDouble(amount);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static int amountStringToInt(CryptoCurrency cryptoCurrency, String amount) {
|
||||
switch (cryptoCurrency) {
|
||||
case CryptoCurrency.xmr:
|
||||
return _moneroParseAmount(amount);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static String amountIntToString(CryptoCurrency cryptoCurrency, int amount) {
|
||||
switch (cryptoCurrency) {
|
||||
case CryptoCurrency.xmr:
|
||||
return _moneroAmountToString(amount);
|
||||
case CryptoCurrency.btc:
|
||||
return _bitcoinAmountToString(amount);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static double cryptoAmountToDouble({num amount, num divider}) =>
|
||||
amount / divider;
|
||||
|
||||
static String _moneroAmountToString(int amount) => _moneroAmountFormat.format(
|
||||
cryptoAmountToDouble(amount: amount, divider: _moneroAmountDivider));
|
||||
|
||||
static double _moneroAmountToDouble(int amount) =>
|
||||
cryptoAmountToDouble(amount: amount, divider: _moneroAmountDivider);
|
||||
|
||||
static int _moneroParseAmount(String amount) =>
|
||||
_moneroAmountFormat.parse(amount).toInt();
|
||||
|
||||
static String _bitcoinAmountToString(int amount) =>
|
||||
_bitcoinAmountFormat.format(
|
||||
cryptoAmountToDouble(amount: amount, divider: _bitcoinAmountDivider));
|
||||
|
||||
static double _bitcoinAmountToDouble(int amount) =>
|
||||
cryptoAmountToDouble(amount: amount, divider: _bitcoinAmountDivider);
|
||||
|
||||
static int _doubleToBitcoinAmount(double amount) =>
|
||||
(amount * _bitcoinAmountDivider).toInt();
|
||||
|
||||
static double _bitcoinCashAmountToDouble(int amount) =>
|
||||
cryptoAmountToDouble(amount: amount, divider: _bitcoinCashAmountDivider);
|
||||
|
||||
static double _dashAmountToDouble(int amount) =>
|
||||
cryptoAmountToDouble(amount: amount, divider: _dashAmountDivider);
|
||||
|
||||
static double _ethereumAmountToDouble(num amount) =>
|
||||
cryptoAmountToDouble(amount: amount, divider: _ethereumAmountDivider);
|
||||
|
||||
static double _litecoinAmountToDouble(int amount) =>
|
||||
cryptoAmountToDouble(amount: amount, divider: _litecoinAmountDivider);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:cake_wallet/core/validator.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
class AmountValidator extends TextValidator {
|
||||
AmountValidator({WalletType type, bool isAutovalidate = false})
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
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/src/domain/common/secret_store_key.dart';
|
||||
import 'package:cake_wallet/src/domain/common/encrypt.dart';
|
||||
import 'package:cake_wallet/entities/preferences_key.dart';
|
||||
import 'package:cake_wallet/entities/secret_store_key.dart';
|
||||
import 'package:cake_wallet/entities/encrypt.dart';
|
||||
|
||||
class AuthService with Store {
|
||||
AuthService({this.secureStorage, this.sharedPreferences});
|
||||
@@ -18,7 +19,8 @@ class AuthService with Store {
|
||||
|
||||
Future<bool> canAuthenticate() async {
|
||||
final key = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword);
|
||||
final walletName = sharedPreferences.getString('current_wallet_name') ?? '';
|
||||
final walletName =
|
||||
sharedPreferences.getString(PreferencesKey.currentWalletName) ?? '';
|
||||
var password = '';
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:cake_wallet/store/contact_list_store.dart';
|
||||
import 'package:cake_wallet/src/domain/common/contact.dart';
|
||||
import 'package:cake_wallet/entities/contact.dart';
|
||||
|
||||
class ContactService {
|
||||
ContactService(this.contactSource, this.contactListStore) {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
abstract class ExecutionState {}
|
||||
|
||||
class InitialExecutionState extends ExecutionState {}
|
||||
|
||||
class IsExecutingState extends ExecutionState {}
|
||||
|
||||
class ExecutedSuccessfullyState extends ExecutionState {}
|
||||
|
||||
class FailureState extends ExecutionState {
|
||||
FailureState(this.error);
|
||||
|
||||
final String error;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:cake_wallet/entities/currency_formatter.dart';
|
||||
|
||||
const fiatApiAuthority = 'fiat-api.cakewallet.com';
|
||||
const fiatApiPath = '/v1/rates';
|
||||
|
||||
Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
||||
final crypto = args['crypto'] as CryptoCurrency;
|
||||
final fiat = args['fiat'] as FiatCurrency;
|
||||
double price = 0.0;
|
||||
|
||||
try {
|
||||
final fiatStringified = fiat.toString();
|
||||
final uri =
|
||||
Uri.https(fiatApiAuthority, fiatApiPath, {'convert': fiatStringified});
|
||||
final response = await get(uri.toString());
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final data = responseJSON['data'] as List<dynamic>;
|
||||
|
||||
for (final item in data) {
|
||||
if (item['symbol'] == cryptoToString(crypto)) {
|
||||
price = item['quote'][fiatStringified]['price'] as double;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return price;
|
||||
} catch (e) {
|
||||
return price;
|
||||
}
|
||||
}
|
||||
|
||||
Future<double> _fetchPriceAsync(
|
||||
CryptoCurrency crypto, FiatCurrency fiat) async =>
|
||||
compute(_fetchPrice, {'fiat': fiat, 'crypto': crypto});
|
||||
|
||||
class FiatConversionService {
|
||||
static Future<double> fetchPrice(CryptoCurrency crypto, FiatCurrency fiat) async =>
|
||||
await _fetchPriceAsync(crypto, fiat);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:cake_wallet/bitcoin/key.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
String generateWalletPassword(WalletType type) {
|
||||
switch (type) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:cake_wallet/src/domain/common/secret_store_key.dart';
|
||||
import 'package:cake_wallet/src/domain/common/encrypt.dart';
|
||||
import 'package:cake_wallet/entities/secret_store_key.dart';
|
||||
import 'package:cake_wallet/entities/encrypt.dart';
|
||||
|
||||
class KeyService {
|
||||
KeyService(this._secureStorage);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
const bitcoinMnemonicLength = 12;
|
||||
const moneroMnemonicLength = 25;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/core/validator.dart';
|
||||
import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
|
||||
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
|
||||
class MoneroLabelValidator extends TextValidator {
|
||||
MoneroLabelValidator({@required CryptoCurrency type})
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import 'package:bip39/src/wordlists/english.dart' as bitcoin_english;
|
||||
import 'package:cake_wallet/core/validator.dart';
|
||||
import 'package:cake_wallet/src/domain/common/mnemonic_item.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/src/domain/monero/mnemonics/chinese_simplified.dart';
|
||||
import 'package:cake_wallet/src/domain/monero/mnemonics/dutch.dart';
|
||||
import 'package:cake_wallet/src/domain/monero/mnemonics/english.dart';
|
||||
import 'package:cake_wallet/src/domain/monero/mnemonics/german.dart';
|
||||
import 'package:cake_wallet/src/domain/monero/mnemonics/japanese.dart';
|
||||
import 'package:cake_wallet/src/domain/monero/mnemonics/portuguese.dart';
|
||||
import 'package:cake_wallet/src/domain/monero/mnemonics/russian.dart';
|
||||
import 'package:cake_wallet/src/domain/monero/mnemonics/spanish.dart';
|
||||
import 'package:cake_wallet/entities/mnemonic_item.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
import 'package:cake_wallet/monero/mnemonics/chinese_simplified.dart';
|
||||
import 'package:cake_wallet/monero/mnemonics/dutch.dart';
|
||||
import 'package:cake_wallet/monero/mnemonics/english.dart';
|
||||
import 'package:cake_wallet/monero/mnemonics/german.dart';
|
||||
import 'package:cake_wallet/monero/mnemonics/japanese.dart';
|
||||
import 'package:cake_wallet/monero/mnemonics/portuguese.dart';
|
||||
import 'package:cake_wallet/monero/mnemonics/russian.dart';
|
||||
import 'package:cake_wallet/monero/mnemonics/spanish.dart';
|
||||
import 'package:cake_wallet/utils/language_list.dart';
|
||||
|
||||
class SeedValidator extends Validator<MnemonicItem> {
|
||||
SeedValidator({this.type, this.language})
|
||||
@@ -31,31 +32,29 @@ class SeedValidator extends Validator<MnemonicItem> {
|
||||
}
|
||||
|
||||
static List<String> getMoneroWordList(String language) {
|
||||
// FIXME: Unnamed constants; Need to be sure that string are in same case;
|
||||
|
||||
switch (language) {
|
||||
case 'English':
|
||||
case LanguageList.english:
|
||||
return EnglishMnemonics.words;
|
||||
break;
|
||||
case 'Chinese (simplified)':
|
||||
case LanguageList.chineseSimplified:
|
||||
return ChineseSimplifiedMnemonics.words;
|
||||
break;
|
||||
case 'Dutch':
|
||||
case LanguageList.dutch:
|
||||
return DutchMnemonics.words;
|
||||
break;
|
||||
case 'German':
|
||||
case LanguageList.german:
|
||||
return GermanMnemonics.words;
|
||||
break;
|
||||
case 'Japanese':
|
||||
case LanguageList.japanese:
|
||||
return JapaneseMnemonics.words;
|
||||
break;
|
||||
case 'Portuguese':
|
||||
case LanguageList.portuguese:
|
||||
return PortugueseMnemonics.words;
|
||||
break;
|
||||
case 'Russian':
|
||||
case LanguageList.russian:
|
||||
return RussianMnemonics.words;
|
||||
break;
|
||||
case 'Spanish':
|
||||
case LanguageList.spanish:
|
||||
return SpanishMnemonics.words;
|
||||
break;
|
||||
default:
|
||||
@@ -64,7 +63,7 @@ class SeedValidator extends Validator<MnemonicItem> {
|
||||
}
|
||||
|
||||
static List<String> getBitcoinWordList(String language) {
|
||||
assert(language.toLowerCase() == 'english');
|
||||
assert(language.toLowerCase() == LanguageList.english.toLowerCase());
|
||||
return bitcoin_english.WORDLIST;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/src/domain/common/transaction_info.dart';
|
||||
import 'package:cake_wallet/entities/transaction_info.dart';
|
||||
|
||||
abstract class TransactionHistoryBase<TransactionType extends TransactionInfo> {
|
||||
TransactionHistoryBase() : _isUpdating = false;
|
||||
|
||||
@@ -1,24 +1,13 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_info.dart';
|
||||
import 'package:cake_wallet/entities/wallet_info.dart';
|
||||
import 'package:cake_wallet/core/pending_transaction.dart';
|
||||
import 'package:cake_wallet/core/transaction_history.dart';
|
||||
import 'package:cake_wallet/src/domain/common/transaction_priority.dart';
|
||||
import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
|
||||
import 'package:cake_wallet/src/domain/common/sync_status.dart';
|
||||
import 'package:cake_wallet/src/domain/common/node.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
|
||||
// FIXME: Move me.
|
||||
CryptoCurrency currencyForWalletType(WalletType type) {
|
||||
switch (type) {
|
||||
case WalletType.bitcoin:
|
||||
return CryptoCurrency.btc;
|
||||
case WalletType.monero:
|
||||
return CryptoCurrency.xmr;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
import 'package:cake_wallet/entities/currency_for_wallet_type.dart';
|
||||
import 'package:cake_wallet/entities/transaction_priority.dart';
|
||||
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
import 'package:cake_wallet/entities/sync_status.dart';
|
||||
import 'package:cake_wallet/entities/node.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
abstract class WalletBase<BalaceType> {
|
||||
WalletBase(this.walletInfo);
|
||||
@@ -61,4 +50,6 @@ abstract class WalletBase<BalaceType> {
|
||||
double calculateEstimatedFee(TransactionPriority priority);
|
||||
|
||||
Future<void> save();
|
||||
|
||||
Future<void> rescan({int height});
|
||||
}
|
||||
|
||||
@@ -3,18 +3,15 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:cake_wallet/core/key_service.dart';
|
||||
import 'package:cake_wallet/core/wallet_base.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/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/entities/wallet_type.dart';
|
||||
|
||||
class WalletCreationService {
|
||||
WalletCreationService(
|
||||
{WalletType initialType,
|
||||
this.appStore,
|
||||
this.secureStorage,
|
||||
this.keyService,
|
||||
this.sharedPreferences})
|
||||
@@ -25,7 +22,6 @@ class WalletCreationService {
|
||||
}
|
||||
|
||||
WalletType type;
|
||||
final AppStore appStore;
|
||||
final FlutterSecureStorage secureStorage;
|
||||
final SharedPreferences sharedPreferences;
|
||||
final KeyService keyService;
|
||||
@@ -36,33 +32,27 @@ class WalletCreationService {
|
||||
_service = getIt.get<WalletService>(param1: type);
|
||||
}
|
||||
|
||||
Future<void> create(WalletCredentials credentials) async {
|
||||
Future<WalletBase> create(WalletCredentials credentials) async {
|
||||
final password = generateWalletPassword(type);
|
||||
credentials.password = password;
|
||||
await keyService.saveWalletPassword(
|
||||
password: password, walletName: credentials.name);
|
||||
final wallet = await _service.create(credentials);
|
||||
appStore.wallet = wallet;
|
||||
appStore.authenticationStore.allowed();
|
||||
return await _service.create(credentials);
|
||||
}
|
||||
|
||||
Future<void> restoreFromKeys(WalletCredentials credentials) async {
|
||||
Future<WalletBase> restoreFromKeys(WalletCredentials credentials) async {
|
||||
final password = generateWalletPassword(type);
|
||||
credentials.password = password;
|
||||
await keyService.saveWalletPassword(
|
||||
password: password, walletName: credentials.name);
|
||||
final wallet = await _service.restoreFromKeys(credentials);
|
||||
appStore.wallet = wallet;
|
||||
appStore.authenticationStore.allowed();
|
||||
return await _service.restoreFromKeys(credentials);
|
||||
}
|
||||
|
||||
Future<void> restoreFromSeed(WalletCredentials credentials) async {
|
||||
Future<WalletBase> restoreFromSeed(WalletCredentials credentials) async {
|
||||
final password = generateWalletPassword(type);
|
||||
credentials.password = password;
|
||||
await keyService.saveWalletPassword(
|
||||
password: password, walletName: credentials.name);
|
||||
final wallet = await _service.restoreFromSeed(credentials);
|
||||
appStore.wallet = wallet;
|
||||
appStore.authenticationStore.allowed();
|
||||
return await _service.restoreFromSeed(credentials);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:cake_wallet/src/domain/common/wallet_info.dart';
|
||||
import 'package:cake_wallet/entities/wallet_info.dart';
|
||||
|
||||
abstract class WalletCredentials {
|
||||
WalletCredentials({this.name, this.password, this.height});
|
||||
|
||||
Reference in New Issue
Block a user