getAccount method

Future<Account?> getAccount({
  1. bool createIfnotExists = true,
})

Fetches the account information for publicKeyPem.

  • createIfnotExists defines wether to create a new account if none exists

Implementation

Future<Account?> getAccount({bool createIfnotExists = true}) async {
  var payload = {
    'onlyReturnExisting': true,
    'termsOfServiceAgreed': acceptTerms,
    'contact': contacts
  };

  var jws = await _createJWS(directories!.newAccount!, payload: payload);
  var body = json.encode(jws.toJson());
  var headers = {'Content-Type': 'application/jose+json'};
  try {
    var response = await Dio().post(
      directories!.newAccount!,
      data: body,
      options: Options(headers: headers),
    );
    nonce = response.headers.map[HEADER_REPLAY_NONCE]!.first;
    var accountUrl = '';
    if (!response.headers.isEmpty) {
      if (response.headers.map.containsKey('Location')) {
        accountUrl = response.headers.map['Location']!.first;
      }
    }
    var account = Account.fromJson(response.data);
    account.accountURL = accountUrl;
    return account;
  } on DioException catch (e) {
    if (createIfnotExists) {
      // No account found, create one
      if (e.response!.statusCode == 400) {
        nonce = e.response!.headers.map[HEADER_REPLAY_NONCE]!.first;
        return await createAccount();
      }
    }
    // TODO Handle error
    return null;
  }
}