use accounts rpc endpoints

Accounts were previously stored within the multiaccount `setting`
serialized in transit

This moves [:multiaccount :accounts] to `:multiaccount/accounts` and uses
the `getAccounts`, `saveAccounts` and `deleteAccounts` endpoints.

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2019-12-16 12:18:40 +01:00
parent c84c44fe3b
commit 2c3e831a03
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
17 changed files with 151 additions and 75 deletions

View File

@ -69,7 +69,7 @@
(some #(when (:wallet %) %) accounts))
(defn default-address [db]
(-> (get-in db [:multiaccount :accounts])
(-> (get db :multiaccount/accounts)
get-default-account
:address))

View File

@ -88,6 +88,7 @@
"settings_saveNodeConfig" {}
"accounts_getAccounts" {}
"accounts_saveAccounts" {}
"accounts_deleteAccount" {}
"mailservers_ping" {}
"mailservers_addMailserver" {}
"mailservers_getMailservers" {}

View File

@ -1777,7 +1777,7 @@
:address (eip55/address->checksum wallet-address)}}
:mnemonic ""
:address address
:publicKey public-key
:public-key public-key
:keycard-instance-uid instance-uid
:keyUid (ethereum/normalized-hex key-uid)
:keycard-pairing pairing

View File

@ -3,6 +3,7 @@
[re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.eip55 :as eip55]
[taoensso.timbre :as log]
[status-im.i18n :as i18n]
[status-im.hardwallet.nfc :as nfc]
@ -55,9 +56,9 @@
hashed-password (ethereum/sha3 (security/safe-unmask-data key-code))
callback (fn [result]
(let [derived-data (types/json->clj result)
publicKey (get-in derived-data [constants/path-whisper-keyword :publicKey])]
public-key (get-in derived-data [constants/path-whisper-keyword :publicKey])]
(status/gfycat-identicon-async
publicKey
public-key
(fn [name photo-path]
(let [derived-whisper (derived-data constants/path-whisper-keyword)
derived-data-extended (assoc-in derived-data
@ -191,16 +192,16 @@
[multiaccount]
[(let [{:keys [publicKey address]}
(get-in multiaccount [:derived constants/path-default-wallet-keyword])]
{:publicKey publicKey
:address address
{:public-key publicKey
:address (eip55/address->checksum address)
:color colors/blue
:wallet true
:path constants/path-default-wallet
:name "Status account"})
(let [{:keys [publicKey address name photo-path]}
(get-in multiaccount [:derived constants/path-whisper-keyword])]
{:publicKey publicKey
:address address
{:public-key publicKey
:address (eip55/address->checksum address)
:name name
:photo-path photo-path
:path constants/path-whisper
@ -227,7 +228,7 @@
:as multiaccount}
password
{:keys [seed-backed-up? login?] :or {login? true}}]
(let [[wallet-account {:keys [publicKey photo-path name]} :as accounts-data] (prepare-accounts-data multiaccount)
(let [[wallet-account {:keys [public-key photo-path name]} :as accounts-data] (prepare-accounts-data multiaccount)
multiaccount-data {:name name
:address address
:photo-path photo-path
@ -244,11 +245,10 @@
:name name
:photo-path photo-path
;; public key of the chat account
:public-key publicKey
:public-key public-key
;; default address for Dapps
:dapps-address (:address wallet-account)
:latest-derived-path 0
:accounts [wallet-account]
:signing-phrase signing-phrase
:installation-id (random-guid-generator)
:mnemonic mnemonic
@ -266,6 +266,7 @@
:creating? true
:processing true}
:multiaccount new-multiaccount
:multiaccount/accounts [wallet-account]
:networks/current-network constants/default-network
:networks/networks constants/default-networks)]
(fx/merge cofx

View File

@ -11,7 +11,23 @@
(spec/def ::password (spec/and :global/not-empty-string valid-length?))
(spec/def :multiaccount/root-address (spec/nilable string?))
(spec/def :multiaccount/accounts (spec/nilable vector?))
(spec/def :account/path string?)
(spec/def :account/color string?)
(spec/def :account/name string?)
(spec/def :account/storage keyword?)
(spec/def :account/type keyword?)
(spec/def :account/wallet boolean?)
(spec/def :account/chat boolean?)
(spec/def :account/public-key :global/public-key)
(spec/def :account/address :global/address)
(spec/def :multiaccount/account
(spec/keys :req-un [:account/address :account/color :account/name]
:opt-un [:account/public-key :account/path
:account/storage :account/type
:account/wallet :account/chat]))
(spec/def :multiaccount/accounts (spec/coll-of :multiaccount/account :kind vector?))
(spec/def :multiaccount/address :global/address)
(spec/def :multiaccount/key-uid string?)

View File

@ -61,14 +61,34 @@
(fn [[account-data hashed-password]]
(status/login account-data hashed-password)))
(defn rpc->accounts [accounts]
(reduce (fn [acc {:keys [chat type wallet] :as account}]
(if chat
acc
(let [account (cond->
(update account :address
eip55/address->checksum)
type
(update :type keyword))]
;; if the account is the default wallet we
;; put it first in the list
(if wallet
(into [account] acc)
(conj acc account)))))
[]
accounts))
(fx/defn initialize-wallet
{:events [::initialize-wallet]}
[cofx custom-tokens]
(fx/merge cofx
(wallet/initialize-tokens custom-tokens)
(wallet/update-balances nil)
(wallet/update-prices)
(transactions/initialize)))
[{:keys [db] :as cofx} accounts custom-tokens]
(fx/merge
cofx
{:db (assoc db :multiaccount/accounts
(rpc->accounts accounts))}
(wallet/initialize-tokens custom-tokens)
(wallet/update-balances nil)
(wallet/update-prices)
(transactions/initialize)))
(fx/defn login
{:events [:multiaccounts.login.ui/password-input-submitted]}
@ -150,6 +170,27 @@
update-address
(update :accounts (partial mapv update-address)))))
(re-frame/reg-fx
;;TODO: this could be replaced by a single API call on status-go side
::initialize-wallet
(fn [callback]
(-> (js/Promise.all
(clj->js
[(js/Promise.
(fn [resolve reject]
(json-rpc/call {:method "accounts_getAccounts"
:on-success resolve
:on-error reject})))
(js/Promise.
(fn [resolve reject]
(json-rpc/call {:method "wallet_getCustomTokens"
:on-success resolve
:on-error reject})))]))
(.then (fn [[accounts custom-tokens]]
(callback accounts custom-tokens)))
(.catch (fn [error]
(log/error "Failed to initialize wallet"))))))
(fx/defn get-config-callback
{:events [::get-config-callback]}
[{:keys [db] :as cofx} config]
@ -166,9 +207,8 @@
notifications-enabled?)
(assoc ::notifications/enable nil)
(not platform/desktop?)
(assoc ::json-rpc/call
[{:method "wallet_getCustomTokens"
:on-success #(re-frame/dispatch [::initialize-wallet %])}]))
(assoc ::initialize-wallet
#(re-frame/dispatch [::initialize-wallet %])))
;; NOTE: initializing mailserver depends on user mailserver
;; preference which is why we wait for config callback
(protocol/initialize-protocol {:default-mailserver true})
@ -224,7 +264,8 @@
(fx/defn create-only-events
[{:keys [db] :as cofx}]
(let [{:keys [multiaccount :networks/networks :networks/current-network]} db]
(let [{:keys [multiaccount :multiaccount/accounts
:networks/networks :networks/current-network]} db]
(fx/merge cofx
{:db (assoc db
;;NOTE when login the filters are initialized twice
@ -256,7 +297,7 @@
(chaos-mode/check-chaos-mode)
(multiaccounts/switch-preview-privacy-mode-flag)
(when-not platform/desktop?
(initialize-wallet nil)))))
(initialize-wallet accounts nil)))))
(defn- keycard-setup? [cofx]
(boolean (get-in cofx [:db :hardwallet :flow])))

View File

@ -15,7 +15,7 @@
(fx/merge cofx
{:db (assoc db :network-status (if is-connected? :online :offline))}
(when is-connected?
(if-not (= (count (get-in db [:wallet :accounts])) (count (get-in db [:multiaccount :accounts])))
(if-not (= (count (get-in db [:wallet :accounts])) (count (get db :multiaccount/accounts)))
(wallet/update-balances nil)))))
(fx/defn change-network-type

View File

@ -113,6 +113,7 @@
(reg-root-key-sub :multiaccounts/multiaccounts :multiaccounts/multiaccounts)
(reg-root-key-sub :multiaccounts/login :multiaccounts/login)
(reg-root-key-sub :multiaccount :multiaccount)
(reg-root-key-sub :multiaccount/accounts :multiaccount/accounts)
(reg-root-key-sub :get-recover-multiaccount :multiaccounts/recover)
;;chat
(reg-root-key-sub ::cooldown-enabled? :chat/cooldown-enabled?)
@ -444,8 +445,8 @@
(re-frame/reg-sub
:multiaccount/default-account
:<- [:multiaccount]
(fn [{:keys [accounts]}]
:<- [:multiaccount/accounts]
(fn [accounts]
(ethereum/get-default-account accounts)))
(re-frame/reg-sub
@ -467,6 +468,12 @@
(fn [acc]
(get acc :settings)))
(re-frame/reg-sub
:multiaccount/address
:<- [:multiaccount]
(fn [acc]
(get acc :address)))
(re-frame/reg-sub
:settings/current-log-level
:<- [:multiaccount-settings]
@ -482,23 +489,23 @@
(re-frame/reg-sub
:dapps-account
:<- [:multiaccount]
:<- [:multiaccount/accounts]
:<- [:dapps-address]
(fn [[acc address]]
(some #(when (= (:address %) address) %) (:accounts acc))))
(fn [[accounts address]]
(some #(when (= (:address %) address) %) accounts)))
(re-frame/reg-sub
:current-account
:<- [:multiaccount]
:multiaccount/current-account
:<- [:multiaccount/accounts]
:<- [:get-screen-params :wallet-account]
(fn [[macc acc]]
(some #(when (= (:address %) (:address acc)) %) (:accounts macc))))
(fn [[accounts acc]]
(some #(when (= (:address %) (:address acc)) %) accounts)))
(re-frame/reg-sub
:account-by-address
:<- [:multiaccount]
(fn [macc [_ address]]
(some #(when (= (:address %) address) %) (:accounts macc))))
:<- [:multiaccount/accounts]
(fn [accounts [_ address]]
(some #(when (= (:address %) address) %) accounts)))
(re-frame/reg-sub
:multiple-multiaccounts?
@ -515,9 +522,9 @@
(re-frame/reg-sub
:accounts-without-watch-only
:<- [:multiaccount]
(fn [macc]
(filter #(not= (:type %) :watch) (:accounts macc))))
:<- [:multiaccount/accounts]
(fn [accounts]
(filter #(not= (:type %) :watch) accounts)))
;;CHAT ==============================================================================================================
@ -1027,8 +1034,8 @@
(re-frame/reg-sub
:balance-default
:<- [:wallet]
:<- [:multiaccount]
(fn [[wallet {:keys [accounts]}]]
:<- [:multiaccount/accounts]
(fn [[wallet accounts]]
(get-in wallet [:accounts (:address (ethereum/get-default-account accounts)) :balance])))
(re-frame/reg-sub

View File

@ -196,6 +196,7 @@
:multiaccounts/multiaccounts
:multiaccounts/recover
:multiaccounts/login
:multiaccount/accounts
:my-profile/profile
:my-profile/default-name
:my-profile/editing?

View File

@ -128,7 +128,7 @@
[transactions address])])))
(views/defview account []
(views/letsubs [{:keys [name address] :as account} [:current-account]]
(views/letsubs [{:keys [name address] :as account} [:multiaccount/current-account]]
[react/view {:flex 1 :background-color colors/white}
[toolbar-view name]
[react/scroll-view

View File

@ -80,7 +80,7 @@
value)])
(defview account-settings []
(letsubs [{:keys [address color path type] :as account} [:current-account]
(letsubs [{:keys [address color path type] :as account} [:multiaccount/current-account]
new-account (reagent/atom nil)]
[react/keyboard-avoiding-view {:flex 1}
[topbar/toolbar {}
@ -133,4 +133,4 @@
[react/view {:margin-bottom 8 :margin-top 28 :height 1 :background-color colors/gray-lighter}]
[list-item/list-item
{:theme :action-destructive :title :t/delete-account
:on-press #(re-frame/dispatch [:wallet.settings/show-delete-account-confirmation account])}]])]]]))
:on-press #(re-frame/dispatch [:wallet.settings/show-delete-account-confirmation account])}]])]]]))

View File

@ -156,13 +156,14 @@
[icons/icon :main-icons/send {:color :white}]]]]))
(views/defview accounts []
(views/letsubs [{:keys [accounts keycard-pairing]} [:multiaccount]]
(views/letsubs [{:keys [keycard-pairing]} [:multiaccount]
accounts [:multiaccount/accounts]]
[react/scroll-view {:horizontal true}
[react/view {:flex-direction :row :padding-top 11 :padding-bottom 12}
(for [account accounts]
^{:key account}
[account-card account])
; TODO: enable keycard support for adding new accounts
;; TODO: enable keycard support for adding new accounts
(when-not keycard-pairing
[add-card])]]))
@ -175,4 +176,4 @@
[accounts]]
[assets]
[react/view {:height 68}]]
[send-button]])
[send-button]])

View File

@ -27,7 +27,7 @@
:on-press #(re-frame/dispatch [:wallet.send/set-field field account])}]))
(views/defview accounts-list [field]
(views/letsubs [{:keys [accounts]} [:multiaccount]
(views/letsubs [accounts [:multiaccount/accounts]
accounts-whithout-watch [:accounts-without-watch-only]]
[list/flat-list {:data (if (= :to field) accounts accounts-whithout-watch)
:key-fn :address
@ -71,4 +71,4 @@
:accessibility-label :choose-recipient-recipient-code
:on-press #(re-frame/dispatch [:wallet.send/navigate-to-recipient-code])}]]
^{:key item}
[list-item/list-item item])])
[list-item/list-item item])])

View File

@ -39,12 +39,12 @@
[path]
hashed-password
(fn [result]
(let [{:keys [public-key address]}
(let [{:keys [publicKey address]}
(get (types/json->clj result) (keyword path))]
(re-frame/dispatch [::account-generated
{:name (str "Account " path-num)
:address address
:public-key public-key
:public-key publicKey
:path (str constants/path-wallet-root "/" path-num)
:color (rand-nth colors/account-colors)}])))))))))))))
@ -88,39 +88,47 @@
(fx/defn save-account
{:events [:wallet.accounts/save-account]}
[{:keys [db] :as cofx} account {:keys [name color]}]
(let [{:keys [accounts]} (:multiaccount db)
(let [accounts (:multiaccount/accounts db)
new-account (cond-> account
name (assoc :name name)
color (assoc :color color))
new-accounts (replace {account new-account} accounts)]
(multiaccounts.update/multiaccount-update cofx {:accounts new-accounts} nil)))
{::json-rpc/call [{:method "accounts_saveAccounts"
:params [[new-account]]
:on-success #()}]
:db (assoc db :multiaccount/accounts new-accounts)}))
(fx/defn delete-account
{:events [:wallet.accounts/delete-account]}
[{:keys [db] :as cofx} account]
(let [{:keys [accounts]} (:multiaccount db)
(let [accounts (:multiaccount/accounts db)
new-accounts (vec (remove #(= account %) accounts))]
(println account new-accounts)
(fx/merge cofx
(multiaccounts.update/multiaccount-update {:accounts new-accounts} nil)
{::json-rpc/call [{:method "accounts_deleteAccount"
:params [(:address account)]
:on-success #()}]
:db (assoc db :multiaccount/accounts new-accounts)}
(navigation/navigate-to-cofx :wallet nil))))
(fx/defn save-generated-account
{:events [:wallet.accounts/save-generated-account]}
[{:keys [db] :as cofx}]
(let [{:keys [accounts latest-derived-path]} (:multiaccount db)
{:keys [account path type]} (:add-account db)]
(let [{:keys [latest-derived-path]} (:multiaccount db)
{:keys [account path type]} (:add-account db)
accounts (:multiaccount/accounts db)
new-accounts (conj accounts account)]
(when account
(fx/merge cofx
{::json-rpc/call [{:method "accounts_saveAccounts"
:params [[account]]
:on-success #()}]
:db (dissoc db :add-account)}
(multiaccounts.update/multiaccount-update
(merge {:accounts (conj accounts account)}
(when (= type :generate)
{:latest-derived-path (max (int path) latest-derived-path)}))
nil)
:db (-> db
(assoc :multiaccount/accounts new-accounts)
(dissoc :add-account))}
(when (= type :generate)
(multiaccounts.update/multiaccount-update
{:latest-derived-path (inc latest-derived-path)}
nil))
(wallet/update-balances nil)
(navigation/navigate-to-cofx :wallet nil)))))
@ -149,8 +157,6 @@
{:db (assoc-in db [:add-account :account]
{:name ""
:address (eip55/address->checksum (ethereum/normalized-hex address))
:public-key nil
:path ""
:type :watch
:color (rand-nth colors/account-colors)})}
(navigation/navigate-to-cofx :account-added nil))))

View File

@ -78,7 +78,7 @@
(re-frame/reg-fx
::resolve-addresses
(fn [{:keys [registry ens-names callback]}]
;; resolve all addresses then call the callback function with the array of
;; resolve all addresses then call the callback function with the array of
;;addresses as parameter
(-> (js/Promise.all
(clj->js (mapv (fn [ens-name]
@ -127,7 +127,7 @@
amount-changed? (changed-amount-warning old-amount new-amount)
(not (:from previous-state))
(update :db assoc-in [:wallet/prepare-transaction :from]
(ethereum/get-default-account (get-in db [:multiaccount :accounts])))
(ethereum/get-default-account (get db :multiaccount/accounts)))
(not old-symbol)
(update :db assoc-in [:wallet/prepare-transaction :symbol] (or new-symbol :ETH))
(not address) (assoc :ui/show-error (i18n/label :t/wallet-invalid-address {:data uri}))

View File

@ -223,9 +223,11 @@
{:wallet/validate-tokens (get tokens/all-default-tokens chain)})))))
(fx/defn update-balances
[{{:keys [network-status :wallet/all-tokens]
{:keys [settings accounts]} :multiaccount :as db} :db :as cofx} addresses]
(let [addresses (or addresses (map (comp string/lower-case :address) accounts))
[{{:keys [network-status :wallet/all-tokens
multiaccount :multiaccount/accounts] :as db} :db
:as cofx} addresses]
(let [{:keys [settings]} multiaccount
addresses (or addresses (map (comp string/lower-case :address) accounts))
chain (ethereum/chain-keyword db)
assets (get-in settings [:wallet :visible-tokens chain])
tokens (->> (tokens/tokens-for all-tokens chain)
@ -402,7 +404,7 @@
[{:keys [db]}]
(let [identity (:current-chat-id db)]
{:db (assoc db :wallet/prepare-transaction
{:from (ethereum/get-default-account (get-in db [:multiaccount :accounts]))
{:from (ethereum/get-default-account (get db [:multiaccount/accounts]))
:to (or (get-in db [:contacts/contacts identity])
(-> identity
contact.db/public-key->new-contact

View File

@ -2,7 +2,7 @@
"_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead",
"owner": "status-im",
"repo": "status-go",
"version": "v0.0.2.experimental.tokens",
"commit-sha1": "0571f561f0e526a996408be96f649c9e17c47c01",
"src-sha256": "1skk9mbjrnbs7jy3nlag9gbn9wlig50piafvbilrg01flcalc3i3"
"version": "v0.38.1",
"commit-sha1": "f8552280103000d720be98fa69ea09c7ae52d88f",
"src-sha256": "04nrwkj34hj6mdv8h1ky8fa1xxxh4vx1jss5x1rxjb46ckd808mq"
}