diff --git a/src/actions.js b/src/actions.js index 51660effa..f188dd893 100644 --- a/src/actions.js +++ b/src/actions.js @@ -913,8 +913,8 @@ export const networkSettingsUpdateState = (customNetwork) => ({ * network: string, * nodeUrl: string, * explorerUrl: string, - * txMiningServiceUrl: string, * explorerServiceUrl: string, + * txMiningServiceUrl: string, * walletServiceUrl?: string * walletServiceWsUrl?: string * }} customNetwork Settings to persist diff --git a/src/reducers/reducer.js b/src/reducers/reducer.js index 0c121c622..98f2d1700 100644 --- a/src/reducers/reducer.js +++ b/src/reducers/reducer.js @@ -227,6 +227,18 @@ const initialState = { featureToggles: { ...FEATURE_TOGGLE_DEFAULTS, }, + /** + * @param {{ + * stage: string; + * network: string; + * nodeUrl: string; + * explorerUrl: string; + * explorerServiceUrl: string; + * txMiningServiceUrl: string; + * walletServiceUrl: string; + * walletServiceWsUrl: string; + * }} + */ networkSettings: PRE_SETTINGS_MAINNET, networkSettingsInvalid: {}, networkSettingsStatus: NETWORKSETTINGS_STATUS.READY, diff --git a/src/sagas/networkSettings.js b/src/sagas/networkSettings.js index eb445b835..f1af235f1 100644 --- a/src/sagas/networkSettings.js +++ b/src/sagas/networkSettings.js @@ -6,7 +6,6 @@ import { networkSettingsPersistStore, networkSettingsUpdateInvalid, networkSettingsUpdateFailure, - networkSettingsUpdateState, networkSettingsUpdateSuccess, networkSettingsUpdateWaiting, types, @@ -36,11 +35,6 @@ import { isWalletServiceEnabled } from './wallet'; * Initialize the network settings saga when the wallet starts successfully. */ export function* initNetworkSettings() { - const customNetwork = STORE.getItem(networkSettingsKeyMap.networkSettings); - if (customNetwork) { - yield put(networkSettingsUpdateState(customNetwork)); - } - const status = yield select((state) => state.networkSettingsStatus); if (status === NETWORKSETTINGS_STATUS.WAITING) { // This branch completes the network update by delivering @@ -60,13 +54,14 @@ export function* initNetworkSettings() { * * @param {{ * payload: { - * stage: string, - * network: string, - * nodeUrl: string, - * explorerUrl: string, - * explorerServiceUrl: string, - * walletServiceUrl?: string - * walletServiceWsUrl?: string + * stage: string; + * network: string; + * nodeUrl: string; + * explorerUrl: string; + * explorerServiceUrl: string; + * txMiningServiceUrl: string; + * walletServiceUrl?: string; + * walletServiceWsUrl?: string; * } * }} action contains the payload with the new * network settings requested by the user to be processd. @@ -148,15 +143,15 @@ export function* updateNetworkSettings(action) { txMiningServiceUrl: networkSettings.txMiningServiceUrl, }; + config.setTxMiningUrl(txMiningServiceUrl); config.setExplorerServiceBaseUrl(explorerServiceUrl); config.setServerUrl(nodeUrl); - config.setTxMiningUrl(txMiningServiceUrl); // - walletServiceUrl has precedence // - nodeUrl as fallback let potentialNetwork; let network; - if (walletServiceUrl && useWalletService) { + if (useWalletService && !isEmpty(walletServiceUrl)) { config.setWalletServiceBaseUrl(walletServiceUrl); config.setWalletServiceBaseWsUrl(walletServiceWsUrl); @@ -222,6 +217,7 @@ export function* updateNetworkSettings(action) { nodeUrl, explorerUrl, explorerServiceUrl, + txMiningServiceUrl, walletServiceUrl, walletServiceWsUrl, }; diff --git a/src/sagas/wallet.js b/src/sagas/wallet.js index 25c25f8c9..c275e7382 100644 --- a/src/sagas/wallet.js +++ b/src/sagas/wallet.js @@ -31,11 +31,12 @@ import { } from 'redux-saga/effects'; import { eventChannel } from 'redux-saga'; import { getUniqueId } from 'react-native-device-info'; -import { get } from 'lodash'; +import { get, isEmpty } from 'lodash'; import { DEFAULT_TOKEN, WALLET_SERVICE_FEATURE_TOGGLE, PUSH_NOTIFICATION_FEATURE_TOGGLE, + networkSettingsKeyMap, } from '../constants'; import { STORE } from '../store'; import { @@ -63,6 +64,7 @@ import { resetWalletSuccess, setTokens, onExceptionCaptured, + networkSettingsUpdateState, } from '../actions'; import { fetchTokenData } from './tokens'; import { @@ -151,10 +153,29 @@ export function* startWallet(action) { dispatch = _dispatch; }); - const networkSettings = yield select(getNetworkSettings); + // Network settings either from store or redux state + let networkSettings; + // Custom network settings are persisted in the app storage + const customNetwork = STORE.getItem(networkSettingsKeyMap.networkSettings); + if (customNetwork) { + networkSettings = customNetwork; + // On custom network settings one may use a different + // URL for the services from the ones registered by default + // for mainnet and testnet in the lib, and the wallet must + // behave consistently to the URLs set + config.setExplorerServiceBaseUrl(networkSettings.explorerServiceUrl); + config.setServerUrl(networkSettings.nodeUrl); + config.setTxMiningUrl(networkSettings.txMiningServiceUrl); + + // If the wallet is initialized from quit state it must + // update the network settings on redux state + yield put(networkSettingsUpdateState(networkSettings)); + } else { + networkSettings = yield select(getNetworkSettings); + } let wallet; - if (useWalletService) { + if (useWalletService && !isEmpty(networkSettings.walletServiceUrl)) { const network = new Network(networkSettings.network); // Set urls for wallet service diff --git a/src/screens/Settings.js b/src/screens/Settings.js index 3fd30fc31..150364eda 100644 --- a/src/screens/Settings.js +++ b/src/screens/Settings.js @@ -14,6 +14,7 @@ import { Text, View, } from 'react-native'; +import { isEmpty } from 'lodash'; import OfflineBar from '../components/OfflineBar'; import Logo from '../components/Logo'; import { HathorList, ListItem, ListMenu } from '../components/HathorList'; @@ -32,9 +33,15 @@ import { isPushNotificationAvailableForUser } from '../utils'; * server {str} URL of server this wallet is connected to */ const mapStateToProps = (state) => { - const server = state.useWalletService - ? state.wallet.storage.config.getWalletServiceBaseUrl() - : state.wallet.storage.config.getServerUrl(); + let server; + const { walletServiceUrl } = state.networkSettings; + if (state.useWalletService && !isEmpty(walletServiceUrl)) { + server = walletServiceUrl; + } + + if (!server) { + server = state.networkSettings.nodeUrl; + } return { selectedToken: state.selectedToken,