2021-05-10 16:00:20 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2020-09-07 15:13:39 +00:00
|
|
|
import 'package:cake_wallet/utils/mobx.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:hive/hive.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/wallet_type.dart';
|
|
|
|
import 'package:cake_wallet/entities/digest_request.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
part 'node.g.dart';
|
|
|
|
|
2021-05-10 16:00:20 +00:00
|
|
|
Uri createUriFromElectrumAddress(String address) =>
|
2021-05-11 16:57:12 +00:00
|
|
|
Uri.tryParse('tcp://$address');
|
2021-05-10 16:00:20 +00:00
|
|
|
|
2021-01-15 17:41:30 +00:00
|
|
|
@HiveType(typeId: Node.typeId)
|
2020-09-07 15:13:39 +00:00
|
|
|
class Node extends HiveObject with Keyable {
|
2020-08-27 16:54:34 +00:00
|
|
|
Node(
|
2021-05-10 16:00:20 +00:00
|
|
|
{@required String uri,
|
2020-08-27 16:54:34 +00:00
|
|
|
@required WalletType type,
|
|
|
|
this.login,
|
2020-11-30 17:05:49 +00:00
|
|
|
this.password,
|
|
|
|
this.useSSL}) {
|
2021-05-10 16:00:20 +00:00
|
|
|
uriRaw = uri;
|
2020-07-06 20:09:03 +00:00
|
|
|
this.type = type;
|
|
|
|
}
|
2020-01-08 12:26:34 +00:00
|
|
|
|
|
|
|
Node.fromMap(Map map)
|
2021-05-10 16:00:20 +00:00
|
|
|
: uriRaw = map['uri'] as String ?? '',
|
2020-01-08 12:26:34 +00:00
|
|
|
login = map['login'] as String,
|
2020-07-06 20:09:03 +00:00
|
|
|
password = map['password'] as String,
|
2020-11-30 17:05:49 +00:00
|
|
|
typeRaw = map['typeRaw'] as int,
|
|
|
|
useSSL = map['useSSL'] as bool;
|
2020-01-08 12:26:34 +00:00
|
|
|
|
2021-01-15 17:41:30 +00:00
|
|
|
static const typeId = 1;
|
2020-01-04 19:31:52 +00:00
|
|
|
static const boxName = 'Nodes';
|
|
|
|
|
|
|
|
@HiveField(0)
|
2021-05-10 16:00:20 +00:00
|
|
|
String uriRaw;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
@HiveField(1)
|
|
|
|
String login;
|
|
|
|
|
|
|
|
@HiveField(2)
|
|
|
|
String password;
|
|
|
|
|
2020-07-06 20:09:03 +00:00
|
|
|
@HiveField(3)
|
|
|
|
int typeRaw;
|
|
|
|
|
2020-11-30 17:05:49 +00:00
|
|
|
@HiveField(4)
|
|
|
|
bool useSSL;
|
|
|
|
|
2020-12-03 10:07:26 +00:00
|
|
|
bool get isSSL => useSSL ?? false;
|
|
|
|
|
2021-05-10 16:00:20 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 19:17:31 +00:00
|
|
|
@override
|
2020-09-30 18:23:15 +00:00
|
|
|
dynamic get keyIndex {
|
|
|
|
_keyIndex ??= key;
|
|
|
|
return _keyIndex;
|
|
|
|
}
|
2020-09-26 19:17:31 +00:00
|
|
|
|
2020-07-06 20:09:03 +00:00
|
|
|
WalletType get type => deserializeFromInt(typeRaw);
|
|
|
|
|
|
|
|
set type(WalletType type) => typeRaw = serializeToInt(type);
|
|
|
|
|
2020-09-30 18:23:15 +00:00
|
|
|
dynamic _keyIndex;
|
|
|
|
|
2020-07-06 20:09:03 +00:00
|
|
|
Future<bool> requestNode() async {
|
|
|
|
try {
|
|
|
|
switch (type) {
|
|
|
|
case WalletType.monero:
|
|
|
|
return requestMoneroNode();
|
|
|
|
case WalletType.bitcoin:
|
2021-05-10 16:00:20 +00:00
|
|
|
return requestElectrumServer();
|
|
|
|
case WalletType.litecoin:
|
|
|
|
return requestElectrumServer();
|
2020-07-06 20:09:03 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> requestMoneroNode() async {
|
2020-09-28 15:47:43 +00:00
|
|
|
try {
|
|
|
|
Map<String, dynamic> resBody;
|
|
|
|
|
|
|
|
if (login != null && password != null) {
|
|
|
|
final digestRequest = DigestRequest();
|
|
|
|
final response = await digestRequest.request(
|
2021-05-10 16:00:20 +00:00
|
|
|
uri: uri.toString(), login: login, password: password);
|
2020-09-28 15:47:43 +00:00
|
|
|
resBody = response.data as Map<String, dynamic>;
|
|
|
|
} else {
|
2021-05-11 16:57:12 +00:00
|
|
|
final rpcUri = Uri.http(uri.authority, '/json_rpc');
|
2020-09-28 15:47:43 +00:00
|
|
|
final headers = {'Content-type': 'application/json'};
|
|
|
|
final body =
|
|
|
|
json.encode({'jsonrpc': '2.0', 'id': '0', 'method': 'get_info'});
|
|
|
|
final response =
|
2021-05-10 16:00:20 +00:00
|
|
|
await http.post(rpcUri.toString(), headers: headers, body: body);
|
2020-09-28 15:47:43 +00:00
|
|
|
resBody = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2020-09-28 15:47:43 +00:00
|
|
|
return !(resBody['result']['offline'] as bool);
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-06 20:09:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 16:00:20 +00:00
|
|
|
Future<bool> requestElectrumServer() async {
|
|
|
|
try {
|
2021-05-11 16:57:12 +00:00
|
|
|
await SecureSocket.connect(uri.host, uri.port,
|
|
|
|
timeout: Duration(seconds: 5), onBadCertificate: (_) => true);
|
|
|
|
return true;
|
2021-05-10 16:00:20 +00:00
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
}
|