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
@@ -3,4 +3,7 @@ import 'package:cake_wallet/src/domain/common/crypto_amount_format.dart';
const bitcoinAmountDivider = 100000000;
double bitcoinAmountToDouble({int amount}) =>
cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider);
cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider);
int doubleToBitcoinAmount(double amount) =>
(amount * bitcoinAmountDivider).toInt();
+6
View File
@@ -22,6 +22,12 @@ class Contact extends HiveObject {
CryptoCurrency get type => CryptoCurrency.deserialize(raw: raw);
@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;
}
@@ -1,3 +1,5 @@
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:shared_preferences/shared_preferences.dart';
@@ -14,7 +16,6 @@ Future defaultSettingsMigration(
final currentVersion =
sharedPreferences.getInt('current_default_settings_migration_version') ??
0;
if (currentVersion >= version) {
return;
}
@@ -28,10 +29,11 @@ Future defaultSettingsMigration(
switch (version) {
case 1:
await sharedPreferences.setString(
'current_fiat_currency', FiatCurrency.usd.toString());
SettingsStoreBase.currentFiatCurrencyKey, FiatCurrency.usd.toString());
await sharedPreferences.setInt(
'current_fee_priority', TransactionPriority.standart.raw);
await sharedPreferences.setInt('current_balance_display_mode',
SettingsStoreBase.currentTransactionPriorityKey, TransactionPriority.standart.raw);
await sharedPreferences.setInt(
SettingsStoreBase.currentBalanceDisplayModeKey,
BalanceDisplayMode.availableBalance.raw);
await sharedPreferences.setBool('save_recipient_address', true);
await resetToDefault(nodes);
@@ -44,6 +46,10 @@ Future defaultSettingsMigration(
await replaceDefaultNode(
sharedPreferences: sharedPreferences, nodes: nodes);
break;
case 3:
await updateNodeTypes(nodes: nodes);
await addBitcoinElectrumServerList(nodes: nodes);
break;
default:
break;
@@ -87,9 +93,11 @@ Future<void> changeCurrentNodeToDefault(
final timeZone = DateTime.now().timeZoneOffset.inHours;
String nodeUri = '';
if (timeZone >= 1) { // Eurasia
if (timeZone >= 1) {
// Eurasia
nodeUri = 'xmr-node-eu.cakewallet.com:18081';
} else if (timeZone <= -4) { // America
} else if (timeZone <= -4) {
// America
nodeUri = 'xmr-node-usa-east.cakewallet.com:18081';
}
@@ -121,3 +129,17 @@ Future<void> replaceDefaultNode(
await changeCurrentNodeToDefault(
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);
}
+36 -5
View File
@@ -2,18 +2,22 @@ import 'package:flutter/foundation.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:hive/hive.dart';
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
import 'package:cake_wallet/src/domain/common/digest_request.dart';
part 'node.g.dart';
@HiveType(typeId: 1)
class Node extends HiveObject {
Node({@required this.uri, this.login, this.password});
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;
password = map['password'] as String,
typeRaw = map['typeRaw'] as int;
static const boxName = 'Nodes';
@@ -26,7 +30,29 @@ class Node extends HiveObject {
@HiveField(2)
String password;
Future<bool> requestNode(String uri, {String login, String password}) async {
@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) {
@@ -38,12 +64,17 @@ class Node extends HiveObject {
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"});
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);
return !(resBody['result']['offline'] as bool);
}
Future<bool> requestBitcoinElectrumServer() async {
// FIXME: IMPLEMENT ME
return true;
}
}
+24 -4
View File
@@ -2,6 +2,7 @@ import 'package:flutter/services.dart';
import 'package:hive/hive.dart';
import "package:yaml/yaml.dart";
import 'package:cake_wallet/src/domain/common/node.dart';
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
Future<List<Node>> loadDefaultNodes() async {
final nodesRaw = await rootBundle.loadString('assets/node_list.yml');
@@ -16,15 +17,34 @@ Future<List<Node>> loadDefaultNodes() async {
}).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 nodes = await loadDefaultNodes();
final enteties = Map<int, Node>();
final moneroNodes = await loadDefaultNodes();
final bitcoinElectrumServerList = await loadElectrumServerList();
final nodes = moneroNodes + bitcoinElectrumServerList;
final entities = <int, Node>{};
await nodeSource.clear();
for (var i = 0; i < nodes.length; i++) {
enteties[i] = nodes[i];
entities[i] = nodes[i];
}
await nodeSource.putAll(enteties);
await nodeSource.putAll(entities);
}
+5 -1
View File
@@ -20,15 +20,19 @@ int serializeToInt(WalletType type) {
switch (type) {
case WalletType.monero:
return 0;
case WalletType.bitcoin:
return 1;
default:
return -1;
}
}
WalletType deserializeToInt(int raw) {
WalletType deserializeFromInt(int raw) {
switch (raw) {
case 0:
return WalletType.monero;
case 1:
return WalletType.bitcoin;
default:
return null;
}