@polkadot/extension-inject
Version:
A generic injector (usable to any extension), that populates the base exposed interfaces to be used by dapps.
26 lines (25 loc) • 1.26 kB
JavaScript
import { cyrb53 } from './cyrb53.js';
export { packageInfo } from './packageInfo.js';
const IS_CONNECT_CAPABLE = false;
export function injectExtension(enable, { name, version }) {
// small helper with the typescript types, just cast window
const windowInject = window;
// don't clobber the existing object, we will add it (or create as needed)
windowInject.injectedWeb3 = windowInject.injectedWeb3 || {};
if (IS_CONNECT_CAPABLE) {
// expose our extension on the window object, new-style with connect(origin)
windowInject.injectedWeb3[cyrb53(`${name}/${version}`)] = {
connect: (origin) => enable(origin).then(({ accounts, metadata, provider, signer }) => ({
accounts, metadata, name, provider, signer, version
})),
enable: () => Promise.reject(new Error('This extension does not have support for enable(...), rather is only supports the new connect(...) variant (no extension name/version metadata without specific user-approval)'))
};
}
else {
// expose our extension on the window object, old-style with enable(origin)
windowInject.injectedWeb3[name] = {
enable: (origin) => enable(origin),
version
};
}
}