forked from PolkaGate/extension
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PolkaGate#696 from AMIRKHANEF/client
Add Light-client
- Loading branch information
Showing
12 changed files
with
22,954 additions
and
44 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/extension-polkagate/src/util/api/lightClient-connect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2019-2023 @polkadot/extension-polkagate authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import * as Sc from '@substrate/connect'; | ||
|
||
import { ApiPromise } from '@polkadot/api'; | ||
import { ScProvider } from '@polkadot/rpc-provider/substrate-connect'; | ||
|
||
const extractChainName = (networkEndpoint: string | undefined): string | undefined => { | ||
if (!networkEndpoint) { | ||
return; | ||
} | ||
|
||
const parts = networkEndpoint.split('/'); | ||
const chainName = parts[3]; // Assuming the chain name is always at index 3 | ||
const chainNameCapitalized = chainName.charAt(0).toUpperCase() + chainName.slice(1); | ||
|
||
return chainNameCapitalized; | ||
}; | ||
|
||
const chainSpec = (networkName: string | undefined) => { | ||
switch (networkName) { | ||
case 'Polkadot': | ||
return Sc.WellKnownChain.polkadot; | ||
case 'Kusama': | ||
return Sc.WellKnownChain.ksmcc3; | ||
case 'Westend': | ||
return Sc.WellKnownChain.westend2; | ||
default: | ||
return ''; | ||
} | ||
}; | ||
|
||
export default async function LCConnector(endpoint: string): Promise<ApiPromise> { | ||
const chainName = extractChainName(endpoint); | ||
const currentChainSpec = chainSpec(chainName); | ||
|
||
try { | ||
console.log('connecting through light client, endpoint:', endpoint); | ||
|
||
if (currentChainSpec.length) { | ||
const provider = new ScProvider(Sc, currentChainSpec); | ||
|
||
await provider.connect(); | ||
|
||
return await ApiPromise.create({ provider }); | ||
} | ||
|
||
return Promise.reject(new Error(`Unsupported network: ${chainName}`)); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
// Copyright 2019-2023 @polkadot/extension-polkagate authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/* eslint-disable header/header */ | ||
|
||
// import Memoize from 'memoize-one'; | ||
import memoize from 'memoizee'; | ||
/* eslint-disable header/header */ | ||
|
||
// var memoize = require("memoizee"); | ||
import { ApiPromise, WsProvider } from '@polkadot/api'; | ||
|
||
async function getApi(endpoint: string): Promise<ApiPromise> { | ||
const wsProvider = new WsProvider(endpoint); | ||
import LCConnector from './api/lightClient-connect'; | ||
|
||
async function getApi(endpoint: string): Promise<ApiPromise | undefined> { | ||
if (endpoint.startsWith('wss')) { | ||
const wsProvider = new WsProvider(endpoint); | ||
|
||
return await ApiPromise.create({ provider: wsProvider }); | ||
return await ApiPromise.create({ provider: wsProvider }); | ||
} else if (endpoint.startsWith('light')) { | ||
return await LCConnector(endpoint); | ||
} else { | ||
throw new Error(`Invalid endpoint: ${endpoint}`); | ||
} | ||
} | ||
|
||
// export default getApi; | ||
// export default memoize(getApi); | ||
export default getApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters