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-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(
|
|
|
|
{@required this.uri,
|
|
|
|
@required WalletType type,
|
|
|
|
this.login,
|
2020-11-30 17:05:49 +00:00
|
|
|
this.password,
|
|
|
|
this.useSSL}) {
|
2020-07-06 20:09:03 +00:00
|
|
|
this.type = type;
|
|
|
|
}
|
2020-01-08 12:26:34 +00:00
|
|
|
|
|
|
|
Node.fromMap(Map map)
|
2020-08-27 16:54:34 +00:00
|
|
|
: uri = 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)
|
|
|
|
String uri;
|
|
|
|
|
|
|
|
@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;
|
|
|
|
|
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:
|
|
|
|
return requestBitcoinElectrumServer();
|
|
|
|
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(
|
|
|
|
uri: uri, login: login, password: password);
|
|
|
|
resBody = response.data as Map<String, dynamic>;
|
|
|
|
} else {
|
|
|
|
final url = Uri.http(uri, '/json_rpc');
|
|
|
|
final headers = {'Content-type': 'application/json'};
|
|
|
|
final body =
|
|
|
|
json.encode({'jsonrpc': '2.0', 'id': '0', 'method': 'get_info'});
|
|
|
|
final response =
|
|
|
|
await http.post(url.toString(), headers: headers, body: body);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> requestBitcoinElectrumServer() async {
|
|
|
|
// FIXME: IMPLEMENT ME
|
|
|
|
return true;
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
}
|