decryptWithBytes static method

Future<UserSecretKey> decryptWithBytes(
  1. Map<String, dynamic> keyFileObject,
  2. Uint8List passwordBytes, {
  3. int? addressIndex,
})

Decrypts a keystore using a mutable password buffer that the caller can zero after use (M2-9).

Parameters

  • keyFileObject - Parsed keystore JSON
  • passwordBytes - Password as UTF-8 bytes (caller owns and may zero)
  • addressIndex - Account index for mnemonic-kind keystores

Implementation

static Future<UserSecretKey> decryptWithBytes(
  Map<String, dynamic> keyFileObject,
  Uint8List passwordBytes, {
  int? addressIndex,
}) async {
  final String kindStr =
      optionalAs<String>(keyFileObject['kind'], 'kind') ??
      UserWalletKind.secretKey.value;
  final UserWalletKind kind = UserWalletKind.fromString(kindStr);

  if (kind == UserWalletKind.secretKey) {
    if (addressIndex != null) {
      throw ArgumentError(
        'addressIndex must not be provided when kind == "secretKey"',
      );
    }
    return _decryptSecretKeyWithBytes(keyFileObject, passwordBytes);
  }

  if (kind == UserWalletKind.mnemonic) {
    final Mnemonic mnemonic = _decryptMnemonicWithBytes(
      keyFileObject,
      passwordBytes,
    );
    return mnemonic.deriveKey(addressIndex: addressIndex ?? 0);
  }

  throw ArgumentError('Unknown kind: $kindStr');
}