cake_wallet/cw_core/lib/node.dart

197 lines
4.8 KiB
Dart
Raw Normal View History

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/wallet_type.dart';
2022-02-03 10:16:24 +00:00
import 'package:http/io_client.dart' as ioc;
part 'node.g.dart';
Uri createUriFromElectrumAddress(String address) =>
2022-10-12 17:09:57 +00:00
Uri.tryParse('tcp://$address')!;
@HiveType(typeId: Node.typeId)
class Node extends HiveObject with Keyable {
Node(
2022-10-12 17:09:57 +00:00
{this.login,
this.password,
2022-10-12 17:09:57 +00:00
this.useSSL,
this.trusted = false,
this.socksProxyAddress,
2022-10-12 17:09:57 +00:00
String? uri,
WalletType? type,}) {
if (uri != null) {
uriRaw = uri;
}
if (type != null) {
this.type = type;
}
}
2022-10-12 17:09:57 +00:00
Node.fromMap(Map<String, Object?> map)
: uriRaw = map['uri'] as String? ?? '',
2022-10-19 22:23:53 +00:00
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 = 1;
static const boxName = 'Nodes';
@HiveField(0, defaultValue: '')
2022-10-12 17:09:57 +00:00
late String uriRaw;
@HiveField(1)
2022-10-12 17:09:57 +00:00
String? login;
@HiveField(2)
2022-10-12 17:09:57 +00:00
String? password;
@HiveField(3, defaultValue: 0)
2022-10-12 17:09:57 +00:00
late int typeRaw;
@HiveField(4)
2022-10-12 17:09:57 +00:00
bool? useSSL;
2022-12-12 19:34:45 +00:00
@HiveField(5, defaultValue: false)
bool trusted;
2022-10-04 12:54:39 +00:00
@HiveField(6)
String? socksProxyAddress;
bool get isSSL => useSSL ?? false;
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);
2022-03-30 15:57:04 +00:00
case WalletType.haven:
return Uri.http(uriRaw, '');
default:
2022-10-12 17:09:57 +00:00
throw Exception('Unexpected type ${type.toString()} for Node uri');
}
}
@override
bool operator ==(other) =>
other is Node &&
(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
dynamic get keyIndex {
_keyIndex ??= key;
return _keyIndex;
}
WalletType get type => deserializeFromInt(typeRaw);
set type(WalletType type) => typeRaw = serializeToInt(type);
dynamic _keyIndex;
Future<bool> requestNode() async {
try {
switch (type) {
case WalletType.monero:
return useSocksProxy ? requestNodeWithProxy(socksProxyAddress ?? '') : requestMoneroNode();
case WalletType.bitcoin:
return requestElectrumServer();
case WalletType.litecoin:
return requestElectrumServer();
2022-06-08 08:17:03 +00:00
case WalletType.haven:
return requestMoneroNode();
default:
return false;
}
} catch (_) {
return false;
}
}
Future<bool> requestMoneroNode() async {
2022-02-03 11:46:45 +00:00
final path = '/json_rpc';
final rpcUri = isSSL ? Uri.https(uri.authority, path) : Uri.http(uri.authority, path);
final realm = 'monero-rpc';
final body = {
'jsonrpc': '2.0',
'id': '0',
2022-02-03 11:46:45 +00:00
'method': 'get_info'
};
try {
final authenticatingClient = HttpClient();
2022-02-03 11:46:45 +00:00
authenticatingClient.addCredentials(
rpcUri,
realm,
2022-02-03 11:46:45 +00:00
HttpClientDigestCredentials(login ?? '', password ?? ''),
);
2022-02-03 11:46:45 +00:00
final http.Client client = ioc.IOClient(authenticatingClient);
2022-02-03 11:46:45 +00:00
final response = await client.post(
rpcUri,
headers: {'Content-Type': 'application/json'},
body: json.encode(body),
);
2022-02-03 11:46:45 +00:00
client.close();
final resBody = json.decode(response.body) as Map<String, dynamic>;
return !(resBody['result']['offline'] as bool);
} catch (_) {
return false;
}
2022-02-02 14:36:18 +00:00
}
Future<bool> requestNodeWithProxy(String proxy) async {
if (proxy.isEmpty || !proxy.contains(':')) {
return false;
}
final proxyAddress = proxy.split(':')[0];
final proxyPort = int.parse(proxy.split(':')[1]);
try {
final socket = await Socket.connect(proxyAddress, proxyPort, timeout: Duration(seconds: 5));
socket.destroy();
return true;
} catch (_) {
return false;
}
}
Future<bool> requestElectrumServer() async {
try {
await SecureSocket.connect(uri.host, uri.port,
timeout: Duration(seconds: 5), onBadCertificate: (_) => true);
return true;
} catch (_) {
return false;
}
}
}