connect method

  1. @override
Future<ConnectResponse> connect({
  1. Map<String, RequiredNamespace>? requiredNamespaces,
  2. Map<String, RequiredNamespace>? optionalNamespaces,
  3. Map<String, String>? sessionProperties,
  4. String? pairingTopic,
  5. List<Relay>? relays,
  6. List<List<String>>? methods = DEFAULT_METHODS,
})
override

Implementation

@override
Future<ConnectResponse> connect({
  Map<String, RequiredNamespace>? requiredNamespaces,
  Map<String, RequiredNamespace>? optionalNamespaces,
  Map<String, String>? sessionProperties,
  String? pairingTopic,
  List<Relay>? relays,
  List<List<String>>? methods = DEFAULT_METHODS,
}) async {
  _checkInitialized();
  _confirmOnlineStateOrThrow();

  await _isValidConnect(
    requiredNamespaces: requiredNamespaces ?? {},
    optionalNamespaces: optionalNamespaces ?? {},
    sessionProperties: sessionProperties,
    pairingTopic: pairingTopic,
    relays: relays,
  );
  String? pTopic = pairingTopic;
  Uri? uri;

  if (pTopic == null) {
    final CreateResponse newTopicAndUri = await core.pairing.create(
      methods: methods,
    );
    pTopic = newTopicAndUri.topic;
    uri = newTopicAndUri.uri;
    // print('connect generated topic: $topic');
  } else {
    core.pairing.isValidPairingTopic(topic: pTopic);
  }

  final publicKey = await core.crypto.generateKeyPair();
  final int id = JsonRpcUtils.payloadId();

  final request = WcSessionProposeRequest(
    relays: relays ?? [Relay(ReownConstants.RELAYER_DEFAULT_PROTOCOL)],
    requiredNamespaces: requiredNamespaces ?? {},
    optionalNamespaces: optionalNamespaces ?? {},
    proposer: ConnectionMetadata(
      publicKey: publicKey,
      metadata: metadata,
    ),
    sessionProperties: sessionProperties,
  );

  final expiry = ReownCoreUtils.calculateExpiry(
    ReownConstants.FIVE_MINUTES,
  );
  final ProposalData proposal = ProposalData(
    id: id,
    expiry: expiry,
    relays: request.relays,
    proposer: request.proposer,
    requiredNamespaces: request.requiredNamespaces,
    optionalNamespaces: request.optionalNamespaces ?? {},
    sessionProperties: request.sessionProperties,
    pairingTopic: pTopic,
  );
  await _setProposal(
    id,
    proposal,
  );

  Completer<SessionData> completer = Completer();

  pendingProposals.add(
    SessionProposalCompleter(
      id: id,
      selfPublicKey: publicKey,
      pairingTopic: pTopic,
      requiredNamespaces: request.requiredNamespaces,
      optionalNamespaces: request.optionalNamespaces ?? {},
      sessionProperties: request.sessionProperties,
      completer: completer,
    ),
  );
  _connectResponseHandler(
    pTopic,
    request,
    id,
  );

  final ConnectResponse resp = ConnectResponse(
    pairingTopic: pTopic,
    session: completer,
    uri: uri,
  );

  return resp;
}