diff --git a/packages/apps-config/package.json b/packages/apps-config/package.json index f61780abfc9b..a0d13f2043a1 100644 --- a/packages/apps-config/package.json +++ b/packages/apps-config/package.json @@ -18,6 +18,7 @@ "dependencies": { "@acala-network/type-definitions": "5.1.2", "@bifrost-finance/type-definitions": "1.11.3", + "@chainsafe/metamask-polkadot-adapter": "^0.6.0", "@crustio/type-definitions": "1.3.0", "@darwinia/types": "2.8.10", "@darwinia/types-known": "2.8.10", diff --git a/packages/apps-config/src/extensions/index.ts b/packages/apps-config/src/extensions/index.ts index eee2a2333c79..42b381489c67 100644 --- a/packages/apps-config/src/extensions/index.ts +++ b/packages/apps-config/src/extensions/index.ts @@ -3,6 +3,9 @@ import type { Browser, Extension, Known } from './types.js'; +import { initPolkadotSnap } from '@chainsafe/metamask-polkadot-adapter/build/extension'; +import { catchError, firstValueFrom, from, of, timeout, zip } from 'rxjs'; + import { PolkadotJs } from './polkadot-js.js'; // The list of known extensions including the links to tem on the store. This is @@ -22,3 +25,13 @@ export const availableExtensions = Object return available; }, { chrome: [], firefox: [] }); + +// Some extensions do not use `@polkadot/extension-inject` and need to be manually inject into a window context +export function injectExtensions (): Promise { + return firstValueFrom(zip([ + initPolkadotSnap + ].map((method) => firstValueFrom(from(method({})).pipe( + timeout(1000), // timeout if method() doesn't resolve after 1s + catchError(() => of(false)) + ))))); +} diff --git a/packages/react-api/src/Api.tsx b/packages/react-api/src/Api.tsx index 71d4540b0c08..2e567d61a946 100644 --- a/packages/react-api/src/Api.tsx +++ b/packages/react-api/src/Api.tsx @@ -14,7 +14,7 @@ import store from 'store'; import { ApiPromise, ScProvider, WsProvider } from '@polkadot/api'; import { deriveMapCache, setDeriveCache } from '@polkadot/api-derive/util'; -import { ethereumChains, typesBundle } from '@polkadot/apps-config'; +import { ethereumChains, injectExtensions, typesBundle } from '@polkadot/apps-config'; import { web3Accounts, web3Enable } from '@polkadot/extension-dapp'; import { TokenUnit } from '@polkadot/react-components/InputConsts/units'; import { useApiUrl, useEndpoint, useQueue } from '@polkadot/react-hooks'; @@ -309,17 +309,19 @@ export function ApiCtxRoot ({ apiUrl, children, isElectron, store: keyringStore statics.api.on('disconnected', () => setIsApiConnected(false)); statics.api.on('error', onError); statics.api.on('ready', (): void => { - const injectedPromise = web3Enable('polkadot-js/apps'); + injectExtensions().then(() => { + const injectedPromise = web3Enable('polkadot-js/apps'); - injectedPromise - .then(setExtensions) - .catch(console.error); + injectedPromise + .then(setExtensions) + .catch(console.error); - const urlIsEthereum = !!location.href.includes('keyring-type=ethereum'); + const urlIsEthereum = !!location.href.includes('keyring-type=ethereum'); - loadOnReady(statics.api, apiEndpoint, injectedPromise, keyringStore, types, urlIsEthereum) - .then(setState) - .catch(onError); + loadOnReady(statics.api, apiEndpoint, injectedPromise, keyringStore, types, urlIsEthereum) + .then(setState) + .catch(onError); + }).catch(onError); }); if (isLocalFork) { diff --git a/yarn.lock b/yarn.lock index a9aa09608733..0e6dbe2cae78 100644 --- a/yarn.lock +++ b/yarn.lock @@ -155,6 +155,17 @@ __metadata: languageName: node linkType: hard +"@chainsafe/metamask-polkadot-adapter@npm:^0.6.0": + version: 0.6.0 + resolution: "@chainsafe/metamask-polkadot-adapter@npm:0.6.0" + dependencies: + "@polkadot/api": "npm:^10.9.1" + "@polkadot/extension-inject": "npm:^0.46.5" + "@polkadot/types-augment": "npm:^10.9.1" + checksum: 10/32635c3a6ead8adfa7b8f99bbfa6548bc7f0632f13bb1c6f2e7833e6d1e0761df1944fcfcf10c3648a5e2dde3cd00a0f3e355ca68e1626eca817dc3e3d6a941e + languageName: node + linkType: hard + "@crustio/crust-pin@npm:^1.0.0": version: 1.0.0 resolution: "@crustio/crust-pin@npm:1.0.0" @@ -1941,6 +1952,7 @@ __metadata: dependencies: "@acala-network/type-definitions": "npm:5.1.2" "@bifrost-finance/type-definitions": "npm:1.11.3" + "@chainsafe/metamask-polkadot-adapter": "npm:^0.6.0" "@crustio/type-definitions": "npm:1.3.0" "@darwinia/types": "npm:2.8.10" "@darwinia/types-known": "npm:2.8.10" @@ -2186,7 +2198,7 @@ __metadata: languageName: node linkType: hard -"@polkadot/extension-inject@npm:0.46.9": +"@polkadot/extension-inject@npm:0.46.9, @polkadot/extension-inject@npm:^0.46.5": version: 0.46.9 resolution: "@polkadot/extension-inject@npm:0.46.9" dependencies: