             [icecap.schema :refer [check-plan]]
             [icecap.handlers.core :as h]
             [icecap.crypto :as crypto]
-            [icecap.store.api :refer [create! retrieve delete!]]
+            [icecap.store.api :as store]
             [taoensso.timbre :refer [info spy]]
             [manifold.deferred :as md]
             [manifold.stream :as ms]))

  "Executes a capability."
  [cap {:keys [store kdf scheme]}]
  (let [{:keys [index cap-key]} (crypto/derive kdf cap)]
    (md/chain (retrieve store index)
              (fn [blob]
                (->> (crypto/decrypt scheme cap-key blob)
                     (nippy/thaw)

      (let [cap (crypto/make-cap)
            {:keys [index cap-key]} (crypto/derive kdf cap)
            blob (crypto/encrypt scheme cap-key (nippy/freeze plan))]
        (md/chain (store/create! store index blob)
                  (constantly {:cap cap})))
      (md/success-deferred {:error error}))))

