Compare commits

..

6 Commits

Author SHA1 Message Date
160d97c19b build: declare @quantus/crypto as a ui-keyring dev dependency
Some checks failed
Lock Threads / lock (push) Has been cancelled
The ML-DSA restore spec imports contextForSpec from it, and polkadot-dev's
build refuses a package whose sources import something undeclared.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012uDUodEcRbBwNRi3UCmw8f
2026-09-16 21:13:56 +03:00
c7c23b7958 feat!: ui-keyring holds post-quantum keys only
Follows quantus/common#6, which removed ed25519, sr25519, ecdsa and ethereum
from the keyring (forks at 14.0.3-quantus.3).

- restoreAccount now goes through keyring.createFromJson instead of building
  the pair itself. A restored backup therefore gets the same refusal as every
  other entry point: a polkadot{.js} export of an sr25519 or ed25519 account is
  told "<type> keys are not quantum-safe and cannot be held here", not decoded.
- Version-0 JSON, which does not record its key type and so cannot be ML-DSA,
  is refused. Upstream guessed ed25519.
- isEthereum is always false; it's kept so callers written against upstream
  still read a boolean.
- loadAll already skips, with a warning, any stored account the keyring
  refuses, so a profile holding an old classical account still starts.

The spec that pinned sr25519 restore as unchanged is replaced by the two
refusals. ui-keyring 3.16.7-quantus.3, on the quantus.3 forks. yarn test 47
passing; lint clean.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012uDUodEcRbBwNRi3UCmw8f
2026-09-16 21:13:01 +03:00
2d6a9ad1b3 build: consume the forks at 14.0.3-quantus.2
Some checks failed
Lock Threads / lock (push) Has been cancelled
Picks up the networks fix (the quantum-safe gate moving out of the library list,
which made @polkadot/api importable again) and Heisenberg's corrected HEI symbol.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012uDUodEcRbBwNRi3UCmw8f
2026-09-15 14:15:02 +03:00
rob thijssen
18105d3ced build: pin the forked packages at 14.0.3-quantus.1
Follows the rename in quantus/common. The point is not tidiness: with the forks
numbered 14.0.3, a missing resolution here would have been satisfied from npm by
the unforked upstream package, installing cleanly and silently dropping Quantus
support. Now it fails with "No candidates found" — verified by removing the
@polkadot/networks resolution and running an install.

Refs quantus/extension#2

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012uDUodEcRbBwNRi3UCmw8f
2026-09-15 11:28:03 +03:00
rob thijssen
f2a1ab3a72 build: consume the forked @polkadot packages from the registry, drop the scaffolding
Removes every workaround the symlink approach needed, and reverts tsconfig to
upstream's exactly.

The three forked packages — @polkadot/{networks,util-crypto,keyring} — are now
pinned to Gitea tarball URLs in resolutions. @polkadot/util is not, because this
fork does not modify it; it comes from npm like the rest of the scope.

Tarball URLs rather than routing the @polkadot scope to Gitea, because the scope
is mostly *not* forked: routing it would send yarn looking for dozens of packages
that are not there. @quantus is routed as a whole scope in .yarnrc.yml, which is
safe because we own every name in it.

