|
1 | 1 | // Copyright 2019-2024 @polkadot/extension authors & contributors |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -import type { |
5 | | - LightClientProvider, |
6 | | - RawChain, |
7 | | -} from "@substrate/light-client-extension-helpers/web-page" |
8 | | -import type { |
9 | | - Chain, |
10 | | - JsonRpcCallback, |
11 | | - SmoldotExtensionAPI, |
12 | | - WellKnownChain, |
13 | | -} from "@substrate/smoldot-discovery/types" |
| 4 | +import type { LightClientProvider, RawChain } from '@substrate/light-client-extension-helpers/web-page'; |
| 5 | +import type { Chain, JsonRpcCallback, SmoldotExtensionAPI, WellKnownChain } from '@substrate/smoldot-discovery/types'; |
14 | 6 |
|
15 | 7 | const wellKnownChainGenesisHashes: Record<string, string> = { |
16 | 8 | polkadot: |
17 | | - "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3", |
18 | | - ksmcc3: "0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe", |
| 9 | + '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3', |
| 10 | + ksmcc3: '0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe', |
19 | 11 | westend2: |
20 | | - "0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e", |
| 12 | + '0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e', |
21 | 13 | rococo_v2_2: |
22 | | - "0x6408de7737c59c238890533af25896a2c20608d8b380bb01029acb392781063e", |
23 | | -} |
| 14 | + '0x6408de7737c59c238890533af25896a2c20608d8b380bb01029acb392781063e' |
| 15 | +}; |
| 16 | + |
| 17 | +export type { LightClientProvider } from '@substrate/light-client-extension-helpers/web-page'; |
24 | 18 |
|
25 | 19 | export const createScClient = ( |
26 | | - lightClientProvider: LightClientProvider, |
| 20 | + lightClientProvider: LightClientProvider |
27 | 21 | ): SmoldotExtensionAPI => { |
28 | 22 | const internalAddChain = async ( |
29 | 23 | isWellKnown: boolean, |
30 | 24 | chainSpecOrWellKnownName: string, |
31 | 25 | jsonRpcCallback: JsonRpcCallback = () => {}, |
32 | | - relayChainGenesisHash?: string, |
| 26 | + relayChainGenesisHash?: string |
33 | 27 | ): Promise<Chain> => { |
34 | | - let chain: RawChain |
| 28 | + let chain: RawChain; |
| 29 | + |
35 | 30 | if (isWellKnown) { |
36 | 31 | const foundChain = Object.values(lightClientProvider.getChains()).find( |
37 | 32 | ({ genesisHash }) => |
38 | | - genesisHash === wellKnownChainGenesisHashes[chainSpecOrWellKnownName], |
39 | | - ) |
40 | | - if (!foundChain) throw new Error("Unknown well-known chain") |
41 | | - chain = foundChain |
| 33 | + genesisHash === wellKnownChainGenesisHashes[chainSpecOrWellKnownName] |
| 34 | + ); |
| 35 | + |
| 36 | + if (!foundChain) { |
| 37 | + throw new Error('Unknown well-known chain'); |
| 38 | + } |
| 39 | + |
| 40 | + chain = foundChain; |
42 | 41 | } else { |
43 | 42 | chain = await lightClientProvider.getChain( |
44 | 43 | chainSpecOrWellKnownName, |
45 | | - relayChainGenesisHash, |
46 | | - ) |
| 44 | + relayChainGenesisHash |
| 45 | + ); |
47 | 46 | } |
48 | 47 |
|
49 | | - const jsonRpcProvider = chain.connect(jsonRpcCallback) |
| 48 | + const jsonRpcProvider = chain.connect(jsonRpcCallback); |
50 | 49 |
|
51 | 50 | return { |
52 | | - sendJsonRpc(rpc: string): void { |
53 | | - jsonRpcProvider.send(rpc) |
| 51 | + sendJsonRpc (rpc: string): void { |
| 52 | + jsonRpcProvider.send(rpc); |
54 | 53 | }, |
55 | | - remove() { |
56 | | - jsonRpcProvider.disconnect() |
| 54 | + remove () { |
| 55 | + jsonRpcProvider.disconnect(); |
57 | 56 | }, |
58 | 57 | addChain: function ( |
59 | 58 | chainSpec: string, |
60 | | - jsonRpcCallback?: JsonRpcCallback | undefined, |
| 59 | + jsonRpcCallback?: JsonRpcCallback | undefined |
61 | 60 | ): Promise<Chain> { |
62 | 61 | return internalAddChain( |
63 | 62 | false, |
64 | 63 | chainSpec, |
65 | 64 | jsonRpcCallback, |
66 | | - chain.genesisHash, |
67 | | - ) |
68 | | - }, |
69 | | - } |
70 | | - } |
| 65 | + chain.genesisHash |
| 66 | + ); |
| 67 | + } |
| 68 | + }; |
| 69 | + }; |
71 | 70 |
|
72 | 71 | return { |
73 | 72 | addChain: (chainSpec: string, jsonRpcCallback?: JsonRpcCallback) => |
74 | 73 | internalAddChain(false, chainSpec, jsonRpcCallback), |
75 | 74 | addWellKnownChain: ( |
76 | 75 | name: WellKnownChain, |
77 | | - jsonRpcCallback?: JsonRpcCallback, |
78 | | - ) => internalAddChain(true, name, jsonRpcCallback), |
79 | | - } |
80 | | -} |
| 76 | + jsonRpcCallback?: JsonRpcCallback |
| 77 | + ) => internalAddChain(true, name, jsonRpcCallback) |
| 78 | + }; |
| 79 | +}; |
0 commit comments