[feature] contenthash utility namespace

- support for ipfs only
- provides fns to encode and decode contenthashes as defined in EIP1577
- provides cat fx to retrieve contenthash

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2019-03-28 09:02:42 +01:00
parent 884a44b5df
commit 89902947b6
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
3 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,34 @@
(ns status-im.utils.contenthash
"TODO: currently we only support encoding/decoding ipfs contenthash
implementing swarm and other protocols will come later"
(:refer-clojure :exclude [cat])
(:require [alphabase.base58 :as b58]
[alphabase.hex :as hex]
[clojure.string :as string]
[status-im.ipfs.core :as ipfs]
[status-im.utils.fx :as fx]))
(defn encode [{:keys [hash namespace ipld]}]
(when (and hash
(= namespace :ipfs)
(= 46 (count hash))
(nil? ipld))
(str "0xe301" (hex/encode (b58/decode hash)))))
(defn decode [contenthash]
(when (and contenthash
(string/starts-with? contenthash "0xe3011220")
(= 74 (count contenthash)))
{:namespace :ipfs
:hash (-> contenthash
(subs 6)
hex/decode
b58/encode)}))
(fx/defn cat
[cofx {:keys [contenthash on-success on-failure]}]
(let [{:keys [namespace hash]} (decode contenthash)]
(when (= namespace :ipfs)
(ipfs/cat {:hash hash
:on-success on-success
:on-failure on-failure}))))

View File

@ -38,6 +38,7 @@
[status-im.test.utils.utils]
[status-im.test.utils.money]
[status-im.test.utils.clocks]
[status-im.test.utils.contenthash]
[status-im.test.utils.ethereum.eip55]
[status-im.test.utils.ethereum.eip681]
[status-im.test.utils.ethereum.core]
@ -111,6 +112,7 @@
'status-im.test.utils.utils
'status-im.test.utils.money
'status-im.test.utils.clocks
'status-im.test.utils.contenthash
'status-im.test.utils.ethereum.eip55
'status-im.test.utils.ethereum.eip681
'status-im.test.utils.ethereum.core

View File

@ -0,0 +1,33 @@
(ns status-im.test.utils.contenthash
(:require [cljs.test :refer-macros [deftest is testing]]
[status-im.utils.contenthash :as contenthash]))
;; we reuse the exemple from EIP-1577 but omit content type: dag-pb (0x70)
;; in the contenthash as we do not support it yet
(deftest contenthash-decode
(testing "decoding a valid ipfs hash"
(is (= (contenthash/decode "0xe301122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f")
{:namespace :ipfs
:hash "QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4"})))
(testing "decoding an invalid ipfs hash"
(is (nil? (contenthash/decode "0xe301122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e2"))))
(testing "decoding random garbage"
(is (nil? (contenthash/decode "0xabcdef1234567890"))))
(testing "decoding nil"
(is (nil? (contenthash/decode nil)))))
(deftest contenthash-encode
(testing "encoding a valid ipfs hash"
(is (= (contenthash/encode
{:namespace :ipfs
:hash "QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4"})
"0xe301122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f")))
(testing "encoding an invalid ipfs hash"
(is (nil? (contenthash/encode {:namespace :ipfs
:hash "0xe301122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e2"}))))
(testing "encoding random garbage"
(is (nil? (contenthash/encode {:namespace :ipfs
:hash "0xabcdef1234567890"}))))
(testing "encoding random garbage"
(is (nil? (contenthash/encode nil)))))