Skip to content

Commit 4f77ab9

Browse files
authored
Merge pull request #426 from simplito/feat/json_struct_helpers
Feat/json struct helpers
2 parents 5b35921 + ddba68e commit 4f77ab9

11 files changed

Lines changed: 31 additions & 22 deletions

File tree

endpoint/core/include/privmx/endpoint/core/encryptors/DataEncryptor.hpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,14 @@ class DataEncryptor : public DataEncryptorBase<T>
9191
{
9292
public:
9393
std::string encrypt(const T& data, const std::string& key) {
94-
auto buffer = utils::Utils::stringifyVar(data.toJSON());
95-
return utils::Base64::from(privmx::crypto::CryptoPrivmx::privmxEncrypt(privmx::crypto::CryptoPrivmx::privmxOptAesWithSignature(), buffer, key));
94+
return utils::Base64::from(privmx::crypto::CryptoPrivmx::privmxEncrypt(privmx::crypto::CryptoPrivmx::privmxOptAesWithSignature(), data.serialize(), key));
9695
}
9796
std::string encrypt(const T& data, const EncKey& encKey) {
9897
return encrypt(data, encKey.key);
9998
}
10099

101100
Pson::BinaryString sign(const T& data, const privmx::crypto::PrivateKey& privKey) {
102-
auto buffer = utils::Utils::stringifyVar(data.toJSON());
101+
auto buffer = data.serialize();
103102
auto signature = privKey.signToCompactSignatureWithHash(buffer);
104103
Pson::BinaryString plain;
105104
plain.push_back(1);
@@ -109,15 +108,12 @@ class DataEncryptor : public DataEncryptorBase<T>
109108
}
110109

111110
Pson::BinaryString getSign(const T& data, const privmx::crypto::PrivateKey& privKey) {
112-
auto buffer = utils::Utils::stringifyVar(data.toJSON());
113-
return Pson::BinaryString(privKey.signToCompactSignatureWithHash(buffer));
111+
return Pson::BinaryString(privKey.signToCompactSignatureWithHash(data.serialize()));
114112
}
115113

116114
T decrypt(const std::string& data, const std::string& key) {
117115
auto decrypted = privmx::crypto::CryptoPrivmx::privmxDecrypt(true, utils::Base64::toString(data), key);
118-
Poco::JSON::Parser parser;
119-
auto var = parser.parse(decrypted);
120-
return T::fromJSON(var);
116+
return T::deserialize(decrypted);
121117
}
122118
T decrypt(const std::string& data, const EncKey& encKey) {
123119
return decrypt(data, encKey.key);
@@ -127,7 +123,7 @@ class DataEncryptor : public DataEncryptorBase<T>
127123
Pson::BinaryString plain = privmx::crypto::CryptoPrivmx::privmxDecrypt(true, utils::Base64::toString(data), key);
128124
Pson::BinaryString signature, data_buf;
129125
std::tie(signature, data_buf) = this->extractSignAndDataBuff(plain);
130-
return {signature, T::fromJSON(privmx::utils::Utils::parseJsonObject(data_buf)), data_buf};
126+
return {signature, T::deserialize(data_buf), data_buf};
131127
}
132128
std::tuple<Pson::BinaryString, T, Pson::BinaryString> decryptAndGetSign(const std::string& data, const EncKey& encKey) {
133129
return decryptAndGetSign(data, encKey.key);

endpoint/core/src/Subscriber.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ std::vector<std::string> Subscriber::subscribeFor(const std::vector<std::string>
7777
model.channels.push_back(subscriptionQueryString);
7878
}
7979
}
80-
LOG_INFO("Subscriber:subscribeFor channels:" + privmx::utils::Utils::stringifyVar(model.toJSON()));
80+
LOG_INFO("Subscriber:subscribeFor channels:" + model.serialize());
8181
auto requestResult = _gateway->request("subscribeToChannels", model.toJSON(), {.channel_type = rpc::ChannelType::WEBSOCKET});
8282
server::SubscribeToChannelsResult value = privmx::endpoint::core::server::SubscribeToChannelsResult::fromJSON(requestResult);
8383
LOG_TIME_DEBUG_CHECKPOINT(Subscriber:subscribeFor, "dataRecived")

endpoint/core/src/encryptors/DIO/DIOEncryptorV1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ std::string DIOEncryptorV1::signAndEncode(const ExpandedDataIntegrityObject& dio
4545
} else {
4646
dioJSON.bridgeIdentity = std::nullopt;
4747
}
48-
return _dataEncryptor.encode(_dataEncryptor.signAndPackDataWithSignature(core::Buffer::from(privmx::utils::Utils::stringify(dioJSON.toJSON())), authorKey));
48+
return _dataEncryptor.encode(_dataEncryptor.signAndPackDataWithSignature(core::Buffer::from(dioJSON.serialize()), authorKey));
4949
}
5050

