diff --git a/packages/extension/package.json b/packages/extension/package.json index d2f36c2061..c6214f3b7a 100644 --- a/packages/extension/package.json +++ b/packages/extension/package.json @@ -22,7 +22,7 @@ "@polkadot/extension-inject": "0.48.3-2-x", "@polkadot/extension-ui": "0.48.3-2-x", "@substrate/light-client-extension-helpers": "^2.0.2", - "@substrate/smoldot-discovery": "^0.0.6", + "@substrate/smoldot-discovery": "^0.0.7", "smoldot": "^2.0.29", "tslib": "^2.6.2" }, diff --git a/packages/extension/src/connector.ts b/packages/extension/src/connector.ts deleted file mode 100644 index 9aa960df91..0000000000 --- a/packages/extension/src/connector.ts +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2019-2024 @polkadot/extension authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -import type { LightClientProvider, RawChain } from '@substrate/light-client-extension-helpers/web-page'; -import type { Chain, JsonRpcCallback, SmoldotExtensionAPI, WellKnownChain } from '@substrate/smoldot-discovery/types'; - -const wellKnownChainGenesisHashes: Record = { - polkadot: - '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3', - ksmcc3: '0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe', - westend2: - '0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e', - rococo_v2_2: - '0x6408de7737c59c238890533af25896a2c20608d8b380bb01029acb392781063e' -}; - -export type { LightClientProvider } from '@substrate/light-client-extension-helpers/web-page'; - -export const createScClient = ( - lightClientProvider: LightClientProvider -): SmoldotExtensionAPI => { - const internalAddChain = async ( - isWellKnown: boolean, - chainSpecOrWellKnownName: string, - jsonRpcCallback: JsonRpcCallback = () => {}, - relayChainGenesisHash?: string - ): Promise => { - let chain: RawChain; - - if (isWellKnown) { - const foundChain = Object.values(lightClientProvider.getChains()).find( - ({ genesisHash }) => - genesisHash === wellKnownChainGenesisHashes[chainSpecOrWellKnownName] - ); - - if (!foundChain) { - throw new Error('Unknown well-known chain'); - } - - chain = foundChain; - } else { - chain = await lightClientProvider.getChain( - chainSpecOrWellKnownName, - relayChainGenesisHash - ); - } - - const jsonRpcProvider = chain.connect(jsonRpcCallback); - - return { - sendJsonRpc (rpc: string): void { - jsonRpcProvider.send(rpc); - }, - remove () { - jsonRpcProvider.disconnect(); - }, - addChain: function ( - chainSpec: string, - jsonRpcCallback?: JsonRpcCallback | undefined - ): Promise { - return internalAddChain( - false, - chainSpec, - jsonRpcCallback, - chain.genesisHash - ); - } - }; - }; - - return { - addChain: (chainSpec: string, jsonRpcCallback?: JsonRpcCallback) => - internalAddChain(false, chainSpec, jsonRpcCallback), - addWellKnownChain: ( - name: WellKnownChain, - jsonRpcCallback?: JsonRpcCallback - ) => internalAddChain(true, name, jsonRpcCallback) - }; -}; diff --git a/packages/extension/src/page.ts b/packages/extension/src/page.ts index 004332f5b1..dd0f6fd814 100644 --- a/packages/extension/src/page.ts +++ b/packages/extension/src/page.ts @@ -11,7 +11,7 @@ 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 { createScClient } from './connector.js'; +import { connector } from "@substrate/smoldot-discovery" import { CHANNEL_ID } from './constants.js'; import { packageInfo } from './packageInfo.js'; @@ -26,7 +26,8 @@ const lightClientProvider = getLightClientProvider(CHANNEL_ID); // #region Smoldot Discovery Provider { - const provider = lightClientProvider.then(createScClient); + const provider = lightClientProvider + .then((provider) => connector.make({ lightClientProvider: provider })); const detail: SmoldotExtensionProviderDetail = Object.freeze({ kind: 'smoldot-v1', @@ -47,7 +48,7 @@ const lightClientProvider = getLightClientProvider(CHANNEL_ID); } // #endregion -function inject () { +function inject() { injectExtension(enable, { name: 'polkadot-js', version: packageInfo.version diff --git a/yarn.lock b/yarn.lock index eb4a7d868f..be193951a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1107,7 +1107,7 @@ __metadata: "@polkadot/extension-inject": "npm:0.48.3-2-x" "@polkadot/extension-ui": "npm:0.48.3-2-x" "@substrate/light-client-extension-helpers": "npm:^2.0.2" - "@substrate/smoldot-discovery": "npm:^0.0.6" + "@substrate/smoldot-discovery": "npm:^0.0.7" browser-resolve: "npm:^2.0.0" buffer: "npm:^6.0.3" copy-webpack-plugin: "npm:^11.0.0" @@ -1987,12 +1987,14 @@ __metadata: languageName: node linkType: hard -"@substrate/smoldot-discovery@npm:^0.0.6": - version: 0.0.6 - resolution: "@substrate/smoldot-discovery@npm:0.0.6" +"@substrate/smoldot-discovery@npm:^0.0.7": + version: 0.0.7 + resolution: "@substrate/smoldot-discovery@npm:0.0.7" dependencies: "@substrate/discovery": "npm:^0.0.1" - checksum: 10/4147035c86f7331dc8095a82c0ff1223a4bb9cc2ebe266e2ac5398d53f1d1b655ce3ee17499bfddd2b49bae082a3ca279e93f2787374dabd85d318c5cf0fe188 + peerDependencies: + "@substrate/light-client-extension-helpers": ^2 + checksum: 10/e13be406980cd74c48910285b991acc17411ad0289cdcebec9f073d93dddd11cd65527ba6c518e56e0103e89fa3286f3431b7efca8bd35863be6479faf6a3561 languageName: node linkType: hard