2017-09-05 14:14:45 +00:00
|
|
|
(ns status-im.native-module.core
|
2019-09-04 20:59:44 +00:00
|
|
|
(:require [clojure.string :as string]
|
2019-09-04 18:59:41 +00:00
|
|
|
[re-frame.core :as re-frame]
|
2019-09-12 09:41:25 +00:00
|
|
|
[status_im.utils.db :as utils.db]
|
2019-09-04 18:59:41 +00:00
|
|
|
[status-im.react-native.js-dependencies :as rn-dependencies]
|
2019-09-04 20:59:44 +00:00
|
|
|
[status-im.ui.components.react :as react]
|
2019-09-04 18:59:41 +00:00
|
|
|
[status-im.utils.platform :as platform]
|
2019-11-15 08:09:17 +00:00
|
|
|
[status-im.utils.types :as types]
|
|
|
|
[taoensso.timbre :as log]))
|
2019-09-04 18:59:41 +00:00
|
|
|
|
|
|
|
(defn status []
|
|
|
|
(when (exists? (.-NativeModules rn-dependencies/react-native))
|
|
|
|
(.-Status (.-NativeModules rn-dependencies/react-native))))
|
2017-09-05 14:14:45 +00:00
|
|
|
|
|
|
|
(def adjust-resize 16)
|
|
|
|
|
2019-09-04 18:59:41 +00:00
|
|
|
(defn clear-web-data []
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] clear-web-data")
|
2019-09-04 18:59:41 +00:00
|
|
|
(when (status)
|
|
|
|
(.clearCookies (status))
|
|
|
|
(.clearStorageAPIs (status))))
|
|
|
|
|
2019-07-31 16:10:38 +00:00
|
|
|
(defn init-keystore []
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] init-keystore")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.initKeystore (status)))
|
2019-07-31 16:10:38 +00:00
|
|
|
|
2019-08-01 20:11:59 +00:00
|
|
|
(defn open-accounts [callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] open-accounts")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.openAccounts (status) #(callback (types/json->clj %))))
|
2017-08-21 14:49:31 +00:00
|
|
|
|
2019-09-04 18:59:41 +00:00
|
|
|
(defn prepare-dir-and-update-config
|
|
|
|
[config callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] prepare-dir-and-update-config")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.prepareDirAndUpdateConfig (status)
|
|
|
|
config
|
|
|
|
#(callback (types/json->clj %))))
|
2018-12-19 12:20:03 +00:00
|
|
|
|
2019-10-21 13:09:57 +00:00
|
|
|
(defn enable-notifications []
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] enable-notifications")
|
2019-10-21 13:09:57 +00:00
|
|
|
(.enableNotifications (status)))
|
|
|
|
|
|
|
|
(defn disable-notifications []
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] disable-notifications")
|
2019-10-21 13:09:57 +00:00
|
|
|
(.disableNotifications (status)))
|
|
|
|
|
2019-08-01 20:11:59 +00:00
|
|
|
(defn save-account-and-login
|
2019-09-04 20:59:44 +00:00
|
|
|
"NOTE: beware, the password has to be sha3 hashed"
|
|
|
|
[multiaccount-data hashed-password config accounts-data]
|
2019-12-05 06:02:31 +00:00
|
|
|
(log/debug "[native-module] save-account-and-login"
|
|
|
|
"multiaccount-data" multiaccount-data)
|
2019-09-04 18:59:41 +00:00
|
|
|
(clear-web-data)
|
2019-09-04 20:59:44 +00:00
|
|
|
(.saveAccountAndLogin (status) multiaccount-data hashed-password config accounts-data))
|
2018-11-26 15:52:29 +00:00
|
|
|
|
2019-08-15 14:44:25 +00:00
|
|
|
(defn save-account-and-login-with-keycard
|
|
|
|
"NOTE: chat-key is a whisper private key sent from keycard"
|
|
|
|
[multiaccount-data password config chat-key]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] save-account-and-login-with-keycard")
|
2019-08-15 14:44:25 +00:00
|
|
|
(.saveAccountAndLoginWithKeycard (status) multiaccount-data password config chat-key))
|
|
|
|
|
2019-08-01 20:11:59 +00:00
|
|
|
(defn login
|
2019-09-04 20:59:44 +00:00
|
|
|
"NOTE: beware, the password has to be sha3 hashed"
|
|
|
|
[account-data hashed-password]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] login")
|
2019-09-04 18:59:41 +00:00
|
|
|
(clear-web-data)
|
2019-09-04 20:59:44 +00:00
|
|
|
(.login (status) account-data hashed-password))
|
2019-09-04 18:59:41 +00:00
|
|
|
|
|
|
|
(defn logout []
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] logout")
|
2019-09-04 18:59:41 +00:00
|
|
|
(clear-web-data)
|
|
|
|
(.logout (status)))
|
|
|
|
|
|
|
|
(defonce listener
|
2019-09-04 20:59:44 +00:00
|
|
|
(.addListener react/device-event-emitter "gethEvent"
|
2019-09-04 18:59:41 +00:00
|
|
|
#(re-frame/dispatch [:signals/signal-received (.-jsonEvent %)])))
|
|
|
|
|
|
|
|
(defn multiaccount-load-account
|
2019-09-04 20:59:44 +00:00
|
|
|
"NOTE: beware, the password has to be sha3 hashed
|
|
|
|
|
|
|
|
this function is used after storing an account when you still want to
|
2019-09-04 18:59:41 +00:00
|
|
|
derive accounts from it, because saving an account flushes the loaded keys
|
|
|
|
from memory"
|
2019-09-04 20:59:44 +00:00
|
|
|
[address hashed-password callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] multiaccount-load-account")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.multiAccountLoadAccount (status)
|
|
|
|
(types/clj->json {:address address
|
2019-09-04 20:59:44 +00:00
|
|
|
:password hashed-password})
|
2019-09-04 18:59:41 +00:00
|
|
|
callback))
|
|
|
|
|
|
|
|
(defn multiaccount-reset
|
|
|
|
"TODO: this function is not used anywhere
|
|
|
|
if usage isn't planned, remove"
|
|
|
|
[callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] multiaccount-reset")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.multiAccountReset (status)
|
|
|
|
callback))
|
|
|
|
|
|
|
|
(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]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] multiaccount-derive-addresses")
|
2019-09-04 18:59:41 +00:00
|
|
|
(when (status)
|
|
|
|
(.multiAccountDeriveAddresses (status)
|
|
|
|
(types/clj->json {:accountID account-id
|
|
|
|
:paths paths})
|
|
|
|
callback)))
|
|
|
|
|
|
|
|
(defn multiaccount-store-account
|
2019-09-04 20:59:44 +00:00
|
|
|
"NOTE: beware, the password has to be sha3 hashed
|
|
|
|
|
|
|
|
this stores the account and flush keys in memory so
|
2019-09-04 18:59:41 +00:00
|
|
|
in order to also store derived accounts like initial wallet
|
|
|
|
and chat accounts, you need to load the account again with
|
|
|
|
`multiaccount-load-account` before using `multiaccount-store-derived`
|
|
|
|
and the id of the account stored will have changed"
|
2019-09-04 20:59:44 +00:00
|
|
|
[account-id hashed-password callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] multiaccount-store-account")
|
2019-09-04 18:59:41 +00:00
|
|
|
(when (status)
|
|
|
|
(.multiAccountStoreAccount (status)
|
|
|
|
(types/clj->json {:accountID account-id
|
2019-09-04 20:59:44 +00:00
|
|
|
:password hashed-password})
|
2019-09-04 18:59:41 +00:00
|
|
|
callback)))
|
|
|
|
|
|
|
|
(defn multiaccount-store-derived
|
2019-09-04 20:59:44 +00:00
|
|
|
"NOTE: beware, the password has to be sha3 hashed"
|
|
|
|
[account-id paths hashed-password callback]
|
2019-12-05 06:02:31 +00:00
|
|
|
(log/debug "[native-module] multiaccount-store-derived"
|
|
|
|
"account-id" account-id)
|
2019-09-04 18:59:41 +00:00
|
|
|
(.multiAccountStoreDerived (status)
|
|
|
|
(types/clj->json {:accountID account-id
|
|
|
|
:paths paths
|
2019-09-04 20:59:44 +00:00
|
|
|
:password hashed-password})
|
2019-09-04 18:59:41 +00:00
|
|
|
callback))
|
|
|
|
|
|
|
|
(defn multiaccount-generate-and-derive-addresses
|
|
|
|
"used to generate multiple multiaccounts for onboarding
|
|
|
|
NOTE: nothing is saved so you will need to use
|
|
|
|
`multiaccount-store-account` on the selected multiaccount
|
|
|
|
to store the key"
|
|
|
|
[n mnemonic-length paths callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] multiaccount-generate-and-derive-addresses")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.multiAccountGenerateAndDeriveAddresses (status)
|
|
|
|
(types/clj->json {:n n
|
|
|
|
:mnemonicPhraseLength mnemonic-length
|
|
|
|
:bip39Passphrase ""
|
|
|
|
:paths paths})
|
|
|
|
callback))
|
|
|
|
|
|
|
|
(defn multiaccount-import-mnemonic
|
|
|
|
[mnemonic password callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] multiaccount-import-mnemonic")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.multiAccountImportMnemonic (status)
|
|
|
|
(types/clj->json {:mnemonicPhrase mnemonic
|
2019-09-04 20:59:44 +00:00
|
|
|
;;NOTE this is not the multiaccount password
|
2019-09-04 18:59:41 +00:00
|
|
|
:Bip39Passphrase password})
|
|
|
|
|
|
|
|
callback))
|
2019-08-01 15:49:33 +00:00
|
|
|
|
2019-09-04 20:59:44 +00:00
|
|
|
(defn verify
|
|
|
|
"NOTE: beware, the password has to be sha3 hashed"
|
|
|
|
[address hashed-password callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] verify")
|
2019-09-04 20:59:44 +00:00
|
|
|
(.verify (status) address hashed-password callback))
|
2018-11-26 15:52:29 +00:00
|
|
|
|
2018-12-04 13:49:09 +00:00
|
|
|
(defn login-with-keycard
|
2019-08-15 14:44:25 +00:00
|
|
|
[{:keys [multiaccount-data password chat-key]}]
|
2019-11-28 09:57:58 +00:00
|
|
|
(log/debug "[native-module] login-with-keycard")
|
2019-09-04 18:59:41 +00:00
|
|
|
(clear-web-data)
|
2019-08-15 14:44:25 +00:00
|
|
|
(.loginWithKeycard (status) multiaccount-data password chat-key))
|
2018-12-04 13:49:09 +00:00
|
|
|
|
2017-09-05 14:14:45 +00:00
|
|
|
(defn set-soft-input-mode [mode]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] set-soft-input-mode")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.setSoftInputMode (status) mode))
|
2017-09-05 14:14:45 +00:00
|
|
|
|
2018-08-10 17:15:16 +00:00
|
|
|
(defn call-rpc [payload callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] call-rpc")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.callRPC (status) payload callback))
|
2017-09-05 14:14:45 +00:00
|
|
|
|
2018-08-10 17:15:16 +00:00
|
|
|
(defn call-private-rpc [payload callback]
|
2019-09-04 18:59:41 +00:00
|
|
|
(.callPrivateRPC (status) payload callback))
|
2018-04-18 08:49:15 +00:00
|
|
|
|
2019-09-04 20:59:44 +00:00
|
|
|
(defn hash-transaction
|
|
|
|
"used for keycard"
|
|
|
|
[rpcParams callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] hash-transaction")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.hashTransaction (status) rpcParams callback))
|
2019-02-28 15:09:21 +00:00
|
|
|
|
2019-09-04 20:59:44 +00:00
|
|
|
(defn hash-message
|
|
|
|
"used for keycard"
|
|
|
|
[message callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] hash-message")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.hashMessage (status) message callback))
|
2019-04-03 08:46:53 +00:00
|
|
|
|
2019-09-04 20:59:44 +00:00
|
|
|
(defn hash-typed-data
|
|
|
|
"used for keycard"
|
|
|
|
[data callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] hash-typed-data")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.hashTypedData (status) data callback))
|
|
|
|
|
2019-09-04 20:59:44 +00:00
|
|
|
(defn send-transaction-with-signature
|
|
|
|
"used for keycard"
|
|
|
|
[rpcParams sig callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] send-transaction-with-signature")
|
2019-09-04 20:59:44 +00:00
|
|
|
(.sendTransactionWithSignature (status) rpcParams sig callback))
|
2019-09-04 18:59:41 +00:00
|
|
|
|
2019-09-04 20:59:44 +00:00
|
|
|
(defn sign-message
|
|
|
|
"NOTE: beware, the password in rpcParams has to be sha3 hashed"
|
|
|
|
[rpcParams callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] sign-message")
|
2019-09-04 20:59:44 +00:00
|
|
|
(.signMessage (status) rpcParams callback))
|
2019-09-04 18:59:41 +00:00
|
|
|
|
2019-09-04 20:59:44 +00:00
|
|
|
(defn send-transaction
|
|
|
|
"NOTE: beware, the password has to be sha3 hashed"
|
|
|
|
[rpcParams hashed-password callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] send-transaction")
|
2019-09-04 20:59:44 +00:00
|
|
|
(.sendTransaction (status) rpcParams hashed-password callback))
|
2019-04-03 08:46:53 +00:00
|
|
|
|
2019-09-04 20:59:44 +00:00
|
|
|
(defn sign-typed-data
|
|
|
|
"NOTE: beware, the password has to be sha3 hashed"
|
|
|
|
[data account hashed-password callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] clear-web-data")
|
2019-09-04 20:59:44 +00:00
|
|
|
(.signTypedData (status) data account hashed-password callback))
|
2019-02-28 15:09:21 +00:00
|
|
|
|
2019-04-30 11:49:45 +00:00
|
|
|
(defn send-logs [dbJson js-logs callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] send-logs")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.sendLogs (status) dbJson js-logs callback))
|
2018-12-15 18:57:00 +00:00
|
|
|
|
2019-09-04 18:59:41 +00:00
|
|
|
(defn add-peer [enode on-result]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] add-peer")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.addPeer (status) enode on-result))
|
2017-12-05 10:18:30 +00:00
|
|
|
|
2017-10-05 11:23:25 +00:00
|
|
|
(defn close-application []
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] close-application")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.closeApplication (status)))
|
2018-02-26 02:27:29 +00:00
|
|
|
|
2019-09-03 10:51:53 +00:00
|
|
|
(defn connection-change [type expensive?]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] connection-change")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.connectionChange (status) type (boolean expensive?)))
|
2018-03-16 12:01:10 +00:00
|
|
|
|
|
|
|
(defn app-state-change [state]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] app-state-change")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.appStateChange (status) state))
|
2018-05-01 10:27:04 +00:00
|
|
|
|
2019-05-08 12:26:39 +00:00
|
|
|
(defn set-blank-preview-flag [flag]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] set-blank-preview-flag")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.setBlankPreviewFlag (status) flag))
|
2019-05-08 12:26:39 +00:00
|
|
|
|
2018-08-03 16:43:37 +00:00
|
|
|
(defn is24Hour []
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] is24Hour")
|
2019-09-04 18:59:41 +00:00
|
|
|
;;NOTE: we have to check for status module because of tests
|
|
|
|
(when (status)
|
|
|
|
(.-is24Hour (status))))
|
2018-07-19 15:51:06 +00:00
|
|
|
|
2019-05-30 14:01:20 +00:00
|
|
|
(defn get-device-model-info []
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] get-device-model-info")
|
2019-09-04 18:59:41 +00:00
|
|
|
;;NOTE: we have to check for status module because of tests
|
|
|
|
(when (status)
|
|
|
|
{:model (.-model (status))
|
|
|
|
:brand (.-brand (status))
|
|
|
|
:build-id (.-buildId (status))
|
|
|
|
:device-id (.-deviceId (status))}))
|
|
|
|
|
|
|
|
(defn extract-group-membership-signatures
|
|
|
|
[signature-pairs callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] extract-group-membership-signatures")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.extractGroupMembershipSignatures (status) signature-pairs callback))
|
|
|
|
|
|
|
|
(defn sign-group-membership [content callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] sign-group-membership")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.signGroupMembership (status) content callback))
|
|
|
|
|
|
|
|
(defn update-mailservers
|
|
|
|
[enodes on-result]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] update-mailservers")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.updateMailservers (status) enodes on-result))
|
|
|
|
|
|
|
|
(defn chaos-mode-update [on on-result]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] chaos-mode-update")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.chaosModeUpdate (status) on on-result))
|
|
|
|
|
|
|
|
(defn get-nodes-from-contract
|
|
|
|
[rpc-endpoint contract-address on-result]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] get-nodes-from-contract")
|
2019-09-04 18:59:41 +00:00
|
|
|
(.getNodesFromContract (status) rpc-endpoint contract-address on-result))
|
|
|
|
|
|
|
|
(defn rooted-device? [callback]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] rooted-device?")
|
2019-09-04 18:59:41 +00:00
|
|
|
(cond
|
|
|
|
;; we assume that iOS is safe by default
|
|
|
|
platform/ios?
|
|
|
|
(callback false)
|
|
|
|
|
|
|
|
;; we assume that Desktop is unsafe by default
|
|
|
|
;; (theoretically, Desktop is always "rooted", by design
|
|
|
|
platform/desktop?
|
|
|
|
(callback true)
|
|
|
|
|
|
|
|
;; we check root on android
|
|
|
|
platform/android?
|
|
|
|
(if (status)
|
|
|
|
(.isDeviceRooted (status) callback)
|
|
|
|
;; if module isn't initialized we return true to avoid degrading security
|
|
|
|
(callback true))
|
|
|
|
|
|
|
|
;; in unknown scenarios we also consider the device rooted to avoid degrading security
|
|
|
|
:else (callback true)))
|
2019-09-12 09:41:25 +00:00
|
|
|
|
|
|
|
(defn generate-gfycat
|
|
|
|
"Generate a 3 words random name based on the user public-key, synchronously"
|
|
|
|
[public-key]
|
|
|
|
{:pre [(utils.db/valid-public-key? public-key)]}
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] generate-gfycat")
|
2019-09-12 09:41:25 +00:00
|
|
|
(.generateAlias (status) public-key))
|
|
|
|
|
2019-11-28 10:00:29 +00:00
|
|
|
(defn generate-gfycat-async
|
|
|
|
"Generate a 3 words random name based on the user public-key, asynchronously"
|
|
|
|
[public-key callback]
|
|
|
|
{:pre [(utils.db/valid-public-key? public-key)]}
|
|
|
|
(.generateAliasAsync (status) public-key callback))
|
|
|
|
|
2019-09-12 09:41:25 +00:00
|
|
|
(defn identicon
|
|
|
|
"Generate a icon based on a string, synchronously"
|
|
|
|
[seed]
|
2019-11-15 08:09:17 +00:00
|
|
|
(log/debug "[native-module] identicon")
|
2019-09-12 09:41:25 +00:00
|
|
|
(.identicon (status) seed))
|
2019-11-28 10:00:29 +00:00
|
|
|
|
|
|
|
(defn identicon-async
|
|
|
|
"Generate a icon based on a string, asynchronously"
|
|
|
|
[seed callback]
|
|
|
|
(.identiconAsync (status) seed callback))
|
|
|
|
|
|
|
|
(defn gfycat-identicon-async
|
|
|
|
"Generate an icon based on a string and 3 words random name asynchronously"
|
|
|
|
[seed callback]
|
|
|
|
(.generateAliasAndIdenticonAsync (status) seed callback))
|