mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-03 12:24:19 +01:00
Part 1
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
enum ActionListDisplayMode { transactions, trades }
|
||||
|
||||
int serializeActionlistDisplayModes(List<ActionListDisplayMode> modes) {
|
||||
var i = 0;
|
||||
|
||||
for (final mode in modes) {
|
||||
switch (mode) {
|
||||
case ActionListDisplayMode.trades:
|
||||
i += 1;
|
||||
break;
|
||||
case ActionListDisplayMode.transactions:
|
||||
i += 10;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
List<ActionListDisplayMode> deserializeActionlistDisplayModes(int raw) {
|
||||
final modes = List<ActionListDisplayMode>();
|
||||
|
||||
if (raw == 1 || raw - 10 == 1) {
|
||||
modes.add(ActionListDisplayMode.trades);
|
||||
}
|
||||
|
||||
if (raw >= 10) {
|
||||
modes.add(ActionListDisplayMode.transactions);
|
||||
}
|
||||
|
||||
return modes;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
abstract class Balance {
|
||||
const Balance();
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/entities/enumerable_item.dart';
|
||||
|
||||
class BalanceDisplayMode extends EnumerableItem<int> with Serializable<int> {
|
||||
const BalanceDisplayMode({@required String title, @required int raw})
|
||||
: super(title: title, raw: raw);
|
||||
|
||||
static const all = [
|
||||
BalanceDisplayMode.fullBalance,
|
||||
BalanceDisplayMode.availableBalance,
|
||||
BalanceDisplayMode.hiddenBalance
|
||||
];
|
||||
static const fullBalance = BalanceDisplayMode(raw: 0, title: 'Full Balance');
|
||||
static const availableBalance =
|
||||
BalanceDisplayMode(raw: 1, title: 'Available Balance');
|
||||
static const hiddenBalance =
|
||||
BalanceDisplayMode(raw: 2, title: 'Hidden Balance');
|
||||
|
||||
static BalanceDisplayMode deserialize({int raw}) {
|
||||
switch (raw) {
|
||||
case 0:
|
||||
return fullBalance;
|
||||
case 1:
|
||||
return availableBalance;
|
||||
case 2:
|
||||
return hiddenBalance;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
switch (this) {
|
||||
case BalanceDisplayMode.fullBalance:
|
||||
return S.current.xmr_full_balance;
|
||||
case BalanceDisplayMode.availableBalance:
|
||||
return S.current.xmr_available_balance;
|
||||
case BalanceDisplayMode.hiddenBalance:
|
||||
return S.current.xmr_hidden;
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
|
||||
class BiometricAuth {
|
||||
final _localAuth = LocalAuthentication();
|
||||
|
||||
Future<bool> isAuthenticated() async {
|
||||
try {
|
||||
return await _localAuth.authenticateWithBiometrics(
|
||||
localizedReason: S.current.biometric_auth_reason,
|
||||
useErrorDialogs: true,
|
||||
stickyAuth: false);
|
||||
} on PlatformException catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<bool> canCheckBiometrics() async {
|
||||
try {
|
||||
return await _localAuth.canCheckBiometrics;
|
||||
} on PlatformException catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import 'package:cake_wallet/entities/transaction_priority.dart';
|
||||
|
||||
double calculateEstimatedFee({TransactionPriority priority}) {
|
||||
if (priority == TransactionPriority.slow) {
|
||||
return 0.00002459;
|
||||
}
|
||||
|
||||
if (priority == TransactionPriority.regular) {
|
||||
return 0.00012305;
|
||||
}
|
||||
|
||||
if (priority == TransactionPriority.medium) {
|
||||
return 0.00024503;
|
||||
}
|
||||
|
||||
if (priority == TransactionPriority.fast) {
|
||||
return 0.00061453;
|
||||
}
|
||||
|
||||
if (priority == TransactionPriority.fastest) {
|
||||
return 0.0260216;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
String calculateFiatAmount({double price, String cryptoAmount}) {
|
||||
if (price == null || cryptoAmount == null) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
final _amount = double.parse(cryptoAmount);
|
||||
final result = price * _amount;
|
||||
|
||||
if (result == 0.0) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
return result > 0.01 ? result.toStringAsFixed(2) : '< 0.01';
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
String calculateFiatAmountRaw({double price, double cryptoAmount}) {
|
||||
if (price == null) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
final result = price * cryptoAmount;
|
||||
|
||||
if (result == 0.0) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
return result > 0.01 ? result.toStringAsFixed(2) : '< 0.01';
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
import 'package:cake_wallet/utils/mobx.dart';
|
||||
|
||||
part 'contact.g.dart';
|
||||
|
||||
@HiveType(typeId: 0)
|
||||
class Contact extends HiveObject with Keyable {
|
||||
Contact({@required this.name, @required this.address, CryptoCurrency type})
|
||||
: raw = type?.raw;
|
||||
|
||||
static const boxName = 'Contacts';
|
||||
|
||||
@HiveField(0)
|
||||
String name;
|
||||
|
||||
@HiveField(1)
|
||||
String address;
|
||||
|
||||
@HiveField(2)
|
||||
int raw;
|
||||
|
||||
CryptoCurrency get type => CryptoCurrency.deserialize(raw: raw);
|
||||
|
||||
@override
|
||||
dynamic get keyIndex => key;
|
||||
|
||||
@override
|
||||
bool operator ==(Object o) => o is Contact && o.key == key;
|
||||
|
||||
@override
|
||||
int get hashCode => key.hashCode;
|
||||
|
||||
void updateCryptoCurrency({@required CryptoCurrency currency}) =>
|
||||
raw = currency.raw;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// import 'package:hive/hive.dart';
|
||||
// import 'package:mobx/mobx.dart';
|
||||
// import 'package:cake_wallet/entities/contact.dart';
|
||||
// import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
|
||||
// part 'contact_model.g.dart';
|
||||
|
||||
// class ContactModel = ContactModelBase with _$ContactModel;
|
||||
|
||||
// abstract class ContactModelBase with Store {
|
||||
// ContactModelBase(this._contacts, {Contact contact}) : _contact = contact {
|
||||
// name = _contact?.name;
|
||||
// address = _contact?.address;
|
||||
// currency = _contact?.type;
|
||||
|
||||
// _contacts.watch(key: contact.key).listen((event) {
|
||||
|
||||
// });
|
||||
// }
|
||||
|
||||
// @observable
|
||||
// String name;
|
||||
|
||||
// @observable
|
||||
// String address;
|
||||
|
||||
// @observable
|
||||
// CryptoCurrency currency;
|
||||
|
||||
// // @computed
|
||||
// // bool get isReady =>
|
||||
// // (name?.isNotEmpty ?? false) &&
|
||||
// // (currency?.toString()?.isNotEmpty ?? false) &&
|
||||
// // (address?.isNotEmpty ?? false);
|
||||
|
||||
// final Box<ContactBase> _contacts;
|
||||
// final Contact _contact;
|
||||
// }
|
||||
@@ -0,0 +1 @@
|
||||
double cryptoAmountToDouble({num amount, num divider}) => amount / divider;
|
||||
@@ -0,0 +1,114 @@
|
||||
import 'package:cake_wallet/entities/enumerable_item.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'crypto_currency.g.dart';
|
||||
|
||||
@HiveType(typeId: 0)
|
||||
class CryptoCurrency extends EnumerableItem<int> with Serializable<int> {
|
||||
const CryptoCurrency({final String title, final int raw})
|
||||
: super(title: title, raw: raw);
|
||||
|
||||
static const all = [
|
||||
CryptoCurrency.xmr,
|
||||
CryptoCurrency.ada,
|
||||
CryptoCurrency.bch,
|
||||
CryptoCurrency.bnb,
|
||||
CryptoCurrency.btc,
|
||||
CryptoCurrency.dash,
|
||||
CryptoCurrency.eos,
|
||||
CryptoCurrency.eth,
|
||||
CryptoCurrency.ltc,
|
||||
CryptoCurrency.nano,
|
||||
CryptoCurrency.trx,
|
||||
CryptoCurrency.usdt,
|
||||
CryptoCurrency.xlm,
|
||||
CryptoCurrency.xrp
|
||||
];
|
||||
static const xmr = CryptoCurrency(title: 'XMR', raw: 0);
|
||||
static const ada = CryptoCurrency(title: 'ADA', raw: 1);
|
||||
static const bch = CryptoCurrency(title: 'BCH', raw: 2);
|
||||
static const bnb = CryptoCurrency(title: 'BNB', raw: 3);
|
||||
static const btc = CryptoCurrency(title: 'BTC', raw: 4);
|
||||
static const dash = CryptoCurrency(title: 'DASH', raw: 5);
|
||||
static const eos = CryptoCurrency(title: 'EOS', raw: 6);
|
||||
static const eth = CryptoCurrency(title: 'ETH', raw: 7);
|
||||
static const ltc = CryptoCurrency(title: 'LTC', raw: 8);
|
||||
static const nano = CryptoCurrency(title: 'NANO', raw: 9);
|
||||
static const trx = CryptoCurrency(title: 'TRX', raw: 10);
|
||||
static const usdt = CryptoCurrency(title: 'USDT', raw: 11);
|
||||
static const xlm = CryptoCurrency(title: 'XLM', raw: 12);
|
||||
static const xrp = CryptoCurrency(title: 'XRP', raw: 13);
|
||||
|
||||
static CryptoCurrency deserialize({int raw}) {
|
||||
switch (raw) {
|
||||
case 0:
|
||||
return CryptoCurrency.xmr;
|
||||
case 1:
|
||||
return CryptoCurrency.ada;
|
||||
case 2:
|
||||
return CryptoCurrency.bch;
|
||||
case 3:
|
||||
return CryptoCurrency.bnb;
|
||||
case 4:
|
||||
return CryptoCurrency.btc;
|
||||
case 5:
|
||||
return CryptoCurrency.dash;
|
||||
case 6:
|
||||
return CryptoCurrency.eos;
|
||||
case 7:
|
||||
return CryptoCurrency.eth;
|
||||
case 8:
|
||||
return CryptoCurrency.ltc;
|
||||
case 9:
|
||||
return CryptoCurrency.nano;
|
||||
case 10:
|
||||
return CryptoCurrency.trx;
|
||||
case 11:
|
||||
return CryptoCurrency.usdt;
|
||||
case 12:
|
||||
return CryptoCurrency.xlm;
|
||||
case 13:
|
||||
return CryptoCurrency.xrp;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static CryptoCurrency fromString(String raw) {
|
||||
switch (raw.toLowerCase()) {
|
||||
case 'xmr':
|
||||
return CryptoCurrency.xmr;
|
||||
case 'ada':
|
||||
return CryptoCurrency.ada;
|
||||
case 'bch':
|
||||
return CryptoCurrency.bch;
|
||||
case 'bnb':
|
||||
return CryptoCurrency.bnb;
|
||||
case 'btc':
|
||||
return CryptoCurrency.btc;
|
||||
case 'dash':
|
||||
return CryptoCurrency.dash;
|
||||
case 'eos':
|
||||
return CryptoCurrency.eos;
|
||||
case 'eth':
|
||||
return CryptoCurrency.eth;
|
||||
case 'ltc':
|
||||
return CryptoCurrency.ltc;
|
||||
case 'nano':
|
||||
return CryptoCurrency.nano;
|
||||
case 'trx':
|
||||
return CryptoCurrency.trx;
|
||||
case 'usdt':
|
||||
return CryptoCurrency.usdt;
|
||||
case 'xlm':
|
||||
return CryptoCurrency.xlm;
|
||||
case 'xrp':
|
||||
return CryptoCurrency.xrp;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => title;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
CryptoCurrency currencyForWalletType(WalletType type) {
|
||||
switch (type) {
|
||||
case WalletType.bitcoin:
|
||||
return CryptoCurrency.btc;
|
||||
case WalletType.monero:
|
||||
return CryptoCurrency.xmr;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
|
||||
String cryptoToString(CryptoCurrency crypto) {
|
||||
switch (crypto) {
|
||||
case CryptoCurrency.xmr:
|
||||
return 'XMR';
|
||||
case CryptoCurrency.btc:
|
||||
return 'BTC';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:cake_wallet/entities/preferences_key.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
import 'package:cake_wallet/entities/node.dart';
|
||||
import 'package:cake_wallet/entities/balance_display_mode.dart';
|
||||
import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||
import 'package:cake_wallet/entities/node_list.dart';
|
||||
import 'package:cake_wallet/entities/transaction_priority.dart';
|
||||
|
||||
Future defaultSettingsMigration(
|
||||
{@required int version,
|
||||
@required SharedPreferences sharedPreferences,
|
||||
@required Box<Node> nodes}) async {
|
||||
final currentVersion =
|
||||
sharedPreferences.getInt('current_default_settings_migration_version') ??
|
||||
0;
|
||||
if (currentVersion >= version) {
|
||||
return;
|
||||
}
|
||||
|
||||
final migrationVersionsLength = version - currentVersion;
|
||||
final migrationVersions = List<int>.generate(
|
||||
migrationVersionsLength, (i) => currentVersion + (i + 1));
|
||||
|
||||
await Future.forEach(migrationVersions, (int version) async {
|
||||
try {
|
||||
switch (version) {
|
||||
case 1:
|
||||
await sharedPreferences.setString(
|
||||
PreferencesKey.currentFiatCurrencyKey,
|
||||
FiatCurrency.usd.toString());
|
||||
await sharedPreferences.setInt(
|
||||
PreferencesKey.currentTransactionPriorityKey,
|
||||
TransactionPriority.standart.raw);
|
||||
await sharedPreferences.setInt(
|
||||
PreferencesKey.currentBalanceDisplayModeKey,
|
||||
BalanceDisplayMode.availableBalance.raw);
|
||||
await sharedPreferences.setBool('save_recipient_address', true);
|
||||
await resetToDefault(nodes);
|
||||
await changeMoneroCurrentNodeToDefault(
|
||||
sharedPreferences: sharedPreferences, nodes: nodes);
|
||||
await changeBitcoinCurrentElectrumServerToDefault(
|
||||
sharedPreferences: sharedPreferences, nodes: nodes);
|
||||
|
||||
break;
|
||||
case 2:
|
||||
await replaceNodesMigration(nodes: nodes);
|
||||
await replaceDefaultNode(
|
||||
sharedPreferences: sharedPreferences, nodes: nodes);
|
||||
|
||||
break;
|
||||
case 3:
|
||||
await updateNodeTypes(nodes: nodes);
|
||||
await addBitcoinElectrumServerList(nodes: nodes);
|
||||
|
||||
break;
|
||||
case 4:
|
||||
await changeBitcoinCurrentElectrumServerToDefault(
|
||||
sharedPreferences: sharedPreferences, nodes: nodes);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
await sharedPreferences.setInt(
|
||||
'current_default_settings_migration_version', version);
|
||||
} catch (e) {
|
||||
print('Migration error: ${e.toString()}');
|
||||
}
|
||||
});
|
||||
|
||||
await sharedPreferences.setInt(
|
||||
'current_default_settings_migration_version', version);
|
||||
}
|
||||
|
||||
Future<void> replaceNodesMigration({@required Box<Node> nodes}) async {
|
||||
final replaceNodes = <String, Node>{
|
||||
'eu-node.cakewallet.io:18081':
|
||||
Node(uri: 'xmr-node-eu.cakewallet.com:18081', type: WalletType.monero),
|
||||
'node.cakewallet.io:18081': Node(
|
||||
uri: 'xmr-node-usa-east.cakewallet.com:18081', type: WalletType.monero),
|
||||
'node.xmr.ru:13666':
|
||||
Node(uri: 'node.monero.net:18081', type: WalletType.monero)
|
||||
};
|
||||
|
||||
nodes.values.forEach((Node node) async {
|
||||
final nodeToReplace = replaceNodes[node.uri];
|
||||
|
||||
if (nodeToReplace != null) {
|
||||
node.uri = nodeToReplace.uri;
|
||||
node.login = nodeToReplace.login;
|
||||
node.password = nodeToReplace.password;
|
||||
await node.save();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> changeMoneroCurrentNodeToDefault(
|
||||
{@required SharedPreferences sharedPreferences,
|
||||
@required Box<Node> nodes}) async {
|
||||
final node = getMoneroDefaultNode(nodes: nodes);
|
||||
final nodeId = node?.key as int ?? 0; // 0 - England
|
||||
|
||||
await sharedPreferences.setInt('current_node_id', nodeId);
|
||||
}
|
||||
|
||||
Node getBitcoinDefaultElectrumServer({@required Box<Node> nodes}) {
|
||||
final uri = 'electrumx.cakewallet.com:50002';
|
||||
|
||||
return nodes.values
|
||||
.firstWhere((Node node) => node.uri == uri, orElse: () => null) ??
|
||||
nodes.values.firstWhere((node) => node.type == WalletType.bitcoin,
|
||||
orElse: () => null);
|
||||
}
|
||||
|
||||
Node getMoneroDefaultNode({@required Box<Node> nodes}) {
|
||||
final timeZone = DateTime.now().timeZoneOffset.inHours;
|
||||
var nodeUri = '';
|
||||
|
||||
if (timeZone >= 1) {
|
||||
// Eurasia
|
||||
nodeUri = 'xmr-node-eu.cakewallet.com:18081';
|
||||
} else if (timeZone <= -4) {
|
||||
// America
|
||||
nodeUri = 'xmr-node-usa-east.cakewallet.com:18081';
|
||||
}
|
||||
|
||||
return nodes.values
|
||||
.firstWhere((Node node) => node.uri == nodeUri, orElse: () => null) ??
|
||||
nodes.values.first;
|
||||
}
|
||||
|
||||
Future<void> changeBitcoinCurrentElectrumServerToDefault(
|
||||
{@required SharedPreferences sharedPreferences,
|
||||
@required Box<Node> nodes}) async {
|
||||
final server = getBitcoinDefaultElectrumServer(nodes: nodes);
|
||||
final serverId = server?.key as int ?? 0;
|
||||
|
||||
await sharedPreferences.setInt('current_node_id_btc', serverId);
|
||||
}
|
||||
|
||||
Future<void> replaceDefaultNode(
|
||||
{@required SharedPreferences sharedPreferences,
|
||||
@required Box<Node> nodes}) async {
|
||||
const nodesForReplace = <String>[
|
||||
'xmr-node-uk.cakewallet.com:18081',
|
||||
'eu-node.cakewallet.io:18081',
|
||||
'node.cakewallet.io:18081'
|
||||
];
|
||||
final currentNodeId = sharedPreferences.getInt('current_node_id');
|
||||
final currentNode =
|
||||
nodes.values.firstWhere((Node node) => node.key == currentNodeId);
|
||||
final needToReplace =
|
||||
currentNode == null ? true : nodesForReplace.contains(currentNode.uri);
|
||||
|
||||
if (!needToReplace) {
|
||||
return;
|
||||
}
|
||||
|
||||
await changeMoneroCurrentNodeToDefault(
|
||||
sharedPreferences: sharedPreferences, nodes: nodes);
|
||||
}
|
||||
|
||||
Future<void> updateNodeTypes({@required Box<Node> nodes}) async {
|
||||
nodes.values.forEach((node) async {
|
||||
if (node.type == null) {
|
||||
node.type = WalletType.monero;
|
||||
await node.save();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> addBitcoinElectrumServerList({@required Box<Node> nodes}) async {
|
||||
final serverList = await loadElectrumServerList();
|
||||
await nodes.addAll(serverList);
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import 'dart:convert';
|
||||
import 'package:dio/dio.dart' as __dio;
|
||||
import 'package:crypto/crypto.dart' as crypto;
|
||||
import 'dart:math' as math;
|
||||
|
||||
class DigestRequest {
|
||||
final md5 = crypto.md5;
|
||||
|
||||
String generateCnonce() {
|
||||
final rnd = math.Random.secure();
|
||||
final values = List<int>.generate(32, (i) => rnd.nextInt(256));
|
||||
return base64Url.encode(values).substring(0, 8);
|
||||
}
|
||||
|
||||
String generateHA1({String realm, String username, String password}) {
|
||||
final ha1CredentialsData =
|
||||
Utf8Encoder().convert('$username:$realm:$password');
|
||||
final ha1 = md5.convert(ha1CredentialsData).toString();
|
||||
|
||||
return ha1;
|
||||
}
|
||||
|
||||
String generateHA2({String method, String uri}) {
|
||||
final ha2Data = Utf8Encoder().convert('$method:$uri');
|
||||
final ha2 = md5.convert(ha2Data).toString();
|
||||
|
||||
return ha2;
|
||||
}
|
||||
|
||||
String generateResponseString(
|
||||
{String ha1,
|
||||
String ha2,
|
||||
String nonce,
|
||||
String nonceCount,
|
||||
String cnonce,
|
||||
String qop}) {
|
||||
final responseData =
|
||||
Utf8Encoder().convert('$ha1:$nonce:$nonceCount:$cnonce:$qop:$ha2');
|
||||
final response = md5.convert(responseData).toString();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
Map<String, String> parsetAuthorizationHeader({String source}) {
|
||||
final authHeaderParts =
|
||||
source.substring(7).split(',').map((item) => item.trim());
|
||||
final authenticate = Map<String, String>();
|
||||
|
||||
for (final part in authHeaderParts) {
|
||||
final kv = part.split('=');
|
||||
authenticate[kv[0]] =
|
||||
kv.getRange(1, kv.length).join('=').replaceAll('"', '');
|
||||
}
|
||||
|
||||
return authenticate;
|
||||
}
|
||||
|
||||
Future<__dio.Response> request(
|
||||
{String uri, String login, String password}) async {
|
||||
const path = '/json_rpc';
|
||||
const method = 'POST';
|
||||
final url = Uri.http(uri, path);
|
||||
final dio = __dio.Dio();
|
||||
final headers = {'Content-type': 'application/json'};
|
||||
final body =
|
||||
json.encode({"jsonrpc": "2.0", "id": "0", "method": "get_info"});
|
||||
final credentialsResponse = await dio.post<Object>(url.toString(),
|
||||
options: __dio.Options(headers: headers, validateStatus: (_) => true));
|
||||
final authenticate = parsetAuthorizationHeader(
|
||||
source: credentialsResponse.headers['www-authenticate'].first);
|
||||
final qop = authenticate['qop'];
|
||||
final algorithm = 'MD5';
|
||||
final realm = 'monero-rpc';
|
||||
final nonce = authenticate['nonce'];
|
||||
final cnonce = generateCnonce();
|
||||
final nonceCount = '00000001';
|
||||
final ha1 = generateHA1(realm: realm, username: login, password: password);
|
||||
final ha2 = generateHA2(method: method, uri: path);
|
||||
final response = generateResponseString(
|
||||
ha1: ha1,
|
||||
ha2: ha2,
|
||||
nonce: nonce,
|
||||
nonceCount: nonceCount,
|
||||
cnonce: cnonce,
|
||||
qop: qop);
|
||||
|
||||
final authorizationHeaders = {
|
||||
'Content-type': 'application/json',
|
||||
'Authorization':
|
||||
'Digest username="$login",realm="$realm",nonce="$nonce",uri="$path",algorithm="$algorithm",qop=$qop,nc=$nonceCount,cnonce="$cnonce",response="$response"'
|
||||
};
|
||||
|
||||
return await dio.post<Object>(url.toString(),
|
||||
options: __dio.Options(headers: authorizationHeaders), data: body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import 'package:encrypt/encrypt.dart';
|
||||
import 'package:password/password.dart';
|
||||
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
||||
|
||||
String encrypt({String source, String key, int keyLength = 16}) {
|
||||
final _key = Key.fromUtf8(key);
|
||||
final iv = IV.fromLength(keyLength);
|
||||
final encrypter = Encrypter(AES(_key));
|
||||
final encrypted = encrypter.encrypt(source, iv: iv);
|
||||
|
||||
return encrypted.base64;
|
||||
}
|
||||
|
||||
String decrypt({String source, String key, int keyLength = 16}) {
|
||||
final _key = Key.fromUtf8(key);
|
||||
final iv = IV.fromLength(keyLength);
|
||||
final encrypter = Encrypter(AES(_key));
|
||||
final decrypted = encrypter.decrypt64(source, iv: iv);
|
||||
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
String hash({String source}) {
|
||||
final algorithm = PBKDF2();
|
||||
final hash = Password.hash(source, algorithm);
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
String encodedPinCode({String pin}) {
|
||||
final source = '${secrets.salt}$pin';
|
||||
|
||||
return encrypt(source: source, key: secrets.key);
|
||||
}
|
||||
|
||||
String decodedPinCode({String pin}) {
|
||||
final decrypted = decrypt(source: pin, key: secrets.key);
|
||||
|
||||
return decrypted.substring(secrets.key.length, decrypted.length);
|
||||
}
|
||||
|
||||
String encodeWalletPassword({String password}) {
|
||||
final source = password;
|
||||
final _key = secrets.shortKey + secrets.walletSalt;
|
||||
|
||||
return encrypt(source: source, key: _key);
|
||||
}
|
||||
|
||||
String decodeWalletPassword({String password}) {
|
||||
final source = password;
|
||||
final _key = secrets.shortKey + secrets.walletSalt;
|
||||
|
||||
return decrypt(source: source, key: _key);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
abstract class EnumerableItem<T> {
|
||||
const EnumerableItem({@required this.title, @required this.raw});
|
||||
|
||||
final T raw;
|
||||
final String title;
|
||||
|
||||
@override
|
||||
String toString() => title;
|
||||
}
|
||||
|
||||
mixin Serializable<T> on EnumerableItem<T> {
|
||||
static Serializable deserialize<T>({T raw}) => null;
|
||||
T serialize() => raw;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import 'package:cake_wallet/entities/enumerable_item.dart';
|
||||
|
||||
class FiatCurrency extends EnumerableItem<String> with Serializable<String> {
|
||||
const FiatCurrency({String symbol}) : super(title: symbol, raw: symbol);
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) => other is FiatCurrency && other.raw == raw;
|
||||
|
||||
static const all = [
|
||||
FiatCurrency.aud,
|
||||
FiatCurrency.bgn,
|
||||
FiatCurrency.brl,
|
||||
FiatCurrency.cad,
|
||||
FiatCurrency.chf,
|
||||
FiatCurrency.cny,
|
||||
FiatCurrency.czk,
|
||||
FiatCurrency.eur,
|
||||
FiatCurrency.dkk,
|
||||
FiatCurrency.gbp,
|
||||
FiatCurrency.hkd,
|
||||
FiatCurrency.hrk,
|
||||
FiatCurrency.huf,
|
||||
FiatCurrency.idr,
|
||||
FiatCurrency.ils,
|
||||
FiatCurrency.inr,
|
||||
FiatCurrency.isk,
|
||||
FiatCurrency.jpy,
|
||||
FiatCurrency.krw,
|
||||
FiatCurrency.mxn,
|
||||
FiatCurrency.myr,
|
||||
FiatCurrency.nok,
|
||||
FiatCurrency.nzd,
|
||||
FiatCurrency.php,
|
||||
FiatCurrency.pln,
|
||||
FiatCurrency.ron,
|
||||
FiatCurrency.rub,
|
||||
FiatCurrency.sek,
|
||||
FiatCurrency.sgd,
|
||||
FiatCurrency.thb,
|
||||
FiatCurrency.usd,
|
||||
FiatCurrency.zar,
|
||||
FiatCurrency.vef
|
||||
];
|
||||
|
||||
static const aud = FiatCurrency(symbol: 'AUD');
|
||||
static const bgn = FiatCurrency(symbol: 'BGN');
|
||||
static const brl = FiatCurrency(symbol: 'BRL');
|
||||
static const cad = FiatCurrency(symbol: 'CAD');
|
||||
static const chf = FiatCurrency(symbol: 'CHF');
|
||||
static const cny = FiatCurrency(symbol: 'CNY');
|
||||
static const czk = FiatCurrency(symbol: 'CZK');
|
||||
static const eur = FiatCurrency(symbol: 'EUR');
|
||||
static const dkk = FiatCurrency(symbol: 'DKK');
|
||||
static const gbp = FiatCurrency(symbol: 'GBP');
|
||||
static const hkd = FiatCurrency(symbol: 'HKD');
|
||||
static const hrk = FiatCurrency(symbol: 'HRK');
|
||||
static const huf = FiatCurrency(symbol: 'HUF');
|
||||
static const idr = FiatCurrency(symbol: 'IDR');
|
||||
static const ils = FiatCurrency(symbol: 'ILS');
|
||||
static const inr = FiatCurrency(symbol: 'INR');
|
||||
static const isk = FiatCurrency(symbol: 'ISK');
|
||||
static const jpy = FiatCurrency(symbol: 'JPY');
|
||||
static const krw = FiatCurrency(symbol: 'KRW');
|
||||
static const mxn = FiatCurrency(symbol: 'MXN');
|
||||
static const myr = FiatCurrency(symbol: 'MYR');
|
||||
static const nok = FiatCurrency(symbol: 'NOK');
|
||||
static const nzd = FiatCurrency(symbol: 'NZD');
|
||||
static const php = FiatCurrency(symbol: 'PHP');
|
||||
static const pln = FiatCurrency(symbol: 'PLN');
|
||||
static const ron = FiatCurrency(symbol: 'RON');
|
||||
static const rub = FiatCurrency(symbol: 'RUB');
|
||||
static const sek = FiatCurrency(symbol: 'SEK');
|
||||
static const sgd = FiatCurrency(symbol: 'SGD');
|
||||
static const thb = FiatCurrency(symbol: 'THB');
|
||||
static const usd = FiatCurrency(symbol: 'USD');
|
||||
static const zar = FiatCurrency(symbol: 'ZAR');
|
||||
static const vef = FiatCurrency(symbol: 'VEF');
|
||||
|
||||
@override
|
||||
int get hashCode => raw.hashCode ^ title.hashCode;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
String formatAmount(String amount) {
|
||||
if (!amount.contains('.')) {
|
||||
return amount + '.00';
|
||||
} else if (amount.endsWith('.')) {
|
||||
return amount + '00';
|
||||
}
|
||||
return amount;
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
import 'package:cake_wallet/entities/contact.dart';
|
||||
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||
import 'package:cake_wallet/entities/wallet_info.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
import 'package:cake_wallet/exchange/trade.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
const reservedNames = ["flutter_assets", "wallets", "db"];
|
||||
|
||||
Future<void> migrate_android_v1() async {
|
||||
final appDocDir = await getApplicationDocumentsDirectory();
|
||||
|
||||
await migrate_hives(appDocDir: appDocDir);
|
||||
await migrate_wallets(appDocDir: appDocDir);
|
||||
}
|
||||
|
||||
Future<void> migrate_ios_v1() async {
|
||||
final appDocDir = await getApplicationDocumentsDirectory();
|
||||
}
|
||||
|
||||
Future<void> migrate_hives({Directory appDocDir}) async {
|
||||
final dbDir = Directory('${appDocDir.path}/db');
|
||||
final files = List<File>();
|
||||
|
||||
appDocDir.listSync().forEach((FileSystemEntity item) {
|
||||
final ext = item.path.split('.').last;
|
||||
|
||||
if (item is File && (ext == "hive" || ext == "lock")) {
|
||||
files.add(item);
|
||||
}
|
||||
});
|
||||
|
||||
if (!dbDir.existsSync()) {
|
||||
dbDir.createSync();
|
||||
}
|
||||
|
||||
files.forEach((File hive) {
|
||||
final name = hive.path.split('/').last;
|
||||
hive.copySync('${dbDir.path}/$name');
|
||||
hive.deleteSync();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> migrate_wallets({Directory appDocDir}) async {
|
||||
final walletsDir = Directory('${appDocDir.path}/wallets');
|
||||
final moneroWalletsDir = Directory('${walletsDir.path}/monero');
|
||||
final dirs = List<Directory>();
|
||||
|
||||
appDocDir.listSync().forEach((FileSystemEntity item) {
|
||||
final name = item.path.split('/').last;
|
||||
|
||||
if (item is Directory && !reservedNames.contains(name)) {
|
||||
dirs.add(item);
|
||||
}
|
||||
});
|
||||
|
||||
if (!moneroWalletsDir.existsSync()) {
|
||||
await moneroWalletsDir.create(recursive: true);
|
||||
}
|
||||
|
||||
dirs.forEach((Directory dir) {
|
||||
final name = dir.path.split('/').last;
|
||||
final newDir = Directory('${moneroWalletsDir.path}/$name');
|
||||
newDir.createSync();
|
||||
|
||||
dir.listSync().forEach((file) {
|
||||
if (file is File) {
|
||||
final fileName = file.path.split('/').last;
|
||||
file.copySync('${newDir.path}/$fileName');
|
||||
file.deleteSync();
|
||||
}
|
||||
});
|
||||
|
||||
dir.deleteSync();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> migrate_ios_wallet_info(
|
||||
{@required Directory appDocDir,
|
||||
@required Box<WalletInfo> walletsInfo}) async {
|
||||
// final walletsDir = Directory('${appDocDir.path}/wallets');
|
||||
// final moneroWalletsDir = Directory('${walletsDir.path}/monero');
|
||||
|
||||
// moneroWalletsDir.listSync().forEach((item) async {
|
||||
// try {
|
||||
// if (item is Directory) {
|
||||
// final name = item.path.split('/').last;
|
||||
// final configFile = File('${item.path}/$name.json');
|
||||
// final config =
|
||||
// json.decode(configFile.readAsStringSync()) as Map<String, dynamic>;
|
||||
// final isRecovery = config["isRecovery"] as bool ?? false;
|
||||
// final id =
|
||||
// walletTypeToString(WalletType.monero).toLowerCase() + '_' + name;
|
||||
// final walletInfo =
|
||||
// WalletInfo(id: id, name: name, isRecovery: isRecovery);
|
||||
|
||||
// await walletsInfo.add(walletInfo);
|
||||
// }
|
||||
// } catch (e) {
|
||||
// print(e.toString());
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
Future<void> migrate_ios_trades_list(
|
||||
{@required Directory appDocDir, @required Box<Trade> trades}) async {
|
||||
final adderessBookJSON = File('${appDocDir.path}/trades_list.json');
|
||||
final List<dynamic> trades =
|
||||
json.decode(adderessBookJSON.readAsStringSync()) as List<dynamic>;
|
||||
}
|
||||
|
||||
Future<void> migrate_ios_address_book(
|
||||
{@required Directory appDocDir, @required Box<Contact> contacts}) async {
|
||||
final adderessBookJSON = File('${appDocDir.path}/address_book.json');
|
||||
final List<dynamic> addresses =
|
||||
json.decode(adderessBookJSON.readAsStringSync()) as List<dynamic>;
|
||||
|
||||
addresses.forEach((dynamic item) async {
|
||||
final _item = item as Map<String, dynamic>;
|
||||
final type = _item["type"] as String;
|
||||
final address = _item["address"] as String;
|
||||
final name = _item["name"] as String;
|
||||
final contact = Contact(
|
||||
address: address, name: name, type: CryptoCurrency.fromString(type));
|
||||
|
||||
await contacts.add(contact);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
Future<List<int>> getEncryptionKey(
|
||||
{String forKey, FlutterSecureStorage secureStorage}) async {
|
||||
final stringifiedKey =
|
||||
await secureStorage.read(key: 'transactionDescriptionsBoxKey');
|
||||
List<int> key;
|
||||
|
||||
if (stringifiedKey == null) {
|
||||
key = Hive.generateSecureKey();
|
||||
final keyStringified = key.join(',');
|
||||
await secureStorage.write(
|
||||
key: 'transactionDescriptionsBoxKey', value: keyStringified);
|
||||
} else {
|
||||
key = stringifiedKey
|
||||
.split(',')
|
||||
.map((i) => int.parse(i))
|
||||
.toList();
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:devicelocale/devicelocale.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
const Map<String, String> languages = {
|
||||
'en': 'English',
|
||||
'de': 'Deutsch (German)',
|
||||
'es': 'Español (Spanish)',
|
||||
'hi': 'हिंदी (Hindi)',
|
||||
'ja': '日本 (Japanese)',
|
||||
'ko': '한국어 (Korean)',
|
||||
'nl': 'Nederlands (Dutch)',
|
||||
'pl': 'Polski (Polish)',
|
||||
'pt': 'Português (Portuguese)',
|
||||
'ru': 'Русский (Russian)',
|
||||
'uk': 'Українська (Ukrainian)',
|
||||
'zh': '中文 (Chinese)'
|
||||
};
|
||||
|
||||
class Language with ChangeNotifier {
|
||||
Language(this._currentLanguage);
|
||||
|
||||
String _currentLanguage;
|
||||
|
||||
String getCurrentLanguage() => _currentLanguage;
|
||||
|
||||
void setCurrentLanguage(String language) {
|
||||
_currentLanguage = language;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
static Future<String> localeDetection() async {
|
||||
var locale = await Devicelocale.currentLocale;
|
||||
locale = Intl.shortLocale(locale);
|
||||
|
||||
return languages.keys.contains(locale) ? locale : 'en';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:cake_wallet/di.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cake_wallet/core/key_service.dart';
|
||||
import 'package:cake_wallet/core/wallet_service.dart';
|
||||
import 'package:cake_wallet/entities/preferences_key.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
Future<void> loadCurrentWallet() async {
|
||||
final appStore = getIt.get<AppStore>();
|
||||
final name = getIt
|
||||
.get<SharedPreferences>()
|
||||
.getString(PreferencesKey.currentWalletName);
|
||||
final typeRaw =
|
||||
getIt.get<SharedPreferences>().getInt(PreferencesKey.currentWalletType) ??
|
||||
0;
|
||||
final type = deserializeFromInt(typeRaw);
|
||||
final password =
|
||||
await getIt.get<KeyService>().getWalletPassword(walletName: name);
|
||||
final _service = getIt.get<WalletService>(param1: type);
|
||||
final wallet = await _service.openWallet(name, password);
|
||||
appStore.wallet = wallet;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class MnemonicItem {
|
||||
MnemonicItem({String text}) : _text = text;
|
||||
|
||||
String get text => _text;
|
||||
String _text;
|
||||
|
||||
void changeText(String text) => _text = text;
|
||||
|
||||
@override
|
||||
String toString() => text;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import 'package:cake_wallet/utils/mobx.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
import 'package:cake_wallet/entities/digest_request.dart';
|
||||
|
||||
part 'node.g.dart';
|
||||
|
||||
@HiveType(typeId: 1)
|
||||
class Node extends HiveObject with Keyable {
|
||||
Node(
|
||||
{@required this.uri,
|
||||
@required WalletType type,
|
||||
this.login,
|
||||
this.password}) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
Node.fromMap(Map map)
|
||||
: uri = map['uri'] as String ?? '',
|
||||
login = map['login'] as String,
|
||||
password = map['password'] as String,
|
||||
typeRaw = map['typeRaw'] as int;
|
||||
|
||||
static const boxName = 'Nodes';
|
||||
|
||||
@HiveField(0)
|
||||
String uri;
|
||||
|
||||
@HiveField(1)
|
||||
String login;
|
||||
|
||||
@HiveField(2)
|
||||
String password;
|
||||
|
||||
@HiveField(3)
|
||||
int typeRaw;
|
||||
|
||||
WalletType get type => deserializeFromInt(typeRaw);
|
||||
|
||||
set type(WalletType type) => typeRaw = serializeToInt(type);
|
||||
|
||||
Future<bool> requestNode() async {
|
||||
try {
|
||||
switch (type) {
|
||||
case WalletType.monero:
|
||||
return requestMoneroNode();
|
||||
case WalletType.bitcoin:
|
||||
return requestBitcoinElectrumServer();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> requestMoneroNode() async {
|
||||
Map<String, dynamic> resBody;
|
||||
|
||||
if (login != null && password != null) {
|
||||
final digestRequest = DigestRequest();
|
||||
final response = await digestRequest.request(
|
||||
uri: uri, login: login, password: password);
|
||||
resBody = response.data as Map<String, dynamic>;
|
||||
} else {
|
||||
final url = Uri.http(uri, '/json_rpc');
|
||||
final headers = {'Content-type': 'application/json'};
|
||||
final body =
|
||||
json.encode({'jsonrpc': '2.0', 'id': '0', 'method': 'get_info'});
|
||||
final response =
|
||||
await http.post(url.toString(), headers: headers, body: body);
|
||||
resBody = json.decode(response.body) as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
return !(resBody['result']['offline'] as bool);
|
||||
}
|
||||
|
||||
Future<bool> requestBitcoinElectrumServer() async {
|
||||
// FIXME: IMPLEMENT ME
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import "package:yaml/yaml.dart";
|
||||
import 'package:cake_wallet/entities/node.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
Future<List<Node>> loadDefaultNodes() async {
|
||||
final nodesRaw = await rootBundle.loadString('assets/node_list.yml');
|
||||
final nodes = loadYaml(nodesRaw) as YamlList;
|
||||
|
||||
return nodes.map((dynamic raw) {
|
||||
if (raw is Map) {
|
||||
final node = Node.fromMap(raw);
|
||||
node?.type = WalletType.monero;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
return null;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
Future<List<Node>> loadElectrumServerList() async {
|
||||
final serverListRaw =
|
||||
await rootBundle.loadString('assets/electrum_server_list.yml');
|
||||
final serverList = loadYaml(serverListRaw) as YamlList;
|
||||
|
||||
return serverList.map((dynamic raw) {
|
||||
if (raw is Map) {
|
||||
final node = Node.fromMap(raw);
|
||||
node?.type = WalletType.bitcoin;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
return null;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
Future resetToDefault(Box<Node> nodeSource) async {
|
||||
final moneroNodes = await loadDefaultNodes();
|
||||
final bitcoinElectrumServerList = await loadElectrumServerList();
|
||||
final nodes = moneroNodes + bitcoinElectrumServerList;
|
||||
|
||||
await nodeSource.clear();
|
||||
await nodeSource.addAll(nodes);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import 'package:basic_utils/basic_utils.dart';
|
||||
|
||||
class OpenaliasRecord {
|
||||
|
||||
OpenaliasRecord({this.address, this.name});
|
||||
|
||||
final String name;
|
||||
final String address;
|
||||
|
||||
static String formatDomainName(String name) {
|
||||
String formattedName = name;
|
||||
|
||||
if (name.contains("@")) {
|
||||
formattedName = name.replaceAll("@", ".");
|
||||
}
|
||||
|
||||
return formattedName;
|
||||
}
|
||||
|
||||
static Future<OpenaliasRecord> fetchAddressAndName(String formattedName) async {
|
||||
String address = formattedName;
|
||||
String name = formattedName;
|
||||
|
||||
if (formattedName.contains(".")) {
|
||||
try {
|
||||
final txtRecord = await DnsUtils.lookupRecord(formattedName, RRecordType.TXT, dnssec: true);
|
||||
|
||||
if (txtRecord != null) {
|
||||
|
||||
for (RRecord element in txtRecord) {
|
||||
String record = element.data;
|
||||
|
||||
if (record.contains("oa1:xmr") && record.contains("recipient_address")) {
|
||||
record = record.replaceAll('\"', "");
|
||||
|
||||
final dataList = record.split(";");
|
||||
|
||||
address = dataList.where((item) => (item.contains("recipient_address")))
|
||||
.toString().replaceAll("oa1:xmr recipient_address=", "")
|
||||
.replaceAll("(", "").replaceAll(")", "").trim();
|
||||
|
||||
final recipientName = dataList.where((item) => (item.contains("recipient_name"))).toString()
|
||||
.replaceAll("(", "").replaceAll(")", "").trim();
|
||||
|
||||
if (recipientName.isNotEmpty) {
|
||||
name = recipientName.replaceAll("recipient_name=", "");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print("${e.toString()}");
|
||||
}
|
||||
}
|
||||
|
||||
return OpenaliasRecord(address: address, name: name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
bool parseBoolFromString(String string) {
|
||||
return string.toString() == 'true';
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'dart:io';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
Future<String> pathForWalletDir({@required String name, @required WalletType type}) async {
|
||||
final root = await getApplicationDocumentsDirectory();
|
||||
final prefix = walletTypeToString(type).toLowerCase();
|
||||
final walletsDir = Directory('${root.path}/wallets');
|
||||
final walletDire = Directory('${walletsDir.path}/$prefix/$name');
|
||||
|
||||
if (!walletDire.existsSync()) {
|
||||
walletDire.createSync(recursive: true);
|
||||
}
|
||||
|
||||
return walletDire.path;
|
||||
}
|
||||
|
||||
Future<String> pathForWallet({@required String name, @required WalletType type}) async =>
|
||||
await pathForWalletDir(name: name, type: type)
|
||||
.then((path) => path + '/$name');
|
||||
@@ -0,0 +1,25 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:cw_monero/transaction_history.dart' as transaction_history;
|
||||
import 'package:cw_monero/structs/pending_transaction.dart';
|
||||
import 'package:cake_wallet/monero/monero_amount_format.dart';
|
||||
|
||||
class PendingTransaction {
|
||||
PendingTransaction(
|
||||
{@required this.amount, @required this.fee, @required this.hash});
|
||||
|
||||
PendingTransaction.fromTransactionDescription(
|
||||
PendingTransactionDescription transactionDescription)
|
||||
: amount = moneroAmountToString(amount: transactionDescription.amount),
|
||||
fee = moneroAmountToString(amount: transactionDescription.fee),
|
||||
hash = transactionDescription.hash,
|
||||
_pointerAddress = transactionDescription.pointerAddress;
|
||||
|
||||
final String amount;
|
||||
final String fee;
|
||||
final String hash;
|
||||
|
||||
int _pointerAddress;
|
||||
|
||||
Future<void> commit() async => transaction_history
|
||||
.commitTransactionFromPointerAddress(address: _pointerAddress);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
class PreferencesKey {
|
||||
static const currentWalletType ='current_wallet_type';
|
||||
static const currentWalletName ='current_wallet_name';
|
||||
static const currentNodeIdKey = 'current_node_id';
|
||||
static const currentBitcoinElectrumSererIdKey = 'current_node_id_btc';
|
||||
static const currentFiatCurrencyKey = 'current_fiat_currency';
|
||||
static const currentTransactionPriorityKey = 'current_fee_priority';
|
||||
static const currentBalanceDisplayModeKey = 'current_balance_display_mode';
|
||||
static const shouldSaveRecipientAddressKey = 'save_recipient_address';
|
||||
static const allowBiometricalAuthenticationKey =
|
||||
'allow_biometrical_authentication';
|
||||
static const currentDarkTheme = 'dark_theme';
|
||||
static const displayActionListModeKey = 'display_list_mode';
|
||||
static const currentPinLength = 'current_pin_length';
|
||||
static const currentLanguageCode = 'language_code';
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:barcode_scan/barcode_scan.dart';
|
||||
|
||||
var isQrScannerShown = false;
|
||||
|
||||
Future<String> presentQRScanner() async {
|
||||
isQrScannerShown = true;
|
||||
try {
|
||||
final result = await BarcodeScanner.scan();
|
||||
isQrScannerShown = false;
|
||||
return result.rawContent;
|
||||
} catch (e) {
|
||||
isQrScannerShown = false;
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
enum SecretStoreKey { moneroWalletPassword, pinCodePassword }
|
||||
|
||||
const moneroWalletPassword = "MONERO_WALLET_PASSWORD";
|
||||
const pinCodePassword = "PIN_CODE_PASSWORD";
|
||||
|
||||
String generateStoreKeyFor({SecretStoreKey key, String walletName = "",}) {
|
||||
var _key = "";
|
||||
|
||||
switch (key) {
|
||||
case SecretStoreKey.moneroWalletPassword:
|
||||
{
|
||||
_key = moneroWalletPassword + "_" + walletName.toUpperCase();
|
||||
}
|
||||
break;
|
||||
|
||||
case SecretStoreKey.pinCodePassword:
|
||||
{
|
||||
_key = pinCodePassword;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
{}
|
||||
}
|
||||
|
||||
return _key;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
|
||||
abstract class SyncStatus {
|
||||
const SyncStatus();
|
||||
|
||||
double progress();
|
||||
|
||||
String title();
|
||||
}
|
||||
|
||||
class SyncingSyncStatus extends SyncStatus {
|
||||
SyncingSyncStatus(this.blocksLeft, this.ptc);
|
||||
|
||||
final double ptc;
|
||||
final int blocksLeft;
|
||||
|
||||
@override
|
||||
double progress() => ptc;
|
||||
|
||||
@override
|
||||
String title() => S.current.sync_status_syncronizing;
|
||||
|
||||
@override
|
||||
String toString() => '$blocksLeft';
|
||||
}
|
||||
|
||||
class SyncedSyncStatus extends SyncStatus {
|
||||
@override
|
||||
double progress() => 1.0;
|
||||
|
||||
@override
|
||||
String title() => S.current.sync_status_syncronized;
|
||||
}
|
||||
|
||||
class NotConnectedSyncStatus extends SyncStatus {
|
||||
const NotConnectedSyncStatus();
|
||||
|
||||
@override
|
||||
double progress() => 0.0;
|
||||
|
||||
@override
|
||||
String title() => S.current.sync_status_not_connected;
|
||||
}
|
||||
|
||||
class StartingSyncStatus extends SyncStatus {
|
||||
@override
|
||||
double progress() => 0.0;
|
||||
|
||||
@override
|
||||
String title() => S.current.sync_status_starting_sync;
|
||||
}
|
||||
|
||||
class FailedSyncStatus extends SyncStatus {
|
||||
@override
|
||||
double progress() => 1.0;
|
||||
|
||||
@override
|
||||
String title() => S.current.sync_status_failed_connect;
|
||||
}
|
||||
|
||||
class ConnectingSyncStatus extends SyncStatus {
|
||||
@override
|
||||
double progress() => 0.0;
|
||||
|
||||
@override
|
||||
String title() => S.current.sync_status_connecting;
|
||||
}
|
||||
|
||||
class ConnectedSyncStatus extends SyncStatus {
|
||||
@override
|
||||
double progress() => 0.0;
|
||||
|
||||
@override
|
||||
String title() => S.current.sync_status_connected;
|
||||
}
|
||||
|
||||
class LostConnectionSyncStatus extends SyncStatus {
|
||||
@override
|
||||
double progress() => 1.0;
|
||||
|
||||
@override
|
||||
String title() => S.current.sync_status_failed_connect;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'template.g.dart';
|
||||
|
||||
@HiveType(typeId: 6)
|
||||
class Template extends HiveObject {
|
||||
Template({this.name, this.address, this.cryptoCurrency, this.amount});
|
||||
|
||||
static const boxName = 'Template';
|
||||
|
||||
@HiveField(0)
|
||||
String name;
|
||||
|
||||
@HiveField(1)
|
||||
String address;
|
||||
|
||||
@HiveField(2)
|
||||
String cryptoCurrency;
|
||||
|
||||
@HiveField(3)
|
||||
String amount;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
abstract class TransactionCreationCredentials {}
|
||||
@@ -0,0 +1,17 @@
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'transaction_description.g.dart';
|
||||
|
||||
@HiveType(typeId: 2)
|
||||
class TransactionDescription extends HiveObject {
|
||||
TransactionDescription({this.id, this.recipientAddress});
|
||||
|
||||
static const boxName = 'TransactionDescriptions';
|
||||
static const boxKey = 'transactionDescriptionsBoxKey';
|
||||
|
||||
@HiveField(0)
|
||||
String id;
|
||||
|
||||
@HiveField(1)
|
||||
String recipientAddress;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
enum TransactionDirection { incoming, outgoing }
|
||||
|
||||
TransactionDirection parseTransactionDirectionFromInt(int raw) {
|
||||
switch (raw) {
|
||||
case 0: return TransactionDirection.incoming;
|
||||
case 1: return TransactionDirection.outgoing;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
TransactionDirection parseTransactionDirectionFromNumber(String raw) {
|
||||
switch (raw) {
|
||||
case "0": return TransactionDirection.incoming;
|
||||
case "1": return TransactionDirection.outgoing;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:cake_wallet/entities/transaction_info.dart';
|
||||
|
||||
abstract class TransactionHistory {
|
||||
Observable<List<TransactionInfo>> transactions;
|
||||
Future<List<TransactionInfo>> getAll();
|
||||
Future update();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import 'package:cake_wallet/entities/transaction_direction.dart';
|
||||
|
||||
abstract class TransactionInfo extends Object {
|
||||
String id;
|
||||
int amount;
|
||||
TransactionDirection direction;
|
||||
bool isPending;
|
||||
DateTime date;
|
||||
int height;
|
||||
int confirmations;
|
||||
String amountFormatted();
|
||||
String fiatAmount();
|
||||
void changeFiatAmount(String amount);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/entities/enumerable_item.dart';
|
||||
|
||||
class TransactionPriority extends EnumerableItem<int> with Serializable<int> {
|
||||
const TransactionPriority({String title, int raw})
|
||||
: super(title: title, raw: raw);
|
||||
|
||||
static const all = [
|
||||
TransactionPriority.slow,
|
||||
TransactionPriority.regular,
|
||||
TransactionPriority.medium,
|
||||
TransactionPriority.fast,
|
||||
TransactionPriority.fastest
|
||||
];
|
||||
static const slow = TransactionPriority(title: 'Slow', raw: 0);
|
||||
static const regular = TransactionPriority(title: 'Regular', raw: 1);
|
||||
static const medium = TransactionPriority(title: 'Medium', raw: 2);
|
||||
static const fast = TransactionPriority(title: 'Fast', raw: 3);
|
||||
static const fastest = TransactionPriority(title: 'Fastest', raw: 4);
|
||||
static const standart = slow;
|
||||
|
||||
static TransactionPriority deserialize({int raw}) {
|
||||
switch (raw) {
|
||||
case 0:
|
||||
return slow;
|
||||
case 1:
|
||||
return regular;
|
||||
case 2:
|
||||
return medium;
|
||||
case 3:
|
||||
return fast;
|
||||
case 4:
|
||||
return fastest;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
switch (this) {
|
||||
case TransactionPriority.slow:
|
||||
return S.current.transaction_priority_slow;
|
||||
case TransactionPriority.regular:
|
||||
return S.current.transaction_priority_regular;
|
||||
case TransactionPriority.medium:
|
||||
return S.current.transaction_priority_medium;
|
||||
case TransactionPriority.fast:
|
||||
return S.current.transaction_priority_fast;
|
||||
case TransactionPriority.fastest:
|
||||
return S.current.transaction_priority_fastest;
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:cake_wallet/entities/sync_status.dart';
|
||||
import 'package:cake_wallet/entities/transaction_history.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
import 'package:cake_wallet/entities/transaction_creation_credentials.dart';
|
||||
import 'package:cake_wallet/entities/pending_transaction.dart';
|
||||
import 'package:cake_wallet/entities/balance.dart';
|
||||
import 'package:cake_wallet/entities/node.dart';
|
||||
|
||||
abstract class Wallet {
|
||||
WalletType getType();
|
||||
|
||||
WalletType walletType;
|
||||
|
||||
Observable<Balance> onBalanceChange;
|
||||
|
||||
Observable<SyncStatus> syncStatus;
|
||||
|
||||
Observable<String> get onNameChange;
|
||||
|
||||
Observable<String> get onAddressChange;
|
||||
|
||||
String get name;
|
||||
|
||||
String get address;
|
||||
|
||||
Future updateInfo();
|
||||
|
||||
Future<String> getFilename();
|
||||
|
||||
Future<String> getName();
|
||||
|
||||
Future<String> getAddress();
|
||||
|
||||
Future<String> getSeed();
|
||||
|
||||
Future<Map<String, String>> getKeys();
|
||||
|
||||
Future<String> getFullBalance();
|
||||
|
||||
Future<String> getUnlockedBalance();
|
||||
|
||||
Future<int> getCurrentHeight();
|
||||
|
||||
Future<int> getNodeHeight();
|
||||
|
||||
Future<bool> isConnected();
|
||||
|
||||
Future close();
|
||||
|
||||
TransactionHistory getHistory();
|
||||
|
||||
Future connectToNode({Node node, bool useSSL = false, bool isLightWallet = false});
|
||||
|
||||
Future startSync();
|
||||
|
||||
Future<PendingTransaction> createTransaction(
|
||||
TransactionCreationCredentials credentials);
|
||||
|
||||
Future rescan({int restoreHeight = 0});
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
class WalletDescription {
|
||||
WalletDescription({this.name, this.type});
|
||||
|
||||
final String name;
|
||||
final WalletType type;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
part 'wallet_info.g.dart';
|
||||
|
||||
@HiveType(typeId: 4)
|
||||
class WalletInfo extends HiveObject {
|
||||
WalletInfo(this.id, this.name, this.type, this.isRecovery, this.restoreHeight,
|
||||
this.timestamp, this.dirPath, this.path);
|
||||
|
||||
factory WalletInfo.external(
|
||||
{@required String id,
|
||||
@required String name,
|
||||
@required WalletType type,
|
||||
@required bool isRecovery,
|
||||
@required int restoreHeight,
|
||||
@required DateTime date,
|
||||
@required String dirPath,
|
||||
@required String path}) {
|
||||
return WalletInfo(id, name, type, isRecovery, restoreHeight,
|
||||
date.millisecondsSinceEpoch ?? 0, dirPath, path);
|
||||
}
|
||||
|
||||
static const boxName = 'WalletInfo';
|
||||
|
||||
@HiveField(0)
|
||||
String id;
|
||||
|
||||
@HiveField(1)
|
||||
String name;
|
||||
|
||||
@HiveField(2)
|
||||
WalletType type;
|
||||
|
||||
@HiveField(3)
|
||||
bool isRecovery;
|
||||
|
||||
@HiveField(4)
|
||||
int restoreHeight;
|
||||
|
||||
@HiveField(5)
|
||||
int timestamp;
|
||||
|
||||
@HiveField(6)
|
||||
String dirPath;
|
||||
|
||||
@HiveField(7)
|
||||
String path;
|
||||
|
||||
DateTime get date => DateTime.fromMillisecondsSinceEpoch(timestamp);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'wallet_type.g.dart';
|
||||
|
||||
const walletTypes = [WalletType.monero, WalletType.bitcoin];
|
||||
|
||||
@HiveType(typeId: 5)
|
||||
enum WalletType {
|
||||
@HiveField(0)
|
||||
monero,
|
||||
|
||||
@HiveField(1)
|
||||
none,
|
||||
|
||||
@HiveField(2)
|
||||
bitcoin
|
||||
}
|
||||
|
||||
int serializeToInt(WalletType type) {
|
||||
switch (type) {
|
||||
case WalletType.monero:
|
||||
return 0;
|
||||
case WalletType.bitcoin:
|
||||
return 1;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
WalletType deserializeFromInt(int raw) {
|
||||
switch (raw) {
|
||||
case 0:
|
||||
return WalletType.monero;
|
||||
case 1:
|
||||
return WalletType.bitcoin;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
String walletTypeToString(WalletType type) {
|
||||
switch (type) {
|
||||
case WalletType.monero:
|
||||
return 'Monero';
|
||||
case WalletType.bitcoin:
|
||||
return 'Bitcoin';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import 'package:cake_wallet/entities/wallet.dart';
|
||||
import 'package:cake_wallet/entities/wallet_description.dart';
|
||||
|
||||
abstract class WalletsManager {
|
||||
Future<Wallet> create(String name, String password, String language);
|
||||
|
||||
Future<Wallet> restoreFromSeed(
|
||||
String name, String password, String seed, int restoreHeight);
|
||||
|
||||
Future<Wallet> restoreFromKeys(String name, String password, String language,
|
||||
int restoreHeight, String address, String viewKey, String spendKey);
|
||||
|
||||
Future<Wallet> openWallet(String name, String password);
|
||||
|
||||
Future<bool> isWalletExit(String name);
|
||||
|
||||
Future remove(WalletDescription wallet);
|
||||
}
|
||||
Reference in New Issue
Block a user