Skip to content

Commit

Permalink
use new API for ens name registration (#17127)
Browse files Browse the repository at this point in the history
* use new API for ens name registration

status-im/status-go@4cc5363...223d215

* update status-go-version.json

* update status-go-version.json

* fix Error:Field validation for 'KeycardPairingDataFile' failed on the 'required' tag

* update status-go-version.json

* fix lint issue
  • Loading branch information
qfrank authored Sep 16, 2023
1 parent b73ea57 commit 442600b
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 212 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ TWO_MINUTES_SYNCING=1
SWAP_ENABLED=1
STICKERS_TEST_ENABLED=1
LOCAL_PAIRING_ENABLED=1
TEST_STATEOFUS=1
153 changes: 89 additions & 64 deletions src/status_im/ens/core.cljs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
(ns status-im.ens.core
(:refer-clojure :exclude [name])
(:require [clojure.string :as string]
[re-frame.core :as re-frame]
[status-im.bottom-sheet.events :as bottom-sheet]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.eip55 :as eip55]
[status-im.ethereum.ens :as ens]
[status-im.ethereum.stateofus :as stateofus]
[status-im.multiaccounts.update.core :as multiaccounts.update]
[utils.re-frame :as rf]
[utils.datetime :as datetime]
[status-im.utils.random :as random]
[status-im2.navigation.events :as navigation]))
(:require
[clojure.set :as set]
[clojure.string :as string]
[re-frame.core :as re-frame]
[status-im.bottom-sheet.events :as bottom-sheet]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.eip55 :as eip55]
[status-im.ethereum.ens :as ens]
[status-im.ethereum.stateofus :as stateofus]
[status-im.multiaccounts.update.core :as multiaccounts.update]
[utils.re-frame :as rf]
[utils.datetime :as datetime]
[status-im.utils.random :as random]
[status-im2.navigation.events :as navigation]
[status-im2.constants :as constants]
[taoensso.timbre :as log]))

(defn fullname
[custom-domain? username]
Expand Down Expand Up @@ -40,7 +44,7 @@
(ens/expire-at chain-id name cb)))

(rf/defn update-ens-tx-state
{:events [:update-ens-tx-state]}
{:events [:ens/update-ens-tx-state]}
[{:keys [db]} new-state username custom-domain? tx-hash]
{:db (assoc-in db
[:ens/registrations tx-hash]
Expand All @@ -53,20 +57,19 @@
[cofx]
;; we reset navigation so that navigate back doesn't return
;; into the registration flow
(navigation/set-stack-root cofx
:profile-stack
[:my-profile
:ens-confirmation]))
(rf/merge cofx
(navigation/navigate-back-to :my-profile)
(navigation/navigate-to :ens-confirmation {})))

(rf/defn update-ens-tx-state-and-redirect
{:events [:update-ens-tx-state-and-redirect]}
[cofx new-state username custom-domain? tx-hash]
[{:keys [db] :as cofx} new-state username custom-domain? tx-hash]
(rf/merge cofx
(update-ens-tx-state new-state username custom-domain? tx-hash)
(redirect-to-ens-summary)))

(rf/defn clear-ens-registration
{:events [:clear-ens-registration]}
{:events [:ens/clear-registration]}
[{:keys [db]} tx-hash]
{:db (update db :ens/registrations dissoc tx-hash)})

Expand All @@ -79,23 +82,43 @@
(assoc-in [:ens/registration :state] state)
(assoc-in [:ens/registration :address] address))}))

(rf/defn update-usernames
{:events [:ens/update-usernames]}
[{:keys [db]} name-details]
(let [name-details (map #(set/rename-keys %
{:chainId :chain-id
:removed :removed?})
name-details)]
{:db (reduce (fn [db {:keys [username removed?] :as name-detail}]
(if removed?
(update-in db [:ens/names] dissoc username)
(let [old (get-in db [:ens/names username])]
(assoc-in db [:ens/names username] (merge old name-detail)))))
db
name-details)}))

