[fix] ipfs CID

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
yenda 2019-05-10 02:00:10 +02:00 committed by Andrey Shovkoplyas
parent 1ff0a937ac
commit b0449f3416
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
3 changed files with 20 additions and 4 deletions

View File

@ -4,7 +4,7 @@
;; we currently use an ipfs gateway but this detail is not relevant
;; outside of this namespace
(def ^:const ipfs-add-url "https://ipfs.infura.io:5001/api/v0/add")
(def ^:const ipfs-add-url "https://ipfs.infura.io:5001/api/v0/add?cid-version=1")
(def ^:const ipfs-cat-url "https://ipfs.infura.io/ipfs/")
(fx/defn cat
@ -30,10 +30,11 @@
:size Size})))
(fx/defn add
"Add `value` on ipfs, and returns its b58 encoded CID"
[cofx {:keys [value on-success on-failure]}]
(let [formdata (doto (js/FormData.)
;; the key is ignored so there is no need to provide one
(.append nil value))]
(.append "file" value))]
{:http-raw-post (cond-> {:url ipfs-add-url
:body formdata
:timeout-ms 5000

View File

@ -11,13 +11,23 @@
(defn encode [{:keys [hash namespace ipld]}]
(when (and hash
(= namespace :ipfs)
(= 46 (count hash))
(nil? ipld))
(str "0xe301" (hex/encode (b58/decode hash)))))
(when-let [b58-hash (if (string/starts-with? hash "z")
(when (= (count hash) 49)
;; this is a CID multihash
;; the z is removed, it indicates that the
;; CID is b58 encoded
(subs hash 1))
(when (= (count hash) 46)
;; this is a deprecated simple ipfs hash
hash))]
(str "0xe301" (hex/encode (b58/decode b58-hash))))))
(defn decode [hex]
(when (and hex (not= hex "0x")
(string/starts-with? hex "0xe30101")
;; TODO properly decode the CID
;; extract the content-type using varint ns
(= 78 (count hex)))
{:namespace :ipfs
:hash (-> hex

View File

@ -23,6 +23,11 @@
{:namespace :ipfs
:hash "QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4"})
"0xe301122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f")))
(testing "encoding a valid ipfs cid"
(is (= (contenthash/encode
{:namespace :ipfs
:hash "zb2rhZfjRh2FHHB2RkHVEvL2vJnCTcu7kwRqgVsf9gpkLgteo"})
"0xe301015512202cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")))
(testing "encoding an invalid ipfs hash"
(is (nil? (contenthash/encode {:namespace :ipfs
:hash "0xe301122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e2"}))))