From e075946b6871a9488c4459d2d15077025ef60c68 Mon Sep 17 00:00:00 2001 From: Sterling Long Date: Fri, 18 Aug 2023 14:16:57 -0600 Subject: [PATCH] 2.1.4 --- CHANGELOG.md | 6 ++ lib/apis/core/core.dart | 31 ++++------ lib/apis/core/i_core.dart | 2 +- lib/apis/core/pairing/pairing.dart | 5 ++ lib/apis/core/relay_client/relay_client.dart | 62 +++++++++++++++----- lib/apis/sign_api/sign_engine.dart | 5 ++ lib/apis/utils/constants.dart | 2 +- pubspec.yaml | 2 +- test/core_api/core_test.dart | 2 + 9 files changed, 80 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc748bb5..006e9788 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 2.1.4 - UNRELEASED + +- Core defaults to `Logger.level = Level.nothing` to prevent logs from being printed by default +- Resolved errors with pairings and sessions trying to subscribe even when the relay wasn't connected +- Additional bugs resolved + ## 2.1.3 - Relay Client no longer throws errors on init diff --git a/lib/apis/core/core.dart b/lib/apis/core/core.dart index 21bfb09c..f16552ec 100644 --- a/lib/apis/core/core.dart +++ b/lib/apis/core/core.dart @@ -30,9 +30,8 @@ class Core implements ICore { @override String get version => '2'; - String _relayUrl = WalletConnectConstants.DEFAULT_RELAY_URL; @override - String get relayUrl => _relayUrl; + String relayUrl = WalletConnectConstants.DEFAULT_RELAY_URL; @override final String projectId; @@ -58,26 +57,29 @@ class Core implements ICore { @override late IEcho echo; - @override - final Logger logger = Logger( - printer: PrettyPrinter( - // methodCount: 3, - ), + Logger _logger = Logger( + level: Level.nothing, + printer: PrettyPrinter(), ); + @override + Logger get logger => _logger; @override late IStore> storage; Core({ - relayUrl = WalletConnectConstants.DEFAULT_RELAY_URL, + this.relayUrl = WalletConnectConstants.DEFAULT_RELAY_URL, required this.projectId, this.pushUrl = WalletConnectConstants.DEFAULT_PUSH_URL, bool memoryStore = false, - Level logLevel = Level.info, + Level logLevel = Level.nothing, IHttpClient httpClient = const HttpWrapper(), IWebSocketHandler? webSocketHandler, }) { - Logger.level = logLevel; + _logger = Logger( + level: logLevel, + printer: PrettyPrinter(), + ); storage = SharedPrefsStores( memoryStore: memoryStore, ); @@ -90,7 +92,6 @@ class Core implements ICore { fromJson: (dynamic value) => value as String, ), ); - _relayUrl = relayUrl; relayClient = RelayClient( core: this, messageTracker: MessageTracker( @@ -153,14 +154,6 @@ class Core implements ICore { await storage.init(); await crypto.init(); await relayClient.init(); - - // If it didn't connect, and the relayUrl is the default, try the fallback - if (!relayClient.isConnected && - _relayUrl == WalletConnectConstants.DEFAULT_RELAY_URL) { - _relayUrl = WalletConnectConstants.FALLBACK_RELAY_URL; - relayClient.connect(); - } - await expirer.init(); await pairing.init(); } diff --git a/lib/apis/core/i_core.dart b/lib/apis/core/i_core.dart index 55b3c733..36c1e130 100644 --- a/lib/apis/core/i_core.dart +++ b/lib/apis/core/i_core.dart @@ -10,7 +10,7 @@ abstract class ICore { final String protocol = 'wc'; final String version = '2'; - abstract final String relayUrl; + abstract String relayUrl; abstract final String projectId; abstract final String pushUrl; diff --git a/lib/apis/core/pairing/pairing.dart b/lib/apis/core/pairing/pairing.dart index da8bc776..c9743649 100644 --- a/lib/apis/core/pairing/pairing.dart +++ b/lib/apis/core/pairing/pairing.dart @@ -524,6 +524,11 @@ class Pairing implements IPairing { /// ---- Private Helpers ---- /// Future _resubscribeAll() async { + // If the relay is not active, stop here + if (!core.relayClient.isConnected) { + return; + } + // Resubscribe to all active pairings final List activePairings = pairings.getAll(); for (final PairingInfo pairing in activePairings) { diff --git a/lib/apis/core/relay_client/relay_client.dart b/lib/apis/core/relay_client/relay_client.dart index a738079d..0cdf221b 100644 --- a/lib/apis/core/relay_client/relay_client.dart +++ b/lib/apis/core/relay_client/relay_client.dart @@ -96,9 +96,10 @@ class RelayClient implements IRelayClient { await topicMap.init(); // Setup the json RPC server - _connectingFuture = _createJsonRPCProvider(); - await _connectingFuture; - _startHeartbeat(); + await _connect(); + // _connectingFuture = _createJsonRPCProvider(); + // await _connectingFuture; + // _startHeartbeat(); _initialized = true; } @@ -146,7 +147,6 @@ class RelayClient implements IRelayClient { _checkInitialized(); String id = topicMap.get(topic) ?? ''; - // print('Unsub from id: $id'); try { await _sendJsonRpcRequest( @@ -173,40 +173,72 @@ class RelayClient implements IRelayClient { Future connect({String? relayUrl}) async { _checkInitialized(); + core.logger.i('RelayClient: Connecting to relay'); + + await _connect(relayUrl: relayUrl); + } + + @override + Future disconnect() async { + _checkInitialized(); + + core.logger.i('RelayClient: Disconnecting from relay'); + + await _disconnect(); + } + + /// PRIVATE FUNCTIONS /// + + Future _connect({String? relayUrl}) async { + core.logger.v('RelayClient Internal: Connecting to relay'); if (isConnected) { return; } - // print('connecting to relay server'); + // If we have tried connecting to the relay before, disconnect if (_active) { - await disconnect(); + await _disconnect(); } + + // Connect and track the connection progress, then start the heartbeat _connectingFuture = _createJsonRPCProvider(); await _connectingFuture; if (_heartbeatTimer == null) { _startHeartbeat(); } - } - @override - Future disconnect() async { - _checkInitialized(); - - core.logger.v('RelayClient: Disconnecting from relay'); + // If it didn't connect, and the relayUrl is the default, + // recursively try the fallback + core.relayUrl = relayUrl ?? core.relayUrl; + if (!isConnected && + core.relayUrl == WalletConnectConstants.DEFAULT_RELAY_URL) { + core.relayUrl = WalletConnectConstants.FALLBACK_RELAY_URL; + await _connect(); + + // If we still didn't connect, reset the relayUrl to the default + if (!isConnected) { + core.relayUrl = WalletConnectConstants.DEFAULT_RELAY_URL; + } + } + } + Future _disconnect() async { + core.logger.v('RelayClient Internal: Disconnecting from relay'); _active = false; + final bool shouldBroadcastDisonnect = isConnected; + await jsonRPC?.close(); jsonRPC = null; await socketHandler.close(); _heartbeatTimer?.cancel(); _heartbeatTimer = null; - onRelayClientDisconnect.broadcast(); + if (shouldBroadcastDisonnect) { + onRelayClientDisconnect.broadcast(); + } } - /// PRIVATE FUNCTIONS /// - Future _createJsonRPCProvider() async { _connecting = true; _active = true; diff --git a/lib/apis/sign_api/sign_engine.dart b/lib/apis/sign_api/sign_engine.dart index 49e5e92f..93e54f44 100644 --- a/lib/apis/sign_api/sign_engine.dart +++ b/lib/apis/sign_api/sign_engine.dart @@ -707,6 +707,11 @@ class SignEngine implements ISignEngine { /// ---- PRIVATE HELPERS ---- //// Future _resubscribeAll() async { + // If the relay is not connected, stop here + if (!core.relayClient.isConnected) { + return; + } + // Subscribe to all the sessions for (final SessionData session in sessions.getAll()) { // print('Session: subscribing to ${session.topic}'); diff --git a/lib/apis/utils/constants.dart b/lib/apis/utils/constants.dart index f07f2ac6..6f737234 100644 --- a/lib/apis/utils/constants.dart +++ b/lib/apis/utils/constants.dart @@ -1,5 +1,5 @@ class WalletConnectConstants { - static const SDK_VERSION = '2.1.3'; + static const SDK_VERSION = '2.1.4'; static const CORE_PROTOCOL = 'wc'; static const CORE_VERSION = 2; diff --git a/pubspec.yaml b/pubspec.yaml index 23c55b00..06507bf8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: walletconnect_flutter_v2 description: WalletConnect v2 client made in dart for flutter. -version: 2.1.3 +version: 2.1.4 repository: https://github.com/WalletConnect/WalletConnectFlutterV2 environment: diff --git a/test/core_api/core_test.dart b/test/core_api/core_test.dart index 8ce04837..8a910e30 100644 --- a/test/core_api/core_test.dart +++ b/test/core_api/core_test.dart @@ -35,6 +35,7 @@ void main() { await core.start(); expect(errorCount, 2); + expect(core.relayUrl, WalletConnectConstants.DEFAULT_RELAY_URL); verifyInOrder([ mockWebSocketHandler.setup( @@ -90,6 +91,7 @@ void main() { ).called(1); verify(mockWebSocketHandler.connect()).called(1); expect(errorCount, 1); + expect(core.relayUrl, testRelayUrl); }); }); }