Skip to content

Commit

Permalink
Added bundleId/packageName into websocket handshake connection
Browse files Browse the repository at this point in the history
  • Loading branch information
quetool committed Oct 25, 2023
1 parent d299d9b commit dda9dc9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 20 deletions.
8 changes: 7 additions & 1 deletion example/dapp/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
PODS:
- Flutter (1.0.0)
- package_info_plus (0.4.5):
- Flutter
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS

DEPENDENCIES:
- Flutter (from `Flutter`)
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)

EXTERNAL SOURCES:
Flutter:
:path: Flutter
package_info_plus:
:path: ".symlinks/plugins/package_info_plus/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
package_info_plus: 115f4ad11e0698c8c1c5d8a689390df880f47e85
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126

PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3

COCOAPODS: 1.12.1
COCOAPODS: 1.13.0
2 changes: 2 additions & 0 deletions example/dapp/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import FlutterMacOS
import Foundation

import package_info_plus
import shared_preferences_foundation

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}
6 changes: 6 additions & 0 deletions example/wallet/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ PODS:
- nanopb/encode (= 2.30909.0)
- nanopb/decode (2.30909.0)
- nanopb/encode (2.30909.0)
- package_info_plus (0.4.5):
- Flutter
- PromisesObjC (2.2.0)
- shared_preferences_foundation (0.0.1):
- Flutter
Expand All @@ -64,6 +66,7 @@ PODS:
DEPENDENCIES:
- Flutter (from `Flutter`)
- mobile_scanner (from `.symlinks/plugins/mobile_scanner/ios`)
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)

SPEC REPOS:
Expand All @@ -86,6 +89,8 @@ EXTERNAL SOURCES:
:path: Flutter
mobile_scanner:
:path: ".symlinks/plugins/mobile_scanner/ios"
package_info_plus:
:path: ".symlinks/plugins/package_info_plus/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

Expand All @@ -103,6 +108,7 @@ SPEC CHECKSUMS:
MLKitVision: 8baa5f46ee3352614169b85250574fde38c36f49
mobile_scanner: 47056db0c04027ea5f41a716385542da28574662
nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431
package_info_plus: 115f4ad11e0698c8c1c5d8a689390df880f47e85
PromisesObjC: 09985d6d70fbe7878040aa746d78236e6946d2ef
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126

Expand Down
2 changes: 2 additions & 0 deletions example/wallet/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import FlutterMacOS
import Foundation

import mobile_scanner
import package_info_plus
import shared_preferences_foundation

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin"))
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}
16 changes: 10 additions & 6 deletions lib/apis/core/relay_client/relay_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class RelayClient implements IRelayClient {
@override
bool get isConnected => jsonRPC != null && !jsonRPC!.isClosed;

bool get _relayIsClosed => jsonRPC != null && jsonRPC!.isClosed;

bool _initialized = false;
bool _active = true;
bool _connecting = false;
Expand Down Expand Up @@ -237,13 +239,14 @@ class RelayClient implements IRelayClient {
var auth = await core.crypto.signJWT(core.relayUrl);
core.logger.t('Signed JWT: $auth');
try {
final String url = WalletConnectUtils.formatRelayRpcUrl(
final url = WalletConnectUtils.formatRelayRpcUrl(
protocol: WalletConnectConstants.CORE_PROTOCOL,
version: WalletConnectConstants.CORE_VERSION,
relayUrl: core.relayUrl,
sdkVersion: WalletConnectConstants.SDK_VERSION,
auth: auth,
projectId: core.projectId,
packageName: (await WalletConnectUtils.getPackageName()),
);

if (jsonRPC != null) {
Expand Down Expand Up @@ -324,6 +327,7 @@ class RelayClient implements IRelayClient {
code == 4008 ||
code == 4010 ||
code == 1002 ||
code == 10002 ||
code == 1005) {
await connect();
} else {
Expand Down Expand Up @@ -357,9 +361,8 @@ class RelayClient implements IRelayClient {

void _heartbeatSubscription(EventArgs? args) async {
core.logger.i('RelayClient heartbeat received');
if (jsonRPC != null && jsonRPC!.isClosed) {
core.logger.t('RelayClient, WebSocket closed, reconnecting');
await connect();
if (_relayIsClosed) {
await _handleRelayClose(10002, null);
}
}

Expand Down Expand Up @@ -400,7 +403,9 @@ class RelayClient implements IRelayClient {
return params.hashCode;
}

void _handleUnsubscribe(Parameters params) {}
void _handleUnsubscribe(Parameters params) {
core.logger.i('[$runtimeType] _handleUnsubscribe $params');
}

/// MESSAGE HANDLING
Expand Down Expand Up @@ -448,7 +453,6 @@ class RelayClient implements IRelayClient {
JsonRpcUtils.payloadId(entropy: 6),
);
} catch (e) {
// print('onSubscribe error: $e, stack: $stacktrace');
core.logger.w('RelayClient, onSubscribe error. Topic: $topic, Error: $e');
onRelayClientError.broadcast(ErrorEvent(e));
}
Expand Down
25 changes: 12 additions & 13 deletions lib/apis/utils/walletconnect_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:package_info_plus/package_info_plus.dart';
import 'package:universal_io/io.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:walletconnect_flutter_v2/apis/core/relay_client/relay_client_models.dart';
Expand Down Expand Up @@ -34,6 +35,11 @@ class WalletConnectUtils {
.join('-');
}

static Future<String> getPackageName() async {
final packageInfo = await PackageInfo.fromPlatform();
return packageInfo.packageName;
}

static String getId() {
if (Platform.isAndroid) {
return 'android';
Expand Down Expand Up @@ -74,19 +80,17 @@ class WalletConnectUtils {
required String sdkVersion,
required String auth,
String? projectId,
String? packageName,
}) {
final Uri uri = Uri.parse(relayUrl);
final Map<String, String> queryParams = Uri.splitQueryString(uri.query);
String ua = formatUA(
protocol,
version,
sdkVersion,
);
String ua = formatUA(protocol, version, sdkVersion);

final Map<String, String> relayParams = {
'auth': auth,
if (projectId != null && projectId.isNotEmpty) 'projectId': projectId,
if ((projectId ?? '').isNotEmpty) 'projectId': projectId!,
'ua': ua,
if ((packageName ?? '').isNotEmpty) 'origin': packageName!,
};
queryParams.addAll(relayParams);
return uri.replace(queryParameters: queryParams).toString();
Expand All @@ -106,13 +110,8 @@ class WalletConnectUtils {
}
List<String> methods = (uri.queryParameters['methods'] ?? '')
// Replace all the square brackets with empty string, split by comma
.replaceAll(
RegExp(r'[\[\]"]+'),
'',
)
.split(
',',
);
.replaceAll(RegExp(r'[\[\]"]+'), '')
.split(',');
if (methods.length == 1 && methods[0].isEmpty) {
methods = [];
}
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies:
web3dart: ^2.5.1
logger: ^2.0.2+1
freezed_annotation: ^2.2.0
package_info_plus: ^4.2.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit dda9dc9

Please sign in to comment.