veilidchat/lib/entities/local_account.dart

77 lines
2.9 KiB
Dart
Raw Normal View History

2023-07-07 19:33:28 -04:00
import 'dart:typed_data';
import 'package:change_case/change_case.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
2023-07-09 00:07:21 -04:00
2023-09-26 18:46:02 -04:00
import '../proto/proto.dart' as proto;
2023-08-01 00:39:50 -04:00
import '../veilid_support/veilid_support.dart';
2023-07-07 19:33:28 -04:00
part 'local_account.freezed.dart';
part 'local_account.g.dart';
// Local account identitySecretKey is potentially encrypted with a key
// using the following mechanisms
// * None : no key, bytes are unencrypted
// * Pin : Code is a numeric pin (4-256 numeric digits) hashed with Argon2
// * Password: Code is a UTF-8 string that is hashed with Argon2
enum EncryptionKeyType {
none,
pin,
password;
2023-07-25 01:04:34 -04:00
factory EncryptionKeyType.fromJson(dynamic j) =>
EncryptionKeyType.values.byName((j as String).toCamelCase());
2023-07-26 17:42:11 -04:00
2023-08-02 21:09:28 -04:00
factory EncryptionKeyType.fromProto(proto.EncryptionKeyType p) {
// ignore: exhaustive_cases
switch (p) {
case proto.EncryptionKeyType.ENCRYPTION_KEY_TYPE_NONE:
return EncryptionKeyType.none;
case proto.EncryptionKeyType.ENCRYPTION_KEY_TYPE_PIN:
return EncryptionKeyType.pin;
case proto.EncryptionKeyType.ENCRYPTION_KEY_TYPE_PASSWORD:
return EncryptionKeyType.password;
}
throw StateError('unknown EncryptionKeyType enum value');
}
2023-07-26 17:42:11 -04:00
String toJson() => name.toPascalCase();
2023-08-02 21:09:28 -04:00
proto.EncryptionKeyType toProto() => switch (this) {
EncryptionKeyType.none =>
proto.EncryptionKeyType.ENCRYPTION_KEY_TYPE_NONE,
EncryptionKeyType.pin =>
proto.EncryptionKeyType.ENCRYPTION_KEY_TYPE_PIN,
EncryptionKeyType.password =>
proto.EncryptionKeyType.ENCRYPTION_KEY_TYPE_PASSWORD,
};
2023-07-07 19:33:28 -04:00
}
// Local Accounts are stored in a table locally and not backed by a DHT key
// and represents the accounts that have been added/imported
// on the current device.
// Stores a copy of the IdentityMaster associated with the account
// and the identitySecretKey optionally encrypted by an unlock code
// This is the root of the account information tree for VeilidChat
//
@freezed
class LocalAccount with _$LocalAccount {
const factory LocalAccount({
// The master key record for the account, containing the identityPublicKey
required IdentityMaster identityMaster,
2023-08-01 00:39:50 -04:00
// The encrypted identity secret that goes with
// the identityPublicKey with appended salt
@Uint8ListJsonConverter() required Uint8List identitySecretBytes,
2023-07-07 19:33:28 -04:00
// The kind of encryption input used on the account
required EncryptionKeyType encryptionKeyType,
// If account is not hidden, password can be retrieved via
required bool biometricsEnabled,
// Keep account hidden unless account password is entered
// (tries all hidden accounts with auth method (no biometrics))
required bool hiddenAccount,
2023-07-28 20:36:05 -04:00
// Display name for account until it is unlocked
required String name,
2023-07-07 19:33:28 -04:00
}) = _LocalAccount;
2023-07-25 01:04:34 -04:00
factory LocalAccount.fromJson(dynamic json) =>
_$LocalAccountFromJson(json as Map<String, dynamic>);
2023-07-07 19:33:28 -04:00
}