Allow ipns with chain specifier (#2868)
* Allow ipns with chain specifier * fallback check * Rename dns* to ipns*
This commit is contained in:
parent
62787d936e
commit
bea9e4f053
@ -6,24 +6,48 @@ import queryString from 'query-string';
|
||||
import store from 'store';
|
||||
import { createEndpoints } from '@polkadot/apps-config/settings';
|
||||
import { registry } from '@polkadot/react-api';
|
||||
import { extractIpfsDetails } from '@polkadot/react-hooks/useIpfs';
|
||||
import settings from '@polkadot/ui-settings';
|
||||
|
||||
// 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]);
|
||||
const stored = store.get('settings') as Record<string, unknown> || {};
|
||||
function getApiUrl (): string {
|
||||
// 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 (Array.isArray(urlOptions.rpc)) {
|
||||
throw new Error('Invalid WS endpoint specified');
|
||||
// if specified, this takes priority
|
||||
if (urlOptions.rpc) {
|
||||
if (Array.isArray(urlOptions.rpc)) {
|
||||
throw new Error('Invalid WS endpoint specified');
|
||||
}
|
||||
|
||||
return urlOptions.rpc.split('#')[0]; // https://polkadot.js.org/apps/?rpc=ws://127.0.0.1:9944#/explorer;
|
||||
}
|
||||
|
||||
const endpoints = createEndpoints(<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 option.value as string;
|
||||
}
|
||||
}
|
||||
|
||||
const stored = store.get('settings') as Record<string, unknown> || {};
|
||||
const fallbackUrl = endpoints.find(({ value }) => !!value);
|
||||
|
||||
// via settings, or the default chain
|
||||
return [stored.apiUrl, process.env.WS_URL].includes(settings.apiUrl)
|
||||
? settings.apiUrl // keep as-is
|
||||
: fallbackUrl
|
||||
? fallbackUrl.value as string // grab the fallback
|
||||
: 'ws://127.0.0.1:9944'; // nothing found, go local
|
||||
}
|
||||
|
||||
const fallbackUrl = createEndpoints(<T = string>(): T => ('' as unknown as T)).find(({ value }) => !!value) || { value: 'ws://127.0.0.1:9944' };
|
||||
const apiUrl = urlOptions.rpc // we have a supplied value
|
||||
? urlOptions.rpc.split('#')[0] // https://polkadot.js.org/apps/?rpc=ws://127.0.0.1:9944#/explorer
|
||||
: [stored.apiUrl, process.env.WS_URL].includes(settings.apiUrl) // overridden, or stored
|
||||
? settings.apiUrl // keep as-is
|
||||
: fallbackUrl.value as string; // grab the fallback
|
||||
const apiUrl = getApiUrl();
|
||||
|
||||
// set the default as retrieved here
|
||||
settings.set({ apiUrl });
|
||||
|
||||
@ -11,12 +11,11 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import keyring from '@polkadot/ui-keyring';
|
||||
import { getLedger, isLedger } from '@polkadot/react-api';
|
||||
import { useApi, useAccounts, useFavorites, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useAccounts, useFavorites, useIpfs, useToggle } from '@polkadot/react-hooks';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
import { Button, Input, Table } from '@polkadot/react-components';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import useIsIpfs from './useIsIpfs';
|
||||
import CreateModal from './modals/Create';
|
||||
import ImportModal from './modals/Import';
|
||||
import Multisig from './modals/MultisigCreate';
|
||||
@ -96,7 +95,7 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { allAccounts } = useAccounts();
|
||||
const isIpfs = useIsIpfs();
|
||||
const { isIpfs } = useIpfs();
|
||||
const [isCreateOpen, toggleCreate] = useToggle();
|
||||
const [isImportOpen, toggleImport] = useToggle();
|
||||
const [isMultisigOpen, toggleMultisig] = useToggle();
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/app-accounts authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
const KNOWN = ['/ipfs/', '/ipns/'];
|
||||
|
||||
export default function useIsIpfs (): boolean {
|
||||
const [isIpfs] = useState(KNOWN.some((part) => window.location.href.includes(part)));
|
||||
|
||||
return isIpfs;
|
||||
}
|
||||
@ -13,6 +13,7 @@ export { default as useFormField } from './useFormField';
|
||||
export { default as useDebounce } from './useDebounce';
|
||||
export { default as useFavorites } from './useFavorites';
|
||||
export { default as useIncrement } from './useIncrement';
|
||||
export { default as useIpfs } from './useIpfs';
|
||||
export { default as useIsMountedRef } from './useIsMountedRef';
|
||||
export { default as useMembers } from './useMembers';
|
||||
export { default as useModal } from './useModal';
|
||||
|
||||
68
packages/react-hooks/src/useIpfs.ts
Normal file
68
packages/react-hooks/src/useIpfs.ts
Normal file
@ -0,0 +1,68 @@
|
||||
// Copyright 2017-2020 @polkadot/app-accounts authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
const KNOWN = ['ipfs', 'ipns'];
|
||||
const SECTIONS = KNOWN.map((part) => `/${part}/`);
|
||||
|
||||
interface State {
|
||||
ipnsChain: string | null;
|
||||
ipnsDomain: string | null;
|
||||
ipfsHash: string | null;
|
||||
ipfsPath: string | null;
|
||||
isIpfs: boolean;
|
||||
isIpns: boolean;
|
||||
}
|
||||
|
||||
export function extractIpfsDetails (): State {
|
||||
// get url and check to see if we are ipfs/ipns
|
||||
const [url] = window.location.href.split('#');
|
||||
const isIpfs = SECTIONS.some((part) => url.includes(part));
|
||||
const isIpns = url.includes(SECTIONS[1]);
|
||||
|
||||
// individual sections, with the index of the ipfs portion
|
||||
const urlParts = url.split('/');
|
||||
const index = urlParts.indexOf(isIpns ? KNOWN[1] : KNOWN[0]);
|
||||
|
||||
// the parts of the path for ipfs re-construction
|
||||
let ipfsHash: string | null = null;
|
||||
let ipfsPath: string | null = null;
|
||||
let ipnsChain: string | null = null;
|
||||
let ipnsDomain: string | null = null;
|
||||
|
||||
// setup the ipfs part and dnslink domain (if available)
|
||||
if (index !== -1) {
|
||||
ipfsPath = urlParts.slice(0, index + 1).join('/');
|
||||
|
||||
if (isIpns) {
|
||||
const dnsLink = urlParts[index + 1];
|
||||
const linkParts = dnsLink.split('.');
|
||||
|
||||
if (linkParts.length > 2) {
|
||||
ipnsChain = linkParts[0];
|
||||
ipnsDomain = linkParts.slice(1).join('.');
|
||||
} else {
|
||||
ipnsDomain = dnsLink;
|
||||
}
|
||||
} else {
|
||||
ipfsHash = urlParts[index + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ipfsHash,
|
||||
ipfsPath,
|
||||
ipnsChain,
|
||||
ipnsDomain,
|
||||
isIpfs,
|
||||
isIpns
|
||||
};
|
||||
}
|
||||
|
||||
export default function useIpfs (): State {
|
||||
const [state] = useState(extractIpfsDetails());
|
||||
|
||||
return state;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user