From b4a49bfe556df25e30d6ad6236fce2b38df5b9ea Mon Sep 17 00:00:00 2001 From: Lungu Cristian Date: Fri, 2 Aug 2024 14:55:46 +0300 Subject: [PATCH] Fix typed-data chainid check (#20943) * fix: typed-data chainid check & toast on sign fail * fix: parsing the chain-id and error-handling --- .../wallet/wallet_connect/events.cljs | 4 +- .../wallet_connect/processing_events.cljs | 68 +++++++++---------- .../wallet_connect/responding_events.cljs | 6 +- .../wallet/wallet_connect/signing.cljs | 4 +- 4 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/status_im/contexts/wallet/wallet_connect/events.cljs b/src/status_im/contexts/wallet/wallet_connect/events.cljs index 7eba50495a..2d97f63ce6 100644 --- a/src/status_im/contexts/wallet/wallet_connect/events.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/events.cljs @@ -265,9 +265,7 @@ persisted-sessions)] (when (seq expired-sessions) (log/info "Updating WalletConnect persisted sessions due to expired/inactive sessions" - {:expired expired-sessions - :persisted persisted-sessions - :active sessions})) + {:expired expired-sessions})) {:fx (mapv (fn [{:keys [topic]}] [:dispatch [:wallet-connect/disconnect-session topic]]) expired-sessions) diff --git a/src/status_im/contexts/wallet/wallet_connect/processing_events.cljs b/src/status_im/contexts/wallet/wallet_connect/processing_events.cljs index fa85668012..224720c98a 100644 --- a/src/status_im/contexts/wallet/wallet_connect/processing_events.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/processing_events.cljs @@ -126,38 +126,37 @@ (rf/reg-event-fx :wallet-connect/process-sign-typed (fn [{:keys [db]}] - (let [[address raw-data] (wallet-connect-core/get-db-current-request-params db) - parsed-raw-data (transforms/js-parse raw-data) - session-chain-id (-> (wallet-connect-core/get-db-current-request-event db) - (get-in [:params :chainId]) - wallet-connect-core/eip155->chain-id) - data-chain-id (-> parsed-raw-data - transforms/js->clj - signing/typed-data-chain-id) - parsed-data (try (-> parsed-raw-data - (transforms/js-dissoc :types :primaryType) - (transforms/js-stringify 2)) - (catch js/Error _ nil))] - (cond - (nil? parsed-data) + (try + (let [[address raw-data] (wallet-connect-core/get-db-current-request-params db) + parsed-raw-data (transforms/js-parse raw-data) + session-chain-id (-> (wallet-connect-core/get-db-current-request-event db) + (get-in [:params :chainId]) + wallet-connect-core/eip155->chain-id) + data-chain-id (-> parsed-raw-data + transforms/js->clj + signing/typed-data-chain-id) + parsed-data (-> parsed-raw-data + (transforms/js-dissoc :types :primaryType) + (transforms/js-stringify 2))] + (if (and data-chain-id + (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 [: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 - [: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]]]})))) + (ex-info "Failed to parse JSON typed data" + {:error err + :data (wallet-connect-core/get-db-current-request-params db)})]]]})))) (rf/reg-event-fx :wallet-connect/wrong-typed-data-chain-id @@ -169,13 +168,14 @@ wallet-connect-core/chain-id->network-details :full-name) 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})] {:fx [[:dispatch [:toasts/upsert - {:type :negative - :theme :dark - :text toast-message}]] + {:type :negative + :text toast-message}]] [:dispatch [:wallet-connect/on-processing-error (ex-info "Can't proceed signing typed data due to wrong chain-id included in the data" diff --git a/src/status_im/contexts/wallet/wallet_connect/responding_events.cljs b/src/status_im/contexts/wallet/wallet_connect/responding_events.cljs index a74dcaafd9..85dd33c77b 100644 --- a/src/status_im/contexts/wallet/wallet_connect/responding_events.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/responding_events.cljs @@ -106,7 +106,11 @@ :method method :wallet-connect-event event :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 :wallet-connect/send-response diff --git a/src/status_im/contexts/wallet/wallet_connect/signing.cljs b/src/status_im/contexts/wallet/wallet_connect/signing.cljs index 575661a902..83f9dd3cdf 100644 --- a/src/status_im/contexts/wallet/wallet_connect/signing.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/signing.cljs @@ -4,6 +4,7 @@ [status-im.contexts.wallet.wallet-connect.core :as core] [status-im.contexts.wallet.wallet-connect.rpc :as rpc] [utils.hex :as hex] + [utils.number :as number] [utils.transforms :as transforms])) (defn typed-data-chain-id @@ -16,7 +17,8 @@ (some #(= "chainId" (:name %)))) data-chain-id (-> typed-data :domain - :chainId)] + :chainId + number/parse-int)] (when chain-id-type? data-chain-id)))