From 96469ee44afb7a31157d1653c172fc11f47d675c Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 25 Apr 2018 05:29:43 +0200 Subject: Store opendht identity in storage file (cached) --- src/Database.cpp | 4 +--- src/DatabaseStorage.cpp | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Database.cpp b/src/Database.cpp index 226aa05..383cc18 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -89,8 +89,6 @@ namespace odhtdb onAddUserCallbackFunc(nullptr), databaseStorage(storageDir) { - // TODO: Cache this in storage. It takes pretty long time to generate new identity - auto identity = dht::crypto::generateIdentity(); node.run(port , { /*.dht_config = */{ /*.node_config = */{ @@ -99,7 +97,7 @@ namespace odhtdb /*.is_bootstrap = */false, /*.maintain_storage*/false }, - /*.id = */identity + /*.id = */databaseStorage.getIdentity() }, /*.threaded = */true, /*.proxy_server = */"", diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index bd98b8b..c507f02 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -24,6 +24,7 @@ namespace odhtdb }; const u64 QUARANTINE_STORAGE_TIME_MICROSECONDS = 60 * 1.0e6; + const u16 STORAGE_VERSION = 1; DatabaseStorageObject::DatabaseStorageObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) : data(_data), createdTimestamp(_timestamp), creatorPublicKey(_creatorPublicKey) @@ -62,9 +63,21 @@ namespace odhtdb if(!metadataLoaded) { sibs::SafeSerializer metadataSerializer; - metadataSerializer.add((u16)0); // Storage version + metadataSerializer.add(STORAGE_VERSION); randombytes_buf(passwordSalt, PASSWORD_SALT_LEN); metadataSerializer.add(passwordSalt, PASSWORD_SALT_LEN); + + //string passwordSaltStr((const char*)passwordSalt, PASSWORD_SALT_LEN); + identity = dht::crypto::generateIdentity(); + dht::Blob privateKeyData = identity.first->serialize(); + metadataSerializer.add((u16)privateKeyData.size()); + metadataSerializer.add(privateKeyData.data(), privateKeyData.size()); + + dht::Blob certificateData; + identity.second->pack(certificateData); + metadataSerializer.add((u16)certificateData.size()); + metadataSerializer.add(certificateData.data(), certificateData.size()); + fileAppend(metadataFilePath, { metadataSerializer.getBuffer().data(), metadataSerializer.getBuffer().size() }); } } @@ -288,9 +301,24 @@ namespace odhtdb sibs::SafeDeserializer deserializer((u8*)metadataFileContent.data, metadataFileContent.size); u16 storageVersion = deserializer.extract(); + if(storageVersion != STORAGE_VERSION) + throw std::runtime_error("Wrong storage version!"); u8 passwordSalt[PASSWORD_SALT_LEN]; deserializer.extract(passwordSalt, PASSWORD_SALT_LEN); + //string passwordSaltStr((const char*)passwordSalt, PASSWORD_SALT_LEN); + + u16 privateKeySize = deserializer.extract(); + dht::Blob privateKeyRaw; + privateKeyRaw.resize(privateKeySize); + deserializer.extract(&privateKeyRaw[0], privateKeySize); + identity.first = make_shared(privateKeyRaw); + + u16 certificateSize = deserializer.extract(); + dht::Blob certificateRaw; + certificateRaw.resize(certificateSize); + deserializer.extract(&certificateRaw[0], certificateSize); + identity.second = make_shared(certificateRaw); assert(deserializer.empty()); } @@ -524,6 +552,11 @@ namespace odhtdb return nullptr; } + const dht::crypto::Identity& DatabaseStorage::getIdentity() const + { + return identity; + } + void DatabaseStorage::update() { // TODO: Modify this to iterate backwards. Because list is sorted in order of timestamp, we can remove data in range -- cgit v1.2.3