read method

  1. @override
Future<Uint8List?> read()
override

The stored key, or null when none has been generated.

Implementation

@override
Future<Uint8List?> read() async {
  // The keyring first, and its refusal propagates: a keyring that cannot
  // answer is never a reason to use the file.
  final Uint8List? held = await keyring.read();
  if (held != null) {
    // A move interrupted between the read-back and the delete leaves both.
    // Finish it -- but only for the same key. A file holding a different
    // key may be the only copy of something still sealed under it.
    final Uint8List? old = await legacy.read();
    if (old != null && _same(old, held)) await legacy.clear();
    return held;
  }

  final Uint8List? old = await legacy.read();
  if (old == null) return null;

  await keyring.write(old);
  final Uint8List? back = await keyring.read();
  if (back == null || !_same(back, old)) {
    throw DVAppKeyStoreUnavailable(
      'the platform key store',
      'the key from ${legacyDescription ?? 'the earlier key file'} was '
          'written to the keyring and did not read back '
          '${back == null ? 'at all' : 'the same'}; the file was kept, and '
          'the move is tried again next time',
    );
  }
  // Only now, with a copy the keyring has given back.
  await legacy.clear();
  return back;
}