diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index 4417f53dd6..65ee621db6 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -229,3 +229,8 @@ (def ^:const web3-send-async-callback "web3-send-async-callback") (def ^:const scan-qr-code "scan-qr-code") (def ^:const scan-qr-code-callback "scan-qr-code-callback") + +;;ipfs +(def ^:const ipfs-add-url "https://ipfs.infura.io:5001/api/v0/add") +(def ^:const ipfs-add-param-name "extension.event.edn") +(def ^:const ipfs-cat-url "https://ipfs.infura.io/ipfs/") \ No newline at end of file diff --git a/src/status_im/extensions/core.cljs b/src/status_im/extensions/core.cljs index 2890f58829..44ff86f22f 100644 --- a/src/status_im/extensions/core.cljs +++ b/src/status_im/extensions/core.cljs @@ -13,6 +13,7 @@ [status-im.ui.screens.wallet.settings.views :as settings] [status-im.i18n :as i18n] [status-im.utils.money :as money] + [status-im.constants :as constants] [status-im.ui.components.colors :as colors] [status-im.ui.screens.navigation :as navigation] [status-im.utils.handlers :as handlers] @@ -196,7 +197,7 @@ (re-frame/reg-event-fx :ipfs/cat (fn [_ [_ _ {:keys [hash on-success on-failure]}]] - {:http-raw-get (merge {:url (str "https://ipfs.infura.io/ipfs/" hash) + {:http-raw-get (merge {:url (str constants/ipfs-cat-url hash) :success-event-creator (fn [{:keys [status body]}] (if (= 200 status) @@ -207,6 +208,30 @@ {:failure-event-creator on-failure}) {:timeout-ms 5000})})) +(defn- parse-ipfs-add-response [res] + (let [{:keys [Name Hash Size]} (parse-json res)] + {:name Name + :hash Hash + :size Size})) + +(re-frame/reg-event-fx + :ipfs/add + (fn [_ [_ _ {:keys [value on-success on-failure]}]] + (let [formdata (doto + (js/FormData.) + (.append constants/ipfs-add-param-name value))] + {:http-raw-post (merge {:url constants/ipfs-add-url + :body formdata + :success-event-creator + (fn [{:keys [status body]}] + (if (= 200 status) + (on-success {:value (parse-ipfs-add-response body)}) + (when on-failure + (on-failure {:value status}))))} + (when on-failure + {:failure-event-creator on-failure}) + {:timeout-ms 5000})}))) + (re-frame/reg-event-fx :http/post (fn [_ [_ _ {:keys [url body on-success on-failure timeout]}]] @@ -458,6 +483,12 @@ :arguments {:hash :string :on-success :event :on-failure? :event}} + 'ipfs/add + {:permissions [:read] + :value :ipfs/add + :arguments {:value :string + :on-success :event + :on-failure? :event}} 'ethereum/transaction-receipt {:permissions [:read] :value :extensions/ethereum-transaction-receipt diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index 861b043a75..fe99e403ad 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -77,6 +77,18 @@ :http-post http-post) +(defn- http-raw-post [{:keys [url body response-validator success-event-creator failure-event-creator timeout-ms opts]}] + (let [on-success #(re-frame/dispatch (success-event-creator %)) + on-error #(re-frame/dispatch (failure-event-creator %)) + all-opts (assoc opts + :valid-response? response-validator + :timeout-ms timeout-ms)] + (http/raw-post url body on-success on-error all-opts))) + +(re-frame/reg-fx + :http-raw-post + http-raw-post) + (re-frame/reg-fx :request-permissions-fx (fn [options]