getKeyFor method

Future<ChaCha20Key> getKeyFor(
  1. LocalityUser localityUser, {
  2. SharedKeyRepository? sharedKeyRepository,
})

Implementation

Future<ChaCha20Key> getKeyFor(LocalityUser localityUser, {SharedKeyRepository? sharedKeyRepository}) async {
  if ( sharedKeyRepository != null ) {
    String? existingKey = await sharedKeyRepository.getKey(localityUser.publicKey, password: privateKey);

    if (  existingKey == null ) {
      BigInt sharedKey = KeyPair.computeSharedSecret(privateKey, localityUser.publicKey);
      sharedKeyRepository.upsertKey(localityUser.id, sharedKey.toRadixString(16), password: privateKey);
      return ChaCha20Key.fromBigInt(sharedKey);
    } else {
      return ChaCha20Key.fromBigInt(BigInt.parse(existingKey, radix: 16));
    }
  }
  return ChaCha20Key.fromBigInt(KeyPair.computeSharedSecret(privateKey, localityUser.publicKey));
}