From a8454b0eaaa0a8aa8ed1e0eaeee2c171dd56eb55 Mon Sep 17 00:00:00 2001 From: Matus Zamborsky Date: Thu, 24 Jan 2019 23:25:27 +0100 Subject: [PATCH] add node selection --- package.json | 2 +- public/manifest.json | 2 +- src/api/constants.ts | 41 +++++++++++++++++++++++ src/background/network.ts | 33 +++++++++++++++++- src/popup/pages/settings/settings.tsx | 3 ++ src/popup/pages/settings/settingsView.tsx | 29 ++++++++++++++-- 6 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 src/api/constants.ts diff --git a/package.json b/package.json index 01d9ce4..b99d925 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cyano-wallet", - "version": "0.7.7", + "version": "0.7.8", "private": true, "scripts": { "lint": "tslint -p .", diff --git a/public/manifest.json b/public/manifest.json index 94d4cc4..2d296e4 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -4,7 +4,7 @@ "name": "Cyano wallet", "author": "Matus Zamborsky ", "description": "Cyano wallet - an Ontology wallet", - "version": "0.7.7", + "version": "0.7.8", "browser_action": { "default_title": "Open the wallet" diff --git a/src/api/constants.ts b/src/api/constants.ts new file mode 100644 index 0000000..dc10989 --- /dev/null +++ b/src/api/constants.ts @@ -0,0 +1,41 @@ +export const testOptions: Array<{ text: string; value: string }> = [ + { + text: 'polaris1.ont.io', + value: 'polaris1.ont.io', + }, + { + text: 'polaris2.ont.io', + value: 'polaris2.ont.io', + }, + { + text: 'polaris3.ont.io', + value: 'polaris3.ont.io', + }, + { + text: 'polaris4.ont.io', + value: 'polaris4.ont.io', + }, + { + text: 'polaris5.ont.io', + value: 'polaris5.ont.io', + }, +]; + +export const prodOptions: Array<{ text: string; value: string }> = [ + { + text: 'dappnode1.ont.io', + value: 'dappnode1.ont.io', + }, + { + text: 'dappnode2.ont.io', + value: 'dappnode2.ont.io', + }, + { + text: 'dappnode3.ont.io', + value: 'dappnode3.ont.io', + }, + { + text: 'dappnode4.ont.io', + value: 'dappnode4.ont.io', + }, +]; diff --git a/src/background/network.ts b/src/background/network.ts index 99c1479..a9371f5 100644 --- a/src/background/network.ts +++ b/src/background/network.ts @@ -16,6 +16,7 @@ * along with The Ontology Wallet&ID. If not, see . */ import { CONST, WebsocketClient } from 'ontology-ts-sdk'; +import { prodOptions, testOptions } from 'src/api/constants'; import Actions from '../redux/actions'; import { compareSettings, SettingsState } from '../redux/settings'; import { GlobalStore } from '../redux/state'; @@ -82,6 +83,14 @@ function constructUrl(s: SettingsState) { } function reconnect(s: SettingsState) { + if (client !== undefined) { + try { + client.close(); + } catch (e) { + // ignored + } + } + const url = constructUrl(s); client = new WebsocketClient(url, false, false); } @@ -105,9 +114,19 @@ export function getExplorerAddress(): string | null { export function getNodeAddress(): string | null { if (settings == null) { return null; - } else if (settings.net === 'MAIN') { + } + + const address = settings.address; + + if (settings.net === 'MAIN') { + if (isProdAddress(address)) { + return address; + } return CONST.MAIN_NODE; } else if (settings.net === 'TEST') { + if (isTestAddress(address)) { + return address; + } return CONST.TEST_NODE; } else if (settings.net === 'PRIVATE') { return settings.address; @@ -116,6 +135,18 @@ export function getNodeAddress(): string | null { } } +function isAddress(address: string) { + return address !== undefined && address.trim() !== ''; +} + +function isProdAddress(address: string) { + return isAddress(address) && prodOptions.map((o) => o.value).includes(address); +} + +function isTestAddress(address: string) { + return isAddress(address) && testOptions.map((o) => o.value).includes(address); +} + export function getNeoNodeAddress(): string { return 'http://neonode1.ont.network:10332'; } diff --git a/src/popup/pages/settings/settings.tsx b/src/popup/pages/settings/settings.tsx index e4913f2..8eebfe2 100644 --- a/src/popup/pages/settings/settings.tsx +++ b/src/popup/pages/settings/settings.tsx @@ -23,6 +23,7 @@ import { RouterProps } from 'react-router'; import { bindActionCreators, Dispatch } from 'redux'; import { getAddress } from 'src/api/accountApi'; import { getWallet } from '../../../api/authApi'; +import { prodOptions, testOptions } from '../../../api/constants'; import { getIdentity } from '../../../api/identityApi'; import Actions from '../../../redux/actions'; import { NetValue } from '../../../redux/settings'; @@ -127,6 +128,8 @@ const enhancer = (Component: React.ComponentType) => (props: RouterProps) routerProps.history.push('/settings/trusted'); }, importError: state.importError, + prodOptions, + testOptions, }, (injectedProps) => , ), diff --git a/src/popup/pages/settings/settingsView.tsx b/src/popup/pages/settings/settingsView.tsx index 50fb4ae..30d910a 100644 --- a/src/popup/pages/settings/settingsView.tsx +++ b/src/popup/pages/settings/settingsView.tsx @@ -41,6 +41,8 @@ export interface Props { enableClear: boolean; enableClearIdentity: boolean; importError: boolean; + testOptions: Array<{ text: string; value: string }>; + prodOptions: Array<{ text: string; value: string }>; } const netOptions: Array<{ text: string; value: NetValue }> = [ @@ -84,16 +86,19 @@ export const SettingsView: React.SFC = (props) => ( fluid={true} selection={true} options={netOptions} - onChange={(e, data) => t.input.onChange(data.value)} + onChange={(e, data) => { + formProps.form.change('address', undefined); + return t.input.onChange(data.value); + }} value={t.input.value} error={t.meta.touched && t.meta.invalid} /> )} /> + {get(formProps.values, 'net') === 'PRIVATE' ? ( <> - = (props) => ( /> - ) : null} + ) : ( + + + ( + t.input.onChange(data.value)} + value={t.input.value} + error={t.meta.touched && t.meta.invalid} + /> + )} + /> + + )}