Skip to content

Commit

Permalink
Add approve, reject, and disconnect session capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
briansztamfater committed Oct 29, 2021
1 parent 9bb5a47 commit cb96d1b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/status_im/ui/screens/profile/user/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
:chevron true
:on-press #(do
(wallet-connect-init)
(re-frame/dispatch [:bottom-sheet/show-sheet :wallet-connect-session-proposal
(re-frame/dispatch [::qr-scanner/scan-code
{:title "Wallet Connect 2.0"
:handler :wallet-connect/qr-code-scanned}]))}])
[quo/list-item
Expand Down
4 changes: 2 additions & 2 deletions src/status_im/ui/screens/wallet/wallet_connect/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@
[react/view {:flex 1
:margin-right 4}
[quo/button
{:on-press #(re-frame/dispatch [:bottom-sheet/hide])}
{:on-press #(re-frame/dispatch [:wallet-connect/reject-proposal])}
(i18n/label :t/cancel)]]
[react/view {:flex 1
:margin-left 4}
[quo/button
{:theme :accent
:on-press #(re-frame/dispatch [:wallet-connect/approve-proposal])}
:on-press #(re-frame/dispatch [:wallet-connect/approve-proposal (first accounts)])}
(i18n/label :t/connect)]]
]]]))

Expand Down
59 changes: 54 additions & 5 deletions src/status_im/wallet/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
[status-im.signing.gas :as signing.gas]
[clojure.set :as clojure.set]
[status-im.utils.wallet-connect :as wallet-connect]
[status-im.ui.components.react :as react]))
[status-im.ui.components.react :as react]
[clojure.string :as str]))

(defn get-balance
[{:keys [address on-success on-error]}]
Expand Down Expand Up @@ -1053,7 +1054,7 @@
proposer (:proposer proposal)
metadata (:metadata proposer)]
(println "EVENT - SESSION PROPOSAL" proposal proposer metadata (keys proposal) (type proposal) (get proposal :proposer))
{:db (assoc db :wallet-connect/proposal-metadata metadata)
{:db (assoc db :wallet-connect/proposal proposal :wallet-connect/proposal-metadata metadata)
:dispatch [:bottom-sheet/show-sheet :wallet-connect-session-proposal]}))

(fx/defn created-handler
Expand All @@ -1077,19 +1078,67 @@
(.on wallet-connect-client (wallet-connect/session-deleted-event) #(re-frame/dispatch [:wallet-connect/deleted %]))
(.on wallet-connect-client (wallet-connect/session-proposal-event) #(re-frame/dispatch [:wallet-connect/proposal %])))

(def default-metadata {:name "Status Wallet"
:description "Status is a secure messaging app, crypto wallet, and Web3 browser built with state of the art technology."
:url "#"
:icons ["https://statusnetwork.com/img/press-kit-status-logo.svg"]})

(fx/defn approve-proposal
{:events [:wallet-connect/approve-proposal]}
[{:keys [db]} data opts]
[{:keys [db]} account]
(let [client (get db :wallet-connect/client)
proposal (get db :wallet-connect/proposal)
topic (:topic proposal)
permissions (:permissions proposal)
blockchain (:blockchain permissions)
proposal-chain-ids (map #(last (str/split % #":")) (:chains blockchain))
available-chain-ids (map #(get-in % [:config :NetworkId]) (vals (get db :networks/networks)))
supported-chain-ids (filter (fn [chain-id] #(boolean (some #{chain-id} available-chain-ids))) proposal-chain-ids)
address (:address account)
accounts (map #(str "eip155:" % ":" (ethereum/normalized-hex address)) supported-chain-ids)
;; TODO: Check for unsupported
metadata (get db :wallet-connect/proposal-metadata)
response {:state {:accounts accounts}
:metadata default-metadata}
active-topics (or (:wallet-connect/active-topics db) {})
new-active-topics (assoc active-topics topic proposal)]
(println accounts "<- APPROVE PAYLOAD")
;; (.approve client (clj->js {:proposal proposal :response response}))
(-> client
(.approve (clj->js {:proposal proposal :response response}))
(.then #(re-frame/dispatch [:bottom-sheet/show-sheet :wallet-connect-session-proposal]))
(.catch #(println % "ERROR")))
{:dispatch [:bottom-sheet/hide]
:db (assoc db :wallet-connect/active-topics new-active-topics)}))

(fx/defn reject-proposal
{:events [:wallet-connect/reject-proposal]}
[{:keys [db]} account]
(let [client (get db :wallet-connect/client)
proposal (get db :wallet-connect/proposal)]
(-> client
(.reject (clj->js {:proposal proposal}))
(.then #(println "REJECTED"))
(.catch #(println % "ERROR")))
{:dispatch [:bottom-sheet/hide]}))

(fx/defn disconnect-session
{:events [:wallet-connect/disconnect-session]}
[{:keys [db]} topic reason]
(let [client (get db :wallet-connect/client)]
{:dispatch [:bottom-sheet/hide-sheet]
(-> client
(.disconnect (clj->js {:topic topic :reason reason}))
(.then #(println "SESSION DISCONNECTED - TOPIC: " topic))
(.catch #(println % "ERROR")))
{:dispatch [:bottom-sheet/hide]
:db (assoc db :wallet-connect/sessions "session data here")}))

(fx/defn qr-code-scanned
{:events [:wallet-connect/qr-code-scanned]}
[{:keys [db]} data opts]
(let [client (get db :wallet-connect/client)
;; used for testing purposes, remove later and use data variable instead
uri "wc:482d6ccae40458f2cc64a45d002da7632c3db11fe768e757e1036bad5e03c7b7@2?controller=false&publicKey=9440a606ce65c3dadff527898b923ee1dea647dae5970125a2ccd354ea641f72&relay=%7B%22protocol%22%3A%22waku%22%7D"]
uri "wc:788fc9349e570fd5ab21249447e986c0569029d2cfd209dc6adf6628f011d444@2?controller=false&publicKey=db73fef4686bba44de17387729da3edb0b2f1178c50d45725d0d3e02b1ac3c75&relay=%7B%22protocol%22%3A%22waku%22%7D"]
(.pair client (clj->js {:uri uri}))
{:dispatch [:navigate-back]
:db (assoc db :wallet-connect/scanned-uri data)}))
Expand Down

0 comments on commit cb96d1b

Please sign in to comment.