approve method

  1. @override
Future<EngineApproved> approve(
  1. SessionApproveParams params
)
override

Implementation

@override
Future<EngineApproved> approve(SessionApproveParams params) async {
  _isInitialized();
  await _isValidApprove(params);

  final id = params.id;
  final namespaces = params.namespaces;
  final relayProtocol = params.relayProtocol;

  final proposal = client.proposal.get(id.toString());
  final pairingTopic = proposal.pairingTopic;
  final proposer = proposal.proposer;
  final requiredNamespaces = proposal.requiredNamespaces;

  final selfPublicKey = await client.core.crypto.generateKeyPair();
  final peerPublicKey = proposer.publicKey;
  final sessionTopic = await client.core.crypto.generateSharedKey(
    selfPublicKey: selfPublicKey,
    peerPublicKey: peerPublicKey,
  );
  final sessionSettle = SessionSettleParams(
    relay: RelayerProtocolOptions(protocol: relayProtocol ?? "irn"),
    namespaces: namespaces,
    requiredNamespaces: requiredNamespaces,
    controller: SessionPublicKeyMetadata(
        publicKey: selfPublicKey, metadata: client.metadata),
    expiry: calcExpiry(ttl: SESSION_EXPIRY),
  );

  await client.core.relayer.subscribe(topic: sessionTopic);
  final requestId = await _sendRequest<SessionSettleParams>(
    sessionTopic,
    JsonRpcMethod.WC_SESSION_SETTLE,
    sessionSettle,
    (v) => v.toJson(),
  );
  final completer = Completer<SessionStruct>();
  final timer = completer.expirer();
  events.once(engineEvent(EngineEvent.SESSION_APPROVE, requestId), (data) {
    timer.cancel();
    if (data is ErrorResponse) {
      completer.completeError(data);
    } else {
      completer.complete(client.session.get(sessionTopic));
    }
  });

  final session = SessionStruct(
    topic: sessionTopic,
    relay: sessionSettle.relay,
    expiry: sessionSettle.expiry,
    acknowledged: false,
    controller: selfPublicKey,
    namespaces: namespaces,
    requiredNamespaces: requiredNamespaces,
    self: sessionSettle.controller,
    peer: SessionPublicKeyMetadata(
      publicKey: proposer.publicKey,
      metadata: proposer.metadata,
    ),
  );

  await client.session.set(sessionTopic, session);
  await _setExpiry(sessionTopic, calcExpiry(ttl: SESSION_EXPIRY));
  if (pairingTopic != null) {
    await client.core.pairing.updateMetadata(
      topic: pairingTopic,
      metadata: session.peer.metadata,
    );
  }
  if (pairingTopic != null) {
    await _sendResult<ResultSessionPropose>(
      id,
      pairingTopic,
      ResultSessionPropose(
        relay: RelayerProtocolOptions(
          protocol: relayProtocol ?? "irn",
        ),
        responderPublicKey: selfPublicKey,
      ),
      (v) => v.toJson(),
    );
    await client.proposal.delete(
      id.toString(),
      getSdkError(SdkErrorKey.USER_DISCONNECTED),
    );
    await client.core.pairing.activate(topic: pairingTopic);
  }

  // final data = await completer.future;
  return EngineApproved(
    topic: sessionTopic,
    acknowledged: completer.future,
  );
}