                               {:folder folder
                                :uid    uid})
                   fetch-cb (fn [data]
+                             (schedule-read cmp-state put-fn)
                              (let [body (get (js->clj data) "body")
-                                   decrypted (decrypt-body body aes-secret)
+                                   decrypted (time (decrypt-body2 body aes-secret))
                                    msg-type (first decrypted)
                                    {:keys [msg-payload msg-meta]} (second decrypted)
                                    msg (with-meta [msg-type msg-payload]

            [cljs.core.async :refer [<!]]
            [meins.ui.shared :as shared :refer [platform-os]]
            [re-frame.core :refer [subscribe]]
            ["crypto-js" :as crypto-js]
            ["buffer" :as buffer]
            ["@matthiasn/react-native-mailcore" :as react-native-mailcore]
            ["@react-native-community/netinfo" :as net-info]
            [meins.ui.db :as uidb]
            [cljs.reader :as rdr]
            [clojure.string :as str]))

(def AES (aget crypto-js "AES"))
(def utf-8 (aget crypto-js "enc" "Utf8"))

(def Buffer (aget buffer "Buffer"))

(defn buffer-convert [from to s]
  (let [buffer (.from Buffer s from)]

          ciphertext (hex-to-utf8 cleaned)
          decrypted-bytes (.decrypt AES ciphertext secret)
          s (.toString decrypted-bytes utf-8)
          data (edn/read-string s)]
      data)
    (catch :default e (shared/alert (str "decrypt body " e)))))

(defn key-from-pw-salt
  [password n salt]
  (let [opts (clj->js {:keySize    256/32
                       :iterations n})
        key-256 (PBKDF2 password salt opts)]
    key-256))

(defn key-from-pw
  "Arbitrarily expensive password based key derivation function.
   The more expensive this function, the harder dictionary attacks
   will be."
  [password n]
  (let [salt (.random word-array 128/8)
        key-256 (key-from-pw-salt password n salt)
        salt-hex (words-to-hex salt)]
    [salt-hex key-256]))

(defn decrypt-body2 [hex-cipher secret]
  (try
    (let [[salt iv hex-cipher] (str/split hex-cipher ".")
          salt (hex-to-words salt)
          iv (hex-to-words iv)
          opts (clj->js {:iv iv})
          key-256 (key-from-pw-salt secret iterations salt)
          ciphertext (hex-to-utf8 hex-cipher)
          decrypted-bytes (.decrypt AES ciphertext key-256 opts)
          s (.toString decrypted-bytes utf-8)
          data (edn/read-string s)]
      data)
    (catch :default e (js/console.error "decrypt" e))))

(defn sync-write [{:keys [msg-type msg-payload put-fn cmp-state db-item]}]
  (when-let [secrets (:secrets @cmp-state)]
