);
+
+const InjectedContext = React.createContext({ injectedAvailable, injectedPromise });
+const InjectedConsumer = InjectedContext.Consumer;
+
+export {
+ InjectedConsumer
+};
+
+export function withInjected (Component: React.ComponentType
): React.ComponentType> {
+ return (props: Subtract) => {
+ return (
+
+ {(injected) => (
+ // @ts-ignore Something here with the props are going wonky
+
+ )}
+
+ );
+ };
+}
diff --git a/packages/ui-api/src/index.ts b/packages/ui-api/src/index.ts
index e0cb450546..42081a22de 100644
--- a/packages/ui-api/src/index.ts
+++ b/packages/ui-api/src/index.ts
@@ -5,6 +5,7 @@
import Api, { api } from './Api';
import ApiContext from './ApiContext';
+export * from './Injected';
export * from './with';
export {
diff --git a/packages/ui-api/src/types.ts b/packages/ui-api/src/types.ts
index 80fd3feadb..6741d6d1c6 100644
--- a/packages/ui-api/src/types.ts
+++ b/packages/ui-api/src/types.ts
@@ -2,6 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
+import { Signer } from '@polkadot/api/types';
import { SubmittableExtrinsicFunction } from '@polkadot/api/promise/types';
import ApiPromise from '@polkadot/api/promise';
@@ -20,9 +21,37 @@ export type ApiProps = {
isApiConnected: boolean,
isApiReady: boolean,
isDevelopment: boolean,
+ isWaitingInjected: boolean,
setApiUrl: (url?: string) => void
};
+export type WindowInjectedInfo = {
+ name: string,
+ version: string
+};
+
+export type WindowInjectedResult = {
+ accounts: {
+ get: () => Promise>
+ },
+ signer: Signer
+};
+
+export type WindowInjected = Window & {
+ injectedWeb3: {
+ [index: string]: WindowInjectedInfo & {
+ enable: (origin: string) => Promise
+ }
+ }
+};
+
+export type ApiInjected = WindowInjectedInfo & WindowInjectedResult;
+
+export type ApiInjectedProps = {
+ injectedAvailable: boolean,
+ injectedPromise: Promise>
+};
+
export type OnChangeCb$Obs = { next: (value?: any) => any };
export type OnChangeCb$Fn = (value?: any) => any;
diff --git a/packages/ui-signer/src/Modal.tsx b/packages/ui-signer/src/Modal.tsx
index 150d085260..0dd1cf89b5 100644
--- a/packages/ui-signer/src/Modal.tsx
+++ b/packages/ui-signer/src/Modal.tsx
@@ -2,10 +2,9 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
-import { Signer as ISigner } from '@polkadot/api/types';
import { SubmittableResult } from '@polkadot/api/SubmittableExtrinsic';
import { SubmittableExtrinsic } from '@polkadot/api/promise/types';
-import { ApiProps } from '@polkadot/ui-api/types';
+import { ApiProps, ApiInjectedProps } from '@polkadot/ui-api/types';
import { I18nProps, BareProps } from '@polkadot/ui-app/types';
import { RpcMethod } from '@polkadot/jsonrpc/types';
import { KeyringPair } from '@polkadot/keyring/types';
@@ -14,8 +13,8 @@ import { QueueTx, QueueTx$MessageSetStatus, QueueTx$Result, QueueTx$Status } fro
import React from 'react';
import { Button, Modal } from '@polkadot/ui-app';
+import { withApi, withInjected, withMulti, withObservable } from '@polkadot/ui-api';
import keyring from '@polkadot/ui-keyring';
-import { withApi, withMulti, withObservable } from '@polkadot/ui-api';
import accountObservable from '@polkadot/ui-keyring/observable/accounts';
import { assert, isFunction } from '@polkadot/util';
import { format } from '@polkadot/util/logger';
@@ -29,7 +28,7 @@ type BaseProps = BareProps & {
queueSetTxStatus: QueueTx$MessageSetStatus
};
-type Props = I18nProps & ApiProps & BaseProps & {
+type Props = I18nProps & ApiProps & ApiInjectedProps & BaseProps & {
allAccounts?: SubjectInfo
};
@@ -40,14 +39,6 @@ type State = {
unlockError?: string | null
};
-type WindowInjected = Window & {
- injectedWeb3: {
- [index: string]: {
- signer: ISigner
- }
- }
-};
-
class Signer extends React.PureComponent {
state: State = {
isSendable: false,
@@ -322,7 +313,7 @@ class Signer extends React.PureComponent {
}
private async makeExtrinsicCall (extrinsic: SubmittableExtrinsic, { id, txFailedCb, txSuccessCb, txUpdateCb }: QueueTx, extrinsicCall: (...params: Array) => any, pair?: KeyringPair): Promise {
- const { api, queueSetTxStatus } = this.props;
+ const { api, injectedPromise, queueSetTxStatus } = this.props;
console.log('makeExtrinsicCall: extrinsic ::', extrinsic.toHex());
@@ -331,13 +322,13 @@ class Signer extends React.PureComponent {
if (pair) {
// set the signer
if (pair.getMeta().isInjected) {
- const injWindow = window as WindowInjected;
const source = pair.getMeta().source;
- const signer = source && injWindow.injectedWeb3 && injWindow.injectedWeb3[source].signer;
+ const sources = await injectedPromise;
+ const injected = source && sources.find(({ name }) => name === source);
- assert(signer, 'Unable to find a signer');
+ assert(injected, 'Unable to find a signer');
- api.setSigner(signer);
+ api.setSigner((injected as any).signer);
params.push(pair.address());
} else {
@@ -403,5 +394,6 @@ export default withMulti(
Signer,
translate,
withApi,
+ withInjected,
withObservable(accountObservable.subject, { propName: 'allAccounts' })
);