approveSession method

  1. @override
Future<ApproveResponse> approveSession({
  1. required int id,
  2. required Map<String, Namespace> namespaces,
  3. Map<String, String>? sessionProperties,
  4. String? relayProtocol,
})
override

Approves a proposal with the id provided in the parameters. Assumes the proposal is already created.

Implementation

@override
Future<ApproveResponse> approveSession({
  required int id,
  required Map<String, Namespace> namespaces,
  Map<String, String>? sessionProperties,
  String? relayProtocol,
}) async {
  _checkInitialized();

  await _isValidApprove(
    id: id,
    namespaces: namespaces,
    sessionProperties: sessionProperties,
    relayProtocol: relayProtocol,
  );

  final ProposalData proposal = proposals.get(
    id.toString(),
  )!;

  final String selfPubKey = await core.crypto.generateKeyPair();
  final String peerPubKey = proposal.proposer.publicKey;
  final String sessionTopic = await core.crypto.generateSharedKey(
    selfPubKey,
    peerPubKey,
  );
  // print('approve session topic: $sessionTopic');
  final relay = Relay(
    relayProtocol != null ? relayProtocol : 'irn',
  );
  final int expiry = WalletConnectUtils.calculateExpiry(
    WalletConnectConstants.SEVEN_DAYS,
  );
  final request = WcSessionSettleRequest(
    relay: relay,
    namespaces: namespaces,
    requiredNamespaces: proposal.requiredNamespaces,
    optionalNamespaces: proposal.optionalNamespaces,
    sessionProperties: sessionProperties,
    expiry: expiry,
    controller: ConnectionMetadata(
      publicKey: selfPubKey,
      metadata: metadata,
    ),
  );

  // Respond to the proposal
  await core.pairing.sendResult(
    id,
    proposal.pairingTopic,
    MethodConstants.WC_SESSION_PROPOSE,
    WcSessionProposeResponse(
      relay: Relay(
        relayProtocol != null
            ? relayProtocol
            : WalletConnectConstants.RELAYER_DEFAULT_PROTOCOL,
      ),
      responderPublicKey: selfPubKey,
    ),
  );
  await _deleteProposal(id);
  await core.pairing.activate(topic: proposal.pairingTopic);

  await core.pairing.updateMetadata(
    topic: proposal.pairingTopic,
    metadata: proposal.proposer.metadata,
  );

  await core.relayClient.subscribe(topic: sessionTopic);
  bool acknowledged = await core.pairing.sendRequest(
    sessionTopic,
    MethodConstants.WC_SESSION_SETTLE,
    request,
  );

  SessionData session = SessionData(
    topic: sessionTopic,
    pairingTopic: proposal.pairingTopic,
    relay: relay,
    expiry: expiry,
    acknowledged: acknowledged,
    controller: selfPubKey,
    namespaces: namespaces,
    requiredNamespaces: proposal.requiredNamespaces,
    optionalNamespaces: proposal.optionalNamespaces,
    self: ConnectionMetadata(
      publicKey: selfPubKey,
      metadata: metadata,
    ),
    peer: proposal.proposer,
  );

  onSessionConnect.broadcast(
    SessionConnect(
      session,
    ),
  );

  await sessions.set(sessionTopic, session);
  await _setSessionExpiry(sessionTopic, expiry);

  return ApproveResponse(
    topic: sessionTopic,
    session: session,
  );
}