Files
pezkuwi-extension/packages/extension-inject/src/bundle.ts
T
pezkuwichain 5d54192e6d chore: update copyright years to 2026 and fix lint issues
- Update all copyright headers from 2025 to 2026
- Fix @polkadot references to @pezkuwi in config files
- Fix eslint.config.js import path

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-09 01:22:46 +03:00

48 lines
2.1 KiB
TypeScript

// Copyright 2019-2026 @pezkuwi/extension-inject authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Injected, InjectedExtension, InjectedWindow, InjectOptions } from './types.js';
import { cyrb53 } from './cyrb53.js';
export { packageInfo } from './packageInfo.js';
// setting for new-style connect (more-secure with no details exposed without
// user acknowledgement, however slightly less-compatible with all dapps, some
// may have not upgraded and don't have access to the latest interfaces)
//
// NOTE: In future versions this will be made the default
const IS_CONNECT_CAPABLE = false;
// 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 || {};
if (IS_CONNECT_CAPABLE) {
// expose our extension on the window object, new-style with connect(origin)
windowInject.injectedWeb3[cyrb53(`${name}/${version}`)] = {
connect: (origin: string): Promise<InjectedExtension> =>
enable(origin).then(({ accounts, metadata, provider, signer }) => ({
accounts, metadata, name, provider, signer, version
})),
enable: (): Promise<Injected> =>
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: string): Promise<Injected> =>
enable(origin),
version
};
}
}