Four things go away, all of which existed for one reason — node resolves a portal
or link symlink to its realpath, after which @polkadot/util-crypto resolved inside
*common's* tree, where it is a workspace source directory with no exports map:

  - resolutions pointing four packages at ../common/packages/*/build
  - tsconfig.base.json `*.d.ts` path mappings, needed because NodeNext will not
    infer an extension for a bare path and looked for a `types` directory
  - tsconfig.json overriding those with a runtime `.js` view, because
    @polkadot/dev-ts reads that file and tried to import a declaration file as a
    module
  - --preserve-symlinks in the test scripts

Also drops fflate and @quantus/crypto from ui-keyring's devDependencies; they were
there only because `link:` does not install its target's dependencies.

`yarn lint` now reports 0 errors, down from 25 on pristine upstream. Those were
no-unsafe-assignment and no-unsafe-argument on values that were `any` purely
because the types did not resolve.

Refs quantus/extension#2, quantus/ui#1

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012uDUodEcRbBwNRi3UCmw8f
2026-09-15 11:08:43 +03:00
rob thijssen
ec1399f3f3 fix(ui-keyring): restoreAccount cannot rebuild an ML-DSA public key from an address
The mirror of the same mistake in keyring.createFromJson, fixed the same way.

restoreAccount passed `decodeAddress(json.address)` as the public key. For the
curve schemes that is correct — the address *is* the public key. An ML-DSA
account id is a one-way Poseidon2 hash, so this constructed a pair whose "public
key" was a hash of the public key. It did not throw; it produced a pair that was
simply wrong.

The public key is in `json.encoded`, still encrypted, and the pair is returned
*locked* with callers reading `pair.address` off it long before a password
appears. So it now passes an accountId — carried as data, used for the address
while locked, and checked against the real key once decodePkcs8 supplies one.
That check matters here as much as in the keyring: the address field sits outside
the encrypted blob, so a tampered one decodes cleanly and yields an account
displaying an address its key cannot sign for.

The 'ed25519' fallback for version-0 JSON is left alone. Those files predate the
crypto type being recorded and are by definition not Quantus, so the guess is
safe — but it is commented, because quantus/common#6 removes ed25519 from
KeypairType and this becomes a refusal.

Build plumbing, all of it dev-time only and all replaced by publishing these
packages to a registry (quantus/extension#2):

Resolutions point @polkadot/{keyring,util-crypto,networks,util} at the sibling
`common` checkout's build output, and @quantus/crypto at `wasm`.

tsconfig.base.json gains `*.d.ts` path mappings for them. Without these, node
resolves the portal symlink to its realpath and `@polkadot/util-crypto` then
resolves inside *common's* tree, where it is the workspace source directory with
no exports map — so EncryptedJson silently fails to resolve and KeyringPair$Json
loses `encoded` and `encoding`. The `*.d.ts` substitution has to come first
because NodeNext will not infer an extension for a bare path, and looks for a
`types` directory instead of `types.d.ts`.

tsconfig.json overrides those with the runtime `.js` view, because
@polkadot/dev-ts reads that file and would otherwise import a declaration file as
a module ("Debug Failure. Output generation failed"). tsc reads
tsconfig.build.json, which extends the base directly, so each gets the view it
needs.

The test script sets --preserve-symlinks, which yarn itself warns is required for
portals: without it the realpath problem above recurs at runtime.

Incidentally, `yarn lint` errors drop from 25 to 8 — resolving those types
removed a pile of `any`. The remaining 8 are upstream's own, in loadContract and
allowGenesis, and are left alone.

Refs quantus/ui#1

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012uDUodEcRbBwNRi3UCmw8f
2026-09-15 10:40:52 +03:00
8 changed files with 163 additions and 34 deletions

View File

@@ -13,3 +13,11 @@ logFilters:
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-4.5.1.cjs
# @quantus packages come from the Gitea registry — safe as a whole-scope route
# because we own every name in it. The forked @polkadot packages cannot be routed
# the same way (most of that scope is unforked and lives on npm), so they are
# pinned to tarball URLs in package.json resolutions instead.
npmScopes:
quantus:
npmRegistryServer: "https://git.lair.cafe/api/packages/quantus/npm/"

View File

@@ -42,6 +42,9 @@
"react-native": "^0.73.1"
},
"resolutions": {
"@polkadot/keyring": "https://git.lair.cafe/api/packages/quantus/npm/%40polkadot%2Fkeyring/-/14.0.3-quantus.3/keyring-14.0.3-quantus.3.tgz",
"@polkadot/networks": "https://git.lair.cafe/api/packages/quantus/npm/%40polkadot%2Fnetworks/-/14.0.3-quantus.3/networks-14.0.3-quantus.3.tgz",
"@polkadot/util-crypto": "https://git.lair.cafe/api/packages/quantus/npm/%40polkadot%2Futil-crypto/-/14.0.3-quantus.3/util-crypto-14.0.3-quantus.3.tgz",
"typescript": "5.5.4"
}
}

View File

@@ -17,19 +17,20 @@
"./packageDetect.cjs"
],
"type": "module",
"version": "3.16.7",
"version": "3.16.7-quantus.3",
"main": "index.js",
"dependencies": {
"@polkadot/keyring": "^14.0.3",
"@polkadot/keyring": "14.0.3-quantus.3",
"@polkadot/ui-settings": "3.16.7",
"@polkadot/util": "^14.0.3",
"@polkadot/util-crypto": "^14.0.3",
"@polkadot/util-crypto": "14.0.3-quantus.3",
"mkdirp": "^3.0.1",
"rxjs": "^7.8.1",
"store": "^2.0.12",
"tslib": "^2.8.1"
},
"devDependencies": {
"@quantus/crypto": "^0.3.0",
"@types/mkdirp": "^2.0.0",
"@types/store": "^2.0.5"
},

View File

@@ -125,8 +125,9 @@ export class Base {
this.setDevMode(options.isDevelopment);
}
// set Ethereum state
this.#isEthereum = keyring.type === 'ethereum';
// Never Ethereum: the keyring holds only ML-DSA keys (quantus/common#6). Kept
// as a property so callers written against upstream still read a boolean.
this.#isEthereum = false;
this.#keyring = keyring;
this._genesisHash = options.genesisHash && (

View File

@@ -0,0 +1,94 @@
// Copyright 2017-2026 @polkadot/ui-keyring authors & contributors
// SPDX-License-Identifier: Apache-2.0
/// <reference types="@polkadot/dev-test/globals.d.ts" />
import type { KeyringPair$Json } from '@polkadot/keyring/types';
import { contextForSpec } from '@quantus/crypto';
import { Keyring as BaseKeyring } from '@polkadot/keyring';
import { dilithiumVerify } from '@polkadot/util-crypto';
import { Keyring } from './Keyring.js';
// crystal_alice, from the chain's dev genesis — a raw 32-byte seed straight into
// ML-DSA-87 keygen. The address is what `quantus developer create-test-wallets`
// prints, i.e. an independent implementation.
const ALICE = 'qzk1Nxai3dZD9Cn5kwGcgL6mKxsfxwqdis7kDQJ52aJS2vSn7';
const PASSWORD = 'not a good password';
const MESSAGE = new Uint8Array([0x75, 0x69]);
function backup (): KeyringPair$Json {
const base = new BaseKeyring({ ss58Format: 189, type: 'dilithium87' });
return base
.addFromSeed(new Uint8Array(32), { name: 'crystal_alice' }, 'dilithium87')
.toJson(PASSWORD);
}
// `loadAll` initialises `@polkadot/ui-settings`, which is a module singleton, so
// it may be called once per *process* — not per instance. One keyring for the
// whole file, loaded once.
const uiKeyring = new Keyring();
uiKeyring.loadAll({ ss58Format: 189, store: undefined, type: 'dilithium87' });
describe('ui-keyring restoreAccount', (): void => {
// Before the fix this constructed a pair whose "public key" was
// decodeAddress(json.address) — i.e. a Poseidon2 hash of the public key, passed
// off as the key itself. It did not throw; it produced a pair that was simply
// wrong.
it('restores an ML-DSA account to the right address', (): void => {
const pair = uiKeyring.restoreAccount(backup(), PASSWORD);
expect(pair.address).toEqual(ALICE);
expect(pair.publicKey.length).toEqual(2592);
expect(pair.addressRaw.length).toEqual(32);
});
it('restores an account that can sign for its own address', (): void => {
const pair = uiKeyring.restoreAccount(backup(), PASSWORD);
pair.decodePkcs8(PASSWORD);
const context = contextForSpec(148);
const signature = pair.sign(MESSAGE, { context });
expect(dilithiumVerify(MESSAGE, signature, pair.addressRaw, 'dilithium87', context)).toEqual(true);
});
it('fails on a wrong password without half-restoring', (): void => {
expect(() => uiKeyring.restoreAccount(backup(), 'wrong')).toThrow();
});
// The address field sits outside the encrypted blob. For the curve schemes a
// tampered one cannot decode at all; here it decodes cleanly and would yield an
// account displaying an address its key does not control.
it('rejects JSON whose address does not match its key', (): void => {
const base = new BaseKeyring({ ss58Format: 189, type: 'dilithium87' });
const other = base.addFromSeed(new Uint8Array(32).fill(3), {}, 'dilithium87');
expect(() => uiKeyring.restoreAccount({ ...backup(), address: other.address }, PASSWORD)).toThrow(/does not match the address/);
});
// quantus/common#6: a backup of a quantum-unsafe key is refused with the
// reason, not decoded. This is the JSON a polkadot{.js} export of an ed25519
// account carries.
it('refuses a polkadot{.js} backup of a quantum-unsafe key, saying why', (): void => {
const json = {
address: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
encoded: '0x00',
encoding: { content: ['pkcs8', 'sr25519'], type: ['scrypt', 'xsalsa20-poly1305'], version: '3' },
meta: { name: 'polkadot account' }
} as unknown as KeyringPair$Json;
expect(() => uiKeyring.restoreAccount(json, PASSWORD)).toThrow(/sr25519 keys are not quantum-safe and cannot be held here/);
});
it('refuses a file that does not record its key type', (): void => {
const json = { ...backup(), encoding: { content: 'pkcs8', type: 'none', version: '0' } } as unknown as KeyringPair$Json;
expect(() => uiKeyring.restoreAccount(json, PASSWORD)).toThrow(/does not record its key type/);
});
});

View File

@@ -8,10 +8,9 @@ import type { KeypairType } from '@polkadot/util-crypto/types';
import type { AddressSubject, SingleAddress } from './observable/types.js';
import type { CreateResult, KeyringAddress, KeyringAddressType, KeyringItemType, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringPairs$Json, KeyringStruct } from './types.js';
import { createPair } from '@polkadot/keyring';
import { chains } from '@polkadot/ui-settings';
import { bnToBn, hexToU8a, isFunction, isHex, isString, objectSpread, stringify, stringToU8a, u8aSorted, u8aToString } from '@polkadot/util';
import { base64Decode, createKeyMulti, jsonDecrypt, jsonEncrypt } from '@polkadot/util-crypto';
import { createKeyMulti, jsonDecrypt, jsonEncrypt } from '@polkadot/util-crypto';
import { env } from './observable/env.js';
import { KeyringOption } from './options/index.js';
@@ -21,7 +20,7 @@ import { accountKey, accountRegex, addressKey, addressRegex, contractKey, contra
const RECENT_EXPIRY = 24 * 60 * 60;
// No accounts (or test accounts) should be loaded until after the chain determination.
// Chain determination occurs outside of Keyring. Loading `keyring.loadAll({ type: 'ed25519' | 'sr25519' })` is triggered
// Chain determination occurs outside of Keyring. Loading `keyring.loadAll({ type: 'dilithium65' | 'dilithium87' })` is triggered
// from the API after the chain is received
export class Keyring extends Base implements KeyringStruct {
public readonly keyringOption = new KeyringOption();
@@ -327,15 +326,18 @@ export class Keyring extends Base implements KeyringStruct {
}
public restoreAccount (json: KeyringPair$Json, password: string): KeyringPair {
const cryptoType = Array.isArray(json.encoding.content) ? json.encoding.content[1] : 'ed25519';
const encType = Array.isArray(json.encoding.type) ? json.encoding.type : [json.encoding.type];
const pair = createPair(
{ toSS58: this.encodeAddress, type: cryptoType as KeypairType },
{ publicKey: this.decodeAddress(json.address, true) },
json.meta,
isHex(json.encoded) ? hexToU8a(json.encoded) : base64Decode(json.encoded),
encType
);
// Version-0 JSON predates recording the key type, so it cannot be an ML-DSA
// account. Upstream guessed ed25519; this keyring cannot hold one.
if (!Array.isArray(json.encoding.content)) {
throw new Error('This account file does not record its key type, so it is not an ML-DSA account and cannot be restored here');
}
// Through the keyring rather than createPair directly, so a restore gets the
// same refusal as every other entry point: a polkadot{.js} backup of an
// sr25519 or ed25519 account is told it is not quantum-safe, not decoded.
// createFromJson also carries the address as an account id until the real
// public key is decrypted, and checks the two agree when it is.
const pair = this.keyring.createFromJson(json, true);
// unlock, save account and then lock (locking cleans secretKey, so needs to be last)
pair.decodePkcs8(password);

View File

@@ -3,4 +3,4 @@
// Do not edit, auto-generated by @polkadot/dev
export const packageInfo = { name: '@polkadot/ui-keyring', path: 'auto', type: 'auto', version: '3.16.7' };
export const packageInfo = { name: '@polkadot/ui-keyring', path: 'auto', type: 'auto', version: '3.16.7-quantus.3' };

View File

@@ -1596,28 +1596,29 @@ __metadata:
languageName: node
linkType: hard
"@polkadot/keyring@npm:^14.0.3":
version: 14.0.3
resolution: "@polkadot/keyring@npm:14.0.3"
"@polkadot/keyring@https://git.lair.cafe/api/packages/quantus/npm/%40polkadot%2Fkeyring/-/14.0.3-quantus.3/keyring-14.0.3-quantus.3.tgz":
version: 14.0.3-quantus.3
resolution: "@polkadot/keyring@https://git.lair.cafe/api/packages/quantus/npm/%40polkadot%2Fkeyring/-/14.0.3-quantus.3/keyring-14.0.3-quantus.3.tgz"
dependencies:
"@polkadot/util": "npm:14.0.3"
"@polkadot/util-crypto": "npm:14.0.3"
"@polkadot/util-crypto": "npm:14.0.3-quantus.3"
"@quantus/crypto": "npm:^0.3.0"
tslib: "npm:^2.8.0"
peerDependencies:
"@polkadot/util": 14.0.3
"@polkadot/util-crypto": 14.0.3
checksum: 10/69f9f776363f8327d72b43794262ae709fc2824182637e499ed6e9ca94315645d78005bf1f25bdfb7305e5d79879cb932c114e6612467ddf21a760117834e8a2
checksum: 10/2264245c4fc2505c852d5a874bda2fcd86a1269cb6545f7bb578a6efdf58beea3deccf28d27ba6e41f9fdf9a8ea50cf230c626b69afe775a7629e069149164f7
languageName: node
linkType: hard
"@polkadot/networks@npm:14.0.3, @polkadot/networks@npm:^14.0.3":
version: 14.0.3
resolution: "@polkadot/networks@npm:14.0.3"
"@polkadot/networks@https://git.lair.cafe/api/packages/quantus/npm/%40polkadot%2Fnetworks/-/14.0.3-quantus.3/networks-14.0.3-quantus.3.tgz":
version: 14.0.3-quantus.3
resolution: "@polkadot/networks@https://git.lair.cafe/api/packages/quantus/npm/%40polkadot%2Fnetworks/-/14.0.3-quantus.3/networks-14.0.3-quantus.3.tgz"
dependencies:
"@polkadot/util": "npm:14.0.3"
"@substrate/ss58-registry": "npm:^1.51.0"
tslib: "npm:^2.8.0"
checksum: 10/eb006f537f103b0d417e52966d0098b528326d1ebbae84e4c7834627bb3e863b7b849856992aa58c4a0aeb0ed1e1838a9619aeba7610d0e7c75e99ffcc6c9ecd
checksum: 10/04228325c8472901fc04e5570e6d3450539c4ee51bbbae5c6eea945c721dc86b4bd9a581503e9f65c0132317ecc3c915b7f2c4cc2774b169f49d6eb2524bd8c2
languageName: node
linkType: hard
@@ -1691,10 +1692,11 @@ __metadata:
version: 0.0.0-use.local
resolution: "@polkadot/ui-keyring@workspace:packages/ui-keyring"
dependencies:
"@polkadot/keyring": "npm:^14.0.3"
"@polkadot/keyring": "npm:14.0.3-quantus.3"
"@polkadot/ui-settings": "npm:3.16.7"
"@polkadot/util": "npm:^14.0.3"
"@polkadot/util-crypto": "npm:^14.0.3"
"@polkadot/util-crypto": "npm:14.0.3-quantus.3"
"@quantus/crypto": "npm:^0.3.0"
"@types/mkdirp": "npm:^2.0.0"
"@types/store": "npm:^2.0.5"
mkdirp: "npm:^3.0.1"
@@ -1740,24 +1742,25 @@ __metadata:
languageName: unknown
linkType: soft
"@polkadot/util-crypto@npm:14.0.3, @polkadot/util-crypto@npm:^14.0.3":
version: 14.0.3
resolution: "@polkadot/util-crypto@npm:14.0.3"
"@polkadot/util-crypto@https://git.lair.cafe/api/packages/quantus/npm/%40polkadot%2Futil-crypto/-/14.0.3-quantus.3/util-crypto-14.0.3-quantus.3.tgz":
version: 14.0.3-quantus.3
resolution: "@polkadot/util-crypto@https://git.lair.cafe/api/packages/quantus/npm/%40polkadot%2Futil-crypto/-/14.0.3-quantus.3/util-crypto-14.0.3-quantus.3.tgz"
dependencies:
"@noble/curves": "npm:^1.3.0"
"@noble/hashes": "npm:^1.3.3"
"@polkadot/networks": "npm:14.0.3"
"@polkadot/networks": "npm:14.0.3-quantus.3"
"@polkadot/util": "npm:14.0.3"
"@polkadot/wasm-crypto": "npm:^7.5.3"
"@polkadot/wasm-util": "npm:^7.5.3"
"@polkadot/x-bigint": "npm:14.0.3"
"@polkadot/x-randomvalues": "npm:14.0.3"
"@quantus/crypto": "npm:^0.3.0"
"@scure/base": "npm:^1.1.7"
"@scure/sr25519": "npm:^0.2.0"
tslib: "npm:^2.8.0"
peerDependencies:
"@polkadot/util": 14.0.3
checksum: 10/e8f2da806cb81d3c014415bdd633f0fc5871132ce790ca892f65899010386d64fa25f7c047574cc96402afa03b5ff77e4dff904e69b90e714a7150e18ef0f507
checksum: 10/ffbbb1a45a45abf51376276674e223b1adbbb9355b8881811c558d428ca84e6d07370d11ff07329cf5c32032b4c710a955b84c02462b6c4f0dc52b5f1117e17e
languageName: node
linkType: hard
@@ -1936,6 +1939,16 @@ __metadata:
languageName: node
linkType: hard
"@quantus/crypto@npm:^0.3.0":
version: 0.3.0
resolution: "@quantus/crypto@npm:0.3.0::__archiveUrl=https%3A%2F%2Fgit.lair.cafe%2Fapi%2Fpackages%2Fquantus%2Fnpm%2F%2540quantus%252Fcrypto%2F-%2F0.3.0%2Fcrypto-0.3.0.tgz"
dependencies:
fflate: "npm:^0.8.2"
tslib: "npm:^2.7.0"
checksum: 10/fa40dbe5206d1ff28038c35825f65f113e4ccf503b3e0126c1d67c7645081c3855b972dfd7e6d76a142b73b1dbfc8947a22354d587b09515f0acf065d6507f13
languageName: node
linkType: hard
"@react-native-community/cli-clean@npm:12.3.0":
version: 12.3.0
resolution: "@react-native-community/cli-clean@npm:12.3.0"
@@ -6436,6 +6449,13 @@ __metadata:
languageName: node
linkType: hard
"fflate@npm:^0.8.2":
version: 0.8.3
resolution: "fflate@npm:0.8.3"
checksum: 10/6ebf528dc9c56e78e715eac615b009b25dc33e15c1920b11ebba44e6d76181c647756a81a23e19247907496b93aa99928514c53090579a65109e026ac2824aa7
languageName: node
linkType: hard
"figures@npm:^3.0.0":
version: 3.2.0
resolution: "figures@npm:3.2.0"