From bb397cd3eea5d9dacc1e6c2e5a18b2a059ba4145 Mon Sep 17 00:00:00 2001 From: fosse Date: Thu, 7 Sep 2023 11:34:22 -0400 Subject: [PATCH 1/7] pownode refactor --- cw_core/lib/pow_node.dart | 272 +++++++++--------- cw_core/lib/wallet_base.dart | 2 +- cw_nano/lib/nano_client.dart | 4 +- cw_nano/lib/nano_wallet.dart | 2 +- lib/di.dart | 10 +- lib/entities/default_settings_migration.dart | 14 +- lib/entities/node_list.dart | 8 +- lib/main.dart | 10 +- lib/router.dart | 2 +- .../nodes/pow_node_create_or_edit_page.dart | 2 +- .../screens/nodes/widgets/pow_node_form.dart | 3 +- .../nodes/widgets/pow_node_list_row.dart | 3 +- lib/store/settings_store.dart | 14 +- .../pow_node_create_or_edit_view_model.dart | 12 +- .../node_list/pow_node_list_view_model.dart | 14 +- 15 files changed, 187 insertions(+), 185 deletions(-) diff --git a/cw_core/lib/pow_node.dart b/cw_core/lib/pow_node.dart index 1e98bc323..9963dc0cb 100644 --- a/cw_core/lib/pow_node.dart +++ b/cw_core/lib/pow_node.dart @@ -1,159 +1,159 @@ -import 'dart:io'; -import 'package:cw_core/keyable.dart'; -import 'dart:convert'; -import 'package:http/http.dart' as http; -import 'package:hive/hive.dart'; -import 'package:cw_core/hive_type_ids.dart'; -import 'package:cw_core/wallet_type.dart'; -import 'package:http/io_client.dart' as ioc; +// import 'dart:io'; +// import 'package:cw_core/keyable.dart'; +// import 'dart:convert'; +// import 'package:http/http.dart' as http; +// import 'package:hive/hive.dart'; +// import 'package:cw_core/hive_type_ids.dart'; +// import 'package:cw_core/wallet_type.dart'; +// import 'package:http/io_client.dart' as ioc; -part 'pow_node.g.dart'; +// part 'pow_node.g.dart'; -Uri createUriFromElectrumAddress(String address) => Uri.tryParse('tcp://$address')!; +// Uri createUriFromElectrumAddress(String address) => Uri.tryParse('tcp://$address')!; -@HiveType(typeId: PowNode.typeId) -class PowNode extends HiveObject with Keyable { - PowNode({ - this.login, - this.password, - this.useSSL, - this.trusted = false, - this.socksProxyAddress, - String? uri, - WalletType? type, - }) { - if (uri != null) { - uriRaw = uri; - } - if (type != null) { - this.type = type; - } - } +// @HiveType(typeId: PowNode.typeId) +// class PowNode extends HiveObject with Keyable { +// PowNode({ +// this.login, +// this.password, +// this.useSSL, +// this.trusted = false, +// this.socksProxyAddress, +// String? uri, +// WalletType? type, +// }) { +// if (uri != null) { +// uriRaw = uri; +// } +// if (type != null) { +// this.type = type; +// } +// } - PowNode.fromMap(Map map) - : uriRaw = map['uri'] as String? ?? '', - login = map['login'] as String?, - password = map['password'] as String?, - useSSL = map['useSSL'] as bool?, - trusted = map['trusted'] as bool? ?? false, - socksProxyAddress = map['socksProxyPort'] as String?; +// PowNode.fromMap(Map map) +// : uriRaw = map['uri'] as String? ?? '', +// login = map['login'] as String?, +// password = map['password'] as String?, +// useSSL = map['useSSL'] as bool?, +// trusted = map['trusted'] as bool? ?? false, +// socksProxyAddress = map['socksProxyPort'] as String?; - static const typeId = POW_NODE_TYPE_ID; - static const boxName = 'PowNodes'; +// static const typeId = POW_NODE_TYPE_ID; +// static const boxName = 'PowNodes'; - @HiveField(0, defaultValue: '') - late String uriRaw; +// @HiveField(0, defaultValue: '') +// late String uriRaw; - @HiveField(1) - String? login; +// @HiveField(1) +// String? login; - @HiveField(2) - String? password; +// @HiveField(2) +// String? password; - @HiveField(3, defaultValue: 0) - late int typeRaw; +// @HiveField(3, defaultValue: 0) +// late int typeRaw; - @HiveField(4) - bool? useSSL; +// @HiveField(4) +// bool? useSSL; - @HiveField(5, defaultValue: false) - bool trusted; +// @HiveField(5, defaultValue: false) +// bool trusted; - @HiveField(6) - String? socksProxyAddress; +// @HiveField(6) +// String? socksProxyAddress; - bool get isSSL => useSSL ?? false; +// bool get isSSL => useSSL ?? false; - bool get useSocksProxy => socksProxyAddress == null ? false : socksProxyAddress!.isNotEmpty; +// bool get useSocksProxy => socksProxyAddress == null ? false : socksProxyAddress!.isNotEmpty; - Uri get uri { - switch (type) { - case WalletType.monero: - return Uri.http(uriRaw, ''); - case WalletType.bitcoin: - return createUriFromElectrumAddress(uriRaw); - case WalletType.litecoin: - return createUriFromElectrumAddress(uriRaw); - case WalletType.haven: - return Uri.http(uriRaw, ''); - case WalletType.ethereum: - return Uri.https(uriRaw, ''); - case WalletType.nano: - case WalletType.banano: - if (uriRaw.contains("https") || uriRaw.endsWith("443") || isSSL) { - return Uri.https(uriRaw, ''); - } else { - return Uri.http(uriRaw, ''); - } - default: - throw Exception('Unexpected type ${type.toString()} for Node uri'); - } - } +// Uri get uri { +// switch (type) { +// case WalletType.monero: +// return Uri.http(uriRaw, ''); +// case WalletType.bitcoin: +// return createUriFromElectrumAddress(uriRaw); +// case WalletType.litecoin: +// return createUriFromElectrumAddress(uriRaw); +// case WalletType.haven: +// return Uri.http(uriRaw, ''); +// case WalletType.ethereum: +// return Uri.https(uriRaw, ''); +// case WalletType.nano: +// case WalletType.banano: +// if (uriRaw.contains("https") || uriRaw.endsWith("443") || isSSL) { +// return Uri.https(uriRaw, ''); +// } else { +// return Uri.http(uriRaw, ''); +// } +// default: +// throw Exception('Unexpected type ${type.toString()} for Node uri'); +// } +// } - @override - bool operator ==(other) => - other is PowNode && - (other.uriRaw == uriRaw && - other.login == login && - other.password == password && - other.typeRaw == typeRaw && - other.useSSL == useSSL && - other.trusted == trusted && - other.socksProxyAddress == socksProxyAddress); +// @override +// bool operator ==(other) => +// other is PowNode && +// (other.uriRaw == uriRaw && +// other.login == login && +// other.password == password && +// other.typeRaw == typeRaw && +// other.useSSL == useSSL && +// other.trusted == trusted && +// other.socksProxyAddress == socksProxyAddress); - @override - int get hashCode => - uriRaw.hashCode ^ - login.hashCode ^ - password.hashCode ^ - typeRaw.hashCode ^ - useSSL.hashCode ^ - trusted.hashCode ^ - socksProxyAddress.hashCode; +// @override +// int get hashCode => +// uriRaw.hashCode ^ +// login.hashCode ^ +// password.hashCode ^ +// typeRaw.hashCode ^ +// useSSL.hashCode ^ +// trusted.hashCode ^ +// socksProxyAddress.hashCode; - @override - dynamic get keyIndex { - _keyIndex ??= key; - return _keyIndex; - } +// @override +// dynamic get keyIndex { +// _keyIndex ??= key; +// return _keyIndex; +// } - WalletType get type => deserializeFromInt(typeRaw); +// WalletType get type => deserializeFromInt(typeRaw); - set type(WalletType type) => typeRaw = serializeToInt(type); +// set type(WalletType type) => typeRaw = serializeToInt(type); - dynamic _keyIndex; +// dynamic _keyIndex; - Future requestNode() async { - try { - switch (type) { - case WalletType.nano: - return requestNanoPowNode(); - default: - return false; - } - } catch (_) { - return false; - } - } +// Future requestNode() async { +// try { +// switch (type) { +// case WalletType.nano: +// return requestNanoPowNode(); +// default: +// return false; +// } +// } catch (_) { +// return false; +// } +// } - Future requestNanoPowNode() async { - return http - .post( - uri, - headers: {'Content-type': 'application/json'}, - body: json.encode( - { - "action": "work_generate", - "hash": "0000000000000000000000000000000000000000000000000000000000000000", - }, - ), - ) - .then((http.Response response) { - if (response.statusCode == 200) { - return true; - } else { - return false; - } - }); - } -} +// Future requestNanoPowNode() async { +// return http +// .post( +// uri, +// headers: {'Content-type': 'application/json'}, +// body: json.encode( +// { +// "action": "work_generate", +// "hash": "0000000000000000000000000000000000000000000000000000000000000000", +// }, +// ), +// ) +// .then((http.Response response) { +// if (response.statusCode == 200) { +// return true; +// } else { +// return false; +// } +// }); +// } +// } diff --git a/cw_core/lib/wallet_base.dart b/cw_core/lib/wallet_base.dart index 42fb22e75..5d0e388a1 100644 --- a/cw_core/lib/wallet_base.dart +++ b/cw_core/lib/wallet_base.dart @@ -60,7 +60,7 @@ abstract class WalletBase< Future connectToNode({required Node node}); // there is a default definition here because only coins with a pow node (nano based) need to override this - Future connectToPowNode({required PowNode node}) async {} + Future connectToPowNode({required Node node}) async {} Future startSync(); diff --git a/cw_nano/lib/nano_client.dart b/cw_nano/lib/nano_client.dart index 236ce68f3..72066afa2 100644 --- a/cw_nano/lib/nano_client.dart +++ b/cw_nano/lib/nano_client.dart @@ -18,7 +18,7 @@ class NanoClient { "nano_38713x95zyjsqzx6nm1dsom1jmm668owkeb9913ax6nfgj15az3nu8xkx579"; Node? _node; - PowNode? _powNode; + Node? _powNode; bool connect(Node node) { try { @@ -29,7 +29,7 @@ class NanoClient { } } - bool connectPow(PowNode node) { + bool connectPow(Node node) { try { _powNode = node; return true; diff --git a/cw_nano/lib/nano_wallet.dart b/cw_nano/lib/nano_wallet.dart index 78855334b..941406bb0 100644 --- a/cw_nano/lib/nano_wallet.dart +++ b/cw_nano/lib/nano_wallet.dart @@ -151,7 +151,7 @@ abstract class NanoWalletBase } @override - Future connectToPowNode({required PowNode node}) async { + Future connectToPowNode({required Node node}) async { _client.connectPow(node); } diff --git a/lib/di.dart b/lib/di.dart index 2573eb6dd..85e6bb87b 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -226,7 +226,7 @@ final getIt = GetIt.instance; var _isSetupFinished = false; late Box _walletInfoSource; late Box _nodeSource; -late Box _powNodeSource; +late Box _powNodeSource; late Box _contactSource; late Box _tradesSource; late Box