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,38 +126,37 @@
(rf/reg-event-fx (rf/reg-event-fx
:wallet-connect/process-sign-typed :wallet-connect/process-sign-typed
(fn [{:keys [db]}] (fn [{:keys [db]}]
(let [[address raw-data] (wallet-connect-core/get-db-current-request-params db) (try
parsed-raw-data (transforms/js-parse raw-data) (let [[address raw-data] (wallet-connect-core/get-db-current-request-params db)
session-chain-id (-> (wallet-connect-core/get-db-current-request-event db) parsed-raw-data (transforms/js-parse raw-data)
(get-in [:params :chainId]) session-chain-id (-> (wallet-connect-core/get-db-current-request-event db)
wallet-connect-core/eip155->chain-id) (get-in [:params :chainId])
data-chain-id (-> parsed-raw-data wallet-connect-core/eip155->chain-id)
transforms/js->clj data-chain-id (-> parsed-raw-data
signing/typed-data-chain-id) transforms/js->clj
parsed-data (try (-> parsed-raw-data signing/typed-data-chain-id)
(transforms/js-dissoc :types :primaryType) parsed-data (-> parsed-raw-data
(transforms/js-stringify 2)) (transforms/js-dissoc :types :primaryType)
(catch js/Error _ nil))] (transforms/js-stringify 2))]
(cond (if (and data-chain-id
(nil? parsed-data) (not= session-chain-id data-chain-id))
{:fx [[:dispatch
[:wallet-connect/wrong-typed-data-chain-id
{:expected-chain-id session-chain-id
:wrong-chain-id data-chain-id}]]]}
{:db (update-in db
[:wallet-connect/current-request]
assoc
:address (string/lower-case address)
:display-data (or parsed-data raw-data)
:raw-data raw-data)
:fx [[:dispatch [:wallet-connect/show-request-modal]]]}))
(catch js/Error err
{:fx [[:dispatch {:fx [[:dispatch
[:wallet-connect/on-processing-error [:wallet-connect/on-processing-error
(ex-info "Failed to parse JSON typed data" {:data raw-data})]]]} (ex-info "Failed to parse JSON typed data"
{:error err
(not= session-chain-id data-chain-id) :data (wallet-connect-core/get-db-current-request-params db)})]]]}))))
{:fx [[:dispatch
[:wallet-connect/wrong-typed-data-chain-id
{:expected-chain-id session-chain-id
:wrong-chain-id data-chain-id}]]]}
:else
{:db (update-in db
[:wallet-connect/current-request]
assoc
:address (string/lower-case address)
:display-data (or parsed-data raw-data)
:raw-data raw-data)
:fx [[:dispatch [:wallet-connect/show-request-modal]]]}))))
(rf/reg-event-fx (rf/reg-event-fx
:wallet-connect/wrong-typed-data-chain-id :wallet-connect/wrong-typed-data-chain-id
@ -169,13 +168,14 @@
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
(ex-info "Can't proceed signing typed data due to wrong chain-id included in the data" (ex-info "Can't proceed signing typed data due to wrong chain-id included in the data"

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)))