(rf/defn save-username
{:events [::save-username]}
[{:keys [db] :as cofx} custom-domain? username redirectToSummary]
(let [name (fullname custom-domain? username)
names (get-in db [:profile/profile :usernames] [])
new-names (conj names name)]
{:events [:ens/save-username]}
[{:keys [db] :as cofx} custom-domain? username redirect-to-summary? connected?]
(let [name (fullname custom-domain? username)
names (get-in db [:ens/names] [])
chain-id (ethereum/chain-id db)]
(rf/merge cofx
(multiaccounts.update/multiaccount-update
:usernames
new-names
(when redirectToSummary
{:on-success #(re-frame/dispatch [::redirect-to-ens-summary])}))
(when (empty? names)
(multiaccounts.update/multiaccount-update
:preferred-name
name
{})))))
(cond-> {:dispatch-n [[:ens/update-usernames [{:username name :chain-id chain-id}]]]}
connected? (assoc :json-rpc/call
[{:method "ens_add"
:params [chain-id name]
:on-success #()
:on-error #(log/error
"Failed to add ens name"
{:chain-id chain-id :name name :error %})}])
redirect-to-summary? (update-in [:dispatch-n] #(conj % [::redirect-to-ens-summary])))
#(when (empty? names)
(multiaccounts.update/multiaccount-update
cofx
:preferred-name
name
{})))))

(rf/defn set-pub-key
{:events [::set-pub-key]}
Expand All @@ -105,15 +128,14 @@
{:keys [public-key]} (:profile/profile db)
chain-id (ethereum/chain-id db)
username (fullname custom-domain? username)]
(ens/set-pub-key-prepare-tx
chain-id
address
username
public-key
#(re-frame/dispatch [:signing.ui/sign
{:tx-obj %
:on-result [::save-username custom-domain? username true]
:on-error [::on-registration-failure]}]))))
{:db (assoc-in db [:ens/registration :action] constants/ens-action-type-set-pub-key)
:json-rpc/call [{:method "ens_setPubKeyPrepareTx"
:params [chain-id {:from address} username public-key]
:on-success #(re-frame/dispatch [:signing.ui/sign
{:tx-obj %
:on-result [:ens/save-username custom-domain?
username true]
:on-error [::on-registration-failure]}])}]}))

(rf/defn on-input-submitted
{:events [::input-submitted]}
Expand All @@ -125,7 +147,7 @@
:connected-with-different-key
(re-frame/dispatch [::set-pub-key])
:connected
(save-username cofx custom-domain? username true)
(save-username cofx custom-domain? username true true)
;; for other states, we do nothing
nil)))

Expand Down Expand Up @@ -171,15 +193,14 @@
(:ens/registration db)
{:keys [public-key]} (:profile/profile db)
chain-id (ethereum/chain-id db)]
(ens/register-prepare-tx
chain-id
address
username
public-key
#(re-frame/dispatch [:signing.ui/sign
{:tx-obj %
:on-result [:update-ens-tx-state-and-redirect :submitted username false]
:on-error [::on-registration-failure]}]))))
{:db (assoc-in db [:ens/registration :action] constants/ens-action-type-register)
:json-rpc/call [{:method "ens_registerPrepareTx"
:params [chain-id {:from address} username public-key]
:on-success #(re-frame/dispatch [:signing.ui/sign
{:tx-obj %
:on-result [:update-ens-tx-state-and-redirect
:submitted username false]
:on-error [::on-registration-failure]}])}]}))

(defn- valid-custom-domain?
[username]
Expand Down Expand Up @@ -211,7 +232,7 @@
{:events [::set-username-candidate]}
[{:keys [db]} username]
(let [{:keys [custom-domain?]} (:ens/registration db)
usernames (into #{} (get-in db [:profile/profile :usernames]))
usernames (into #{} (keys (get-in db [:ens/names])))
state (state custom-domain? username usernames)]
(reset! resolve-last-id (random/id))
(merge
Expand Down Expand Up @@ -244,9 +265,9 @@
{:db (dissoc db :ens/registration)}
;; we reset navigation so that navigate back doesn't return
;; into the registration flow
(navigation/set-stack-root :profile-stack
[:my-profile
:ens-main])))
(navigation/navigate-back-to :my-profile)
(navigation/navigate-to :ens-main {})
))

(rf/defn switch-domain-type
{:events [::switch-domain-type]}
Expand Down Expand Up @@ -323,14 +344,18 @@
(rf/defn remove-username
{:events [::remove-username]}
[{:keys [db] :as cofx} name]
(let [names (get-in db [:profile/profile :usernames] [])
preferred-name (get-in db [:profile/profile :preferred-name])
new-names (remove #(= name %) names)]
(let [names (get-in db [:ens/names] [])
preferred-name (get-in db [:profile/profile :preferred-name])
new-names (remove #(= name %) (keys names))
{:keys [chain-id username]} (get-in names [name])]
(rf/merge cofx
(multiaccounts.update/multiaccount-update
:usernames
new-names
{})
{:json-rpc/call [{:method "ens_remove"
:params [chain-id username]
:on-success #()
:on-error #(log/error "Failed to remove ENS name"
{:name name :error %})}]
:dispatch [:ens/update-usernames
[{:username username :chain-id chain-id :removed? true}]]}
(when (= name preferred-name)
(multiaccounts.update/multiaccount-update
:preferred-name
Expand Down
12 changes: 0 additions & 12 deletions src/status_im/ethereum/ens.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,3 @@
:on-success
;;NOTE: returns a timestamp in s and we want ms
#(cb (* (js/Number (native-module/hex-to-number %)) 1000))}))

(defn register-prepare-tx
[chain-id from ens-name public-key cb]
(json-rpc/call {:method "ens_registerPrepareTx"
:params [chain-id {:from from} ens-name public-key]
:on-success cb}))

(defn set-pub-key-prepare-tx
[chain-id from ens-name public-key cb]
(json-rpc/call {:method "ens_setPubKeyPrepareTx"
:params [chain-id {:from from} ens-name public-key]
:on-success cb}))
51 changes: 3 additions & 48 deletions src/status_im/ethereum/transactions/core.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns status-im.ethereum.transactions.core
(:require [cljs.spec.alpha :as spec]
[re-frame.core :as re-frame]
[status-im.ens.core :as ens]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.decode :as decode]
[status-im.ethereum.eip55 :as eip55]
Expand Down Expand Up @@ -111,6 +110,7 @@
`trigger-fn` is a function that returns true if the watch has been triggered
`on-trigger` is a function that returns the effects to apply when the
transaction has been triggered"
{:events [:transactions/watch-transaction]}
[{:keys [db]} transaction-id {:keys [trigger-fn on-trigger] :as watch-params}]
(when (and (fn? trigger-fn)
(fn? on-trigger))
Expand Down Expand Up @@ -246,19 +246,6 @@
:all
:all-preloaded))}))

(rf/defn delete-pending-transactions
[{:keys [db]} address transactions]
(let [all-transactions
(get-in db [:wallet :accounts (eip55/address->checksum address) :transactions])
pending-tx-hashes (keep (fn [{:keys [hash]}]
(let [{:keys [type] :as old-tx}
(get all-transactions hash)]
(when (and (get all-transactions hash)
(= type :pending))
hash)))
transactions)]
{:wallet/delete-pending-transactions pending-tx-hashes}))

(rf/defn handle-new-transfer
[{:keys [db] :as cofx} transfers {:keys [address limit]}]
(log/debug "[transfers] new-transfers"
Expand All @@ -273,7 +260,7 @@

(seq transfers)
(concat
[(delete-pending-transactions address transfers)]
[]
(mapv add-transfer transfers))

(and max-known-block
Expand All @@ -290,44 +277,12 @@
(conj (tx-history-end-reached checksum)))]
(apply rf/merge cofx (tx-fetching-ended [checksum]) effects)))

(rf/defn check-ens-transactions
[{:keys [db] :as cofx} transfers]
(let [set-of-transactions-hash (reduce (fn [acc {:keys [hash]}] (conj acc hash)) #{} transfers)
registrations (filter
(fn [[hash {:keys [state]}]]
(and
(or (= state :dismissed) (= state :submitted))
(contains? set-of-transactions-hash hash)))
(get db :ens/registrations))
fxs (map
(fn [[hash {:keys [username custom-domain?]}]]
(let [transfer (first (filter (fn [transfer]
(let [transfer-hash
(get transfer
:hash)]
(= transfer-hash hash)))
transfers))
type (get transfer :type)
transaction-success (get transfer :transfer)]
(cond
(= transaction-success true)
(rf/merge cofx
(ens/clear-ens-registration hash)
(ens/save-username custom-domain? username false))
(= type :failed)
(ens/update-ens-tx-state :failure username custom-domain? hash)
:else
nil)))
registrations)]
(apply rf/merge cofx fxs)))

(rf/defn new-transfers
{:events [::new-transfers]}
[cofx transfers params]
(rf/merge cofx
(handle-new-transfer transfers params)
(wallet/stop-fetching-on-empty-tx-history transfers)
(check-ens-transactions transfers)))
(wallet/stop-fetching-on-empty-tx-history transfers)))

(rf/defn tx-fetching-failed
{:events [::tx-fetching-failed]}
Expand Down
1 change: 1 addition & 0 deletions src/status_im/node/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@

:always
(assoc :LocalNotificationsConfig {:Enabled true}
:KeycardPairingDataFile "/ethereum/mainnet_rpc/keycard/pairings.json"
:BrowsersConfig {:Enabled true}
:PermissionsConfig {:Enabled true}
:MailserversConfig {:Enabled true}
Expand Down
Loading

0 comments on commit 442600b

Please sign in to comment.