From efd60c01fef4c2bd759c37713d739f242ba62b2f Mon Sep 17 00:00:00 2001 From: jackkru69 Date: Sat, 4 Nov 2023 20:37:40 +0300 Subject: [PATCH] updated README, bootstrap from localStorage nodeList --- packages/tssdk/README.md | 9 +++++ packages/tssdk/src/libs/network/network.ts | 46 +++++++++++++++------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/packages/tssdk/README.md b/packages/tssdk/README.md index fc004423..fb4ecc2c 100644 --- a/packages/tssdk/README.md +++ b/packages/tssdk/README.md @@ -39,3 +39,12 @@ npm i @thepowereco/tssdk - [GitHub](https://github.com/thepower/power_hub/tree/master/packages/tssdk/src/libs) - [Report an issue](https://github.com/thepower/power_hub/issues) [The Power API Reference](https://doc.thepower.io/docs/Build/api/api-reference) + +## Custom chain + +If you need to join to custom chain you can override chain id and bootstrap nodes this way: + + localStorage.setItem("nodesList", JSON.stringify( + [{address:"nodeUrl", nodeId:"nodeId"}] + )) + localStorage.setItem("chainId", "chainId") \ No newline at end of file diff --git a/packages/tssdk/src/libs/network/network.ts b/packages/tssdk/src/libs/network/network.ts index d9266eac..42b63fc0 100755 --- a/packages/tssdk/src/libs/network/network.ts +++ b/packages/tssdk/src/libs/network/network.ts @@ -152,29 +152,45 @@ export class NetworkApi { } public bootstrap = async (isHTTPSNodesOnly = false) => { - const chainInfo = await NetworkApi.getChainGlobalConfig(); + if (typeof localStorage !== 'undefined' && localStorage !== null && localStorage.getItem('nodesList')) { + const stringifiedNodesList = localStorage.getItem('nodesList'); + const chainIdString = localStorage.getItem('chainId'); + const chainId = chainIdString ? +chainIdString : this.currentChain; + + if (stringifiedNodesList) { + this.currentChain = chainId; + const nodesList: ChainNode[] = JSON.parse(stringifiedNodesList); + await this.setCurrentConfig(nodesList); + info(`Bootstrapped chain ${chainId}`, nodesList); + await this.loadFeeGasSettings(); + } else { + throw new NoNodesFoundException(chainId); + } + } else { + const chainInfo = await NetworkApi.getChainGlobalConfig(); - const chainData = chainInfo.chains[this.currentChain]; + const chainData = chainInfo.chains[this.currentChain]; - if (chainData) { - this.isHTTPSNodesOnly = isHTTPSNodesOnly; + if (chainData) { + this.isHTTPSNodesOnly = isHTTPSNodesOnly; - const httpsRegExp = /^https:\/\//ig; + const httpsRegExp = /^https:\/\//ig; - const transformedNodeList = transformNodeList(chainData); - const nodesList = isHTTPSNodesOnly ? transformedNodeList.filter((node) => httpsRegExp.test(node.address)) : transformedNodeList; + const transformedNodeList = transformNodeList(chainData); + const nodesList = isHTTPSNodesOnly ? transformedNodeList.filter((node) => httpsRegExp.test(node.address)) : transformedNodeList; - if (!transformedNodeList.length) { - throw new NoNodesFoundException(this.currentChain); + if (!transformedNodeList.length) { + throw new NoNodesFoundException(this.currentChain); + } + + await this.setCurrentConfig(nodesList); + info(`Bootstrapped chain ${this.currentChain}`, this.currentNodes); + await this.loadFeeGasSettings(); + return; } - await this.setCurrentConfig(nodesList); - info(`Bootstrapped chain ${this.currentChain}`, this.currentNodes); - await this.loadFeeGasSettings(); - return; + throw new UnknownChainException(this.currentChain); } - - throw new UnknownChainException(this.currentChain); }; private async loadRemoteSCInterface(interfaceData: any[]) {