From a847f508f9367c3553fb41a18e492f3f4b4cf9ae Mon Sep 17 00:00:00 2001 From: flexsurfer Date: Wed, 27 Sep 2023 11:57:51 +0200 Subject: [PATCH] [#17410] migrate status-im.ethereum.core (#17422) --- .carve_ignore | 5 - src/native_module/core.cljs | 8 +- src/status_im/browser/core.cljs | 20 +- src/status_im/browser/eip3326.cljs | 12 +- src/status_im/commands/core.cljs | 7 +- src/status_im/contact/db.cljs | 6 +- src/status_im/data_store/settings.cljs | 2 +- src/status_im/ens/core.cljs | 31 +-- src/status_im/ethereum/contracts.cljs | 21 -- src/status_im/ethereum/core.cljs | 202 ------------------ src/status_im/ethereum/core_test.cljs | 16 -- src/status_im/ethereum/eip681.cljs | 19 +- src/status_im/ethereum/eip681_test.cljs | 2 +- src/status_im/ethereum/mnemonic.cljs | 4 - src/status_im/ethereum/stateofus.cljs | 17 +- src/status_im/ethereum/subscriptions.cljs | 2 +- src/status_im/ethereum/tokens.cljs | 4 +- src/status_im/ethereum/transactions/core.cljs | 14 +- src/status_im/integration_test.cljs | 6 +- src/status_im/keycard/common.cljs | 8 +- src/status_im/keycard/login.cljs | 6 +- src/status_im/keycard/real_keycard.cljs | 8 +- src/status_im/keycard/recovery.cljs | 32 +-- src/status_im/keycard/sign.cljs | 20 +- src/status_im/keycard/simulated_keycard.cljs | 10 +- src/status_im/keycard/wallet.cljs | 19 +- src/status_im/multiaccounts/create/core.cljs | 5 +- src/status_im/multiaccounts/login/core.cljs | 5 +- .../multiaccounts/reset_password/core.cljs | 7 +- src/status_im/network/core.cljs | 6 +- src/status_im/qr_scanner/core.cljs | 6 +- src/status_im/router/core.cljs | 9 +- src/status_im/signing/core.cljs | 56 +++-- src/status_im/signing/gas.cljs | 6 +- src/status_im/stickers/core.cljs | 15 +- src/status_im/ui/screens/ens/views.cljs | 6 +- .../privacy_and_security_settings/events.cljs | 3 +- .../ui/screens/wallet/account/views.cljs | 6 +- .../ui/screens/wallet/add_new/views.cljs | 6 +- .../ui/screens/wallet/recipient/views.cljs | 6 +- .../ui/screens/wallet/request/views.cljs | 2 +- .../ui/screens/wallet/send/views.cljs | 6 +- src/status_im/utils/universal_links/core.cljs | 10 +- src/status_im/utils/utils.cljs | 8 +- src/status_im/wallet/accounts/core.cljs | 13 +- .../wallet/choose_recipient/core.cljs | 11 +- src/status_im/wallet/core.cljs | 62 +++--- src/status_im/wallet/custom_tokens/core.cljs | 9 +- src/status_im/wallet/prices.cljs | 8 +- src/status_im/wallet/recipient/core.cljs | 11 +- src/status_im/wallet/utils.cljs | 10 + src/status_im/wallet_connect/core.cljs | 8 +- .../common/password_authentication/view.cljs | 6 +- src/status_im2/config.cljs | 20 +- .../contexts/add_new_contact/events.cljs | 6 +- .../contexts/profile/create/events.cljs | 3 +- .../contexts/profile/login/events.cljs | 3 +- .../contexts/profile/recover/events.cljs | 3 +- src/status_im2/subs/contact.cljs | 6 +- src/status_im2/subs/ens.cljs | 11 +- src/status_im2/subs/general.cljs | 16 +- src/status_im2/subs/keycard.cljs | 6 +- src/status_im2/subs/networks.cljs | 8 +- src/status_im2/subs/profile.cljs | 9 +- src/status_im2/subs/wallet/signing.cljs | 6 +- src/status_im2/subs/wallet/wallet.cljs | 6 +- src/utils/address.cljs | 33 ++- src/utils/ethereum/chain.cljs | 84 ++++++++ src/utils/ethereum/chain_test.cljs | 8 + .../ethereum/eip}/eip55.cljs | 2 +- .../ethereum/eip}/eip55_test.cljs | 4 +- src/utils/requests.cljs | 9 - 72 files changed, 479 insertions(+), 571 deletions(-) delete mode 100644 src/status_im/ethereum/contracts.cljs delete mode 100644 src/status_im/ethereum/core.cljs delete mode 100644 src/status_im/ethereum/core_test.cljs create mode 100644 src/utils/ethereum/chain.cljs create mode 100644 src/utils/ethereum/chain_test.cljs rename src/{status_im/ethereum => utils/ethereum/eip}/eip55.cljs (96%) rename src/{status_im/ethereum => utils/ethereum/eip}/eip55_test.cljs (92%) delete mode 100644 src/utils/requests.cljs diff --git a/.carve_ignore b/.carve_ignore index ab656c4309..09be820f77 100644 --- a/.carve_ignore +++ b/.carve_ignore @@ -136,11 +136,6 @@ quo.animated/re-timing quo.design-system.colors/white quo.design-system.colors/black status-im.transport.core-test/messages -status-im.ethereum.core/sidechain? -status-im.ethereum.core/network-with-upstream-rpc? -status-im.ethereum.core/current-address -status-im.ethereum.core/network->network-name -status-im.ethereum.core/estimate-gas quo.core/animated-header quo.core/safe-area-provider quo.core/safe-area-consumer diff --git a/src/native_module/core.cljs b/src/native_module/core.cljs index 14a3452fd9..855484bed7 100644 --- a/src/native_module/core.cljs +++ b/src/native_module/core.cljs @@ -470,7 +470,8 @@ (defn sha3 [s] (log/debug "[native-module] sha3") - (.sha3 ^js (status) s)) + (when s + (.sha3 ^js (status) (str s)))) (defn utf8-to-hex [s] @@ -491,8 +492,9 @@ (defn address? [address] (log/debug "[native-module] address?") - (let [result (.isAddress ^js (status) address)] - (types/json->clj result))) + (when address + (let [result (.isAddress ^js (status) address)] + (types/json->clj result)))) (defn to-checksum-address [address] diff --git a/src/status_im/browser/core.cljs b/src/status_im/browser/core.cljs index 9addb14eca..2b357100aa 100644 --- a/src/status_im/browser/core.cljs +++ b/src/status_im/browser/core.cljs @@ -9,7 +9,6 @@ [status-im.browser.permissions :as browser.permissions] [status-im.browser.webview-ref :as webview-ref] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.ens :as ens] [utils.i18n :as i18n] [status-im.multiaccounts.update.core :as multiaccounts.update] @@ -25,7 +24,9 @@ [status-im2.navigation.events :as navigation] [taoensso.timbre :as log] [utils.debounce :as debounce] - [utils.security.core :as security])) + [utils.security.core :as security] + [utils.ethereum.chain :as chain] + [utils.address :as address])) (rf/defn update-browser-option [{:keys [db]} option-key option-value] @@ -91,7 +92,7 @@ host (url/url-host current-url)] (if (and (not resolved-url) (ens/is-valid-eth-name? host)) {:db (update db :browser/options assoc :resolving? true) - :browser/resolve-ens-contenthash {:chain-id (ethereum/chain-id db) + :browser/resolve-ens-contenthash {:chain-id (chain/chain-id db) :ens-name host :cb resolve-ens-contenthash-callback}} {:db (update db :browser/options assoc :url (or resolved-url current-url) :resolving? false)})))) @@ -352,12 +353,19 @@ :id (int id) :result result}}}) +(defn utf8-to-hex + [s] + (let [hex (native-module/utf8-to-hex (str s))] + (if (empty? hex) + nil + hex))) + (defn normalize-message "NOTE (andrey) there is no spec for this, so this implementation just to be compatible with MM" [message] (if (string/starts-with? message "0x") message - (ethereum/utf8-to-hex message))) + (utf8-to-hex message))) (defn normalize-sign-message-params "NOTE (andrey) we need this function, because params may be mixed up" @@ -365,9 +373,9 @@ (let [[first-param second-param] params] (when (and (string? first-param) (string? second-param)) (cond - (ethereum/address? first-param) + (address/address? first-param) [first-param (if typed? second-param (normalize-message second-param))] - (ethereum/address? second-param) + (address/address? second-param) [second-param (if typed? first-param (normalize-message first-param))])))) (rf/defn send-to-bridge diff --git a/src/status_im/browser/eip3326.cljs b/src/status_im/browser/eip3326.cljs index 40cbf26a5f..aacc7748b4 100644 --- a/src/status_im/browser/eip3326.cljs +++ b/src/status_im/browser/eip3326.cljs @@ -2,9 +2,9 @@ ;(`wallet_switchEthereumChain`) (ns status-im.browser.eip3326 (:require [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] [status-im.ui.screens.browser.eip3326.sheet :as sheet] - [utils.re-frame :as rf])) + [utils.re-frame :as rf] + [utils.ethereum.chain :as chain])) (rf/defn deny-permission {:events [:eip3326.ui/dapp-permission-denied]} @@ -20,8 +20,8 @@ [{:keys [db] :as cofx} dapp-name id message-id {:keys [chainId] :as params}] (let [target-chain-id (js/parseInt chainId 16) networks (vals (get-in db [:networks/networks])) - exist-chain-ids (set (map ethereum/network->chain-id networks)) - current-chain-id (ethereum/chain-id db)] + exist-chain-ids (set (map chain/network->chain-id networks)) + current-chain-id (chain/chain-id db)] (if (exist-chain-ids target-chain-id) (if (= current-chain-id target-chain-id) {:browser/send-to-bridge {:type constants/web3-send-async-callback @@ -29,10 +29,10 @@ :result {:jsonrpc "2.0" :id (int id) :result nil}}} - (let [target-network (first (filter #(= (ethereum/network->chain-id %1) target-chain-id) + (let [target-network (first (filter #(= (chain/network->chain-id %1) target-chain-id) networks)) target-network-id (:id target-network) - current-network (ethereum/current-network db) + current-network (chain/current-network db) network-from (:name current-network) network-to (:name target-network) params (assoc params diff --git a/src/status_im/commands/core.cljs b/src/status_im/commands/core.cljs index d12f1bb0d2..3b0fe10140 100644 --- a/src/status_im/commands/core.cljs +++ b/src/status_im/commands/core.cljs @@ -1,7 +1,7 @@ (ns status-im.commands.core (:require [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] - [utils.re-frame :as rf])) + [utils.re-frame :as rf] + [status-im.wallet.utils :as wallet.utils])) (rf/defn handle-prepare-accept-request-address-for-transaction {:events [::prepare-accept-request-address-for-transaction]} @@ -9,7 +9,8 @@ {:db (assoc db :commands/select-account {:message message - :from (ethereum/get-default-account (:profile/wallet-accounts db))}) + :from (wallet.utils/get-default-account (:profile/wallet-accounts + db))}) :show-select-acc-sheet nil}) (rf/defn set-selected-account diff --git a/src/status_im/contact/db.cljs b/src/status_im/contact/db.cljs index 14eeeaf0e0..e2b501bcf7 100644 --- a/src/status_im/contact/db.cljs +++ b/src/status_im/contact/db.cljs @@ -2,8 +2,8 @@ (:require [clojure.set :as set] [clojure.string :as string] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] - [status-im.utils.gfycat.core :as gfycat])) + [status-im.utils.gfycat.core :as gfycat] + [utils.address :as address])) (defn public-key->new-contact [public-key] @@ -31,7 +31,7 @@ (defn- contact-by-address [[addr contact] address] - (when (ethereum/address= addr address) + (when (address/address= addr address) contact)) (defn find-contact-by-address diff --git a/src/status_im/data_store/settings.cljs b/src/status_im/data_store/settings.cljs index 545b269a0d..90bf51993b 100644 --- a/src/status_im/data_store/settings.cljs +++ b/src/status_im/data_store/settings.cljs @@ -1,6 +1,6 @@ (ns status-im.data-store.settings (:require [status-im.data-store.visibility-status-updates :as visibility-status-updates] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [status-im2.config :as config] [clojure.set :as set])) diff --git a/src/status_im/ens/core.cljs b/src/status_im/ens/core.cljs index 86081f6480..7165826b02 100644 --- a/src/status_im/ens/core.cljs +++ b/src/status_im/ens/core.cljs @@ -5,8 +5,7 @@ [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] + [utils.ethereum.eip.eip55 :as eip55] [status-im.ethereum.ens :as ens] [status-im.ethereum.stateofus :as stateofus] [status-im.multiaccounts.update.core :as multiaccounts.update] @@ -15,7 +14,9 @@ [status-im.utils.random :as random] [status-im2.navigation.events :as navigation] [status-im2.constants :as constants] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.ethereum.chain :as chain] + [status-im.wallet.utils :as wallet.utils])) (defn fullname [custom-domain? username] @@ -102,7 +103,7 @@ [{: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)] + chain-id (chain/chain-id db)] (rf/merge cofx (cond-> {:dispatch-n [[:ens/update-usernames [{:username name :chain-id chain-id}]]]} connected? (assoc :json-rpc/call @@ -124,9 +125,9 @@ {:events [::set-pub-key]} [{:keys [db]}] (let [{:keys [username address custom-domain?]} (:ens/registration db) - address (or address (ethereum/default-address db)) + address (or address (wallet.utils/default-address db)) {:keys [public-key]} (:profile/profile db) - chain-id (ethereum/chain-id db) + chain-id (chain/chain-id db) username (fullname custom-domain? username)] {:db (assoc-in db [:ens/registration :action] constants/ens-action-type-set-pub-key) :json-rpc/call [{:method "ens_setPubKeyPrepareTx" @@ -192,7 +193,7 @@ (let [{:keys [username]} (:ens/registration db) {:keys [public-key]} (:profile/profile db) - chain-id (ethereum/chain-id db)] + chain-id (chain/chain-id db)] {: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] @@ -225,6 +226,12 @@ :searching) :else :invalid)) +(defn addresses-without-watch + [db] + (into #{} + (remove #(= (:type %) :watch) + (map #(eip55/address->checksum (:address %)) (get db :profile/wallet-accounts))))) + ;;NOTE we want to handle only last resolve (def resolve-last-id (atom nil)) @@ -243,8 +250,8 @@ (when (= state :searching) (let [{:profile/keys [profile]} db {:keys [public-key]} profile - addresses (ethereum/addresses-without-watch db) - chain-id (ethereum/chain-id db)] + addresses (addresses-without-watch db) + chain-id (chain/chain-id db)] {::resolve-owner [chain-id (fullname custom-domain? username) #(on-resolve-owner @@ -266,8 +273,8 @@ ;; we reset navigation so that navigate back doesn't return ;; into the registration flow (navigation/navigate-back-to :my-profile) - (navigation/navigate-to :ens-main {}) - )) + (navigation/navigate-to :ens-main {}))) + (rf/defn switch-domain-type {:events [::switch-domain-type]} @@ -320,7 +327,7 @@ (rf/defn navigate-to-name {:events [::navigate-to-name]} [{:keys [db] :as cofx} username] - (let [chain-id (ethereum/chain-id db)] + (let [chain-id (chain/chain-id db)] (rf/merge cofx {::get-expiration-time [chain-id diff --git a/src/status_im/ethereum/contracts.cljs b/src/status_im/ethereum/contracts.cljs deleted file mode 100644 index c9da0d564b..0000000000 --- a/src/status_im/ethereum/contracts.cljs +++ /dev/null @@ -1,21 +0,0 @@ -(ns status-im.ethereum.contracts - (:require [status-im.ethereum.core :as ethereum])) - -(def contracts - {:status/snt - {:mainnet "0x744d70fdbe2ba4cf95131626614a1763df805b9e" - :goerli "0x3D6AFAA395C31FCd391fE3D562E75fe9E8ec7E6a"} - :status/stickers - {:mainnet "0x0577215622f43a39f4bc9640806dfea9b10d2a36" - :goerli "0x07f7CB0C0a4ab3e0999AfE8b3997Da34880f05d0"} - :status/sticker-market - {:mainnet "0x12824271339304d3a9f7e096e62a2a7e73b4a7e7" - :goerli "0xf1E149A7DF70D5Ff1E265daAa738d785D3274717"} - :status/sticker-pack - {:mainnet "0x110101156e8F0743948B2A61aFcf3994A8Fb172e" - :goerli "0x8D3fD2EA24bD53a8Bd2b1026727db8bbe9A8C8Af"}}) - -(defn get-address - [db contract] - (let [chain-keyword (ethereum/chain-keyword db)] - (get-in contracts [contract chain-keyword]))) diff --git a/src/status_im/ethereum/core.cljs b/src/status_im/ethereum/core.cljs deleted file mode 100644 index 28d409a924..0000000000 --- a/src/status_im/ethereum/core.cljs +++ /dev/null @@ -1,202 +0,0 @@ -(ns status-im.ethereum.core - (:require [clojure.string :as string] - [status-im.ethereum.eip55 :as eip55] - [native-module.core :as native-module])) - -(defn sha3 - [s] - (when s - (native-module/sha3 (str s)))) - -(defn utf8-to-hex - [s] - (let [hex (native-module/utf8-to-hex (str s))] - (if (empty? hex) - nil - hex))) - -(defn hex-to-utf8 - [s] - (let [utf8 (native-module/hex-to-utf8 s)] - (if (empty? utf8) - nil - utf8))) - -(def BSC-mainnet-chain-id 56) -(def BSC-testnet-chain-id 97) - -;; IDs standardized in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md#list-of-chain-ids -(def chains - {:mainnet {:id 1 :name "Mainnet"} - :xdai {:id 100 :name "xDai"} - :goerli {:id 5 :name "Goerli"} - :bsc {:id BSC-mainnet-chain-id - :name "BSC"} - :bsc-testnet {:id BSC-testnet-chain-id - :name "BSC tetnet"}}) - -(defn chain-id->chain-keyword - [i] - (or (some #(when (= i (:id (val %))) (key %)) chains) - :custom)) - -(defn chain-id->chain-name - [i] - (or (some #(when (= i (:id (val %))) (:name (val %))) chains) - :custom)) - -(defn chain-keyword->chain-id - [k] - (get-in chains [k :id])) - -(defn chain-keyword->snt-symbol - [k] - (case k - :mainnet :SNT - :STT)) - -(defn testnet? - [id] - (contains? #{(chain-keyword->chain-id :goerli) - (chain-keyword->chain-id :bsc-testnet)} - id)) - -(defn sidechain? - [id] - (contains? #{(chain-keyword->chain-id :xdai) - (chain-keyword->chain-id :bsc)} - id)) - -(defn network-with-upstream-rpc? - [network] - (get-in network [:config :UpstreamConfig :Enabled])) - -(def hex-prefix "0x") - -(defn normalized-hex - [hex] - (when hex - (if (string/starts-with? hex hex-prefix) - hex - (str hex-prefix hex)))) - -(defn current-address - [db] - (-> (get-in db [:profile/profile :address]) - normalized-hex)) - -(defn get-default-account - [accounts] - (some #(when (:wallet %) %) accounts)) - -(defn default-address - [db] - (-> (get db :profile/wallet-accounts) - get-default-account - :address)) - -(defn addresses-without-watch - [db] - (into #{} - (remove #(= (:type %) :watch) - (map #(eip55/address->checksum (:address %)) (get db :profile/wallet-accounts))))) - -(defn naked-address - [s] - (when s - (string/replace s hex-prefix ""))) - -(def public-key-length 128) - -(defn coordinates - [public-key] - (when-let [hex (naked-address public-key)] - (when (= public-key-length (count (subs hex 2))) - {:x (normalized-hex (subs hex 2 66)) - :y (normalized-hex (subs hex 66))}))) - -(defn address? - [s] - (when s - (native-module/address? s))) - -(defn network->chain-id - [network] - (get-in network [:config :NetworkId])) - -(defn network->chain-keyword - [network] - (chain-id->chain-keyword (network->chain-id network))) - -(defn current-network - [db] - (let [networks (get db :networks/networks) - network-id (get db :networks/current-network)] - (get networks network-id))) - -(defn binance-chain-id? - [chain-id] - (or (= BSC-mainnet-chain-id chain-id) - (= BSC-testnet-chain-id chain-id))) - -(defn binance-chain? - [db] - (-> db - current-network - network->chain-id - binance-chain-id?)) - -(def custom-rpc-node-id-len 45) - -(defn custom-rpc-node? - [{:keys [id]}] - (= custom-rpc-node-id-len (count id))) - -(defn network->network-name - [network] - (chain-id->chain-name (network->chain-id network))) - -(defn network->chain-name - [network] - (-> network - network->chain-keyword - name)) - -(defn get-current-network - [m] - (get (:networks/networks m) (:networks/current-network m))) - -(defn chain-keyword - [db] - (network->chain-keyword (get-current-network db))) - -(defn chain-id - [db] - (network->chain-id (get-current-network db))) - -(defn snt-symbol - [db] - (chain-keyword->snt-symbol (chain-keyword db))) - -(defn address= - [address1 address2] - (and address1 - address2 - (= (string/lower-case (normalized-hex address1)) - (string/lower-case (normalized-hex address2))))) - -(defn public-key->address - [public-key] - (let [length (count public-key) - normalized-key (case length - 132 (str "0x" (subs public-key 4)) - 130 public-key - 128 (str "0x" public-key) - nil)] - (when normalized-key - (subs (sha3 normalized-key) 26)))) - -(defn hex->text - "Converts a hexstring to UTF8 text." - [data] - (or (hex-to-utf8 data) data)) diff --git a/src/status_im/ethereum/core_test.cljs b/src/status_im/ethereum/core_test.cljs deleted file mode 100644 index bddf8d9b04..0000000000 --- a/src/status_im/ethereum/core_test.cljs +++ /dev/null @@ -1,16 +0,0 @@ -(ns status-im.ethereum.core-test - (:require [cljs.test :refer-macros [deftest is]] - [status-im.ethereum.core :as ethereum])) - -(deftest chain-id->chain-keyword - (is (= (ethereum/chain-id->chain-keyword 1) :mainnet)) - (is (= (ethereum/chain-id->chain-keyword 5) :goerli)) - (is (= (ethereum/chain-id->chain-keyword 5777) :custom))) - -(deftest coordinates - (is - (= - {:x "0x46fa4851f3cccd01e3b8d96c130c00bf812502354939eacf06a68fa519ebcbd1" - :y "0xeb08bebe7403856c0d9686210b9b2e324aa0179747bbba56d53f304a002f31c3"} - (ethereum/coordinates - "0x0446fa4851f3cccd01e3b8d96c130c00bf812502354939eacf06a68fa519ebcbd1eb08bebe7403856c0d9686210b9b2e324aa0179747bbba56d53f304a002f31c3")))) diff --git a/src/status_im/ethereum/eip681.cljs b/src/status_im/ethereum/eip681.cljs index bbd3b88f76..a279b2a08c 100644 --- a/src/status_im/ethereum/eip681.cljs +++ b/src/status_im/ethereum/eip681.cljs @@ -5,10 +5,11 @@ e.g. ethereum:0x1234@1/transfer?to=0x5678&value=1e18&gas=5000" (:require [clojure.string :as string] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.ens :as ens] [status-im.ethereum.tokens :as tokens] - [utils.money :as money])) + [utils.money :as money] + [utils.ethereum.chain :as chain] + [utils.address :as address])) (def scheme "ethereum") (def scheme-separator ":") @@ -64,16 +65,16 @@ Invalid URI will be parsed as `nil`." [s] (when (string? s) - (if (ethereum/address? s) + (if (address/address? s) {:address s} (let [[_ authority-path query] (re-find uri-pattern s)] (when authority-path (let [[_ raw-address chain-id function-name] (re-find authority-path-pattern authority-path)] - (when (or (or (ens/is-valid-eth-name? raw-address) (ethereum/address? raw-address)) + (when (or (or (ens/is-valid-eth-name? raw-address) (address/address? raw-address)) (when (string/starts-with? raw-address "pay-") (let [pay-address (string/replace-first raw-address "pay-" "")] (or (ens/is-valid-eth-name? pay-address) - (ethereum/address? pay-address))))) + (address/address? pay-address))))) (let [address (if (string/starts-with? raw-address "pay-") (string/replace-first raw-address "pay-" "") raw-address)] @@ -81,12 +82,12 @@ (let [contract-address (get-in arguments [:function-arguments :address])] (if-not (or (not contract-address) (or (ens/is-valid-eth-name? contract-address) - (ethereum/address? contract-address))) + (address/address? contract-address))) nil (merge {:address address :chain-id (if chain-id (js/parseInt chain-id) - (ethereum/chain-keyword->chain-id :mainnet))} + (chain/chain-keyword->chain-id :mainnet))} arguments)))))))))))) (defn parse-eth-value @@ -124,12 +125,12 @@ "Generate a EIP 681 URI based on `address` and a map (keywords / {bignumbers/strings} ) of extra properties. No validation of address format is performed." [address {:keys [chain-id function-name function-arguments] :as m}] - (when (ethereum/address? address) + (when (address/address? address) (let [parameters (dissoc (into {} (filter second m)) :chain-id)] ;; filter nil values (str scheme scheme-separator address - (when (and chain-id (not= chain-id (ethereum/chain-keyword->chain-id :mainnet))) + (when (and chain-id (not= chain-id (chain/chain-keyword->chain-id :mainnet))) ;; Add chain-id if specified and is not main-net (str chain-id-separator chain-id)) (when-not (empty? parameters) diff --git a/src/status_im/ethereum/eip681_test.cljs b/src/status_im/ethereum/eip681_test.cljs index 61a6e62976..a168ccb8b3 100644 --- a/src/status_im/ethereum/eip681_test.cljs +++ b/src/status_im/ethereum/eip681_test.cljs @@ -1,5 +1,5 @@ (ns status-im.ethereum.eip681-test - (:require [cljs.test :refer-macros [deftest is] :as test] + (:require [cljs.test :refer-macros [deftest is]] [status-im.ethereum.eip681 :as eip681] [utils.money :as money])) diff --git a/src/status_im/ethereum/mnemonic.cljs b/src/status_im/ethereum/mnemonic.cljs index e02d7e6c61..a289dcf3a9 100644 --- a/src/status_im/ethereum/mnemonic.cljs +++ b/src/status_im/ethereum/mnemonic.cljs @@ -189,10 +189,6 @@ (-> (sanitize-passphrase s) (string/split #" ")))) -(defn words->passphrase - [v] - (string/join " " v)) - (def valid-word-counts #{12 15 18 21 24}) (defn valid-word-counts? diff --git a/src/status_im/ethereum/stateofus.cljs b/src/status_im/ethereum/stateofus.cljs index ec187f7498..af9179634f 100644 --- a/src/status_im/ethereum/stateofus.cljs +++ b/src/status_im/ethereum/stateofus.cljs @@ -1,6 +1,5 @@ (ns status-im.ethereum.stateofus (:require [clojure.string :as string] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.ens :as ens] [status-im2.config :as config])) @@ -33,23 +32,9 @@ (when config/test-stateofus? {:goerli "0xD1f7416F91E7Eb93dD96A61F12FC092aD6B67B11"}))) -(def registrars-cache (atom {})) - -(defn get-registrar - [chain callback] - (if-let [contract (get @registrars-cache chain)] - (callback contract) - (ens/owner - (ethereum/chain-keyword->chain-id chain) - domain - (fn [addr] - (let [addr (or addr (get old-registrars chain))] - (swap! registrars-cache assoc chain addr) - (callback addr)))))) - (defn get-cached-registrar [chain] - (get @registrars-cache chain (get old-registrars chain))) + (get old-registrars chain)) (defn lower-case? [s] diff --git a/src/status_im/ethereum/subscriptions.cljs b/src/status_im/ethereum/subscriptions.cljs index b022a28e94..5917107fee 100644 --- a/src/status_im/ethereum/subscriptions.cljs +++ b/src/status_im/ethereum/subscriptions.cljs @@ -1,5 +1,5 @@ (ns status-im.ethereum.subscriptions - (:require [status-im.ethereum.eip55 :as eip55] + (:require [utils.ethereum.eip.eip55 :as eip55] [status-im.ethereum.transactions.core :as transactions] [utils.re-frame :as rf] [status-im.wallet.core :as wallet.core] diff --git a/src/status_im/ethereum/tokens.cljs b/src/status_im/ethereum/tokens.cljs index ef273ce006..a6a9d1bf4a 100644 --- a/src/status_im/ethereum/tokens.cljs +++ b/src/status_im/ethereum/tokens.cljs @@ -1,6 +1,6 @@ (ns status-im.ethereum.tokens (:require [clojure.string :as string] - [status-im.ethereum.core :as ethereum]) + [utils.ethereum.chain :as chain]) (:require-macros [status-im.ethereum.macros :as ethereum.macros :refer [resolve-icons]])) (def default-native-currency @@ -42,7 +42,7 @@ (defn native-currency [{sym :symbol :as current-network}] - (let [chain (ethereum/network->chain-keyword current-network)] + (let [chain (chain/network->chain-keyword current-network)] (get all-native-currencies chain (default-native-currency sym)))) (defn ethereum? diff --git a/src/status_im/ethereum/transactions/core.cljs b/src/status_im/ethereum/transactions/core.cljs index a4b9b958ac..f377f6173d 100644 --- a/src/status_im/ethereum/transactions/core.cljs +++ b/src/status_im/ethereum/transactions/core.cljs @@ -1,24 +1,24 @@ (ns status-im.ethereum.transactions.core (:require [cljs.spec.alpha :as spec] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.decode :as decode] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [status-im.ethereum.encode :as encode] [utils.re-frame :as rf] [status-im.utils.mobile-sync :as utils.mobile-sync] [status-im.wallet.core :as wallet] [status-im2.common.json-rpc.events :as json-rpc] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.ethereum.chain :as chain])) (def confirmations-count-threshold 12) (def etherscan-supported? - #{(ethereum/chain-keyword->chain-id :mainnet) - (ethereum/chain-keyword->chain-id :goerli)}) + #{(chain/chain-keyword->chain-id :mainnet) + (chain/chain-keyword->chain-id :goerli)}) -(def binance-mainnet-chain-id (ethereum/chain-keyword->chain-id :bsc)) -(def binance-testnet-chain-id (ethereum/chain-keyword->chain-id :bsc-testnet)) +(def binance-mainnet-chain-id (chain/chain-keyword->chain-id :bsc)) +(def binance-testnet-chain-id (chain/chain-keyword->chain-id :bsc-testnet)) (def network->subdomain {5 "goerli"}) diff --git a/src/status_im/integration_test.cljs b/src/status_im/integration_test.cljs index a746d0868f..7a92e91de0 100644 --- a/src/status_im/integration_test.cljs +++ b/src/status_im/integration_test.cljs @@ -3,7 +3,6 @@ [clojure.string :as string] [day8.re-frame.test :as rf-test] [re-frame.core :as rf] - [status-im.ethereum.core :as ethereum] status-im.events status-im2.events [status-im.multiaccounts.logout.core :as logout] @@ -12,7 +11,8 @@ status-im2.navigation.core status-im2.subs.root ; so integration tests can run independently [taoensso.timbre :as log] - [status-im2.constants :as constants])) + [status-im2.constants :as constants] + [native-module.core :as native-module])) (def password "testabc") @@ -52,7 +52,7 @@ [] (rf/dispatch-sync [:wallet.accounts/start-adding-new-account {:type :generate}]) (rf/dispatch-sync [:set-in [:add-account :account :name] account-name]) - (rf/dispatch [:wallet.accounts/add-new-account (ethereum/sha3 password)])) + (rf/dispatch [:wallet.accounts/add-new-account (native-module/sha3 password)])) (defn assert-new-account-created [] diff --git a/src/status_im/keycard/common.cljs b/src/status_im/keycard/common.cljs index 14ebbc6242..f3953a1170 100644 --- a/src/status_im/keycard/common.cljs +++ b/src/status_im/keycard/common.cljs @@ -2,7 +2,6 @@ (: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] [utils.i18n :as i18n] [status-im.keycard.nfc :as nfc] [status-im.popover.core :as popover] @@ -13,7 +12,8 @@ [react-native.platform :as platform] [status-im.utils.deprecated-types :as types] [status-im2.navigation.events :as navigation] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.address :as address])) (def default-pin "000000") @@ -78,7 +78,7 @@ (when key-uid (->> (:profile/profiles-overview db) vals - (filter #(= (ethereum/normalized-hex key-uid) (:key-uid %))) + (filter #(= (address/normalized-hex key-uid) (:key-uid %))) first))) (defn get-pairing @@ -354,7 +354,7 @@ :puk-retry-counter 5 :pin-retry-counter 3) (assoc-in [:keycard :profile/profile] - (update account-data :whisper-public-key ethereum/normalized-hex)) + (update account-data :whisper-public-key address/normalized-hex)) (assoc-in [:keycard :flow] nil) (update :profile/login assoc :password encryption-public-key diff --git a/src/status_im/keycard/login.cljs b/src/status_im/keycard/login.cljs index d6b0831b26..39154a1944 100644 --- a/src/status_im/keycard/login.cljs +++ b/src/status_im/keycard/login.cljs @@ -1,6 +1,5 @@ (ns status-im.keycard.login (:require [status-im.bottom-sheet.events :as bottom-sheet] - [status-im.ethereum.core :as ethereum] [status-im.keycard.common :as common] status-im.keycard.fx [status-im.keycard.onboarding :as onboarding] @@ -9,7 +8,8 @@ [utils.re-frame :as rf] [status-im.utils.deprecated-types :as types] [status-im2.navigation.events :as navigation] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.address :as address])) (rf/defn login-got-it-pressed {:events [:keycard.login.pin.ui/got-it-pressed @@ -162,7 +162,7 @@ (assoc-in [:keycard :pin :status] nil) (assoc-in [:keycard :pin :login] []) (assoc-in [:keycard :profile/profile] - (update account-data :whisper-public-key ethereum/normalized-hex)) + (update account-data :whisper-public-key address/normalized-hex)) (assoc-in [:keycard :flow] nil) (update :profile/login assoc :password encryption-public-key diff --git a/src/status_im/keycard/real_keycard.cljs b/src/status_im/keycard/real_keycard.cljs index 192906bc7c..049e9d8300 100644 --- a/src/status_im/keycard/real_keycard.cljs +++ b/src/status_im/keycard/real_keycard.cljs @@ -2,12 +2,12 @@ (:require ["react-native" :as rn] ["react-native-status-keycard" :default status-keycard] [clojure.string :as string] - [status-im.ethereum.core :as ethereum] [status-im.keycard.keycard :as keycard] [native-module.core :as native-module] [react-native.platform :as platform] [status-im.utils.deprecated-types :as types] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.address :as address])) (defonce event-emitter (if platform/ios? @@ -114,7 +114,7 @@ (then (fn [response] (let [info (-> response (js->clj :keywordize-keys true) - (update :key-uid ethereum/normalized-hex))] + (update :key-uid address/normalized-hex))] (on-success info)))) (catch on-failure))) @@ -125,7 +125,7 @@ (then (fn [response] (let [info (-> response (js->clj :keywordize-keys true) - (update :key-uid ethereum/normalized-hex))] + (update :key-uid address/normalized-hex))] (on-success info)))) (catch on-failure))) diff --git a/src/status_im/keycard/recovery.cljs b/src/status_im/keycard/recovery.cljs index 91a3657503..6fcb362d19 100644 --- a/src/status_im/keycard/recovery.cljs +++ b/src/status_im/keycard/recovery.cljs @@ -3,8 +3,7 @@ [re-frame.core :as re-frame] [status-im.bottom-sheet.events :as bottom-sheet] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [utils.i18n :as i18n] [status-im.keycard.common :as common] status-im.keycard.fx @@ -19,7 +18,8 @@ [status-im.utils.deprecated-types :as types] [status-im2.navigation.events :as navigation] [taoensso.timbre :as log] - [utils.security.core :as security])) + [utils.security.core :as security] + [utils.address :as address])) (rf/defn pair* [_ password] @@ -203,7 +203,7 @@ :address address :public-key public-key :keycard-instance-uid instance-uid - :key-uid (ethereum/normalized-hex key-uid) + :key-uid (address/normalized-hex key-uid) :keycard-pairing pairing :keycard-paired-on paired-on :chat-key whisper-private-key} @@ -272,9 +272,9 @@ settings {:keycard-instance-uid instance-uid :keycard-paired-on paired-on :keycard-pairing pairing} - password (ethereum/sha3 (security/safe-unmask-data (get-in db - [:keycard - :migration-password]))) + password (native-module/sha3 (security/safe-unmask-data (get-in db + [:keycard + :migration-password]))) encryption-pass (get-in db [:keycard :profile/profile :encryption-public-key]) login-params {:key-uid key-uid :multiaccount-data (types/clj->json account) @@ -316,20 +316,20 @@ (assoc-in [:keycard :profile/profile] (-> account-data - (update :address ethereum/normalized-hex) - (update :whisper-address ethereum/normalized-hex) - (update :wallet-address ethereum/normalized-hex) - (update :wallet-root-address ethereum/normalized-hex) - (update :public-key ethereum/normalized-hex) - (update :whisper-public-key ethereum/normalized-hex) - (update :wallet-public-key ethereum/normalized-hex) - (update :wallet-root-public-key ethereum/normalized-hex) + (update :address address/normalized-hex) + (update :whisper-address address/normalized-hex) + (update :wallet-address address/normalized-hex) + (update :wallet-root-address address/normalized-hex) + (update :public-key address/normalized-hex) + (update :whisper-public-key address/normalized-hex) + (update :wallet-public-key address/normalized-hex) + (update :wallet-root-public-key address/normalized-hex) (update :instance-uid #(get-in db [:keycard :profile/profile :instance-uid] %)))) (assoc-in [:keycard :multiaccount-wallet-address] (:wallet-address account-data)) (assoc-in [:keycard :multiaccount-whisper-public-key] (:whisper-public-key account-data)) (assoc-in [:keycard :pin :status] nil) (assoc-in [:keycard :application-info :key-uid] - (ethereum/normalized-hex (:key-uid account-data))) + (address/normalized-hex (:key-uid account-data))) (update :keycard dissoc :recovery-phrase :creating-backup? :converting-account?) (update-in [:keycard :secrets] dissoc :pin :puk :password :mnemonic) (assoc :multiaccounts/new-installation-id (random-guid-generator)))} diff --git a/src/status_im/keycard/sign.cljs b/src/status_im/keycard/sign.cljs index de7f3d8d9f..0a2c4fad7d 100644 --- a/src/status_im/keycard/sign.cljs +++ b/src/status_im/keycard/sign.cljs @@ -1,12 +1,14 @@ (ns status-im.keycard.sign (:require [clojure.string :as string] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [status-im.keycard.common :as common] [utils.re-frame :as rf] [utils.money :as money] [status-im.utils.deprecated-types :as types] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.ethereum.chain :as chain] + [status-im.wallet.utils :as wallet.utils] + [utils.address :as address])) (rf/defn sign {:events [:keycard/sign]} @@ -22,10 +24,10 @@ pin (common/vector->string (get-in db [:keycard :pin :sign])) from (or (get-in db [:signing/tx :from :address]) (get-in db [:signing/tx :message :from]) - (ethereum/default-address db)) + (wallet.utils/default-address db)) path (reduce (fn [_ {:keys [address path]}] - (when (ethereum/address= from address) + (when (address/address= from address) (reduced path))) nil (:profile/wallet-accounts db))] @@ -43,7 +45,7 @@ {:db (-> db (assoc-in [:keycard :card-read-in-progress?] true) (assoc-in [:keycard :pin :status] :verifying)) - :keycard/sign {:hash (ethereum/naked-address hash) + :keycard/sign {:hash (address/naked-address hash) :data data :typed? typed? ; this parameter is for e2e :on-success on-success @@ -55,7 +57,7 @@ (-> signature (string/replace-first #"00$" "1b") (string/replace-first #"01$" "1c") - ethereum/normalized-hex)) + address/normalized-hex)) (rf/defn sign-message {:events [:keycard/sign-message]} @@ -63,7 +65,7 @@ (let [{:keys [result error]} (types/json->clj result) on-success #(re-frame/dispatch [:keycard/on-sign-message-success params (normalize-signature %)]) - hash (ethereum/naked-address result)] + hash (address/naked-address result)] (sign cofx hash on-success))) (rf/defn on-sign-message-success @@ -92,7 +94,7 @@ {:db (-> db (assoc-in [:keycard :card-read-in-progress?] true) (assoc-in [:signing/sign :keycard-step] :signing)) - :keycard/sign-typed-data {:hash (ethereum/naked-address hash)}} + :keycard/sign-typed-data {:hash (address/naked-address hash)}} (rf/merge cofx (common/set-on-card-connected :keycard/sign-typed-data) {:db (assoc-in db [:signing/sign :keycard-step] :signing)})))) @@ -116,7 +118,7 @@ currency-contract (:currency message)] (when currency-contract {:json-rpc/call [{:method "wallet_discoverToken" - :params [(ethereum/chain-id db) currency-contract] + :params [(chain/chain-id db) currency-contract] :on-success #(re-frame/dispatch [:keycard/fetch-currency-token-on-success %])}]}) (rf/merge cofx diff --git a/src/status_im/keycard/simulated_keycard.cljs b/src/status_im/keycard/simulated_keycard.cljs index 7d09ebe49d..bf4fea74ef 100644 --- a/src/status_im/keycard/simulated_keycard.cljs +++ b/src/status_im/keycard/simulated_keycard.cljs @@ -3,7 +3,6 @@ [re-frame.core :as re-frame] [re-frame.db :as re-frame.db] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] [utils.i18n :as i18n] [status-im.keycard.keycard :as keycard] [status-im.multiaccounts.create.core :as multiaccounts.create] @@ -11,12 +10,13 @@ [status-im.node.core :as node] [status-im.utils.deprecated-types :as types] [status-im.utils.utils :as utils] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.address :as address])) (def kk1-password "000000") (def default-pin "111111") (def default-puk "000000000000") -(def account-password (ethereum/sha3 "no password")) +(def account-password (native-module/sha3 "no password")) (def initial-state {:card-connected? false @@ -476,7 +476,7 @@ (let [signature (-> res types/json->clj :result - ethereum/normalized-hex)] + address/normalized-hex)] (on-success signature))))) (native-module/sign-typed-data data @@ -486,7 +486,7 @@ (let [signature (-> res types/json->clj :result - ethereum/normalized-hex)] + address/normalized-hex)] (on-success signature)))))))) (defn sign-typed-data diff --git a/src/status_im/keycard/wallet.cljs b/src/status_im/keycard/wallet.cljs index 820f195ec7..6c4a9157ab 100644 --- a/src/status_im/keycard/wallet.cljs +++ b/src/status_im/keycard/wallet.cljs @@ -1,12 +1,12 @@ (ns status-im.keycard.wallet (:require [status-im.bottom-sheet.events :as bottom-sheet] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [status-im.keycard.common :as common] [status-im.ui.screens.wallet.add-new.views :as add-new.views] [utils.re-frame :as rf] - [status-im.utils.hex :as utils.hex])) + [status-im.utils.hex :as utils.hex] + [native-module.core :as native-module])) (rf/defn show-pin-sheet {:events [:keycard/new-account-pin-sheet]} @@ -31,6 +31,17 @@ [cofx] (bottom-sheet/hide-bottom-sheet-old cofx)) +(defn public-key->address + [public-key] + (let [length (count public-key) + normalized-key (case length + 132 (str "0x" (subs public-key 4)) + 130 public-key + 128 (str "0x" public-key) + nil)] + (when normalized-key + (subs (native-module/sha3 normalized-key) 26)))) + (rf/defn generate-new-keycard-account {:events [:wallet.accounts/generate-new-keycard-account]} [{:keys [db]}] @@ -46,7 +57,7 @@ {;; Strip leading 04 prefix denoting uncompressed key format :address (eip55/address->checksum (str "0x" - (ethereum/public-key->address + (public-key->address (subs public-key 2)))) :public-key (str "0x" public-key) :path path}))) diff --git a/src/status_im/multiaccounts/create/core.cljs b/src/status_im/multiaccounts/create/core.cljs index dc635c09ef..2967824d79 100644 --- a/src/status_im/multiaccounts/create/core.cljs +++ b/src/status_im/multiaccounts/create/core.cljs @@ -3,8 +3,7 @@ [re-frame.core :as re-frame] [status-im2.constants :as constants] [status-im.data-store.settings :as data-store.settings] - [status-im.ethereum.core :as ethereum] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [utils.i18n :as i18n] [native-module.core :as native-module] [status-im.node.core :as node] @@ -210,7 +209,7 @@ (save-account-and-login key-uid multiaccount-data - (ethereum/sha3 (security/safe-unmask-data password)) + (native-module/sha3 (security/safe-unmask-data password)) settings (node/get-new-config db) accounts-data))))) diff --git a/src/status_im/multiaccounts/login/core.cljs b/src/status_im/multiaccounts/login/core.cljs index 33db20de80..3cc6a073e4 100644 --- a/src/status_im/multiaccounts/login/core.cljs +++ b/src/status_im/multiaccounts/login/core.cljs @@ -1,7 +1,6 @@ (ns status-im.multiaccounts.login.core (:require [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [native-module.core :as native-module] [status-im.ui.components.react :as react] [utils.re-frame :as rf] @@ -26,7 +25,7 @@ {::export-db [key-uid (types/clj->json {:name name :key-uid key-uid}) - (ethereum/sha3 (security/safe-unmask-data password)) + (native-module/sha3 (security/safe-unmask-data password)) (fn [path] (when platform/ios? (let [uri (str "file://" path)] @@ -41,4 +40,4 @@ {::import-db [key-uid (types/clj->json {:name name :key-uid key-uid}) - (ethereum/sha3 (security/safe-unmask-data password))]})) + (native-module/sha3 (security/safe-unmask-data password))]})) diff --git a/src/status_im/multiaccounts/reset_password/core.cljs b/src/status_im/multiaccounts/reset_password/core.cljs index e4c92f7cf2..e03b4e6de2 100644 --- a/src/status_im/multiaccounts/reset_password/core.cljs +++ b/src/status_im/multiaccounts/reset_password/core.cljs @@ -1,7 +1,6 @@ (ns status-im.multiaccounts.reset-password.core (:require [clojure.string :as string] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [native-module.core :as native-module] [status-im.popover.core :as popover] [utils.re-frame :as rf] @@ -54,8 +53,8 @@ (fn [[key-uid {:keys [current-password new-password]}]] (native-module/reset-password key-uid - (ethereum/sha3 (security/safe-unmask-data current-password)) - (ethereum/sha3 (security/safe-unmask-data new-password)) + (native-module/sha3 (security/safe-unmask-data current-password)) + (native-module/sha3 (security/safe-unmask-data new-password)) change-db-password-cb))) (rf/defn handle-verification-success @@ -80,7 +79,7 @@ (re-frame/reg-fx ::validate-current-password-and-reset (fn [{:keys [address current-password] :as form-vals}] - (let [hashed-pass (ethereum/sha3 (security/safe-unmask-data current-password))] + (let [hashed-pass (native-module/sha3 (security/safe-unmask-data current-password))] (native-module/verify address hashed-pass (partial handle-verification form-vals))))) diff --git a/src/status_im/network/core.cljs b/src/status_im/network/core.cljs index 40bc7765bd..55b7174b0d 100644 --- a/src/status_im/network/core.cljs +++ b/src/status_im/network/core.cljs @@ -1,11 +1,11 @@ (ns status-im.network.core (:require [clojure.string :as string] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [utils.i18n :as i18n] [status-im.node.core :as node] [utils.re-frame :as rf] - [status-im2.navigation.events :as navigation])) + [status-im2.navigation.events :as navigation] + [utils.ethereum.chain :as chain])) (def url-regex #"https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}(\.[a-z]{2,6})?\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)") @@ -136,7 +136,7 @@ [random-id network-name sym upstream-url chain-type chain-id] (let [data-dir (str "/ethereum/" (name chain-type) "_rpc") config {:NetworkId (or (when chain-id (int chain-id)) - (ethereum/chain-keyword->chain-id chain-type)) + (chain/chain-keyword->chain-id chain-type)) :DataDir data-dir :UpstreamConfig {:Enabled true :URL upstream-url}}] diff --git a/src/status_im/qr_scanner/core.cljs b/src/status_im/qr_scanner/core.cljs index f4030b6340..4468a47c15 100644 --- a/src/status_im/qr_scanner/core.cljs +++ b/src/status_im/qr_scanner/core.cljs @@ -1,13 +1,13 @@ (ns status-im.qr-scanner.core (:require [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [status-im.group-chats.core :as group-chats] [utils.i18n :as i18n] [status-im.router.core :as router] [utils.re-frame :as rf] [status-im.utils.utils :as utils] [status-im2.navigation.events :as navigation] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.ethereum.chain :as chain])) (rf/defn scan-qr-code {:events [::scan-code]} @@ -110,7 +110,7 @@ (rf/defn on-scan {:events [::on-scan-success]} [{:keys [db]} uri] - {::router/handle-uri {:chain (ethereum/chain-keyword db) + {::router/handle-uri {:chain (chain/chain-keyword db) :chats (get db :chats) :uri uri :cb #(re-frame/dispatch [::match-scanned-value %])}}) diff --git a/src/status_im/router/core.cljs b/src/status_im/router/core.cljs index fa066ba429..21b08af811 100644 --- a/src/status_im/router/core.cljs +++ b/src/status_im/router/core.cljs @@ -4,7 +4,6 @@ [re-frame.core :as re-frame] [status-im2.contexts.chat.events :as chat.events] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.eip681 :as eip681] [status-im.ethereum.ens :as ens] [status-im.utils.deprecated-types :as types] @@ -14,7 +13,9 @@ [utils.url :as url] [status-im.utils.wallet-connect :as wallet-connect] [taoensso.timbre :as log] - [utils.security.core :as security])) + [utils.security.core :as security] + [utils.ethereum.chain :as chain] + [utils.address :as address])) (def ethereum-scheme "ethereum:") @@ -93,7 +94,7 @@ (string? user-id) (not (string/blank? user-id)) (not= user-id "0x")) - (let [chain-id (ethereum/chain-keyword->chain-id chain) + (let [chain-id (chain/chain-keyword->chain-id chain) ens-name (stateofus/ens-name-parse user-id) on-success #(match-contact-async chain {:user-id % :ens-name ens-name} callback)] (ens/pubkey chain-id ens-name on-success)) @@ -244,7 +245,7 @@ (= handler :wallet-account) (cb (match-wallet-account route-params)) - (ethereum/address? uri) + (address/address? uri) (cb (address->eip681 uri)) (url/url? uri) diff --git a/src/status_im/signing/core.cljs b/src/status_im/signing/core.cljs index 908890da89..965a9e3438 100644 --- a/src/status_im/signing/core.cljs +++ b/src/status_im/signing/core.cljs @@ -4,8 +4,7 @@ [clojure.string :as string] [re-frame.core :as re-frame] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [status-im.ethereum.tokens :as tokens] [utils.i18n :as i18n] [status-im.keycard.card :as keycard.card] @@ -22,7 +21,10 @@ [status-im.wallet.prices :as prices] [status-im2.common.json-rpc.events :as json-rpc] [taoensso.timbre :as log] - [utils.security.core :as security])) + [utils.security.core :as security] + [utils.address :as address] + [status-im.wallet.utils :as wallet.utils] + [utils.ethereum.chain :as chain])) (re-frame/reg-fx :signing/send-transaction-fx @@ -65,7 +67,7 @@ (let [to (utils.hex/normalize-hex to)] (or (get-in db [:contacts/contacts to]) - {:address (ethereum/normalized-hex to)}))) + {:address (address/normalized-hex to)}))) (rf/defn change-password {:events [:signing.ui/password-is-changed]} @@ -81,8 +83,9 @@ [{{:signing/keys [sign tx] :as db} :db}] (let [{{:keys [data typed? from v4]} :message} tx {:keys [in-progress? password]} sign - from (or from (ethereum/default-address db)) - hashed-password (ethereum/sha3 (security/safe-unmask-data password))] + from (or from (wallet.utils/default-address db)) + hashed-password (native-module/sha3 (security/safe-unmask-data + password))] (when-not in-progress? (merge {:db (update db :signing/sign assoc :error nil :in-progress? true)} @@ -105,10 +108,11 @@ (let [{:keys [in-progress? password]} sign {:keys [tx-obj gas gasPrice maxPriorityFeePerGas maxFeePerGas message nonce]} tx - hashed-password (ethereum/sha3 (security/safe-unmask-data password)) + hashed-password (native-module/sha3 (security/safe-unmask-data + password)) {:keys [action username custom-domain?]} registration {:keys [public-key]} (:profile/profile db) - chain-id (ethereum/chain-id db)] + chain-id (chain/chain-id db)] (if message (sign-message cofx) (let [tx-obj-to-send (merge tx-obj @@ -255,13 +259,13 @@ :value value :amount (str eth-amount) :token (tokens/asset-for (:wallet/all-tokens db) - (ethereum/get-current-network db) + (chain/get-current-network db) :ETH)} (not (nil? token)) token :else {:to to - :contact {:address (ethereum/normalized-hex to)}}))))) + :contact {:address (address/normalized-hex to)}}))))) (defn prepare-tx [db {{:keys [data gas gasPrice maxFeePerGas maxPriorityFeePerGas] :as tx-obj} :tx-obj :as tx}] @@ -276,6 +280,18 @@ :maxPriorityFeePerGas (when maxPriorityFeePerGas (money/bignumber maxPriorityFeePerGas))})) +(defn hex-to-utf8 + [s] + (let [utf8 (native-module/hex-to-utf8 s)] + (if (empty? utf8) + nil + utf8))) + +(defn hex->text + "Converts a hexstring to UTF8 text." + [data] + (or (hex-to-utf8 data) data)) + (rf/defn show-sign [{:keys [db] :as cofx}] (let [{:signing/keys [queue]} db @@ -297,7 +313,7 @@ :formatted-data (if typed? (types/js->pretty-json (types/json->js data)) - (ethereum/hex->text data)) + (hex->text data)) :keycard-step (when pinless? :connect)}) :show-signing-sheet nil} #(when-not wallet-set-up-passed? @@ -338,7 +354,7 @@ {:success-callback #(re-frame/dispatch [:wallet.send/update-gas-price-success :signing/tx % tx-obj]) :error-callback #(re-frame/dispatch [:signing/update-gas-price-error %]) - :network-id (get-in (ethereum/current-network db) + :network-id (get-in (chain/current-network db) [:config :NetworkId])}}))))) (rf/defn check-queue @@ -355,7 +371,7 @@ ;; big-int :params [chat-id (str value) contract transaction-hash (or (:result (types/json->clj signature)) - (ethereum/normalized-hex signature))] + (address/normalized-hex signature))] :js-response true :on-success #(re-frame/dispatch [:transport/message-sent %])}]}) @@ -366,7 +382,7 @@ {:json-rpc/call [{:method "wakuext_acceptRequestTransaction" :params [transaction-hash message-id (or (:result (types/json->clj signature)) - (ethereum/normalized-hex signature))] + (address/normalized-hex signature))] :js-response true :on-success #(re-frame/dispatch [:transport/message-sent %])}]}) @@ -496,7 +512,7 @@ (defn normalize-tx-obj [db tx] - (update-in tx [:tx-obj :from] #(eip55/address->checksum (or % (ethereum/default-address db))))) + (update-in tx [:tx-obj :from] #(eip55/address->checksum (or % (wallet.utils/default-address db))))) (rf/defn sign "Signing transaction or message, shows signing sheet @@ -516,7 +532,7 @@ [{:keys [db] :as cofx} {:keys [to amount from token]}] (let [{:keys [symbol address]} token amount-hex (str "0x" (native-module/number-to-hex amount)) - to-norm (ethereum/normalized-hex (if (string? to) to (:address to))) + to-norm (address/normalized-hex (if (string? to) to (:address to))) from-address (:address from) identity (:current-chat-id db) db (dissoc db :wallet/prepare-transaction :signing/edit-fee)] @@ -530,7 +546,7 @@ :chat-id identity :command? true :value amount-hex} - {:to (ethereum/normalized-hex address) + {:to (address/normalized-hex address) :from from-address :chat-id identity :command? true @@ -566,7 +582,7 @@ :chat-id chat-id :command? true :value amount-hex} - {:to (ethereum/normalized-hex address) + {:to (address/normalized-hex address) :from from-address :command? true :message-id (:id request-parameters) @@ -578,7 +594,7 @@ [{:keys [db] :as cofx} {:keys [to amount from token gas gasPrice maxFeePerGas maxPriorityFeePerGas]}] (let [{:keys [symbol address]} token amount-hex (str "0x" (native-module/number-to-hex amount)) - to-norm (ethereum/normalized-hex (if (string? to) to (:address to))) + to-norm (address/normalized-hex (if (string? to) to (:address to))) from-address (:address from)] (rf/merge cofx {:db (dissoc db :wallet/prepare-transaction :signing/edit-fee)} @@ -596,7 +612,7 @@ (if (= symbol :ETH) {:to to-norm :value amount-hex} - {:to (ethereum/normalized-hex address) + {:to (address/normalized-hex address) :data (native-module/encode-transfer to-norm amount-hex)}))})))) (re-frame/reg-fx diff --git a/src/status_im/signing/gas.cljs b/src/status_im/signing/gas.cljs index 68644e80f0..3ece87856d 100644 --- a/src/status_im/signing/gas.cljs +++ b/src/status_im/signing/gas.cljs @@ -2,14 +2,14 @@ (: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] [utils.i18n :as i18n] [status-im.popover.core :as popover] [status-im.signing.eip1559 :as eip1559] [utils.re-frame :as rf] [utils.money :as money] [status-im2.common.json-rpc.events :as json-rpc] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.ethereum.chain :as chain])) (def min-gas-price-wei ^js (money/bignumber 1)) @@ -409,7 +409,7 @@ (check-base-fee (assoc fee-history :testnet? - (ethereum/testnet? + (chain/testnet? (get-in networks [current-network :config :NetworkId]))))] (merge {:max-priority-fee (max-priority-fee-hex (money/bignumber %) current-base-fee)} diff --git a/src/status_im/stickers/core.cljs b/src/status_im/stickers/core.cljs index 5f3617f1fe..1403f13ffb 100644 --- a/src/status_im/stickers/core.cljs +++ b/src/status_im/stickers/core.cljs @@ -2,11 +2,12 @@ (:require [clojure.string :as string] [re-frame.core :as re-frame] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] [status-im2.config :as config] [utils.re-frame :as rf] [status-im.utils.utils :as utils] - [status-im2.navigation.events :as navigation])) + [status-im2.navigation.events :as navigation] + [utils.ethereum.chain :as chain] + [status-im.wallet.utils :as wallet.utils])) (re-frame/reg-fx :stickers/set-pending-timeout-fx @@ -21,14 +22,14 @@ cofx {:db (assoc-in db [:stickers/packs id :status] constants/sticker-pack-status-installed) :json-rpc/call [{:method "stickers_install" - :params [(ethereum/chain-id db) id] + :params [(chain/chain-id db) id] :on-success #()}]})) (rf/defn load-packs {:events [:stickers/load-packs]} [{:keys [db]}] {:json-rpc/call [{:method "stickers_market" - :params [(ethereum/chain-id db)] + :params [(chain/chain-id db)] :on-success #(re-frame/dispatch [:stickers/stickers-market-success %])} {:method "stickers_installed" :params [] @@ -44,7 +45,7 @@ {:events [:stickers/buy-pack]} [{db :db} pack-id] {:json-rpc/call [{:method "stickers_buyPrepareTx" - :params [(ethereum/chain-id db) (ethereum/default-address db) (int pack-id)] + :params [(chain/chain-id db) (wallet.utils/default-address db) (int pack-id)] :on-success #(re-frame/dispatch [:signing.ui/sign {:tx-obj % :on-result [:stickers/pending-pack pack-id]}])}]}) @@ -58,7 +59,7 @@ (update :stickers/packs-pending conj id)) :stickers/set-pending-timeout-fx nil :json-rpc/call [{:method "stickers_addPending" - :params [(ethereum/chain-id db) (int id)] + :params [(chain/chain-id db) (int id)] :on-success #()}]}) (rf/defn pending-timeout @@ -66,7 +67,7 @@ [{{:stickers/keys [packs-pending] :as db} :db}] (when (seq packs-pending) {:json-rpc/call [{:method "stickers_processPending" - :params [(ethereum/chain-id db)] + :params [(chain/chain-id db)] :on-success #(re-frame/dispatch [:stickers/stickers-process-pending-success %])}]})) diff --git a/src/status_im/ui/screens/ens/views.cljs b/src/status_im/ui/screens/ens/views.cljs index f3fc72bb78..c457a6a27e 100644 --- a/src/status_im/ui/screens/ens/views.cljs +++ b/src/status_im/ui/screens/ens/views.cljs @@ -6,7 +6,6 @@ [re-frame.core :as re-frame] [reagent.core :as reagent] [status-im.ens.core :as ens] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.ens :as ethereum.ens] [status-im.ethereum.stateofus :as stateofus] [status-im.ethereum.tokens :as tokens] @@ -23,7 +22,8 @@ [status-im.ui.screens.profile.components.views :as profile.components] [status-im.ui.screens.wallet.send.sheets :as sheets] [status-im.utils.utils :as utils] - [utils.debounce :as debounce]) + [utils.debounce :as debounce] + [utils.address :as address]) (:require-macros [status-im.utils.views :as views])) (defn- link @@ -547,7 +547,7 @@ (when-not pending? [section {:title (i18n/label :t/wallet-address) - :content (ethereum/normalized-hex address)}]) + :content (address/normalized-hex address)}]) (when-not pending? [react/view {:style {:margin-top 14}} [section diff --git a/src/status_im/ui/screens/privacy_and_security_settings/events.cljs b/src/status_im/ui/screens/privacy_and_security_settings/events.cljs index e58180f9da..1d3954b58e 100644 --- a/src/status_im/ui/screens/privacy_and_security_settings/events.cljs +++ b/src/status_im/ui/screens/privacy_and_security_settings/events.cljs @@ -1,7 +1,6 @@ (ns status-im.ui.screens.privacy-and-security-settings.events (:require [clojure.string :as string] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [utils.i18n :as i18n] [native-module.core :as native-module] [utils.re-frame :as rf] @@ -20,7 +19,7 @@ (let [hashed-password (-> masked-password security/safe-unmask-data - ethereum/sha3)] + native-module/sha3)] (native-module/verify address hashed-password diff --git a/src/status_im/ui/screens/wallet/account/views.cljs b/src/status_im/ui/screens/wallet/account/views.cljs index 6de722aa2e..d9baf17cf2 100644 --- a/src/status_im/ui/screens/wallet/account/views.cljs +++ b/src/status_im/ui/screens/wallet/account/views.cljs @@ -8,7 +8,6 @@ [quo2.foundations.colors :as quo2.colors] [re-frame.core :as re-frame] [reagent.core :as reagent] - [status-im.ethereum.core :as ethereum] [utils.i18n :as i18n] [status-im.ui.components.animation :as animation] [status-im.ui.components.icons.icons :as icons] @@ -22,7 +21,8 @@ [status-im.ui.screens.wallet.collectibles.views :as collectibles.views] [status-im.ui.screens.wallet.transactions.views :as history] [status-im2.config :as config] - [utils.re-frame :as rf]) + [utils.re-frame :as rf] + [utils.address :as address]) (:require-macros [status-im.utils.views :as views])) (def state (reagent/atom {:tab :assets})) @@ -61,7 +61,7 @@ :style {:width (/ window-width 3) :line-height 22 :color colors/white-transparent-70-persist}} - (ethereum/normalized-hex address)]] + (address/normalized-hex address)]] [react/view {:position :absolute :top 12 :right 12} [react/touchable-highlight {:on-press #(re-frame/dispatch [:wallet/share-popover address])} [icons/icon :main-icons/share diff --git a/src/status_im/ui/screens/wallet/add_new/views.cljs b/src/status_im/ui/screens/wallet/add_new/views.cljs index 589fa960de..a87865705d 100644 --- a/src/status_im/ui/screens/wallet/add_new/views.cljs +++ b/src/status_im/ui/screens/wallet/add_new/views.cljs @@ -6,7 +6,6 @@ [quo.design-system.colors :as colors] [re-frame.core :as re-frame] [reagent.core :as reagent] - [status-im.ethereum.core :as ethereum] [utils.i18n :as i18n] [status-im.multiaccounts.db :as multiaccounts.db] [status-im.ui.components.icons.icons :as icons] @@ -15,7 +14,8 @@ [status-im.ui.components.topbar :as topbar] [status-im.ui.screens.keycard.pin.views :as pin.views] [status-im.ui.screens.wallet.account-settings.views :as account-settings] - [utils.security.core :as security])) + [utils.security.core :as security] + [native-module.core :as native-module])) (defn add-account-topbar [type] @@ -179,7 +179,7 @@ {:view {:content pin :height 256}}]) #(re-frame/dispatch [:wallet.accounts/add-new-account - (ethereum/sha3 @entered-password)])) + (native-module/sha3 @entered-password)])) :disabled (or add-account-disabled? (and diff --git a/src/status_im/ui/screens/wallet/recipient/views.cljs b/src/status_im/ui/screens/wallet/recipient/views.cljs index 0ce8fc68d5..80cd0bd46e 100644 --- a/src/status_im/ui/screens/wallet/recipient/views.cljs +++ b/src/status_im/ui/screens/wallet/recipient/views.cljs @@ -4,7 +4,6 @@ [quo.design-system.colors :as colors] [re-frame.core :as re-frame] [reagent.core :as reagent] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.stateofus :as stateofus] [utils.i18n :as i18n] [status-im.ui.components.chat-icon.screen :as chat-icon] @@ -17,7 +16,8 @@ [status-im.ui.screens.wallet.components.views :as components] [status-im.utils.utils :as utils] [utils.debounce :as debounce] - [utils.string :as utils.string]) + [utils.string :as utils.string] + [utils.address :as address]) (:require-macros [status-im.utils.views :as views])) (defn- recipient-topbar @@ -317,7 +317,7 @@ :size :small :align :center :color :secondary} - (when-not (ethereum/address? address) + (when-not (address/address? address) (str (stateofus/username-with-domain address) " • ")) [quo/text {:monospace true diff --git a/src/status_im/ui/screens/wallet/request/views.cljs b/src/status_im/ui/screens/wallet/request/views.cljs index e43b680779..77e3efa176 100644 --- a/src/status_im/ui/screens/wallet/request/views.cljs +++ b/src/status_im/ui/screens/wallet/request/views.cljs @@ -2,7 +2,7 @@ (:require [quo.core :as quo] [re-frame.core :as re-frame] [reagent.core :as reagent] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [status-im.ethereum.eip681 :as eip681] [utils.i18n :as i18n] [status-im.ui.components.copyable-text :as copyable-text] diff --git a/src/status_im/ui/screens/wallet/send/views.cljs b/src/status_im/ui/screens/wallet/send/views.cljs index 2f65f67bd1..4b21f2db4e 100644 --- a/src/status_im/ui/screens/wallet/send/views.cljs +++ b/src/status_im/ui/screens/wallet/send/views.cljs @@ -5,7 +5,6 @@ [quo.design-system.colors :as colors] [re-frame.core :as re-frame] [status-im.commands.core :as commands] - [status-im.ethereum.core :as ethereum] [utils.i18n :as i18n] [status-im.multiaccounts.core :as multiaccounts] [status-im.ui.components.bottom-panel.views :as bottom-panel] @@ -20,7 +19,8 @@ [status-im.ui.screens.wallet.send.styles :as styles] [utils.money :as money] [status-im.utils.utils :as utils] - [status-im.wallet.utils :as wallet.utils])) + [status-im.wallet.utils :as wallet.utils] + [utils.address :as address])) (defn header [{:keys [label small-screen?]}] @@ -232,7 +232,7 @@ prices [:prices] wallet-currency [:wallet/currency] window-width [:dimensions/window-width]] - (let [to-norm (ethereum/normalized-hex (if (string? to) to (:address to)))] + (let [to-norm (address/normalized-hex (if (string? to) to (:address to)))] [kb-presentation/keyboard-avoiding-view {:style {:flex 1}} [:<> [quo2/page-nav diff --git a/src/status_im/utils/universal_links/core.cljs b/src/status_im/utils/universal_links/core.cljs index 24031297c1..07ddb4cec4 100644 --- a/src/status_im/utils/universal_links/core.cljs +++ b/src/status_im/utils/universal_links/core.cljs @@ -3,7 +3,6 @@ [goog.string :as gstring] [re-frame.core :as re-frame] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] [status-im.group-chats.core :as group-chats] [utils.i18n :as i18n] [status-im.multiaccounts.model :as multiaccounts.model] @@ -13,7 +12,8 @@ [status-im.wallet.choose-recipient.core :as choose-recipient] [status-im2.navigation.events :as navigation] [taoensso.timbre :as log] - [native-module.core :as native-module])) + [native-module.core :as native-module] + [utils.ethereum.chain :as chain])) ;; TODO(yenda) investigate why `handle-universal-link` event is ;; dispatched 7 times for the same link @@ -161,7 +161,7 @@ (rf/defn route-url "Match a url against a list of routes and handle accordingly" [{:keys [db]} url] - {::router/handle-uri {:chain (ethereum/chain-keyword db) + {::router/handle-uri {:chain (chain/chain-keyword db) :chats (:chats db) :uri url :cb #(re-frame/dispatch [::match-value url %])}}) @@ -215,8 +215,8 @@ ;;It can be called after the error "route ip+net: netlinkrib: permission denied" is fixed on status-go ;;side #_(native-module/start-searching-for-local-pairing-peers - #(log/info "[local-pairing] errors from local-pairing-preflight-outbound-check ->" %)) -) + #(log/info "[local-pairing] errors from local-pairing-preflight-outbound-check ->" %))) + (defn finalize "Remove event listener for url" diff --git a/src/status_im/utils/utils.cljs b/src/status_im/utils/utils.cljs index ac5bae52a9..3a47da6245 100644 --- a/src/status_im/utils/utils.cljs +++ b/src/status_im/utils/utils.cljs @@ -4,9 +4,9 @@ [clojure.string :as string] [goog.string :as gstring] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] - [status-im.ethereum.eip55 :as eip55] - [utils.i18n :as i18n])) + [utils.ethereum.eip.eip55 :as eip55] + [utils.i18n :as i18n] + [utils.address :as address])) ;;TODO (14/11/22 flexsurfer) .-Alert usage code has been moved to the status-im2 namespace, we keep this ;;only for old (status 1.0) code, @@ -106,7 +106,7 @@ (defn get-shortened-checksum-address [address] (when address - (get-shortened-address (eip55/address->checksum (ethereum/normalized-hex address))))) + (get-shortened-address (eip55/address->checksum (address/normalized-hex address))))) ;;TODO (14/11/22 flexsurfer) haven't moved yet (defn format-decimals diff --git a/src/status_im/wallet/accounts/core.cljs b/src/status_im/wallet/accounts/core.cljs index 943e5dabcb..2708af0ec1 100644 --- a/src/status_im/wallet/accounts/core.cljs +++ b/src/status_im/wallet/accounts/core.cljs @@ -5,8 +5,7 @@ [re-frame.core :as re-frame] [status-im2.constants :as constants] [status-im.ens.core :as ens.core] - [status-im.ethereum.core :as ethereum] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [status-im.ethereum.eip681 :as eip681] [status-im.ethereum.mnemonic :as mnemonic] [status-im.ethereum.stateofus :as stateofus] @@ -23,7 +22,9 @@ [status-im.wallet.prices :as prices] [status-im2.navigation.events :as navigation] [taoensso.timbre :as log] - [utils.security.core :as security])) + [utils.security.core :as security] + [utils.ethereum.chain :as chain] + [utils.address :as address])) (rf/defn start-adding-new-account {:events [:wallet.accounts/start-adding-new-account]} @@ -249,7 +250,7 @@ [{:keys [db] :as cofx}] (let [address (get-in db [:add-account :address])] (account-generated cofx - {:address (eip55/address->checksum (ethereum/normalized-hex address)) + {:address (eip55/address->checksum (address/normalized-hex address)) :type :watch}))) (rf/defn add-new-account-password-verifyied @@ -282,7 +283,7 @@ (cond-> {:db (assoc-in db [:add-account :address] account)} name? (assoc ::ens.core/resolve-address - [(ethereum/chain-id db) + [(chain/chain-id db) (stateofus/ens-name-parse account) #(re-frame/dispatch [:wallet.accounts/set-account-to-watch %])])))) @@ -336,7 +337,7 @@ (re-frame/reg-fx :key-storage/delete-imported-key (fn [{:keys [key-uid address password on-success on-error]}] - (let [hashed-pass (ethereum/sha3 (security/safe-unmask-data password))] + (let [hashed-pass (native-module/sha3 (security/safe-unmask-data password))] (native-module/delete-imported-key key-uid (string/lower-case (subs address 2)) diff --git a/src/status_im/wallet/choose_recipient/core.cljs b/src/status_im/wallet/choose_recipient/core.cljs index e7e1abf35e..cd3bb01ed3 100644 --- a/src/status_im/wallet/choose_recipient/core.cljs +++ b/src/status_im/wallet/choose_recipient/core.cljs @@ -2,7 +2,6 @@ (:require [re-frame.core :as re-frame] [status-im.bottom-sheet.events :as bottom-sheet] [status-im.contact.db :as contact.db] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.eip681 :as eip681] [status-im.ethereum.ens :as ens] [utils.i18n :as i18n] @@ -13,7 +12,9 @@ [utils.money :as money] [status-im.utils.universal-links.utils :as links] [status-im.utils.wallet-connect :as wallet-connect] - [status-im2.navigation.events :as navigation])) + [status-im2.navigation.events :as navigation] + [utils.ethereum.chain :as chain] + [status-im.wallet.utils :as wallet.utils])) ;; FIXME(Ferossgp): Should be part of QR scanner not wallet (rf/defn toggle-flashlight @@ -66,7 +67,7 @@ :wallet/prepare-transaction (cond-> {:to address :to-name (or name (find-address-name db address)) - :from (ethereum/get-default-account + :from (wallet.utils/get-default-account (get db :profile/wallet-accounts))} gas (assoc :gas (money/bignumber gas)) gas-limit (assoc :gas (money/bignumber gas-limit)) @@ -106,7 +107,7 @@ {:success-callback #(re-frame/dispatch [:wallet.send/update-gas-price-success :wallet/prepare-transaction %]) - :network-id (get-in (ethereum/current-network db) + :network-id (get-in (chain/current-network db) [:config :NetworkId])}}) (when (and chain-id (not= current-chain-id chain-id)) {:ui/show-error (i18n/label :t/wallet-invalid-chain-id @@ -129,7 +130,7 @@ ;; if there are no ens-names, we dispatch request-uri-parsed immediately (request-uri-parsed cofx message uri) {::resolve-addresses - {:chain-id (ethereum/chain-id db) + {:chain-id (chain/chain-id db) :ens-names ens-names :callback (fn [addresses] diff --git a/src/status_im/wallet/core.cljs b/src/status_im/wallet/core.cljs index dbd745dcd3..32557b7b5b 100644 --- a/src/status_im/wallet/core.cljs +++ b/src/status_im/wallet/core.cljs @@ -6,8 +6,7 @@ [react-native.async-storage :as async-storage] [status-im.bottom-sheet.events :as bottom-sheet] [status-im.contact.db :as contact.db] - [status-im.ethereum.core :as ethereum] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [status-im.ethereum.ens :as ens] [status-im.ethereum.stateofus :as stateofus] [status-im.ethereum.tokens :as tokens] @@ -31,7 +30,8 @@ [status-im2.navigation.events :as navigation] [taoensso.timbre :as log] [status-im.utils.mobile-sync :as utils.mobile-sync] - [native-module.core :as native-module])) + [native-module.core :as native-module] + [utils.ethereum.chain :as chain])) (defn get-balance [{:keys [address on-success on-error]}] @@ -158,8 +158,8 @@ (let [addresses (map (comp string/lower-case :address) (get db :profile/wallet-accounts)) chain-id (-> db - ethereum/current-network - ethereum/network->chain-id)] + chain/current-network + chain/network->chain-id)] (when (get-in db [:profile/profile :opensea-enabled?]) {:json-rpc/call (map (fn [address] @@ -186,7 +186,7 @@ (rf/defn fetch-collectible-assets-by-owner-and-collection {:events [::fetch-collectible-assets-by-owner-and-collection]} [{:keys [db]} address collectible-slug limit] - (let [chain-id (ethereum/network->chain-id (ethereum/current-network db))] + (let [chain-id (chain/network->chain-id (chain/current-network db))] {:db (assoc-in db [:wallet/fetching-collection-assets collectible-slug] true) :json-rpc/call [{:method "wallet_getOpenseaAssetsByOwnerAndCollection" :params [chain-id address collectible-slug limit] @@ -243,7 +243,7 @@ (let [addresses (or addresses (map (comp string/lower-case :address) wallet-accounts)) {:keys [:wallet/visible-tokens]} profile - chain (ethereum/chain-keyword db) + chain (chain/chain-keyword db) assets (get visible-tokens chain) tokens (->> (vals all-tokens) (remove #(or (:hidden? %) @@ -310,7 +310,7 @@ (rf/defn update-toggle-in-settings [{{:profile/keys [profile] :as db} :db :as cofx} symbol checked?] - (let [chain (ethereum/chain-keyword db) + (let [chain (chain/chain-keyword db) visible-tokens (get profile :wallet/visible-tokens)] (rf/merge cofx (multiaccounts.update/multiaccount-update @@ -355,7 +355,7 @@ (rf/defn configure-token-balance-and-visibility {:events [::tokens-found]} [{:keys [db] :as cofx} balances] - (let [chain (ethereum/chain-keyword db) + (let [chain (chain/chain-keyword db) visible-tokens (get-in db [:profile/profile :wallet/visible-tokens]) chain-visible-tokens (into (or (config/default-visible-tokens chain) #{}) @@ -459,11 +459,12 @@ {:keys [symbol decimals]} (if (seq contract) (get all-tokens contract) - (tokens/native-currency (ethereum/get-current-network db))) + (tokens/native-currency (chain/get-current-network db))) amount-text (str (money/internal->formatted value symbol decimals))] {:db (assoc db :wallet/prepare-transaction - {:from (ethereum/get-default-account (:profile/wallet-accounts db)) + {:from (wallet.utils/get-default-account (:profile/wallet-accounts + db)) :to (or (get-in db [:contacts/contacts identity]) (-> identity contact.db/public-key->new-contact @@ -503,7 +504,7 @@ :signing/update-gas-price {:success-callback #(re-frame/dispatch [:wallet.send/update-gas-price-success :wallet/prepare-transaction %]) - :network-id (get-in (ethereum/current-network db) + :network-id (get-in (chain/current-network db) [:config :NetworkId])}}) (rf/defn prepare-transaction-from-chat @@ -517,7 +518,7 @@ contact.db/enrich-contact))] (cond-> {:db (assoc db :wallet/prepare-transaction - {:from (ethereum/get-default-account + {:from (wallet.utils/get-default-account (:profile/wallet-accounts db)) :to contact :symbol :ETH @@ -525,7 +526,7 @@ :dispatch [:open-modal :prepare-send-transaction]} ens-verified (assoc ::resolve-address - {:chain-id (ethereum/chain-id db) + {:chain-id (chain/chain-id db) :ens-name (if (= (.indexOf ^js name ".") -1) (stateofus/subdomain name) name) @@ -538,7 +539,7 @@ (let [identity (:current-chat-id db)] {:db (assoc db :wallet/prepare-transaction - {:from (ethereum/get-default-account (:profile/wallet-accounts db)) + {:from (wallet.utils/get-default-account (:profile/wallet-accounts db)) :to (or (get-in db [:contacts/contacts identity]) (-> identity contact.db/public-key->new-contact @@ -561,7 +562,7 @@ :signing/update-gas-price {:success-callback #(re-frame/dispatch [:wallet.send/update-gas-price-success :wallet/prepare-transaction %]) - :network-id (get-in (ethereum/current-network db) + :network-id (get-in (chain/current-network db) [:config :NetworkId])}}) (rf/defn cancel-transaction-command @@ -671,13 +672,15 @@ nil (get-in db [:wallet :accounts]))) +(defn custom-rpc-node? [{:keys [id]}] (= 45 (count id))) + (defn get-restart-interval [db] (let [max-block (get-max-block-with-transfer db) custom-interval (get db :wallet-service/custom-interval)] (cond - (ethereum/custom-rpc-node? - (ethereum/current-network db)) + (custom-rpc-node? + (chain/current-network db)) ms-2-min (and max-block @@ -690,8 +693,8 @@ (defn get-watching-interval [db] - (if (ethereum/custom-rpc-node? - (ethereum/current-network db)) + (if (custom-rpc-node? + (chain/current-network db)) ms-2-min ms-10-min)) @@ -734,7 +737,7 @@ :as params}] (when (:profile/profile db) (let [syncing-allowed? (utils.mobile-sync/syncing-allowed? cofx) - binance-chain? (ethereum/binance-chain? db)] + binance-chain? (chain/binance-chain? db)] (log/info "restart-wallet-service" "force-restart?" force-restart? "syncing-allowed?" syncing-allowed? @@ -778,7 +781,7 @@ (rf/defn transaction-included {:events [::transaction-included]} [{:keys [db] :as cofx} address tx-hash] - (if (ethereum/binance-chain? db) + (if (chain/binance-chain? db) (load-transaction-by-hash cofx address tx-hash) (restart cofx true))) @@ -813,7 +816,7 @@ (rf/defn watch-tx {:events [:watch-tx]} [{:keys [db] :as cofx} address tx-id] - (let [chain-id (ethereum/chain-id db)] + (let [chain-id (chain/chain-id db)] {::start-watching [[address tx-id chain-id]]})) (rf/defn clear-timeouts @@ -889,8 +892,8 @@ (rf/defn stop-fetching-on-empty-tx-history [{:keys [db now] :as cofx} transfers] (let [non-empty-history? (get db :wallet/non-empty-tx-history?) - custom-node? (ethereum/custom-rpc-node? - (ethereum/current-network db)) + custom-node? (custom-rpc-node? + (chain/current-network db)) until-ms (get db :wallet/keep-watching-until-ms)] (when-not (and until-ms (> until-ms now)) (rf/merge @@ -1056,7 +1059,7 @@ [network-id tokens] (mapv #(-> % (update :symbol keyword) - ((partial tokens/update-icon (ethereum/chain-id->chain-keyword (int network-id))))) + ((partial tokens/update-icon (chain/chain-id->chain-keyword (int network-id))))) tokens)) (re-frame/reg-fx @@ -1166,7 +1169,8 @@ (fn [already-seen] (when (and (not already-seen) (boolean (get invalid-addrr - (ethereum/sha3 (string/lower-case (ethereum/default-address db)))))) + (native-module/sha3 (string/lower-case (wallet.utils/default-address + db)))))) (utils.utils/show-popup (i18n/label :t/warning) (i18n/label :t/ens-username-invalid-name-warning) @@ -1190,7 +1194,7 @@ ;; NOTE: Local notifications should be enabled only after wallet was started ::enable-local-notifications nil :dispatch-n [(when (or (not (utils.mobile-sync/syncing-allowed? cofx)) - (ethereum/binance-chain? db)) + (chain/binance-chain? db)) [:transaction/get-fetched-transfers])]} (check-invalid-ens) (initialize-tokens tokens custom-tokens) @@ -1211,7 +1215,7 @@ (get-cached-balances scan-all-tokens?)) (when-not (get db :wallet/new-account) (restart-wallet-service nil)) - (when (ethereum/binance-chain? db) + (when (chain/binance-chain? db) (request-current-block-update)) (prices/update-prices))) diff --git a/src/status_im/wallet/custom_tokens/core.cljs b/src/status_im/wallet/custom_tokens/core.cljs index 9d28f9c7d4..a3cf7b947f 100644 --- a/src/status_im/wallet/custom_tokens/core.cljs +++ b/src/status_im/wallet/custom_tokens/core.cljs @@ -2,13 +2,14 @@ (:require [clojure.string :as string] [quo.design-system.colors :as colors] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [utils.i18n :as i18n] [status-im.ui.components.react :as react] [utils.re-frame :as rf] [status-im.wallet.core :as wallet] [status-im.wallet.prices :as prices] - [status-im2.navigation.events :as navigation])) + [status-im2.navigation.events :as navigation] + [utils.ethereum.chain :as chain] + [utils.address :as address])) (re-frame/reg-fx :wallet.custom-token/contract-address-paste @@ -28,7 +29,7 @@ (rf/defn contract-address-is-changed {:events [:wallet.custom-token/contract-address-is-pasted]} [{:keys [db]} contract] - (if (ethereum/address? contract) + (if (address/address? contract) (if (token-in-list? db contract) {:db (assoc db :wallet/custom-token-screen @@ -37,7 +38,7 @@ :wallet/custom-token-screen {:contract contract :in-progress? true}) :json-rpc/call [{:method "wallet_discoverToken" - :params [(ethereum/chain-id db) contract] + :params [(chain/chain-id db) contract] :on-success #(re-frame/dispatch [:wallet.custom-token/token-discover-result %]) :on-error #(re-frame/dispatch [:wallet.custom-token/not-supported])}]}) {:db (assoc db diff --git a/src/status_im/wallet/prices.cljs b/src/status_im/wallet/prices.cljs index 8067d626ff..a92f34d066 100644 --- a/src/status_im/wallet/prices.cljs +++ b/src/status_im/wallet/prices.cljs @@ -1,13 +1,13 @@ (ns status-im.wallet.prices (:require [clojure.set :as set] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.tokens :as tokens] [status-im.utils.currency :as currency] [utils.re-frame :as rf] [status-im.utils.prices :as prices] [status-im.wallet.utils :as wallet.utils] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.ethereum.chain :as chain])) (defn assoc-error-message [db error-type err] @@ -54,7 +54,7 @@ :profile/profile :as db} :db}] - (let [chain (ethereum/chain-keyword db) + (let [chain (chain/chain-keyword db) mainnet? (= :mainnet chain) assets (get visible-tokens chain #{}) tokens (tokens-symbols assets all-tokens) @@ -63,7 +63,7 @@ {:wallet/get-prices {:from (if mainnet? (conj tokens "ETH") - [(-> (tokens/native-currency (ethereum/get-current-network db)) + [(-> (tokens/native-currency (chain/get-current-network db)) (wallet.utils/exchange-symbol))]) :to [(:code currency)] :mainnet? mainnet? diff --git a/src/status_im/wallet/recipient/core.cljs b/src/status_im/wallet/recipient/core.cljs index 97f6b0ef67..8d3918eda7 100644 --- a/src/status_im/wallet/recipient/core.cljs +++ b/src/status_im/wallet/recipient/core.cljs @@ -1,8 +1,7 @@ (ns status-im.wallet.recipient.core (:require [clojure.string :as string] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] - [status-im.ethereum.eip55 :as eip55] + [utils.ethereum.eip.eip55 :as eip55] [status-im.ethereum.ens :as ens] [status-im.ethereum.stateofus :as stateofus] [utils.i18n :as i18n] @@ -12,7 +11,9 @@ [status-im.utils.utils :as utils] [status-im2.common.json-rpc.events :as json-rpc] [status-im2.navigation.events :as navigation] - [utils.string :as utils.string])) + [utils.string :as utils.string] + [utils.ethereum.chain :as chain] + [utils.address :as address])) ;;NOTE we want to handle only last resolve (def resolve-last-id (atom nil)) @@ -40,7 +41,7 @@ (reset! resolve-last-id nil) (let [recipient (utils.string/safe-trim raw-recipient)] (cond - (ethereum/address? recipient) + (address/address? recipient) (let [checksum (eip55/address->checksum recipient)] (if (eip55/valid-address-checksum? checksum) (rf/merge cofx @@ -67,7 +68,7 @@ (do (reset! resolve-last-id (random/id)) {::resolve-address - {:chain-id (ethereum/chain-id db) + {:chain-id (chain/chain-id db) :ens-name ens-name :cb #(re-frame/dispatch [::recipient-address-resolved % @resolve-last-id])}}) {:db (assoc-in db [:wallet/recipient :searching] false)})) diff --git a/src/status_im/wallet/utils.cljs b/src/status_im/wallet/utils.cljs index 6e629e5a42..4ff53d3270 100644 --- a/src/status_im/wallet/utils.cljs +++ b/src/status_im/wallet/utils.cljs @@ -24,3 +24,13 @@ (name (or (:symbol-exchange m) (:symbol-display m) (:symbol m)))) + +(defn get-default-account + [accounts] + (some #(when (:wallet %) %) accounts)) + +(defn default-address + [db] + (-> (get db :profile/wallet-accounts) + get-default-account + :address)) diff --git a/src/status_im/wallet_connect/core.cljs b/src/status_im/wallet_connect/core.cljs index ee394962d7..7c6a79c093 100644 --- a/src/status_im/wallet_connect/core.cljs +++ b/src/status_im/wallet_connect/core.cljs @@ -3,13 +3,13 @@ [re-frame.core :as re-frame] [status-im.browser.core :as browser] [status-im2.constants :as constants] - [status-im.ethereum.core :as ethereum] [status-im.signing.core :as signing] [status-im2.config :as config] [utils.re-frame :as rf] [status-im.utils.deprecated-types :as types] [status-im.utils.wallet-connect :as wallet-connect] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [utils.address :as address])) (rf/defn proposal-handler {:events [:wallet-connect/proposal]} @@ -149,7 +149,7 @@ supported-chain-ids (filter (fn [chain-id] #(boolean (some #{chain-id} available-chain-ids))) proposal-chain-ids) address (:address account) - accounts (map #(str "eip155:" % ":" (ethereum/normalized-hex address)) + accounts (map #(str "eip155:" % ":" (address/normalized-hex address)) supported-chain-ids) ;; TODO: Check for unsupported metadata (get db :wallet-connect/proposal-metadata) @@ -179,7 +179,7 @@ available-chain-ids (map #(get-in % [:config :NetworkId]) (vals (get db :networks/networks))) supported-chain-ids (filter (fn [chain-id] #(boolean (some #{chain-id} available-chain-ids))) proposal-chain-ids) - accounts (map #(str "eip155:" % ":" (ethereum/normalized-hex address)) + accounts (map #(str "eip155:" % ":" (address/normalized-hex address)) supported-chain-ids)] {:db (assoc db :wallet-connect/showing-app-management-sheet? diff --git a/src/status_im2/common/password_authentication/view.cljs b/src/status_im2/common/password_authentication/view.cljs index d46058955e..12eb2569c4 100644 --- a/src/status_im2/common/password_authentication/view.cljs +++ b/src/status_im2/common/password_authentication/view.cljs @@ -4,8 +4,8 @@ [utils.re-frame :as rf] [status-im.multiaccounts.core :as multiaccounts] [utils.i18n :as i18n] - [status-im.ethereum.core :as ethereum] - [reagent.core :as reagent])) + [reagent.core :as reagent] + [native-module.core :as native-module])) (defn view [] @@ -37,5 +37,5 @@ (i18n/label :t/oops-wrong-password)]) [quo/button {:container-style {:margin-bottom 12 :margin-top 40} - :on-press #((:on-press button) (ethereum/sha3 @entered-password))} + :on-press #((:on-press button) (native-module/sha3 @entered-password))} (:label button)]])))) diff --git a/src/status_im2/config.cljs b/src/status_im2/config.cljs index 46970abc41..68b925f0ad 100644 --- a/src/status_im2/config.cljs +++ b/src/status_im2/config.cljs @@ -1,8 +1,8 @@ (ns status-im2.config (:require [clojure.string :as string] [react-native.config :as react-native-config] - [status-im.ethereum.core :as ethereum] - [status-im.ethereum.ens :as ens])) + [status-im.ethereum.ens :as ens] + [utils.ethereum.chain :as chain])) (def get-config react-native-config/get-config) @@ -55,18 +55,18 @@ (def verify-transaction-chain-id (js/parseInt (get-config :VERIFY_TRANSACTION_CHAIN_ID "1"))) (def verify-transaction-url - (if (= :mainnet (ethereum/chain-id->chain-keyword verify-transaction-chain-id)) + (if (= :mainnet (chain/chain-id->chain-keyword verify-transaction-chain-id)) mainnet-rpc-url goerli-rpc-url)) (def verify-ens-chain-id (js/parseInt (get-config :VERIFY_ENS_CHAIN_ID "1"))) (def verify-ens-url - (if (= :mainnet (ethereum/chain-id->chain-keyword verify-ens-chain-id)) + (if (= :mainnet (chain/chain-id->chain-keyword verify-ens-chain-id)) mainnet-rpc-url goerli-rpc-url)) (def verify-ens-contract-address (get-config :VERIFY_ENS_CONTRACT_ADDRESS - ((ethereum/chain-id->chain-keyword verify-ens-chain-id) ens/ens-registries))) + ((chain/chain-id->chain-keyword verify-ens-chain-id) ens/ens-registries))) (def fast-create-community-enabled? (enabled? (get-config :FAST_CREATE_COMMUNITY_ENABLED "0"))) @@ -92,7 +92,7 @@ [{:id "mainnet_rpc" :chain-explorer-link "https://etherscan.io/address/" :name "Mainnet with upstream RPC" - :config {:NetworkId (ethereum/chain-keyword->chain-id :mainnet) + :config {:NetworkId (chain/chain-keyword->chain-id :mainnet) :DataDir "/ethereum/mainnet_rpc" :UpstreamConfig {:Enabled true :URL mainnet-rpc-url}}}]) @@ -101,14 +101,14 @@ [{:id "xdai_rpc" :name "xDai Chain" :chain-explorer-link "https://blockscout.com/xdai/mainnet/address/" - :config {:NetworkId (ethereum/chain-keyword->chain-id :xdai) + :config {:NetworkId (chain/chain-keyword->chain-id :xdai) :DataDir "/ethereum/xdai_rpc" :UpstreamConfig {:Enabled true :URL "https://gnosischain-rpc.gateway.pokt.network"}}} {:id "bsc_rpc" :chain-explorer-link "https://bscscan.com/address/" :name "BSC Network" - :config {:NetworkId (ethereum/chain-keyword->chain-id :bsc) + :config {:NetworkId (chain/chain-keyword->chain-id :bsc) :DataDir "/ethereum/bsc_rpc" :UpstreamConfig {:Enabled true :URL "https://bsc-dataseed.binance.org"}}}]) @@ -117,14 +117,14 @@ [{:id "goerli_rpc" :chain-explorer-link "https://goerli.etherscan.io/address/" :name "Goerli with upstream RPC" - :config {:NetworkId (ethereum/chain-keyword->chain-id :goerli) + :config {:NetworkId (chain/chain-keyword->chain-id :goerli) :DataDir "/ethereum/goerli_rpc" :UpstreamConfig {:Enabled true :URL goerli-rpc-url}}} {:id "bsc_testnet_rpc" :chain-explorer-link "https://testnet.bscscan.com/address/" :name "BSC testnet" - :config {:NetworkId (ethereum/chain-keyword->chain-id :bsc-testnet) + :config {:NetworkId (chain/chain-keyword->chain-id :bsc-testnet) :DataDir "/ethereum/bsc_testnet_rpc" :UpstreamConfig {:Enabled true :URL "https://data-seed-prebsc-1-s1.binance.org:8545/"}}}]) diff --git a/src/status_im2/contexts/add_new_contact/events.cljs b/src/status_im2/contexts/add_new_contact/events.cljs index 69154d096a..4b5cf5fccf 100644 --- a/src/status_im2/contexts/add_new_contact/events.cljs +++ b/src/status_im2/contexts/add_new_contact/events.cljs @@ -3,7 +3,6 @@ [utils.re-frame :as rf] [utils.transforms :as transforms] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.ens :as ens] [status-im.ethereum.stateofus :as stateofus] [native-module.core :as native-module] @@ -11,7 +10,8 @@ [utils.validators :as validators] [status-im2.contexts.contacts.events :as data-store.contacts] [status-im2.constants :as constants] - [utils.string :as utils.string])) + [utils.string :as utils.string] + [utils.ethereum.chain :as chain])) (defn init-contact "Create a new contact (persisted to app-db as [:contacts/new-identity]). @@ -115,7 +115,7 @@ (dispatcher :contacts/set-new-identity-error input)}} :resolve-ens {:db (assoc db :contacts/new-identity contact) :contacts/resolve-public-key-from-ens - {:chain-id (ethereum/chain-id db) + {:chain-id (chain/chain-id db) :ens ens :on-success (dispatcher :contacts/set-new-identity-success input) diff --git a/src/status_im2/contexts/profile/create/events.cljs b/src/status_im2/contexts/profile/create/events.cljs index 62765d2296..39837524eb 100644 --- a/src/status_im2/contexts/profile/create/events.cljs +++ b/src/status_im2/contexts/profile/create/events.cljs @@ -2,7 +2,6 @@ (:require [utils.re-frame :as rf] [native-module.core :as native-module] [status-im2.contexts.profile.config :as profile.config] - [status-im.ethereum.core :as ethereum] [utils.security.core :as security] [re-frame.core :as re-frame] [status-im2.contexts.shell.jump-to.utils :as shell.utils] @@ -26,7 +25,7 @@ {::create-profile-and-login (merge (profile.config/create) {:displayName display-name - :password (ethereum/sha3 (security/safe-unmask-data password)) + :password (native-module/sha3 (security/safe-unmask-data password)) :imagePath (profile.config/strip-file-prefix image-path) :customizationColor color})}) diff --git a/src/status_im2/contexts/profile/login/events.cljs b/src/status_im2/contexts/profile/login/events.cljs index 1383d04949..fa6f3b9ca5 100644 --- a/src/status_im2/contexts/profile/login/events.cljs +++ b/src/status_im2/contexts/profile/login/events.cljs @@ -1,7 +1,6 @@ (ns status-im2.contexts.profile.login.events (:require [utils.re-frame :as rf] - [status-im.ethereum.core :as ethereum] [utils.security.core :as security] [re-frame.core :as re-frame] [native-module.core :as native-module] @@ -40,7 +39,7 @@ [{:keys [db]}] (let [{:keys [key-uid password]} (:profile/login db)] {:db (assoc-in db [:profile/login :processing] true) - ::login [key-uid (ethereum/sha3 (security/safe-unmask-data password))]})) + ::login [key-uid (native-module/sha3 (security/safe-unmask-data password))]})) (rf/defn login-local-paired-user {:events [:profile.login/local-paired-user]} diff --git a/src/status_im2/contexts/profile/recover/events.cljs b/src/status_im2/contexts/profile/recover/events.cljs index 5eafb6cbb6..8186e6aa6f 100644 --- a/src/status_im2/contexts/profile/recover/events.cljs +++ b/src/status_im2/contexts/profile/recover/events.cljs @@ -1,6 +1,5 @@ (ns status-im2.contexts.profile.recover.events (:require [utils.security.core :as security] - [status-im.ethereum.core :as ethereum] [status-im2.contexts.profile.config :as profile.config] [utils.re-frame :as rf] [re-frame.core :as re-frame] @@ -22,6 +21,6 @@ (merge (profile.config/create) {:displayName display-name :mnemonic (security/safe-unmask-data seed-phrase) - :password (ethereum/sha3 (security/safe-unmask-data password)) + :password (native-module/sha3 (security/safe-unmask-data password)) :imagePath (profile.config/strip-file-prefix image-path) :customizationColor color})}) diff --git a/src/status_im2/subs/contact.cljs b/src/status_im2/subs/contact.cljs index 4e72398244..ed8bb14a59 100644 --- a/src/status_im2/subs/contact.cljs +++ b/src/status_im2/subs/contact.cljs @@ -3,14 +3,14 @@ [quo2.theme :as theme] [re-frame.core :as re-frame] [status-im.contact.db :as contact.db] - [status-im.ethereum.core :as ethereum] [status-im.multiaccounts.core :as multiaccounts] [status-im.ui.screens.profile.visibility-status.utils :as visibility-status-utils] [status-im.utils.gfycat.core :as gfycat] [status-im2.constants :as constants] [utils.collection] [utils.i18n :as i18n] - [utils.image-server :as image-server])) + [utils.image-server :as image-server] + [utils.address :as address])) (re-frame/reg-sub ::query-current-chat-contacts @@ -295,7 +295,7 @@ :<- [:contacts/contacts] :<- [:multiaccount/contact] (fn [[contacts multiaccount] [_ address]] - (if (ethereum/address= address (:public-key multiaccount)) + (if (address/address= address (:public-key multiaccount)) multiaccount (contact.db/find-contact-by-address contacts address)))) diff --git a/src/status_im2/subs/ens.cljs b/src/status_im2/subs/ens.cljs index 70cff94c08..f369a3892d 100644 --- a/src/status_im2/subs/ens.cljs +++ b/src/status_im2/subs/ens.cljs @@ -2,8 +2,9 @@ (:require [clojure.string :as string] [re-frame.core :as re-frame] [status-im.ens.core :as ens] - [status-im.ethereum.core :as ethereum] - [utils.money :as money])) + [utils.money :as money] + [utils.ethereum.chain :as chain] + [utils.address :as address])) (re-frame/reg-sub :ens/preferred-name @@ -37,7 +38,7 @@ :<- [:wallet] (fn [[{:keys [custom-domain? username address]} chain default-account public-key chain-id wallet]] - (let [address (or address (ethereum/normalized-hex (:address default-account))) + (let [address (or address (address/normalized-hex (:address default-account))) balance (get-in wallet [:accounts address :balance])] {:address address :username username @@ -47,9 +48,9 @@ :amount-label (ens-amount-label chain-id) :sufficient-funds? (money/sufficient-funds? (money/formatted->internal (money/bignumber 10) - (ethereum/chain-keyword->snt-symbol chain) + (chain/chain-keyword->snt-symbol chain) 18) - (get balance (ethereum/chain-keyword->snt-symbol chain)))}))) + (get balance (chain/chain-keyword->snt-symbol chain)))}))) (re-frame/reg-sub :ens/confirmation-screen diff --git a/src/status_im2/subs/general.cljs b/src/status_im2/subs/general.cljs index beaf7bbaee..2ef813e860 100644 --- a/src/status_im2/subs/general.cljs +++ b/src/status_im2/subs/general.cljs @@ -1,12 +1,12 @@ (ns status-im2.subs.general (:require [re-frame.core :as re-frame] [clojure.string :as string] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.tokens :as tokens] [status-im.multiaccounts.model :as multiaccounts.model] [status-im.utils.build :as build] [status-im.utils.mobile-sync :as mobile-network-utils] - [status-im2.constants :as constants])) + [status-im2.constants :as constants] + [utils.ethereum.chain :as chain])) (re-frame/reg-sub :visibility-status-updates/visibility-status-update @@ -50,26 +50,26 @@ (re-frame/reg-sub :custom-rpc-node :<- [:current-network] - (fn [network] - (ethereum/custom-rpc-node? network))) + (fn [{:keys [id]}] + (= 45 (count id)))) (re-frame/reg-sub :chain-keyword :<- [:current-network] (fn [network] - (ethereum/network->chain-keyword network))) + (chain/network->chain-keyword network))) (re-frame/reg-sub :chain-name :<- [:current-network] (fn [network] - (ethereum/network->chain-name network))) + (chain/network->chain-name network))) (re-frame/reg-sub :chain-id :<- [:current-network] (fn [network] - (ethereum/network->chain-id network))) + (chain/network->chain-id network))) (re-frame/reg-sub :mainnet? @@ -220,7 +220,7 @@ :ethereum/chain-keyword :<- [:current-network] (fn [network] - (ethereum/network->chain-keyword network))) + (chain/network->chain-keyword network))) (re-frame/reg-sub :ethereum/native-currency diff --git a/src/status_im2/subs/keycard.cljs b/src/status_im2/subs/keycard.cljs index 4b223b65f8..1fab38fd11 100644 --- a/src/status_im2/subs/keycard.cljs +++ b/src/status_im2/subs/keycard.cljs @@ -1,9 +1,9 @@ (ns status-im2.subs.keycard (:require [clojure.string :as string] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [status-im.keycard.common :as common] - [utils.datetime :as datetime])) + [utils.datetime :as datetime] + [utils.address :as address])) (re-frame/reg-sub :keycard/nfc-enabled? @@ -116,7 +116,7 @@ (re-frame/reg-sub :keycard-multiaccount-whisper-public-key (fn [db] - (ethereum/normalized-hex (get-in db [:keycard :multiaccount-whisper-public-key])))) + (address/normalized-hex (get-in db [:keycard :multiaccount-whisper-public-key])))) (re-frame/reg-sub :keycard-paired-on diff --git a/src/status_im2/subs/networks.cljs b/src/status_im2/subs/networks.cljs index 9578d009bf..2b5894239a 100644 --- a/src/status_im2/subs/networks.cljs +++ b/src/status_im2/subs/networks.cljs @@ -1,13 +1,13 @@ (ns status-im2.subs.networks (:require [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] - [status-im2.config :as config])) + [status-im2.config :as config] + [utils.ethereum.chain :as chain])) (defn- filter-networks [network-type] (fn [network] - (let [chain-id (ethereum/network->chain-id network) - testnet? (ethereum/testnet? chain-id) + (let [chain-id (chain/network->chain-id network) + testnet? (chain/testnet? chain-id) custom? (:custom? network)] (case network-type :custom custom? diff --git a/src/status_im2/subs/profile.cljs b/src/status_im2/subs/profile.cljs index 6c81783e8b..2f08cc0b0c 100644 --- a/src/status_im2/subs/profile.cljs +++ b/src/status_im2/subs/profile.cljs @@ -3,12 +3,13 @@ [clojure.string :as string] [quo2.theme :as theme] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [status-im.fleet.core :as fleet] [status-im.multiaccounts.db :as multiaccounts.db] [status-im2.constants :as constants] [utils.image-server :as image-server] - [utils.security.core :as security])) + [utils.security.core :as security] + [status-im.wallet.utils :as wallet.utils] + [utils.address :as address])) (re-frame/reg-sub :profile/customization-color @@ -77,7 +78,7 @@ :multiaccount/default-account :<- [:profile/wallet-accounts] (fn [accounts] - (ethereum/get-default-account accounts))) + (wallet.utils/get-default-account accounts))) (re-frame/reg-sub :multiaccount/visible-accounts @@ -208,7 +209,7 @@ :generate false :watch - (or (not (ethereum/address? address)) + (or (not (address/address? address)) (some #(when (= (:address %) address) %) accounts)) :key (string/blank? (security/safe-unmask-data private-key)) diff --git a/src/status_im2/subs/wallet/signing.cljs b/src/status_im2/subs/wallet/signing.cljs index 1555d40855..ee0c1ded34 100644 --- a/src/status_im2/subs/wallet/signing.cljs +++ b/src/status_im2/subs/wallet/signing.cljs @@ -1,12 +1,12 @@ (ns status-im2.subs.wallet.signing (:require [clojure.string :as string] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.tokens :as tokens] [utils.i18n :as i18n] [status-im.signing.gas :as signing.gas] [utils.money :as money] - [status-im.wallet.db :as wallet.db])) + [status-im.wallet.db :as wallet.db] + [utils.ethereum.chain :as chain])) (re-frame/reg-sub ::send-transaction @@ -41,7 +41,7 @@ :wallet/binance-chain? :<- [:current-network] (fn [network] - (ethereum/binance-chain-id? (get-in network [:config :NetworkId])))) + (chain/binance-chain-id? (get-in network [:config :NetworkId])))) (re-frame/reg-sub :signing/fee diff --git a/src/status_im2/subs/wallet/wallet.cljs b/src/status_im2/subs/wallet/wallet.cljs index f4523699d2..2ff5383b8a 100644 --- a/src/status_im2/subs/wallet/wallet.cljs +++ b/src/status_im2/subs/wallet/wallet.cljs @@ -1,12 +1,12 @@ (ns status-im2.subs.wallet.wallet (:require [clojure.string :as string] [re-frame.core :as re-frame] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.tokens :as tokens] [status-im.utils.currency :as currency] [utils.money :as money] [status-im2.config :as config] - [utils.i18n :as i18n])) + [utils.i18n :as i18n] + [status-im.wallet.utils :as wallet.utils])) (re-frame/reg-sub :balance @@ -19,7 +19,7 @@ :<- [:wallet] :<- [:profile/wallet-accounts] (fn [[wallet accounts]] - (get-in wallet [:accounts (:address (ethereum/get-default-account accounts)) :balance]))) + (get-in wallet [:accounts (:address (wallet.utils/get-default-account accounts)) :balance]))) (re-frame/reg-sub :balances diff --git a/src/utils/address.cljs b/src/utils/address.cljs index 76e4cfef17..85b45a4abd 100644 --- a/src/utils/address.cljs +++ b/src/utils/address.cljs @@ -1,7 +1,32 @@ (ns utils.address - ;; TODO move to status-im2 - (:require [status-im.ethereum.core :as ethereum] - [status-im.ethereum.eip55 :as eip55])) + (:require [utils.ethereum.eip.eip55 :as eip55] + [clojure.string :as string] + [native-module.core :as native-module])) + +(def hex-prefix "0x") + +(defn normalized-hex + [hex] + (when hex + (if (string/starts-with? hex hex-prefix) + hex + (str hex-prefix hex)))) + +(defn naked-address + [s] + (when s + (string/replace s hex-prefix ""))) + +(defn address? + [address] + (native-module/address? address)) + +(defn address= + [address1 address2] + (and address1 + address2 + (= (string/lower-case (normalized-hex address1)) + (string/lower-case (normalized-hex address2))))) (defn get-shortened-key "Takes first and last 4 digits from address including leading 0x @@ -13,7 +38,7 @@ (defn get-shortened-checksum-address [address] (when address - (get-shortened-key (eip55/address->checksum (ethereum/normalized-hex address))))) + (get-shortened-key (eip55/address->checksum (normalized-hex address))))) (defn get-abbreviated-profile-url "The goal here is to generate a string that begins with diff --git a/src/utils/ethereum/chain.cljs b/src/utils/ethereum/chain.cljs new file mode 100644 index 0000000000..3bce18f027 --- /dev/null +++ b/src/utils/ethereum/chain.cljs @@ -0,0 +1,84 @@ +(ns utils.ethereum.chain) + +(def BSC-mainnet-chain-id 56) +(def BSC-testnet-chain-id 97) + +;; IDs standardized in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md#list-of-chain-ids +(def chains + {:mainnet {:id 1 :name "Mainnet"} + :xdai {:id 100 :name "xDai"} + :goerli {:id 5 :name "Goerli"} + :bsc {:id BSC-mainnet-chain-id + :name "BSC"} + :bsc-testnet {:id BSC-testnet-chain-id + :name "BSC tetnet"}}) + +(defn chain-id->chain-keyword + [i] + (or (some #(when (= i (:id (val %))) (key %)) chains) + :custom)) + +(defn chain-id->chain-name + [i] + (or (some #(when (= i (:id (val %))) (:name (val %))) chains) + :custom)) + +(defn chain-keyword->chain-id + [k] + (get-in chains [k :id])) + +(defn chain-keyword->snt-symbol + [k] + (case k + :mainnet :SNT + :STT)) + +(defn testnet? + [id] + (contains? #{(chain-keyword->chain-id :goerli) + (chain-keyword->chain-id :bsc-testnet)} + id)) + +(defn network->chain-id + [network] + (get-in network [:config :NetworkId])) + +(defn network->chain-keyword + [network] + (chain-id->chain-keyword (network->chain-id network))) + +(defn binance-chain-id? + [chain-id] + (or (= BSC-mainnet-chain-id chain-id) + (= BSC-testnet-chain-id chain-id))) + +(defn current-network + [db] + (let [networks (get db :networks/networks) + network-id (get db :networks/current-network)] + (get networks network-id))) + +(defn binance-chain? + [db] + (-> db + current-network + network->chain-id + binance-chain-id?)) + +(defn network->chain-name + [network] + (-> network + network->chain-keyword + name)) + +(defn get-current-network + [m] + (get (:networks/networks m) (:networks/current-network m))) + +(defn chain-keyword + [db] + (network->chain-keyword (get-current-network db))) + +(defn chain-id + [db] + (network->chain-id (get-current-network db))) diff --git a/src/utils/ethereum/chain_test.cljs b/src/utils/ethereum/chain_test.cljs new file mode 100644 index 0000000000..e29b53f636 --- /dev/null +++ b/src/utils/ethereum/chain_test.cljs @@ -0,0 +1,8 @@ +(ns utils.ethereum.chain-test + (:require [cljs.test :refer-macros [deftest is]] + [utils.ethereum.chain :as chain])) + +(deftest chain-id->chain-keyword + (is (= (chain/chain-id->chain-keyword 1) :mainnet)) + (is (= (chain/chain-id->chain-keyword 5) :goerli)) + (is (= (chain/chain-id->chain-keyword 5777) :custom))) diff --git a/src/status_im/ethereum/eip55.cljs b/src/utils/ethereum/eip/eip55.cljs similarity index 96% rename from src/status_im/ethereum/eip55.cljs rename to src/utils/ethereum/eip/eip55.cljs index 4d15a6ac86..8e0cb28ab9 100644 --- a/src/status_im/ethereum/eip55.cljs +++ b/src/utils/ethereum/eip/eip55.cljs @@ -1,4 +1,4 @@ -(ns status-im.ethereum.eip55 +(ns utils.ethereum.eip.eip55 "Utility function related to [EIP55](https://eips.ethereum.org/EIPS/eip-55) This EIP standardize how ethereum addresses should be printed as strings to validate checksum. diff --git a/src/status_im/ethereum/eip55_test.cljs b/src/utils/ethereum/eip/eip55_test.cljs similarity index 92% rename from src/status_im/ethereum/eip55_test.cljs rename to src/utils/ethereum/eip/eip55_test.cljs index b9e6bd3a1f..67167378ad 100644 --- a/src/status_im/ethereum/eip55_test.cljs +++ b/src/utils/ethereum/eip/eip55_test.cljs @@ -1,6 +1,6 @@ -(ns status-im.ethereum.eip55-test +(ns utils.ethereum.eip.eip55-test (:require [cljs.test :refer-macros [deftest is]] - [status-im.ethereum.eip55 :as eip55])) + [utils.ethereum.eip.eip55 :as eip55])) (deftest valid-address-checksum? (is (= true (eip55/valid-address-checksum? "0x52908400098527886E0F7030069857D2E4169EE7"))) diff --git a/src/utils/requests.cljs b/src/utils/requests.cljs deleted file mode 100644 index 0eb28f5f8f..0000000000 --- a/src/utils/requests.cljs +++ /dev/null @@ -1,9 +0,0 @@ -(ns utils.requests - (:require [utils.datetime :as datetime])) - -(def request-cooldown-ms (* 24 60 60 1000)) - -(defn can-request-access-again? - [requested-at] - (> (datetime/timestamp) (+ (* requested-at 1000) request-cooldown-ms))) -