mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-12-23 11:59:30 +00:00
comment out unused class to avoid confusion
This commit is contained in:
parent
3bb0a647bf
commit
7dabba3d0e
1 changed files with 324 additions and 324 deletions
|
@ -1,324 +1,324 @@
|
||||||
/*
|
// /*
|
||||||
* This file is part of Stack Wallet.
|
// * This file is part of Stack Wallet.
|
||||||
*
|
// *
|
||||||
* Copyright (c) 2023 Cypher Stack
|
// * Copyright (c) 2023 Cypher Stack
|
||||||
* All Rights Reserved.
|
// * All Rights Reserved.
|
||||||
* The code is distributed under GPLv3 license, see LICENSE file for details.
|
// * The code is distributed under GPLv3 license, see LICENSE file for details.
|
||||||
* Generated by Cypher Stack on 2023-05-26
|
// * Generated by Cypher Stack on 2023-05-26
|
||||||
*
|
// *
|
||||||
*/
|
// */
|
||||||
|
//
|
||||||
import 'dart:async';
|
// import 'dart:async';
|
||||||
import 'dart:convert';
|
// import 'dart:convert';
|
||||||
import 'dart:io';
|
// import 'dart:io';
|
||||||
|
//
|
||||||
import 'package:flutter/foundation.dart';
|
// import 'package:flutter/foundation.dart';
|
||||||
import 'package:stackwallet/utilities/logger.dart';
|
// import 'package:stackwallet/utilities/logger.dart';
|
||||||
|
//
|
||||||
class ElectrumXSubscription with ChangeNotifier {
|
// class ElectrumXSubscription with ChangeNotifier {
|
||||||
dynamic _response;
|
// dynamic _response;
|
||||||
dynamic get response => _response;
|
// dynamic get response => _response;
|
||||||
set response(dynamic newData) {
|
// set response(dynamic newData) {
|
||||||
_response = newData;
|
// _response = newData;
|
||||||
notifyListeners();
|
// notifyListeners();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
class SocketTask {
|
// class SocketTask {
|
||||||
SocketTask({this.completer, this.subscription});
|
// SocketTask({this.completer, this.subscription});
|
||||||
|
//
|
||||||
final Completer<dynamic>? completer;
|
// final Completer<dynamic>? completer;
|
||||||
final ElectrumXSubscription? subscription;
|
// final ElectrumXSubscription? subscription;
|
||||||
|
//
|
||||||
bool get isSubscription => subscription != null;
|
// bool get isSubscription => subscription != null;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
class SubscribableElectrumXClient {
|
// class SubscribableElectrumXClient {
|
||||||
int _currentRequestID = 0;
|
// int _currentRequestID = 0;
|
||||||
bool _isConnected = false;
|
// bool _isConnected = false;
|
||||||
List<int> _responseData = [];
|
// List<int> _responseData = [];
|
||||||
final Map<String, SocketTask> _tasks = {};
|
// final Map<String, SocketTask> _tasks = {};
|
||||||
Timer? _aliveTimer;
|
// Timer? _aliveTimer;
|
||||||
Socket? _socket;
|
// Socket? _socket;
|
||||||
late final bool _useSSL;
|
// late final bool _useSSL;
|
||||||
late final Duration _connectionTimeout;
|
// late final Duration _connectionTimeout;
|
||||||
late final Duration _keepAlive;
|
// late final Duration _keepAlive;
|
||||||
|
//
|
||||||
bool get isConnected => _isConnected;
|
// bool get isConnected => _isConnected;
|
||||||
bool get useSSL => _useSSL;
|
// bool get useSSL => _useSSL;
|
||||||
|
//
|
||||||
void Function(bool)? onConnectionStatusChanged;
|
// void Function(bool)? onConnectionStatusChanged;
|
||||||
|
//
|
||||||
SubscribableElectrumXClient({
|
// SubscribableElectrumXClient({
|
||||||
bool useSSL = true,
|
// bool useSSL = true,
|
||||||
this.onConnectionStatusChanged,
|
// this.onConnectionStatusChanged,
|
||||||
Duration connectionTimeout = const Duration(seconds: 5),
|
// Duration connectionTimeout = const Duration(seconds: 5),
|
||||||
Duration keepAlive = const Duration(seconds: 10),
|
// Duration keepAlive = const Duration(seconds: 10),
|
||||||
}) {
|
// }) {
|
||||||
_useSSL = useSSL;
|
// _useSSL = useSSL;
|
||||||
_connectionTimeout = connectionTimeout;
|
// _connectionTimeout = connectionTimeout;
|
||||||
_keepAlive = keepAlive;
|
// _keepAlive = keepAlive;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
Future<void> connect({required String host, required int port}) async {
|
// Future<void> connect({required String host, required int port}) async {
|
||||||
try {
|
// try {
|
||||||
await _socket?.close();
|
// await _socket?.close();
|
||||||
} catch (_) {}
|
// } catch (_) {}
|
||||||
|
//
|
||||||
if (_useSSL) {
|
// if (_useSSL) {
|
||||||
_socket = await SecureSocket.connect(
|
// _socket = await SecureSocket.connect(
|
||||||
host,
|
// host,
|
||||||
port,
|
// port,
|
||||||
timeout: _connectionTimeout,
|
// timeout: _connectionTimeout,
|
||||||
onBadCertificate: (_) => true,
|
// onBadCertificate: (_) => true,
|
||||||
);
|
// );
|
||||||
} else {
|
// } else {
|
||||||
_socket = await Socket.connect(
|
// _socket = await Socket.connect(
|
||||||
host,
|
// host,
|
||||||
port,
|
// port,
|
||||||
timeout: _connectionTimeout,
|
// timeout: _connectionTimeout,
|
||||||
);
|
// );
|
||||||
}
|
// }
|
||||||
_updateConnectionStatus(true);
|
// _updateConnectionStatus(true);
|
||||||
|
//
|
||||||
_socket!.listen(
|
// _socket!.listen(
|
||||||
_dataHandler,
|
// _dataHandler,
|
||||||
onError: _errorHandler,
|
// onError: _errorHandler,
|
||||||
onDone: _doneHandler,
|
// onDone: _doneHandler,
|
||||||
cancelOnError: true,
|
// cancelOnError: true,
|
||||||
);
|
// );
|
||||||
|
//
|
||||||
_aliveTimer?.cancel();
|
// _aliveTimer?.cancel();
|
||||||
_aliveTimer = Timer.periodic(
|
// _aliveTimer = Timer.periodic(
|
||||||
_keepAlive,
|
// _keepAlive,
|
||||||
(_) async => _updateConnectionStatus(await ping()),
|
// (_) async => _updateConnectionStatus(await ping()),
|
||||||
);
|
// );
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
Future<void> disconnect() async {
|
// Future<void> disconnect() async {
|
||||||
_aliveTimer?.cancel();
|
// _aliveTimer?.cancel();
|
||||||
await _socket?.close();
|
// await _socket?.close();
|
||||||
onConnectionStatusChanged = null;
|
// onConnectionStatusChanged = null;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
String _buildJsonRequestString({
|
// String _buildJsonRequestString({
|
||||||
required String method,
|
// required String method,
|
||||||
required String id,
|
// required String id,
|
||||||
required List<dynamic> params,
|
// required List<dynamic> params,
|
||||||
}) {
|
// }) {
|
||||||
final paramString = jsonEncode(params);
|
// final paramString = jsonEncode(params);
|
||||||
return '{"jsonrpc": "2.0", "id": "$id","method": "$method","params": $paramString}\r\n';
|
// return '{"jsonrpc": "2.0", "id": "$id","method": "$method","params": $paramString}\r\n';
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void _updateConnectionStatus(bool connectionStatus) {
|
// void _updateConnectionStatus(bool connectionStatus) {
|
||||||
if (_isConnected != connectionStatus && onConnectionStatusChanged != null) {
|
// if (_isConnected != connectionStatus && onConnectionStatusChanged != null) {
|
||||||
onConnectionStatusChanged!(connectionStatus);
|
// onConnectionStatusChanged!(connectionStatus);
|
||||||
}
|
// }
|
||||||
_isConnected = connectionStatus;
|
// _isConnected = connectionStatus;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void _dataHandler(List<int> data) {
|
// void _dataHandler(List<int> data) {
|
||||||
_responseData.addAll(data);
|
// _responseData.addAll(data);
|
||||||
|
//
|
||||||
// 0x0A is newline
|
// // 0x0A is newline
|
||||||
// https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-basics.html
|
// // https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-basics.html
|
||||||
if (data.last == 0x0A) {
|
// if (data.last == 0x0A) {
|
||||||
try {
|
// try {
|
||||||
final response = jsonDecode(String.fromCharCodes(_responseData))
|
// final response = jsonDecode(String.fromCharCodes(_responseData))
|
||||||
as Map<String, dynamic>;
|
// as Map<String, dynamic>;
|
||||||
_responseHandler(response);
|
// _responseHandler(response);
|
||||||
} catch (e, s) {
|
// } catch (e, s) {
|
||||||
Logging.instance
|
// Logging.instance
|
||||||
.log("JsonRPC jsonDecode: $e\n$s", level: LogLevel.Error);
|
// .log("JsonRPC jsonDecode: $e\n$s", level: LogLevel.Error);
|
||||||
rethrow;
|
// rethrow;
|
||||||
} finally {
|
// } finally {
|
||||||
_responseData = [];
|
// _responseData = [];
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void _responseHandler(Map<String, dynamic> response) {
|
// void _responseHandler(Map<String, dynamic> response) {
|
||||||
// subscriptions will have a method in the response
|
// // subscriptions will have a method in the response
|
||||||
if (response['method'] is String) {
|
// if (response['method'] is String) {
|
||||||
_subscriptionHandler(response: response);
|
// _subscriptionHandler(response: response);
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
final id = response['id'] as String;
|
// final id = response['id'] as String;
|
||||||
final result = response['result'];
|
// final result = response['result'];
|
||||||
|
//
|
||||||
_complete(id, result);
|
// _complete(id, result);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void _subscriptionHandler({
|
// void _subscriptionHandler({
|
||||||
required Map<String, dynamic> response,
|
// required Map<String, dynamic> response,
|
||||||
}) {
|
// }) {
|
||||||
final method = response['method'];
|
// final method = response['method'];
|
||||||
switch (method) {
|
// switch (method) {
|
||||||
case "blockchain.scripthash.subscribe":
|
// case "blockchain.scripthash.subscribe":
|
||||||
final params = response["params"] as List<dynamic>;
|
// final params = response["params"] as List<dynamic>;
|
||||||
final scripthash = params.first as String;
|
// final scripthash = params.first as String;
|
||||||
final taskId = "blockchain.scripthash.subscribe:$scripthash";
|
// final taskId = "blockchain.scripthash.subscribe:$scripthash";
|
||||||
|
//
|
||||||
_tasks[taskId]?.subscription?.response = params.last;
|
// _tasks[taskId]?.subscription?.response = params.last;
|
||||||
break;
|
// break;
|
||||||
case "blockchain.headers.subscribe":
|
// case "blockchain.headers.subscribe":
|
||||||
final params = response["params"];
|
// final params = response["params"];
|
||||||
const taskId = "blockchain.headers.subscribe";
|
// const taskId = "blockchain.headers.subscribe";
|
||||||
|
//
|
||||||
_tasks[taskId]?.subscription?.response = params.first;
|
// _tasks[taskId]?.subscription?.response = params.first;
|
||||||
break;
|
// break;
|
||||||
default:
|
// default:
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void _errorHandler(Object error, StackTrace trace) {
|
// void _errorHandler(Object error, StackTrace trace) {
|
||||||
_updateConnectionStatus(false);
|
// _updateConnectionStatus(false);
|
||||||
Logging.instance.log(
|
// Logging.instance.log(
|
||||||
"SubscribableElectrumXClient called _errorHandler with: $error\n$trace",
|
// "SubscribableElectrumXClient called _errorHandler with: $error\n$trace",
|
||||||
level: LogLevel.Info);
|
// level: LogLevel.Info);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void _doneHandler() {
|
// void _doneHandler() {
|
||||||
_updateConnectionStatus(false);
|
// _updateConnectionStatus(false);
|
||||||
Logging.instance.log("SubscribableElectrumXClient called _doneHandler",
|
// Logging.instance.log("SubscribableElectrumXClient called _doneHandler",
|
||||||
level: LogLevel.Info);
|
// level: LogLevel.Info);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void _complete(String id, dynamic data) {
|
// void _complete(String id, dynamic data) {
|
||||||
if (_tasks[id] == null) {
|
// if (_tasks[id] == null) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
if (!(_tasks[id]?.completer?.isCompleted ?? false)) {
|
// if (!(_tasks[id]?.completer?.isCompleted ?? false)) {
|
||||||
_tasks[id]?.completer?.complete(data);
|
// _tasks[id]?.completer?.complete(data);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
if (!(_tasks[id]?.isSubscription ?? false)) {
|
// if (!(_tasks[id]?.isSubscription ?? false)) {
|
||||||
_tasks.remove(id);
|
// _tasks.remove(id);
|
||||||
} else {
|
// } else {
|
||||||
_tasks[id]?.subscription?.response = data;
|
// _tasks[id]?.subscription?.response = data;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void _addTask({
|
// void _addTask({
|
||||||
required String id,
|
// required String id,
|
||||||
required Completer<dynamic> completer,
|
// required Completer<dynamic> completer,
|
||||||
}) {
|
// }) {
|
||||||
_tasks[id] = SocketTask(completer: completer, subscription: null);
|
// _tasks[id] = SocketTask(completer: completer, subscription: null);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void _addSubscriptionTask({
|
// void _addSubscriptionTask({
|
||||||
required String id,
|
// required String id,
|
||||||
required ElectrumXSubscription subscription,
|
// required ElectrumXSubscription subscription,
|
||||||
}) {
|
// }) {
|
||||||
_tasks[id] = SocketTask(completer: null, subscription: subscription);
|
// _tasks[id] = SocketTask(completer: null, subscription: subscription);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
Future<dynamic> _call({
|
// Future<dynamic> _call({
|
||||||
required String method,
|
// required String method,
|
||||||
List<dynamic> params = const [],
|
// List<dynamic> params = const [],
|
||||||
}) async {
|
// }) async {
|
||||||
final completer = Completer<dynamic>();
|
// final completer = Completer<dynamic>();
|
||||||
_currentRequestID++;
|
// _currentRequestID++;
|
||||||
final id = _currentRequestID.toString();
|
// final id = _currentRequestID.toString();
|
||||||
_addTask(id: id, completer: completer);
|
// _addTask(id: id, completer: completer);
|
||||||
|
//
|
||||||
_socket?.write(
|
// _socket?.write(
|
||||||
_buildJsonRequestString(
|
// _buildJsonRequestString(
|
||||||
method: method,
|
// method: method,
|
||||||
id: id,
|
// id: id,
|
||||||
params: params,
|
// params: params,
|
||||||
),
|
// ),
|
||||||
);
|
// );
|
||||||
|
//
|
||||||
return completer.future;
|
// return completer.future;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
Future<dynamic> _callWithTimeout({
|
// Future<dynamic> _callWithTimeout({
|
||||||
required String method,
|
// required String method,
|
||||||
List<dynamic> params = const [],
|
// List<dynamic> params = const [],
|
||||||
Duration timeout = const Duration(seconds: 2),
|
// Duration timeout = const Duration(seconds: 2),
|
||||||
}) async {
|
// }) async {
|
||||||
final completer = Completer<dynamic>();
|
// final completer = Completer<dynamic>();
|
||||||
_currentRequestID++;
|
// _currentRequestID++;
|
||||||
final id = _currentRequestID.toString();
|
// final id = _currentRequestID.toString();
|
||||||
_addTask(id: id, completer: completer);
|
// _addTask(id: id, completer: completer);
|
||||||
|
//
|
||||||
_socket?.write(
|
// _socket?.write(
|
||||||
_buildJsonRequestString(
|
// _buildJsonRequestString(
|
||||||
method: method,
|
// method: method,
|
||||||
id: id,
|
// id: id,
|
||||||
params: params,
|
// params: params,
|
||||||
),
|
// ),
|
||||||
);
|
// );
|
||||||
|
//
|
||||||
Timer(timeout, () {
|
// Timer(timeout, () {
|
||||||
if (!completer.isCompleted) {
|
// if (!completer.isCompleted) {
|
||||||
completer.completeError(
|
// completer.completeError(
|
||||||
Exception("Request \"id: $id, method: $method\" timed out!"),
|
// Exception("Request \"id: $id, method: $method\" timed out!"),
|
||||||
);
|
// );
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
//
|
||||||
return completer.future;
|
// return completer.future;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
ElectrumXSubscription _subscribe({
|
// ElectrumXSubscription _subscribe({
|
||||||
required String taskId,
|
// required String taskId,
|
||||||
required String method,
|
// required String method,
|
||||||
List<dynamic> params = const [],
|
// List<dynamic> params = const [],
|
||||||
}) {
|
// }) {
|
||||||
// try {
|
// // try {
|
||||||
final subscription = ElectrumXSubscription();
|
// final subscription = ElectrumXSubscription();
|
||||||
_addSubscriptionTask(id: taskId, subscription: subscription);
|
// _addSubscriptionTask(id: taskId, subscription: subscription);
|
||||||
_currentRequestID++;
|
// _currentRequestID++;
|
||||||
_socket?.write(
|
// _socket?.write(
|
||||||
_buildJsonRequestString(
|
// _buildJsonRequestString(
|
||||||
method: method,
|
// method: method,
|
||||||
id: taskId,
|
// id: taskId,
|
||||||
params: params,
|
// params: params,
|
||||||
),
|
// ),
|
||||||
);
|
// );
|
||||||
|
//
|
||||||
return subscription;
|
// return subscription;
|
||||||
// } catch (e, s) {
|
// // } catch (e, s) {
|
||||||
// Logging.instance.log("SubscribableElectrumXClient _subscribe: $e\n$s", level: LogLevel.Error);
|
// // Logging.instance.log("SubscribableElectrumXClient _subscribe: $e\n$s", level: LogLevel.Error);
|
||||||
// return null;
|
// // return null;
|
||||||
|
// // }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /// Ping the server to ensure it is responding
|
||||||
|
// ///
|
||||||
|
// /// Returns true if ping succeeded
|
||||||
|
// Future<bool> ping() async {
|
||||||
|
// try {
|
||||||
|
// final response = (await _callWithTimeout(method: "server.ping")) as Map;
|
||||||
|
// return response.keys.contains("result") && response["result"] == null;
|
||||||
|
// } catch (_) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /// Subscribe to a scripthash to receive notifications on status changes
|
||||||
|
// ElectrumXSubscription subscribeToScripthash({required String scripthash}) {
|
||||||
|
// return _subscribe(
|
||||||
|
// taskId: 'blockchain.scripthash.subscribe:$scripthash',
|
||||||
|
// method: 'blockchain.scripthash.subscribe',
|
||||||
|
// params: [scripthash],
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /// Subscribe to block headers to receive notifications on new blocks found
|
||||||
|
// ///
|
||||||
|
// /// Returns the existing subscription if found
|
||||||
|
// ElectrumXSubscription subscribeToBlockHeaders() {
|
||||||
|
// return _tasks["blockchain.headers.subscribe"]?.subscription ??
|
||||||
|
// _subscribe(
|
||||||
|
// taskId: "blockchain.headers.subscribe",
|
||||||
|
// method: "blockchain.headers.subscribe",
|
||||||
|
// params: [],
|
||||||
|
// );
|
||||||
|
// }
|
||||||
// }
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
/// Ping the server to ensure it is responding
|
|
||||||
///
|
|
||||||
/// Returns true if ping succeeded
|
|
||||||
Future<bool> ping() async {
|
|
||||||
try {
|
|
||||||
final response = (await _callWithTimeout(method: "server.ping")) as Map;
|
|
||||||
return response.keys.contains("result") && response["result"] == null;
|
|
||||||
} catch (_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Subscribe to a scripthash to receive notifications on status changes
|
|
||||||
ElectrumXSubscription subscribeToScripthash({required String scripthash}) {
|
|
||||||
return _subscribe(
|
|
||||||
taskId: 'blockchain.scripthash.subscribe:$scripthash',
|
|
||||||
method: 'blockchain.scripthash.subscribe',
|
|
||||||
params: [scripthash],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Subscribe to block headers to receive notifications on new blocks found
|
|
||||||
///
|
|
||||||
/// Returns the existing subscription if found
|
|
||||||
ElectrumXSubscription subscribeToBlockHeaders() {
|
|
||||||
return _tasks["blockchain.headers.subscribe"]?.subscription ??
|
|
||||||
_subscribe(
|
|
||||||
taskId: "blockchain.headers.subscribe",
|
|
||||||
method: "blockchain.headers.subscribe",
|
|
||||||
params: [],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue