Skip to content

Commit

Permalink
Fix typed-data chainid check (#20943)
Browse files Browse the repository at this point in the history
* fix: typed-data chainid check & toast on sign fail

* fix: parsing the chain-id and error-handling
  • Loading branch information
clauxx authored and ilmotta committed Aug 2, 2024
1 parent 8d07444 commit 461b229
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
4 changes: 1 addition & 3 deletions src/status_im/contexts/wallet/wallet_connect/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
68 changes: 34 additions & 34 deletions src/status_im/contexts/wallet/wallet_connect/processing_events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/status_im/contexts/wallet/wallet_connect/signing.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))

Expand Down

0 comments on commit 461b229

Please sign in to comment.