|
1 | 1 | // Copyright 2019-2024 @polkadot/extension authors & contributors
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
| 4 | +import type { Unstable } from '@substrate/connect-discovery'; |
| 5 | +import type { SmoldotExtensionProviderDetail } from '@substrate/smoldot-discovery/types'; |
4 | 6 | import type { RequestSignatures, TransportRequestMessage } from '@polkadot/extension-base/background/types';
|
5 | 7 | import type { Message } from '@polkadot/extension-base/types';
|
6 | 8 |
|
| 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 | + |
7 | 13 | import { MESSAGE_ORIGIN_CONTENT } from '@polkadot/extension-base/defaults';
|
8 | 14 | import { enable, handleResponse, redirectIfPhishing } from '@polkadot/extension-base/page';
|
9 | 15 | import { injectExtension } from '@polkadot/extension-inject';
|
| 16 | +import { connectInjectedExtension } from '@polkadot-api/pjs-signer'; |
| 17 | +import { fromHex, toHex } from '@polkadot-api/utils'; |
10 | 18 |
|
| 19 | +import { CHANNEL_ID } from './constants.js'; |
11 | 20 | import { packageInfo } from './packageInfo.js';
|
12 | 21 |
|
| 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 | + |
13 | 109 | function inject () {
|
14 | 110 | injectExtension(enable, {
|
15 | 111 | name: 'polkadot-js',
|
|
0 commit comments