fix ens contenthash resolution

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
yenda 2020-03-31 19:53:54 +02:00 committed by Andrey Shovkoplyas
parent 3d1a1c5be9
commit 430a8fb27c
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
2 changed files with 18 additions and 7 deletions

View File

@ -7,7 +7,8 @@
(:refer-clojure :exclude [name])
(:require [clojure.string :as string]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.json-rpc :as json-rpc]))
[status-im.ethereum.json-rpc :as json-rpc]
[status-im.ethereum.abi-spec :as abi-spec]))
;; this is the addresses of ens registries for the different networks
(def ens-registries
@ -105,10 +106,21 @@
:on-success
(fn [raw-hash]
;; NOTE: it would be better if our abi-spec/decode was able to do that
(let [hash (subs raw-hash 130)
[cid & hash-and-zeros] (string/split hash "1b20")
hash (str "0x" cid "1b20" (subs (apply str hash-and-zeros) 0 64))]
(cb hash)))}))
(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))))}))
(defn content
[resolver ens-name cb]

View File

@ -32,10 +32,9 @@
(cond
(and (string/starts-with? hex "0xe40101"))
;; content type can be 2 or 4 bytes
;; we expect 1b20 (hash algorithm and hash length 64)
;; we expect 1b20 (hash algorithm keccak256 and hash length 64)
;; before the hash so we split the contenthash there
(when-let [hash (second (string/split hex "1b20"))]
(when (= 0x20 (count hash)))
{:namespace :swarm
:hash hash})
(and (= 78 (count hex))