diff --git a/src/status_im/ethereum/ens.cljs b/src/status_im/ethereum/ens.cljs index 19cb86654d..07b3afa3d8 100644 --- a/src/status_im/ethereum/ens.cljs +++ b/src/status_im/ethereum/ens.cljs @@ -97,6 +97,24 @@ (fn [[name]] (cb name))})) +(defn cleanup-hash [raw-hash] + ;; NOTE: it would be better if our abi-spec/decode was able to do that + (let [;; ignore hex prefix + [_ raw-hash-rest] (split-at 2 raw-hash) + ;; the first field gives us the length of the next one in hex and has + ;; a length of 32 bytes + ;; 1 byte is 2 chars here + [next-field-length-hex raw-hash-rest] (split-at 64 raw-hash-rest) + next-field-length (* ^number (abi-spec/hex-to-number (string/join next-field-length-hex)) 2) + ;; we get the next field which is the length of the hash and is + ;; expected to be 32 bytes as well + [hash-field-length-hex raw-hash-rest] (split-at next-field-length + raw-hash-rest) + hash-field-length (* ^number (abi-spec/hex-to-number (string/join hash-field-length-hex)) 2) + ;; we get the hash + [hash _] (split-at hash-field-length raw-hash-rest)] + (str "0x" (string/join hash)))) + (defn contenthash [resolver ens-name cb] (json-rpc/eth-call @@ -106,21 +124,7 @@ :on-success (fn [raw-hash] ;; NOTE: it would be better if our abi-spec/decode was able to do that - (let [;; ignore hex prefix - [_ raw-hash-rest] (split-at 2 raw-hash) - ;; the first field gives us the length of the next one in hex and has - ;; a length of 32 bytes - ;; 1 byte is 2 chars here - [next-field-length-hex raw-hash-rest] (split-at 64 raw-hash-rest) - next-field-length (* ^number (abi-spec/hex-to-number (string/join next-field-length-hex)) 2) - ;; we get the next field which is the length of the hash and is - ;; expected to be 32 bytes as well - [hash-field-length-hex raw-hash-rest] (split-at next-field-length - raw-hash-rest) - hash-field-length (* ^number (abi-spec/hex-to-number (string/join hash-field-length-hex)) 2) - ;; we get the hash - [hash _] (split-at hash-field-length raw-hash-rest)] - (cb (string/join "0x" hash))))})) + (cb (cleanup-hash raw-hash)))})) (defn content [resolver ens-name cb] diff --git a/test/cljs/status_im/test/ethereum/ens.cljs b/test/cljs/status_im/test/ethereum/ens.cljs index 3c0d03aef7..ce11e79942 100644 --- a/test/cljs/status_im/test/ethereum/ens.cljs +++ b/test/cljs/status_im/test/ethereum/ens.cljs @@ -9,3 +9,11 @@ (ens/namehash "eth"))) (is (= "0xde9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f" (ens/namehash "foo.eth")))) + +(deftest cleanup-hash + ;; simpledapp.eth + (is (= "0xe30101701220795c1ea0ceaf4ceedc9896f14b73bb30e978e4855ee221b9a57f5a934268280e" + (ens/cleanup-hash "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000026e30101701220795c1ea0ceaf4ceedc9896f14b73bb30e978e4855ee221b9a57f5a934268280e0000000000000000000000000000000000000000000000000000"))) + ;; ethereum.eth + (is (= "0xe301017012206e70f0f72e2bcd0ad69bb2b15ee9e0f4f88693f6b2f485e48699e7d6f8dbd62a" + (ens/cleanup-hash "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000026e301017012206e70f0f72e2bcd0ad69bb2b15ee9e0f4f88693f6b2f485e48699e7d6f8dbd62a0000000000000000000000000000000000000000000000000000"))))