From d0e75f754096161d6c85f59426870da1f0177366 Mon Sep 17 00:00:00 2001 From: Sitaram Kalluri Date: Mon, 25 Nov 2024 10:10:18 +0530 Subject: [PATCH] feat: at_persistence_secondary_server: Add "PublicKeyHash" to the "AtMetadata" and run dart formatter --- .../CHANGELOG.md | 6 +++ .../lib/src/config/at_config.dart | 3 +- .../lib/src/keystore/hive_manager.dart | 8 +++- .../lib/src/model/at_meta_data.dart | 46 +++++++++++++++++-- .../lib/src/model/at_metadata_builder.dart | 43 +++++++++-------- .../at_notification_keystore.dart | 7 ++- .../lib/src/utils/type_adapter_util.dart | 3 +- .../pubspec.yaml | 8 ++-- .../test/at_config_test.dart | 3 +- .../test/hive_keystore_impl_test.dart | 5 +- 10 files changed, 95 insertions(+), 37 deletions(-) diff --git a/packages/at_persistence_secondary_server/CHANGELOG.md b/packages/at_persistence_secondary_server/CHANGELOG.md index cff1223c2..0a1b19255 100644 --- a/packages/at_persistence_secondary_server/CHANGELOG.md +++ b/packages/at_persistence_secondary_server/CHANGELOG.md @@ -1,3 +1,9 @@ +## 3.0.66 +- feat: Add "PublicKeyHash" to the "AtMetadata" which holds the hash value of encryption public key +- build[deps]: Upgraded the following packages: + - at_commons to v5.0.2 + - lints to v5.0.0 + - test to v1.25.8 ## 3.0.65 - fix: Modified checks in commit log keystore _alwaysIncludeInSync method to match only reserved shared_key, encryption public key and public key without namespace. diff --git a/packages/at_persistence_secondary_server/lib/src/config/at_config.dart b/packages/at_persistence_secondary_server/lib/src/config/at_config.dart index cb71d5c00..06737cca1 100644 --- a/packages/at_persistence_secondary_server/lib/src/config/at_config.dart +++ b/packages/at_persistence_secondary_server/lib/src/config/at_config.dart @@ -26,7 +26,8 @@ class AtConfig { persistenceManager = SecondaryPersistenceStoreFactory.getInstance() .getSecondaryPersistenceStore(_atSign)! .getHivePersistenceManager()!; - configKey = HiveKeyStoreHelper.getInstance().prepareKey('private:blocklist$_atSign'); + configKey = HiveKeyStoreHelper.getInstance() + .prepareKey('private:blocklist$_atSign'); } ///Returns 'success' on adding unique [blockList] into blocklist. diff --git a/packages/at_persistence_secondary_server/lib/src/keystore/hive_manager.dart b/packages/at_persistence_secondary_server/lib/src/keystore/hive_manager.dart index ca5391338..c25785eae 100644 --- a/packages/at_persistence_secondary_server/lib/src/keystore/hive_manager.dart +++ b/packages/at_persistence_secondary_server/lib/src/keystore/hive_manager.dart @@ -31,6 +31,9 @@ class HivePersistenceManager with HiveBase { if (!Hive.isAdapterRegistered(AtMetaDataAdapter().typeId)) { Hive.registerAdapter(AtMetaDataAdapter()); } + if (!Hive.isAdapterRegistered(PublicKeyHashAdapater().typeId)) { + Hive.registerAdapter(PublicKeyHashAdapater()); + } var secret = await _getHiveSecretFromFile(_atsign!, storagePath); _boxName = AtUtils.getShaForAtSign(_atsign!); @@ -113,10 +116,11 @@ class HivePersistenceManager with HiveBase { } //TODO change into to Duration and construct cron string dynamically - void scheduleKeyExpireTask(int? runFrequencyMins, {Duration? runTimeInterval, bool skipCommits = false}) { + void scheduleKeyExpireTask(int? runFrequencyMins, + {Duration? runTimeInterval, bool skipCommits = false}) { logger.finest('scheduleKeyExpireTask starting cron job.'); Schedule schedule; - if(runTimeInterval != null){ + if (runTimeInterval != null) { schedule = Schedule(seconds: runTimeInterval.inSeconds); } else { schedule = Schedule.parse('*/$runFrequencyMins * * * *'); diff --git a/packages/at_persistence_secondary_server/lib/src/model/at_meta_data.dart b/packages/at_persistence_secondary_server/lib/src/model/at_meta_data.dart index 571f80850..d48460013 100644 --- a/packages/at_persistence_secondary_server/lib/src/model/at_meta_data.dart +++ b/packages/at_persistence_secondary_server/lib/src/model/at_meta_data.dart @@ -77,6 +77,9 @@ class AtMetaData extends HiveObject { @HiveField(23) String? skeEncAlgo; + @HiveField(24) + PublicKeyHash? pubKeyHash; + @override String toString() { return toJson().toString(); @@ -100,7 +103,8 @@ class AtMetaData extends HiveObject { ..encAlgo = encAlgo ..ivNonce = ivNonce ..skeEncKeyName = skeEncKeyName - ..skeEncAlgo = skeEncAlgo; + ..skeEncAlgo = skeEncAlgo + ..pubKeyHash = pubKeyHash; } factory AtMetaData.fromCommonsMetadata(Metadata metadata) { @@ -120,7 +124,8 @@ class AtMetaData extends HiveObject { ..encAlgo = metadata.encAlgo ..ivNonce = metadata.ivNonce ..skeEncKeyName = metadata.skeEncKeyName - ..skeEncAlgo = metadata.skeEncAlgo; + ..skeEncAlgo = metadata.skeEncAlgo + ..pubKeyHash = metadata.pubKeyHash; return AtMetadataBuilder(newAtMetaData: atMetadata).build(); } @@ -151,6 +156,7 @@ class AtMetaData extends HiveObject { map[AtConstants.ivOrNonce] = ivNonce; map[AtConstants.sharedKeyEncryptedEncryptingKeyName] = skeEncKeyName; map[AtConstants.sharedKeyEncryptedEncryptingAlgo] = skeEncAlgo; + map[AtConstants.sharedWithPublicKeyHash] = pubKeyHash?.toJson(); return map; } @@ -205,6 +211,8 @@ class AtMetaData extends HiveObject { ivNonce = json[AtConstants.ivOrNonce]; skeEncKeyName = json[AtConstants.sharedKeyEncryptedEncryptingKeyName]; skeEncAlgo = json[AtConstants.sharedKeyEncryptedEncryptingAlgo]; + pubKeyHash = + PublicKeyHash.fromJson(json[AtConstants.sharedWithPublicKeyHash]); return this; } @@ -301,13 +309,14 @@ class AtMetaDataAdapter extends TypeAdapter { ..encAlgo = fields[20] ..ivNonce = fields[21] ..skeEncKeyName = fields[22] - ..skeEncAlgo = fields[23]; + ..skeEncAlgo = fields[23] + ..pubKeyHash = fields[24]; } @override void write(BinaryWriter writer, AtMetaData obj) { writer - ..writeByte(24) + ..writeByte(25) ..writeByte(0) ..write(obj.createdBy) ..writeByte(1) @@ -355,6 +364,33 @@ class AtMetaDataAdapter extends TypeAdapter { ..writeByte(22) ..write(obj.skeEncKeyName) ..writeByte(23) - ..write(obj.skeEncAlgo); + ..write(obj.skeEncAlgo) + ..writeByte(24) + ..write(obj.pubKeyHash); + } +} + +@HiveType(typeId: 11) +class PublicKeyHashAdapater extends TypeAdapter { + @override + final int typeId = typeAdapterMap['PublicKeyHashAdapater']; + + @override + PublicKeyHash read(BinaryReader reader) { + var numOfFields = reader.readByte(); + var fields = { + for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), + }; + return PublicKeyHash(fields[0] as String, fields[1] as String); + } + + @override + void write(BinaryWriter writer, PublicKeyHash obj) { + writer + ..writeByte(2) + ..writeByte(0) + ..write(obj.hash) + ..writeByte(1) + ..write(obj.hashingAlgo); } } diff --git a/packages/at_persistence_secondary_server/lib/src/model/at_metadata_builder.dart b/packages/at_persistence_secondary_server/lib/src/model/at_metadata_builder.dart index 5588a41ad..f34e1447d 100644 --- a/packages/at_persistence_secondary_server/lib/src/model/at_metadata_builder.dart +++ b/packages/at_persistence_secondary_server/lib/src/model/at_metadata_builder.dart @@ -1,3 +1,4 @@ +import 'package:at_commons/at_commons.dart'; import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart'; import 'package:at_utils/at_logger.dart'; @@ -18,26 +19,26 @@ class AtMetadataBuilder { /// ttb : Time to birth of the key. If ttb is null, atMetadata's ttb is assigned to ttb. /// ttr : Time to refresh of the key. If ttr is null, atMetadata's ttr is assigned to ttr. /// ccd : Cascade delete. If ccd is null, atMetadata's ccd is assigned to ccd. - AtMetadataBuilder({ - String? atSign, - AtMetaData? newAtMetaData, - AtMetaData? existingMetaData, - int? ttl, - int? ttb, - int? ttr, - bool? ccd, - bool? isBinary, - bool? isEncrypted, - String? dataSignature, - String? sharedKeyEncrypted, - String? publicKeyChecksum, - String? encoding, - String? encKeyName, - String? encAlgo, - String? ivNonce, - String? skeEncKeyName, - String? skeEncAlgo, - }) { + AtMetadataBuilder( + {String? atSign, + AtMetaData? newAtMetaData, + AtMetaData? existingMetaData, + int? ttl, + int? ttb, + int? ttr, + bool? ccd, + bool? isBinary, + bool? isEncrypted, + String? dataSignature, + String? sharedKeyEncrypted, + String? publicKeyChecksum, + String? encoding, + String? encKeyName, + String? encAlgo, + String? ivNonce, + String? skeEncKeyName, + String? skeEncAlgo, + PublicKeyHash? publicKeyHash}) { newAtMetaData ??= AtMetaData(); atMetaData = newAtMetaData; // createdAt indicates the date and time of the key created. @@ -85,6 +86,7 @@ class AtMetadataBuilder { ivNonce ??= newAtMetaData.ivNonce; skeEncKeyName ??= newAtMetaData.skeEncKeyName; skeEncAlgo ??= newAtMetaData.skeEncAlgo; + publicKeyHash ??= newAtMetaData.pubKeyHash; if (ttl != null && ttl >= 0) { setTTL(ttl, ttb: ttb); @@ -110,6 +112,7 @@ class AtMetadataBuilder { atMetaData.ivNonce = ivNonce; atMetaData.skeEncKeyName = skeEncKeyName; atMetaData.skeEncAlgo = skeEncAlgo; + atMetaData.pubKeyHash = publicKeyHash; } void setTTL(int? ttl, {int? ttb}) { diff --git a/packages/at_persistence_secondary_server/lib/src/notification/at_notification_keystore.dart b/packages/at_persistence_secondary_server/lib/src/notification/at_notification_keystore.dart index 9ffd0b7e5..2712c9a05 100644 --- a/packages/at_persistence_secondary_server/lib/src/notification/at_notification_keystore.dart +++ b/packages/at_persistence_secondary_server/lib/src/notification/at_notification_keystore.dart @@ -1,10 +1,10 @@ // ignore_for_file: non_constant_identifier_names +import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart'; +import 'package:at_persistence_secondary_server/src/keystore/hive_base.dart'; import 'package:at_utf7/at_utf7.dart'; import 'package:at_utils/at_utils.dart'; import 'package:hive/hive.dart'; -import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart'; -import 'package:at_persistence_secondary_server/src/keystore/hive_base.dart'; /// Class to initialize, put and get entries into [AtNotificationKeystore] class AtNotificationKeystore @@ -42,6 +42,9 @@ class AtNotificationKeystore if (!Hive.isAdapterRegistered(AtMetaDataAdapter().typeId)) { Hive.registerAdapter(AtMetaDataAdapter()); } + if (!Hive.isAdapterRegistered(PublicKeyHashAdapater().typeId)) { + Hive.registerAdapter(PublicKeyHashAdapater()); + } _register = true; } await super.openBox(_boxName); diff --git a/packages/at_persistence_secondary_server/lib/src/utils/type_adapter_util.dart b/packages/at_persistence_secondary_server/lib/src/utils/type_adapter_util.dart index 8c8da9e70..bde94e0ae 100644 --- a/packages/at_persistence_secondary_server/lib/src/utils/type_adapter_util.dart +++ b/packages/at_persistence_secondary_server/lib/src/utils/type_adapter_util.dart @@ -10,5 +10,6 @@ final Map typeAdapterMap = { 'NotificationTypeAdapter': 7, 'OperationTypeAdapter': 8, 'NotificationPriorityAdapter': 9, - 'MessageTypeAdapter': 10 + 'MessageTypeAdapter': 10, + 'PublicKeyHashAdapater': 11 }; diff --git a/packages/at_persistence_secondary_server/pubspec.yaml b/packages/at_persistence_secondary_server/pubspec.yaml index b1ea0cd42..df8914818 100644 --- a/packages/at_persistence_secondary_server/pubspec.yaml +++ b/packages/at_persistence_secondary_server/pubspec.yaml @@ -1,6 +1,6 @@ name: at_persistence_secondary_server description: A Dart library with the implementation classes for the persistence layer of the secondary server. -version: 3.0.65 +version: 3.0.66 repository: https://github.com/atsign-foundation/at_server homepage: https://docs.atsign.com/ @@ -14,13 +14,13 @@ dependencies: crypto: ^3.0.3 uuid: ^3.0.6 at_utf7: ^1.0.0 - at_commons: ^5.0.1 + at_commons: ^5.0.2 at_utils: ^3.0.19 at_persistence_spec: ^2.0.14 meta: ^1.8.0 dev_dependencies: - lints: ^2.0.1 - test: ^1.22.1 + lints: ^5.0.0 + test: ^1.25.8 coverage: ^1.6.1 collection: ^1.17.1 diff --git a/packages/at_persistence_secondary_server/test/at_config_test.dart b/packages/at_persistence_secondary_server/test/at_config_test.dart index 3fc81677a..1819b007c 100644 --- a/packages/at_persistence_secondary_server/test/at_config_test.dart +++ b/packages/at_persistence_secondary_server/test/at_config_test.dart @@ -137,7 +137,8 @@ Future tearDownFunc() async { // closes the instance of hive keystore await SecondaryPersistenceStoreFactory.getInstance() .getSecondaryPersistenceStore('@test_user_1')! - .getHivePersistenceManager()?.close(); + .getHivePersistenceManager() + ?.close(); var isExists = await Directory('test/hive/').exists(); if (isExists) { diff --git a/packages/at_persistence_secondary_server/test/hive_keystore_impl_test.dart b/packages/at_persistence_secondary_server/test/hive_keystore_impl_test.dart index 60d125010..1eaf54b88 100644 --- a/packages/at_persistence_secondary_server/test/hive_keystore_impl_test.dart +++ b/packages/at_persistence_secondary_server/test/hive_keystore_impl_test.dart @@ -84,7 +84,8 @@ void main() async { ..encAlgo = 'AES/CTR/PKCS7Padding' ..ivNonce = 'someIvNonce' ..skeEncKeyName = 'someSkeEncKeyName' - ..skeEncAlgo = 'someSkeEncAlgo'; + ..skeEncAlgo = 'someSkeEncAlgo' + ..pubKeyHash = PublicKeyHash('someHashValue', 'sha512'); var atMetaData = AtMetaData.fromCommonsMetadata(commonsMetadata); atData.metaData = atMetaData; await keyStore.create(key, atData); @@ -92,6 +93,8 @@ void main() async { var dataFromHive = await (keyStore.get(key)); expect(dataFromHive?.data, 'india'); expect(dataFromHive?.metaData, atMetaData); + expect(dataFromHive?.metaData?.pubKeyHash?.hash, 'someHashValue'); + expect(dataFromHive?.metaData?.pubKeyHash?.hashingAlgo, 'sha512'); var updateData = AtData(); var updateMetaData =