From 5f743344e526de043868358aeccda5fd0cbf539d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Wed, 25 May 2022 11:04:46 -0400
Subject: [PATCH] value: cleanup, move sign & encrypt to cpp

---
 include/opendht/value.h | 21 ++++-----------------
 src/value.cpp           | 21 +++++++++++++++++++++
 2 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/include/opendht/value.h b/include/opendht/value.h
index 35bd1446..33d335cf 100644
--- a/include/opendht/value.h
+++ b/include/opendht/value.h
@@ -213,7 +213,7 @@ struct OPENDHT_PUBLIC Value
         }
         static Filter notFilter(Filter&& f) {
             if (not f) return [](const Value&) { return false; };
-            return [f](const Value& v) { return not f(v); };
+            return [f = std::move(f)](const Value& v) { return not f(v); };
         }
         std::vector<Sp<Value>> filter(const std::vector<Sp<Value>>& values) {
             if (not (*this))
@@ -355,12 +355,7 @@ struct OPENDHT_PUBLIC Value
      * Afterward, checkSignature() will return true and owner will
      * be set to the corresponding public key.
      */
-    inline void sign(const crypto::PrivateKey& key) {
-        if (isEncrypted())
-            throw DhtException("Can't sign encrypted data.");
-        owner = key.getSharedPublicKey();
-        signature = key.sign(getToSign());
-    }
+    void sign(const crypto::PrivateKey& key);
 
     /**
      * Check that the value is signed and that the signature matches.
@@ -371,21 +366,13 @@ struct OPENDHT_PUBLIC Value
     }
 
     inline std::shared_ptr<crypto::PublicKey> getOwner() const {
-        return std::static_pointer_cast<crypto::PublicKey>(owner);
+        return owner;
     }
 
     /**
      * Sign the value with from and returns the encrypted version for to.
      */
-    inline Value encrypt(const crypto::PrivateKey& from, const crypto::PublicKey& to) {
-        if (isEncrypted())
-            throw DhtException("Data is already encrypted.");
-        setRecipient(to.getId());
-        sign(from);
-        Value nv {id};
-        nv.setCypher(to.encrypt(getToEncrypt()));
-        return nv;
-    }
+    Value encrypt(const crypto::PrivateKey& from, const crypto::PublicKey& to);
 
     Value() {}
 
diff --git a/src/value.cpp b/src/value.cpp
index 3d0c4000..f5ef263d 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -254,6 +254,27 @@ unpackId(const Json::Value& json, const std::string& key) {
 }
 #endif
 
+void
+Value::sign(const crypto::PrivateKey& key)
+{
+    if (isEncrypted())
+        throw DhtException("Can't sign encrypted data.");
+    owner = key.getSharedPublicKey();
+    signature = key.sign(getToSign());
+}
+
+Value
+Value::encrypt(const crypto::PrivateKey& from, const crypto::PublicKey& to)
+{
+    if (isEncrypted())
+        throw DhtException("Data is already encrypted.");
+    setRecipient(to.getId());
+    sign(from);
+    Value nv {id};
+    nv.setCypher(to.encrypt(getToEncrypt()));
+    return nv;
+}
+
 bool
 Value::checkSignature()
 {
-- 
GitLab