diff --git a/packages/ui-settings/src/defaults/ss58.spec.ts b/packages/ui-settings/src/defaults/ss58.spec.ts new file mode 100644 index 00000000..d20ef9a8 --- /dev/null +++ b/packages/ui-settings/src/defaults/ss58.spec.ts @@ -0,0 +1,26 @@ +// Copyright 2017-2026 @pezkuwi/ui-settings authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +/// + +import { PREFIXES, prefixOptions } from './ss58.js'; + +describe('ss58 prefix options', (): void => { + it('gives each prefix one option, naming every network that uses it', (): void => { + expect(prefixOptions([ + { displayName: 'Pezkuwi Relay Chain', network: 'pezkuwi', prefix: 42 }, + { displayName: 'Zagros Relay Chain', network: 'zagros', prefix: 42 }, + { displayName: 'Other', network: 'other', prefix: 7 }, + { displayName: 'Bizinikiwi', network: 'bizinikiwi', prefix: 42 } + ])).toEqual([ + { info: 'pezkuwi', text: 'Pezkuwi Relay Chain / Zagros Relay Chain / Bizinikiwi', value: 42 }, + { info: 'other', text: 'Other', value: 7 } + ]); + }); + + it('has no two options with the same value', (): void => { + const values = PREFIXES.map(({ value }) => value); + + expect(new Set(values).size).toEqual(values.length); + }); +}); diff --git a/packages/ui-settings/src/defaults/ss58.ts b/packages/ui-settings/src/defaults/ss58.ts index 8c03fb8d..0ee868af 100644 --- a/packages/ui-settings/src/defaults/ss58.ts +++ b/packages/ui-settings/src/defaults/ss58.ts @@ -1,6 +1,7 @@ -// Copyright 2017-2025 @pezkuwi/ui-settings authors & contributors +// Copyright 2017-2026 @pezkuwi/ui-settings authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { Network } from '@pezkuwi/networks/types'; import type { Option } from '../types.js'; import { availableNetworks } from '@pezkuwi/networks'; @@ -13,10 +14,26 @@ const defaultNetwork: Option = { value: -1 }; -const networks = availableNetworks.map(({ displayName, network, prefix }) => ({ - info: network, - text: displayName, - value: prefix -})); +/** + * One option per address format. The value of an option is the ss58 prefix, + * so networks that share a prefix (Pezkuwi, Zagros and Bizinikiwi all use 42) + * are the same choice; they are listed together in its text instead of + * appearing as separate options with the same value. + */ +export function prefixOptions (networks: Pick[]): Option[] { + const byPrefix = new Map(); -export const PREFIXES: Option[] = [defaultNetwork, ...networks]; + for (const { displayName, network, prefix } of networks) { + const existing = byPrefix.get(prefix); + + if (existing) { + existing.text = `${existing.text} / ${displayName}`; + } else { + byPrefix.set(prefix, { info: network, text: displayName, value: prefix }); + } + } + + return [...byPrefix.values()]; +} + +export const PREFIXES: Option[] = [defaultNetwork, ...prefixOptions(availableNetworks)]; diff --git a/packages/ui-settings/tsconfig.build.json b/packages/ui-settings/tsconfig.build.json index da24d9f2..e69f4d94 100644 --- a/packages/ui-settings/tsconfig.build.json +++ b/packages/ui-settings/tsconfig.build.json @@ -5,5 +5,8 @@ "outDir": "./build", "rootDir": "./src" }, + "exclude": [ + "**/*.spec.ts" + ], "references": [] } diff --git a/packages/ui-settings/tsconfig.spec.json b/packages/ui-settings/tsconfig.spec.json new file mode 100644 index 00000000..90a1806a --- /dev/null +++ b/packages/ui-settings/tsconfig.spec.json @@ -0,0 +1,16 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "baseUrl": "..", + "outDir": "./build", + "rootDir": "./src", + "emitDeclarationOnly": false, + "noEmit": true + }, + "include": [ + "**/*.spec.ts" + ], + "references": [ + { "path": "../ui-settings/tsconfig.build.json" } + ] +} diff --git a/tsconfig.build.json b/tsconfig.build.json index e4129236..2fdfb34f 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -11,6 +11,7 @@ { "path": "./packages/ui-keyring/tsconfig.build.json" }, { "path": "./packages/ui-keyring/tsconfig.spec.json" }, { "path": "./packages/ui-settings/tsconfig.build.json" }, + { "path": "./packages/ui-settings/tsconfig.spec.json" }, { "path": "./packages/ui-shared/tsconfig.build.json" }, { "path": "./packages/ui-shared/tsconfig.spec.json" }, { "path": "./packages/vue-identicon/tsconfig.build.json" }