Fix typed-data chainid check (#20943)

* fix: typed-data chainid check & toast on sign fail

* fix: parsing the chain-id and error-handling
This commit is contained in:
Lungu Cristian 2024-08-02 14:55:46 +03:00 committed by Icaro Motta
parent e5c3357317
commit b4a49bfe55
4 changed files with 43 additions and 39 deletions

View File

@ -265,9 +265,7 @@
persisted-sessions)] persisted-sessions)]
(when (seq expired-sessions) (when (seq expired-sessions)
(log/info "Updating WalletConnect persisted sessions due to expired/inactive sessions" (log/info "Updating WalletConnect persisted sessions due to expired/inactive sessions"
{:expired expired-sessions {:expired expired-sessions}))
:persisted persisted-sessions
:active sessions}))
{:fx (mapv (fn [{:keys [topic]}] {:fx (mapv (fn [{:keys [topic]}]
[:dispatch [:wallet-connect/disconnect-session topic]]) [:dispatch [:wallet-connect/disconnect-session topic]])
expired-sessions) expired-sessions)

View File

@ -126,6 +126,7 @@
(rf/reg-event-fx (rf/reg-event-fx
:wallet-connect/process-sign-typed :wallet-connect/process-sign-typed
(fn [{:keys [db]}] (fn [{:keys [db]}]
(try
(let [[address raw-data] (wallet-connect-core/get-db-current-request-params db) (let [[address raw-data] (wallet-connect-core/get-db-current-request-params db)
parsed-raw-data (transforms/js-parse raw-data) parsed-raw-data (transforms/js-parse raw-data)
session-chain-id (-> (wallet-connect-core/get-db-current-request-event db) session-chain-id (-> (wallet-connect-core/get-db-current-request-event db)
@ -134,30 +135,28 @@
data-chain-id (-> parsed-raw-data data-chain-id (-> parsed-raw-data
transforms/js->clj transforms/js->clj
signing/typed-data-chain-id) signing/typed-data-chain-id)
parsed-data (try (-> parsed-raw-data parsed-data (-> parsed-raw-data
(transforms/js-dissoc :types :primaryType) (transforms/js-dissoc :types :primaryType)
(transforms/js-stringify 2)) (transforms/js-stringify 2))]
(catch js/Error _ nil))] (if (and data-chain-id
(cond (not= session-chain-id data-chain-id))
(nil? parsed-data)
{:fx [[:dispatch
[:wallet-connect/on-processing-error
(ex-info "Failed to parse JSON typed data" {:data raw-data})]]]}
(not= session-chain-id data-chain-id)
{:fx [[:dispatch {:fx [[:dispatch
[:wallet-connect/wrong-typed-data-chain-id [:wallet-connect/wrong-typed-data-chain-id
{:expected-chain-id session-chain-id {:expected-chain-id session-chain-id
:wrong-chain-id data-chain-id}]]]} :wrong-chain-id data-chain-id}]]]}
:else
{:db (update-in db {:db (update-in db
[:wallet-connect/current-request] [:wallet-connect/current-request]
assoc assoc
:address (string/lower-case address) :address (string/lower-case address)
:display-data (or parsed-data raw-data) :display-data (or parsed-data raw-data)
:raw-data raw-data) :raw-data raw-data)
:fx [[:dispatch [:wallet-connect/show-request-modal]]]})))) :fx [[:dispatch [:wallet-connect/show-request-modal]]]}))
(catch js/Error err
{:fx [[:dispatch
[:wallet-connect/on-processing-error
(ex-info "Failed to parse JSON typed data"
{:error err
:data (wallet-connect-core/get-db-current-request-params db)})]]]}))))
(rf/reg-event-fx (rf/reg-event-fx
:wallet-connect/wrong-typed-data-chain-id :wallet-connect/wrong-typed-data-chain-id
@ -169,12 +168,13 @@
wallet-connect-core/chain-id->network-details wallet-connect-core/chain-id->network-details
:full-name) :full-name)
toast-message (i18n/label :t/wallet-connect-typed-data-wrong-chain-id-warning toast-message (i18n/label :t/wallet-connect-typed-data-wrong-chain-id-warning
{:wrong-chain wrong-network-name {:wrong-chain (or wrong-network-name
(wallet-connect-core/chain-id->eip155
wrong-chain-id))
:expected-chain expected-network-name})] :expected-chain expected-network-name})]
{:fx [[:dispatch {:fx [[:dispatch
[:toasts/upsert [:toasts/upsert
{:type :negative {:type :negative
:theme :dark
:text toast-message}]] :text toast-message}]]
[:dispatch [:dispatch
[:wallet-connect/on-processing-error [:wallet-connect/on-processing-error

View File

@ -106,7 +106,11 @@
:method method :method method
:wallet-connect-event event :wallet-connect-event event
:event :wallet-connect/on-sign-error}) :event :wallet-connect/on-sign-error})
{:fx [[:dispatch [:wallet-connect/dismiss-request-modal]]]}))) {:fx [[:dispatch [:wallet-connect/dismiss-request-modal]]
[:dispatch
[:toasts/upsert
{:type :negative
:text (i18n/label :t/something-went-wrong)}]]]})))
(rf/reg-event-fx (rf/reg-event-fx
:wallet-connect/send-response :wallet-connect/send-response

View File

@ -4,6 +4,7 @@
[status-im.contexts.wallet.wallet-connect.core :as core] [status-im.contexts.wallet.wallet-connect.core :as core]
[status-im.contexts.wallet.wallet-connect.rpc :as rpc] [status-im.contexts.wallet.wallet-connect.rpc :as rpc]
[utils.hex :as hex] [utils.hex :as hex]
[utils.number :as number]
[utils.transforms :as transforms])) [utils.transforms :as transforms]))
(defn typed-data-chain-id (defn typed-data-chain-id
@ -16,7 +17,8 @@
(some #(= "chainId" (:name %)))) (some #(= "chainId" (:name %))))
data-chain-id (-> typed-data data-chain-id (-> typed-data
:domain :domain
:chainId)] :chainId
number/parse-int)]
(when chain-id-type? (when chain-id-type?
data-chain-id))) data-chain-id)))