|
|
@@ -6,11 +6,15 @@
|
|
|
[frontend.state :as state]
|
|
|
[clojure.string :as str]
|
|
|
[cljs.reader :as reader]
|
|
|
- ;; required for async npm module
|
|
|
- ["regenerator-runtime/runtime"]
|
|
|
- ["@kanru/rage-wasm" :as rage]
|
|
|
+ [shadow.lazy :as lazy]
|
|
|
[lambdaisland.glogi :as log]))
|
|
|
|
|
|
+(def lazy-keygen (lazy/loadable frontend.extensions.age-encryption/keygen))
|
|
|
+(def lazy-encrypt-with-x25519 (lazy/loadable frontend.extensions.age-encryption/encrypt-with-x25519))
|
|
|
+(def lazy-decrypt-with-x25519 (lazy/loadable frontend.extensions.age-encryption/decrypt-with-x25519))
|
|
|
+(def lazy-encrypt-with-user-passphrase (lazy/loadable frontend.extensions.age-encryption/encrypt-with-user-passphrase))
|
|
|
+(def lazy-decrypt-with-user-passphrase (lazy/loadable frontend.extensions.age-encryption/decrypt-with-user-passphrase))
|
|
|
+
|
|
|
(defonce age-pem-header-line "-----BEGIN AGE ENCRYPTED FILE-----")
|
|
|
(defonce age-version-line "age-encryption.org/v1")
|
|
|
|
|
|
@@ -33,9 +37,11 @@
|
|
|
(db/set-key-value repo-url :db/encryption-keys keys)
|
|
|
(db/set-key-value repo-url :db/encrypted? true)))
|
|
|
|
|
|
-(defn- generate-key-pair
|
|
|
+(defn generate-key-pair
|
|
|
[]
|
|
|
- (rage/keygen))
|
|
|
+ (let [await (p/deferred)]
|
|
|
+ (lazy/load lazy-keygen #(p/resolve! await))
|
|
|
+ (p/then await #(@lazy-keygen))))
|
|
|
|
|
|
(defn generate-key-pair-and-save!
|
|
|
[repo-url]
|
|
|
@@ -58,10 +64,13 @@
|
|
|
([repo-url content]
|
|
|
(cond
|
|
|
(encrypted-db? repo-url)
|
|
|
- (p/let [content (utf8/encode content)
|
|
|
- public-key (get-public-key repo-url)
|
|
|
- encrypted (rage/encrypt_with_x25519 public-key content true)]
|
|
|
- (utf8/decode encrypted))
|
|
|
+ (let [await (p/deferred)]
|
|
|
+ (lazy/load lazy-encrypt-with-x25519 #(p/resolve! await))
|
|
|
+ (p/let [_ await
|
|
|
+ content (utf8/encode content)
|
|
|
+ public-key (get-public-key repo-url)
|
|
|
+ encrypted (@lazy-encrypt-with-x25519 public-key content true)]
|
|
|
+ (utf8/decode encrypted)))
|
|
|
:else
|
|
|
(p/resolved content))))
|
|
|
|
|
|
@@ -72,9 +81,12 @@
|
|
|
(cond
|
|
|
(and (encrypted-db? repo-url)
|
|
|
(content-encrypted? content))
|
|
|
- (let [content (utf8/encode content)]
|
|
|
+ (let [content (utf8/encode content)
|
|
|
+ await (p/deferred)]
|
|
|
+ (lazy/load lazy-decrypt-with-x25519 #(p/resolve! await))
|
|
|
(if-let [secret-key (get-secret-key repo-url)]
|
|
|
- (p/let [decrypted (rage/decrypt_with_x25519 secret-key content)]
|
|
|
+ (p/let [_ await
|
|
|
+ decrypted (@lazy-decrypt-with-x25519 secret-key content)]
|
|
|
(utf8/decode decrypted))
|
|
|
(log/error :encrypt/empty-secret-key (str "Can't find the secret key for repo: " repo-url))))
|
|
|
:else
|
|
|
@@ -82,13 +94,19 @@
|
|
|
|
|
|
(defn encrypt-with-passphrase
|
|
|
[passphrase content]
|
|
|
- (p/let [content (utf8/encode content)
|
|
|
- encrypted (rage/encrypt_with_user_passphrase passphrase content true)]
|
|
|
- (utf8/decode encrypted)))
|
|
|
+ (let [await (p/deferred)]
|
|
|
+ (lazy/load lazy-encrypt-with-user-passphrase #(p/resolve! await))
|
|
|
+ (p/let [_ await
|
|
|
+ content (utf8/encode content)
|
|
|
+ encrypted (@lazy-encrypt-with-user-passphrase passphrase content true)]
|
|
|
+ (utf8/decode encrypted))))
|
|
|
|
|
|
-;; TODO: What if decryption failed
|
|
|
+;; ;; TODO: What if decryption failed
|
|
|
(defn decrypt-with-passphrase
|
|
|
[passphrase content]
|
|
|
- (p/let [content (utf8/encode content)
|
|
|
- decrypted (rage/decrypt_with_user_passphrase passphrase content)]
|
|
|
- (utf8/decode decrypted)))
|
|
|
+ (let [await (p/deferred)]
|
|
|
+ (lazy/load lazy-decrypt-with-user-passphrase #(p/resolve! await))
|
|
|
+ (p/let [_ await
|
|
|
+ content (utf8/encode content)
|
|
|
+ decrypted (@lazy-decrypt-with-user-passphrase passphrase content)]
|
|
|
+ (utf8/decode decrypted))))
|