[refactor] replace web3-prototype

- replace web3-prototype wherever possible
- currently only the money namespace is left
for future refactoring, the ideal solution
would be to use strings for big numbers all
the time and only convert for arithmetic operations
- use json-rpc call to replace trivial web3 calls

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2019-06-02 14:10:46 +02:00
parent c8bd627de2
commit 04de022eae
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
21 changed files with 116 additions and 147 deletions

View File

@ -138,9 +138,7 @@ var TopLevel = {
"format" : function () {},
"FormData" : function () {},
"from" : function () {},
"fromAscii" : function () {},
"fromCharCode" : function () {},
"fromUtf8" : function () {},
"fromWei" : function () {},
"generateAndLoadKey" : function () {},
"generateMnemonic" : function () {},
@ -187,6 +185,7 @@ var TopLevel = {
"hexToNumber" : function () {},
"hexToNumberString" : function () {},
"hexToUtf8" : function () {},
"utf8ToHex" : function () {},
"hide" : function () {},
"i18n" : function () {},
"ignoreWarnings" : function () {},

View File

@ -9,6 +9,7 @@
[status-im.constants :as constants]
[status-im.contact.db :as contact.db]
[status-im.data-store.messages :as messages-store]
[status-im.ethereum.core :as ethereum]
[status-im.mailserver.core :as mailserver]
[status-im.native-module.core :as status]
[status-im.notifications.core :as notifications]
@ -356,13 +357,13 @@
wrapped-record (if (= (:message-type send-record) :group-user-message)
(wrap-group-message cofx chat-id send-record)
send-record)
raw-payload (transport.utils/from-utf8 (transit/serialize wrapped-record))
raw-payload (ethereum/utf8-to-hex (transit/serialize wrapped-record))
message-id (transport.utils/message-id from raw-payload)
message-with-id (assoc message
:outgoing-status :sending
:message-id message-id
:old-message-id old-message-id
:raw-payload-hash (transport.utils/sha3 raw-payload))]
:raw-payload-hash (ethereum/sha3 raw-payload))]
(fx/merge cofx
(chat-model/upsert-chat

View File

@ -1,7 +1,6 @@
(ns status-im.contact.db
(:require [cljs.spec.alpha :as spec]
[status-im.ethereum.core :as ethereum]
[status-im.js-dependencies :as js-dependencies]
[status-im.tribute-to-talk.db :as tribute-to-talk.db]
[status-im.utils.gfycat.core :as gfycat]
[status-im.utils.identicon :as identicon]

View File

@ -4,7 +4,6 @@
[re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.data-store.realm.core :as core]
[status-im.js-dependencies :as dependencies]
[status-im.utils.core :as utils]))
(defn get-message-by-id
@ -86,9 +85,6 @@
(fn [cofx _]
(assoc cofx :get-stored-messages get-by-chat-id)))
(defn- sha3 [s]
(.sha3 (dependencies/web3-prototype) s))
(re-frame/reg-cofx
:data-store/get-referenced-messages
(fn [cofx _]

View File

@ -1,14 +1,15 @@
(ns status-im.data-store.realm.schemas.account.migrations
(:require [taoensso.timbre :as log]
[cljs.reader :as reader]
[status-im.chat.models.message-content :as message-content]
[status-im.transport.utils :as transport.utils]
(:require [cljs.reader :as reader]
[cljs.tools.reader.edn :as edn]
[clojure.string :as string]
[status-im.constants :as constants]
[cognitect.transit :as transit]
[status-im.chat.models.message-content :as message-content]
[status-im.constants :as constants]
[status-im.ethereum.core :as ethereum]
[status-im.js-dependencies :as dependencies]
[status-im.utils.clocks :as utils.clocks]))
[status-im.transport.utils :as transport.utils]
[status-im.utils.clocks :as utils.clocks]
[taoensso.timbre :as log]))
(defn v1 [old-realm new-realm]
(log/debug "migrating v1 account database: " old-realm new-realm))
@ -187,13 +188,10 @@
"status-im.data-store.realm.schemas.account.migrations"
"status-im.transport.message.protocol"))
(defn sha3 [s]
(.sha3 (dependencies/web3-prototype) s))
(defn old-message-id
"Calculates the same `message-id` as was used in `0.9.31`"
[message]
(sha3 (replace-ns (pr-str message))))
(ethereum/sha3 (replace-ns (pr-str message))))
;; The code below copied from status-im.transport.message.transit
;; in order to make sure that future changes will not have any impact
@ -239,7 +237,7 @@
(defn raw-payload
[message]
(transport.utils/from-utf8 (serialize message)))
(ethereum/utf8-to-hex (serialize message)))
(defn v27 [old-ream new-realm]
(let [messages (.objects new-realm "message")
@ -264,7 +262,7 @@
old-message-id (old-message-id message-record)
raw-payload (raw-payload message-record)
message-id (transport.utils/message-id from raw-payload)
raw-payload-hash (transport.utils/sha3 raw-payload)]
raw-payload-hash (ethereum/sha3 raw-payload)]
(vswap! old-ids->new-ids assoc prev-message-id message-id)
(if (.objectForPrimaryKey
new-realm

View File

@ -1,7 +1,9 @@
(ns status-im.ethereum.abi-spec
(:require [cljs.spec.alpha :as spec]
[clojure.string :as string]
[status-im.ethereum.core :as ethereum]
[status-im.js-dependencies :as dependencies]))
;; Utility functions for encoding
(defn utils [] (dependencies/web3-utils))
@ -24,9 +26,9 @@
(when x
(subs (.toTwosComplement (utils) x) 2)))
(defn from-utf8 [x]
(defn utf8-to-hex [x]
(when x
(subs (.fromUtf8 (utils) x) 2)))
(subs (ethereum/utf8-to-hex x) 2)))
(defn hex-to-boolean [x]
(= x "0x0"))
@ -40,7 +42,7 @@
(subs (.numberToHex (utils) x) 2)))
(defn hex-to-utf8 [x]
(.hexToUtf8 (utils) (str "0x" x)))
(ethereum/hex-to-utf8 (str "0x" x)))
(defn hex-to-number [x]
(when x
@ -50,9 +52,6 @@
(catch :default err
(.hexToNumberString (utils) hex-x))))))
(defn sha3 [s]
(.sha3 (utils) (str s)))
(defn is-hex? [value]
(when value
(string/starts-with? value "0x")))
@ -102,7 +101,7 @@
(let [encoded-value? (is-hex? value)
encoded-value (if encoded-value?
(subs value 2)
(from-utf8 value))]
(utf8-to-hex value))]
(str (when dynamic? (enc {:type :int :value (/ (count encoded-value) 2)}))
(right-pad encoded-value))))
@ -273,7 +272,7 @@
params))))
(defn signature->method-id [signature]
(apply str (take 10 (sha3 signature))))
(apply str (take 10 (ethereum/sha3 signature))))
(defn encode [method params]
(let [method-id (signature->method-id method)]

View File

@ -4,6 +4,22 @@
[status-im.js-dependencies :as dependencies]
[status-im.utils.money :as money]))
(defn utils [] (dependencies/web3-utils))
(defn sha3 [s]
(when s
(.sha3 (utils) (str s))))
(defn utf8-to-hex [s]
(try
(.utf8ToHex (utils) (str s))
(catch :default err nil)))
(defn hex-to-utf8 [s]
(try
(.hexToUtf8 (utils) s)
(catch :default err nil)))
;; IDs standardized in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md#list-of-chain-ids
(def chains
@ -51,7 +67,7 @@
(defn address? [s]
(when s
(.isAddress (dependencies/web3-prototype) s)))
(.isAddress (utils) s)))
(defn network->chain-id [network]
(get-in network [:config :NetworkId]))
@ -75,12 +91,6 @@
:mainnet :SNT
:STT))
(defn sha3
([s]
(.sha3 (dependencies/web3-prototype) (str s)))
([s opts]
(.sha3 (dependencies/web3-prototype) (str s) (clj->js opts))))
(def default-transaction-gas (money/bignumber 21000))
(defn estimate-gas [symbol]
@ -97,12 +107,9 @@
(defn public-key->address [public-key]
(let [length (count public-key)
normalized-key (case length
132 (subs public-key 4)
130 (subs public-key 2)
128 public-key
132 (str "0x" (subs public-key 4))
130 public-key
128 (str "0x" public-key)
nil)]
(when normalized-key
(subs (.sha3 (dependencies/web3-prototype)
normalized-key
#js {:encoding "hex"})
26))))
(subs (sha3 normalized-key) 26))))

