diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-04-25 05:29:43 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-04-25 05:29:49 +0200 |
commit | d71fe129b83f34b46136070fcf9a64865203be14 (patch) | |
tree | bd5d6bb47274f4a1e6e89a29f06498689e470505 /src | |
parent | 13718f15767db6774dbf562b6798838ad9058984 (diff) |
Store opendht identity in storage file (cached)
Diffstat (limited to 'src')
-rw-r--r-- | src/Database.cpp | 4 | ||||
-rw-r--r-- | src/DatabaseStorage.cpp | 35 |
2 files changed, 35 insertions, 4 deletions
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<u16>(); + 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<u16>(); + dht::Blob privateKeyRaw; + privateKeyRaw.resize(privateKeySize); + deserializer.extract(&privateKeyRaw[0], privateKeySize); + identity.first = make_shared<dht::crypto::PrivateKey>(privateKeyRaw); + + u16 certificateSize = deserializer.extract<u16>(); + dht::Blob certificateRaw; + certificateRaw.resize(certificateSize); + deserializer.extract(&certificateRaw[0], certificateSize); + identity.second = make_shared<dht::crypto::Certificate>(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 |