connect method

  1. @override
Future<EngineConnection> connect(
  1. SessionConnectParams params
)
override

Implementation

@override
Future<EngineConnection> connect(SessionConnectParams params) async {
  _isInitialized();
  await _isValidConnect(params);

  String? topic = params.pairingTopic;
  String? uri;
  bool active = false;

  if (topic != null) {
    final pairing = client.core.pairing.pairings.get(topic);
    active = pairing.active;
  }

  if (topic == null || !active) {
    final pairingData = await client.core.pairing.create();
    topic = pairingData.topic;
    uri = pairingData.uri;
  }

  final publicKey = await client.core.crypto.generateKeyPair();

  final proposal = ProposalRequestStruct(
    requiredNamespaces: params.requiredNamespaces,
    relays: params.relays ??
        [RelayerProtocolOptions(protocol: RELAYER_DEFAULT_PROTOCOL)],
    proposer: ProposalProposer(
      publicKey: publicKey,
      metadata: client.metadata,
    ),
  );

  final completer = Completer<SessionStruct>();
  final timer = completer.expirer();
  events.once(
    engineEvent(EngineEvent.SESSION_CONNECT),
    (data) async {
      timer.cancel();
      if (data is ErrorResponse) {
        completer.completeError(data.toString());
      } else if (data is SessionStruct) {
        final SessionStruct session = data;
        final completeSession = session.copyWith(
          self: session.self.copyWith(publicKey: publicKey),
          requiredNamespaces: params.requiredNamespaces,
        );
        await client.session.set(session.topic, completeSession);
        await _setExpiry(session.topic, session.expiry);
        if (topic != null) {
          await client.core.pairing.updateMetadata(
            topic: topic,
            metadata: session.peer.metadata,
          );
        }
        completer.complete(completeSession);
      }
    },
  );

  // if (topic == null) {
  //   final error = getInternalError(InternalErrorKey.NO_MATCHING_KEY,
  //       context: 'connect() pairing topic: ${topic}');
  //   throw WCException(error.message);
  // }

  final id = await _sendRequest<ProposalRequestStruct>(
    topic,
    JsonRpcMethod.WC_SESSION_PROPOSE,
    proposal,
    (v) => v.toJson(),
  );

  final expiry = calcExpiry(ttl: FIVE_MINUTES);
  await _setProposal(
    id.toString(),
    ProposalStruct(
      id: id,
      expiry: expiry,
      relays: proposal.relays,
      proposer: proposal.proposer,
      requiredNamespaces: proposal.requiredNamespaces,
    ),
  );
  return EngineConnection(
    uri: uri,
    approval: completer.future,
  );
}