View File

@ -28,8 +28,7 @@
string/lower-case
(string/split #"\." 2))]
(ethereum/sha3 (+ (namehash remainder)
(subs (ethereum/sha3 label) 2))
{:encoding "hex"})))))
(subs (ethereum/sha3 label) 2)))))))
;; Registry contract

View File

@ -23,6 +23,8 @@
"eth_getTransactionReceipt" {}
"eth_newBlockFilter" {:subscription? true}
"eth_newFilter" {:subscription? true}
"eth_syncing" {}
"net_version" {}
"status_joinPublicChat" {}
"status_chats" {}
"status_startOneOnOneChat" {}

View File

@ -963,13 +963,6 @@
(fn [cofx _]
(protocol/check-sync-state cofx)))
;; web3 module
(handlers/register-handler-fx
:web3.callback/get-syncing-success
(fn [cofx [_ error sync]]
(web3/update-syncing-progress cofx error sync)))
;; notifications module
(handlers/register-handler-fx

View File

@ -235,7 +235,7 @@
(defn- event-topic-enc [event params]
(let [eventid (str event "(" (string/join "," params) ")")]
(abi-spec/sha3 eventid)))
(ethereum/sha3 eventid)))
(defn- get-no-indexed
"Return a vector without indexed elements"
@ -392,7 +392,7 @@
{:db (assoc-in db [:wallet :send-transaction]
{:id id
:from (ethereum/current-address db)
:data (or data (str "0x" (abi-spec/from-utf8 message)))
:data (or data (ethereum/utf8-to-hex message))
:on-result [:extensions/wallet-ui-on-success on-success]
:on-error [:extensions/wallet-ui-on-failure on-failure]
:method constants/web3-personal-sign})}

View File

@ -1,16 +1,16 @@
(ns status-im.notifications.core
(:require [goog.object :as object]
[re-frame.core :as re-frame]
[status-im.react-native.js-dependencies :as rn]
[status-im.js-dependencies :as dependencies]
[taoensso.timbre :as log]
[status-im.i18n :as i18n]
[status-im.accounts.db :as accounts.db]
[status-im.contact.db :as contact.db]
[status-im.chat.models :as chat-model]
[status-im.utils.platform :as platform]
[status-im.contact.db :as contact.db]
[status-im.ethereum.core :as ethereum]
[status-im.i18n :as i18n]
[status-im.react-native.js-dependencies :as rn]
[status-im.utils.fx :as fx]
[status-im.utils.utils :as utils]))
[status-im.utils.platform :as platform]
[status-im.utils.utils :as utils]
[taoensso.timbre :as log]))
;; Work in progress namespace responsible for push notifications and interacting
;; with Firebase Cloud Messaging.
@ -51,15 +51,12 @@
(and (= (.-length from) pn-pubkey-hash-length)
(= (.-length to) pn-pubkey-hash-length)))))
(defn sha3 [s]
(.sha3 (dependencies/web3-prototype) s))
(defn anonymize-pubkey
[pubkey]
"Anonymize a public key, if needed, by hashing it and taking the first 4 bytes"
(if (= (count pubkey) pn-pubkey-hash-length)
pubkey
(apply str (take pn-pubkey-hash-length (sha3 pubkey)))))
(apply str (take pn-pubkey-hash-length (ethereum/sha3 pubkey)))))
(defn encode-notification-payload
[{:keys [from to id] :as payload}]

View File

@ -2,8 +2,10 @@
(:require [re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.json-rpc :as json-rpc]
[status-im.i18n :as i18n]
[status-im.mailserver.core :as mailserver]
[status-im.node.core :as node]
[status-im.transport.core :as transport]
[status-im.utils.fx :as fx]
[status-im.utils.semaphores :as semaphores]
@ -29,10 +31,24 @@
(not= sync-state new-state)
(assoc :sync-state new-state))}))
(fx/defn update-syncing-progress
{:events [:ethereum.callback/get-syncing-success]}
[cofx error sync]
(fx/merge cofx
(update-sync-state error sync)
(node/update-sync-state error sync)))
(fx/defn check-sync-state
[{{:keys [web3] :as db} :db :as cofx}]
[{:keys [db] :as cofx}]
(if (:account/account db)
{:web3/get-syncing web3
{:json-rpc/call
[{:method "eth_syncing"
:on-success
(fn [sync]
(re-frame/dispatch [:ethereum.callback/get-syncing-success nil sync]))
:on-error
(fn [error]
(re-frame/dispatch [:ethereum.callback/get-syncing-success error nil]))}]
:utils/dispatch-later [{:ms 10000
:dispatch [:protocol/state-sync-timed-out]}]}
(semaphores/free cofx :check-sync-state?)))
@ -40,23 +56,33 @@
(fx/defn start-check-sync-state
[{{:keys [network account/account] :as db} :db :as cofx}]
(when (and (not (semaphores/locked? cofx :check-sync-state?))
(not (ethereum/network-with-upstream-rpc? (get-in account [:networks network]))))
(not (ethereum/network-with-upstream-rpc?
(get-in account [:networks network]))))
(fx/merge cofx
(check-sync-state)
(semaphores/lock :check-sync-state?))))
(fx/defn initialize-protocol
[{:data-store/keys [transport mailserver-topics mailservers]
:keys [db web3] :as cofx}]
:keys [db] :as cofx}]
(let [network (get-in db [:account/account :network])
network-id (str (get-in db [:account/account :networks network :config :NetworkId]))]
(fx/merge cofx
{:db (assoc db
:rpc-url constants/ethereum-rpc-url
:transport/chats transport
:mailserver/topics mailserver-topics)
:protocol/assert-correct-network {:web3 web3
:network-id network-id}}
{:db (assoc db
:rpc-url constants/ethereum-rpc-url
:transport/chats transport
:mailserver/topics mailserver-topics)
:json-rpc/call
[{:method "net_version"
:on-success
(fn [fetched-network-id]
(when (not= network-id fetched-network-id)
(utils/show-popup
(i18n/label :t/ethereum-node-started-incorrectly-title)
(i18n/label :t/ethereum-node-started-incorrectly-description
{:network-id network-id
:fetched-network-id fetched-network-id})
#(re-frame/dispatch [:protocol.ui/close-app-confirmed]))))}]}
(start-check-sync-state)
(mailserver/initialize-mailserver mailservers)
(transport/init-whisper))))
@ -64,19 +90,3 @@
(fx/defn handle-close-app-confirmed
[_]
{:ui/close-application nil})
(re-frame/reg-fx
:protocol/assert-correct-network
(fn [{:keys [web3 network-id]}]
;; ensure that node was started correctly
(when (and network-id web3) ; necessary because of the unit tests
(.getNetwork (.-version web3)
(fn [error fetched-network-id]
(when (and (not error) ; error most probably means we are offline
(not= network-id fetched-network-id))
(utils/show-popup
(i18n/label :t/ethereum-node-started-incorrectly-title)
(i18n/label :t/ethereum-node-started-incorrectly-description
{:network-id network-id
:fetched-network-id fetched-network-id})
#(re-frame/dispatch [:protocol.ui/close-app-confirmed]))))))))

View File

@ -5,6 +5,7 @@
[status-im.chat.models.message :as models.message]
[status-im.contact.device-info :as device-info]
[status-im.data-store.transport :as transport-store]
[status-im.ethereum.core :as ethereum]
[status-im.transport.message.contact :as contact]
[status-im.transport.message.protocol :as protocol]
[status-im.transport.message.transit :as transit]
@ -36,7 +37,7 @@
dedup-id :id
raw-payload :raw-payload} (unwrap-message js-message)
status-message (-> payload
transport.utils/to-utf8
ethereum/hex-to-utf8
transit/deserialize)]
(when (and sig
status-message

View File

@ -2,6 +2,7 @@
status-im.transport.message.protocol
(:require [cljs.spec.alpha :as spec]
[status-im.accounts.db :as accounts.db]
[status-im.ethereum.core :as ethereum]
[status-im.transport.db :as transport.db]
[status-im.transport.partitioned-topic :as transport.topic]
[status-im.transport.utils :as transport.utils]
@ -122,7 +123,7 @@
(.-payload (:js-obj cofx)))
:chat-id chat-id
:whisper-timestamp timestamp
:raw-payload-hash (transport.utils/sha3
:raw-payload-hash (ethereum/sha3
(.-payload (:js-obj cofx)))
:from signature
:dedup-id (:dedup-id cofx)

View File

@ -1,12 +1,11 @@
(ns ^{:doc "Whisper API and events for managing keys and posting messages"}
status-im.transport.shh
(:require [re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.ethereum.core :as ethereum]
[status-im.transport.message.transit :as transit]
[status-im.transport.utils :as transport.utils]
[taoensso.timbre :as log]
[status-im.transport.partitioned-topic :as transport.topic]
[status-im.utils.config :as config]))
[status-im.transport.utils :as transport.utils]
[taoensso.timbre :as log]))
(defn get-new-key-pair [{:keys [web3 on-success on-error]}]
(if web3
@ -73,7 +72,7 @@
(.. web3
-shh
(sendDirectMessage
(clj->js (update direct-message :payload (comp transport.utils/from-utf8
(clj->js (update direct-message :payload (comp ethereum/utf8-to-hex
transit/serialize)))
(handle-response success-event error-event count))))
@ -101,7 +100,7 @@
:chat (transport.topic/public-key->discovery-topic src)
:payload (-> payload
transit/serialize
transport.utils/from-utf8)})]
ethereum/utf8-to-hex)})]
(.. web3
-shh
(sendPairingMessage
@ -123,7 +122,7 @@
:sig src
:payload (-> payload
transit/serialize
transport.utils/from-utf8)})]
ethereum/utf8-to-hex)})]
(.. web3
-shh
@ -147,7 +146,7 @@
:sig src
:payload (-> payload
transit/serialize
transport.utils/from-utf8)}]
ethereum/utf8-to-hex)}]
(send-public-message! web3 message success-event error-event)))))
(re-frame/reg-fx
@ -156,7 +155,7 @@
(doseq [{:keys [web3 message success-event error-event]
:or {error-event :transport/send-status-message-error}} post-calls]
(post-message {:web3 web3
:whisper-message (update message :payload (comp transport.utils/from-utf8
:whisper-message (update message :payload (comp ethereum/utf8-to-hex
transit/serialize))
:on-success (if success-event
#(re-frame/dispatch (conj success-event % 1))

View File

@ -1,39 +1,26 @@
(ns ^{:doc "Utils for transport layer"}
status-im.transport.utils
(:require [clojure.string :as string]
[status-im.ethereum.core :as ethereum]
[status-im.js-dependencies :as dependencies]))
(defn from-utf8 [s]
(.fromUtf8 (dependencies/web3-prototype) s))
(defn to-ascii [s]
(.toAscii (dependencies/web3-prototype) s))
(defn to-utf8 [s]
(try
(.toUtf8 (dependencies/web3-prototype) (str s))
(catch :default err nil)))
(defn sha3 [s]
(.sha3 (dependencies/web3-prototype) s))
(defn old-message-id
[message]
(sha3 (pr-str message)))
(ethereum/sha3 (pr-str message)))
(defn system-message-id
[{:keys [from chat-id clock-value]}]
(sha3 (str from chat-id clock-value)))
(ethereum/sha3 (str from chat-id clock-value)))
(defn message-id
"Get a message-id"
[from raw-payload]
(sha3 (str from (sha3 raw-payload))))
(ethereum/sha3 (str from (ethereum/sha3 raw-payload))))
(defn get-topic
"Get the topic of a group chat or public chat from the chat-id"
[chat-id]
(subs (sha3 chat-id) 0 10))
(subs (ethereum/sha3 chat-id) 0 10))
(defn shh [web3]
(.-shh web3))

View File

@ -245,7 +245,7 @@
:from address
:data data
:typed? typed?
:decoded-data (if typed? (types/json->clj data) (transport.utils/to-utf8 data))
:decoded-data (if typed? (types/json->clj data) (ethereum/hex-to-utf8 data))
:on-result [:wallet.dapp/transaction-on-result message-id]
:on-error [:wallet.dapp/transaction-on-error message-id]
:method method

View File

@ -1,12 +1,8 @@
(ns status-im.web3.core
(:require [re-frame.core :as re-frame]
[status-im.utils.fx :as fx]
(:require [status-im.ethereum.core :as ethereum]
[status-im.js-dependencies :as dependencies]
[status-im.ethereum.core :as ethereum]
[status-im.node.core :as node]
[status-im.protocol.core :as protocol]
[taoensso.timbre :as log]
[status-im.native-module.core :as status]))
[status-im.native-module.core :as status]
[taoensso.timbre :as log]))
(defn make-internal-web3
"This Web3 object will allow access to private RPC calls
@ -27,19 +23,7 @@
(let [web3 (make-internal-web3)]
(assoc cofx :web3 web3)))
(fx/defn update-syncing-progress [cofx error sync]
(fx/merge cofx
(protocol/update-sync-state error sync)
(node/update-sync-state error sync)))
;;; FX
(defn get-syncing [web3]
(when web3
(.getSyncing
(.-eth web3)
(fn [error sync]
(re-frame/dispatch [:web3.callback/get-syncing-success error sync])))))
(defn set-default-account
[web3 address]
(set! (.-defaultAccount (.-eth web3))

View File

@ -9,10 +9,6 @@
web3/get-web3)
;;;; FX
(re-frame/reg-fx
:web3/get-syncing
web3/get-syncing)
(re-frame/reg-fx
:web3/set-default-account
(fn [[web3 address]]

View File

@ -182,7 +182,8 @@
:data-store/mailserver-topics data/topics}
login-result "{\"error\":\"\"}"
efx (login.core/user-login-callback cofx login-result)
new-db (:db efx)]
new-db (:db efx)
json-rpc (into #{} (map :method (:json-rpc/call efx)))]
(testing ":accounts/login cleared."
(is (not (contains? new-db :accounts/login))))
(testing "Check messaging related effects."
@ -191,7 +192,6 @@
(is (contains? efx :shh/add-discovery-filters))
(is (contains? efx :mailserver/add-peer))
(is (contains? efx :mailserver/update-mailservers))
(is (contains? efx :protocol/assert-correct-network))
(is (= #{{:ms 10000
:dispatch [:mailserver/check-connection-timeout]}
{:ms 10000
@ -200,7 +200,8 @@
(testing "Check the rest of effects."
(is (contains? efx :web3/set-default-account))
(is (contains? efx :web3/fetch-node-version))
(is (contains? efx :web3/get-syncing))
(is (json-rpc "net_version"))
(is (json-rpc "eth_syncing"))
(is (contains? efx :wallet/get-balance))
(is (contains? efx :wallet/get-tokens-balance))
(is (contains? efx :wallet/get-prices))))))