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

[TESTING]: support @substrate/discovery #1389

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@polkadot/util-crypto": "^13.2.2",
"@polkadot/x-fetch": "^13.2.2",
"safe-buffer": "^5.2.1",
"smoldot": "2.0.30",
"typescript": "^5.3.3"
}
}
5 changes: 3 additions & 2 deletions packages/extension/manifest_chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "polkadot{.js} extension",
"short_name": "polkadot{.js}",
"manifest_version": 3,
"permissions": ["storage", "tabs"],
"permissions": ["storage", "scripting", "tabs", "notifications", "alarms"],
"background": {
"service_worker": "background.js",
"type": "module"
Expand All @@ -29,7 +29,8 @@
"web_accessible_resources": [
{
"resources": [
"page.js"
"page.js",
"*.js"
],
"matches": [
"*://*/*"
Expand Down
7 changes: 7 additions & 0 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@
"type": "module",
"version": "0.56.1",
"dependencies": {
"@polkadot-api/pjs-signer": "^0.6.0",
"@polkadot-api/utils": "^0.1.2",
"@polkadot/api": "^14.2.1",
"@polkadot/extension-base": "0.56.1",
"@polkadot/extension-inject": "0.56.1",
"@polkadot/extension-ui": "0.56.1",
"@substrate/connect-discovery": "^0.2.1",
"@substrate/light-client-extension-helpers": "^2.5.2",
"@substrate/smoldot-discovery": "^2.0.1",
"@substrate/smoldot-discovery-connector": "^0.3.1",
"smoldot": "2.0.30",
"tslib": "^2.6.2"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ import '@polkadot/extension-inject/crossenv';

import type { RequestSignatures, TransportRequestMessage } from '@polkadot/extension-base/background/types';

import { ksmcc3, polkadot, westend2 } from '@substrate/connect-known-chains';
import { register } from '@substrate/light-client-extension-helpers/background';
import { start } from '@substrate/light-client-extension-helpers/smoldot';

import { handlers, withErrorLog } from '@polkadot/extension-base/background';
import { PORT_CONTENT, PORT_EXTENSION } from '@polkadot/extension-base/defaults';
import { AccountsStore } from '@polkadot/extension-base/stores';
import { keyring } from '@polkadot/ui-keyring';
import { assert } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';

register({
getWellKnownChainSpecs: () => Promise.resolve([polkadot, ksmcc3, westend2]),
smoldotClient: start({ maxLogLevel: 4 })
});

// setup the notification (same a FF default background, white text)
withErrorLog(() => chrome.action.setBadgeBackgroundColor({ color: '#d90000' }));

Expand Down
4 changes: 4 additions & 0 deletions packages/extension/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright 2017-2024 @polkadot/extension authors & contributors
// SPDX-License-Identifier: Apache-2.0

export const CHANNEL_ID = 'polkadot-js-extension';
5 changes: 5 additions & 0 deletions packages/extension/src/content.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// Copyright 2019-2024 @polkadot/extension authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Message } from '@polkadot/extension-base/types';

Check failure on line 4 in packages/extension/src/content.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Run autofix to sort these imports!

import { register } from '@substrate/light-client-extension-helpers/content-script';

import { MESSAGE_ORIGIN_CONTENT, MESSAGE_ORIGIN_PAGE, PORT_CONTENT } from '@polkadot/extension-base/defaults';
import { ensurePortConnection } from '@polkadot/extension-base/utils/portUtils';
import { chrome } from '@polkadot/extension-inject/chrome';
import { CHANNEL_ID } from './constants';

register(CHANNEL_ID);

let port: chrome.runtime.Port | undefined;

Expand Down
98 changes: 97 additions & 1 deletion packages/extension/src/page.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,112 @@
// Copyright 2019-2024 @polkadot/extension authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Unstable } from '@substrate/connect-discovery';

Check failure on line 4 in packages/extension/src/page.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Run autofix to sort these imports!
import type { RequestSignatures, TransportRequestMessage } from '@polkadot/extension-base/background/types';
import type { Message } from '@polkadot/extension-base/types';

import { createTx } from '@substrate/light-client-extension-helpers/tx-helper';
import { getLightClientProvider } from '@substrate/light-client-extension-helpers/web-page';

import { MESSAGE_ORIGIN_CONTENT } from '@polkadot/extension-base/defaults';
import { enable, handleResponse, redirectIfPhishing } from '@polkadot/extension-base/page';
import { injectExtension } from '@polkadot/extension-inject';
import { connectInjectedExtension } from '@polkadot-api/pjs-signer';
import { toHex, fromHex } from '@polkadot-api/utils'

Check failure on line 15 in packages/extension/src/page.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon
import { type SmoldotExtensionProviderDetail } from "@substrate/smoldot-discovery-connector"

Check failure on line 16 in packages/extension/src/page.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Strings must use singlequote

Check failure on line 16 in packages/extension/src/page.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon

import { CHANNEL_ID } from './constants.js';
import { packageInfo } from './packageInfo.js';

function inject () {
const PROVIDER_INFO = {
icon: "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>",
name: 'Polkadot JS Extension',
rdns: 'io.github.paritytech.PolkadotJsExtension',
uuid: crypto.randomUUID()
};

const lightClientProvider = getLightClientProvider(CHANNEL_ID);

// #region Smoldot Discovery Provider
{
const provider = lightClientProvider.then(async (provider) => {
const { make: makeSmoldotDiscoveryConnector } = await import('@substrate/smoldot-discovery-connector')

Check failure on line 33 in packages/extension/src/page.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon
return makeSmoldotDiscoveryConnector(provider)

Check failure on line 34 in packages/extension/src/page.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Expected blank line before this statement

Check failure on line 34 in packages/extension/src/page.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon
})

Check failure on line 35 in packages/extension/src/page.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon
const detail: SmoldotExtensionProviderDetail = Object.freeze({
info: PROVIDER_INFO,
kind: 'smoldot-v1',
provider
});

window.addEventListener(
'substrateDiscovery:requestProvider',
({ detail: { onProvider } }) => onProvider(detail)
);

window.dispatchEvent(
new CustomEvent('substrateDiscovery:announceProvider', {
detail
})
);
}
// #endregion

// #region Connect Discovery Provider
{
const provider = lightClientProvider.then((lightClientProvider): Unstable.Provider => ({
...lightClientProvider,
async createTx(chainId: string, from: string, callData: string) {

Check failure on line 59 in packages/extension/src/page.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing space before function parentheses
const chains = Object.values(lightClientProvider.getChains());
const chain = chains.find(({ genesisHash }) => genesisHash === chainId);

if (!chain) {
throw new Error('unknown chain');
}

const injectedExt = await connectInjectedExtension('polkadot-js');

const account = injectedExt.getAccounts()
.find((account) => toHex(account.polkadotSigner.publicKey) === from);

if (!account) {
throw new Error('no account');
}

const signer = account.polkadotSigner;

const tx = await createTx(chain.connect)({ callData: fromHex(callData), signer });

return toHex(tx);
},
async getAccounts(_chainId: string) {
const injectedExt = await connectInjectedExtension('polkadot-js');
const accounts = injectedExt.getAccounts();

return accounts;
}
}));

const detail: Unstable.SubstrateConnectProviderDetail = Object.freeze({
info: PROVIDER_INFO,
kind: 'substrate-connect-unstable',
provider
});

window.addEventListener(
'substrateDiscovery:requestProvider',
({ detail: { onProvider } }) => onProvider(detail)
);

window.dispatchEvent(
new CustomEvent('substrateDiscovery:announceProvider', {
detail
})
);
}
// #endregion

function inject() {
injectExtension(enable, {
name: 'polkadot-js',
version: packageInfo.version
Expand Down
Loading