5151
ExpandedDataIntegrityObject DIOEncryptorV1::decodeAndVerify(const std::string& signedDio) {

endpoint/core/src/encryptors/module/ModuleDataEncryptorV5.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dynamic::EncryptedModuleDataV5 ModuleDataEncryptorV5::encrypt(const ModuleDataTo
3434
result.privateMeta = _dataEncryptor.signAndEncryptAndEncode(kvdbData.privateMeta, authorPrivateKey, encryptionKey);
3535
fieldChecksums.insert(std::make_pair("privateMeta",privmx::crypto::Crypto::sha256(result.privateMeta)));
3636
dynamic::ModuleInternalMetaV5 internalMeta{.secret=kvdbData.internalMeta.secret, .resourceId=kvdbData.internalMeta.resourceId, .randomId=kvdbData.internalMeta.randomId};
37-
result.internalMeta = _dataEncryptor.signAndEncryptAndEncode(Buffer::from(utils::Utils::stringifyVar(internalMeta.toJSON())), authorPrivateKey, encryptionKey);
37+
result.internalMeta = _dataEncryptor.signAndEncryptAndEncode(Buffer::from(internalMeta.serialize()), authorPrivateKey, encryptionKey);
3838
fieldChecksums.insert(std::make_pair("internalMeta",privmx::crypto::Crypto::sha256(result.internalMeta)));
3939
result.authorPubKey = authorPrivateKey.getPublicKey().toBase58DER();
4040
ExpandedDataIntegrityObject expandedDio = {kvdbData.dio, .structureVersion=5, .fieldChecksums=fieldChecksums};

endpoint/event/src/EventApiImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void EventApiImpl::emitEventEx(const std::string& contextId, const std::vector<c
196196
model.channel = channelName;
197197
model.users = _eventKeyProvider.prepareKeysList(users, encryptionKey);
198198
_serverApi.contextSendCustomEvent(model);
199-
PRIVMX_DEBUG("EventApiImpl", "emitEventEx", privmx::utils::Utils::stringify(model.toJSON(), true));
199+
PRIVMX_DEBUG("EventApiImpl", "emitEventEx", model.serialize(), true);
200200
}
201201

202202
void EventApiImpl::validateChannelName(const std::string& channelName) {

endpoint/inbox/src/InboxApiImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ store::FileMetaToEncryptV4 InboxApiImpl::prepareMeta(const privmx::endpoint::inb
575575
.publicMeta = commitFileInfo.publicMeta,
576576
.privateMeta = commitFileInfo.privateMeta,
577577
.fileSize = commitFileInfo.size,
578-
.internalMeta = core::Buffer::from(utils::Utils::stringifyVar(internalFileMeta.toJSON()))
578+
.internalMeta = core::Buffer::from(internalFileMeta.serialize())
579579
};
580580
return fileMetaToEncrypt;
581581
}

endpoint/inbox/src/encryptors/inbox/InboxDataProcessorV5.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ server::InboxData InboxDataProcessorV5::packForServer(const InboxDataProcessorMo
4545
.resourceId = plainData.privateData.internalMeta.resourceId,
4646
.randomId = plainData.privateData.internalMeta.randomId,
4747
};
48-
serverPrivateData.internalMeta = _dataEncryptor.signAndEncryptAndEncode(core::Buffer::from(utils::Utils::stringifyVar(internalMetaObj.toJSON())), authorPrivateKey, inboxKey);
48+
serverPrivateData.internalMeta = _dataEncryptor.signAndEncryptAndEncode(core::Buffer::from(internalMetaObj.serialize()), authorPrivateKey, inboxKey);
4949
privateDataMapOfDataSha256.insert(std::make_pair("internalMeta", privmx::crypto::Crypto::sha256(serverPrivateData.internalMeta)));
5050
serverPrivateData.authorPubKey = authorPubKeyECC;
5151
core::ExpandedDataIntegrityObject privateDataExpandedDio = {plainData.privateData.dio, .structureVersion=InboxDataSchema::Version::VERSION_5, .fieldChecksums=privateDataMapOfDataSha256};

endpoint/store/src/StoreApiImpl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ std::string StoreApiImpl::storeFileFinalizeWriteRequest(const std::shared_ptr<Fi
611611
.publicMeta = handle->getPublicMeta(),
612612
.privateMeta = handle->getPrivateMeta(),
613613
.fileSize = handle->getSize(),
614-
.internalMeta = core::Buffer::from(utils::Utils::stringifyVar(internalFileMeta.toJSON()))
614+
.internalMeta = core::Buffer::from(internalFileMeta.serialize())
615615
};
616616
encryptedMetaVar = _fileMetaEncryptorV4.encrypt(fileMeta, _userPrivKey, key.key).toJSON();
617617
break;
@@ -626,7 +626,7 @@ std::string StoreApiImpl::storeFileFinalizeWriteRequest(const std::shared_ptr<Fi
626626
store::FileMetaToEncryptV5 fileMeta {
627627
.publicMeta = handle->getPublicMeta(),
628628
.privateMeta = handle->getPrivateMeta(),
629-
.internalMeta = core::Buffer::from(utils::Utils::stringifyVar(internalFileMeta.toJSON())),
629+
.internalMeta = core::Buffer::from(internalFileMeta.serialize()),
630630
.dio = fileDIO
631631
};
632632
auto encryptedMeta = _fileMetaEncryptorV5.encrypt(fileMeta, _userPrivKey, key.key);
@@ -1136,7 +1136,7 @@ File StoreApiImpl::convertStoreFileMetaV1ToFile(server::File file, dynamic::comp
11361136
return convertServerFileToLibFile(
11371137
file,
11381138
core::Buffer(),
1139-
core::Buffer::from(utils::Utils::stringifyVar(storeFileMeta.toJSON())),
1139+
core::Buffer::from(storeFileMeta.serialize()),
11401140
storeFileMeta.size,
11411141
storeFileMeta.author.pubKey,
11421142
storeFileMeta.statusCode,
@@ -1437,7 +1437,7 @@ void StoreApiImpl::updateFileMeta(const std::string& fileId, const core::Buffer&
14371437
}
14381438
Poco::Dynamic::Var encryptedMetaVar;
14391439
auto fileInternalMeta = validateDecryptFileInternalMeta(file, storeToModuleKeys(store));
1440-
auto internalMeta = core::Buffer::from(utils::Utils::stringifyVar(fileInternalMeta.toJSON()));
1440+
auto internalMeta = core::Buffer::from(fileInternalMeta.serialize());
14411441
switch (getStoreEntryDataStructureVersion(store.data.back())) {
14421442
case StoreDataSchema::Version::UNKNOWN:
14431443
throw UnknowStoreFormatException();

endpoint/store/src/encryptors/file/FileMetaEncryptor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Poco::Dynamic::Var FileMetaEncryptor::encrypt(const FileInfo& fileInfo, const Fi
3131
.publicMeta = fileMeta.publicMeta,
3232
.privateMeta = fileMeta.privateMeta,
3333
.fileSize = fileMeta.internalFileMeta.size,
34-
.internalMeta = core::Buffer::from(utils::Utils::stringifyVar(fileMeta.internalFileMeta.toJSON()))
34+
.internalMeta = core::Buffer::from(fileMeta.internalFileMeta.serialize())
3535
},
3636
_userPrivKey,
3737
encKey.key
@@ -41,7 +41,7 @@ Poco::Dynamic::Var FileMetaEncryptor::encrypt(const FileInfo& fileInfo, const Fi
4141
store::FileMetaToEncryptV5{
4242
.publicMeta = fileMeta.publicMeta,
4343
.privateMeta = fileMeta.privateMeta,
44-
.internalMeta = core::Buffer::from(utils::Utils::stringifyVar(fileMeta.internalFileMeta.toJSON())),
44+
.internalMeta = core::Buffer::from(fileMeta.internalFileMeta.serialize()),
4545
.dio = createDIO(fileInfo)
4646
},
4747
_userPrivKey,

endpoint/store/src/encryptors/file/FileMetaEncryptorV1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919
using namespace privmx::endpoint::store;
2020

2121
std::string FileMetaEncryptorV1::signAndEncrypt(const dynamic::compat_v1::StoreFileMeta& data, const privmx::crypto::PrivateKey& priv, const std::string& key) {
22-
auto buffer {utils::Utils::stringify(data.toJSON())};
22+
auto buffer {data.serialize()};
2323
auto signature = priv.signToCompactSignatureWithHash(buffer);
2424
std::string plain;
2525
plain.push_back(1);

0 commit comments

Comments
 (0)