From bc0c9bfe43307d251b1055c846ce1e186c224c98 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 14 Feb 2019 17:23:55 +0100 Subject: [PATCH] Inject keyring only on dev chains (#717) * Inject keyring only on dev chains * Remove useless deconstruct --- packages/app-js/src/Editor.tsx | 9 ++++++--- packages/app-js/src/index.tsx | 12 +++++++----- packages/app-js/src/snippets/wrapping.ts | 8 ++++++-- packages/ui-api/src/Api.tsx | 8 +++++--- packages/ui-api/src/types.ts | 1 + 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/app-js/src/Editor.tsx b/packages/app-js/src/Editor.tsx index 59759bd21c..3da090e4a0 100644 --- a/packages/app-js/src/Editor.tsx +++ b/packages/app-js/src/Editor.tsx @@ -7,10 +7,11 @@ import { BareProps } from '@polkadot/ui-app/types'; import React from 'react'; import CodeFlask from 'codeflask'; -import WRAPPING from './snippets/wrapping'; +import makeWrapper from './snippets/wrapping'; type Props = BareProps & { children?: React.ReactNode, + isDevelopment: boolean, code: string, onEdit: (code: string) => void, snippet: string @@ -30,6 +31,7 @@ export default class Editor extends React.PureComponent { constructor (props: Props) { super(props); + this.id = `flask-${Date.now()}`; } @@ -53,11 +55,12 @@ export default class Editor extends React.PureComponent { } componentDidUpdate () { - const { code, onEdit, snippet } = this.props; + const { code, isDevelopment, onEdit, snippet } = this.props; if (snippet !== this.state.snippet) { onEdit(code); - this.editor.updateCode(`${WRAPPING}${code}`); + + this.editor.updateCode(`${makeWrapper(isDevelopment)}${code}`); this.setState({ snippet }); } } diff --git a/packages/app-js/src/index.tsx b/packages/app-js/src/index.tsx index 91d5127b91..ac1762e093 100644 --- a/packages/app-js/src/index.tsx +++ b/packages/app-js/src/index.tsx @@ -30,7 +30,7 @@ type Injected = { }, global: null, hashing: typeof hashing, - keyring: KeyringInstance, + keyring: KeyringInstance | null, util: typeof util, window: null }; @@ -60,8 +60,8 @@ class App extends React.PureComponent { } render () { + const { isDevelopment, t } = this.props; const { code, isRunning, logs, snippet } = this.state; - const { t } = this.props; const options = snippets.map(({ code, ...options }) => ({ ...options })); return ( @@ -79,6 +79,7 @@ class App extends React.PureComponent {
@@ -113,9 +114,8 @@ class App extends React.PureComponent { } private runJs = async (): Promise => { - const { api } = this.props; + const { api, isDevelopment } = this.props; const { code } = this.state; - const { keyring } = uiKeyring; this.stopJs(); this.clearConsole(); @@ -128,7 +128,9 @@ class App extends React.PureComponent { }, global: null, hashing, - keyring, + keyring: isDevelopment + ? uiKeyring.keyring + : null, util, window: null }; diff --git a/packages/app-js/src/snippets/wrapping.ts b/packages/app-js/src/snippets/wrapping.ts index 85f8fc3a4f..52dff861fd 100644 --- a/packages/app-js/src/snippets/wrapping.ts +++ b/packages/app-js/src/snippets/wrapping.ts @@ -2,9 +2,13 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -export default `// All code is wrapped within an async closure, allowing access to api, hashing, keyring, util. -// (async ({ api, hashing, keyring, util }) => { +export default function makeWrapper (isDevelopment: boolean): string { + const args = `api, hashing, ${isDevelopment ? 'keyring, ' : ''}util`; + + return `// All code is wrapped within an async closure, allowing access to ${args}. +// (async ({ ${args} }) => { // ... any user code is executed here ... // })(); `; +} diff --git a/packages/ui-api/src/Api.tsx b/packages/ui-api/src/Api.tsx index 4d9d27c431..77beb17e5e 100644 --- a/packages/ui-api/src/Api.tsx +++ b/packages/ui-api/src/Api.tsx @@ -95,6 +95,7 @@ export default class ApiWrapper extends React.PureComponent { tokenSymbol: undefined }; const tokenSymbol = properties.get('tokenSymbol') || found.tokenSymbol; + const isDevelopment = isTestChain(chain || ''); console.log('api: found chain', chain, [...properties.entries()]); @@ -105,10 +106,10 @@ export default class ApiWrapper extends React.PureComponent { // setup keyring (loadAll) only after prefix has been set keyring.setAddressPrefix(properties.get('networkId') || found.networkId as any || 42); - keyring.setDevMode(isTestChain(chain || '')); + keyring.setDevMode(isDevelopment); keyring.loadAll(); - this.setState({ chain }); + this.setState({ chain, isDevelopment }); } private subscribeIsConnected = (api: ApiPromise) => { @@ -134,7 +135,7 @@ export default class ApiWrapper extends React.PureComponent { } render () { - const { api, apiDefaultTx, chain, isApiConnected, isApiReady, setApiUrl } = this.state; + const { api, apiDefaultTx, chain, isApiConnected, isApiReady, isDevelopment, setApiUrl } = this.state; return ( { apiDefaultTx, isApiConnected, isApiReady: isApiReady && !!chain, + isDevelopment, setApiUrl }} > diff --git a/packages/ui-api/src/types.ts b/packages/ui-api/src/types.ts index fbc63534ee..3c5e753702 100644 --- a/packages/ui-api/src/types.ts +++ b/packages/ui-api/src/types.ts @@ -19,6 +19,7 @@ export type ApiProps = { apiDefaultTx: SubmittableExtrinsicFunction, isApiConnected: boolean, isApiReady: boolean, + isDevelopment: boolean, setApiUrl: (url?: string) => void };