Skip to content

Commit

Permalink
[Fixes #8066] Added native ENS registration
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluard committed May 21, 2019
1 parent 373291b commit d106198
Show file tree
Hide file tree
Showing 16 changed files with 642 additions and 11 deletions.
Binary file added resources/images/ui/ens-header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/status_im/react_native/resources.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
:warning-sign (js/require "./resources/images/ui/warning-sign.png")
:phone-nfc-on (js/require "./resources/images/ui/phone-nfc-on.png")
:phone-nfc-off (js/require "./resources/images/ui/phone-nfc-off.png")
:dapp-store (js/require "./resources/images/ui/dapp-store.png")})
:dapp-store (js/require "./resources/images/ui/dapp-store.png")
:ens-header (js/require "./resources/images/ui/ens-header.png")})
25 changes: 25 additions & 0 deletions src/status_im/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
[status-im.utils.config :as config]
[status-im.utils.datetime :as datetime]
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.stateofus :as stateofus]
[status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.hex :as utils.hex]
[status-im.utils.identicon :as identicon]
Expand Down Expand Up @@ -1633,3 +1634,27 @@
:<- [:search/filter]
(fn [[chats search-filter]]
(apply-filter search-filter chats extract-chat-attributes)))

;;ENS ==================================================================================================================

(re-frame/reg-sub
:ens/state
(fn [db]
(get-in db [:ens :state])))

(re-frame/reg-sub
:ens/username
(fn [db]
(get-in db [:ens :username])))

(re-frame/reg-sub
:ens/custom-domain?
(fn [db]
(get-in db [:ens :custom-domain?])))

(re-frame/reg-sub
:ens.stateofus/registrar
:<- [:account/network]
(fn [network]
(let [chain (ethereum/network->chain-keyword network)]
(get stateofus/registrars chain))))
2 changes: 1 addition & 1 deletion src/status_im/ui/components/list/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
active? true
style {}}}]
{:pre [(or icon image-source activity-indicator)
(and action-fn text)
text
(or (nil? accessibility-label) (keyword? accessibility-label))]}
[react/touchable-highlight
{:on-press action-fn
Expand Down
1 change: 1 addition & 0 deletions src/status_im/ui/components/status_bar/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
:profile-stack {:type :main}
:my-profile-ext-settings {:type :main}
:contacts-list {:type :main}
:ens-usernames {:type :main}
:browser {:type :main}
:mobile-network-settings {:type :main}
:backup-seed {:type :main}
Expand Down
122 changes: 122 additions & 0 deletions src/status_im/ui/screens/ens/events.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
(ns status-im.ui.screens.ens.events
(:require [clojure.string :as string]
[re-frame.core :as re-frame]
[status-im.models.wallet :as wallet]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.ethereum.abi-spec :as abi-spec]
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.ens :as ens]
[status-im.utils.ethereum.stateofus :as stateofus]
[status-im.utils.fx :as fx]
[status-im.utils.handlers :as handlers]
[status-im.utils.money :as money]))

(re-frame/reg-fx
::ens/resolve
(fn [[registry name cb]]
(ens/get-addr registry name cb)))

(defn- on-resolve [registry custom-domain? name address public-key s]
(if (and (seq address) (= address (ethereum/normalized-address s)))
(ens/get-pubkey registry name
(fn [ss]
(if (= ss public-key)
(re-frame/dispatch [:ens/set-state :connected])
(re-frame/dispatch [:ens/set-state :registrable]))))
(if (and (nil? s) (not custom-domain?)) ;; No address for a stateofus subdomain: it can be registered
(re-frame/dispatch [:ens/set-state :registrable])
(re-frame/dispatch [:ens/set-state :unregistrable]))))

(defn- chain [{:keys [network] :as db}]
(let [network (get-in db [:account/account :networks network])]
(ethereum/network->chain-keyword network)))

(defn- assoc-state [db state]
(assoc-in db [:ens :state] state))

(defn- assoc-username [db username]
(assoc-in db [:ens :username] username))

(defn- valid-custom-domain? [username]
(and (ens/is-valid-eth-name? username)
(stateofus/lower-case? username)))

(defn- valid-username? [custom-domain? username]
(if custom-domain?
(valid-custom-domain? username)
(stateofus/valid-username? username)))

(handlers/register-handler-fx
:ens/set-state
(fn [{:keys [db]} [_ state]]
{:db (assoc-state db state)}))

(defn- state [valid? username]
(cond
(string/blank? username) :initial
valid? :typing
:else
:invalid))

(handlers/register-handler-fx
:ens/set-username
(fn [{:keys [db]} [_ custom-domain? username]]
(let [valid? (valid-username? custom-domain? username)]
(merge
{:db (-> db
(assoc-username username)
(assoc-state (state valid? username)))}
(when valid?
(let [{:keys [account/account]} db
{:keys [address public-key]} account
registry (get ens/ens-registries (chain db))
name (if custom-domain? username (stateofus/subdomain username))]
{::ens/resolve [registry name #(on-resolve registry custom-domain? name address public-key %)]}))))))

(handlers/register-handler-fx
:ens/navigate-back
(fn [{:keys [db] :as cofx} _]
(fx/merge cofx
{:db (-> db
(assoc-state :initial)
(assoc-username ""))}
(navigation/navigate-back))))

(handlers/register-handler-fx
:ens/switch-domain-type
(fn [{:keys [db]} _]
{:db (-> (update-in db [:ens :custom-domain?] not)
(assoc-state :initial)
(assoc-username ""))}))

(handlers/register-handler-fx
:ens/on-registration-success
(fn [{:keys [db]} _]
{:db (assoc-state db :registered)
:dispatch [:navigate-back]}))

(handlers/register-handler-fx
:ens/on-registration-failure
(fn [{:keys [db]} _]
{:db (assoc-state db :registration-failed)}))

(defn- prepare-extension-transaction [{:keys [contract username address public-key]}]
(let [method "register(bytes32,address,bytes32,bytes32)"
{:keys [x y]} (ethereum/coordinates public-key)
data (abi-spec/encode method [(ethereum/sha3 username) address x y])]
{:to-name "Stateofus registrar"
:symbol :ETH
:method method
:amount (money/bignumber 0)
:to contract
:gas (money/bignumber 200000)
:data data
:on-result [:ens/on-registration-success]
:on-error [:ens/on-registration-failure]}))

(handlers/register-handler-fx
:ens/register
(fn [{db :db} [_ data]]
(let [transaction (prepare-extension-transaction data)]
;; Last argument is nil: no need to estimate gas
(wallet/open-modal-wallet-for-transaction db transaction nil))))
Loading

0 comments on commit d106198

Please sign in to comment.