Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: at_persistence _impl changes for replacing md5 #1775

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class HivePersistenceManager with HiveBase {
if (!Hive.isAdapterRegistered(AtMetaDataAdapter().typeId)) {
Hive.registerAdapter(AtMetaDataAdapter());
}
if (!Hive.isAdapterRegistered(PublicKeyHashAdapter().typeId)) {
Hive.registerAdapter(PublicKeyHashAdapter());
}

var secret = await _getHiveSecretFromFile(_atsign!, storagePath);
_boxName = AtUtils.getShaForAtSign(_atsign!);
Expand Down
182 changes: 135 additions & 47 deletions packages/at_persistence_secondary_server/lib/src/model/at_meta_data.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:at_commons/at_commons.dart';
import 'package:at_commons/at_commons.dart' as at_commons;
import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart';
import 'package:at_persistence_secondary_server/src/utils/type_adapter_util.dart';
import 'package:hive/hive.dart';
Expand Down Expand Up @@ -77,15 +77,18 @@ class AtMetaData extends HiveObject {
@HiveField(23)
String? skeEncAlgo;

@HiveField(24)
PublicKeyHash? publicKeyHash;

@override
String toString() {
return toJson().toString();
}

AtMetaData();

Metadata toCommonsMetadata() {
return Metadata()
at_commons.Metadata toCommonsMetadata() {
return at_commons.Metadata()
..ttl = ttl
..ttb = ttb
..ttr = ttr
Expand All @@ -95,6 +98,7 @@ class AtMetaData extends HiveObject {
..dataSignature = dataSignature
..sharedKeyEnc = sharedKeyEnc
..pubKeyCS = pubKeyCS
..pubKeyHash = _toCommonsPublicKeyHash(publicKeyHash)
..encoding = encoding
..encKeyName = encKeyName
..encAlgo = encAlgo
Expand All @@ -103,7 +107,17 @@ class AtMetaData extends HiveObject {
..skeEncAlgo = skeEncAlgo;
}

factory AtMetaData.fromCommonsMetadata(Metadata metadata) {
at_commons.PublicKeyHash? _toCommonsPublicKeyHash(
PublicKeyHash? publicKeyHash) =>
publicKeyHash != null
? at_commons.PublicKeyHash(
publicKeyHash.hash,
at_commons.PublicKeyHashingAlgo.values
.byName(publicKeyHash.hashingAlgo),
)
: null;

factory AtMetaData.fromCommonsMetadata(at_commons.Metadata metadata) {
var atMetadata = AtMetaData();
atMetadata
..ttl = metadata.ttl
Expand All @@ -115,6 +129,10 @@ class AtMetaData extends HiveObject {
..dataSignature = metadata.dataSignature
..sharedKeyEnc = metadata.sharedKeyEnc
..pubKeyCS = metadata.pubKeyCS
..publicKeyHash = (metadata.pubKeyHash != null
? PublicKeyHash(metadata.pubKeyHash!.hash,
metadata.pubKeyHash!.publicKeyHashingAlgo.name)
: null)
..encoding = metadata.encoding
..encKeyName = metadata.encKeyName
..encAlgo = metadata.encAlgo
Expand All @@ -136,21 +154,24 @@ class AtMetaData extends HiveObject {
map['refreshAt'] = refreshAt?.toUtc().toString();
map['status'] = status;
map['version'] = version;
map[AtConstants.ttl] = ttl;
map[AtConstants.ttb] = ttb;
map[AtConstants.ttr] = ttr;
map[AtConstants.ccd] = isCascade;
map[AtConstants.isBinary] = isBinary;
map[AtConstants.isEncrypted] = isEncrypted;
map[AtConstants.publicDataSignature] = dataSignature;
map[AtConstants.sharedKeyEncrypted] = sharedKeyEnc;
map[AtConstants.sharedWithPublicKeyCheckSum] = pubKeyCS;
map[AtConstants.encoding] = encoding;
map[AtConstants.encryptingKeyName] = encKeyName;
map[AtConstants.encryptingAlgo] = encAlgo;
map[AtConstants.ivOrNonce] = ivNonce;
map[AtConstants.sharedKeyEncryptedEncryptingKeyName] = skeEncKeyName;
map[AtConstants.sharedKeyEncryptedEncryptingAlgo] = skeEncAlgo;
map[at_commons.AtConstants.ttl] = ttl;
map[at_commons.AtConstants.ttb] = ttb;
map[at_commons.AtConstants.ttr] = ttr;
map[at_commons.AtConstants.ccd] = isCascade;
map[at_commons.AtConstants.isBinary] = isBinary;
map[at_commons.AtConstants.isEncrypted] = isEncrypted;
map[at_commons.AtConstants.publicDataSignature] = dataSignature;
map[at_commons.AtConstants.sharedKeyEncrypted] = sharedKeyEnc;
map[at_commons.AtConstants.sharedWithPublicKeyCheckSum] = pubKeyCS;
map[at_commons.AtConstants.encoding] = encoding;
map[at_commons.AtConstants.encryptingKeyName] = encKeyName;
map[at_commons.AtConstants.encryptingAlgo] = encAlgo;
map[at_commons.AtConstants.ivOrNonce] = ivNonce;
map[at_commons.AtConstants.sharedKeyEncryptedEncryptingKeyName] =
skeEncKeyName;
map[at_commons.AtConstants.sharedKeyEncryptedEncryptingAlgo] = skeEncAlgo;
map[at_commons.AtConstants.sharedWithPublicKeyHash] =
publicKeyHash?.toJson();
return map;
}

Expand Down Expand Up @@ -178,34 +199,36 @@ class AtMetaData extends HiveObject {
: (json['version'] == null)
? 0
: json['version'];
ttl = (json[AtConstants.ttl] is String)
? int.parse(json[AtConstants.ttl])
: (json[AtConstants.ttl] == null)
ttl = (json[at_commons.AtConstants.ttl] is String)
? int.parse(json[at_commons.AtConstants.ttl])
: (json[at_commons.AtConstants.ttl] == null)
? null
: json[AtConstants.ttl];
ttb = (json[AtConstants.ttb] is String)
? int.parse(json[AtConstants.ttb])
: (json[AtConstants.ttb] == null)
: json[at_commons.AtConstants.ttl];
ttb = (json[at_commons.AtConstants.ttb] is String)
? int.parse(json[at_commons.AtConstants.ttb])
: (json[at_commons.AtConstants.ttb] == null)
? null
: json[AtConstants.ttb];
ttr = (json[AtConstants.ttr] is String)
? int.parse(json[AtConstants.ttr])
: (json[AtConstants.ttr] == null)
: json[at_commons.AtConstants.ttb];
ttr = (json[at_commons.AtConstants.ttr] is String)
? int.parse(json[at_commons.AtConstants.ttr])
: (json[at_commons.AtConstants.ttr] == null)
? null
: json[AtConstants.ttr];
isCascade = json[AtConstants.ccd];
isBinary = json[AtConstants.isBinary];
isEncrypted = json[AtConstants.isEncrypted];
dataSignature = json[AtConstants.publicDataSignature];
sharedKeyEnc = json[AtConstants.sharedKeyEncrypted];
pubKeyCS = json[AtConstants.sharedWithPublicKeyCheckSum];
encoding = json[AtConstants.encoding];
encKeyName = json[AtConstants.encryptingKeyName];
encAlgo = json[AtConstants.encryptingAlgo];
ivNonce = json[AtConstants.ivOrNonce];
skeEncKeyName = json[AtConstants.sharedKeyEncryptedEncryptingKeyName];
skeEncAlgo = json[AtConstants.sharedKeyEncryptedEncryptingAlgo];

: json[at_commons.AtConstants.ttr];
isCascade = json[at_commons.AtConstants.ccd];
isBinary = json[at_commons.AtConstants.isBinary];
isEncrypted = json[at_commons.AtConstants.isEncrypted];
dataSignature = json[at_commons.AtConstants.publicDataSignature];
sharedKeyEnc = json[at_commons.AtConstants.sharedKeyEncrypted];
pubKeyCS = json[at_commons.AtConstants.sharedWithPublicKeyCheckSum];
encoding = json[at_commons.AtConstants.encoding];
encKeyName = json[at_commons.AtConstants.encryptingKeyName];
encAlgo = json[at_commons.AtConstants.encryptingAlgo];
ivNonce = json[at_commons.AtConstants.ivOrNonce];
skeEncKeyName =
json[at_commons.AtConstants.sharedKeyEncryptedEncryptingKeyName];
skeEncAlgo = json[at_commons.AtConstants.sharedKeyEncryptedEncryptingAlgo];
publicKeyHash = PublicKeyHash.fromJson(
json[at_commons.AtConstants.sharedWithPublicKeyHash]);
return this;
}

Expand All @@ -232,6 +255,7 @@ class AtMetaData extends HiveObject {
dataSignature == other.dataSignature &&
sharedKeyEnc == other.sharedKeyEnc &&
pubKeyCS == other.pubKeyCS &&
publicKeyHash == other.publicKeyHash &&
encoding == other.encoding &&
encKeyName == other.encKeyName &&
encAlgo == other.encAlgo &&
Expand Down Expand Up @@ -259,6 +283,7 @@ class AtMetaData extends HiveObject {
dataSignature.hashCode ^
sharedKeyEnc.hashCode ^
pubKeyCS.hashCode ^
publicKeyHash.hashCode ^
encoding.hashCode ^
encKeyName.hashCode ^
encAlgo.hashCode ^
Expand Down Expand Up @@ -301,13 +326,14 @@ class AtMetaDataAdapter extends TypeAdapter<AtMetaData> {
..encAlgo = fields[20]
..ivNonce = fields[21]
..skeEncKeyName = fields[22]
..skeEncAlgo = fields[23];
..skeEncAlgo = fields[23]
..publicKeyHash = fields[24];
}

@override
void write(BinaryWriter writer, AtMetaData obj) {
writer
..writeByte(24)
..writeByte(25)
..writeByte(0)
..write(obj.createdBy)
..writeByte(1)
Expand Down Expand Up @@ -355,6 +381,68 @@ class AtMetaDataAdapter extends TypeAdapter<AtMetaData> {
..writeByte(22)
..write(obj.skeEncKeyName)
..writeByte(23)
..write(obj.skeEncAlgo);
..write(obj.skeEncAlgo)
..writeByte(24)
..write(obj.publicKeyHash);
}
}

@HiveType(typeId: 11)
class PublicKeyHash extends HiveObject {
String hash;
String hashingAlgo;
PublicKeyHash(this.hash, this.hashingAlgo);
Map toJson() {
Map map = {};
map['hash'] = hash;
map['hashingAlgo'] = hashingAlgo;
return map;
}

static PublicKeyHash? fromJson(Map? json) {
if (json == null) {
return null;
}
return PublicKeyHash(json['hash'], json['hashingAlgo']);
}

@override
String toString() {
return toJson().toString();
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is PublicKeyHash &&
runtimeType == other.runtimeType &&
hash == other.hash &&
hashingAlgo == other.hashingAlgo;

@override
int get hashCode => hash.hashCode ^ hashingAlgo.hashCode;
}

class PublicKeyHashAdapter extends TypeAdapter<PublicKeyHash> {
@override
final int typeId = typeAdapterMap['PublicKeyHashAdapter'];

@override
PublicKeyHash read(BinaryReader reader) {
var numOfFields = reader.readByte();
var fields = <int, dynamic>{
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ final Map typeAdapterMap = {
'NotificationTypeAdapter': 7,
'OperationTypeAdapter': 8,
'NotificationPriorityAdapter': 9,
'MessageTypeAdapter': 10
'MessageTypeAdapter': 10,
'PublicKeyHashAdapter': 11
};
7 changes: 6 additions & 1 deletion packages/at_persistence_secondary_server/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ dependencies:
at_persistence_spec: ^2.0.14
meta: ^1.8.0

dev_dependencies:
dependency_overrides:
at_commons:
git:
url: https://github.com/atsign-foundation/at_libraries
path: packages/at_commons
ref: trunk
lints: ^2.0.1
test: ^1.22.1
coverage: ^1.6.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';
import 'dart:io';

import 'package:at_commons/at_commons.dart';
import 'package:at_commons/at_commons.dart' as at_commons;
import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart';
import 'package:collection/collection.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -135,11 +135,34 @@ void main() async {
expect(atData.metaData!.version, 1);
expect(atData.metaData!.ttl, 10000);
});
test(
'A test to verify public key hash object in metadata is set and retrieved correctly',
() async {
var hiveKeyStore = SecondaryPersistenceStoreFactory.getInstance()
.getSecondaryPersistenceStore(atSign)!
.getSecondaryKeyStore();
var key = '@bob:mobile@alice';
var value = '12345678';
var metaData = AtMetaData()
..publicKeyHash = PublicKeyHash('randomhash', 'sha512');
await hiveKeyStore?.put(
key,
AtData()
..data = value
..metaData = metaData);
var atData = await hiveKeyStore?.get(key);
expect(atData?.data, '12345678');
expect(atData?.metaData, isNotNull);
expect(atData?.metaData!.createdBy, atSign);
expect(atData?.metaData!.publicKeyHash, isNotNull);
expect(atData?.metaData!.publicKeyHash!.hash, 'randomhash');
expect(atData?.metaData!.publicKeyHash!.hashingAlgo, 'sha512');
});
tearDownAll(() async => await tearDownFunc());
});
group('A group of tests to verify at_metadata adapter', () {
test('at_meta_data adapter test', () async {
final metaData = Metadata()
final metaData = at_commons.Metadata()
..ttl = 1000
..ccd = true
..pubKeyCS = 'xyz'
Expand All @@ -156,7 +179,7 @@ void main() async {

group('Test json round-tripping', () {
test('Test without null values', () {
final Metadata startMetaData = Metadata()
final at_commons.Metadata startMetaData = at_commons.Metadata()
..ttl = 100
..ttb = 200
..ttr = 3600
Expand All @@ -180,11 +203,11 @@ void main() async {
expect(DeepCollectionEquality().equals(endMap, startMap), true);
AtMetaData endAtMetaData = AtMetaData().fromJson(endMap);
expect(endAtMetaData, startAtMetaData);
final Metadata endMetaData = endAtMetaData.toCommonsMetadata();
final at_commons.Metadata endMetaData = endAtMetaData.toCommonsMetadata();
expect(endMetaData, startMetaData);
});
test('Test with null values', () {
final Metadata startMetaData = Metadata()
final at_commons.Metadata startMetaData = at_commons.Metadata()
..ttl = null
..ttb = null
..ttr = null
Expand All @@ -208,11 +231,11 @@ void main() async {
expect(DeepCollectionEquality().equals(endMap, startMap), true);
AtMetaData endAtMetaData = AtMetaData().fromJson(endMap);
expect(endAtMetaData, startAtMetaData);
final Metadata endMetaData = endAtMetaData.toCommonsMetadata();
final at_commons.Metadata endMetaData = endAtMetaData.toCommonsMetadata();
expect(endMetaData, startMetaData);
});
test('Test with some null, some non-null values', () {
final Metadata startMetaData = Metadata()
final at_commons.Metadata startMetaData = at_commons.Metadata()
..ttl = 0
..ttb = 0
..ttr = 0
Expand All @@ -236,7 +259,7 @@ void main() async {
expect(DeepCollectionEquality().equals(endMap, startMap), true);
AtMetaData endAtMetaData = AtMetaData().fromJson(endMap);
expect(endAtMetaData, startAtMetaData);
final Metadata endMetaData = endAtMetaData.toCommonsMetadata();
final at_commons.Metadata endMetaData = endAtMetaData.toCommonsMetadata();
expect(endMetaData, startMetaData);
});
});
Expand Down