Skip to content

Commit

Permalink
Merge pull request #215 from WalletConnect/chores/better_logging
Browse files Browse the repository at this point in the history
Just minor changes about different logs for debug purposes
  • Loading branch information
quetool committed Nov 2, 2023
2 parents ef200e9 + 2aaa5cc commit f7bb820
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 53 deletions.
2 changes: 1 addition & 1 deletion example/wallet/lib/dependencies/chains/evm_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class EVMService extends IChain {
return null;
}

Future personalSign(String topic, dynamic parameters) async {
Future<String?> personalSign(String topic, dynamic parameters) async {
print('received personal sign request: $parameters');

final String message = EthUtils.getUtf8Message(parameters[0]);
Expand Down
51 changes: 41 additions & 10 deletions example/wallet/lib/dependencies/web3wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:walletconnect_flutter_v2_wallet/dependencies/i_web3wallet_servic
import 'package:walletconnect_flutter_v2_wallet/dependencies/key_service/chain_key.dart';
import 'package:walletconnect_flutter_v2_wallet/dependencies/key_service/i_key_service.dart';
import 'package:walletconnect_flutter_v2_wallet/utils/dart_defines.dart';
import 'package:walletconnect_flutter_v2_wallet/utils/eth_utils.dart';
import 'package:walletconnect_flutter_v2_wallet/widgets/wc_connection_request/wc_auth_request_model.dart';
import 'package:walletconnect_flutter_v2_wallet/widgets/wc_connection_request/wc_connection_request_widget.dart';
import 'package:walletconnect_flutter_v2_wallet/widgets/wc_connection_request/wc_session_request_model.dart';
Expand Down Expand Up @@ -79,8 +80,10 @@ class Web3WalletService extends IWeb3WalletService {
_web3Wallet!.onSessionProposal.subscribe(_onSessionProposal);
_web3Wallet!.onSessionProposalError.subscribe(_onSessionProposalError);
_web3Wallet!.onSessionConnect.subscribe(_onSessionConnect);
// _web3Wallet!.onSessionRequest.subscribe(_onSessionRequest);
_web3Wallet!.onSessionRequest.subscribe(_onSessionRequest);
_web3Wallet!.onAuthRequest.subscribe(_onAuthRequest);
_web3Wallet!.core.relayClient.onRelayClientError
.subscribe(_onRelayClientError);
}

@override
Expand All @@ -102,8 +105,10 @@ class Web3WalletService extends IWeb3WalletService {
_web3Wallet!.onSessionProposal.unsubscribe(_onSessionProposal);
_web3Wallet!.onSessionProposalError.unsubscribe(_onSessionProposalError);
_web3Wallet!.onSessionConnect.unsubscribe(_onSessionConnect);
// _web3Wallet!.onSessionRequest.unsubscribe(_onSessionRequest);
_web3Wallet!.onSessionRequest.unsubscribe(_onSessionRequest);
_web3Wallet!.onAuthRequest.unsubscribe(_onAuthRequest);
_web3Wallet!.core.relayClient.onRelayClientError
.unsubscribe(_onRelayClientError);
}

@override
Expand All @@ -117,17 +122,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,11 +162,38 @@ 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) {
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) {
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
25 changes: 7 additions & 18 deletions lib/apis/core/relay_client/relay_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,10 @@ class RelayClient implements IRelayClient {
}

core.logger.t('Initializing WebSocket with $url');
await socketHandler.setup(
url: url,
);
await socketHandler.setup(url: url);
await socketHandler.connect();

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

jsonRPC!.registerMethod(
_buildMethod(JSON_RPC_SUBSCRIPTION),
Expand Down Expand Up @@ -299,11 +295,7 @@ class RelayClient implements IRelayClient {

onRelayClientConnect.broadcast();
} catch (e) {
onRelayClientError.broadcast(
ErrorEvent(
e,
),
);
onRelayClientError.broadcast(ErrorEvent(e));
}
_connecting = false;
}
Expand Down Expand Up @@ -332,15 +324,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
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ abstract class IWebSocketHandler {

Future<void> get ready;

Future<void> setup({
required String url,
});
Future<void> setup({required String url});

Future<void> connect();

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
10 changes: 2 additions & 8 deletions test/core_api/relay_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ void main() {
test('when connection parameters are invalid', () async {
final http = MockHttpWrapper();
when(http.get(any)).thenAnswer(
(_) async => Response(
'',
WebSocketErrors.PROJECT_ID_NOT_FOUND,
),
(_) async => Response('', 3000),
);
final ICore core = Core(
projectId: 'abc',
Expand All @@ -91,10 +88,7 @@ void main() {
Completer completer = Completer();
core.relayClient.onRelayClientError.subscribe((args) {
expect(args!.error, isA<WalletConnectError>());
expect(
args.error.message,
WebSocketErrors.INVALID_PROJECT_ID_OR_JWT,
);
expect(args.error.code, 3000);
completer.complete();
});

Expand Down
2 changes: 2 additions & 0 deletions test/sign_api/sign_client_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@Timeout(Duration(seconds: 45))

import 'package:flutter_test/flutter_test.dart';
import 'package:logger/logger.dart';
import 'package:package_info_plus/package_info_plus.dart';
Expand Down
2 changes: 2 additions & 0 deletions test/sign_api/tests/sign_common.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@Timeout(Duration(seconds: 45))

import 'package:flutter_test/flutter_test.dart';
import 'package:walletconnect_flutter_v2/apis/sign_api/i_sign_engine_app.dart';
import 'package:walletconnect_flutter_v2/apis/sign_api/i_sign_engine_common.dart';
Expand Down

0 comments on commit f7bb820

Please sign in to comment.