getKeychain method

Future<Keychain> getKeychain(
  1. String seed
)

Retrieve a keychain by using keychain access seed @param {String} seed Keychain's access seed

Implementation

Future<Keychain> getKeychain(String seed) async {
  final keypair = deriveKeyPair(seed, 0);
  final accessKeychainAddress = deriveAddress(seed, 1);

  try {
    final ownershipsMap =
        await getTransactionOwnerships([accessKeychainAddress]);
    if (ownershipsMap[accessKeychainAddress] == null ||
        ownershipsMap[accessKeychainAddress]!.isEmpty) {
      throw "Keychain doesn't exists";
    }

    final ownership = ownershipsMap[accessKeychainAddress]![0];
    final authorizedPublicKey = ownership.authorizedPublicKeys.firstWhere(
      (AuthorizedKey authKey) =>
          authKey.publicKey!.toUpperCase() ==
          uint8ListToHex(
            Uint8List.fromList(keypair.publicKey!),
          ).toUpperCase(),
    );

    final aesKey =
        ecDecrypt(authorizedPublicKey.encryptedSecretKey, keypair.privateKey);
    final keychainAddress = aesDecrypt(ownership.secret, aesKey);
    log(
      'keychainAddress (getKeychain): ${uint8ListToHex(keychainAddress)}',
      logsActivation: logsActivation,
    );

    final lastTransactionKeychainMap = await getLastTransaction(
      [uint8ListToHex(keychainAddress)],
      request: 'address',
    );

    final ownerships2Map = await getTransactionOwnerships(
      [
        lastTransactionKeychainMap[uint8ListToHex(keychainAddress)]!
            .address!
            .address!,
      ],
    );
    final ownership2 = ownerships2Map[
        lastTransactionKeychainMap[uint8ListToHex(keychainAddress)]!
            .address!
            .address!]![0];

    final authorizedPublicKey2 = ownership2.authorizedPublicKeys.firstWhere(
      (AuthorizedKey publicKey) =>
          publicKey.publicKey!.toUpperCase() ==
          uint8ListToHex(
            Uint8List.fromList(keypair.publicKey!),
          ).toUpperCase(),
    );
    final aesKey2 = ecDecrypt(
      authorizedPublicKey2.encryptedSecretKey,
      keypair.privateKey,
    );
    final keychain = aesDecrypt(ownership2.secret, aesKey2);
    return decodeKeychain(keychain);
  } catch (e) {
    if (e.toString() == "Keychain doesn't exists") {
      throw Exception(e.toString());
    } else {
      throw ArchethicConnectionException(e.toString());
    }
  }
}