Skip to content

Commit

Permalink
2.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Sterling Long committed Aug 18, 2023
1 parent 3b27b06 commit e075946
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 12 additions & 19 deletions lib/apis/core/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Map<String, dynamic>> 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,
);
Expand All @@ -90,7 +92,6 @@ class Core implements ICore {
fromJson: (dynamic value) => value as String,
),
);
_relayUrl = relayUrl;
relayClient = RelayClient(
core: this,
messageTracker: MessageTracker(
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/apis/core/i_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions lib/apis/core/pairing/pairing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,11 @@ class Pairing implements IPairing {
/// ---- Private Helpers ---- ///
Future<void> _resubscribeAll() async {
// If the relay is not active, stop here
if (!core.relayClient.isConnected) {
return;
}

// Resubscribe to all active pairings
final List<PairingInfo> activePairings = pairings.getAll();
for (final PairingInfo pairing in activePairings) {
Expand Down
62 changes: 47 additions & 15 deletions lib/apis/core/relay_client/relay_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -146,7 +147,6 @@ class RelayClient implements IRelayClient {
_checkInitialized();

String id = topicMap.get(topic) ?? '';
// print('Unsub from id: $id');

try {
await _sendJsonRpcRequest(
Expand All @@ -173,40 +173,72 @@ class RelayClient implements IRelayClient {
Future<void> connect({String? relayUrl}) async {
_checkInitialized();

core.logger.i('RelayClient: Connecting to relay');

await _connect(relayUrl: relayUrl);
}

@override
Future<void> disconnect() async {
_checkInitialized();

core.logger.i('RelayClient: Disconnecting from relay');

await _disconnect();
}

/// PRIVATE FUNCTIONS ///
Future<void> _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<void> 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<void> _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<void> _createJsonRPCProvider() async {
_connecting = true;
_active = true;
Expand Down
5 changes: 5 additions & 0 deletions lib/apis/sign_api/sign_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,11 @@ class SignEngine implements ISignEngine {
/// ---- PRIVATE HELPERS ---- ////
Future<void> _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}');
Expand Down
2 changes: 1 addition & 1 deletion lib/apis/utils/constants.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 2 additions & 0 deletions test/core_api/core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void main() {

await core.start();
expect(errorCount, 2);
expect(core.relayUrl, WalletConnectConstants.DEFAULT_RELAY_URL);

verifyInOrder([
mockWebSocketHandler.setup(
Expand Down Expand Up @@ -90,6 +91,7 @@ void main() {
).called(1);
verify(mockWebSocketHandler.connect()).called(1);
expect(errorCount, 1);
expect(core.relayUrl, testRelayUrl);
});
});
}

0 comments on commit e075946

Please sign in to comment.