Allow extension metadata update (#2458)
* Allow extension metadata update * Counter + display * add icon * getSystemIcon import * Upgrade with types & calls * trigger displays * Bumps * Bump
This commit is contained in:
parent
f32494d097
commit
519b2f4848
@ -5,6 +5,13 @@
|
||||
import typesChain from './chain';
|
||||
import typesSpec from './spec';
|
||||
|
||||
export function getChainTypes (specName: string, chainName: string): Record<string, string | object> {
|
||||
return {
|
||||
...(typesSpec[specName as 'kusama'] || {}),
|
||||
...(typesChain[chainName as 'Westend'] || {})
|
||||
};
|
||||
}
|
||||
|
||||
export {
|
||||
typesChain,
|
||||
typesSpec
|
||||
|
||||
@ -2,9 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import chainTypes from './chain';
|
||||
import specTypes from './spec';
|
||||
|
||||
export function typesFromDefs (definitions: Record<string, { types: any }>): object {
|
||||
return Object
|
||||
.values(definitions)
|
||||
@ -13,10 +10,3 @@ export function typesFromDefs (definitions: Record<string, { types: any }>): obj
|
||||
...types
|
||||
}), {});
|
||||
}
|
||||
|
||||
export function getChainTypes (specName: string, chainName: string): object {
|
||||
return {
|
||||
...(specTypes[specName as 'kusama'] || {}),
|
||||
...(chainTypes[chainName as 'Westend'] || {})
|
||||
};
|
||||
}
|
||||
|
||||
9
packages/apps-config/src/ui/index.ts
Normal file
9
packages/apps-config/src/ui/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
// Copyright 2017-2020 @polkadot/apps-config 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 { identityNodes } from './identityIcons';
|
||||
|
||||
export function getSystemIcon (systemName: string): 'beachball' | 'polkadot' | 'substrate' {
|
||||
return (identityNodes[systemName.toLowerCase().replace(/-/g, ' ')] || 'substrate') as 'substrate';
|
||||
}
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
import { Routes } from './types';
|
||||
|
||||
import Settings from '@polkadot/app-settings';
|
||||
import Settings, { useCounter } from '@polkadot/app-settings';
|
||||
|
||||
export default ([
|
||||
{
|
||||
@ -14,6 +14,7 @@ export default ([
|
||||
defaultValue: 'Settings'
|
||||
},
|
||||
icon: 'settings',
|
||||
name: 'settings'
|
||||
name: 'settings',
|
||||
useCounter
|
||||
}
|
||||
] as Routes);
|
||||
|
||||
72
packages/page-settings/src/Extensions.tsx
Normal file
72
packages/page-settings/src/Extensions.tsx
Normal file
@ -0,0 +1,72 @@
|
||||
// Copyright 2017-2020 @polkadot/app-settings 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 React, { useCallback, useMemo, useState } from 'react';
|
||||
import { getChainTypes } from '@polkadot/apps-config/api';
|
||||
import { getSystemIcon } from '@polkadot/apps-config/ui';
|
||||
import { Button, Dropdown } from '@polkadot/react-components';
|
||||
import { useApi, useToggle } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from './translate';
|
||||
import useExtensions from './useExtensions';
|
||||
|
||||
function Extensions (): React.ReactElement {
|
||||
const { api, systemChain, systemName } = useApi();
|
||||
const { t } = useTranslation();
|
||||
const { extensions } = useExtensions();
|
||||
const [selectedIndex, setSelectedIndex] = useState(0);
|
||||
const [isBusy, toggleBusy] = useToggle();
|
||||
const options = useMemo(
|
||||
() => (extensions || []).map(({ extension: { name, version } }, value) => ({ text: `${name} ${version}`, value })),
|
||||
[extensions]
|
||||
);
|
||||
const _updateMeta = useCallback(
|
||||
(): void => {
|
||||
if (extensions && extensions[selectedIndex]) {
|
||||
toggleBusy();
|
||||
|
||||
extensions[selectedIndex]
|
||||
.update({
|
||||
chain: systemChain,
|
||||
genesisHash: api.genesisHash.toHex(),
|
||||
icon: getSystemIcon(systemName),
|
||||
metaCalls: Buffer.from(api.runtimeMetadata.asCallsOnly.toU8a()).toString('base64'),
|
||||
specVersion: api.runtimeVersion.specVersion.toNumber(),
|
||||
ss58Format: api.registry.chainSS58 || 42,
|
||||
tokenDecimals: api.registry.chainDecimals || 12,
|
||||
tokenSymbol: api.registry.chainToken || 'Unit',
|
||||
types: getChainTypes(api.runtimeVersion.specName.toString(), systemChain)
|
||||
})
|
||||
.catch(() => false)
|
||||
.then(() => toggleBusy());
|
||||
}
|
||||
},
|
||||
[api, extensions, selectedIndex, systemChain, systemName]
|
||||
);
|
||||
|
||||
if (!options.length) {
|
||||
return <div>{t('No upgradable extensions found')}</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Dropdown
|
||||
label={t('upgradable extensions')}
|
||||
onChange={setSelectedIndex}
|
||||
options={options}
|
||||
value={selectedIndex}
|
||||
/>
|
||||
<Button.Group>
|
||||
<Button
|
||||
icon='refresh'
|
||||
isDisabled={isBusy}
|
||||
label={t('Update metadata')}
|
||||
onClick={_updateMeta}
|
||||
/>
|
||||
</Button.Group>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Extensions);
|
||||
@ -6,20 +6,24 @@ import { AppProps as Props } from '@polkadot/react-components/types';
|
||||
|
||||
import React, { useMemo } from 'react';
|
||||
import { Route, Switch } from 'react-router';
|
||||
|
||||
import { HelpOverlay, Tabs } from '@polkadot/react-components';
|
||||
import uiSettings from '@polkadot/ui-settings';
|
||||
|
||||
import md from './md/basics.md';
|
||||
import { useTranslation } from './translate';
|
||||
import Developer from './Developer';
|
||||
import Extensions from './Extensions';
|
||||
import General from './General';
|
||||
import useCounter from './useCounter';
|
||||
|
||||
export { useCounter };
|
||||
|
||||
const hidden = uiSettings.uiMode === 'full'
|
||||
? []
|
||||
: ['developer'];
|
||||
|
||||
function SettingsApp ({ basePath, onStatusChange }: Props): React.ReactElement<Props> {
|
||||
const numExtensions = useCounter();
|
||||
const { t } = useTranslation();
|
||||
const items = useMemo(() => [
|
||||
{
|
||||
@ -27,11 +31,21 @@ function SettingsApp ({ basePath, onStatusChange }: Props): React.ReactElement<P
|
||||
name: 'general',
|
||||
text: t('General')
|
||||
},
|
||||
{
|
||||
name: 'extensions',
|
||||
text: t('Extensions {{count}}', {
|
||||
replace: {
|
||||
count: numExtensions
|
||||
? `(${numExtensions})`
|
||||
: ''
|
||||
}
|
||||
})
|
||||
},
|
||||
{
|
||||
name: 'developer',
|
||||
text: t('Developer')
|
||||
}
|
||||
], [t]);
|
||||
], [numExtensions, t]);
|
||||
|
||||
return (
|
||||
<main className='settings--App'>
|
||||
@ -50,6 +64,9 @@ function SettingsApp ({ basePath, onStatusChange }: Props): React.ReactElement<P
|
||||
onStatusChange={onStatusChange}
|
||||
/>
|
||||
</Route>
|
||||
<Route path={`${basePath}/extensions`}>
|
||||
<Extensions />
|
||||
</Route>
|
||||
<Route component={General} />
|
||||
</Switch>
|
||||
</main>
|
||||
|
||||
11
packages/page-settings/src/useCounter.ts
Normal file
11
packages/page-settings/src/useCounter.ts
Normal file
@ -0,0 +1,11 @@
|
||||
// Copyright 2017-2020 @polkadot/app-settings 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 useExtensions from './useExtensions';
|
||||
|
||||
export default function useCounter (): number {
|
||||
const { count } = useExtensions();
|
||||
|
||||
return count;
|
||||
}
|
||||
117
packages/page-settings/src/useExtensions.ts
Normal file
117
packages/page-settings/src/useExtensions.ts
Normal file
@ -0,0 +1,117 @@
|
||||
// Copyright 2017-2020 @polkadot/app-settings 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 { InjectedExtension, InjectedMetadataKnown, MetadataDef } from '@polkadot/extension-inject/types';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { ApiPromise } from '@polkadot/api';
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
|
||||
interface ExtensionKnown {
|
||||
extension: InjectedExtension;
|
||||
known: InjectedMetadataKnown[];
|
||||
update: (def: MetadataDef) => Promise<boolean>;
|
||||
}
|
||||
|
||||
interface ExtensionInfo extends ExtensionKnown {
|
||||
current: InjectedMetadataKnown | null;
|
||||
}
|
||||
|
||||
interface Extensions {
|
||||
count: number;
|
||||
extensions?: ExtensionInfo[];
|
||||
}
|
||||
|
||||
type TriggerFn = (counter: number) => void;
|
||||
|
||||
let triggerCount = 0;
|
||||
const triggers = new Map<string, TriggerFn>();
|
||||
|
||||
function triggerAll (): void {
|
||||
[...triggers.values()].forEach((trigger) => trigger(Date.now()));
|
||||
}
|
||||
|
||||
async function getExtensionInfo (extension: InjectedExtension): Promise<ExtensionKnown | null> {
|
||||
if (!extension.metadata) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const metadata = extension.metadata;
|
||||
const known = await metadata.get();
|
||||
|
||||
return {
|
||||
extension,
|
||||
known,
|
||||
update: async (def: MetadataDef): Promise<boolean> => {
|
||||
let result = false;
|
||||
|
||||
try {
|
||||
result = await metadata.provide(def);
|
||||
|
||||
triggerAll();
|
||||
} catch (error) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function getKnown (extensions: InjectedExtension[]): Promise<ExtensionKnown[]> {
|
||||
const all = await Promise.all(extensions.map(getExtensionInfo));
|
||||
|
||||
return all.filter((info): info is ExtensionKnown => !!info);
|
||||
}
|
||||
|
||||
function filterAll (api: ApiPromise, all: ExtensionKnown[]): Extensions {
|
||||
const extensions = all
|
||||
.map((info): ExtensionInfo | null => {
|
||||
const current = info.known.find(({ genesisHash }) => api.genesisHash.eq(genesisHash)) || null;
|
||||
const isUpgradable = !current || api.runtimeVersion.specVersion.gtn(current.specVersion);
|
||||
|
||||
return isUpgradable
|
||||
? { ...info, current }
|
||||
: null;
|
||||
})
|
||||
.filter((info): info is ExtensionInfo => !!info);
|
||||
|
||||
return {
|
||||
count: extensions.length,
|
||||
extensions
|
||||
};
|
||||
}
|
||||
|
||||
export default function useExtensions (): Extensions {
|
||||
const { api, isApiReady, isDevelopment, extensions } = useApi();
|
||||
const [all, setAll] = useState<ExtensionKnown[] | undefined>();
|
||||
const [state, setState] = useState<Extensions>({ count: 0 });
|
||||
const [trigger, setTrigger] = useState(0);
|
||||
|
||||
useEffect((): () => void => {
|
||||
const myId = `${++triggerCount}-${Date.now()}`;
|
||||
|
||||
triggers.set(myId, setTrigger);
|
||||
|
||||
return (): void => {
|
||||
triggers.delete(myId);
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect((): void => {
|
||||
extensions && getKnown(extensions).then(setAll);
|
||||
}, [extensions, trigger]);
|
||||
|
||||
useEffect((): void => {
|
||||
isApiReady && !isDevelopment && all && setState(
|
||||
filterAll(api, all)
|
||||
);
|
||||
}, [all, api, isApiReady, isDevelopment]);
|
||||
|
||||
return state;
|
||||
}
|
||||
@ -32,7 +32,7 @@
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.9.2",
|
||||
"@polkadot/api": "^1.9.0-beta.7",
|
||||
"@polkadot/extension-dapp": "^0.23.0-beta.9",
|
||||
"@polkadot/extension-dapp": "^0.23.0-beta.10",
|
||||
"rxjs-compat": "^6.5.4"
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,13 +2,14 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { InjectedExtension } from '@polkadot/extension-inject/types';
|
||||
import { ChainProperties } from '@polkadot/types/interfaces';
|
||||
import { ApiProps, ApiState } from './types';
|
||||
|
||||
import React, { useContext, useEffect, useMemo, useState } from 'react';
|
||||
import ApiPromise from '@polkadot/api/promise';
|
||||
import { typesChain, typesSpec } from '@polkadot/apps-config/api';
|
||||
import { isWeb3Injected, web3Accounts, web3Enable } from '@polkadot/extension-dapp';
|
||||
import { web3Accounts, web3Enable } from '@polkadot/extension-dapp';
|
||||
import { WsProvider } from '@polkadot/rpc-provider';
|
||||
import { StatusContext } from '@polkadot/react-components/Status';
|
||||
import { TokenUnit } from '@polkadot/react-components/InputNumber';
|
||||
@ -43,7 +44,7 @@ interface ChainData {
|
||||
systemVersion: string;
|
||||
}
|
||||
|
||||
const injectedPromise = new Promise((resolve): void => {
|
||||
const injectedPromise = new Promise<InjectedExtension[]>((resolve): void => {
|
||||
window.addEventListener('load', (): void => {
|
||||
resolve(web3Enable('polkadot-js/apps'));
|
||||
});
|
||||
@ -143,10 +144,10 @@ function Api ({ children, url }: Props): React.ReactElement<Props> | null {
|
||||
const [state, setState] = useState<ApiState>({ isApiReady: false } as unknown as ApiState);
|
||||
const [isApiConnected, setIsApiConnected] = useState(false);
|
||||
const [isApiInitialized, setIsApiInitialized] = useState(false);
|
||||
const [isWaitingInjected, setIsWaitingInjected] = useState(isWeb3Injected);
|
||||
const [extensions, setExtensions] = useState<InjectedExtension[] | undefined>();
|
||||
const props = useMemo<ApiProps>(
|
||||
() => ({ ...state, api, isApiConnected, isApiInitialized, isWaitingInjected }),
|
||||
[isApiConnected, isApiInitialized, isWaitingInjected, state]
|
||||
() => ({ ...state, api, extensions, isApiConnected, isApiInitialized, isWaitingInjected: !extensions }),
|
||||
[extensions, isApiConnected, isApiInitialized, state]
|
||||
);
|
||||
|
||||
// initial initialization
|
||||
@ -156,8 +157,8 @@ function Api ({ children, url }: Props): React.ReactElement<Props> | null {
|
||||
|
||||
api = new ApiPromise({ provider, registry, signer, typesChain, typesSpec });
|
||||
|
||||
api.on('connected', (): void => setIsApiConnected(true));
|
||||
api.on('disconnected', (): void => setIsApiConnected(false));
|
||||
api.on('connected', () => setIsApiConnected(true));
|
||||
api.on('disconnected', () => setIsApiConnected(false));
|
||||
api.on('ready', async (): Promise<void> => {
|
||||
try {
|
||||
setState(await loadOnReady(api));
|
||||
@ -167,8 +168,8 @@ function Api ({ children, url }: Props): React.ReactElement<Props> | null {
|
||||
});
|
||||
|
||||
injectedPromise
|
||||
.then((): void => setIsWaitingInjected(false))
|
||||
.catch((error: Error) => console.error(error));
|
||||
.then(setExtensions)
|
||||
.catch((error) => console.error(error));
|
||||
|
||||
setIsApiInitialized(true);
|
||||
}, []);
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { InjectedExtension } from '@polkadot/extension-inject/types';
|
||||
import { SubmittableExtrinsicFunction } from '@polkadot/api/promise/types';
|
||||
|
||||
import ApiPromise from '@polkadot/api/promise';
|
||||
@ -28,6 +29,7 @@ export interface ApiState {
|
||||
|
||||
export interface ApiProps extends ApiState {
|
||||
api: ApiPromise;
|
||||
extensions?: InjectedExtension[];
|
||||
isApiConnected: boolean;
|
||||
isApiInitialized: boolean;
|
||||
isWaitingInjected: boolean;
|
||||
|
||||
@ -6,7 +6,7 @@ import { DeriveAccountInfo } from '@polkadot/api-derive/types';
|
||||
import { IdentityProps as Props } from '@polkadot/react-identicon/types';
|
||||
|
||||
import React, { useContext, useEffect, useState } from 'react';
|
||||
import { identityNodes } from '@polkadot/apps-config/ui/identityIcons';
|
||||
import { getSystemIcon } from '@polkadot/apps-config/ui';
|
||||
import { useApi, useCall } from '@polkadot/react-hooks';
|
||||
import BaseIdentityIcon from '@polkadot/react-identicon';
|
||||
import uiSettings from '@polkadot/ui-settings';
|
||||
@ -15,8 +15,8 @@ import { ValidatorsContext } from '@polkadot/react-query';
|
||||
import StatusContext from './Status/Context';
|
||||
import { useTranslation } from './translate';
|
||||
|
||||
export function getIdentityTheme (systemName: string): 'empty' {
|
||||
return ((uiSettings.icon === 'default' && identityNodes[systemName.toLowerCase().replace(/-/g, ' ')]) || uiSettings.icon) as 'empty';
|
||||
export function getIdentityTheme (systemName: string): 'substrate' {
|
||||
return ((uiSettings.icon === 'default' && getSystemIcon(systemName)) || uiSettings.icon) as 'substrate';
|
||||
}
|
||||
|
||||
function IdentityIcon ({ className, onCopy, prefix, size, style, theme, value }: Props): React.ReactElement<Props> {
|
||||
|
||||
20
yarn.lock
20
yarn.lock
@ -3206,26 +3206,26 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/extension-dapp@npm:^0.23.0-beta.9":
|
||||
version: 0.23.0-beta.9
|
||||
resolution: "@polkadot/extension-dapp@npm:0.23.0-beta.9"
|
||||
"@polkadot/extension-dapp@npm:^0.23.0-beta.10":
|
||||
version: 0.23.0-beta.10
|
||||
resolution: "@polkadot/extension-dapp@npm:0.23.0-beta.10"
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.9.2
|
||||
"@polkadot/extension-inject": 0.23.0-beta.9
|
||||
"@polkadot/extension-inject": 0.23.0-beta.10
|
||||
peerDependencies:
|
||||
"@polkadot/api": "*"
|
||||
checksum: 2/38f13ee9e432e3ae579e3002e4b7f12291403a414fd993f6529bb316ceaa7659c3ba2ca265789ad23dd9ab0185fac9307bfe1c6a1a282f679b35e83d12da4bf6
|
||||
checksum: 2/169045bbdafeb323bd1fee010e43d3534b7bc90662ab9fedafdc6dd7b14ac41b08a9578cd61cfa330331ae1cf827baad5042644d433edbb67ca5e78659776f85
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/extension-inject@npm:0.23.0-beta.9":
|
||||
version: 0.23.0-beta.9
|
||||
resolution: "@polkadot/extension-inject@npm:0.23.0-beta.9"
|
||||
"@polkadot/extension-inject@npm:0.23.0-beta.10":
|
||||
version: 0.23.0-beta.10
|
||||
resolution: "@polkadot/extension-inject@npm:0.23.0-beta.10"
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.9.2
|
||||
peerDependencies:
|
||||
"@polkadot/api": "*"
|
||||
checksum: 2/08566bef15c971cd209313e6165d33513b960af3a73718b09217df04e5e2cfb4f194508ddde9f0b6f7b53ffb68b94dea3f1e9e4c7a9716fa1a44b6dd15be04fc
|
||||
checksum: 2/425672cf6e0d7ba3973f06f0b5cb863d297de6892eb4d4c4b175db90a7cf267d58c8210a54059d2ca56d8de50376778f160f71b32058d10d35124d2a299f3966
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@ -3259,7 +3259,7 @@ __metadata:
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.9.2
|
||||
"@polkadot/api": ^1.9.0-beta.7
|
||||
"@polkadot/extension-dapp": ^0.23.0-beta.9
|
||||
"@polkadot/extension-dapp": ^0.23.0-beta.10
|
||||
rxjs-compat: ^6.5.4
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
Loading…
Reference in New Issue
Block a user