ghost-portal/packages/apps/src/initSettings.ts
Nikos Kontakis e202dda584
Integrate substrate connect with Apps (#5644)
* integrate apps with smoldot init

* Add a new category for substrate connect light clients to be grouped under. Add translations; Alter Api.tsx in order to choose between light clients and/or url addresses

* remove supportedLightClient array that was introduced. SubstrateConnect already has the information included and will return error if no supported network is found

* rollback 'hacky' changes in order to proceed with better implementation

* alter configuration of testing and production networks in order to introduce light client type

* upgrade substrate connect to 0.3.7

* implementation for integrating substrate-connect

* merge from latest master

* Add substrate/connect version to 0.3.10

* fix merge conflicts

* introduce function for correctly creating the provider URLs and update URLs

* Fix mistaken typos

* fix linter issue about rpc-provider@npm:^4.10.1 missing

* move substrate-connect for react-api to dependencies

* Fix lint issues

* Add tests for substrate-connect light clients urls and fix for ws

* Update packages/react-api/src/Api.tsx

Co-authored-by: Jaco <jacogr@gmail.com>

* Remove duplicated code

* add exceptions in jest config (transformIgnorePatterns)

* minor fixes

* fix typo errors

* minor yarn update

* Alter urls to string | union type (revert urls for json-rpc)

* fix erroneously changed url

* fix yarn.lock

* Stub mock for tests of substrate connect

* fix yarn

* Update yarn.lock

* Update yarn.lock

* Update packages/apps/src/Endpoints/index.tsx

Co-authored-by: Jaco <jacogr@gmail.com>

* Update packages/apps-config/src/endpoints/util.ts

Co-authored-by: Jaco <jacogr@gmail.com>

* Fix Pr comments

* Fix function that was recreated in every rerender

* Update packages/apps/src/Endpoints/index.tsx

Co-authored-by: Jaco <jacogr@gmail.com>

* Fix 2nd bundle of PR comments

* Fix function on PR comment

* filter light clients out of auto-selected sidebar networks

* Update package.json

Co-authored-by: Jaco <jacogr@gmail.com>

* Update packages/react-api/package.json

Co-authored-by: Jaco <jacogr@gmail.com>

* run install

* update yarn.lock from master

* sync yarn.lock from master

* Alter implementation to avoid white screen and make sure that API returned from substrate-connect works

* Update packages/page-accounts/src/CreateAccount.slow.spec.tsx

* Update packages/page-bounties/src/Bounties.slow.spec.tsx

Co-authored-by: Jaco <jacogr@gmail.com>
2021-07-12 06:51:21 +02:00

77 lines
2.8 KiB
TypeScript

// Copyright 2017-2021 @polkadot/apps authors & contributors
// SPDX-License-Identifier: Apache-2.0
import queryString from 'query-string';
import store from 'store';
import { createWsEndpoints } from '@polkadot/apps-config';
import { extractIpfsDetails } from '@polkadot/react-hooks/useIpfs';
import { settings } from '@polkadot/ui-settings';
import { Endpoint } from '@polkadot/ui-settings/types';
import { assert } from '@polkadot/util';
function networkOrUrl (apiType: Endpoint): void {
if (apiType.type === 'json-rpc') {
console.log('WS endpoint=', apiType.param);
} else if (apiType.type === 'substrate-connect') {
console.log('Chain of light client is =', apiType.param);
}
}
function getApiType (): Endpoint {
// we split here so that both these forms are allowed
// - http://localhost:3000/?rpc=wss://substrate-rpc.parity.io/#/explorer
// - http://localhost:3000/#/explorer?rpc=wss://substrate-rpc.parity.io
const urlOptions = queryString.parse(location.href.split('?')[1]);
// if specified, this takes priority
if (urlOptions.rpc) {
assert(!Array.isArray(urlOptions.rpc), 'Invalid WS endpoint specified');
// https://polkadot.js.org/apps/?rpc=ws://127.0.0.1:9944#/explorer;
const url = decodeURIComponent(urlOptions.rpc.split('#')[0]);
assert(url.startsWith('ws://') || url.startsWith('wss://'), 'Non-prefixed ws/wss url');
return { param: url, type: 'json-rpc' };
} else if (urlOptions.sc) {
assert(!Array.isArray(urlOptions.sc), 'Invalid network specified');
// https://polkadot.js.org/apps/?sc=kusama#/explorer;
const network = decodeURIComponent(urlOptions.sc.split('#')[0]);
const chain = network.split('-')[0];
return { param: chain, type: 'substrate-connect' };
}
const endpoints = createWsEndpoints(<T = string>(): T => ('' as unknown as T));
const { ipnsChain } = extractIpfsDetails();
// check against ipns domains (could be expanded to others)
if (ipnsChain) {
const option = endpoints.find(({ dnslink }) => dnslink === ipnsChain);
if (option) {
return { param: option.value, type: 'json-rpc' };
}
}
const stored = store.get('settings') as Record<string, unknown> || {};
const fallbackUrl = endpoints.find(({ value }) => !!value);
// via settings, or the default chain
return [stored.apiType, process.env.WS_URL].includes(settings.apiType)
? settings.apiType // keep as-is
: fallbackUrl
? { param: fallbackUrl.value, type: 'json-rpc' } // grab the fallback
: { param: 'ws://127.0.0.1:9944', type: 'json-rpc' }; // nothing found, go local
}
// There cannot be a Substrate Connect light client default (expect only jrpc EndpointType)
const apiType = getApiType();
// set the default as retrieved here
settings.set({ apiType });
networkOrUrl(apiType);