diff --git a/src/status_im/utils/ethereum/core.cljs b/src/status_im/utils/ethereum/core.cljs index 24d93ad688..f801ae3a09 100644 --- a/src/status_im/utils/ethereum/core.cljs +++ b/src/status_im/utils/ethereum/core.cljs @@ -87,7 +87,9 @@ (if b "0x0" "0x1")) (defn hex->int [s] - (js/parseInt s 16)) + (if (= s hex-prefix) + 0 + (js/parseInt s 16))) (defn int->hex [i] (.toHex dependencies/Web3.prototype i)) @@ -95,9 +97,16 @@ (defn hex->bignumber [s] (money/bignumber (if (= s hex-prefix) 0 s))) +(defn hex->address [s] + (when (= 66 (count s)) + (normalized-address (subs s 26)))) + (defn zero-pad-64 [s] (str (apply str (drop (count s) (repeat 64 "0"))) s)) +(defn string->hex [i] + (.fromAscii dependencies/Web3.prototype i)) + (defn format-param [param] (if (number? param) (zero-pad-64 (str (hex->int param))) diff --git a/src/status_im/utils/ethereum/ens.cljs b/src/status_im/utils/ethereum/ens.cljs index c5f7a86372..4a29ba5fee 100644 --- a/src/status_im/utils/ethereum/ens.cljs +++ b/src/status_im/utils/ethereum/ens.cljs @@ -4,10 +4,12 @@ https://eips.ethereum.org/EIPS/eip-137 https://eips.ethereum.org/EIPS/eip-181 " + (:refer-clojure :exclude [name]) (:require [clojure.string :as string] [status-im.utils.ethereum.core :as ethereum])) -(def registries +;; this is the addresses of ens registries for the different networks +(def ens-registries {:mainnet "0x314159265dd8dbb310642f98f50c066173c1259b" :ropsten "0x112234455c3a32fd11230c42e7bccd4a84e02010" :rinkeby "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"}) @@ -15,59 +17,94 @@ (def default-namehash "0000000000000000000000000000000000000000000000000000000000000000") (defn namehash [s] - (if (string/blank? s) - default-namehash - (let [[label remainder] (string/split s #"\." 2)] - (ethereum/sha3 (+ (namehash remainder) (subs (ethereum/sha3 label) 2)) {:encoding "hex"})))) + (ethereum/normalized-address (if (string/blank? s) + default-namehash + (let [[label remainder] (string/split s #"\." 2)] + (ethereum/sha3 (+ (namehash remainder) + (subs (ethereum/sha3 label) 2)) + {:encoding "hex"}))))) ;; Registry contract -(defn resolver [web3 registry name cb] +(defn resolver [web3 registry ens-name cb] (ethereum/call web3 - (ethereum/call-params registry "resolver(bytes32)" (namehash name)) - #(cb %1 (ethereum/hex->string %2)))) + (ethereum/call-params registry + "resolver(bytes32)" + (namehash ens-name)) + (fn [_ address] (cb (ethereum/hex->address address))))) -(defn owner [web3 registry name cb] +(defn owner [web3 registry ens-name cb] (ethereum/call web3 - (ethereum/call-params registry "owner(bytes32)" (namehash name)) - #(cb %1 (ethereum/hex->string %2)))) + (ethereum/call-params registry + "owner(bytes32)" + (namehash ens-name)) + (fn [_ address] (cb address)))) -(defn ttl [web3 registry name cb] +(defn ttl [web3 registry ens-name cb] (ethereum/call web3 - (ethereum/call-params registry "ttl(bytes32)" (namehash name)) - #(cb %1 (ethereum/hex->int %2)))) + (ethereum/call-params registry + "ttl(bytes32)" + (namehash ens-name)) + (fn [_ ttl] (cb (ethereum/hex->int ttl))))) ;; Resolver contract +(def status-resolvers + {:mainnet "0x314159265dd8dbb310642f98f50c066173c1259b" + :ropsten "0x5FfC014343cd971B7eb70732021E26C35B744cc4" + :rinkeby "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"}) + ;; Resolver must implement EIP65 (supportsInterface). When interacting with an unknown resolver it's safer to rely on it. (def addr-hash "0x3b3b57de") -(defn addr [web3 resolver name cb] +(defn addr [web3 resolver ens-name cb] (ethereum/call web3 - (ethereum/call-params resolver "addr(bytes32)" (namehash name)) - #(cb %1 %2))) + (ethereum/call-params resolver "addr(bytes32)" (namehash ens-name)) + (fn [_ address] (cb (ethereum/hex->address address))))) -(defn content [web3 resolver name cb] +(defn content [web3 resolver ens-name cb] (ethereum/call web3 - (ethereum/call-params resolver "content(bytes32)" (namehash name)) - #(cb %1 (ethereum/hex->string %2)))) + (ethereum/call-params resolver + "content(bytes32)" + (namehash ens-name)) + (fn [_ content] (cb content #_(ethereum/hex->string content))))) (def name-hash "0x691f3431") ;; Defined by https://eips.ethereum.org/EIPS/eip-181 -(defn name [web3 resolver name cb] +(defn name [web3 resolver ens-name cb] (ethereum/call web3 - (ethereum/call-params resolver "name(bytes32)" (namehash name)) - #(cb %1 (ethereum/hex->string %2)))) + (ethereum/call-params resolver + "name(bytes32)" + (namehash ens-name)) + (fn [_ address] (cb (ethereum/hex->address address))))) -(defn text [web3 resolver name key cb] +(defn text [web3 resolver ens-name key cb] (ethereum/call web3 - (ethereum/call-params resolver "text(bytes32,string)" (namehash name) key) - #(cb %1 (ethereum/hex->string %2)))) + (ethereum/call-params resolver + "text(bytes32)" + (namehash ens-name) + (ethereum/string->hex key)) + (fn [err text] (cb err text)))) (def ABI-hash "0x2203ab56") (def pubkey-hash "0xc8690233") ;; TODO ABI, pubkey + +(comment + (let [web3-o (:web3 @re-frame.db/app-db)] + (resolver web3-o (:ropsten ens-registries) "test.stateofus.eth" println)) + + (let [web3-o (:web3 @re-frame.db/app-db)] + (addr web3-o "0x5FfC014343cd971B7eb70732021E26C35B744cc4" "test.stateofus.eth" #(println %))) + + (let [web3-o (:web3 @re-frame.db/app-db)] + (text web3-o "0x5FfC014343cd971B7eb70732021E26C35B744cc4" "test.stateofus.eth" "statusAccount" println)) + + (ethereum/call-params "0x5FfC014343cd971B7eb70732021E26C35B744cc4" + "text(bytes32,string)" + (namehash "test.stateofus.eth") + (ethereum/string->hex "statusAccount"))) diff --git a/src/status_im/utils/ethereum/stateofus.cljs b/src/status_im/utils/ethereum/stateofus.cljs index 925627d5f9..ba036cd9b9 100644 --- a/src/status_im/utils/ethereum/stateofus.cljs +++ b/src/status_im/utils/ethereum/stateofus.cljs @@ -1,4 +1,5 @@ (ns status-im.utils.ethereum.stateofus + (:refer-clojure :exclude [name]) (:require [clojure.string :as string] [status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.ens :as ens]))