Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
160d97c19b
|
|||
|
c7c23b7958
|
|||
|
2d6a9ad1b3
|
|||
|
|
18105d3ced
|
||
|
|
f2a1ab3a72
|
||
|
|
ec1399f3f3
|
@@ -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/"
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
},
|
||||
|
||||
@@ -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 && (
|
||||
|
||||
94
packages/ui-keyring/src/Keyring.dilithium.spec.ts
Normal file
94
packages/ui-keyring/src/Keyring.dilithium.spec.ts
Normal 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/);
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
@@ -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' };
|
||||
|
||||
52
yarn.lock
52
yarn.lock
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user