Skip to content

Commit 5f338fd

Browse files
committed
[TESTING]: support @substrate/discovery
1 parent fd2d811 commit 5f338fd

File tree

8 files changed

+378
-9
lines changed

8 files changed

+378
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@polkadot/util-crypto": "^13.2.2",
6060
"@polkadot/x-fetch": "^13.2.2",
6161
"safe-buffer": "^5.2.1",
62+
"smoldot": "2.0.30",
6263
"typescript": "^5.3.3"
6364
}
6465
}

packages/extension/manifest_chrome.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "polkadot{.js} extension",
66
"short_name": "polkadot{.js}",
77
"manifest_version": 3,
8-
"permissions": ["storage", "tabs"],
8+
"permissions": ["storage", "scripting", "tabs", "notifications", "alarms"],
99
"background": {
1010
"service_worker": "background.js",
1111
"type": "module"

packages/extension/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717
"type": "module",
1818
"version": "0.56.1",
1919
"dependencies": {
20+
"@polkadot-api/pjs-signer": "^0.6.0",
21+
"@polkadot-api/utils": "^0.1.2",
2022
"@polkadot/api": "^14.2.1",
2123
"@polkadot/extension-base": "0.56.1",
2224
"@polkadot/extension-inject": "0.56.1",
2325
"@polkadot/extension-ui": "0.56.1",
26+
"@substrate/connect-discovery": "^0.2.1",
27+
"@substrate/light-client-extension-helpers": "^2.5.2",
28+
"@substrate/smoldot-discovery": "^2.0.1",
29+
"@substrate/smoldot-discovery-connector": "^0.3.1",
30+
"smoldot": "2.0.30",
2431
"tslib": "^2.6.2"
2532
},
2633
"devDependencies": {

packages/extension/src/background.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ import '@polkadot/extension-inject/crossenv';
99

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

12+
import { ksmcc3, polkadot, westend2 } from '@substrate/connect-known-chains';
13+
import { register } from '@substrate/light-client-extension-helpers/background';
14+
import { start } from '@substrate/light-client-extension-helpers/smoldot';
15+
1216
import { handlers, withErrorLog } from '@polkadot/extension-base/background';
1317
import { PORT_CONTENT, PORT_EXTENSION } from '@polkadot/extension-base/defaults';
1418
import { AccountsStore } from '@polkadot/extension-base/stores';
1519
import { keyring } from '@polkadot/ui-keyring';
1620
import { assert } from '@polkadot/util';
1721
import { cryptoWaitReady } from '@polkadot/util-crypto';
1822

23+
register({
24+
getWellKnownChainSpecs: () => Promise.resolve([polkadot, ksmcc3, westend2]),
25+
smoldotClient: start({ maxLogLevel: 4 })
26+
});
27+
1928
// setup the notification (same a FF default background, white text)
2029
withErrorLog(() => chrome.action.setBadgeBackgroundColor({ color: '#d90000' }));
2130

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright 2017-2024 @polkadot/extension authors & contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
export const CHANNEL_ID = 'polkadot-js-extension';

packages/extension/src/content.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33

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

6+
import { register } from '@substrate/light-client-extension-helpers/content-script';
7+
68
import { MESSAGE_ORIGIN_CONTENT, MESSAGE_ORIGIN_PAGE, PORT_CONTENT } from '@polkadot/extension-base/defaults';
79
import { ensurePortConnection } from '@polkadot/extension-base/utils/portUtils';
810
import { chrome } from '@polkadot/extension-inject/chrome';
11+
import { CHANNEL_ID } from './constants';
12+
13+
register(CHANNEL_ID);
914

1015
let port: chrome.runtime.Port | undefined;
1116

packages/extension/src/page.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,111 @@
11
// Copyright 2019-2024 @polkadot/extension authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import type { Unstable } from '@substrate/connect-discovery';
45
import type { RequestSignatures, TransportRequestMessage } from '@polkadot/extension-base/background/types';
56
import type { Message } from '@polkadot/extension-base/types';
67

8+
import { createTx } from '@substrate/light-client-extension-helpers/tx-helper';
9+
import { getLightClientProvider } from '@substrate/light-client-extension-helpers/web-page';
10+
711
import { MESSAGE_ORIGIN_CONTENT } from '@polkadot/extension-base/defaults';
812
import { enable, handleResponse, redirectIfPhishing } from '@polkadot/extension-base/page';
913
import { injectExtension } from '@polkadot/extension-inject';
14+
import { connectInjectedExtension } from '@polkadot-api/pjs-signer';
15+
import { toHex, fromHex } from '@polkadot-api/utils'
16+
import {
17+
make as makeSmoldotDiscoveryConnector,
18+
type SmoldotExtensionProviderDetail,
19+
} from "@substrate/smoldot-discovery-connector"
1020

21+
import { CHANNEL_ID } from './constants.js';
1122
import { packageInfo } from './packageInfo.js';
1223

24+
const PROVIDER_INFO = {
25+
icon: "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>",
26+
name: 'Polkadot JS Extension',
27+
rdns: 'io.github.paritytech.PolkadotJsExtension',
28+
uuid: crypto.randomUUID()
29+
};
30+
31+
const lightClientProvider = getLightClientProvider(CHANNEL_ID);
32+
33+
// #region Smoldot Discovery Provider
34+
{
35+
const provider = lightClientProvider.then(makeSmoldotDiscoveryConnector)
36+
const detail: SmoldotExtensionProviderDetail = Object.freeze({
37+
info: PROVIDER_INFO,
38+
kind: 'smoldot-v1',
39+
provider
40+
});
41+
42+
window.addEventListener(
43+
'substrateDiscovery:requestProvider',
44+
({ detail: { onProvider } }) => onProvider(detail)
45+
);
46+
47+
window.dispatchEvent(
48+
new CustomEvent('substrateDiscovery:announceProvider', {
49+
detail
50+
})
51+
);
52+
}
53+
// #endregion
54+
55+
// #region Connect Discovery Provider
56+
{
57+
const provider = lightClientProvider.then((lightClientProvider): Unstable.Provider => ({
58+
...lightClientProvider,
59+
async createTx (chainId: string, from: string, callData: string) {
60+
const chains = Object.values(lightClientProvider.getChains());
61+
const chain = chains.find(({ genesisHash }) => genesisHash === chainId);
62+
63+
if (!chain) {
64+
throw new Error('unknown chain');
65+
}
66+
67+
const injectedExt = await connectInjectedExtension('polkadot-js');
68+
69+
const account = injectedExt.getAccounts()
70+
.find((account) => toHex(account.polkadotSigner.publicKey) === from);
71+
72+
if (!account) {
73+
throw new Error('no account');
74+
}
75+
76+
const signer = account.polkadotSigner;
77+
78+
const tx = await createTx(chain.connect)({ callData: fromHex(callData), signer });
79+
80+
return toHex(tx);
81+
},
82+
async getAccounts (_chainId: string) {
83+
const injectedExt = await connectInjectedExtension('polkadot-js');
84+
const accounts = injectedExt.getAccounts();
85+
86+
return accounts;
87+
}
88+
}));
89+
90+
const detail: Unstable.SubstrateConnectProviderDetail = Object.freeze({
91+
info: PROVIDER_INFO,
92+
kind: 'substrate-connect-unstable',
93+
provider
94+
});
95+
96+
window.addEventListener(
97+
'substrateDiscovery:requestProvider',
98+
({ detail: { onProvider } }) => onProvider(detail)
99+
);
100+
101+
window.dispatchEvent(
102+
new CustomEvent('substrateDiscovery:announceProvider', {
103+
detail
104+
})
105+
);
106+
}
107+
// #endregion
108+
13109
function inject () {
14110
injectExtension(enable, {
15111
name: 'polkadot-js',

0 commit comments

Comments
 (0)