From 768a065a154e5fba7d6792ee11a9b4d925ac7a30 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 27 Nov 2018 15:27:11 +0300 Subject: [PATCH] custom RPC validation: JSON RPC request instead of HTTP request --- old-ui/app/config.js | 77 +++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/old-ui/app/config.js b/old-ui/app/config.js index 59efe1d4d..58cc9355d 100644 --- a/old-ui/app/config.js +++ b/old-ui/app/config.js @@ -3,9 +3,8 @@ const Component = require('react').Component const h = require('react-hyperscript') const connect = require('react-redux').connect const actions = require('../../ui/app/actions') -const url = require('url') -const http = require('http') -const https = require('https') +const LoadingIndicator = require('./components/loading') +const Web3 = require('web3') const infuraCurrencies = require('./infura-conversion.json').objects.sort((a, b) => { return a.quote.name.toLocaleLowerCase().localeCompare(b.quote.name.toLocaleLowerCase()) }) @@ -25,13 +24,16 @@ function mapStateToProps (state) { inherits(ConfigScreen, Component) function ConfigScreen () { + this.state = { + loading: false, + } Component.call(this) } ConfigScreen.prototype.render = function () { - var state = this.props - var metamaskState = state.metamask - var warning = state.warning + const state = this.props + const metamaskState = state.metamask + const warning = state.warning return ( h('.flex-column.flex-grow', { @@ -41,6 +43,10 @@ ConfigScreen.prototype.render = function () { }, }, [ + h(LoadingIndicator, { + isLoading: this.state.loading, + }), + h(Modal, {}, []), // subtitle and nav @@ -92,11 +98,11 @@ ConfigScreen.prototype.render = function () { border: '1px solid #e2e2e2', padding: '10px', }, - onKeyPress (event) { + onKeyPress: (event) => { if (event.key === 'Enter') { - var element = event.target - var newRpc = element.value - rpcValidation(newRpc, state) + const element = event.target + const newRpc = element.value + this.rpcValidation(newRpc, state) } }, }), @@ -105,11 +111,11 @@ ConfigScreen.prototype.render = function () { alignSelf: 'center', marginTop: '20px', }, - onClick (event) { + onClick: (event) => { event.preventDefault() - var element = document.querySelector('input#new_rpc') - var newRpc = element.value - rpcValidation(newRpc, state) + const element = document.querySelector('input#new_rpc') + const newRpc = element.value + this.rpcValidation(newRpc, state) }, }, 'Save'), ]), @@ -234,41 +240,46 @@ ConfigScreen.prototype.componentWillUnmount = function () { this.props.dispatch(actions.displayWarning('')) } -function rpcValidation (newRpc, state) { +ConfigScreen.prototype.rpcValidation = (newRpc, state) => { + this.setState({ + loading: true, + }) if (validUrl.isWebUri(newRpc)) { - const rpc = url.parse(newRpc) - const protocolName = rpc.protocol.replace(/:/g, '') - const protocol = protocolName === 'https' ? https : http - const options = {method: 'GET', host: rpc.hostname, port: rpc.port, path: rpc.pathname} - const req = protocol.request(options) - .on('response', () => { - state.dispatch(actions.setRpcTarget(newRpc)) + const web3 = new Web3(new Web3.providers.HttpProvider(newRpc)) + web3.eth.getBlockNumber((err, res) => { + if (err) { + state.dispatch(actions.displayWarning('Invalid RPC endpoint')) + } else { + state.dispatch(actions.setRpcTarget(newRpc)) + } + this.setState({ + loading: false, + }) }) - .on('error', () => { - state.dispatch(actions.displayWarning('Invalid RPC endpoint')) - }) - req.end() } else { - var appendedRpc = `http://${newRpc}` + const appendedRpc = `http://${newRpc}` if (validUrl.isWebUri(appendedRpc)) { state.dispatch(actions.displayWarning('URIs require the appropriate HTTP/HTTPS prefix.')) } else { state.dispatch(actions.displayWarning('Invalid RPC URI')) } + this.setState({ + loading: false, + }) } } function currentConversionInformation (metamaskState, state) { - var currentCurrency = metamaskState.currentCurrency - var conversionDate = metamaskState.conversionDate + const currentCurrency = metamaskState.currentCurrency + const conversionDate = metamaskState.conversionDate return h('div', [ h('span', {style: { fontWeight: 'bold', paddingRight: '10px'}}, 'Current Conversion'), h('span', {style: { fontWeight: 'bold', paddingRight: '10px', fontSize: '13px'}}, `Updated ${Date(conversionDate)}`), h('select#currentCurrency', { onChange (event) { event.preventDefault() - var element = document.getElementById('currentCurrency') - var newCurrency = element.value + const element = document.getElementById('currentCurrency') + const newCurrency = element.value state.dispatch(actions.setCurrentCurrency(newCurrency)) }, defaultValue: currentCurrency, @@ -280,8 +291,8 @@ function currentConversionInformation (metamaskState, state) { } function currentProviderDisplay (metamaskState, state) { - var provider = metamaskState.provider - var title, value + const provider = metamaskState.provider + let title, value switch (provider.type) {