Skip to content

Commit

Permalink
Remove fallback to .org if .com fails and use .org by default
Browse files Browse the repository at this point in the history
  • Loading branch information
quetool committed Jul 8, 2024
1 parent 3955222 commit dc13450
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 171 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.3.0-beta03
## 2.3.0-beta04

- One-Click Auth support
- Bug fixes

## 2.2.3

Expand Down
65 changes: 49 additions & 16 deletions example/dapp/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ class _MyHomePageState extends State<MyHomePage> {
}

Future<void> initialize() async {
_web3App = await Web3App.createInstance(
projectId: DartDefines.projectId,
logLevel: LogLevel.error,
_web3App = Web3App(
core: Core(
projectId: DartDefines.projectId,
),
metadata: const PairingMetadata(
name: 'Sample dApp Flutter',
description: 'WalletConnect\'s sample dapp with Flutter',
Expand All @@ -75,19 +76,12 @@ class _MyHomePageState extends State<MyHomePage> {
),
);

// Loop through all the chain data
for (final ChainMetadata chain in ChainData.allChains) {
// Loop through the events for that chain
for (final event in getChainEvents(chain.type)) {
_web3App!.registerEventHandler(
chainId: chain.chainId,
event: event,
);
}
}
_web3App!.core.addLogListener(_logListener);

// Register event handlers
_web3App!.core.addLogListener(_logListener);
_web3App!.core.relayClient.onRelayClientError.subscribe(
_relayClientError,
);
_web3App!.core.relayClient.onRelayClientConnect.subscribe(_setState);
_web3App!.core.relayClient.onRelayClientDisconnect.subscribe(_setState);
_web3App!.core.relayClient.onRelayClientMessage.subscribe(
Expand All @@ -100,6 +94,19 @@ class _MyHomePageState extends State<MyHomePage> {
_web3App!.onSessionConnect.subscribe(_onSessionConnect);
_web3App!.onSessionAuthResponse.subscribe(_onSessionAuthResponse);

await _web3App!.init();

// Loop through all the chain data
for (final ChainMetadata chain in ChainData.allChains) {
// Loop through the events for that chain
for (final event in getChainEvents(chain.type)) {
_web3App!.registerEventHandler(
chainId: chain.chainId,
event: event,
);
}
}

setState(() {
_pageDatas = [
PageData(
Expand Down Expand Up @@ -138,10 +145,34 @@ class _MyHomePageState extends State<MyHomePage> {

void _setState(dynamic args) => setState(() {});

void _relayClientError(ErrorEvent? event) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text(event?.error.toString() ?? 'Relay Client error'),
actions: [
TextButton(
onPressed: () {
_web3App!.core.relayClient.connect();
Navigator.of(context).pop();
},
child: const Text('RETRY'),
),
],
);
},
);
}

@override
void dispose() {
// Unregister event handlers
_web3App!.core.removeLogListener(_logListener);

_web3App!.core.relayClient.onRelayClientError.unsubscribe(
_relayClientError,
);
_web3App!.core.relayClient.onRelayClientConnect.unsubscribe(_setState);
_web3App!.core.relayClient.onRelayClientDisconnect.unsubscribe(_setState);
_web3App!.core.relayClient.onRelayClientMessage.unsubscribe(
Expand All @@ -158,9 +189,11 @@ class _MyHomePageState extends State<MyHomePage> {
}

void _logListener(LogEvent event) {
debugPrint('[Logger] ${event.level.name}: ${event.message}');
if (event.level == Level.error) {
if (event.level == Level.debug) {
// TODO send to mixpanel
log('[Mixpanel] ${event.message}');
} else {
debugPrint('[Logger] ${event.level.name}: ${event.message}');
}
}

Expand Down
48 changes: 26 additions & 22 deletions example/wallet/lib/dependencies/web3wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class Web3WalletService extends IWeb3WalletService {
@override
Future<void> create() async {
// Create the web3wallet
_web3Wallet = await Web3Wallet.createInstance(
projectId: DartDefines.projectId,
logLevel: LogLevel.error,
_web3Wallet = Web3Wallet(
core: Core(
projectId: DartDefines.projectId,
),
metadata: const PairingMetadata(
name: 'Sample Wallet Flutter',
description: 'WalletConnect\'s sample wallet with Flutter',
Expand All @@ -44,6 +45,28 @@ class Web3WalletService extends IWeb3WalletService {
),
);

_web3Wallet!.core.addLogListener(_logListener);

// Setup our listeners
debugPrint('[SampleWallet] create');
_web3Wallet!.core.pairing.onPairingInvalid.subscribe(_onPairingInvalid);
_web3Wallet!.core.pairing.onPairingCreate.subscribe(_onPairingCreate);
_web3Wallet!.core.relayClient.onRelayClientError.subscribe(
_onRelayClientError,
);
_web3Wallet!.core.relayClient.onRelayClientMessage.subscribe(
_onRelayClientMessage,
);

_web3Wallet!.onSessionProposal.subscribe(_onSessionProposal);
_web3Wallet!.onSessionProposalError.subscribe(_onSessionProposalError);
_web3Wallet!.onSessionConnect.subscribe(_onSessionConnect);
_web3Wallet!.onSessionAuthRequest.subscribe(_onSessionAuthRequest);

_web3Wallet!.onAuthRequest.subscribe(_onAuthRequest);

await _web3Wallet!.init();

// Setup our accounts
List<ChainKey> chainKeys = await GetIt.I<IKeyService>().setKeys();
if (chainKeys.isEmpty) {
Expand All @@ -69,25 +92,6 @@ class Web3WalletService extends IWeb3WalletService {
}
}
}

// Setup our listeners
debugPrint('[SampleWallet] create');
_web3Wallet!.core.addLogListener(_logListener);
_web3Wallet!.core.pairing.onPairingInvalid.subscribe(_onPairingInvalid);
_web3Wallet!.core.pairing.onPairingCreate.subscribe(_onPairingCreate);
_web3Wallet!.core.relayClient.onRelayClientError.subscribe(
_onRelayClientError,
);
_web3Wallet!.core.relayClient.onRelayClientMessage.subscribe(
_onRelayClientMessage,
);

_web3Wallet!.onSessionProposal.subscribe(_onSessionProposal);
_web3Wallet!.onSessionProposalError.subscribe(_onSessionProposalError);
_web3Wallet!.onSessionConnect.subscribe(_onSessionConnect);
_web3Wallet!.onSessionAuthRequest.subscribe(_onSessionAuthRequest);

_web3Wallet!.onAuthRequest.subscribe(_onAuthRequest);
}

@override
Expand Down
136 changes: 66 additions & 70 deletions lib/apis/core/relay_client/relay_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,33 +187,33 @@ class RelayClient implements IRelayClient {
/// PRIVATE FUNCTIONS ///
Future<void> _connect({String? relayUrl}) async {
core.logger.t('RelayClient Internal: Connecting to relay');
if (isConnected) {
return;
}

core.relayUrl = relayUrl ?? core.relayUrl;
core.logger.d('[$runtimeType] Connecting to relay ${core.relayUrl}');

// If we have tried connecting to the relay before, disconnect
if (_active) {
await _disconnect();
}

// Connect and track the connection progress, then start the heartbeat
_connectingFuture = _createJsonRPCProvider();
await _connectingFuture;
_subscribeToHeartbeat();

// 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;
}
try {
// Connect and track the connection progress, then start the heartbeat
_connectingFuture = _createJsonRPCProvider();
await _connectingFuture;
_connecting = false;
_subscribeToHeartbeat();
//
} on TimeoutException catch (e) {
core.logger.d('[$runtimeType] Connect timeout: $e');
onRelayClientError.broadcast(ErrorEvent('Connection to relay timeout'));
_connecting = false;
} catch (e) {
core.logger.d('[$runtimeType] Connect error: $e');
onRelayClientError.broadcast(ErrorEvent(e));
_connecting = false;
}
}

Expand All @@ -238,66 +238,62 @@ class RelayClient implements IRelayClient {
_active = true;
final auth = await core.crypto.signJWT(core.relayUrl);
core.logger.t('Signed JWT: $auth');
try {
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()),
);
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) {
await jsonRPC!.close();
jsonRPC = null;
}
if (jsonRPC != null) {
await jsonRPC!.close();
jsonRPC = null;
}

core.logger.t('Initializing WebSocket with $url');
await socketHandler.setup(url: url);
await socketHandler.connect();
core.logger.t('Initializing WebSocket with $url');
await socketHandler.setup(url: url);
await socketHandler.connect().timeout(Duration(seconds: 5));

jsonRPC = Peer(socketHandler.channel!);
jsonRPC = Peer(socketHandler.channel!);

jsonRPC!.registerMethod(
_buildMethod(JSON_RPC_SUBSCRIPTION),
_handleSubscription,
);
jsonRPC!.registerMethod(
_buildMethod(JSON_RPC_SUBSCRIBE),
_handleSubscribe,
);
jsonRPC!.registerMethod(
_buildMethod(JSON_RPC_UNSUBSCRIBE),
_handleUnsubscribe,
);
jsonRPC!.registerMethod(
_buildMethod(JSON_RPC_SUBSCRIPTION),
_handleSubscription,
);
jsonRPC!.registerMethod(
_buildMethod(JSON_RPC_SUBSCRIBE),
_handleSubscribe,
);
jsonRPC!.registerMethod(
_buildMethod(JSON_RPC_UNSUBSCRIBE),
_handleUnsubscribe,
);

if (jsonRPC!.isClosed) {
throw const WalletConnectError(
code: 0,
message: 'WebSocket closed',
);
}
if (jsonRPC!.isClosed) {
throw const WalletConnectError(
code: 0,
message: 'WebSocket closed',
);
}

jsonRPC!.listen();
jsonRPC!.listen();

// When jsonRPC closes, emit the event
_handledClose = false;
jsonRPC!.done.then(
(value) {
_handleRelayClose(
socketHandler.closeCode,
socketHandler.closeReason,
);
},
);
// When jsonRPC closes, emit the event
_handledClose = false;
jsonRPC!.done.then(
(value) {
_handleRelayClose(
socketHandler.closeCode,
socketHandler.closeReason,
);
},
);

onRelayClientConnect.broadcast();
} catch (e) {
onRelayClientError.broadcast(ErrorEvent(e));
}
_connecting = false;
onRelayClientConnect.broadcast();
core.logger.d('[$runtimeType] Connected to relay ${core.relayUrl}');
}

Future<void> _handleRelayClose(int? code, String? reason) async {
Expand Down
Loading

0 comments on commit dc13450

Please sign in to comment.