2023-05-26 21:21:16 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Stack Wallet.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2023 Cypher Stack
|
|
|
|
* All Rights Reserved.
|
|
|
|
* The code is distributed under GPLv3 license, see LICENSE file for details.
|
|
|
|
* Generated by Cypher Stack on 2023-05-26
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-11-02 21:03:14 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:flutter_svg/svg.dart';
|
2022-11-02 21:03:14 +00:00
|
|
|
import 'package:stackwallet/electrumx_rpc/electrumx.dart';
|
|
|
|
import 'package:stackwallet/models/node_model.dart';
|
|
|
|
import 'package:stackwallet/notifications/show_flush_bar.dart';
|
2022-12-12 21:59:06 +00:00
|
|
|
import 'package:stackwallet/pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart';
|
2022-11-02 21:03:14 +00:00
|
|
|
import 'package:stackwallet/pages/settings_views/global_settings_view/manage_nodes_views/node_details_view.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/providers/providers.dart';
|
2023-05-09 21:57:40 +00:00
|
|
|
import 'package:stackwallet/themes/stack_colors.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/utilities/assets.dart';
|
|
|
|
import 'package:stackwallet/utilities/constants.dart';
|
|
|
|
import 'package:stackwallet/utilities/default_nodes.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2022-11-02 21:03:14 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/sync_type_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
|
|
|
import 'package:stackwallet/utilities/test_epic_box_connection.dart';
|
2023-05-30 16:38:47 +00:00
|
|
|
import 'package:stackwallet/utilities/test_eth_node_connection.dart';
|
2022-11-02 21:03:14 +00:00
|
|
|
import 'package:stackwallet/utilities/test_monero_node_connection.dart';
|
2023-08-23 16:59:13 +00:00
|
|
|
import 'package:stackwallet/utilities/test_stellar_node_connection.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/utilities/text_styles.dart';
|
2022-11-02 21:03:14 +00:00
|
|
|
import 'package:stackwallet/utilities/util.dart';
|
|
|
|
import 'package:stackwallet/widgets/conditional_parent.dart';
|
|
|
|
import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart';
|
|
|
|
import 'package:stackwallet/widgets/expandable.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/widgets/node_options_sheet.dart';
|
|
|
|
import 'package:stackwallet/widgets/rounded_white_container.dart';
|
2022-11-02 21:03:14 +00:00
|
|
|
import 'package:tuple/tuple.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
class NodeCard extends ConsumerStatefulWidget {
|
|
|
|
const NodeCard({
|
|
|
|
Key? key,
|
|
|
|
required this.nodeId,
|
|
|
|
required this.coin,
|
|
|
|
required this.popBackToRoute,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
final Coin coin;
|
|
|
|
final String nodeId;
|
|
|
|
final String popBackToRoute;
|
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<NodeCard> createState() => _NodeCardState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NodeCardState extends ConsumerState<NodeCard> {
|
|
|
|
String _status = "Disconnected";
|
|
|
|
late final String nodeId;
|
2022-11-17 16:12:19 +00:00
|
|
|
bool _advancedIsExpanded = false;
|
2022-11-02 21:03:14 +00:00
|
|
|
|
|
|
|
Future<void> _notifyWalletsOfUpdatedNode(WidgetRef ref) async {
|
|
|
|
final managers = ref
|
|
|
|
.read(walletsChangeNotifierProvider)
|
|
|
|
.managers
|
|
|
|
.where((e) => e.coin == widget.coin);
|
|
|
|
final prefs = ref.read(prefsChangeNotifierProvider);
|
|
|
|
|
|
|
|
switch (prefs.syncType) {
|
|
|
|
case SyncingType.currentWalletOnly:
|
|
|
|
for (final manager in managers) {
|
|
|
|
if (manager.isActiveWallet) {
|
|
|
|
manager.updateNode(true);
|
|
|
|
} else {
|
|
|
|
manager.updateNode(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SyncingType.selectedWalletsAtStartup:
|
|
|
|
final List<String> walletIdsToSync = prefs.walletIdsSyncOnStartup;
|
|
|
|
for (final manager in managers) {
|
|
|
|
if (walletIdsToSync.contains(manager.walletId)) {
|
|
|
|
manager.updateNode(true);
|
|
|
|
} else {
|
|
|
|
manager.updateNode(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SyncingType.allWalletsOnStartup:
|
|
|
|
for (final manager in managers) {
|
|
|
|
manager.updateNode(true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> _testConnection(
|
|
|
|
NodeModel node,
|
|
|
|
BuildContext context,
|
|
|
|
WidgetRef ref,
|
|
|
|
) async {
|
|
|
|
bool testPassed = false;
|
|
|
|
|
|
|
|
switch (widget.coin) {
|
|
|
|
case Coin.epicCash:
|
|
|
|
try {
|
2022-12-12 21:59:06 +00:00
|
|
|
testPassed = await testEpicNodeConnection(
|
|
|
|
NodeFormData()
|
|
|
|
..host = node.host
|
|
|
|
..useSSL = node.useSSL
|
|
|
|
..port = node.port,
|
|
|
|
) !=
|
|
|
|
null;
|
2022-11-02 21:03:14 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log("$e\n$s", level: LogLevel.Warning);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Coin.monero:
|
|
|
|
case Coin.wownero:
|
|
|
|
try {
|
|
|
|
final uri = Uri.parse(node.host);
|
|
|
|
if (uri.scheme.startsWith("http")) {
|
|
|
|
final String path = uri.path.isEmpty ? "/json_rpc" : uri.path;
|
|
|
|
|
|
|
|
String uriString = "${uri.scheme}://${uri.host}:${node.port}$path";
|
|
|
|
|
2022-11-08 16:18:48 +00:00
|
|
|
final response = await testMoneroNodeConnection(
|
|
|
|
Uri.parse(uriString),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (response.cert != null) {
|
|
|
|
if (mounted) {
|
|
|
|
final shouldAllowBadCert = await showBadX509CertificateDialog(
|
|
|
|
response.cert!,
|
|
|
|
response.url!,
|
|
|
|
response.port!,
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (shouldAllowBadCert) {
|
|
|
|
final response = await testMoneroNodeConnection(
|
|
|
|
Uri.parse(uriString), true);
|
|
|
|
testPassed = response.success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
testPassed = response.success;
|
|
|
|
}
|
2022-11-02 21:03:14 +00:00
|
|
|
}
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log("$e\n$s", level: LogLevel.Warning);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Coin.bitcoin:
|
|
|
|
case Coin.litecoin:
|
|
|
|
case Coin.dogecoin:
|
|
|
|
case Coin.firo:
|
2023-03-02 20:52:17 +00:00
|
|
|
case Coin.particl:
|
2022-11-02 21:03:14 +00:00
|
|
|
case Coin.bitcoinTestNet:
|
|
|
|
case Coin.firoTestNet:
|
|
|
|
case Coin.dogecoinTestNet:
|
|
|
|
case Coin.bitcoincash:
|
|
|
|
case Coin.litecoinTestNet:
|
|
|
|
case Coin.namecoin:
|
|
|
|
case Coin.bitcoincashTestnet:
|
2023-05-30 16:31:49 +00:00
|
|
|
case Coin.eCash:
|
2022-11-02 21:03:14 +00:00
|
|
|
final client = ElectrumX(
|
|
|
|
host: node.host,
|
|
|
|
port: node.port,
|
|
|
|
useSSL: node.useSSL,
|
|
|
|
failovers: [],
|
|
|
|
prefs: ref.read(prefsChangeNotifierProvider),
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
testPassed = await client.ping();
|
|
|
|
} catch (_) {
|
|
|
|
testPassed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2023-05-30 16:38:47 +00:00
|
|
|
|
|
|
|
case Coin.ethereum:
|
|
|
|
try {
|
|
|
|
testPassed = await testEthNodeConnection(node.host);
|
|
|
|
} catch (_) {
|
|
|
|
testPassed = false;
|
|
|
|
}
|
|
|
|
break;
|
2023-06-05 20:55:41 +00:00
|
|
|
|
|
|
|
case Coin.nano:
|
|
|
|
case Coin.banano:
|
2023-07-19 14:37:54 +00:00
|
|
|
case Coin.tezos:
|
2023-08-23 17:35:04 +00:00
|
|
|
//TODO: check network/node
|
|
|
|
throw UnimplementedError();
|
2023-07-19 17:08:46 +00:00
|
|
|
case Coin.stellar:
|
2023-08-23 17:29:10 +00:00
|
|
|
case Coin.stellarTestnet:
|
2023-08-23 13:13:17 +00:00
|
|
|
try {
|
2023-08-23 14:59:00 +00:00
|
|
|
testPassed = await testStellarNodeConnection(node.host, node.port);
|
2023-08-23 16:59:13 +00:00
|
|
|
} catch (_) {
|
2023-08-23 13:13:17 +00:00
|
|
|
testPassed = false;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-02 21:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (testPassed) {
|
|
|
|
// showFloatingFlushBar(
|
|
|
|
// type: FlushBarType.success,
|
|
|
|
// message: "Server ping success",
|
|
|
|
// context: context,
|
|
|
|
// );
|
|
|
|
} else {
|
|
|
|
unawaited(
|
|
|
|
showFloatingFlushBar(
|
|
|
|
type: FlushBarType.warning,
|
|
|
|
iconAsset: Assets.svg.circleAlert,
|
|
|
|
message: "Could not connect to node",
|
|
|
|
context: context,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return testPassed;
|
|
|
|
}
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
nodeId = widget.nodeId;
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final node = ref.watch(nodeServiceChangeNotifierProvider
|
|
|
|
.select((value) => value.getPrimaryNodeFor(coin: widget.coin)));
|
|
|
|
final _node = ref.watch(nodeServiceChangeNotifierProvider
|
|
|
|
.select((value) => value.getNodeById(id: nodeId)))!;
|
|
|
|
|
|
|
|
if (node?.name == _node.name) {
|
|
|
|
_status = "Connected";
|
|
|
|
} else {
|
|
|
|
_status = "Disconnected";
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:03:14 +00:00
|
|
|
final isDesktop = Util.isDesktop;
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
return RoundedWhiteContainer(
|
|
|
|
padding: const EdgeInsets.all(0),
|
2022-11-02 21:03:14 +00:00
|
|
|
borderColor: isDesktop
|
|
|
|
? Theme.of(context).extension<StackColors>()!.background
|
|
|
|
: null,
|
|
|
|
child: ConditionalParent(
|
|
|
|
condition: !isDesktop,
|
|
|
|
builder: (child) {
|
|
|
|
return RawMaterialButton(
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
Constants.size.circularBorderRadius,
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-02 21:03:14 +00:00
|
|
|
onPressed: () {
|
|
|
|
showModalBottomSheet<void>(
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
context: context,
|
|
|
|
builder: (_) => NodeOptionsSheet(
|
|
|
|
nodeId: nodeId,
|
|
|
|
coin: widget.coin,
|
|
|
|
popBackToRoute: widget.popBackToRoute,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: child,
|
2022-08-26 08:11:35 +00:00
|
|
|
);
|
|
|
|
},
|
2022-11-02 21:03:14 +00:00
|
|
|
child: ConditionalParent(
|
|
|
|
condition: isDesktop,
|
|
|
|
builder: (child) {
|
|
|
|
return Expandable(
|
|
|
|
onExpandChanged: (state) {
|
|
|
|
setState(() {
|
|
|
|
_advancedIsExpanded = state == ExpandableState.expanded;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
header: child,
|
|
|
|
body: Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
bottom: 24,
|
|
|
|
),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
const SizedBox(
|
|
|
|
width: 66,
|
|
|
|
),
|
2023-01-30 21:34:21 +00:00
|
|
|
CustomTextButton(
|
2022-11-02 21:03:14 +00:00
|
|
|
text: "Connect",
|
|
|
|
enabled: _status == "Disconnected",
|
|
|
|
onTap: () async {
|
|
|
|
final canConnect =
|
|
|
|
await _testConnection(_node, context, ref);
|
|
|
|
if (!canConnect) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await ref
|
|
|
|
.read(nodeServiceChangeNotifierProvider)
|
|
|
|
.setPrimaryNodeFor(
|
|
|
|
coin: widget.coin,
|
|
|
|
node: _node,
|
|
|
|
shouldNotifyListeners: true,
|
|
|
|
);
|
|
|
|
|
|
|
|
await _notifyWalletsOfUpdatedNode(ref);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
width: 48,
|
|
|
|
),
|
2023-01-30 21:34:21 +00:00
|
|
|
CustomTextButton(
|
2022-11-02 21:03:14 +00:00
|
|
|
text: "Details",
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushNamed(
|
|
|
|
NodeDetailsView.routeName,
|
|
|
|
arguments: Tuple3(
|
|
|
|
widget.coin,
|
|
|
|
widget.nodeId,
|
|
|
|
widget.popBackToRoute,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-02 21:03:14 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.all(isDesktop ? 16 : 12),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
width: isDesktop ? 40 : 24,
|
|
|
|
height: isDesktop ? 40 : 24,
|
|
|
|
decoration: BoxDecoration(
|
2022-11-25 20:28:53 +00:00
|
|
|
color: _node.id.startsWith(DefaultNodes.defaultNodeIdPrefix)
|
2022-09-22 23:48:50 +00:00
|
|
|
? Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
2022-11-02 21:03:14 +00:00
|
|
|
.buttonBackSecondary
|
2022-09-22 23:48:50 +00:00
|
|
|
: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
2022-11-02 21:03:14 +00:00
|
|
|
.infoItemIcons
|
|
|
|
.withOpacity(0.2),
|
|
|
|
borderRadius: BorderRadius.circular(100),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-02 21:03:14 +00:00
|
|
|
child: Center(
|
|
|
|
child: SvgPicture.asset(
|
|
|
|
Assets.svg.node,
|
|
|
|
height: isDesktop ? 18 : 11,
|
|
|
|
width: isDesktop ? 20 : 14,
|
2022-11-25 20:28:53 +00:00
|
|
|
color:
|
|
|
|
_node.id.startsWith(DefaultNodes.defaultNodeIdPrefix)
|
|
|
|
? Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.accentColorDark
|
|
|
|
: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.infoItemIcons,
|
2022-11-02 21:03:14 +00:00
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-02 21:03:14 +00:00
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
width: 12,
|
|
|
|
),
|
|
|
|
Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
_node.name,
|
|
|
|
style: STextStyles.titleBold12(context),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 2,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
_status,
|
|
|
|
style: STextStyles.label(context),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
const Spacer(),
|
|
|
|
if (!isDesktop)
|
|
|
|
SvgPicture.asset(
|
|
|
|
Assets.svg.network,
|
|
|
|
color: _status == "Connected"
|
|
|
|
? Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.accentColorGreen
|
|
|
|
: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.buttonBackSecondary,
|
|
|
|
width: 20,
|
|
|
|
height: 20,
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-02 21:03:14 +00:00
|
|
|
if (isDesktop)
|
|
|
|
SvgPicture.asset(
|
|
|
|
_advancedIsExpanded
|
2022-11-17 16:12:19 +00:00
|
|
|
? Assets.svg.chevronUp
|
|
|
|
: Assets.svg.chevronDown,
|
2022-11-02 21:03:14 +00:00
|
|
|
width: 12,
|
|
|
|
height: 6,
|
|
|
|
color: Theme.of(context)
|
2022-09-22 23:48:50 +00:00
|
|
|
.extension<StackColors>()!
|
2022-11-02 21:03:14 +00:00
|
|
|
.textSubtitle1,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|