Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Wallet Connect 1.0 #13032

Merged
merged 1 commit into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@react-native-community/push-notification-ios": "^1.4.1",
"@react-native-community/slider": "^3.0.0",
"@walletconnect/client": "^2.0.0-beta.23",
"@walletconnect/client-legacy": "npm:@walletconnect/client@^1.7.1",
"bignumber.js": "git+https://github.com/status-im/bignumber.js.git#v4.0.2-status",
"buffer": "^5.4.2",
"chance": "^1.1.0",
Expand Down
3 changes: 3 additions & 0 deletions src/status_im/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,6 @@
(def ^:const visibility-status-dnd 2)
(def ^:const visibility-status-always-online 3)
(def ^:const visibility-status-inactive 4)

(def ^:const wallet-connect-version-1 1)
(def ^:const wallet-connect-version-2 2)
3 changes: 2 additions & 1 deletion src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
[status-im.navigation.core :as navigation.core]
[status-im.multiaccounts.login.core :as login.core]
[status-im.signing.core :as signing]
status-im.wallet-connect.core))
status-im.wallet-connect.core
status-im.wallet-connect-legacy.core))

(re-frame/reg-fx
:dismiss-keyboard
Expand Down
12 changes: 9 additions & 3 deletions src/status_im/qr_scanner/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[status-im.ethereum.core :as ethereum]
[status-im.add-new.db :as new-chat.db]
[status-im.utils.fx :as fx]
[status-im.group-chats.core :as group-chats]))
[status-im.group-chats.core :as group-chats]
[clojure.string :as string]))

(fx/defn scan-qr-code
{:events [::scan-code]}
Expand Down Expand Up @@ -78,8 +79,13 @@
(navigation/change-tab :wallet)
(navigation/pop-to-root-tab :wallet-stack)))

(fx/defn handle-wallet-connect [cofx data]
{:dispatch [:wallet-connect/pair data]})
(fx/defn handle-wallet-connect
{:events [::handle-wallet-connect-uri]}
[cofx data]
(let [wc-version (last (string/split (first (string/split data "?")) "@"))]
(if (= wc-version "1")
{:dispatch [:wallet-connect-legacy/pair data]}
{:dispatch [:wallet-connect/pair data]})))

(fx/defn match-scan
{:events [::match-scanned-value]}
Expand Down
1 change: 1 addition & 0 deletions src/status_im/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@
(reg-root-key-sub :wallet-connect/session-connected :wallet-connect/session-connected)
(reg-root-key-sub :wallet-connect/showing-app-management-sheet? :wallet-connect/showing-app-management-sheet?)
(reg-root-key-sub :wallet-connect/sessions :wallet-connect/sessions)
(reg-root-key-sub :wallet-connect-legacy/sessions :wallet-connect-legacy/sessions)
(reg-root-key-sub :wallet-connect/session-managed :wallet-connect/session-managed)

(re-frame/reg-sub
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/ui/components/bottom_panel/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@

(views/defview animated-bottom-panel [val view on-close on-touch-outside show-overlay?]
(views/letsubs [{window-height :height} [:dimensions/window]]
[bottom-panel (when val (select-keys val [:from :contact :amount :token :approve? :message :cancel? :hash :name :url :icons :description :topic :relay :self :peer :permissions :state])) view window-height on-close on-touch-outside (if-not (nil? show-overlay?) show-overlay? true)]))
[bottom-panel (when val (select-keys val [:from :contact :amount :token :approve? :message :cancel? :hash :name :url :icons :wc-version :params :connector :description :topic :relay :self :peer :permissions :state])) view window-height on-close on-touch-outside (if-not (nil? show-overlay?) show-overlay? true)]))
2 changes: 1 addition & 1 deletion src/status_im/ui/screens/advanced_settings/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
:accessory :switch
:active waku-bloom-filter-mode}
{:size :small
:title (i18n/label :t/wallet-connect-2.0)
:title (i18n/label :t/wallet-connect)
:accessibility-label :wallet-connect-settings-switch
:container-margin-bottom 8
:on-press
Expand Down
58 changes: 38 additions & 20 deletions src/status_im/ui/screens/wallet_connect/session_proposal/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[status-im.ui.components.list.views :as list]
[reagent.core :as reagent]
[clojure.string :as string]
[quo.platform :as platform]))
[quo.platform :as platform]
[status-im.constants :as constants]))

(def chevron-icon-container-width 24)

