@oxy.so/core 7.29.0 → 7.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/api/identity.js +40 -1
- package/dist/cjs/crypto/index.js +11 -2
- package/dist/cjs/crypto/keyManager.js +69 -1
- package/dist/cjs/crypto/retiredKeys.js +371 -0
- package/dist/esm/api/identity.js +40 -1
- package/dist/esm/crypto/index.js +3 -0
- package/dist/esm/crypto/keyManager.js +69 -1
- package/dist/esm/crypto/retiredKeys.js +361 -0
- package/dist/types/api/identity.d.ts +10 -0
- package/dist/types/crypto/identitySigner.d.ts +23 -0
- package/dist/types/crypto/index.d.ts +2 -0
- package/dist/types/crypto/keyManager.d.ts +23 -1
- package/dist/types/crypto/retiredKeys.d.ts +116 -0
- package/package.json +3 -3
- package/src/api/identity.ts +46 -3
- package/src/crypto/identitySigner.ts +23 -0
- package/src/crypto/index.ts +11 -0
- package/src/crypto/keyManager.ts +81 -3
- package/src/crypto/retiredKeys.ts +453 -0
|
@@ -739,13 +739,21 @@ export declare class KeyManager {
|
|
|
739
739
|
* keychain group, or Commons' identity host. It never falls back to the
|
|
740
740
|
* personal key.
|
|
741
741
|
*
|
|
742
|
+
* With `opts.publicKey` the seed is derived from the key of that account
|
|
743
|
+
* (personal, or `accountId`'s) whose public key it names: the current key
|
|
744
|
+
* when it matches, else a RETIRED key Commons kept after a rotation or
|
|
745
|
+
* replacement (`./retiredKeys`, ADR 0061 D7 as amended). Spending only: a
|
|
746
|
+
* retired key never signs in. Never falls back to another key.
|
|
747
|
+
*
|
|
742
748
|
* @param info Context/domain-binding label (distinct labels → independent seeds).
|
|
743
749
|
* @param opts.accountId An organization's account id, for its key.
|
|
750
|
+
* @param opts.publicKey Selects one key of that account by its public key.
|
|
744
751
|
* @returns 32 bytes of derived keying material, or `null` on web / when no
|
|
745
752
|
* identity key is available to this app.
|
|
746
753
|
*/
|
|
747
754
|
static deriveScopedSeed(info: string, opts?: {
|
|
748
755
|
accountId?: string;
|
|
756
|
+
publicKey?: string;
|
|
749
757
|
}): Promise<Uint8Array | null>;
|
|
750
758
|
/**
|
|
751
759
|
* {@link deriveScopedSeed} over an ORGANIZATION key (ADR 0061 D6): Commons'
|
|
@@ -762,7 +770,8 @@ export declare class KeyManager {
|
|
|
762
770
|
* compressed public key for the scriptSig.
|
|
763
771
|
*
|
|
764
772
|
* Key source: as {@link deriveScopedSeed}, including `opts.accountId` for an
|
|
765
|
-
* organization's key (ADR 0061 D6).
|
|
773
|
+
* organization's key (ADR 0061 D6) and `opts.publicKey` for one key of the
|
|
774
|
+
* account, current or retired. On Android outside Commons the signature
|
|
766
775
|
* comes from Commons, which allows it for the wallet app only.
|
|
767
776
|
*
|
|
768
777
|
* @returns the signature, or `null` on web / when no identity key is
|
|
@@ -770,7 +779,20 @@ export declare class KeyManager {
|
|
|
770
779
|
*/
|
|
771
780
|
static signSocialReceive(index: number, digest: string, opts?: {
|
|
772
781
|
accountId?: string;
|
|
782
|
+
publicKey?: string;
|
|
773
783
|
}): Promise<CommonsSocialReceiveSignature | null>;
|
|
784
|
+
/**
|
|
785
|
+
* The private key of the account (personal, or `accountId`'s organization)
|
|
786
|
+
* whose public key is `publicKey`, from this app's own storage: the current
|
|
787
|
+
* key when it matches, else the retired ring. iOS and Commons only; `null`
|
|
788
|
+
* for a key this device does not hold. Never another key in its place. @internal
|
|
789
|
+
*/
|
|
790
|
+
/**
|
|
791
|
+
* The selector in the identity host's encoding (lowercase uncompressed), so a
|
|
792
|
+
* compressed `#key-1` from a DID or a gateway row selects the same key. @internal
|
|
793
|
+
*/
|
|
794
|
+
private static _selectorKey;
|
|
795
|
+
private static _selectedPrivateKey;
|
|
774
796
|
/**
|
|
775
797
|
* Get a shortened version of the public key for display
|
|
776
798
|
* Format: first 8 chars...last 8 chars
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retired identity keys: the keys an account HAD, kept on the device for
|
|
3
|
+
* spending only (oxy ADR 0061 D7 as amended 2026-11-06, Peable ADR 0002).
|
|
4
|
+
*
|
|
5
|
+
* The Peable wallet derives from the account's identity key (`deriveScopedSeed`
|
|
6
|
+
* for its HD wallet, `signSocialReceive` for its social-receive addresses), and
|
|
7
|
+
* coins keep arriving under a key after it is replaced: payments already
|
|
8
|
+
* broadcast settle against the key they were reserved under, and late payers
|
|
9
|
+
* and `@username` senders hold old addresses. So a personal rotation
|
|
10
|
+
* (`oxy.identity.rotateKey`) and an organization replacement keep the replaced
|
|
11
|
+
* private key here, in a ring per account — `personal`, and one per
|
|
12
|
+
* organization account id — instead of deleting it.
|
|
13
|
+
*
|
|
14
|
+
* A retired key is NEVER a way to sign in, publish, receive a new reservation or
|
|
15
|
+
* rotate: nothing here signs a challenge, and the identity host refuses
|
|
16
|
+
* `proveIdentity` / `describe` for one. It is reached exactly as the current key
|
|
17
|
+
* is, by derivation and never by export, through the `publicKey` selector of
|
|
18
|
+
* `KeyManager.deriveScopedSeed` / `signSocialReceive`:
|
|
19
|
+
*
|
|
20
|
+
* - iOS: the private key in the wallet keychain group (ADR 0062's
|
|
21
|
+
* `spending-key`: Commons and the Peable wallet only), beside the current
|
|
22
|
+
* private keys; the ring's index (public keys and dates only) in the same
|
|
23
|
+
* group, so the wallet can list it. Only Commons writes either.
|
|
24
|
+
* - Android: Commons' own expo-secure-store slot, mirrored into the identity
|
|
25
|
+
* signer store (`./identitySigner`) that Commons' identity host answers
|
|
26
|
+
* `keyPublicKey` requests from (wallet only). Every other app asks over IPC
|
|
27
|
+
* (`loadCommonsIdentityBridge`) and never gets the key.
|
|
28
|
+
*
|
|
29
|
+
* A retired phrase is not kept: the person wrote it down, and a phrase is a
|
|
30
|
+
* second copy of the key that buys the wallet nothing.
|
|
31
|
+
*
|
|
32
|
+
* A key enters the ring only from Commons: the key being replaced (rotation,
|
|
33
|
+
* organization replacement), or a previous recovery phrase the person adds,
|
|
34
|
+
* which Commons accepts only when Oxy's key history names it as one of the
|
|
35
|
+
* account's retired keys. Never into the current slot.
|
|
36
|
+
*
|
|
37
|
+
* Security: a retired key that was compromised is also the attacker's. The
|
|
38
|
+
* ring makes the coins under it spendable from Peable again; it cannot make
|
|
39
|
+
* them safe. A rotation forced by a compromise must be followed at once by the
|
|
40
|
+
* wallet's "move to your current key" sweep, which races the attacker.
|
|
41
|
+
*
|
|
42
|
+
* This module imports nothing from `./keyManager` (which imports it).
|
|
43
|
+
*/
|
|
44
|
+
/** Whose ring: the person's (`accountId` absent) or an organization's. */
|
|
45
|
+
export interface RetiredKeySelector {
|
|
46
|
+
/** An organization's account id; absent for the personal ring. */
|
|
47
|
+
accountId?: string;
|
|
48
|
+
}
|
|
49
|
+
/** One retired key as the ring lists it: public data only. */
|
|
50
|
+
export interface RetiredIdentityKey {
|
|
51
|
+
/** Lowercase uncompressed secp256k1 public key (`04…`, 130 hex). */
|
|
52
|
+
publicKey: string;
|
|
53
|
+
/** The organization it was the key of; absent for a personal root. */
|
|
54
|
+
accountId?: string;
|
|
55
|
+
/** Unix milliseconds when this device kept it. */
|
|
56
|
+
retiredAt: number;
|
|
57
|
+
}
|
|
58
|
+
/** A pair to keep in the ring. */
|
|
59
|
+
export interface RetiredKeyPair {
|
|
60
|
+
privateKey: string;
|
|
61
|
+
publicKey: string;
|
|
62
|
+
}
|
|
63
|
+
/** The ring a selector names: `personal`, or the organization's account id. */
|
|
64
|
+
export declare function retiredKeyScope(selector?: RetiredKeySelector): string | null;
|
|
65
|
+
/**
|
|
66
|
+
* Keep `key` in the ring of `selector`'s account. Commons only. Idempotent: a
|
|
67
|
+
* key already kept stays, with its first date. Refuses a pair whose public key
|
|
68
|
+
* does not derive from its private key. Resolves once every copy confirmed it
|
|
69
|
+
* (Android: the signer store's read-back too), and throws otherwise, so a
|
|
70
|
+
* caller about to replace the key can stop before anything is lost.
|
|
71
|
+
*/
|
|
72
|
+
export declare function retireIdentityKey(key: RetiredKeyPair, selector?: RetiredKeySelector): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* The retired keys of `selector`'s account this app can reach, oldest first.
|
|
75
|
+
* iOS and Commons: the ring's index. Android elsewhere: Commons' identity host
|
|
76
|
+
* (the wallet only), which leaves out the scope's current key. After an
|
|
77
|
+
* interrupted rotation the local list can include the current key: compare
|
|
78
|
+
* with the DID's `#key-1` before treating an entry as retired.
|
|
79
|
+
*/
|
|
80
|
+
export declare function listRetiredIdentityKeys(selector?: RetiredKeySelector): Promise<RetiredIdentityKey[]>;
|
|
81
|
+
/**
|
|
82
|
+
* The private half of a retired key, where this app may hold it: Commons (both
|
|
83
|
+
* platforms) and, on iOS, the keychain group. `null` on Android outside Commons
|
|
84
|
+
* (ask the identity host instead) and for a key not in the ring. Sign within
|
|
85
|
+
* the app only; never transmit it. @internal — `KeyManager` reads it for the
|
|
86
|
+
* `publicKey` selector.
|
|
87
|
+
*/
|
|
88
|
+
export declare function getRetiredPrivateKey(publicKey: string, selector?: RetiredKeySelector): Promise<string | null>;
|
|
89
|
+
/**
|
|
90
|
+
* Forget a retired key on this device: its private half, its signer copy and
|
|
91
|
+
* its index entry. Commons only, and only on the person's explicit request
|
|
92
|
+
* (their coins under it are then spendable only with its phrase again).
|
|
93
|
+
*/
|
|
94
|
+
export declare function forgetRetiredIdentityKey(publicKey: string, selector?: RetiredKeySelector): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Forget every retired key of one ring (the person's, or an organization's):
|
|
97
|
+
* called when the identity itself is deleted from this device, so a phone
|
|
98
|
+
* handed on keeps none of the person's old keys. Commons only. Resolves how
|
|
99
|
+
* many it removed.
|
|
100
|
+
*/
|
|
101
|
+
export declare function forgetRetiredIdentityKeys(selector?: RetiredKeySelector): Promise<number>;
|
|
102
|
+
/**
|
|
103
|
+
* Commons on Android: copy every retired key into the identity signer store
|
|
104
|
+
* again, so the identity host answers for it after that file was rebuilt (its
|
|
105
|
+
* self-heal leaves it empty). Call on every boot, beside `syncOrganizationKeys`.
|
|
106
|
+
* Resolves how many it mirrored. Elsewhere 0.
|
|
107
|
+
*/
|
|
108
|
+
export declare function syncRetiredKeys(): Promise<number>;
|
|
109
|
+
/**
|
|
110
|
+
* The pair a previous recovery phrase derives (`seed[0:32]`, the personal-root
|
|
111
|
+
* and organization-key derivation; 12–24 words). Stores nothing: the caller
|
|
112
|
+
* checks the public key against the account's key history (personal:
|
|
113
|
+
* `oxy.identity.keyHistory()`; organization: `oxy.accounts.identityKey.get`)
|
|
114
|
+
* and only then calls {@link retireIdentityKey}.
|
|
115
|
+
*/
|
|
116
|
+
export declare function retiredKeyFromPhrase(phrase: string): Promise<RetiredKeyPair>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oxy.so/core",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.30.0",
|
|
4
4
|
"description": "OxyHQ SDK Foundation — API client, authentication, cryptographic identity, and shared utilities",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -181,8 +181,8 @@
|
|
|
181
181
|
"dependencies": {
|
|
182
182
|
"@noble/ciphers": "^2.4.0",
|
|
183
183
|
"@noble/hashes": "^2.4.0",
|
|
184
|
-
"@oxy.so/contracts": "^6.
|
|
185
|
-
"@oxy.so/protocol": "^1.
|
|
184
|
+
"@oxy.so/contracts": "^6.12.0",
|
|
185
|
+
"@oxy.so/protocol": "^1.9.0",
|
|
186
186
|
"@oxy.so/telemetry": "^2.1.0",
|
|
187
187
|
"@scure/bip39": "^2.4.0",
|
|
188
188
|
"buffer": "^6.0.3",
|
package/src/api/identity.ts
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
identityLinkCreateResponseCheck,
|
|
24
24
|
identityLinkStateCheck,
|
|
25
25
|
} from '@oxy.so/contracts/client';
|
|
26
|
+
import { identityKeyHistoryCheck, type IdentityKeyHistory } from '@oxy.so/contracts/client';
|
|
26
27
|
import type {
|
|
27
28
|
AuthMethodsResponse,
|
|
28
29
|
BackupStatusResponse,
|
|
@@ -283,9 +284,8 @@ export class IdentityApi {
|
|
|
283
284
|
throw new Error('No authenticated user — sign in before rotating your key.');
|
|
284
285
|
}
|
|
285
286
|
|
|
286
|
-
const { KeyManager, RecoveryPhraseService, SignatureService, signMessage } =
|
|
287
|
-
'../crypto/index'
|
|
288
|
-
);
|
|
287
|
+
const { KeyManager, RecoveryPhraseService, SignatureService, signMessage, retireIdentityKey } =
|
|
288
|
+
await import('../crypto/index');
|
|
289
289
|
|
|
290
290
|
// 1. The NEW identity (in memory only). The UI may pre-derive + pre-show
|
|
291
291
|
// it and pass it back here so the phrase shown === the phrase committed.
|
|
@@ -296,6 +296,7 @@ export class IdentityApi {
|
|
|
296
296
|
// 2. Resolve the OLD signing capability from the chosen proof mode.
|
|
297
297
|
let oldPublicKey: string;
|
|
298
298
|
let signWithOldKey: (message: string) => Promise<string>;
|
|
299
|
+
let oldPrivateKeyForRing: string | null;
|
|
299
300
|
if (options.proof === 'phrase') {
|
|
300
301
|
const phrase = options.phrase?.trim();
|
|
301
302
|
if (!phrase) {
|
|
@@ -304,6 +305,7 @@ export class IdentityApi {
|
|
|
304
305
|
const oldPrivateKey = await RecoveryPhraseService.derivePrivateKeyFromPhrase(phrase);
|
|
305
306
|
oldPublicKey = KeyManager.derivePublicKey(oldPrivateKey);
|
|
306
307
|
signWithOldKey = (message) => signMessage(message, oldPrivateKey);
|
|
308
|
+
oldPrivateKeyForRing = oldPrivateKey;
|
|
307
309
|
} else {
|
|
308
310
|
const currentPublicKey = await KeyManager.getPublicKey();
|
|
309
311
|
if (!currentPublicKey) {
|
|
@@ -313,6 +315,30 @@ export class IdentityApi {
|
|
|
313
315
|
}
|
|
314
316
|
oldPublicKey = currentPublicKey;
|
|
315
317
|
signWithOldKey = (message) => SignatureService.sign(message);
|
|
318
|
+
oldPrivateKeyForRing = isWeb() ? null : await KeyManager.getPrivateKey();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// 2b. Keep the key being replaced in the retired ring BEFORE anything
|
|
322
|
+
// changes (oxy ADR 0061 D7 as amended, Peable ADR 0002): coins keep
|
|
323
|
+
// arriving under it after the swap, and the Peable wallet spends them
|
|
324
|
+
// through the `publicKey` selector of `deriveScopedSeed` /
|
|
325
|
+
// `signSocialReceive`. A device that cannot keep it does not rotate:
|
|
326
|
+
// the phrase would become the only way to those coins. Native only —
|
|
327
|
+
// on web no key is stored. Idempotent, so a retried rotation is fine.
|
|
328
|
+
if (!isWeb()) {
|
|
329
|
+
try {
|
|
330
|
+
if (!oldPrivateKeyForRing) throw new Error('no readable private key');
|
|
331
|
+
await retireIdentityKey({ privateKey: oldPrivateKeyForRing, publicKey: oldPublicKey });
|
|
332
|
+
} catch (retainError) {
|
|
333
|
+
logger.warn(
|
|
334
|
+
'Could not keep the current key as a retired key; not rotating.',
|
|
335
|
+
{ component: 'oxy.identity', method: 'rotateKey' },
|
|
336
|
+
retainError,
|
|
337
|
+
);
|
|
338
|
+
throw new Error(
|
|
339
|
+
'Your current key could not be kept on this device, so it was not rotated. Nothing changed.',
|
|
340
|
+
);
|
|
341
|
+
}
|
|
316
342
|
}
|
|
317
343
|
|
|
318
344
|
// 3. Request a single-use rotate_key challenge (bearer).
|
|
@@ -502,6 +528,23 @@ export class IdentityApi {
|
|
|
502
528
|
if (userId) keys.push(`GET:/u/${encodeURIComponent(userId)}/did.json`);
|
|
503
529
|
this.ctx.http.invalidateCache({ keys, prefixes: ['GET:/users/me'] });
|
|
504
530
|
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* The signed-in personal account's current root (`null` without one) and
|
|
534
|
+
* every root a rotation replaced, oldest first, all canonical (uncompressed,
|
|
535
|
+
* lowercase). Commons reads it to accept a previous recovery phrase only when
|
|
536
|
+
* it derives a key that WAS this account's root. A retired root is never any
|
|
537
|
+
* account's again. First-party only; a non-personal account is refused
|
|
538
|
+
* (`IDENTITY_NOT_PERSONAL_ACCOUNT`).
|
|
539
|
+
*/
|
|
540
|
+
async keyHistory(): Promise<IdentityKeyHistory> {
|
|
541
|
+
const res = await this.ctx.request<unknown>('GET', '/identity/key-history', undefined, {
|
|
542
|
+
cache: false,
|
|
543
|
+
});
|
|
544
|
+
const parsed = checkContract(identityKeyHistoryCheck, res);
|
|
545
|
+
if (!parsed) throw new Error('identity/key-history returned an unexpected response shape');
|
|
546
|
+
return parsed;
|
|
547
|
+
}
|
|
505
548
|
}
|
|
506
549
|
|
|
507
550
|
/** `oxy.identity.links` — joining Commons' root to an account (ADR 0029 D3). */
|
|
@@ -34,6 +34,29 @@ export interface IdentitySignerStore {
|
|
|
34
34
|
writeOrganization?(accountId: string, privateKey: string, publicKey: string): Promise<boolean>;
|
|
35
35
|
/** Drop that organization's pair. */
|
|
36
36
|
clearOrganization?(accountId: string): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* The RETIRED key ring: keys a scope rotated away from (a personal rotation,
|
|
39
|
+
* or an organization key replacement, ADR 0061 D7), kept so the identity host
|
|
40
|
+
* can still derive and sign with them for the wallet when a request names
|
|
41
|
+
* their public key. Spending only: the host never signs anyone in with one.
|
|
42
|
+
*
|
|
43
|
+
* `scope` is `'personal'` or an organization's account id (24-hex or uuid);
|
|
44
|
+
* `publicKey` is the retired key's lowercase uncompressed SEC1 hex (130),
|
|
45
|
+
* which names its entry. The native side refuses anything else. Clearing the
|
|
46
|
+
* personal pair ({@link clear}) or an organization's never drops a ring.
|
|
47
|
+
* Optional so a store built before retired keys still type-checks; Commons
|
|
48
|
+
* offers all four or none.
|
|
49
|
+
*/
|
|
50
|
+
readRetired?(
|
|
51
|
+
scope: string,
|
|
52
|
+
publicKey: string,
|
|
53
|
+
): Promise<{ privateKey: string; publicKey: string } | null>;
|
|
54
|
+
/** Keep a retired pair in `scope`'s ring. Resolves whether a read-back confirmed it. */
|
|
55
|
+
writeRetired?(scope: string, privateKey: string, publicKey: string): Promise<boolean>;
|
|
56
|
+
/** The public keys in `scope`'s ring (healthy entries only, sorted); `[]` when none. */
|
|
57
|
+
listRetired?(scope: string): Promise<string[]>;
|
|
58
|
+
/** Drop one retired key from `scope`'s ring. */
|
|
59
|
+
clearRetired?(scope: string, publicKey: string): Promise<void>;
|
|
37
60
|
}
|
|
38
61
|
|
|
39
62
|
/**
|
package/src/crypto/index.ts
CHANGED
|
@@ -74,6 +74,17 @@ export type {
|
|
|
74
74
|
KeychainGroupMigrationReport,
|
|
75
75
|
KeychainItemMigrationOutcome,
|
|
76
76
|
} from './keychainGroupMigration';
|
|
77
|
+
// Retired identity keys: kept for spending after a rotation or replacement
|
|
78
|
+
// (docs/adr/0061-organization-identity-key.md D7 as amended 2026-11-06)
|
|
79
|
+
export {
|
|
80
|
+
retireIdentityKey,
|
|
81
|
+
listRetiredIdentityKeys,
|
|
82
|
+
forgetRetiredIdentityKey,
|
|
83
|
+
forgetRetiredIdentityKeys,
|
|
84
|
+
syncRetiredKeys,
|
|
85
|
+
retiredKeyFromPhrase,
|
|
86
|
+
} from './retiredKeys';
|
|
87
|
+
export type { RetiredIdentityKey, RetiredKeyPair, RetiredKeySelector } from './retiredKeys';
|
|
77
88
|
export { SignatureService } from './signatureService';
|
|
78
89
|
export type { SignedMessage, AuthChallenge } from './signatureService';
|
|
79
90
|
export { RecoveryPhraseService, BACKUP_KDF_ENCRYPTION_INFO } from './recoveryPhrase';
|
package/src/crypto/keyManager.ts
CHANGED
|
@@ -32,6 +32,7 @@ import { isDev, logger } from '../logger';
|
|
|
32
32
|
import { deriveScopedSeedFromKey, signSocialReceiveDigest } from './identityDerivations';
|
|
33
33
|
import { type IdentitySignerStore, registerIdentitySignerStore } from './identitySigner';
|
|
34
34
|
import { getOrganizationPrivateKey, isOrganizationAccountId } from './organizationKeys';
|
|
35
|
+
import { forgetRetiredIdentityKeys, getRetiredPrivateKey, retiredKeyScope } from './retiredKeys';
|
|
35
36
|
import {
|
|
36
37
|
type KeychainItemClass,
|
|
37
38
|
groupedKeychainStore,
|
|
@@ -2611,6 +2612,16 @@ export class KeyManager {
|
|
|
2611
2612
|
if (force) {
|
|
2612
2613
|
await KeyManager._bestEffortDeleteBackupsAllGenerations(store);
|
|
2613
2614
|
await KeyManager._clearSharedSlot(store);
|
|
2615
|
+
// The person's retired keys go with the identity they belonged to.
|
|
2616
|
+
try {
|
|
2617
|
+
await forgetRetiredIdentityKeys();
|
|
2618
|
+
} catch (error) {
|
|
2619
|
+
logger.warn(
|
|
2620
|
+
'Failed to clear retired keys during delete',
|
|
2621
|
+
{ component: 'KeyManager' },
|
|
2622
|
+
error,
|
|
2623
|
+
);
|
|
2624
|
+
}
|
|
2614
2625
|
}
|
|
2615
2626
|
|
|
2616
2627
|
// The device backup goes on EVERY delete, forced or not: it is restored
|
|
@@ -3229,18 +3240,37 @@ export class KeyManager {
|
|
|
3229
3240
|
* keychain group, or Commons' identity host. It never falls back to the
|
|
3230
3241
|
* personal key.
|
|
3231
3242
|
*
|
|
3243
|
+
* With `opts.publicKey` the seed is derived from the key of that account
|
|
3244
|
+
* (personal, or `accountId`'s) whose public key it names: the current key
|
|
3245
|
+
* when it matches, else a RETIRED key Commons kept after a rotation or
|
|
3246
|
+
* replacement (`./retiredKeys`, ADR 0061 D7 as amended). Spending only: a
|
|
3247
|
+
* retired key never signs in. Never falls back to another key.
|
|
3248
|
+
*
|
|
3232
3249
|
* @param info Context/domain-binding label (distinct labels → independent seeds).
|
|
3233
3250
|
* @param opts.accountId An organization's account id, for its key.
|
|
3251
|
+
* @param opts.publicKey Selects one key of that account by its public key.
|
|
3234
3252
|
* @returns 32 bytes of derived keying material, or `null` on web / when no
|
|
3235
3253
|
* identity key is available to this app.
|
|
3236
3254
|
*/
|
|
3237
3255
|
static async deriveScopedSeed(
|
|
3238
3256
|
info: string,
|
|
3239
|
-
opts?: { accountId?: string },
|
|
3257
|
+
opts?: { accountId?: string; publicKey?: string },
|
|
3240
3258
|
): Promise<Uint8Array | null> {
|
|
3241
3259
|
if (isWebPlatform()) {
|
|
3242
3260
|
return null;
|
|
3243
3261
|
}
|
|
3262
|
+
if (opts?.publicKey !== undefined) {
|
|
3263
|
+
if (isAndroid() && !KeyManager.identitySignerStore) {
|
|
3264
|
+
const selected = KeyManager._selectorKey(opts.publicKey);
|
|
3265
|
+
if (!selected) return null;
|
|
3266
|
+
const bridge = await loadCommonsIdentityBridge();
|
|
3267
|
+
const seed =
|
|
3268
|
+
(await bridge?.deriveRetiredScopedSeed?.(info, selected, opts.accountId)) ?? null;
|
|
3269
|
+
return seed ? hexToBytes(seed) : null;
|
|
3270
|
+
}
|
|
3271
|
+
const selected = await KeyManager._selectedPrivateKey(opts.publicKey, opts.accountId);
|
|
3272
|
+
return selected ? deriveScopedSeedFromKey(selected, info) : null;
|
|
3273
|
+
}
|
|
3244
3274
|
if (opts?.accountId !== undefined) {
|
|
3245
3275
|
return KeyManager._deriveOrganizationScopedSeed(info, opts.accountId);
|
|
3246
3276
|
}
|
|
@@ -3292,7 +3322,8 @@ export class KeyManager {
|
|
|
3292
3322
|
* compressed public key for the scriptSig.
|
|
3293
3323
|
*
|
|
3294
3324
|
* Key source: as {@link deriveScopedSeed}, including `opts.accountId` for an
|
|
3295
|
-
* organization's key (ADR 0061 D6).
|
|
3325
|
+
* organization's key (ADR 0061 D6) and `opts.publicKey` for one key of the
|
|
3326
|
+
* account, current or retired. On Android outside Commons the signature
|
|
3296
3327
|
* comes from Commons, which allows it for the wallet app only.
|
|
3297
3328
|
*
|
|
3298
3329
|
* @returns the signature, or `null` on web / when no identity key is
|
|
@@ -3301,11 +3332,24 @@ export class KeyManager {
|
|
|
3301
3332
|
static async signSocialReceive(
|
|
3302
3333
|
index: number,
|
|
3303
3334
|
digest: string,
|
|
3304
|
-
opts?: { accountId?: string },
|
|
3335
|
+
opts?: { accountId?: string; publicKey?: string },
|
|
3305
3336
|
): Promise<CommonsSocialReceiveSignature | null> {
|
|
3306
3337
|
if (isWebPlatform()) {
|
|
3307
3338
|
return null;
|
|
3308
3339
|
}
|
|
3340
|
+
if (opts?.publicKey !== undefined) {
|
|
3341
|
+
if (isAndroid() && !KeyManager.identitySignerStore) {
|
|
3342
|
+
const selected = KeyManager._selectorKey(opts.publicKey);
|
|
3343
|
+
if (!selected) return null;
|
|
3344
|
+
const bridge = await loadCommonsIdentityBridge();
|
|
3345
|
+
return (
|
|
3346
|
+
(await bridge?.signRetiredSocialReceive?.(index, digest, selected, opts.accountId)) ??
|
|
3347
|
+
null
|
|
3348
|
+
);
|
|
3349
|
+
}
|
|
3350
|
+
const selected = await KeyManager._selectedPrivateKey(opts.publicKey, opts.accountId);
|
|
3351
|
+
return selected ? signSocialReceiveDigest(selected, index, digest) : null;
|
|
3352
|
+
}
|
|
3309
3353
|
const accountId = opts?.accountId;
|
|
3310
3354
|
if (accountId !== undefined) {
|
|
3311
3355
|
// An organization key (ADR 0061 D6); never the personal one in its place.
|
|
@@ -3330,6 +3374,40 @@ export class KeyManager {
|
|
|
3330
3374
|
return local ? signSocialReceiveDigest(local, index, digest) : null;
|
|
3331
3375
|
}
|
|
3332
3376
|
|
|
3377
|
+
/**
|
|
3378
|
+
* The private key of the account (personal, or `accountId`'s organization)
|
|
3379
|
+
* whose public key is `publicKey`, from this app's own storage: the current
|
|
3380
|
+
* key when it matches, else the retired ring. iOS and Commons only; `null`
|
|
3381
|
+
* for a key this device does not hold. Never another key in its place. @internal
|
|
3382
|
+
*/
|
|
3383
|
+
/**
|
|
3384
|
+
* The selector in the identity host's encoding (lowercase uncompressed), so a
|
|
3385
|
+
* compressed `#key-1` from a DID or a gateway row selects the same key. @internal
|
|
3386
|
+
*/
|
|
3387
|
+
private static _selectorKey(publicKey: string): string | null {
|
|
3388
|
+
return KeyManager.isValidPublicKey(publicKey) ? KeyManager.canonicalPublicKey(publicKey) : null;
|
|
3389
|
+
}
|
|
3390
|
+
|
|
3391
|
+
private static async _selectedPrivateKey(
|
|
3392
|
+
publicKey: string,
|
|
3393
|
+
accountId?: string,
|
|
3394
|
+
): Promise<string | null> {
|
|
3395
|
+
if (retiredKeyScope({ accountId }) === null || !KeyManager.isValidPublicKey(publicKey)) {
|
|
3396
|
+
return null;
|
|
3397
|
+
}
|
|
3398
|
+
const wanted = KeyManager.canonicalPublicKey(publicKey);
|
|
3399
|
+
const current =
|
|
3400
|
+
accountId !== undefined
|
|
3401
|
+
? await getOrganizationPrivateKey(accountId)
|
|
3402
|
+
: isAndroid()
|
|
3403
|
+
? await KeyManager.getPrivateKey()
|
|
3404
|
+
: ((await KeyManager.getSharedPrivateKey()) ?? (await KeyManager.getPrivateKey()));
|
|
3405
|
+
if (current && KeyManager.canonicalPublicKey(KeyManager.derivePublicKey(current)) === wanted) {
|
|
3406
|
+
return current;
|
|
3407
|
+
}
|
|
3408
|
+
return getRetiredPrivateKey(wanted, accountId !== undefined ? { accountId } : undefined);
|
|
3409
|
+
}
|
|
3410
|
+
|
|
3333
3411
|
/**
|
|
3334
3412
|
* Get a shortened version of the public key for display
|
|
3335
3413
|
* Format: first 8 chars...last 8 chars
|