createSession method

Future<SessionStatus> createSession({
  1. int? chainId,
  2. OnDisplayUriCallback? onDisplayUri,
})

Creates a new session between the dApp and wallet. The dapp should call this method for initiating the session. https://docs.walletconnect.com/client-api#create-new-session-session_request

Implementation

Future<SessionStatus> createSession({
  int? chainId,
  OnDisplayUriCallback? onDisplayUri,
}) async {
  if (connected) {
    throw WalletConnectException('Session currently connected');
  }

  // Generate encryption key
  session.key = await cipherBox.generateKey();

  final request = JsonRpcRequest(
    id: payloadId,
    method: 'wc_sessionRequest',
    params: [
      {
        'peerId': session.clientId,
        'peerMeta': session.clientMeta,
        'chainId': chainId,
      }
    ],
  );

  session.handshakeId = request.id;
  session.handshakeTopic = const Uuid().v4();

  // Display the URI
  final uri = session.toUri();
  onDisplayUri?.call(uri);
  _eventBus.fire(Event<String>('display_uri', uri));

  // Send the request
  final response = await _sendRequest(request, topic: session.handshakeTopic);

  // Notify listeners
  await _handleSessionResponse(response);

  return WCSessionRequestResponse.fromJson(response).status;
}