Expand Down Expand Up @@ -65,17 +66,23 @@
:background-color (:color @selected-account-atom)
:on-press on-press}]]))

(defview success-sheet-view [{:keys [topic]}]
(defview success-sheet-view [{:keys [wc-version] :as session-data}]
(letsubs [visible-accounts [:visible-accounts-without-watch-only]
dapps-account [:dapps-account]
sessions [:wallet-connect/sessions]
sessions-legacy [:wallet-connect-legacy/sessions]
managed-session [:wallet-connect/session-managed]]
(let [{:keys [peer state] :as session} (first (filter #(= (:topic %) topic) sessions))
{:keys [accounts]} state
(let [{:keys [topic]} (when-not (= wc-version constants/wallet-connect-version-1) session-data)
peerId (when (= wc-version constants/wallet-connect-version-1) (get-in session-data [:params 0 :peerId]))
session-legacy (when (= wc-version constants/wallet-connect-version-1) (first (filter #(= (get-in % [:params 0 :peerId]) peerId) sessions-legacy)))
{:keys [peer state] :as session} (first (filter #(= (:topic %) topic) sessions))
{:keys [params]} (when (= wc-version constants/wallet-connect-version-1) session-legacy)
{:keys [metadata]} peer
{:keys [name icons]} metadata
icon-uri (when (and icons (> (count icons) 0)) (first icons))
address (last (string/split (first accounts) #":"))
{:keys [peerMeta]} (first params)
{:keys [accounts]} (if (= wc-version constants/wallet-connect-version-1) (first params) state)
{:keys [name icons]} (if (= wc-version constants/wallet-connect-version-1) peerMeta metadata)
icon-uri (when (and icons (pos? (count icons))) (first icons))
address (if (= wc-version constants/wallet-connect-version-1) (first accounts) (last (string/split (first accounts) #":")))
account (first (filter #(= (:address %) address) visible-accounts))
selected-account-atom (reagent/atom account)]
[react/view (styles/proposal-sheet-container)
Expand All @@ -97,7 +104,7 @@
(vector dapps-account)
selected-account-atom
{:on-press #(do
(re-frame/dispatch [:wallet-connect/manage-app session])
(re-frame/dispatch (if (= wc-version constants/wallet-connect-version-1) [:wallet-connect-legacy/manage-app session-data] [:wallet-connect/manage-app session]))
(reset! show-account-selector? true))}]
[quo/text {:weight :regular
:color :secondary
Expand All @@ -117,16 +124,23 @@
:blurType (if (colors/dark?) :dark :light)}]
[react/view (styles/blur-view)]))])))

(defview app-management-sheet-view [{:keys [topic]}]
(defview app-management-sheet-view [{:keys [wc-version] :as session}]
(letsubs [sessions [:wallet-connect/sessions]
sessions-legacy [:wallet-connect-legacy/sessions]
visible-accounts [:visible-accounts-without-watch-only]]
(let [{:keys [peer state]} (first (filter #(= (:topic %) topic) sessions))
{:keys [accounts]} state
(let [peerId (when (= wc-version constants/wallet-connect-version-1) (get-in session [:params 0 :peerId]))
session-legacy (when (= wc-version constants/wallet-connect-version-1) (first (filter #(= (get-in % [:params 0 :peerId]) peerId) sessions-legacy)))
{:keys [topic]} (when-not (= wc-version constants/wallet-connect-version-1) session)
{:keys [peer state]} (first (filter #(= (:topic %) topic) sessions))
{:keys [params]} (when (= wc-version constants/wallet-connect-version-1) session-legacy)
{:keys [metadata]} peer
{:keys [name icons url]} metadata
icon-uri (when (and icons (> (count icons) 0)) (first icons))
account-address (last (string/split (first accounts) #":"))
selected-account-atom (reagent/atom (first (filter #(= (:address %) account-address) visible-accounts)))]
{:keys [peerMeta]} (first params)
{:keys [accounts]} (if (= wc-version constants/wallet-connect-version-1) (first params) state)
{:keys [name icons url]} (if (= wc-version constants/wallet-connect-version-1) peerMeta metadata)
icon-uri (when (and icons (pos? (count icons))) (first icons))
address (if (= wc-version constants/wallet-connect-version-1) (first accounts) (last (string/split (first accounts) #":")))
account (first (filter #(= (:address %) address) visible-accounts))
selected-account-atom (reagent/atom account)]
[react/view {:style (merge (styles/acc-sheet) {:background-color "rgba(0,0,0,0)"})}
[react/linear-gradient {:colors ["rgba(0,0,0,0)" "rgba(0,0,0,0.3)"]
:start {:x 0 :y 0} :end {:x 0 :y 1}
Expand All @@ -143,14 +157,14 @@
[quo/button
{:type :secondary
:theme :secondary
:on-press #(re-frame/dispatch [:wallet-connect/disconnect topic])}
:on-press #(re-frame/dispatch (if (= wc-version constants/wallet-connect-version-1) [:wallet-connect-legacy/disconnect session-legacy] [:wallet-connect/disconnect topic]))}
(i18n/label :t/disconnect)]]
[account-selector
visible-accounts
selected-account-atom
#(re-frame/dispatch [:wallet-connect/change-session-account topic @selected-account-atom])]]])))
#(re-frame/dispatch (if (= wc-version constants/wallet-connect-version-1) [:wallet-connect-legacy/change-session-account session-legacy @selected-account-atom] [:wallet-connect/change-session-account topic @selected-account-atom]))]]])))

(defview session-proposal-sheet [{:keys [name icons]}]
(defview session-proposal-sheet [{:keys [name icons wc-version]}]
(letsubs [visible-accounts [:visible-accounts-without-watch-only]
dapps-account [:dapps-account]]
(let [icon-uri (when (and icons (> (count icons) 0)) (first icons))
Expand All @@ -174,11 +188,15 @@
[react/view (merge (styles/proposal-buttons-container) (when (= (count visible-accounts) 1) {:margin-top 12}))
[quo/button
{:type :secondary
:on-press #(re-frame/dispatch [:wallet-connect/reject-proposal])}
:on-press #(if (= wc-version constants/wallet-connect-version-1)
(re-frame/dispatch [:wallet-connect-legacy/reject-proposal])
(re-frame/dispatch [:wallet-connect/reject-proposal]))}
(i18n/label :t/reject)]
[quo/button
{:theme :accent
:on-press #(re-frame/dispatch [:wallet-connect/approve-proposal @selected-account-atom])}
:on-press #(if (= wc-version constants/wallet-connect-version-1)
(re-frame/dispatch [:wallet-connect-legacy/approve-proposal @selected-account-atom])
(re-frame/dispatch [:wallet-connect/approve-proposal @selected-account-atom]))}
(i18n/label :t/connect)]]])))

(defview wallet-connect-proposal-sheet []
Expand Down
8 changes: 8 additions & 0 deletions src/status_im/utils/wallet_connect_legacy.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns status-im.utils.wallet-connect-legacy
(:require ["@walletconnect/client-legacy" :default WalletConnect]
[status-im.utils.config :as config]))

(defn create-connector [uri]
(WalletConnect.
(clj->js {:uri uri
:clientMeta config/default-wallet-connect-metadata})))
7 changes: 5 additions & 2 deletions src/status_im/wallet/choose_recipient/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[status-im.bottom-sheet.core :as bottom-sheet]
[status-im.navigation :as navigation]
[status-im.utils.http :as http]
[status-im.utils.universal-links.utils :as links]))
[status-im.utils.universal-links.utils :as links]
[status-im.utils.wallet-connect :as wallet-connect]))

;; FIXME(Ferossgp): Should be part of QR scanner not wallet
(fx/defn toggle-flashlight
Expand Down Expand Up @@ -124,7 +125,9 @@
(if (links/universal-link? uri)
{:dispatch [:universal-links/handle-url uri]}
{:browser/show-browser-selection uri})
{:ui/show-error (i18n/label :t/wallet-invalid-address {:data uri})})))
(if (wallet-connect/url? uri)
{:dispatch [::qr-scaner/handle-wallet-connect-uri {:data uri}]}
{:ui/show-error (i18n/label :t/wallet-invalid-address {:data uri})}))))

(fx/defn qr-scanner-result
{:events [:wallet.send/qr-scanner-result]}
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/wallet_connect/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
(fx/defn session-connected
{:events [:wallet-connect/created]}
[{:keys [db]} session]
(let [session (types/js->clj session)
(let [session (merge (types/js->clj session) {:wc-version 2})
client (get db :wallet-connect/client)]
(log/debug "[wallet connect] session created - " session)
{:show-wallet-connect-success-sheet nil
Expand Down
Loading