ghost-keychain/packages/extension-inject/src/index.ts
2021-01-03 11:33:53 +01:00

23 lines
1001 B
TypeScript

// Copyright 2019-2021 @polkadot/extension-inject authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Injected, InjectedWindow, InjectOptions } from './types';
// It is recommended to always use the function below to shield the extension and dapp from
// any future changes. The exposed interface will manage access between the 2 environments,
// be it via window (current), postMessage (under consideration) or any other mechanism
export function injectExtension (enable: (origin: string) => Promise<Injected>, { name, version }: InjectOptions): void {
// small helper with the typescript types, just cast window
const windowInject = window as Window & InjectedWindow;
// don't clobber the existing object, we will add it (or create as needed)
windowInject.injectedWeb3 = windowInject.injectedWeb3 || {};
// add our enable function
windowInject.injectedWeb3[name] = {
enable: (origin: string): Promise<Injected> =>
enable(origin),
version
};
}