Skip to content

Commit

Permalink
support wallet extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Leouarz committed Jul 24, 2023
1 parent 7b9db77 commit ba6d269
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
48 changes: 37 additions & 11 deletions packages/page-settings/src/Metadata/NetworkSpecs.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright 2017-2023 @polkadot/app-settings authors & contributors
// SPDX-License-Identifier: Apache-2.0

import store from 'store';
import type { NetworkSpecsStruct } from '@polkadot/ui-settings/types';
import type { ChainInfo, ChainType } from '../types.js';

import React, { useCallback, useEffect, useReducer, useRef, useState } from 'react';

import { ChainImg, Input, QrNetworkSpecs, Spinner, styled, Table } from '@polkadot/react-components';
import { Button, ChainImg, Input, QrNetworkSpecs, Spinner, styled, Table } from '@polkadot/react-components';
import { useApi, useDebounce } from '@polkadot/react-hooks';

import { useTranslation } from '../translate.js';
Expand Down Expand Up @@ -61,17 +62,21 @@ function NetworkSpecs ({ chainInfo, className }: Props): React.ReactElement<Prop
};

const [networkSpecs, setNetworkSpecs] = useReducer(reducer, initialState);
const [userExtensions, setUserExtensions] = useState<string[] | undefined>();

useEffect((): void => {
chainInfo && setNetworkSpecs({
chainType: chainInfo.chainType,
color: chainInfo.color || getRandomColor(),
decimals: chainInfo.tokenDecimals,
genesisHash: chainInfo.genesisHash,
prefix: chainInfo.ss58Format,
title: systemChain,
unit: chainInfo.tokenSymbol
});
if (chainInfo) {
setNetworkSpecs({
chainType: chainInfo.chainType,
color: chainInfo.color || getRandomColor(),
decimals: chainInfo.tokenDecimals,
genesisHash: chainInfo.genesisHash,
prefix: chainInfo.ss58Format,
title: systemChain,
unit: chainInfo.tokenSymbol,
});
setUserExtensions(chainInfo.userExtensions ? Object.keys(chainInfo.userExtensions) : undefined);
}
}, [chainInfo, systemChain]);

const _onChangeColor = useCallback(
Expand Down Expand Up @@ -120,7 +125,7 @@ function NetworkSpecs ({ chainInfo, className }: Props): React.ReactElement<Prop
<ChainImg className='settings--networkSpecs-logo' />
</div>
</td>
<td rowSpan={7}>
<td rowSpan={9}>
{qrData.genesisHash && (
<QrNetworkSpecs
className='settings--networkSpecs-qr'
Expand Down Expand Up @@ -205,6 +210,27 @@ function NetworkSpecs ({ chainInfo, className }: Props): React.ReactElement<Prop
/>
</td>
</tr>
<tr>
<td>
<Input
className='full'
isDisabled
label={t<string>('User extensions')}
value={userExtensions && userExtensions.length > 0 ? userExtensions.join(", ") : "No signed / user extensions found"}
/>
</td>
</tr>
<tr>
<td>
<Button.Group>
<Button
icon='trash'
label={t<string>('Reset extensions store')}
onClick={() => store.clearAll()}
/>
</Button.Group>
</td>
</tr>
</StyledTable>
);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/page-settings/src/useChainInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useMemo } from 'react';
import { getSystemIcon } from '@polkadot/apps-config';
import { DEFAULT_DECIMALS, DEFAULT_SS58 } from '@polkadot/react-api';
import { createNamedHook, useApi } from '@polkadot/react-hooks';
import { getSpecTypes } from '@polkadot/types-known';
import { getSpecTypes, getSpecExtensions } from '@polkadot/types-known';
import { formatBalance, isNumber } from '@polkadot/util';
import { base64Encode } from '@polkadot/util-crypto';

Expand All @@ -32,7 +32,8 @@ function useChainInfoImpl (): ChainInfo | null {
: DEFAULT_SS58.toNumber(),
tokenDecimals: (api.registry.chainDecimals || [DEFAULT_DECIMALS.toNumber()])[0],
tokenSymbol: (api.registry.chainTokens || formatBalance.getDefaults().unit)[0],
types: getSpecTypes(api.registry, systemChain, api.runtimeVersion.specName, api.runtimeVersion.specVersion) as unknown as Record<string, string>
types: getSpecTypes(api.registry, systemChain, api.runtimeVersion.specName, api.runtimeVersion.specVersion) as unknown as Record<string, string>,
userExtensions: getSpecExtensions(api.registry, systemChain, api.runtimeVersion.specName)
}
: null,
[api, apiEndpoint, isApiReady, specName, systemChain, systemName, isEthereum]
Expand Down
16 changes: 10 additions & 6 deletions packages/page-settings/src/useExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface ExtensionProperties {
tokenDecimals: number;
tokenSymbol: string;
ss58Format?: number;
userExtensionsLoaded?: boolean
}

interface SavedProperties {
Expand All @@ -45,15 +46,16 @@ function triggerAll (): void {
}

// save the properties for a specific extension
function saveProperties (api: ApiPromise, { name, version }: InjectedExtension): void {
function saveProperties (api: ApiPromise, { name, version }: InjectedExtension, hasLoadedUserExtensions: boolean): void {
const storeKey = `properties:${api.genesisHash.toHex()}`;
const allProperties = store.get(storeKey, {}) as SavedProperties;

allProperties[name] = {
extensionVersion: version,
ss58Format: api.registry.chainSS58,
tokenDecimals: api.registry.chainDecimals[0],
tokenSymbol: api.registry.chainTokens[0]
tokenSymbol: api.registry.chainTokens[0],
userExtensionsLoaded: hasLoadedUserExtensions,
};

store.set(storeKey, allProperties);
Expand All @@ -65,16 +67,17 @@ function hasCurrentProperties (api: ApiPromise, { extension }: ExtensionKnown):

// when we don't have properties yet, assume nothing has changed and store
if (!allProperties[extension.name]) {
saveProperties(api, extension);
saveProperties(api, extension, false);

return true;
}

const { ss58Format, tokenDecimals, tokenSymbol } = allProperties[extension.name];
const { ss58Format, tokenDecimals, tokenSymbol, userExtensionsLoaded } = allProperties[extension.name];

return ss58Format === api.registry.chainSS58 &&
tokenDecimals === api.registry.chainDecimals[0] &&
tokenSymbol === api.registry.chainTokens[0];
tokenSymbol === api.registry.chainTokens[0] &&
(tokenSymbol !== "AVL" || Boolean(userExtensionsLoaded));
}

// filter extensions based on the properties we have available
Expand Down Expand Up @@ -113,9 +116,10 @@ async function getExtensionInfo (api: ApiPromise, extension: InjectedExtension):

try {
isOk = await metadata.provide(def);
let hasLoadedUserExtensions = !!def.userExtensions

if (isOk) {
saveProperties(api, extension);
saveProperties(api, extension, hasLoadedUserExtensions);
triggerAll();
}
} catch {
Expand Down

0 comments on commit ba6d269

Please sign in to comment.