code review fixes

This commit is contained in:
Volodymyr Kozieiev 2025-01-02 13:44:16 +00:00
parent 2b24462d58
commit e9c4fb5b9c
No known key found for this signature in database
GPG Key ID: 82B04968DF4C0535
7 changed files with 177 additions and 91 deletions

View File

@ -20,13 +20,20 @@
;; will need it later
quo.foundations.colors
;; same for funcs from
;; native-module
native-module.core
;; some funcs used
;; temporarily during
;; development
test-helpers.component
test-helpers.unit
;; not sure if unused
;; functions here should
;; be removed:
keycard.keycard
test-helpers.unit
test-helpers.component
react-native.fs}}
:case-duplicate-test {:level :error}
:case-quoted-test {:level :error}

View File

@ -232,14 +232,14 @@
(let [{:keys [custom-domain?]} (:ens/registration db)
chain-id (chain/chain-id db)
usernames (into #{} (keys (get-in db [:ens/names chain-id])))
st (state custom-domain? username usernames)]
next-state (state custom-domain? username usernames)]
(reset! resolve-last-id (random/id))
(merge
{:db (update db
:ens/registration assoc
:username username
:state st)}
(when (= st :searching)
:state next-state)}
(when (= next-state :searching)
(let [{:profile/keys [profile]} db
{:keys [public-key]} profile
addresses (addresses-without-watch db)

View File

@ -93,16 +93,16 @@
(rf/defn check-transaction
"Check if the transaction has been triggered and applies the effects returned
by `on-trigger` if that is the case"
[{:keys [db] :as cofx} {h :hash :as transaction}]
[{:keys [db] :as cofx} {tx-hash :hash :as transaction}]
(when-let [watch-params
(get-in db [:ethereum/watched-transactions h])]
(get-in db [:ethereum/watched-transactions tx-hash])]
(let [{:keys [trigger-fn on-trigger]} watch-params]
(when (trigger-fn db transaction)
(rf/merge cofx
{:db (update db
:ethereum/watched-transactions
dissoc
h)}
tx-hash)}
(on-trigger transaction))))))
(rf/defn check-watched-transactions
@ -124,14 +124,14 @@
"We determine a unique id for the transfer before adding it because some
transaction can contain multiple transfers and they would overwrite each other
in the transfer map if identified by hash"
[{:keys [db] :as cofx} {h :hash :keys [id address] :as transfer}]
(let [transfer-by-hash (get-in db [:wallet-legacy :accounts address :transactions h])]
[{:keys [db] :as cofx} {tx-hash :hash :keys [id address] :as transfer}]
(let [transfer-by-hash (get-in db [:wallet-legacy :accounts address :transactions tx-hash])]
(when-let [unique-id (when-not (= transfer transfer-by-hash)
(if (and transfer-by-hash
(not (= :pending
(:type transfer-by-hash))))
id
h))]
tx-hash))]
(rf/merge cofx
{:db (assoc-in db
[:wallet-legacy :accounts address :transactions unique-id]
@ -142,7 +142,7 @@
[db address]
(get-in db [:wallet-legacy :accounts (eip55/address->checksum address) :min-block]))
(defn min-block-transfers-count-fn
(defn count-min-block-transfers
[db address]
(get-in db
[:wallet-legacy :accounts
@ -155,14 +155,14 @@
{:keys [min-block min-block-transfers-count]}
(reduce
(fn [{:keys [min-block] :as acc}
{h :hash block :block}]
{tx-hash :hash block :block}]
(cond
(or (nil? min-block) (> min-block (js/parseInt block)))
{:min-block (js/parseInt block)
:min-block-transfers-count 1}
(and (= min-block block)
(nil? (get-in db [:wallet-legacy :accounts checksum :transactions h])))
(nil? (get-in db [:wallet-legacy :accounts checksum :transactions tx-hash])))
(update acc :min-block-transfers-count inc)
:else acc))
@ -171,7 +171,7 @@
(js/parseInt min-block-string))
:min-block-transfers-count
(min-block-transfers-count-fn db address)}
(count-min-block-transfers db address)}
transfers)]
(log/debug "[transactions] set-lowest-fetched-block"
"address" address
@ -283,7 +283,7 @@
[{:keys [db] :as cofx} address]
(let [min-known-block (or (get-min-known-block db address)
(:ethereum/current-block db))
min-count (or (min-block-transfers-count-fn db address) 0)]
min-count (or (count-min-block-transfers db address) 0)]
(rf/merge
cofx
{:transactions/get-transfers

View File

@ -204,9 +204,9 @@
(rf/defn details
{:events [:logging/report-details]}
[{:keys [db]} k value]
[{:keys [db]} log-key value]
{:db (-> db
(assoc-in [:bug-report/details k] value)
(assoc-in [:bug-report/details log-key] value)
(dissoc :bug-report/description-error))})
(def min-description-lenght 6)

View File

@ -1,12 +1,13 @@
(ns legacy.status-im.utils.utils-test
(:require
[cljs.test :refer-macros [deftest is]]
[legacy.status-im.utils.core :as u]))
[legacy.status-im.utils.core :as sut]))
(deftest truncate-str-test
(is (= (u/truncate-str "Long string" 7) "Long...")) ; threshold is less then string length
(is (= (u/truncate-str "Long string" 7 true) "Lo...ng")) ; threshold is less then string length
; (truncate middle)
(is (= (u/truncate-str "Long string" 11) "Long string")) ; threshold is the same as string length
(is (= (u/truncate-str "Long string" 20) "Long string"))) ; threshold is more then string length
(is (= (sut/truncate-str "Long string" 7) "Long...")) ; threshold is less then string length
(is (= (sut/truncate-str "Long string" 7 true) "Lo...ng")) ; threshold is less then string length
; (truncate middle)
(is (= (sut/truncate-str "Long string" 11) "Long string")) ; threshold is the same as string
; length
(is (= (sut/truncate-str "Long string" 20) "Long string"))) ; threshold is more then string length

View File

@ -85,6 +85,14 @@
([callback]
(.acceptTerms ^js (account-manager) callback)))
(defn prepare-dir-and-update-config
[key-uid config callback]
(log/debug "[native-module] prepare-dir-and-update-config")
(.prepareDirAndUpdateConfig ^js (account-manager)
key-uid
config
#(callback (types/json->clj %))))
(defn login-account
"NOTE: beware, the password has to be sha3 hashed"
[{:keys [keyUid] :as request}]
@ -126,6 +134,19 @@
(clear-web-data)
(.logout ^js (account-manager)))
(defn multiaccount-derive-addresses
"NOTE: this should be named derive-accounts
this only derive addresses, they still need to be stored
with `multiaccount-store-derived` if you want to be able to
reuse the derived addresses later"
[account-id paths callback]
(log/debug "[native-module] multiaccount-derive-addresses")
(when (status)
(.multiAccountDeriveAddresses ^js (account-manager)
(types/clj->json {:accountID account-id
:paths paths})
callback)))
(defn multiaccount-generate-and-derive-addresses
"used to generate multiple multiaccounts for onboarding
NOTE: nothing is saved so you will need to use
@ -140,15 +161,54 @@
:paths paths})
callback))
(defn multiaccount-import-mnemonic
[mnemonic password callback]
(log/debug "[native-module] multiaccount-import-mnemonic")
(.multiAccountImportMnemonic ^js (account-manager)
(types/clj->json {:mnemonicPhrase mnemonic
;;NOTE this is not the multiaccount password
:Bip39Passphrase password})
callback))
(defn verify
"NOTE: beware, the password has to be sha3 hashed"
[address hashed-password callback]
(log/debug "[native-module] verify")
(.verify ^js (account-manager) address hashed-password callback))
(defn set-soft-input-mode
[mode]
(log/debug "[native-module] set-soft-input-mode")
(.setSoftInputMode ^js (ui-helper) mode))
(defn call-rpc
[payload callback]
(log/debug "[native-module] call-rpc")
(.callRPC ^js (network) payload callback))
(defn call-private-rpc
[payload callback]
(.callPrivateRPC ^js (network) payload callback))
(defn hash-transaction
"used for keycard"
[rpcParams callback]
(log/debug "[native-module] hash-transaction")
(.hashTransaction ^js (encryption) rpcParams callback))
(defn hash-message
"used for keycard"
[message callback]
(log/debug "[native-module] hash-message")
(.hashMessage ^js (encryption) message callback))
(defn start-searching-for-local-pairing-peers
"starts a UDP multicast beacon that both listens for and broadcasts to LAN peers"
[callback]
(log/info "[native-module] Start Searching for Local Pairing Peers"
{:fn :start-searching-for-local-pairing-peers})
(.startSearchForLocalPairingPeers ^js (network) callback))
(defn local-pairing-preflight-outbound-check
"Checks whether the device has allows connecting to the local server"
[callback]
@ -197,6 +257,24 @@
:public-key public-key})
(.multiformatDeserializePublicKey ^js (encryption) public-key deserialization-key callback))
(defn hash-typed-data
"used for keycard"
[data callback]
(log/debug "[native-module] hash-typed-data")
(.hashTypedData ^js (encryption) data callback))
(defn hash-typed-data-v4
"used for keycard"
[data callback]
(log/debug "[native-module] hash-typed-data-v4")
(.hashTypedDataV4 ^js (encryption) data callback))
(defn send-transaction-with-signature
"used for keycard"
[rpcParams sig callback]
(log/debug "[native-module] send-transaction-with-signature")
(.sendTransactionWithSignature ^js (network) rpcParams sig callback))
(defn sign-message
"NOTE: beware, the password in rpcParams has to be sha3 hashed"
([rpcParams]
@ -205,11 +283,43 @@
(log/debug "[native-module] sign-message")
(.signMessage ^js (encryption) rpcParams callback)))
(defn recover-message
[rpcParams callback]
(log/debug "[native-module] recover")
(.recover ^js (network) rpcParams callback))
(defn send-transaction
"NOTE: beware, the password has to be sha3 hashed"
[rpcParams hashed-password callback]
(log/debug "[native-module] send-transaction")
(.sendTransaction ^js (network) rpcParams hashed-password callback))
(defn sign-typed-data
"NOTE: beware, the password has to be sha3 hashed"
([data account hashed-password]
(native-utils/promisify-native-module-call sign-typed-data data account hashed-password))
([data account hashed-password callback]
(log/debug "[native-module] sign-typed-data")
(.signTypedData ^js (encryption) data account hashed-password callback)))
(defn sign-typed-data-v4
"NOTE: beware, the password has to be sha3 hashed"
([data account hashed-password]
(native-utils/promisify-native-module-call sign-typed-data-v4 data account hashed-password))
([data account hashed-password callback]
(log/debug "[native-module] sign-typed-data-v4")
(.signTypedDataV4 ^js (encryption) data account hashed-password callback)))
(defn send-logs
[dbJson js-logs callback]
(log/debug "[native-module] send-logs")
(.sendLogs ^js (log-manager) dbJson js-logs callback))
(defn close-application
[]
(log/debug "[native-module] close-application")
(.closeApplication ^js (status)))
(defn connection-change
[type expensive?]
(log/debug "[native-module] connection-change")
@ -278,6 +388,23 @@
;; in unknown scenarios we also consider the device rooted to avoid degrading security
:else (callback true)))
(defn encode-transfer
[to-norm amount-hex]
(log/debug "[native-module] encode-transfer")
(.encodeTransfer ^js (encryption) to-norm amount-hex))
(defn encode-function-call
[method params]
(log/debug "[native-module] encode-function-call")
(.encodeFunctionCall ^js (encryption) method (types/clj->json params)))
(defn decode-parameters
[bytes-string types]
(log/debug "[native-module] decode-parameters")
(let [json-str (.decodeParameters ^js (encryption)
(types/clj->json {:bytesString bytes-string :types types}))]
(types/json->clj json-str)))
(defn hex-to-number
[hex]
(log/debug "[native-module] hex-to-number")
@ -295,6 +422,11 @@
(when s
(.sha3 ^js (encryption) (str s))))
(defn utf8-to-hex
[s]
(log/debug "[native-module] utf8-to-hex")
(.utf8ToHex ^js (encryption) s))
(defn hex-to-utf8
[s]
(log/debug "[native-module] hex-to-utf8")
@ -348,6 +480,18 @@
(log/debug "[native-module] delete-multiaccount")
(.deleteMultiaccount ^js (account-manager) key-uid callback))
(defn delete-imported-key
"Delete imported key file."
[key-uid address hashed-password callback]
(log/debug "[native-module] delete-imported-key")
(.deleteImportedKey ^js (status) key-uid address hashed-password callback))
(defn reset-keyboard-input
[input selection]
(log/debug "[native-module] resetKeyboardInput")
(when platform/android?
(.resetKeyboardInputCursor ^js (ui-helper) input selection)))
(defn reset-password
([key-uid current-password-hashed new-password-hashed]
(native-utils/promisify-native-module-call reset-password

View File

@ -9,81 +9,15 @@
;; We must require test-helpers.matchers namespace to register the custom cljs.test directive
;; `match-strict?`
(:require
[re-frame.core :as rf]
[re-frame.db :as rf-db]
[re-frame.events :as rf-events]
[re-frame.registrar :as rf-registrar]
[re-frame.subs :as rf-subs]
[taoensso.timbre :as log]
test-helpers.matchers))
;; We must require `test-helpers.matchers` this namespace to register the custom cljs.test
;; directive `match-strict?`.
(defn db
"A simple wrapper to get the latest value from the app db."
[]
@rf-db/app-db)
(defn spy-event-fx
"Re-register event effect using id `id`, but conj to `state` the event
arguments before calling the original handler.
Callers of this function can later on deref `state` to make assertions.
It's recommended to run this function in the scope of the macro
`day8.re-frame.test/run-test-sync` (or the async variant) as they
automatically clean up effects."
[state id]
(let [interceptors (get-in @rf-registrar/kind->id->handler [:event id])]
(rf-events/register
id
(concat (butlast interceptors)
(list {:id :test/spy-event-fx
:before (fn [context]
(swap! state conj {:id id :args (get-in context [:coeffects :event])})
context)}
(last interceptors))))))
(defn spy-fx
"Re-register effect using id `id`, but conj to `state` the effect arguments
before calling the original effect handler.
Callers of this function can later on inspect `state` to make assertions.
It's recommended to run this function in the scope of the macro
`day8.re-frame.test/run-test-sync` (or the async variant) as they
automatically clean up effects."
[state id]
(let [original-fn (get-in @rf-registrar/kind->id->handler [:fx id])]
(rf/reg-fx
id
(fn [fx-args]
(swap! state conj {:id id :args fx-args})
(original-fn fx-args)))))
(defn stub-fx-with-callbacks
"Re-register effect using id `id` with a no-op version.
This function is useful to redefine effects that expect callbacks, usually to
pass downstream dummy data to successful/failure events. In re-frame parlance,
such effects accept on-success and on-error keywords.
The original effect handler for `id` is expected to receive a single map as
argument with either :on-success or :on-error keywords.
This function expects to receive either `on-success` or `on-error`, but not
both. If both are passed, `on-error` will be preferred."
[id & {:keys [on-success on-error]}]
(rf/reg-fx
id
(fn [[fx-map]]
(let [original-on-error (:on-error fx-map)
original-on-success (:on-success fx-map)]
(cond (and original-on-error on-error)
(original-on-error (on-error fx-map))
(and original-on-success on-success)
(original-on-success (on-success fx-map)))))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} ;; used in unit.clj
(defn restore-app-db
"Saves current app DB, calls `f` and restores the original app DB.