Skip to content

Commit

Permalink
Better logging on socket connection error and debug prints on example…
Browse files Browse the repository at this point in the history
… app
  • Loading branch information
quetool committed Oct 31, 2023
1 parent ef200e9 commit ccd874d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
47 changes: 39 additions & 8 deletions example/wallet/lib/dependencies/web3wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Web3WalletService extends IWeb3WalletService {
_web3Wallet!.onSessionConnect.subscribe(_onSessionConnect);
// _web3Wallet!.onSessionRequest.subscribe(_onSessionRequest);
_web3Wallet!.onAuthRequest.subscribe(_onAuthRequest);
_web3Wallet!.core.relayClient.onRelayClientError
.subscribe(_onRelayClientError);
}

@override
Expand All @@ -104,6 +106,8 @@ class Web3WalletService extends IWeb3WalletService {
_web3Wallet!.onSessionConnect.unsubscribe(_onSessionConnect);
// _web3Wallet!.onSessionRequest.unsubscribe(_onSessionRequest);
_web3Wallet!.onAuthRequest.unsubscribe(_onAuthRequest);
_web3Wallet!.core.relayClient.onRelayClientError
.unsubscribe(_onRelayClientError);
}

@override
Expand All @@ -117,17 +121,16 @@ class Web3WalletService extends IWeb3WalletService {
}
}

void _onRelayClientError(ErrorEvent? args) {
debugPrint('[$runtimeType] _onRelayClientError ${args?.error}');
}

void _onSessionProposalError(SessionProposalErrorEvent? args) {
print(args);
debugPrint('[$runtimeType] _onSessionProposalError $args');
}

void _onSessionProposal(SessionProposalEvent? args) async {
if (args != null) {
// print(args);

// Validate the
// args.params.

final Widget w = WCRequestWidget(
child: WCConnectionRequestWidget(
wallet: _web3Wallet!,
Expand Down Expand Up @@ -158,13 +161,41 @@ class Web3WalletService extends IWeb3WalletService {
}

void _onPairingInvalid(PairingInvalidEvent? args) {
print('Pairing Invalid Event: $args');
debugPrint('[$runtimeType] _onPairingInvalid $args');
}

void _onPairingCreate(PairingEvent? args) {
print('Pairing Create Event: $args');
debugPrint('[$runtimeType] _onPairingCreate $args');
}

// void _onSessionRequest(SessionRequestEvent? args) {
// debugPrint('[$runtimeType] _onSessionRequest $args');
// if (args == null) return;

// final id = args.id;
// final topic = args.topic;
// final parameters = args.params;

// final message = EthUtils.getUtf8Message(parameters[0]);

// debugPrint('On session request event: $id, $topic, $message');

// // // Load the private key
// // final keys = GetIt.I<IKeyService>().getKeysForChain(getChainId());
// // final credentials = EthPrivateKey.fromHex(keys[0].privateKey);

// // final signedMessage = hex.encode(
// // credentials.signPersonalMessageToUint8List(
// // Uint8List.fromList(utf8.encode(message)),
// // ),
// // );

// // final r = {'id': id, 'result': signedMessage, 'jsonrpc': '2.0'};
// // final response = JsonRpcResponse.fromJson(r);
// // print(r);
// // _web3Wallet?.respondSessionRequest(topic: topic, response: response);
// }

void _onSessionConnect(SessionConnect? args) {
if (args != null) {
print(args);
Expand Down
17 changes: 5 additions & 12 deletions lib/apis/core/pairing/pairing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -397,17 +397,17 @@ class Pairing implements IPairing {
EncodeOptions? encodeOptions,
}) async {
core.logger.t(
'pairing sendResult, id: $id topic: $topic, method: $method, params: $params, ttl: $ttl',
'pairing sendRequest, id: $id topic: $topic, method: $method, params: $params, ttl: $ttl',
);

final Map<String, dynamic> payload = JsonRpcUtils.formatJsonRpcRequest(
final payload = JsonRpcUtils.formatJsonRpcRequest(
method,
params,
id: id,
);
// print('sending request: $payload');

final String? message = await core.crypto.encode(
final message = await core.crypto.encode(
topic,
payload,
options: encodeOptions,
Expand All @@ -423,12 +423,9 @@ class Pairing implements IPairing {
}

// print('adding payload to pending requests: ${payload['id']}');
final PendingRequestResponse resp = PendingRequestResponse(
completer: Completer(),
);
resp.completer.future.catchError((_) {
final resp = PendingRequestResponse(completer: Completer());
resp.completer.future.catchError((err) {
// Catch the error so that it won't throw an uncaught error
// print('inner caught error: $err');
});
pendingRequests[payload['id']] = resp;
// print('sent request');
Expand All @@ -441,7 +438,6 @@ class Pairing implements IPairing {

// Get the result from the completer, if it's an error, throw it
try {
// print('checking error');
if (resp.error != null) {
throw resp.error!;
}
Expand All @@ -451,10 +447,8 @@ class Pairing implements IPairing {
return resp.response;
}

// print('waiting for response');
return await resp.completer.future;
} catch (e) {
// print('caught error: $e');
rethrow;
}
}
Expand Down Expand Up @@ -812,7 +806,6 @@ class Pairing implements IPairing {
}

void _heartbeatSubscription(EventArgs? args) async {
core.logger.i('Pairing heartbeat received');
await checkAndExpire();
}

Expand Down
11 changes: 4 additions & 7 deletions lib/apis/core/relay_client/relay_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,12 @@ class RelayClient implements IRelayClient {
await connect();
} else {
await disconnect();
final String errorReason = code == 3000
? WebSocketErrors.INVALID_PROJECT_ID_OR_JWT
: reason ?? '';
final errorReason = code == 3000
? reason ?? WebSocketErrors.INVALID_PROJECT_ID_OR_JWT
: '';
onRelayClientError.broadcast(
ErrorEvent(
WalletConnectError(
code: code,
message: errorReason,
),
WalletConnectError(code: code, message: errorReason),
),
);
}
Expand Down
1 change: 0 additions & 1 deletion lib/apis/sign_api/sign_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,6 @@ class SignEngine implements ISignEngine {
}

void _heartbeatSubscription(EventArgs? args) async {
core.logger.i('SignEngine heartbeat received');
await checkAndExpire();
}

Expand Down

0 comments on commit ccd874d

Please sign in to comment.