Skip to content

Commit

Permalink
Display hex public key on address conversion (polkadot-js#9388)
Browse files Browse the repository at this point in the history
* Display hex public key on address conversion

* CHANGELOG

* lint

* Adjust imgConvert sorting
  • Loading branch information
jacogr authored and graysurf committed May 1, 2023
1 parent fa54434 commit e2a9a64
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## master

Changes:

- Display hex public key in address conversion utility
- Disable unreachable endpoints


## 0.129.1 Apr 30, 2023

Contributed:
Expand Down
4 changes: 2 additions & 2 deletions packages/apps-config/src/ui/logos/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export { chainsBitgreenPNG } from './generated/bitgreenPNG.js';
export { chainsBittensorPNG } from './generated/bittensorPNG.js';
export { chainsBrainstormPNG } from './generated/brainstormPNG.js';
export { chainsComposableFinancePNG } from './generated/composableFinancePNG.js';
export { chainsCreditcoinPNG } from './generated/creditcoinPNG.js';
export { chainsCreditcoinTestPNG } from './generated/creditcoin-testPNG.js';
export { chainsCreditcoinPNG } from './generated/creditcoinPNG.js';
export { chainsDebioSVG } from './generated/debioSVG.js';
export { chainsDorafactoryPNG } from './generated/dorafactoryPNG.js';
export { chainsDustyPNG } from './generated/dustyPNG.js';
Expand All @@ -41,8 +41,8 @@ export { chainsMangataxPNG } from './generated/mangataxPNG.js';
export { chainsMoonsamaPNG } from './generated/moonsamaPNG.js';
export { chainsMyriadPNG } from './generated/myriadPNG.js';
export { chainsOakPNG } from './generated/oakPNG.js';
export { chainsOrigintrailPNG } from './generated/origintrailPNG.js';
export { chainsOrigintrailTestnetPNG } from './generated/origintrail-testnetPNG.js';
export { chainsOrigintrailPNG } from './generated/origintrailPNG.js';
export { chainsPeaqPNG } from './generated/peaqPNG.js';
export { chainsPendulumSVG } from './generated/pendulumSVG.js';
export { chainsPicassoSVG } from './generated/picassoSVG.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/apps-config/src/ui/logos/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export { nodesAstarPNG } from './generated/astarPNG.js';
export { nodesAutomataPNG } from './generated/automataPNG.js';
export { nodesAventusSVG } from './generated/aventusSVG.js';
export { nodesBajunPNG } from './generated/bajunPNG.js';
export { nodesBasiliskPNG } from './generated/basiliskPNG.js';
export { nodesBasiliskRococoBgPNG } from './generated/basilisk-rococo-bgPNG.js';
export { nodesBasiliskPNG } from './generated/basiliskPNG.js';
export { nodesBeastSVG } from './generated/beastSVG.js';
export { nodesBifrostSVG } from './generated/bifrostSVG.js';
export { nodesBitcountryPNG } from './generated/bitcountryPNG.js';
Expand Down Expand Up @@ -75,8 +75,8 @@ export { nodesKulupuSVG } from './generated/kulupuSVG.js';
export { nodesKusariSVG } from './generated/kusariSVG.js';
export { nodesKylinPNG } from './generated/kylinPNG.js';
export { nodesLaminarCircleSVG } from './generated/laminar-circleSVG.js';
export { nodesLitentryPNG } from './generated/litentryPNG.js';
export { nodesLitentryRococoPNG } from './generated/litentry-rococoPNG.js';
export { nodesLitentryPNG } from './generated/litentryPNG.js';
export { nodesLitmusPNG } from './generated/litmusPNG.js';
export { nodesLoomNetworkPNG } from './generated/loom_networkPNG.js';
export { nodesMantaPNG } from './generated/mantaPNG.js';
Expand Down
51 changes: 32 additions & 19 deletions packages/page-utilities/src/Convert.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Copyright 2017-2023 @polkadot/app-utilities authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { HexString } from '@polkadot/util/types';

import React, { useCallback, useMemo, useState } from 'react';

import { createOption } from '@polkadot/app-settings/util';
import { createSs58 } from '@polkadot/apps-config';
import { allNetworks } from '@polkadot/networks';
import { Dropdown, InputAddressSimple, Static } from '@polkadot/react-components';
import { useApi } from '@polkadot/react-hooks';
import { formatNumber } from '@polkadot/util';
import { base58Decode, checkAddressChecksum, encodeAddress, isAddress } from '@polkadot/util-crypto';
import { formatNumber, u8aToHex } from '@polkadot/util';
import { base58Decode, checkAddressChecksum, decodeAddress, encodeAddress, isAddress } from '@polkadot/util-crypto';

import { useTranslation } from './translate.js';

Expand All @@ -18,32 +20,36 @@ interface Props {
}

interface State {
address: string | null;
inputSS58: number;
publicKey: HexString | null;
}

function getState (input: string | null): State {
let address: string | null = null;
let inputSS58 = 42;

if (input && isAddress(input)) {
const decoded = base58Decode(input);
const [,,, ss58Decoded] = checkAddressChecksum(decoded);

address = input;
inputSS58 = ss58Decoded;
try {
if (input && isAddress(input)) {
const decoded = base58Decode(input);
const [,,, inputSS58] = checkAddressChecksum(decoded);
const publicU8a = decodeAddress(input);

return {
inputSS58,
publicKey: u8aToHex(publicU8a)
};
}
} catch {
// ignore
}

return {
address,
inputSS58
inputSS58: 42,
publicKey: null
};
}

function Addresses ({ className }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { chainSS58 } = useApi();
const [{ address, inputSS58 }, setState] = useState<State>({ address: null, inputSS58: 42 });
const [{ inputSS58, publicKey }, setState] = useState<State>({ inputSS58: 42, publicKey: null });
const [prefix, setPrefix] = useState(-1);

const setAddress = useCallback(
Expand All @@ -69,16 +75,16 @@ function Addresses ({ className }: Props): React.ReactElement<Props> {
);

const converted = useMemo(
() => address && encodeAddress(address, prefix === -1 ? chainSS58 : prefix),
[address, chainSS58, prefix]
() => publicKey && encodeAddress(publicKey, prefix === -1 ? chainSS58 : prefix),
[chainSS58, prefix, publicKey]
);

return (
<div className={className}>
<div className='ui--row'>
<InputAddressSimple
autoFocus
isError={!address}
isError={!publicKey}
label={t<string>('address to convert')}
noConvert
onChange={setAddress}
Expand All @@ -92,7 +98,7 @@ function Addresses ({ className }: Props): React.ReactElement<Props> {
options={prefixOptions}
/>
</div>
{address && (
{publicKey && (
<>
<div className='ui--row'>
<Static
Expand All @@ -111,6 +117,13 @@ function Addresses ({ className }: Props): React.ReactElement<Props> {
/>
</div>
)}
<div className='ui--row'>
<Static
className='full'
label={t<string>('hex public key')}
value={publicKey}
/>
</div>
</>
)}
</div>
Expand Down
10 changes: 9 additions & 1 deletion scripts/imgConvert.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import path from 'node:path';
import { formatNumber, stringCamelCase } from '@polkadot/util';

const MAX_SIZE = 48 * 1024;

// FIXME The sorting here and the sorting from linting seems like a mismatch...
const HEADER = '// Copyright 2017-2023 @polkadot/apps authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\n// Do not edit. Auto-generated via node scripts/imgConvert.mjs\n\n';

const MIME = {
Expand Down Expand Up @@ -90,7 +92,13 @@ for (let dir of ['extensions', 'external', 'chains', 'nodes']) {
console.log('\n', notfound.length.toString().padStart(3), 'not referenced in', dir, '::\n\n\t', notfound.join(', '), '\n');
}

fs.writeFileSync(path.join(sub, 'index.ts'), `${HEADER}${Object.keys(result).sort().map((k) => `export { ${k} } from './${result[k]}.js';`).join('\n')}\n`);
fs.writeFileSync(path.join(sub, 'index.ts'), `${HEADER}${
Object
.keys(result)
.sort((a, b) => result[a].localeCompare(result[b]))
.map((k) => `export { ${k} } from './${result[k]}.js';`)
.join('\n')
}\n`);
}
}

Expand Down

0 comments on commit e2a9a64

Please sign in to comment.