Skip to content

Commit

Permalink
feat(api): added isAuthenticated, currentNetwork and localCurrency to…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
Maurice Dalderup authored Oct 18, 2019
1 parent 8ff4780 commit 30fd7b3
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 10 deletions.
17 changes: 17 additions & 0 deletions src/renderer/browser/actions/makeCurrentNetworkActions.js
Original file line number Diff line number Diff line change
@@ -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;
});
}
17 changes: 17 additions & 0 deletions src/renderer/browser/actions/makeLocalCurrencyActions.js
Original file line number Diff line number Diff line change
@@ -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;
});
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import withAuthState from 'auth/hocs/withAuthState';

import IsAuthenticated from './IsAuthenticated';

export default function makeIsAuthenticated() {
return withAuthState()(IsAuthenticated);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}

Expand Down
35 changes: 26 additions & 9 deletions src/renderer/browser/components/RequestsProcessor/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions static/preloads/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 30fd7b3

Please sign in to comment.