diff --git a/src/status_im/utils/keychain/core.cljs b/src/status_im/utils/keychain/core.cljs index f0c0123646..166882d74d 100644 --- a/src/status_im/utils/keychain/core.cljs +++ b/src/status_im/utils/keychain/core.cljs @@ -129,14 +129,23 @@ (comp validate string->js-array)) +(defonce generic-password (atom nil)) + (defn get-encryption-key [] - (log/debug "initializing realm encryption key...") - (.. (.getGenericPassword rn/keychain) - (then - (fn [res] - (if res - (handle-found res) - (handle-not-found)))))) + (log/debug "PERF" "initializing realm encryption key..." (.now js/Date)) + (if @generic-password + (js/Promise. + (fn [on-success _] + (on-success (security/unmask @generic-password)))) + (.. (.getGenericPassword rn/keychain) + (then + (fn [res] + (if res + (let [handled-res (handle-found res)] + (reset! generic-password + (security/->MaskedData handled-res)) + handled-res) + (handle-not-found))))))) (defn safe-get-encryption-key "Return encryption key or empty string in case invalid/empty" diff --git a/test/cljs/status_im/test/utils/keychain/core.cljs b/test/cljs/status_im/test/utils/keychain/core.cljs index e7334b4827..8a2386919e 100644 --- a/test/cljs/status_im/test/utils/keychain/core.cljs +++ b/test/cljs/status_im/test/utils/keychain/core.cljs @@ -40,7 +40,8 @@ (deftest key-is-weak (async done - (with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password (key->json weak-key)}))}] + (with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password (key->json weak-key)}))} + keychain/generic-password (atom nil)] (testing "it returns a valid key" (.. (keychain/get-encryption-key) (then (fn [_] @@ -54,7 +55,8 @@ (deftest safe-key-is-not-valid (async done - (with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password (key->json weak-key)}))}] + (with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password (key->json weak-key)}))} + keychain/generic-password (atom nil)] (testing "it returns a valid key" (.. (keychain/safe-get-encryption-key) (then (fn [k] @@ -67,7 +69,8 @@ (deftest safe-key-is-nil (async done - (with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password nil}))}] + (with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password nil}))} + keychain/generic-password (atom nil)] (testing "it returns a valid key" (.. (keychain/safe-get-encryption-key) (then (fn [k]