initialize static method

Future<PushAPI> initialize({
  1. Signer? signer,
  2. PushAPIInitializeOptions? options,
})

Implementation

static Future<PushAPI> initialize({
  Signer? signer,
  PushAPIInitializeOptions? options,
}) async {
  options ??= PushAPIInitializeOptions();
  if (signer == null && options.account == null) {
    throw Exception("Either 'signer' or 'account' must be provided.");
  }

  final readMode = signer != null;

  // Get account
  // Derives account from signer if not provided
  String? derivedAccount;

  if (signer != null) {
    derivedAccount = getAccountAddress(
        getWallet(address: options.account, signer: signer));
  } else {
    derivedAccount = options.account;
  }

  if (derivedAccount == null) {
    throw Exception('Account could not be derived.');
  }

  String? decryptedPGPPrivateKey;
  String? pgpPublicKey;

  /**
     * Decrypt PGP private key
     * If user exists, decrypts the PGP private key
     * If user does not exist, creates a new user and returns the decrypted PGP private key
     */
  final user = await getUser(address: derivedAccount);

  if (readMode) {
    if (user != null && user.encryptedPrivateKey != null) {
      decryptedPGPPrivateKey = await decryptPGPKey(
        toUpgrade: options.autoUpgrade,
        progressHook: options.progressHook,
        additionalMeta: options.versionMeta,
        encryptedPGPPrivateKey: user.encryptedPrivateKey!,
        wallet: getWallet(address: options.account, signer: signer),
      );
      pgpPublicKey = user.publicKey;
    } else {
      final newUser = await createUser(
        signer: signer,
        progressHook: options.progressHook ?? (_) {},
        version: options.version ?? ENCRYPTION_TYPE.PGP_V3,
      );
      decryptedPGPPrivateKey = newUser.decryptedPrivateKey;
      pgpPublicKey = newUser.publicKey;
    }
  }

  final api = PushAPI(
    account: derivedAccount.toLowerCase(),
    env: options.env,
    signer: signer,
    decryptedPgpPvtKey: decryptedPGPPrivateKey,
    pgpPublicKey: pgpPublicKey,
    readMode: readMode,
    showHttpLog: options.showHttpLog,
  );

  return api;
}