requestAuth method

  1. @override
Future<AuthRequestResponse> requestAuth({
  1. required AuthRequestParams params,
  2. String? pairingTopic,
  3. List<List<String>>? methods = DEFAULT_METHODS,
})
override

Implementation

@override
Future<AuthRequestResponse> requestAuth({
  required AuthRequestParams params,
  String? pairingTopic,
  List<List<String>>? methods = DEFAULT_METHODS,
}) async {
  _checkInitialized();

  AuthApiValidators.isValidRequest(params);
  String? pTopic = pairingTopic;
  Uri? uri;

  if (pTopic == null) {
    final CreateResponse newTopicAndUri = await core.pairing.create(
      methods: methods,
    );
    pTopic = newTopicAndUri.topic;
    uri = newTopicAndUri.uri;
  } else {
    core.pairing.isValidPairingTopic(topic: pTopic);
  }

  final publicKey = await core.crypto.generateKeyPair();
  // print('requestAuth, publicKey: $publicKey');
  final String responseTopic = core.crypto.getUtils().hashKey(publicKey);
  final int id = JsonRpcUtils.payloadId();

  WcAuthRequestRequest request = WcAuthRequestRequest(
    payloadParams: AuthPayloadParams.fromRequestParams(
      params,
    ),
    requester: ConnectionMetadata(
      publicKey: publicKey,
      metadata: metadata,
    ),
  );

  final int expiry = params.expiry ?? WalletConnectConstants.FIVE_MINUTES;

  await authKeys.set(
    StringConstants.AUTH_CLIENT_PUBLIC_KEY_NAME,
    AuthPublicKey(publicKey: publicKey),
  );

  await pairingTopics.set(
    responseTopic,
    pTopic,
  );

  // Set the one time use receiver public key for decoding the Type 1 envelope
  await core.pairing.setReceiverPublicKey(
    topic: responseTopic,
    publicKey: publicKey,
    expiry: expiry,
  );

  Completer<AuthResponse> completer = Completer();

  _requestAuthResponseHandler(
    pairingTopic: pTopic,
    responseTopic: responseTopic,
    request: request,
    id: id,
    expiry: expiry,
    completer: completer,
  );

  return AuthRequestResponse(
    id: id,
    pairingTopic: pTopic,
    completer: completer,
    uri: uri,
  );
}