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.0.2",
"@polkadot/x-fetch": "^13.0.2",
"safe-buffer": "^5.2.1",
"smoldot": "2.0.30",
"typescript": "^5.3.3"
}
}
6 changes: 6 additions & 0 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
"type": "module",
"version": "0.52.3",
"dependencies": {
"@polkadot-api/pjs-signer": "^0.4.1",
"@polkadot-api/utils": "^0.1.1",
"@polkadot/api": "^12.4.1",
"@polkadot/extension-base": "0.52.3",
"@polkadot/extension-inject": "0.52.3",
"@polkadot/extension-ui": "0.52.3",
"@substrate/connect-discovery": "^0.1.0",
"@substrate/light-client-extension-helpers": "^2.4.0",
"@substrate/smoldot-discovery": "^1.1.0",
"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
96 changes: 96 additions & 0 deletions packages/extension/src/page.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,111 @@
// Copyright 2019-2024 @polkadot/extension authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Unstable } from '@substrate/connect-discovery';
import type { SmoldotExtensionProviderDetail } from '@substrate/smoldot-discovery/types';
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 { connector } from '@substrate/smoldot-discovery';

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

View workflow job for this annotation

GitHub Actions / pr (build)

Module '"@substrate/smoldot-discovery"' has no exported member 'connector'.

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

View workflow job for this annotation

GitHub Actions / pr (diff)

Module '"@substrate/smoldot-discovery"' has no exported member 'connector'.

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 { fromHex, toHex } from '@polkadot-api/utils';

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

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 lightClientProviderPromise = getLightClientProvider(CHANNEL_ID);

// #region Smoldot Discovery Provider
{
const provider = lightClientProviderPromise
.then((provider) => connector.make({ lightClientProvider: provider }));

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

View workflow job for this annotation

GitHub Actions / pr (lint)

Unsafe return of an `any` typed value

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

View workflow job for this annotation

GitHub Actions / pr (lint)

Unsafe call of an `any` typed value

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

View workflow job for this annotation

GitHub Actions / pr (lint)

Unsafe member access .make on an `any` value

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 = lightClientProviderPromise.then((lightClientProvider): Unstable.Provider => ({
...lightClientProvider,
async createTx (chainId: string, from: string, callData: string) {
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',
Expand Down
Loading
Loading