fromDomain static method

Future<WebAuth> fromDomain(
  1. String domain,
  2. Network network, {
  3. Client? httpClient,
  4. Map<String, String>? httpRequestHeaders,
})

Creates a WebAuth instance by automatically discovering configuration from stellar.toml.

This is the recommended way to initialize WebAuth. It fetches the stellar.toml file from the specified domain and extracts the required configuration:

  • WEB_AUTH_ENDPOINT: The authentication endpoint URL
  • SIGNING_KEY: The server's public signing key for validating challenges

Parameters:

  • domain The domain name (without protocol) hosting the stellar.toml file
  • network The Stellar network (Network.PUBLIC or Network.TESTNET)
  • httpClient Optional custom HTTP client for testing or proxy configuration
  • httpRequestHeaders Optional custom HTTP headers for requests

Returns: Future

Throws:

Example:

// Initialize from a domain's stellar.toml
final webAuth = await WebAuth.fromDomain(
  'testanchor.stellar.org',
  Network.TESTNET,
);

// Then use for authentication
final userKeyPair = KeyPair.fromSecretSeed('S...');
final token = await webAuth.jwtToken(userKeyPair.accountId, [userKeyPair]);

Example with error handling:

try {
  final webAuth = await WebAuth.fromDomain('example.com', Network.PUBLIC);
} on NoWebAuthEndpointFoundException catch (e) {
  print('Domain does not support WebAuth: ${e.domain}');
} catch (e) {
  print('Failed to initialize WebAuth: $e');
}

Implementation

static Future<WebAuth> fromDomain(
  String domain,
  Network network, {
  http.Client? httpClient,
  Map<String, String>? httpRequestHeaders,
}) async {
  final StellarToml toml = await StellarToml.fromDomain(
    domain,
    httpClient: httpClient,
    httpRequestHeaders: httpRequestHeaders,
  );

  if (toml.generalInformation.webAuthEndpoint == null) {
    throw NoWebAuthEndpointFoundException(domain);
  }
  if (toml.generalInformation.signingKey == null) {
    throw NoWebAuthServerSigningKeyFoundException(domain);
  }

  return new WebAuth(
    toml.generalInformation.webAuthEndpoint!,
    network,
    toml.generalInformation.signingKey!,
    domain,
    httpClient: httpClient,
    httpRequestHeaders: httpRequestHeaders,
  );
}