From 6cf5244bc514042fe419fffe1cd26a7f5e3c778f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Thu, 16 Apr 2020 21:42:45 +0200 Subject: Remove optional from load result Makes loading of data well-defined for arbitrary object contents. Introduce zero reference and object to represent missing or mismatched parts. --- src/pubkey.h | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'src/pubkey.h') diff --git a/src/pubkey.h b/src/pubkey.h index c922dc7..7b80752 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -18,7 +18,7 @@ class PublicKey key(key, EVP_PKEY_free) {} friend class SecretKey; public: - static optional load(const Ref &); + static PublicKey load(const Ref &); Ref store(const Storage &) const; const shared_ptr key; @@ -49,7 +49,7 @@ private: class Signature { public: - static optional load(const Ref &); + static Signature load(const Ref &); Ref store(const Storage &) const; bool verify(const Ref &) const; @@ -67,7 +67,7 @@ template class Signed { public: - static optional> load(const Ref &); + static Signed load(const Ref &); Ref store(const Storage &) const; bool isSignedBy(const Stored &) const; @@ -90,23 +90,20 @@ Stored> SecretKey::sign(const Stored & val) const } template -optional> Signed::load(const Ref & ref) +Signed Signed::load(const Ref & ref) { - auto rec = ref->asRecord(); - if (!rec) - return nullopt; - - auto data = rec->item("SDATA").as(); - if (!data) - return nullopt; - - vector> sigs; - for (auto item : rec->items("sig")) - if (auto sig = item.as()) - if (sig.value()->verify(data.value().ref())) - sigs.push_back(sig.value()); - - return Signed(*data, sigs); + if (auto rec = ref->asRecord()) + if (auto data = rec->item("SDATA").as()) { + vector> sigs; + for (auto item : rec->items("sig")) + if (auto sig = item.as()) + if (sig.value()->verify(data.value().ref())) + sigs.push_back(sig.value()); + + return Signed(*data, sigs); + } + + return Signed(Stored::load(ref.storage().zref()), {}); } template @@ -137,7 +134,7 @@ class PublicKexKey key(key, EVP_PKEY_free) {} friend class SecretKexKey; public: - static optional load(const Ref &); + static PublicKexKey load(const Ref &); Ref store(const Storage &) const; const shared_ptr key; -- cgit v1.2.3