respondAuthRequest method

  1. @override
Future<void> respondAuthRequest({
  1. required int id,
  2. required String iss,
  3. CacaoSignature? signature,
  4. WalletConnectError? error,
})
override

Construct the Namespaces map for a session proposal. Uses the registered methods, events, and keys to build them. If the required namespaces is not satisfied, this function will throw a WalletConnectError. The optional namespaces are included if they exist in the registered information.

Implementation

@override
Future<void> respondAuthRequest({
  required int id,
  required String iss,
  CacaoSignature? signature,
  WalletConnectError? error,
}) async {
  _checkInitialized();

  final pendingAuthRequests = getPendingAuthRequests();
  AuthApiValidators.isValidRespond(
    id: id,
    pendingRequests: pendingAuthRequests,
    signature: signature,
    error: error,
  );

  final PendingAuthRequest pendingRequest = pendingAuthRequests[id]!;
  if (!pendingRequest.transportType.isLinkMode) {
    _confirmOnlineStateOrThrow();
  }

  final String receiverPublicKey = pendingRequest.metadata.publicKey;
  final String senderPublicKey = await core.crypto.generateKeyPair();
  final String responseTopic = core.crypto.getUtils().hashKey(
        receiverPublicKey,
      );
  final EncodeOptions encodeOpts = EncodeOptions(
    type: EncodeOptions.TYPE_1,
    receiverPublicKey: receiverPublicKey,
    senderPublicKey: senderPublicKey,
  );

  if (error != null) {
    await core.pairing.sendError(
      id,
      responseTopic,
      MethodConstants.WC_AUTH_REQUEST,
      JsonRpcError.serverError(error.message),
      encodeOptions: encodeOpts,
    );
  } else {
    final Cacao cacao = Cacao(
      h: const CacaoHeader(),
      p: CacaoPayload.fromRequestPayload(
        issuer: iss,
        payload: pendingRequest.cacaoPayload,
      ),
      s: signature!,
    );

    // print('auth res id: $id');
    await core.pairing.sendResult(
      id,
      responseTopic,
      MethodConstants.WC_AUTH_REQUEST,
      cacao.toJson(),
      encodeOptions: encodeOpts,
    );

    await authRequests.delete(id.toString());

    await completeRequests.set(
      id.toString(),
      StoredCacao.fromCacao(
        id: id,
        pairingTopic: pendingRequest.pairingTopic,
        cacao: cacao,
      ),
    );
  }
}