make send commands work directly with ens names

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2020-01-15 16:08:55 +01:00
parent 97560d3dfe
commit db74e0431c
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
6 changed files with 151 additions and 103 deletions

View File

@ -137,6 +137,7 @@
:contact (get-contact db address) :contact (get-contact db address)
:contract to :contract to
:approve? (not= type :transfer) :approve? (not= type :transfer)
:value value
:amount (money/to-fixed (money/token->unit value decimals)) :amount (money/to-fixed (money/token->unit value decimals))
:token token :token token
:symbol symbol})))))) :symbol symbol}))))))
@ -153,6 +154,7 @@
{:to to {:to to
:contact (get-contact db to) :contact (get-contact db to)
:symbol :ETH :symbol :ETH
:value value
:amount (str eth-amount) :amount (str eth-amount)
:token (tokens/asset-for (:wallet/all-tokens db) (ethereum/chain-keyword db) :ETH)} :token (tokens/asset-for (:wallet/all-tokens db) (ethereum/chain-keyword db) :ETH)}
(not (nil? token)) (not (nil? token))
@ -182,7 +184,8 @@
:signing/tx tx :signing/tx tx
:signing/sign {:type (if keycard-multiaccount? :keycard :password) :signing/sign {:type (if keycard-multiaccount? :keycard :password)
:formatted-data (if typed? (types/json->clj data) (ethereum/hex-to-utf8 data))})} :formatted-data (if typed? (types/json->clj data) (ethereum/hex-to-utf8 data))})}
(fx/merge cofx (fx/merge
cofx
{:db {:db
(assoc updated-db (assoc updated-db
:signing/in-progress? true :signing/in-progress? true
@ -207,17 +210,21 @@
(fx/defn send-transaction-message (fx/defn send-transaction-message
{:events [::send-transaction-message]} {:events [::send-transaction-message]}
[cofx chat-id transaction-hash signature] [cofx chat-id value contract transaction-hash signature]
{::json-rpc/call [{:method "shhext_sendTransaction" {::json-rpc/call [{:method "shhext_sendTransaction"
:params [chat-id transaction-hash (:result (types/json->clj signature))] :params [chat-id value contract transaction-hash
:on-success #(re-frame/dispatch [:transport/message-sent % 1])}]}) (:result (types/json->clj signature))]
:on-success
#(re-frame/dispatch [:transport/message-sent % 1])}]})
(fx/defn send-accept-request-transaction-message (fx/defn send-accept-request-transaction-message
{:events [::send-accept-transaction-message]} {:events [::send-accept-transaction-message]}
[cofx message-id transaction-hash signature] [cofx message-id transaction-hash signature]
{::json-rpc/call [{:method "shhext_acceptRequestTransaction" {::json-rpc/call [{:method "shhext_acceptRequestTransaction"
:params [transaction-hash message-id (:result (types/json->clj signature))] :params [transaction-hash message-id
:on-success #(re-frame/dispatch [:transport/message-sent % 1])}]}) (:result (types/json->clj signature))]
:on-success
#(re-frame/dispatch [:transport/message-sent % 1])}]})
(fx/defn transaction-result (fx/defn transaction-result
[{:keys [db] :as cofx} result tx-obj] [{:keys [db] :as cofx} result tx-obj]
@ -231,25 +238,28 @@
{:dispatch (conj on-result result)})))) {:dispatch (conj on-result result)}))))
(fx/defn command-transaction-result (fx/defn command-transaction-result
[{:keys [db] :as cofx} result hashed-password tx-obj] [{:keys [db] :as cofx} transaction-hash hashed-password
(let [{:keys [on-result symbol amount]} (get db :signing/tx)] {:keys [message-id chat-id from] :as tx-obj}]
(fx/merge cofx (let [{:keys [on-result symbol amount contract value]} (get db :signing/tx)]
(fx/merge
cofx
{:db (dissoc db :signing/tx :signing/in-progress? :signing/sign) {:db (dissoc db :signing/tx :signing/in-progress? :signing/sign)
:signing.fx/sign-message :signing.fx/sign-message
{:params {:data (str (get-in db [:multiaccount :public-key]) {:params {:data (str (get-in db [:multiaccount :public-key])
(subs result 2)) (subs transaction-hash 2))
:password hashed-password :password hashed-password
:account (:from tx-obj)} :account from}
:on-completed :on-completed
#(re-frame/dispatch #(re-frame/dispatch
(if (:message-id tx-obj) (if message-id
[::send-accept-transaction-message (:message-id tx-obj) result %] [::send-accept-transaction-message message-id transaction-hash %]
[::send-transaction-message (:chat-id tx-obj) result %]))} [::send-transaction-message
chat-id value contract transaction-hash %]))}
:signing/show-transaction-result nil} :signing/show-transaction-result nil}
(prepare-unconfirmed-transaction result tx-obj symbol amount) (prepare-unconfirmed-transaction transaction-hash tx-obj symbol amount)
(check-queue) (check-queue)
#(when on-result #(when on-result
{:dispatch (conj on-result result)})))) {:dispatch (conj on-result transaction-hash)}))))
(fx/defn transaction-error (fx/defn transaction-error
[{:keys [db]} {:keys [code message]}] [{:keys [db]} {:keys [code message]}]

View File

@ -286,12 +286,10 @@
(i18n/label (if (or (= command-state constants/command-state-request-address-for-transaction-declined) (i18n/label (if (or (= command-state constants/command-state-request-address-for-transaction-declined)
(= command-state constants/command-state-request-transaction-declined)) (= command-state constants/command-state-request-transaction-declined))
:t/transaction-declined :t/transaction-declined
(case direction (case transaction-type
:outgoing (case transaction-type
:pending :t/status-pending :pending :t/status-pending
:failed :t/transaction-failed :failed :t/transaction-failed
:t/status-confirmed) :t/status-confirmed)))]])
:incoming :t/status-confirmed)))]])
(defn- command-status-and-timestamp (defn- command-status-and-timestamp
[command-state direction to timestamp-str transaction-type] [command-state direction to timestamp-str transaction-type]

View File

@ -68,6 +68,7 @@
(defn render-contact [contact from-chat?] (defn render-contact [contact from-chat?]
(if from-chat? (if from-chat?
[list-item/list-item {:title (multiaccounts/displayed-name contact) [list-item/list-item {:title (multiaccounts/displayed-name contact)
:subtitle (:address contact)
:icon (multiaccounts/displayed-photo contact)}] :icon (multiaccounts/displayed-photo contact)}]
[list-item/list-item [list-item/list-item
{:title (utils/get-shortened-checksum-address {:title (utils/get-shortened-checksum-address

View File

@ -23,6 +23,8 @@
[status-im.signing.core :as signing] [status-im.signing.core :as signing]
[clojure.string :as string] [clojure.string :as string]
[status-im.contact.db :as contact.db] [status-im.contact.db :as contact.db]
[status-im.ethereum.ens :as ens]
[status-im.ethereum.stateofus :as stateofus]
[status-im.ui.components.bottom-sheet.core :as bottom-sheet])) [status-im.ui.components.bottom-sheet.core :as bottom-sheet]))
(re-frame/reg-fx (re-frame/reg-fx
@ -375,15 +377,33 @@
{:events [:wallet.ui/sign-transaction-button-clicked-from-chat]} {:events [:wallet.ui/sign-transaction-button-clicked-from-chat]}
[{:keys [db] :as cofx} {:keys [to amount from token request? from-chat? gas gasPrice]}] [{:keys [db] :as cofx} {:keys [to amount from token request? from-chat? gas gasPrice]}]
(let [{:keys [symbol address]} token (let [{:keys [symbol address]} token
amount-hex (str "0x" (abi-spec/number-to-hex amount))
to-norm (ethereum/normalized-hex (if (string? to) to (:address to))) to-norm (ethereum/normalized-hex (if (string? to) to (:address to)))
from-address (:address from) from-address (:address from)
identity (:current-chat-id db)] identity (:current-chat-id db)
(fx/merge cofx db (-> db
{:db (-> db
(update-in [:chat-ui-props identity] dissoc :input-bottom-sheet) (update-in [:chat-ui-props identity] dissoc :input-bottom-sheet)
(dissoc :wallet/prepare-transaction)) (dissoc :wallet/prepare-transaction))]
;;TODO from chat, send request message or if ens name sign tx and send tx message (if to-norm
::json-rpc/call [{:method "shhext_requestAddressForTransaction" (fx/merge
cofx
{:db db}
(signing/sign {:tx-obj (if (= symbol :ETH)
{:to to-norm
:from from-address
:chat-id identity
:command? true
:value amount-hex}
{:to (ethereum/normalized-hex address)
:from from-address
:chat-id identity
:command? true
:data (abi-spec/encode
"transfer(address,uint256)"
[to-norm amount-hex])})}))
{:db db
::json-rpc/call
[{:method "shhext_requestAddressForTransaction"
:params [(:current-chat-id db) :params [(:current-chat-id db)
from-address from-address
amount amount
@ -449,7 +469,9 @@
(fx/merge cofx (fx/merge cofx
{:db (dissoc db :wallet/prepare-transaction)} {:db (dissoc db :wallet/prepare-transaction)}
(fn [cofx] (fn [cofx]
(signing/sign cofx {:tx-obj (if (= symbol :ETH) (signing/sign
cofx
{:tx-obj (if (= symbol :ETH)
{:to to-norm {:to to-norm
:from from-address :from from-address
:message-id (:id request-parameters) :message-id (:id request-parameters)
@ -461,10 +483,7 @@
:message-id (:id request-parameters) :message-id (:id request-parameters)
:data (abi-spec/encode :data (abi-spec/encode
"transfer(address,uint256)" "transfer(address,uint256)"
[to-norm amount-hex]) [to-norm amount-hex])})})))))
;;Note: data from qr (eip681)
:gas gas
:gasPrice gasPrice})})))))
(fx/defn sign-transaction-button-clicked (fx/defn sign-transaction-button-clicked
{:events [:wallet.ui/sign-transaction-button-clicked]} {:events [:wallet.ui/sign-transaction-button-clicked]}
@ -498,19 +517,16 @@
from-address (:address from)] from-address (:address from)]
(fx/merge cofx (fx/merge cofx
{:db (dissoc db :wallet/prepare-transaction)} {:db (dissoc db :wallet/prepare-transaction)}
(fn [cofx] (signing/sign
(signing/sign cofx {:tx-obj (if (= symbol :ETH) {:tx-obj (merge {:from from-address}
(if (= symbol :ETH)
{:to to-norm {:to to-norm
:from from-address
:value amount-hex} :value amount-hex}
{:to (ethereum/normalized-hex address) {:to (ethereum/normalized-hex address)
:from from-address :from from-address
:data (abi-spec/encode :data (abi-spec/encode
"transfer(address,uint256)" "transfer(address,uint256)"
[to-norm amount-hex]) [to-norm amount-hex])}))}))))
;;Note: data from qr (eip681)
:gas gas
:gasPrice gasPrice})})))))
(fx/defn set-and-validate-amount-request (fx/defn set-and-validate-amount-request
{:events [:wallet.request/set-and-validate-amount]} {:events [:wallet.request/set-and-validate-amount]}
@ -521,24 +537,46 @@
(assoc-in [:wallet :request-transaction :amount-text] amount) (assoc-in [:wallet :request-transaction :amount-text] amount)
(assoc-in [:wallet :request-transaction :amount-error] error))})) (assoc-in [:wallet :request-transaction :amount-error] error))}))
;;TODO request isn't implemented
(fx/defn set-symbol-request (fx/defn set-symbol-request
{:events [:wallet.request/set-symbol]} {:events [:wallet.request/set-symbol]}
[{:keys [db]} symbol] [{:keys [db]} symbol]
{:db (assoc-in db [:wallet :request-transaction :symbol] symbol)}) {:db (assoc-in db [:wallet :request-transaction :symbol] symbol)})
(re-frame/reg-fx
::resolve-address
(fn [{:keys [registry ens-name cb]}]
(ens/get-addr registry ens-name cb)))
(fx/defn on-recipient-address-resolved
{:events [::recipient-address-resolved]}
[{:keys [db]} address]
{:db (assoc-in db [:wallet/prepare-transaction :to :address] address)})
(fx/defn prepare-transaction-from-chat (fx/defn prepare-transaction-from-chat
{:events [:wallet/prepare-transaction-from-chat]} {:events [:wallet/prepare-transaction-from-chat]}
[{:keys [db]}] [{:keys [db]}]
(let [identity (:current-chat-id db)] (let [chain (ethereum/chain-keyword db)
{:db (assoc db :wallet/prepare-transaction identity (:current-chat-id db)
{:from (ethereum/get-default-account (:multiaccount/accounts db)) {:keys [ens-verified name] :as contact}
:to (or (get-in db [:contacts/contacts identity]) (or (get-in db [:contacts/contacts identity])
(-> identity (-> identity
contact.db/public-key->new-contact contact.db/public-key->new-contact
contact.db/enrich-contact)) contact.db/enrich-contact))]
(cond-> {:db (assoc db
:wallet/prepare-transaction
{:from (ethereum/get-default-account
(:multiaccount/accounts db))
:to contact
:symbol :ETH :symbol :ETH
:from-chat? true})})) :from-chat? true})}
ens-verified
(assoc ::resolve-address
{:registry (get ens/ens-registries chain)
:ens-name (if (= (.indexOf name ".") -1)
(stateofus/subdomain name)
name)
;;TODO handle errors and timeout for ens name resolution
:cb #(re-frame/dispatch [::recipient-address-resolved %])}))))
(fx/defn prepare-request-transaction-from-chat (fx/defn prepare-request-transaction-from-chat
{:events [:wallet/prepare-request-transaction-from-chat]} {:events [:wallet/prepare-request-transaction-from-chat]}

View File

@ -2,7 +2,7 @@
"_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead", "_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead",
"owner": "status-im", "owner": "status-im",
"repo": "status-go", "repo": "status-go",
"version": "v0.39.6", "version": "v0.39.8",
"commit-sha1": "25d46c6d82fbf3e4b6659cb96cf1789bac87f12a", "commit-sha1": "456bcfa022fedb476fadb82bedaa6c07b0c5f869",
"src-sha256": "1m6d07hlxfp8yr94ld73m6dflrgdyjhmpr2ajnjkhjlv9ffvicgb" "src-sha256": "0gd4mfai366lqpw522l9yjlxp9v0i1wfjj2bkfzmgb3lg1hl8l6h"
} }

View File

@ -33,6 +33,7 @@
:to to :to to
:contact {:address to} :contact {:address to}
:symbol :ETH :symbol :ETH
:value "0x8ac7230489e80000"
:amount "10"}))))) :amount "10"})))))
(testing "after second transaction" (testing "after second transaction"
(testing "signing still in progress" (testing "signing still in progress"