approve method

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

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

Implementation

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

  await _isValidApprove(
    id,
    namespaces,
    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(
    id: id,
    relay: relay,
    namespaces: namespaces,
    requiredNamespaces: proposal.requiredNamespaces,
    expiry: expiry,
    controller: ConnectionMetadata(
      publicKey: selfPubKey,
      metadata: selfMetadata,
    ),
  );

  // If we received this request from somewhere, respond with the sessionTopic
  // so they can update their listener.
  // print('approve requestId: ${id}');

  if (proposal.pairingTopic != null && id > 0) {
    // print('approve proposal topic: ${proposal.pairingTopic!}');
    await core.pairing.sendResult(
      id,
      proposal.pairingTopic!,
      'wc_sessionPropose',
      WcSessionProposeResponse(
        relay: Relay(
          relayProtocol != null
              ? relayProtocol
              : WalletConnectConstants.RELAYER_DEFAULT_PROTOCOL,
        ),
        responderPublicKey: selfPubKey,
      ).toJson(),
    );
    await _deleteProposal(id);
    await core.pairing.activate(proposal.pairingTopic!);

    await core.pairing.updateMetadata(
      proposal.pairingTopic!,
      proposal.proposer.metadata,
    );
  }

  await core.relayClient.subscribe(sessionTopic);
  bool acknowledged = await core.pairing.sendRequest(
    sessionTopic,
    'wc_sessionSettle',
    request.toJson(),
  );

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

  await sessions.set(sessionTopic, session);

  // If we have a pairing topic, update its metadata with the peer
  if (proposal.pairingTopic != null) {}

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