respondAuthRequest method

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

respond wallet authentication

Implementation

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

  Map<int, PendingAuthRequest> pendingRequests = getPendingAuthRequests();
  AuthApiValidators.isValidRespond(
    id: id,
    pendingRequests: pendingRequests,
    signature: signature,
    error: error,
  );

  final PendingAuthRequest pendingRequest = pendingRequests[id]!;
  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,
      ),
    );
  }
}