jwtToken method

Future<String> jwtToken(
  1. String clientAccountId,
  2. List<KeyPair> signers, {
  3. int? memo,
  4. String? homeDomain,
  5. String? clientDomain,
  6. KeyPair? clientDomainAccountKeyPair,
  7. Future<String> clientDomainSigningDelegate(
    1. String transactionXdr
    )?,
})

Get JWT token for wallet.

  • Parameter clientAccountId: The account id of the client/user to get the JWT token for.
  • Parameter signers: list of signers (keypairs including secret seed) of the client account
  • Parameter memo: optional, ID memo of the client account if muxed and accountId starts with G
  • Parameter homeDomain: optional, used for requesting the challenge depending on the home domain if needed. The web auth server may serve multiple home domains.
  • Parameter clientDomain: optional, domain of the client hosting it's stellar.toml
  • Parameter clientDomainAccountKeyPair: optional, KeyPair of the client domain account including the seed (mandatory and used for signing the transaction if client domain is provided)
  • Parameter clientDomainSigningDelegate: optional, callback function to sign the challenge transaction with the client domain account. This is a async callback because it should be possible to sign the transaction from a external source without exposing the keypair.

Implementation

Future<String> jwtToken(String clientAccountId, List<KeyPair> signers,
    {int? memo,
    String? homeDomain,
    String? clientDomain,
    KeyPair? clientDomainAccountKeyPair,
    Future<String> Function(String transactionXdr)?
        clientDomainSigningDelegate}) async {
  // get the challenge transaction from the web auth server
  String transaction =
      await getChallenge(clientAccountId, memo, homeDomain, clientDomain);

  String? clientDomainAccountId;
  if (clientDomainAccountKeyPair != null) {
    clientDomainAccountId = clientDomainAccountKeyPair.accountId;
  } else if (clientDomainSigningDelegate != null) {
    if (clientDomain == null) {
      throw MissingClientDomainException();
    }
    final StellarToml clientToml =
        await StellarToml.fromDomain(clientDomain, httpClient: httpClient);
    if (clientToml.generalInformation.signingKey == null) {
      throw NoClientDomainSigningKeyFoundException(clientDomain);
    }
    clientDomainAccountId = clientToml.generalInformation.signingKey;
  }
  // validate the transaction received from the web auth server.
  validateChallenge(transaction, clientAccountId, clientDomainAccountId,
      gracePeriod, memo); // throws if not valid

  if (clientDomainAccountKeyPair != null) {
    transaction = signTransaction(transaction, [clientDomainAccountKeyPair]);
  } else if (clientDomainSigningDelegate != null) {
    transaction = await clientDomainSigningDelegate(transaction);
  }

  List<KeyPair> mSigners = List.from(signers, growable: true);
  // sign the transaction received from the web auth server using the provided user/client keypair by parameter.
  final signedTransaction = signTransaction(transaction, mSigners);

  // request the jwt token by sending back the signed challenge transaction to the web auth server.
  final String jwtToken =
      await sendSignedChallengeTransaction(signedTransaction);

  return jwtToken;
}