persistKeys method

Future<bool> persistKeys(
  1. String atSign
)

Returns true on persisting keys into keystore.

Implementation

Future<bool> persistKeys(String atSign) async {
  // Get keys from KeyChain manager
  String? pkamPrivateKey = await keyChainManager.getPkamPrivateKey(atSign);
  String? pkamPublicKey = await keyChainManager.getPkamPublicKey(atSign);
  String? encryptPrivateKey =
      await keyChainManager.getEncryptionPrivateKey(atSign);
  String? encryptPublicKey =
      await keyChainManager.getEncryptionPublicKey(atSign);
  String? selfEncryptionKey =
      await keyChainManager.getSelfEncryptionAESKey(atSign);

  // If the keys are missed, the authentication and encryption/decryption of data
  // does not work. Hence first throwing exception without going further.
  if (pkamPrivateKey == null || pkamPrivateKey.isEmpty) {
    throw (OnboardingStatus.PKAM_PRIVATE_KEY_NOT_FOUND);
  }
  if (pkamPublicKey == null || pkamPublicKey.isEmpty) {
    throw (OnboardingStatus.PKAM_PUBLIC_KEY_NOT_FOUND);
  }
  if (encryptPrivateKey == null || encryptPrivateKey.isEmpty) {
    throw (OnboardingStatus.ENCRYPTION_PRIVATE_KEY_NOT_FOUND);
  }
  if (encryptPublicKey == null || encryptPublicKey.isEmpty) {
    throw (OnboardingStatus.ENCRYPTION_PUBLIC_KEY_NOT_FOUND);
  }
  if (selfEncryptionKey == null || selfEncryptionKey.isEmpty) {
    throw (OnboardingStatus.SELF_ENCRYPTION_KEY_NOT_FOUND);
  }

  //Store keys into local secondary.
  await _atClient!
      .getLocalSecondary()!
      .putValue(AtConstants.atPkamPublicKey, pkamPublicKey);

  await _atClient!
      .getLocalSecondary()!
      .putValue(AtConstants.atPkamPrivateKey, pkamPrivateKey);

  await _atClient!
      .getLocalSecondary()!
      .putValue(AtConstants.atEncryptionPrivateKey, encryptPrivateKey);

  var updateBuilder = UpdateVerbBuilder()
    ..atKey = (AtKey()
      ..key = 'publickey'
      ..sharedBy = atSign
      ..metadata = (Metadata()
        ..ttr = -1
        ..isPublic = true))
    ..value = encryptPublicKey;

  await _atClient!
      .getLocalSecondary()!
      .executeVerb(updateBuilder, sync: true);

  await _atClient!
      .getLocalSecondary()!
      .putValue(AtConstants.atEncryptionSelfKey, selfEncryptionKey);

  // Verify if keys are added to local storage.
  var result = await _getKeysFromLocalSecondary(atSign);
  return result;
}