cake_wallet/lib/src/domain/common/node.dart

85 lines
2.1 KiB
Dart
Raw Normal View History

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-07-06 20:09:03 +00:00
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
2020-01-04 19:31:52 +00:00
import 'package:cake_wallet/src/domain/common/digest_request.dart';
part 'node.g.dart';
2020-05-26 15:27:10 +00:00
@HiveType(typeId: 1)
2020-01-04 19:31:52 +00:00
class Node extends HiveObject {
2020-08-27 16:54:34 +00:00
Node(
{@required this.uri,
@required WalletType type,
this.login,
this.password}) {
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,
typeRaw = map['typeRaw'] as int;
2020-01-08 12:26:34 +00:00
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;
WalletType get type => deserializeFromInt(typeRaw);
set type(WalletType type) => typeRaw = serializeToInt(type);
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-01-08 12:26:34 +00:00
Map<String, dynamic> resBody;
2020-01-04 19:31:52 +00:00
if (login != null && password != null) {
final digestRequest = DigestRequest();
2020-01-08 12:26:34 +00:00
final response = await digestRequest.request(
2020-01-04 19:31:52 +00:00
uri: uri, login: login, password: password);
2020-01-08 12:26:34 +00:00
resBody = response.data as Map<String, dynamic>;
2020-01-04 19:31:52 +00:00
} else {
final url = Uri.http(uri, '/json_rpc');
2020-01-08 12:26:34 +00:00
final headers = {'Content-type': 'application/json'};
final body =
2020-07-06 20:09:03 +00:00
json.encode({'jsonrpc': '2.0', 'id': '0', 'method': 'get_info'});
2020-01-08 12:26:34 +00:00
final response =
2020-01-04 19:31:52 +00:00
await http.post(url.toString(), headers: headers, body: body);
2020-01-08 12:26:34 +00:00
resBody = json.decode(response.body) as Map<String, dynamic>;
2020-01-04 19:31:52 +00:00
}
2020-07-06 20:09:03 +00:00
return !(resBody['result']['offline'] as bool);
}
Future<bool> requestBitcoinElectrumServer() async {
// FIXME: IMPLEMENT ME
return true;
2020-01-04 19:31:52 +00:00
}
}