connect(<dappName>) new-style support (#1175)

* connect(<dappName>) new-style support

* Add connect interface

* Seed required

* Support both old & new codepaths in inject

* Explicit enable(...) reject on new-style

* Cleanups

* Condense enable(...) error

* Adjust enable ordering

* Adjust web3IsInjected
This commit is contained in:
Jaco 2022-12-17 14:38:32 +02:00 committed by GitHub
parent 230bd01f1c
commit 45a02f7268
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 132 additions and 62 deletions

View File

@ -38,7 +38,7 @@ export default class Signer implements SignerInterface {
} }
// NOTE We don't listen to updates at all, if we do we can interpret the // NOTE We don't listen to updates at all, if we do we can interpret the
// resuklt as provided by the API here // result as provided by the API here
// public update (id: number, status: Hash | SubmittableResult): void { // public update (id: number, status: Hash | SubmittableResult): void {
// // ignore // // ignore
// } // }

View File

@ -1,9 +1,9 @@
// Copyright 2019-2022 @polkadot/extension-dapp authors & contributors // Copyright 2019-2022 @polkadot/extension-dapp authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import type { Injected, InjectedAccount, InjectedAccountWithMeta, InjectedExtension, InjectedExtensionInfo, InjectedProviderWithMeta, InjectedWindow, ProviderList, Unsubcall, Web3AccountsOptions } from '@polkadot/extension-inject/types'; import type { InjectedAccount, InjectedAccountWithMeta, InjectedExtension, InjectedProviderWithMeta, InjectedWindow, ProviderList, Unsubcall, Web3AccountsOptions } from '@polkadot/extension-inject/types';
import { u8aEq } from '@polkadot/util'; import { objectSpread, u8aEq } from '@polkadot/util';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto'; import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
import { documentReadyPromise } from './util'; import { documentReadyPromise } from './util';
@ -20,7 +20,10 @@ win.injectedWeb3 = win.injectedWeb3 || {};
// true when anything has been injected and is available // true when anything has been injected and is available
function web3IsInjected (): boolean { function web3IsInjected (): boolean {
return Object.keys(win.injectedWeb3).length !== 0; return Object
.values(win.injectedWeb3)
.filter(({ connect, enable }) => !!(connect || enable))
.length !== 0;
} }
// helper to throw a consistent error when not enabled // helper to throw a consistent error when not enabled
@ -47,18 +50,31 @@ let web3EnablePromise: Promise<InjectedExtension[]> | null = null;
export { isWeb3Injected, web3EnablePromise }; export { isWeb3Injected, web3EnablePromise };
function getWindowExtensions (originName: string): Promise<[InjectedExtensionInfo, Injected | void][]> { function getWindowExtensions (originName: string): Promise<InjectedExtension[]> {
return Promise.all( return Promise
Object.entries(win.injectedWeb3).map( .all(
([name, { enable, version }]): Promise<[InjectedExtensionInfo, Injected | void]> => Object
Promise.all([ .entries(win.injectedWeb3)
Promise.resolve({ name, version }), .map(([nameOrHash, { connect, enable, version }]): Promise<(InjectedExtension | void)> =>
enable(originName).catch((error: Error): void => { Promise
console.error(`Error initializing ${name}: ${error.message}`); .resolve()
}) .then(() =>
]) connect
// new style, returning all info
? connect(originName)
: enable
// previous interface, leakages on name/version
? enable(originName).then((e) =>
objectSpread<InjectedExtension>({ name: nameOrHash, version: version || 'unknown' }, e)
)
: Promise.reject(new Error('No connect(..) or enable(...) hook found'))
)
.catch(({ message }: Error): void => {
console.error(`Error initializing ${nameOrHash}: ${message}`);
})
)
) )
); .then((exts) => exts.filter((e): e is InjectedExtension => !!e));
} }
// enables all the providers found on the injected window interface // enables all the providers found on the injected window interface
@ -76,29 +92,30 @@ export function web3Enable (originName: string, compatInits: (() => Promise<bool
initCompat.then(() => initCompat.then(() =>
getWindowExtensions(originName) getWindowExtensions(originName)
.then((values): InjectedExtension[] => .then((values): InjectedExtension[] =>
values values.map((e): InjectedExtension => {
.filter((value): value is [InjectedExtensionInfo, Injected] => !!value[1]) // if we don't have an accounts subscriber, add a single-shot version
.map(([info, ext]): InjectedExtension => { if (!e.accounts.subscribe) {
// if we don't have an accounts subscriber, add a single-shot version e.accounts.subscribe = (cb: (accounts: InjectedAccount[]) => void | Promise<void>): Unsubcall => {
if (!ext.accounts.subscribe) { e.accounts
ext.accounts.subscribe = (cb: (accounts: InjectedAccount[]) => void | Promise<void>): Unsubcall => { .get()
ext.accounts.get().then(cb).catch(console.error); .then(cb)
.catch(console.error);
return (): void => { return (): void => {
// no ubsubscribe needed, this is a single-shot // no ubsubscribe needed, this is a single-shot
};
}; };
} };
}
return { ...info, ...ext }; return e;
}) })
) )
.catch((): InjectedExtension[] => []) .catch((): InjectedExtension[] => [])
.then((values): InjectedExtension[] => { .then((values): InjectedExtension[] => {
const names = values.map(({ name, version }): string => `${name}/${version}`); const names = values.map(({ name, version }): string => `${name}/${version}`);
isWeb3Injected = web3IsInjected(); isWeb3Injected = web3IsInjected();
console.log(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`); console.info(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
return values; return values;
}) })
@ -118,27 +135,25 @@ export async function web3Accounts ({ accountType, extensions, ss58Format }: Web
const injected = await web3EnablePromise; const injected = await web3EnablePromise;
const retrieved = await Promise.all( const retrieved = await Promise.all(
injected.filter( injected
({ name: source }) => !extensions || extensions.includes(source) .filter(({ name: source }) => !extensions || extensions.includes(source))
).map(async ({ accounts, name: source }): Promise<InjectedAccountWithMeta[]> => { .map(async ({ accounts, name: source }): Promise<InjectedAccountWithMeta[]> => {
try { try {
const list = await accounts.get(); const list = await accounts.get();
return mapAccounts(source, list.filter(({ type }) => type && accountType ? accountType.includes(type) : true), ss58Format); return mapAccounts(source, list.filter(({ type }) => type && accountType ? accountType.includes(type) : true), ss58Format);
} catch (error) { } catch (error) {
// cannot handle this one // cannot handle this one
return []; return [];
} }
}) })
); );
retrieved.forEach((result): void => { retrieved.forEach((result): void => {
accounts.push(...result); accounts.push(...result);
}); });
const addresses = accounts.map(({ address }) => address); console.info(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}`);
console.log(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}: ${addresses.join(', ')}`);
return accounts; return accounts;
} }
@ -152,20 +167,18 @@ export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithM
const triggerUpdate = (): void | Promise<void> => const triggerUpdate = (): void | Promise<void> =>
cb( cb(
Object.entries(accounts).reduce( Object
(result: InjectedAccountWithMeta[], [source, list]): InjectedAccountWithMeta[] => { .entries(accounts)
.reduce((result: InjectedAccountWithMeta[], [source, list]): InjectedAccountWithMeta[] => {
result.push(...mapAccounts(source, list, ss58Format)); result.push(...mapAccounts(source, list, ss58Format));
return result; return result;
}, }, [])
[]
)
); );
const unsubs = (await web3EnablePromise).filter( const unsubs = (await web3EnablePromise)
({ name: source }) => !extensions || extensions.includes(source) .filter(({ name: source }) => !extensions || extensions.includes(source))
).map( .map(({ accounts: { subscribe }, name: source }): Unsubcall =>
({ accounts: { subscribe }, name: source }): Unsubcall =>
subscribe((result): void => { subscribe((result): void => {
accounts[source] = result; accounts[source] = result;
@ -175,7 +188,7 @@ export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithM
console.error(error); console.error(error);
} }
}) })
); );
return (): void => { return (): void => {
unsubs.forEach((unsub): void => { unsubs.forEach((unsub): void => {

View File

@ -1,10 +1,19 @@
// Copyright 2019-2022 @polkadot/extension-inject authors & contributors // Copyright 2019-2022 @polkadot/extension-inject authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import type { Injected, InjectedWindow, InjectOptions } from './types'; import type { Injected, InjectedExtension, InjectedWindow, InjectOptions } from './types';
import { cyrb53 } from './cyrb53';
export { packageInfo } from './packageInfo'; export { packageInfo } from './packageInfo';
// 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 // 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, // 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 // be it via window (current), postMessage (under consideration) or any other mechanism
@ -15,10 +24,24 @@ export function injectExtension (enable: (origin: string) => Promise<Injected>,
// don't clobber the existing object, we will add it (or create as needed) // don't clobber the existing object, we will add it (or create as needed)
windowInject.injectedWeb3 = windowInject.injectedWeb3 || {}; windowInject.injectedWeb3 = windowInject.injectedWeb3 || {};
// add our enable function if (IS_CONNECT_CAPABLE) {
windowInject.injectedWeb3[name] = { // expose our extension on the window object, new-style with connect(origin)
enable: (origin: string): Promise<Injected> => windowInject.injectedWeb3[cyrb53(`${name}/${version}`)] = {
enable(origin), connect: (origin: string): Promise<InjectedExtension> =>
version 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
};
}
} }

View File

@ -0,0 +1,33 @@
// Copyright 2019-2022 @polkadot/extension-inject authors & contributors
// SPDX-License-Identifier: Apache-2.0
// cyrb53 (c) 2018 bryc (github.com/bryc)
// A fast and simple hash function with decent collision resistance.
// Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
// Public domain. Attribution appreciated.
//
// From https://github.com/bryc/code/blob/fed42df9db547493452e32375c93d7854383e480/jshash/experimental/cyrb53.js
// As shared in https://stackoverflow.com/a/52171480
//
// Small changes made to the code as linked above:
// . - Seed value is required (set as Date.now() in usage, could change)
// - Return value is a hex string (as per comment in SO answer)
// - TS typings added
// - Non-intrusive coding-style variable declaration changes
export function cyrb53 (input: string, seed = Date.now()): string {
let h1 = 0xdeadbeef ^ seed;
let h2 = 0x41c6ce57 ^ seed;
for (let i = 0; i < input.length; i++) {
const ch = input.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
// https://stackoverflow.com/a/52171480
return (h2 >>> 0).toString(16).padStart(8, '0') + (h1 >>> 0).toString(16).padStart(8, '0');
}

View File

@ -99,8 +99,9 @@ export interface Injected {
} }
export interface InjectedWindowProvider { export interface InjectedWindowProvider {
enable: (origin: string) => Promise<Injected>; connect?: (origin: string) => Promise<InjectedExtension>;
version: string; enable?: (origin: string) => Promise<Injected>;
version?: string;
} }
export interface InjectedWindow extends This { export interface InjectedWindow extends This {