Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Metamask Snap integration #8070

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/apps-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions packages/apps-config/src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<boolean[]> {
return firstValueFrom(zip([
initPolkadotSnap
].map((method) => firstValueFrom(from(method({})).pipe(
timeout(1000), // timeout if method() doesn't resolve after 1s
catchError(() => of(false))
)))));
}
20 changes: 11 additions & 9 deletions packages/react-api/src/Api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
Loading