Skip to content

Commit 14bf694

Browse files
committed
[TESTING]: support @substrate/discovery
1 parent ace0125 commit 14bf694

8 files changed

+309
-4
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@polkadot/util-crypto": "^12.6.2",
5959
"@polkadot/x-fetch": "^12.6.2",
6060
"safe-buffer": "^5.2.1",
61+
"smoldot": "2.0.29",
6162
"typescript": "^5.3.3"
6263
}
6364
}

packages/extension/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
"type": "module",
1818
"version": "0.48.3-3-x",
1919
"dependencies": {
20+
"@polkadot-api/pjs-signer": "^0.2.1",
21+
"@polkadot-api/utils": "^0.1.0",
2022
"@polkadot/api": "^12.0.2",
2123
"@polkadot/extension-base": "0.48.3-3-x",
2224
"@polkadot/extension-inject": "0.48.3-3-x",
2325
"@polkadot/extension-ui": "0.48.3-3-x",
26+
"@substrate/connect-discovery": "^0.0.6",
27+
"@substrate/light-client-extension-helpers": "./substrate-light-client-extension-helpers-2.0.3.tgz",
28+
"@substrate/smoldot-discovery": "^0.0.7",
29+
"smoldot": "^2.0.29",
2430
"tslib": "^2.6.2"
2531
},
2632
"devDependencies": {

packages/extension/src/background.ts

+9
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

packages/extension/src/constants.ts

+4
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

+6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
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 { chrome } from '@polkadot/extension-inject/chrome';
810

11+
import { CHANNEL_ID } from './constants';
12+
13+
register(CHANNEL_ID);
14+
915
// connect to the extension
1016
const port = chrome.runtime.connect({ name: PORT_CONTENT });
1117

packages/extension/src/page.ts

+96
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';
5+
import type { SmoldotExtensionProviderDetail } from '@substrate/smoldot-discovery/types';
46
import type { RequestSignatures, TransportRequestMessage } from '@polkadot/extension-base/background/types';
57
import type { Message } from '@polkadot/extension-base/types';
68

9+
import { createTx } from '@substrate/light-client-extension-helpers/tx-helper';
10+
import { getLightClientProvider } from '@substrate/light-client-extension-helpers/web-page';
11+
import { connector } from '@substrate/smoldot-discovery';
12+
713
import { MESSAGE_ORIGIN_CONTENT } from '@polkadot/extension-base/defaults';
814
import { enable, handleResponse, redirectIfPhishing } from '@polkadot/extension-base/page';
915
import { injectExtension } from '@polkadot/extension-inject';
16+
import { connectInjectedExtension } from '@polkadot-api/pjs-signer';
17+
import { fromHex, toHex } from '@polkadot-api/utils';
1018

19+
import { CHANNEL_ID } from './constants.js';
1120
import { packageInfo } from './packageInfo.js';
1221

22+
const PROVIDER_INFO = {
23+
icon: "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>",
24+
name: 'Polkadot JS Extension',
25+
rdns: 'io.github.paritytech.PolkadotJsExtension',
26+
uuid: crypto.randomUUID()
27+
};
28+
29+
const lightClientProviderPromise = getLightClientProvider(CHANNEL_ID);
30+
31+
// #region Smoldot Discovery Provider
32+
{
33+
const provider = lightClientProviderPromise
34+
.then((provider) => connector.make({ lightClientProvider: provider }));
35+
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 = lightClientProviderPromise.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',
Binary file not shown.

0 commit comments

Comments
 (0)