checkUIRSchemeIsValid method Null safety

Future<bool> checkUIRSchemeIsValid(
  1. String url
)

Checks if the URL is valid; signature and domain must be present and correct for the signer's keypair. returns true if valid, otherwise thrown the corresponding URISchemeError.

Implementation

Future<bool> checkUIRSchemeIsValid(String url) async {
  final String? originDomain =
  getParameterValue(originDomainParameterName, url);
  if (originDomain == null) {
    throw URISchemeError(URISchemeError.missingOriginDomain);
  }

  final isFullyQualifiedDomainNameRegExp = new RegExp(
      r"(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-).)+[a-zA-Z]{2,63}.?$)");
  if (!isFullyQualifiedDomainNameRegExp.hasMatch(originDomain)) {
    throw URISchemeError(URISchemeError.invalidOriginDomain);
  }

  final String? signature = getParameterValue(signatureParameterName, url);
  if (signature == null) {
    throw URISchemeError(URISchemeError.missingSignature);
  }

  StellarToml? toml;
  try {
    toml = await StellarToml.fromDomain(originDomain, httpClient: httpClient);
  } on Exception catch (_) {
    throw URISchemeError(URISchemeError.tomlNotFoundOrInvalid);
  }

  final String? uriRequestSigningKey =
      toml.generalInformation.uriRequestSigningKey;
  if (uriRequestSigningKey == null) {
    throw URISchemeError(URISchemeError.tomlSignatureMissing);
  }

  final KeyPair signerPublicKey = KeyPair.fromAccountId(uriRequestSigningKey);
  try {
    if (!verify(url, signature, signerPublicKey)) {
      throw URISchemeError(URISchemeError.invalidSignature);
    }
  } on Exception catch (_) {
    throw URISchemeError(URISchemeError.invalidSignature);
  }
  return true;
}