General changes. CAKE-1.

This commit is contained in:
M
2020-07-06 23:09:03 +03:00
160 changed files with 5492 additions and 4033 deletions
+82
View File
@@ -0,0 +1,82 @@
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';
class AddressValidator extends TextValidator {
AddressValidator({@required CryptoCurrency type})
: super(
errorMessage: S.current.error_text_address,
pattern: getPattern(type),
length: getLength(type));
static String getPattern(CryptoCurrency type) {
switch (type) {
case CryptoCurrency.xmr:
return '[0-9a-zA-Z]';
case CryptoCurrency.ada:
return '[0-9a-zA-Z]';
case CryptoCurrency.bch:
return '[0-9a-zA-Z]';
case CryptoCurrency.bnb:
return '[0-9a-zA-Z]';
case CryptoCurrency.btc:
return '[0-9a-zA-Z]';
case CryptoCurrency.dash:
return '[0-9a-zA-Z]';
case CryptoCurrency.eos:
return '[0-9a-zA-Z]';
case CryptoCurrency.eth:
return '[0-9a-zA-Z]';
case CryptoCurrency.ltc:
return '[0-9a-zA-Z]';
case CryptoCurrency.nano:
return '[0-9a-zA-Z_]';
case CryptoCurrency.trx:
return '[0-9a-zA-Z]';
case CryptoCurrency.usdt:
return '[0-9a-zA-Z]';
case CryptoCurrency.xlm:
return '[0-9a-zA-Z]';
case CryptoCurrency.xrp:
return '[0-9a-zA-Z]';
default:
return '[0-9a-zA-Z]';
}
}
static List<int> getLength(CryptoCurrency type) {
switch (type) {
case CryptoCurrency.xmr:
return [95, 106];
case CryptoCurrency.ada:
return [59, 92, 105];
case CryptoCurrency.bch:
return [42];
case CryptoCurrency.bnb:
return [42];
case CryptoCurrency.btc:
return [34, 42];
case CryptoCurrency.dash:
return [34];
case CryptoCurrency.eos:
return [42];
case CryptoCurrency.eth:
return [42];
case CryptoCurrency.ltc:
return [34];
case CryptoCurrency.nano:
return [64, 65];
case CryptoCurrency.trx:
return [34];
case CryptoCurrency.usdt:
return [42];
case CryptoCurrency.xlm:
return [56];
case CryptoCurrency.xrp:
return [34];
default:
return [];
}
}
}
+11
View File
@@ -0,0 +1,11 @@
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/core/validator.dart';
class ContactNameValidator extends TextValidator {
ContactNameValidator()
: super(
errorMessage: S.current.error_text_contact_name,
pattern: '''[^`,'"]''',
minLength: 1,
maxLength: 32);
}
+38
View File
@@ -0,0 +1,38 @@
import 'package:hive/hive.dart';
import 'package:cake_wallet/store/contact_list_store.dart';
import 'package:cake_wallet/src/domain/common/contact.dart';
class ContactService {
ContactService(this.contactSource, this.contactListStore) {
_forceUpdateContactListStore();
}
final Box<Contact> contactSource;
final ContactListStore contactListStore;
Future add(Contact contact) async {
await contactSource.add(contact);
contactListStore.contacts.add(contact);
}
Future update(Contact contact) async {
await contact.save();
final index = contactListStore.contacts.indexOf(contact) ?? -1;
if (index >= 0) {
_forceUpdateContactListStore();
} else {
contactListStore.contacts.add(contact);
}
}
Future delete(Contact contact) async {
await contact.delete();
contactListStore.contacts.remove(contact);
}
void _forceUpdateContactListStore() {
contactListStore.contacts.clear();
contactListStore.contacts.addAll(contactSource.values);
}
}
+25
View File
@@ -0,0 +1,25 @@
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';
class KeyService {
KeyService(this._secureStorage);
final FlutterSecureStorage _secureStorage;
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);
}
}
@@ -0,0 +1,13 @@
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';
class MoneroLabelValidator extends TextValidator {
MoneroLabelValidator({@required CryptoCurrency type})
: super(
errorMessage: S.current.error_text_account_name,
pattern: '^[a-zA-Z0-9_]{1,15}\$',
minLength: 1,
maxLength: 15);
}
+10
View File
@@ -0,0 +1,10 @@
import 'package:cake_wallet/core/validator.dart';
import 'package:cake_wallet/generated/i18n.dart';
class NodeAddressValidator extends TextValidator {
NodeAddressValidator()
: super(
errorMessage: S.current.error_text_node_address,
pattern:
'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\$|^[0-9a-zA-Z.]+\$');
}
+12
View File
@@ -0,0 +1,12 @@
import 'package:flutter/foundation.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/core/validator.dart';
class NodePortValidator extends TextValidator {
NodePortValidator()
: super(
errorMessage: S.current.error_text_node_port,
minLength: 1,
maxLength: 5,
pattern: '^[0-9]');
}
+9 -3
View File
@@ -12,11 +12,16 @@ abstract class Validator<T> {
class TextValidator extends Validator<String> {
TextValidator(
{this.minLength, this.maxLength, this.pattern, String errorMessage})
{this.minLength,
this.maxLength,
this.pattern,
this.length,
String errorMessage})
: super(errorMessage: errorMessage);
final int minLength;
final int maxLength;
final List<int> length;
String pattern;
@override
@@ -25,8 +30,9 @@ class TextValidator extends Validator<String> {
return true;
}
return value.length > minLength &&
(maxLength > 0 ? (value.length <= maxLength) : true) &&
return value.length > (minLength ?? 0) &&
(length?.contains(value.length) ?? true) &&
((maxLength ?? 0) > 0 ? (value.length <= maxLength) : true) &&
(pattern != null ? match(value) : true);
}
+12
View File
@@ -1,19 +1,31 @@
import 'package:flutter/foundation.dart';
import 'package:cake_wallet/core/transaction_history.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';
abstract class WalletBase<BalaceType> {
WalletType type;
CryptoCurrency currency;
String get name;
String address;
BalaceType balance;
SyncStatus syncStatus;
String get seed;
Object get keys;
TransactionHistoryBase transactionHistory;
String get id => walletTypeToString(type).toLowerCase() + '_' + name;
Future<void> connectToNode({@required Node node});
Future<void> startSync();
+9 -25
View File
@@ -1,6 +1,7 @@
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/generate_wallet_password.dart';
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/core/wallet_credentials.dart';
@@ -8,14 +9,13 @@ 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';
class WalletCreationService {
WalletCreationService(
{WalletType initialType,
this.appStore,
this.secureStorage,
this.keyService,
this.sharedPreferences})
: type = initialType {
if (type != null) {
@@ -27,10 +27,7 @@ class WalletCreationService {
final AppStore appStore;
final FlutterSecureStorage secureStorage;
final SharedPreferences sharedPreferences;
// final WalletService walletService;
// final Box<WalletInfo> walletInfoSource;
final KeyService keyService;
WalletService _service;
void changeWalletType({@required WalletType type}) {
@@ -51,7 +48,8 @@ class WalletCreationService {
Future<void> create(WalletCredentials credentials) async {
final password = generateWalletPassword(type);
credentials.password = password;
await saveWalletPassword(password: password, walletName: credentials.name);
await keyService.saveWalletPassword(
password: password, walletName: credentials.name);
final wallet = await _service.create(credentials);
appStore.wallet = wallet;
appStore.authenticationStore.allowed();
@@ -60,7 +58,8 @@ class WalletCreationService {
Future<void> restoreFromKeys(WalletCredentials credentials) async {
final password = generateWalletPassword(type);
credentials.password = password;
await saveWalletPassword(password: password, walletName: credentials.name);
await keyService.saveWalletPassword(
password: password, walletName: credentials.name);
final wallet = await _service.restoreFromKeys(credentials);
appStore.wallet = wallet;
appStore.authenticationStore.allowed();
@@ -69,25 +68,10 @@ class WalletCreationService {
Future<void> restoreFromSeed(WalletCredentials credentials) async {
final password = generateWalletPassword(type);
credentials.password = password;
await saveWalletPassword(password: password, walletName: credentials.name);
await keyService.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);
}
}