2021-12-24 12:52:08 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:cw_core/keyable.dart';
|
|
|
|
import 'package:flutter/foundation.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;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
part 'node.g.dart';
|
|
|
|
|
|
|
|
Uri createUriFromElectrumAddress(String address) =>
|
|
|
|
Uri.tryParse('tcp://$address');
|
|
|
|
|
|
|
|
@HiveType(typeId: Node.typeId)
|
|
|
|
class Node extends HiveObject with Keyable {
|
|
|
|
Node(
|
|
|
|
{@required String uri,
|
|
|
|
@required WalletType type,
|
|
|
|
this.login,
|
|
|
|
this.password,
|
|
|
|
this.useSSL}) {
|
|
|
|
uriRaw = uri;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node.fromMap(Map map)
|
|
|
|
: uriRaw = map['uri'] as String ?? '',
|
|
|
|
login = map['login'] as String,
|
|
|
|
password = map['password'] as String,
|
|
|
|
typeRaw = map['typeRaw'] as int,
|
|
|
|
useSSL = map['useSSL'] as bool;
|
|
|
|
|
|
|
|
static const typeId = 1;
|
|
|
|
static const boxName = 'Nodes';
|
|
|
|
|
|
|
|
@HiveField(0)
|
|
|
|
String uriRaw;
|
|
|
|
|
|
|
|
@HiveField(1)
|
|
|
|
String login;
|
|
|
|
|
|
|
|
@HiveField(2)
|
|
|
|
String password;
|
|
|
|
|
|
|
|
@HiveField(3)
|
|
|
|
int typeRaw;
|
|
|
|
|
|
|
|
@HiveField(4)
|
|
|
|
bool useSSL;
|
|
|
|
|
|
|
|
bool get isSSL => useSSL ?? false;
|
|
|
|
|
|
|
|
Uri get uri {
|
|
|
|
switch (type) {
|
|
|
|
case WalletType.monero:
|
|
|
|
return Uri.http(uriRaw, '');
|
|
|
|
case WalletType.bitcoin:
|
|
|
|
return createUriFromElectrumAddress(uriRaw);
|
|
|
|
case WalletType.litecoin:
|
|
|
|
return createUriFromElectrumAddress(uriRaw);
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 requestMoneroNode();
|
|
|
|
case WalletType.bitcoin:
|
|
|
|
return requestElectrumServer();
|
|
|
|
case WalletType.litecoin:
|
|
|
|
return requestElectrumServer();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> requestMoneroNode() async {
|
2022-02-03 10:16:24 +00:00
|
|
|
|
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',
|
|
|
|
'method': 'get_info'
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
final authenticatingClient = HttpClient();
|
|
|
|
|
|
|
|
authenticatingClient.addCredentials(
|
|
|
|
rpcUri,
|
|
|
|
realm,
|
|
|
|
HttpClientDigestCredentials(login ?? '', password ?? ''),
|
|
|
|
);
|
|
|
|
|
|
|
|
final http.Client client = ioc.IOClient(authenticatingClient);
|
|
|
|
|
|
|
|
final response = await client.post(
|
|
|
|
rpcUri,
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
body: json.encode(body),
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
Future<bool> requestElectrumServer() async {
|
|
|
|
try {
|
|
|
|
await SecureSocket.connect(uri.host, uri.port,
|
|
|
|
timeout: Duration(seconds: 5), onBadCertificate: (_) => true);
|
|
|
|
return true;
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|