small fixes

This commit is contained in:
Eric Dvorsak 2018-07-31 00:13:35 +02:00
parent 9bbea373f3
commit e81704a0b0
No known key found for this signature in database
GPG Key ID: 932AC1CE5F05DE0C
3 changed files with 74 additions and 27 deletions

View File

@ -87,7 +87,9 @@
(if b "0x0" "0x1")) (if b "0x0" "0x1"))
(defn hex->int [s] (defn hex->int [s]
(js/parseInt s 16)) (if (= s hex-prefix)
0
(js/parseInt s 16)))
(defn int->hex [i] (defn int->hex [i]
(.toHex dependencies/Web3.prototype i)) (.toHex dependencies/Web3.prototype i))
@ -95,9 +97,16 @@
(defn hex->bignumber [s] (defn hex->bignumber [s]
(money/bignumber (if (= s hex-prefix) 0 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] (defn zero-pad-64 [s]
(str (apply str (drop (count s) (repeat 64 "0"))) s)) (str (apply str (drop (count s) (repeat 64 "0"))) s))
(defn string->hex [i]
(.fromAscii dependencies/Web3.prototype i))
(defn format-param [param] (defn format-param [param]
(if (number? param) (if (number? param)
(zero-pad-64 (str (hex->int param))) (zero-pad-64 (str (hex->int param)))

View File

@ -4,10 +4,12 @@
https://eips.ethereum.org/EIPS/eip-137 https://eips.ethereum.org/EIPS/eip-137
https://eips.ethereum.org/EIPS/eip-181 https://eips.ethereum.org/EIPS/eip-181
" "
(:refer-clojure :exclude [name])
(:require [clojure.string :as string] (:require [clojure.string :as string]
[status-im.utils.ethereum.core :as ethereum])) [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" {:mainnet "0x314159265dd8dbb310642f98f50c066173c1259b"
:ropsten "0x112234455c3a32fd11230c42e7bccd4a84e02010" :ropsten "0x112234455c3a32fd11230c42e7bccd4a84e02010"
:rinkeby "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"}) :rinkeby "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"})
@ -15,59 +17,94 @@
(def default-namehash "0000000000000000000000000000000000000000000000000000000000000000") (def default-namehash "0000000000000000000000000000000000000000000000000000000000000000")
(defn namehash [s] (defn namehash [s]
(if (string/blank? s) (ethereum/normalized-address (if (string/blank? s)
default-namehash default-namehash
(let [[label remainder] (string/split s #"\." 2)] (let [[label remainder] (string/split s #"\." 2)]
(ethereum/sha3 (+ (namehash remainder) (subs (ethereum/sha3 label) 2)) {:encoding "hex"})))) (ethereum/sha3 (+ (namehash remainder)
(subs (ethereum/sha3 label) 2))
{:encoding "hex"})))))
;; Registry contract ;; Registry contract
(defn resolver [web3 registry name cb] (defn resolver [web3 registry ens-name cb]
(ethereum/call web3 (ethereum/call web3
(ethereum/call-params registry "resolver(bytes32)" (namehash name)) (ethereum/call-params registry
#(cb %1 (ethereum/hex->string %2)))) "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 web3
(ethereum/call-params registry "owner(bytes32)" (namehash name)) (ethereum/call-params registry
#(cb %1 (ethereum/hex->string %2)))) "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 web3
(ethereum/call-params registry "ttl(bytes32)" (namehash name)) (ethereum/call-params registry
#(cb %1 (ethereum/hex->int %2)))) "ttl(bytes32)"
(namehash ens-name))
(fn [_ ttl] (cb (ethereum/hex->int ttl)))))
;; Resolver contract ;; 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. ;; Resolver must implement EIP65 (supportsInterface). When interacting with an unknown resolver it's safer to rely on it.
(def addr-hash "0x3b3b57de") (def addr-hash "0x3b3b57de")
(defn addr [web3 resolver name cb] (defn addr [web3 resolver ens-name cb]
(ethereum/call web3 (ethereum/call web3
(ethereum/call-params resolver "addr(bytes32)" (namehash name)) (ethereum/call-params resolver "addr(bytes32)" (namehash ens-name))
#(cb %1 %2))) (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 web3
(ethereum/call-params resolver "content(bytes32)" (namehash name)) (ethereum/call-params resolver
#(cb %1 (ethereum/hex->string %2)))) "content(bytes32)"
(namehash ens-name))
(fn [_ content] (cb content #_(ethereum/hex->string content)))))
(def name-hash "0x691f3431") (def name-hash "0x691f3431")
;; Defined by https://eips.ethereum.org/EIPS/eip-181 ;; 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 web3
(ethereum/call-params resolver "name(bytes32)" (namehash name)) (ethereum/call-params resolver
#(cb %1 (ethereum/hex->string %2)))) "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 web3
(ethereum/call-params resolver "text(bytes32,string)" (namehash name) key) (ethereum/call-params resolver
#(cb %1 (ethereum/hex->string %2)))) "text(bytes32)"
(namehash ens-name)
(ethereum/string->hex key))
(fn [err text] (cb err text))))
(def ABI-hash "0x2203ab56") (def ABI-hash "0x2203ab56")
(def pubkey-hash "0xc8690233") (def pubkey-hash "0xc8690233")
;; TODO ABI, pubkey ;; 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")))

View File

@ -1,4 +1,5 @@
(ns status-im.utils.ethereum.stateofus (ns status-im.utils.ethereum.stateofus
(:refer-clojure :exclude [name])
(:require [clojure.string :as string] (:require [clojure.string :as string]
[status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.ens :as ens])) [status-im.utils.ethereum.ens :as ens]))