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 3 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 @@ -16,6 +16,7 @@
"@acala-network/type-definitions": "^4.1.5",
"@babel/runtime": "^7.18.9",
"@bifrost-finance/type-definitions": "1.6.1",
"@chainsafe/metamask-polkadot-adapter": "^0.4.1",
"@crustio/type-definitions": "1.3.0",
"@darwinia/types": "2.8.9",
"@darwinia/types-known": "2.8.9",
Expand Down
21 changes: 21 additions & 0 deletions packages/apps-config/src/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2017-2022 @polkadot/apps-config authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { initPolkadotSnap } from '@chainsafe/metamask-polkadot-adapter/build/extension';

// it would have been really good to import this from detect-browser, however... not exported
BeroBurny marked this conversation as resolved.
Show resolved Hide resolved
type Browser = 'chrome' | 'firefox';

interface Extension {
Expand Down Expand Up @@ -37,3 +40,21 @@ export const availableExtensions = known.reduce<Record<Browser, Extension[]>>((a

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 Promise.all([
initPolkadotSnap
].map((method) => {
// Timeout injecting extension
return Promise.race([
method(),
new Promise<false>((resolve) => {
setTimeout((): void => {
resolve(false);
}, 1000 /* 1 sec */);
})
]);
}));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why does the return time have to be Promise<boolean[]>? After all, initPolkadotSnap is just returning a single Promise<boolean>.

  2. In general, this code could be simplified when using rxjs which already is a dependency:

Suggested change
export function injectExtensions (): Promise<boolean[]> {
return Promise.all([
initPolkadotSnap
].map((method) => {
// Timeout injecting extension
return Promise.race([
method(),
new Promise<false>((resolve) => {
setTimeout((): void => {
resolve(false);
}, 1000 /* 1 sec */);
})
]);
}));
}
export const injectExtensions = () =>
firstValueFrom(from(initPolkadotSnap())
.pipe(
timeout(1000), // timeout if initPolkadotSnap() doesn't resolve after 1s
catchError(() => of(false)),
));

(Please note that I haven't actually tested this code)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Idea is to make it future proof it there will be another wallet like MM snaps so users can easily add it to apps
    for example @polkadot/extension-compat-metamask you can just import initMetaMask and add it to the list to support it

  2. did the same solution with RxJs (personally for me is more complex as I'm uset to async/await js)

18 changes: 10 additions & 8 deletions packages/react-api/src/Api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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/InputNumber';
import { StatusContext } from '@polkadot/react-components/Status';
Expand Down Expand Up @@ -328,15 +328,17 @@ function Api ({ apiUrl, children, isElectron, store }: Props): React.ReactElemen
api.on('disconnected', () => setIsApiConnected(false));
api.on('error', onError);
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);

loadOnReady(api, apiEndpoint, injectedPromise, store, types)
.then(setState)
.catch(onError);
loadOnReady(api, apiEndpoint, injectedPromise, store, types)
.then(setState)
.catch(onError);
}).catch(onError);
});

setIsApiInitialized(true);
Expand Down
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,16 @@ __metadata:
languageName: node
linkType: hard

"@chainsafe/metamask-polkadot-adapter@npm:^0.4.1":
version: 0.4.1
resolution: "@chainsafe/metamask-polkadot-adapter@npm:0.4.1"
dependencies:
"@polkadot/api": ^8.7.1
"@polkadot/extension-inject": ^0.44.5
checksum: dc142a8cfa0e0195bcfc89da440b2c8bec3740ee8617197ba295ffe06d291247ba7691ca85499585ecdd9d090ffc1adec5f75e5196e186c509b30a17ccd8962e
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 @@ -3176,6 +3186,7 @@ __metadata:
"@acala-network/type-definitions": ^4.1.5
"@babel/runtime": ^7.18.9
"@bifrost-finance/type-definitions": 1.6.1
"@chainsafe/metamask-polkadot-adapter": ^0.4.1
"@crustio/type-definitions": 1.3.0
"@darwinia/types": 2.8.9
"@darwinia/types-known": 2.8.9
Expand Down