From 30fd7b37ab2ed4a1eb7d9de66e30cf23c0a729eb Mon Sep 17 00:00:00 2001 From: Maurice Dalderup Date: Fri, 18 Oct 2019 17:33:26 +0200 Subject: [PATCH] feat(api): added isAuthenticated, currentNetwork and localCurrency to the nOS API (#1187) * feat(api): added current network to the nOS API * feat(api): add getLocalCurrency to nOS API * chore(api): added isAuthenticated to api and added unprotected channels --- .../actions/makeCurrentNetworkActions.js | 17 +++++++++ .../actions/makeLocalCurrencyActions.js | 17 +++++++++ .../CurrentNetwork/CurrentNetwork.js | 17 +++++++++ .../RequestsProcessor/CurrentNetwork/index.js | 20 +++++++++++ .../IsAuthenticated/IsAuthenticated.js | 22 ++++++++++++ .../IsAuthenticated/index.js | 7 ++++ .../LocalCurrency/LocalCurrency.js | 17 +++++++++ .../RequestsProcessor/LocalCurrency/index.js | 20 +++++++++++ .../RequestsProcessor/RequestProcessor.js | 11 +++++- .../components/RequestsProcessor/mappings.js | 35 ++++++++++++++----- static/preloads/api.js | 3 ++ 11 files changed, 176 insertions(+), 10 deletions(-) create mode 100644 src/renderer/browser/actions/makeCurrentNetworkActions.js create mode 100644 src/renderer/browser/actions/makeLocalCurrencyActions.js create mode 100644 src/renderer/browser/components/RequestsProcessor/CurrentNetwork/CurrentNetwork.js create mode 100644 src/renderer/browser/components/RequestsProcessor/CurrentNetwork/index.js create mode 100644 src/renderer/browser/components/RequestsProcessor/IsAuthenticated/IsAuthenticated.js create mode 100644 src/renderer/browser/components/RequestsProcessor/IsAuthenticated/index.js create mode 100644 src/renderer/browser/components/RequestsProcessor/LocalCurrency/LocalCurrency.js create mode 100644 src/renderer/browser/components/RequestsProcessor/LocalCurrency/index.js diff --git a/src/renderer/browser/actions/makeCurrentNetworkActions.js b/src/renderer/browser/actions/makeCurrentNetworkActions.js new file mode 100644 index 000000000..b98df7731 --- /dev/null +++ b/src/renderer/browser/actions/makeCurrentNetworkActions.js @@ -0,0 +1,17 @@ +import { createActions } from 'spunky'; + +import { DEFAULT_NET } from 'values/networks'; +import { getStorage } from 'shared/lib/storage'; + +import generateDAppActionId from './generateDAppActionId'; + +export const ID = 'currentNetwork'; + +export default function makeCurrentNetworkActions(sessionId, requestId) { + const id = generateDAppActionId(sessionId, `${ID}-${requestId}`); + + return createActions(id, () => async () => { + const currentNetwork = await getStorage(ID); + return typeof currentNetwork === 'string' ? currentNetwork : DEFAULT_NET; + }); +} diff --git a/src/renderer/browser/actions/makeLocalCurrencyActions.js b/src/renderer/browser/actions/makeLocalCurrencyActions.js new file mode 100644 index 000000000..e3fcb2464 --- /dev/null +++ b/src/renderer/browser/actions/makeLocalCurrencyActions.js @@ -0,0 +1,17 @@ +import { createActions } from 'spunky'; + +import { getStorage } from 'shared/lib/storage'; +import { DEFAULT_CURRENCY } from 'shared/values/currencies'; + +import generateDAppActionId from './generateDAppActionId'; + +export const ID = 'currency'; + +export default function makeCurrentNetworkActions(sessionId, requestId) { + const id = generateDAppActionId(sessionId, `${ID}-${requestId}`); + + return createActions(id, () => async () => { + const currency = await getStorage(ID); + return typeof currency === 'string' ? currency : DEFAULT_CURRENCY; + }); +} diff --git a/src/renderer/browser/components/RequestsProcessor/CurrentNetwork/CurrentNetwork.js b/src/renderer/browser/components/RequestsProcessor/CurrentNetwork/CurrentNetwork.js new file mode 100644 index 000000000..4009dfd9b --- /dev/null +++ b/src/renderer/browser/components/RequestsProcessor/CurrentNetwork/CurrentNetwork.js @@ -0,0 +1,17 @@ +import React from 'react'; +import { func, any, objectOf } from 'prop-types'; + +export default class CurrentNetwork extends React.PureComponent { + static propTypes = { + data: objectOf(any).isRequired, + onResolve: func.isRequired + }; + + componentDidMount() { + this.props.onResolve(this.props.data); + } + + render() { + return null; + } +} diff --git a/src/renderer/browser/components/RequestsProcessor/CurrentNetwork/index.js b/src/renderer/browser/components/RequestsProcessor/CurrentNetwork/index.js new file mode 100644 index 000000000..ebc484fd1 --- /dev/null +++ b/src/renderer/browser/components/RequestsProcessor/CurrentNetwork/index.js @@ -0,0 +1,20 @@ +import { compose } from 'recompose'; +import { withData } from 'spunky'; + +import withInitialCall from 'shared/hocs/withInitialCall'; + +import withClean from '../../../hocs/withClean'; +import withNullLoader from '../../../hocs/withNullLoader'; + +import CurrentNetwork from './CurrentNetwork'; + +const mapCurrentNetworkActionsDataToProps = (data) => ({ data }); + +export default function makeGetCurrentNetwork(currentNetworkActions) { + return compose( + withClean(currentNetworkActions), + withInitialCall(currentNetworkActions), + withNullLoader(currentNetworkActions), + withData(currentNetworkActions, mapCurrentNetworkActionsDataToProps) + )(CurrentNetwork); +} diff --git a/src/renderer/browser/components/RequestsProcessor/IsAuthenticated/IsAuthenticated.js b/src/renderer/browser/components/RequestsProcessor/IsAuthenticated/IsAuthenticated.js new file mode 100644 index 000000000..0e055265c --- /dev/null +++ b/src/renderer/browser/components/RequestsProcessor/IsAuthenticated/IsAuthenticated.js @@ -0,0 +1,22 @@ +import React from 'react'; +import { func, bool } from 'prop-types'; + +export default class IsAuthenticated extends React.PureComponent { + static propTypes = { + onResolve: func.isRequired, + authenticated: bool + }; + + static defaultProps = { + authenticated: false + }; + + componentDidMount() { + const { authenticated, onResolve } = this.props; + onResolve(authenticated); + } + + render() { + return null; + } +} diff --git a/src/renderer/browser/components/RequestsProcessor/IsAuthenticated/index.js b/src/renderer/browser/components/RequestsProcessor/IsAuthenticated/index.js new file mode 100644 index 000000000..494d6606f --- /dev/null +++ b/src/renderer/browser/components/RequestsProcessor/IsAuthenticated/index.js @@ -0,0 +1,7 @@ +import withAuthState from 'auth/hocs/withAuthState'; + +import IsAuthenticated from './IsAuthenticated'; + +export default function makeIsAuthenticated() { + return withAuthState()(IsAuthenticated); +} diff --git a/src/renderer/browser/components/RequestsProcessor/LocalCurrency/LocalCurrency.js b/src/renderer/browser/components/RequestsProcessor/LocalCurrency/LocalCurrency.js new file mode 100644 index 000000000..4009dfd9b --- /dev/null +++ b/src/renderer/browser/components/RequestsProcessor/LocalCurrency/LocalCurrency.js @@ -0,0 +1,17 @@ +import React from 'react'; +import { func, any, objectOf } from 'prop-types'; + +export default class CurrentNetwork extends React.PureComponent { + static propTypes = { + data: objectOf(any).isRequired, + onResolve: func.isRequired + }; + + componentDidMount() { + this.props.onResolve(this.props.data); + } + + render() { + return null; + } +} diff --git a/src/renderer/browser/components/RequestsProcessor/LocalCurrency/index.js b/src/renderer/browser/components/RequestsProcessor/LocalCurrency/index.js new file mode 100644 index 000000000..2a43b6fb8 --- /dev/null +++ b/src/renderer/browser/components/RequestsProcessor/LocalCurrency/index.js @@ -0,0 +1,20 @@ +import { compose } from 'recompose'; +import { withData } from 'spunky'; + +import withInitialCall from 'shared/hocs/withInitialCall'; + +import withClean from '../../../hocs/withClean'; +import withNullLoader from '../../../hocs/withNullLoader'; + +import LocalCurrency from './LocalCurrency'; + +const mapLocalCurrencyActionsDataToProps = (data) => ({ data }); + +export default function makeGetLocalCurrency(localCurrencyActions) { + return compose( + withClean(localCurrencyActions), + withInitialCall(localCurrencyActions), + withNullLoader(localCurrencyActions), + withData(localCurrencyActions, mapLocalCurrencyActionsDataToProps) + )(LocalCurrency); +} diff --git a/src/renderer/browser/components/RequestsProcessor/RequestProcessor.js b/src/renderer/browser/components/RequestsProcessor/RequestProcessor.js index 9b1c857a4..18ae0936f 100644 --- a/src/renderer/browser/components/RequestsProcessor/RequestProcessor.js +++ b/src/renderer/browser/components/RequestsProcessor/RequestProcessor.js @@ -6,6 +6,14 @@ import { getComponent, getActions } from './mappings'; import Unauthenticated from './Unauthenticated'; import requestShape from '../../shapes/requestShape'; +const unprotectedChannels = [ + 'isAuthenticated', + 'getStorage', + 'getLastBlock', + 'getCurrentNetwork', + 'getLocalCurrency' +]; + export default class RequestProcessor extends React.PureComponent { static propTypes = { sessionId: string.isRequired, @@ -56,7 +64,8 @@ export default class RequestProcessor extends React.PureComponent { }; getComponent = ({ sessionId, request }) => { - if (!this.props.authenticated) { + const isUnprotectedChannel = unprotectedChannels.includes(request.channel); + if (!this.props.authenticated && !isUnprotectedChannel) { return Unauthenticated(); } diff --git a/src/renderer/browser/components/RequestsProcessor/mappings.js b/src/renderer/browser/components/RequestsProcessor/mappings.js index d24e39d05..0ed6ee5ec 100644 --- a/src/renderer/browser/components/RequestsProcessor/mappings.js +++ b/src/renderer/browser/components/RequestsProcessor/mappings.js @@ -9,6 +9,9 @@ import ClaimGas from './ClaimGas'; import GetPublicKey from './GetPublicKey'; import Encrypt from './Encrypt'; import Decrypt from './Decrypt'; +import CurrentNetwork from './CurrentNetwork'; +import LocalCurrency from './LocalCurrency'; +import IsAuthenticated from './IsAuthenticated'; import makeInvokeActions from '../../actions/makeInvokeActions'; import makeTestInvokeActions from '../../actions/makeTestInvokeActions'; import makeStorageActions from '../../actions/makeStorageActions'; @@ -18,31 +21,45 @@ import makeBalancesActions from '../../actions/makeBalancesActions'; import makePublicKeyActions from '../../actions/makePublicKeyActions'; import makeEncryptActions from '../../actions/makeEncryptActions'; import makeDecryptActions from '../../actions/makeDecryptActions'; +import makeCurrentNetworkActions from '../../actions/makeCurrentNetworkActions'; +import makeLocalCurrencyActions from '../../actions/makeLocalCurrencyActions'; const COMPONENT_MAP = { + isAuthenticated: IsAuthenticated, + // Getters getAddress: GetAddress, getBalance: GetBalance, getStorage: GetStorage, - testInvoke: TestInvoke, getPublicKey: GetPublicKey, + getCurrentNetwork: CurrentNetwork, + getLocalCurrency: LocalCurrency, + // Encryption encrypt: Encrypt, decrypt: Decrypt, - getLastBlock: GetLastBlock, + // Invocations invoke: Invoke, + testInvoke: TestInvoke, send: Send, - claimGas: ClaimGas + claimGas: ClaimGas, + // Events + getLastBlock: GetLastBlock }; const ACTIONS_MAP = { - getStorage: makeStorageActions, + // Getters getBalance: makeBalancesActions, - testInvoke: makeTestInvokeActions, - invoke: [makeInvokeActions, makeBalancesActions], - send: makeSendActions, - claimGas: makeClaimActions, + getStorage: makeStorageActions, getPublicKey: makePublicKeyActions, + getCurrentNetwork: makeCurrentNetworkActions, + getLocalCurrency: makeLocalCurrencyActions, + // Encryption encrypt: makeEncryptActions, - decrypt: makeDecryptActions + decrypt: makeDecryptActions, + // Invocations + invoke: [makeInvokeActions, makeBalancesActions], + testInvoke: makeTestInvokeActions, + send: makeSendActions, + claimGas: makeClaimActions }; const makeNullActions = () => null; diff --git a/static/preloads/api.js b/static/preloads/api.js index e91e64ab5..0faf0e1a8 100644 --- a/static/preloads/api.js +++ b/static/preloads/api.js @@ -29,9 +29,12 @@ const api = { getStorage: createDelegate('getStorage'), getLastBlock: createDelegate('getLastBlock'), getPublicKey: createDelegate('getPublicKey'), + getCurrentNetwork: createDelegate('getCurrentNetwork'), + getLocalCurrency: createDelegate('getLocalCurrency'), testInvoke: createDelegate('testInvoke'), encrypt: createDelegate('encrypt'), decrypt: createDelegate('decrypt'), + isAuthenticated: createDelegate('isAuthenticated'), // Permissions required invoke: createDelegate('invoke'),