@oxy.so/core 7.30.0 → 7.32.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 +146 -23
- package/dist/cjs/crypto/identityMarker.js +15 -0
- package/dist/cjs/crypto/index.js +7 -2
- package/dist/cjs/crypto/keyManager.js +7 -10
- package/dist/cjs/crypto/keychainGroupMigration.js +327 -160
- package/dist/cjs/crypto/organizationKeys.js +33 -5
- package/dist/cjs/crypto/retiredKeys.js +80 -18
- package/dist/cjs/session/authStateStore.js +13 -0
- package/dist/cjs/session/index.js +5 -2
- package/dist/cjs/utils/keychainAccessGroups.js +208 -51
- package/dist/esm/api/identity.js +146 -23
- package/dist/esm/crypto/identityMarker.js +14 -0
- package/dist/esm/crypto/index.js +3 -3
- package/dist/esm/crypto/keyManager.js +9 -12
- package/dist/esm/crypto/keychainGroupMigration.js +327 -162
- package/dist/esm/crypto/organizationKeys.js +34 -6
- package/dist/esm/crypto/retiredKeys.js +76 -17
- package/dist/esm/session/authStateStore.js +12 -0
- package/dist/esm/session/index.js +1 -1
- package/dist/esm/utils/keychainAccessGroups.js +205 -51
- package/dist/types/api/identity.d.ts +36 -0
- package/dist/types/crypto/identityMarker.d.ts +8 -0
- package/dist/types/crypto/index.d.ts +4 -4
- package/dist/types/crypto/keychainGroupMigration.d.ts +28 -6
- package/dist/types/crypto/organizationKeys.d.ts +4 -2
- package/dist/types/crypto/retiredKeys.d.ts +30 -4
- package/dist/types/session/authStateStore.d.ts +9 -0
- package/dist/types/session/index.d.ts +2 -2
- package/dist/types/utils/keychainAccessGroups.d.ts +108 -26
- package/package.json +2 -2
- package/src/api/identity.ts +185 -40
- package/src/crypto/identityMarker.ts +15 -0
- package/src/crypto/index.ts +10 -1
- package/src/crypto/keyManager.ts +9 -15
- package/src/crypto/keychainGroupMigration.ts +396 -186
- package/src/crypto/organizationKeys.ts +38 -6
- package/src/crypto/retiredKeys.ts +87 -17
- package/src/session/authStateStore.ts +14 -0
- package/src/session/index.ts +8 -1
- package/src/utils/keychainAccessGroups.ts +276 -52
|
@@ -1,63 +1,110 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Commons, iOS: move every identity item out of the shared keychain group
|
|
3
|
-
* into the group it belongs to (docs/identity/ios-keychain-access-groups.md
|
|
4
|
-
* "Migrating existing items", ADR 0062).
|
|
3
|
+
* into the group it belongs to, ONCE (docs/identity/ios-keychain-access-groups.md
|
|
4
|
+
* "Migrating existing items", ADR 0062; round-4 review R4-6, R4-7).
|
|
5
5
|
*
|
|
6
6
|
* Before the split Commons listed one access group, the shared one, so every
|
|
7
7
|
* item it wrote landed there — the root key, both phrases, organization keys.
|
|
8
|
-
*
|
|
8
|
+
* But every Oxy app can WRITE that group too, and keychain items survive an
|
|
9
|
+
* uninstall. So a value found there is adopted only when Commons can prove it
|
|
10
|
+
* wrote it:
|
|
9
11
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* - **Provenance.** Commons' identity marker (`./identityMarker`) lives in its
|
|
13
|
+
* own sandbox (AsyncStorage): no other app can write it and an uninstall
|
|
14
|
+
* removes it. A personal key is adopted only when it IS the key that marker
|
|
15
|
+
* records; a phrase only when it derives it. Organization items are adopted
|
|
16
|
+
* only on such a proven device (before the split every Oxy app could already
|
|
17
|
+
* read and replace them, so a proven upgrade gains nothing by doubting them).
|
|
18
|
+
* No marker — a fresh install, a reinstall, a device that never ran a
|
|
19
|
+
* marker-aware Commons — adopts NOTHING from the shared group, ever.
|
|
20
|
+
* - **One shot.** When a run settles, a completion record is written to the
|
|
21
|
+
* identity group (Commons only) and mirrored into the wallet group (for the
|
|
22
|
+
* Peable wallet). With it, no later run looks at the shared group and no
|
|
23
|
+
* read falls back to it (`setHolderSharedSecretFallback`,
|
|
24
|
+
* `closeWalletSharedSecretFallback`).
|
|
25
|
+
* - **A different key is retired, then deleted (R4-6).** On a proven device a
|
|
26
|
+
* shared copy that is a valid key but not the one being kept (a replaced
|
|
27
|
+
* root, an old backup slot, an organization's previous key, a phrase of
|
|
28
|
+
* either) is kept in the retired ring (`./retiredKeys`, spending only), read
|
|
29
|
+
* back, and only then deleted from the shared group. Nothing that could
|
|
30
|
+
* hold money is destroyed, and nothing stays where every app can read it.
|
|
15
31
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* the
|
|
32
|
+
* Each adopted item moves by VERIFY-BEFORE-DELETE: read it from the shared
|
|
33
|
+
* group, write it to its group, read it back (a pair must be healthy, a value
|
|
34
|
+
* byte-identical), and only then delete the shared copy. A failure at any step
|
|
35
|
+
* leaves the shared copy; the run is `incomplete`, no record is written, and
|
|
36
|
+
* on a proven device Commons' reads may still fall back to the shared group
|
|
37
|
+
* for that launch; the next launch resumes.
|
|
21
38
|
*
|
|
22
|
-
*
|
|
23
|
-
* -
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* after the split, so it is current, but the shared copy may be a replaced
|
|
27
|
-
* key that still receives money, and no step here may destroy one.
|
|
28
|
-
* - a pair that is not healthy → `unhealthy`: left in place, never moved.
|
|
29
|
-
*
|
|
30
|
-
* Every read the rest of the SDK makes goes through `keychainReadPlan` (its
|
|
31
|
-
* group, then the shared group), so it finds an item wherever this run left it.
|
|
32
|
-
* `ensureKeychainGroupsMigrated` is awaited by every KeyManager and
|
|
33
|
-
* organization-key storage access in Commons, so no write of theirs can
|
|
34
|
-
* interleave with a copy here (a copy racing a rotation could otherwise put
|
|
35
|
-
* the replaced key back).
|
|
36
|
-
*
|
|
37
|
-
* Nothing is logged but item labels and outcomes; never a value.
|
|
39
|
+
* `ensureKeychainGroupsMigrated` is awaited by every KeyManager, organization
|
|
40
|
+
* key and retired-ring access in Commons, so no write of theirs can interleave
|
|
41
|
+
* with a copy here. Nothing is logged but item labels and outcomes; never a
|
|
42
|
+
* value.
|
|
38
43
|
*/
|
|
39
|
-
import { deriveSecp256k1PublicKey, normalizeSecp256k1PrivateKey, normalizeSecp256k1PublicKey, } from '@oxy.so/protocol/secp256k1';
|
|
44
|
+
import { deriveSecp256k1PublicKey, isValidSecp256k1PrivateKey, normalizeSecp256k1PrivateKey, normalizeSecp256k1PublicKey, } from '@oxy.so/protocol/secp256k1';
|
|
40
45
|
import { loadSecureStore } from '@oxy.so/protocol';
|
|
46
|
+
import { mnemonicToSeed, validateMnemonic } from '@scure/bip39';
|
|
47
|
+
import { wordlist } from '@scure/bip39/wordlists/english.js';
|
|
41
48
|
import { logger } from '../logger/index.js';
|
|
42
|
-
import { keychainAccessGroupValue, keychainGroupFor, resolveKeychainAccess, } from '../utils/keychainAccessGroups.js';
|
|
49
|
+
import { closeWalletSharedSecretFallback, keychainAccessGroupValue, keychainGroupFor, resolveKeychainAccess, setHolderSharedSecretFallback, } from '../utils/keychainAccessGroups.js';
|
|
50
|
+
import { readIdentityMarkerStrict } from './identityMarker.js';
|
|
43
51
|
import { ORGANIZATION_INDEX_KEYCHAIN_SERVICE, ORGANIZATION_INDEX_STORAGE_KEY, ORGANIZATION_MNEMONIC_KEYCHAIN_SERVICE, RECOVERY_MNEMONIC_KEYCHAIN_SERVICE, RECOVERY_MNEMONIC_STORAGE_KEY, STORAGE_KEYS, V2_BACKUP_KEYCHAIN_SERVICE, V2_PRIMARY_KEYCHAIN_SERVICE, V2_STORAGE_KEYS, organizationMnemonicName, organizationPrivateKeyName, organizationPublicKeyName, parseOrganizationIndex, serializeOrganizationIndex, } from './keychainItems.js';
|
|
52
|
+
import { retireIdentityKeyDuringMigration } from './retiredKeys.js';
|
|
53
|
+
/**
|
|
54
|
+
* The completion record (identity group) and its mirror (wallet group). The
|
|
55
|
+
* record also remembers whether the CURRENT personal key, and which
|
|
56
|
+
* organization keys, were found in the shared group: every Oxy iOS app could
|
|
57
|
+
* read those before the split ({@link getPreSplitExposure}).
|
|
58
|
+
*/
|
|
59
|
+
const COMPLETION_KEY = 'oxy_keychain_group_migration_v1';
|
|
60
|
+
const COMPLETION_SERVICE = 'oxy_keychain_group_migration';
|
|
61
|
+
function parseRecord(raw) {
|
|
62
|
+
if (!raw)
|
|
63
|
+
return null;
|
|
64
|
+
try {
|
|
65
|
+
const parsed = JSON.parse(raw);
|
|
66
|
+
return parsed && parsed.v === 1 && typeof parsed.completedAt === 'number'
|
|
67
|
+
? parsed
|
|
68
|
+
: null;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
44
74
|
const slot = (key, item, service, deviceOnly = true) => ({
|
|
45
75
|
key,
|
|
46
76
|
service,
|
|
47
77
|
group: keychainGroupFor(item),
|
|
48
78
|
deviceOnly,
|
|
49
79
|
});
|
|
50
|
-
function
|
|
51
|
-
if (!privateKey || !
|
|
52
|
-
return
|
|
80
|
+
function publicKeyOf(privateKey) {
|
|
81
|
+
if (!privateKey || !isValidSecp256k1PrivateKey(privateKey))
|
|
82
|
+
return null;
|
|
53
83
|
try {
|
|
54
|
-
return
|
|
55
|
-
normalizeSecp256k1PublicKey(publicKey));
|
|
84
|
+
return deriveSecp256k1PublicKey(normalizeSecp256k1PrivateKey(privateKey));
|
|
56
85
|
}
|
|
57
86
|
catch {
|
|
58
|
-
return
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function canonicalPublic(publicKey) {
|
|
91
|
+
try {
|
|
92
|
+
return normalizeSecp256k1PublicKey(publicKey.trim());
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return null;
|
|
59
96
|
}
|
|
60
97
|
}
|
|
98
|
+
/** The pair a phrase derives (`seed[0:32]`), or `null` for anything that is not a phrase. */
|
|
99
|
+
async function pairOfPhrase(phrase) {
|
|
100
|
+
const normalized = phrase.trim().toLowerCase().split(/\s+/).join(' ');
|
|
101
|
+
if (!validateMnemonic(normalized, wordlist))
|
|
102
|
+
return null;
|
|
103
|
+
const seed = await mnemonicToSeed(normalized);
|
|
104
|
+
const privateKey = Array.from(seed.subarray(0, 32), (b) => b.toString(16).padStart(2, '0')).join('');
|
|
105
|
+
const publicKey = publicKeyOf(privateKey);
|
|
106
|
+
return publicKey ? { privateKey, publicKey } : null;
|
|
107
|
+
}
|
|
61
108
|
class Deferred extends Error {
|
|
62
109
|
}
|
|
63
110
|
class Mover {
|
|
@@ -83,153 +130,169 @@ class Mover {
|
|
|
83
130
|
throw new Deferred(error instanceof Error ? error.message : 'keychain read threw');
|
|
84
131
|
}
|
|
85
132
|
}
|
|
86
|
-
/**
|
|
87
|
-
|
|
88
|
-
* only when the new group holds exactly `value`. A copy that reads back as
|
|
89
|
-
* something else is removed again (best effort), because reads look in the
|
|
90
|
-
* new group FIRST and must not find it there.
|
|
91
|
-
*/
|
|
92
|
-
async copy(s, value, replacing = false) {
|
|
133
|
+
/** Write `value` to the slot's group and read it back; `true` only when it holds exactly `value`. */
|
|
134
|
+
async write(s, value) {
|
|
93
135
|
try {
|
|
94
136
|
await this.store.setItemAsync(s.key, value, this.options(s, s.group, true));
|
|
95
|
-
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
137
|
+
return (await this.store.getItemAsync(s.key, this.options(s, s.group))) === value;
|
|
98
138
|
}
|
|
99
139
|
catch {
|
|
100
140
|
return false;
|
|
101
141
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Write `value` where the slot's group had nothing, then read it back. A
|
|
145
|
+
* copy that reads back as something else is removed again (best effort):
|
|
146
|
+
* reads look in the slot's group first and must not find it there.
|
|
147
|
+
*/
|
|
148
|
+
async copy(s, value) {
|
|
149
|
+
if (await this.write(s, value))
|
|
150
|
+
return true;
|
|
106
151
|
try {
|
|
107
152
|
await this.store.deleteItemAsync(s.key, this.options(s, s.group));
|
|
108
153
|
}
|
|
109
154
|
catch {
|
|
110
|
-
// The
|
|
155
|
+
// The shared copy is intact; the next run tries again.
|
|
111
156
|
}
|
|
112
157
|
return false;
|
|
113
158
|
}
|
|
114
|
-
async deleteShared(
|
|
159
|
+
async deleteShared(slots) {
|
|
115
160
|
try {
|
|
116
|
-
|
|
161
|
+
for (const s of slots)
|
|
162
|
+
await this.store.deleteItemAsync(s.key, this.options(s, 'shared'));
|
|
117
163
|
return true;
|
|
118
164
|
}
|
|
119
165
|
catch {
|
|
120
166
|
return false;
|
|
121
167
|
}
|
|
122
168
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
if (
|
|
127
|
-
return '
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const merged = unit.merge?.(shared, target) ?? null;
|
|
132
|
-
if (merged === null)
|
|
133
|
-
return 'conflict';
|
|
134
|
-
keep = merged;
|
|
169
|
+
/** Keep a valid private key in a ring, then delete the shared copies. */
|
|
170
|
+
async retireThenDelete(privateKey, ring, shared) {
|
|
171
|
+
const publicKey = publicKeyOf(privateKey);
|
|
172
|
+
if (!publicKey)
|
|
173
|
+
return (await this.deleteShared(shared)) ? 'discarded' : 'failed';
|
|
174
|
+
try {
|
|
175
|
+
// Resolves only once the ring read it back.
|
|
176
|
+
await retireIdentityKeyDuringMigration({ privateKey, publicKey }, ring);
|
|
135
177
|
}
|
|
136
|
-
|
|
137
|
-
return 'failed';
|
|
138
|
-
// Verify the copy that is about to become the only one.
|
|
139
|
-
if ((await this.read(s, s.group)) !== keep)
|
|
178
|
+
catch {
|
|
140
179
|
return 'failed';
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
180
|
+
}
|
|
181
|
+
return (await this.deleteShared(shared)) ? 'retired' : 'failed';
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* A personal or organization pair. `keep` decides, from the shared private
|
|
185
|
+
* key, whether it is the key to adopt (`true`) or one to retire (`false`).
|
|
186
|
+
* A half whose own group IS the shared group (the shared slot's public key)
|
|
187
|
+
* is never moved or deleted here.
|
|
188
|
+
*/
|
|
189
|
+
async movePair(privateSlot, publicSlot, keep, ring) {
|
|
190
|
+
const movable = [privateSlot, publicSlot].filter((h) => h.group !== 'shared');
|
|
191
|
+
const sharedPrivate = await this.read(privateSlot, 'shared');
|
|
192
|
+
const sharedPublic = publicSlot.group === 'shared' ? null : await this.read(publicSlot, 'shared');
|
|
193
|
+
if (sharedPrivate === null && sharedPublic === null)
|
|
149
194
|
return 'absent';
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
return 'conflict';
|
|
195
|
+
const derived = publicKeyOf(sharedPrivate);
|
|
196
|
+
if (!sharedPrivate || !derived) {
|
|
197
|
+
// A public half alone, or a private half that is not a key: nothing to keep.
|
|
198
|
+
return (await this.deleteShared(movable)) ? 'discarded' : 'failed';
|
|
155
199
|
}
|
|
156
|
-
const
|
|
157
|
-
if (!
|
|
158
|
-
return
|
|
159
|
-
for (const h of movable) {
|
|
160
|
-
const i = halves.indexOf(h);
|
|
161
|
-
const value = shared[i];
|
|
162
|
-
if (value !== null && target[i] === null && !(await this.copy(h, value)))
|
|
163
|
-
return 'failed';
|
|
200
|
+
const targetPrivate = await this.read(privateSlot, privateSlot.group);
|
|
201
|
+
if (!keep(derived) || (targetPrivate !== null && targetPrivate !== sharedPrivate)) {
|
|
202
|
+
return this.retireThenDelete(sharedPrivate, ring, movable);
|
|
164
203
|
}
|
|
165
|
-
|
|
166
|
-
|
|
204
|
+
// Adopt (or finish adopting, after a run that died half-way): the private
|
|
205
|
+
// key, and (when its group is not shared) the public key it derives —
|
|
206
|
+
// never a stored public half that disagrees with it.
|
|
207
|
+
if (targetPrivate === null && !(await this.copy(privateSlot, sharedPrivate)))
|
|
167
208
|
return 'failed';
|
|
209
|
+
if (publicSlot.group !== 'shared') {
|
|
210
|
+
const targetPublic = await this.read(publicSlot, publicSlot.group);
|
|
211
|
+
if (targetPublic === null || canonicalPublic(targetPublic) !== derived) {
|
|
212
|
+
if (!(await this.write(publicSlot, derived)))
|
|
213
|
+
return 'failed';
|
|
214
|
+
}
|
|
168
215
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
216
|
+
if (publicKeyOf(await this.read(privateSlot, privateSlot.group)) !== derived)
|
|
217
|
+
return 'failed';
|
|
218
|
+
return (await this.deleteShared(movable)) ? 'moved' : 'failed';
|
|
219
|
+
}
|
|
220
|
+
/** A phrase: adopted when it derives the key being kept, retired otherwise. */
|
|
221
|
+
async movePhrase(s, keep, ring) {
|
|
222
|
+
const shared = await this.read(s, 'shared');
|
|
223
|
+
if (shared === null)
|
|
224
|
+
return 'absent';
|
|
225
|
+
const pair = await pairOfPhrase(shared);
|
|
226
|
+
if (!pair)
|
|
227
|
+
return (await this.deleteShared([s])) ? 'discarded' : 'failed';
|
|
228
|
+
const target = await this.read(s, s.group);
|
|
229
|
+
if (!keep(pair.publicKey) || (target !== null && target !== shared)) {
|
|
230
|
+
return this.retireThenDelete(pair.privateKey, ring, [s]);
|
|
172
231
|
}
|
|
173
|
-
|
|
232
|
+
if (target === null && !(await this.copy(s, shared)))
|
|
233
|
+
return 'failed';
|
|
234
|
+
return (await this.deleteShared([s])) ? 'moved' : 'failed';
|
|
174
235
|
}
|
|
175
|
-
|
|
176
|
-
|
|
236
|
+
/** A non-secret value (a timestamp, the index): copied when the group has none, merged by `merge`. */
|
|
237
|
+
async moveValue(s, merge) {
|
|
238
|
+
const shared = await this.read(s, 'shared');
|
|
239
|
+
if (shared === null)
|
|
240
|
+
return 'absent';
|
|
241
|
+
const target = await this.read(s, s.group);
|
|
242
|
+
const keep = target === null ? shared : merge ? merge(shared, target) : target;
|
|
243
|
+
if (keep !== target) {
|
|
244
|
+
if (!(target === null ? await this.copy(s, keep) : await this.write(s, keep)))
|
|
245
|
+
return 'failed';
|
|
246
|
+
}
|
|
247
|
+
return (await this.deleteShared([s])) ? 'moved' : 'failed';
|
|
177
248
|
}
|
|
178
249
|
}
|
|
179
|
-
|
|
180
|
-
function personalUnits() {
|
|
250
|
+
const personalPairs = () => {
|
|
181
251
|
const v2 = (key, service, deviceOnly) => slot(key, 'identity-secret', service, deviceOnly);
|
|
182
252
|
const legacy = (key, deviceOnly) => slot(key, 'identity-secret', undefined, deviceOnly);
|
|
183
253
|
return [
|
|
184
254
|
{
|
|
185
255
|
label: 'primary',
|
|
186
|
-
kind: 'pair',
|
|
187
256
|
privateSlot: v2(V2_STORAGE_KEYS.PRIVATE_KEY, V2_PRIMARY_KEYCHAIN_SERVICE, true),
|
|
188
257
|
publicSlot: v2(V2_STORAGE_KEYS.PUBLIC_KEY, V2_PRIMARY_KEYCHAIN_SERVICE, false),
|
|
189
258
|
},
|
|
190
259
|
{
|
|
191
260
|
label: 'backup',
|
|
192
|
-
kind: 'pair',
|
|
193
261
|
privateSlot: v2(V2_STORAGE_KEYS.BACKUP_PRIVATE_KEY, V2_BACKUP_KEYCHAIN_SERVICE, true),
|
|
194
262
|
publicSlot: v2(V2_STORAGE_KEYS.BACKUP_PUBLIC_KEY, V2_BACKUP_KEYCHAIN_SERVICE, false),
|
|
195
263
|
},
|
|
196
|
-
{
|
|
197
|
-
label: 'backup-timestamp',
|
|
198
|
-
kind: 'value',
|
|
199
|
-
slot: v2(V2_STORAGE_KEYS.BACKUP_TIMESTAMP, V2_BACKUP_KEYCHAIN_SERVICE, false),
|
|
200
|
-
},
|
|
201
264
|
{
|
|
202
265
|
label: 'legacy-primary',
|
|
203
|
-
kind: 'pair',
|
|
204
266
|
privateSlot: legacy(STORAGE_KEYS.PRIVATE_KEY, true),
|
|
205
267
|
publicSlot: legacy(STORAGE_KEYS.PUBLIC_KEY, false),
|
|
206
268
|
},
|
|
207
269
|
{
|
|
208
270
|
label: 'legacy-backup',
|
|
209
|
-
kind: 'pair',
|
|
210
271
|
privateSlot: legacy(STORAGE_KEYS.BACKUP_PRIVATE_KEY, true),
|
|
211
272
|
publicSlot: legacy(STORAGE_KEYS.BACKUP_PUBLIC_KEY, false),
|
|
212
273
|
},
|
|
213
|
-
{
|
|
214
|
-
label: 'legacy-backup-timestamp',
|
|
215
|
-
kind: 'value',
|
|
216
|
-
slot: legacy(STORAGE_KEYS.BACKUP_TIMESTAMP, false),
|
|
217
|
-
},
|
|
218
|
-
{
|
|
219
|
-
label: 'phrase',
|
|
220
|
-
kind: 'value',
|
|
221
|
-
slot: slot(RECOVERY_MNEMONIC_STORAGE_KEY, 'identity-secret', RECOVERY_MNEMONIC_KEYCHAIN_SERVICE),
|
|
222
|
-
},
|
|
223
274
|
{
|
|
224
275
|
// The private key moves to the wallet group; its public key stays shared.
|
|
225
276
|
label: 'shared-slot',
|
|
226
|
-
kind: 'pair',
|
|
227
277
|
privateSlot: slot(STORAGE_KEYS.SHARED_PRIVATE_KEY, 'spending-key'),
|
|
228
278
|
publicSlot: slot(STORAGE_KEYS.SHARED_PUBLIC_KEY, 'shared', undefined, false),
|
|
229
279
|
},
|
|
230
280
|
];
|
|
231
|
-
}
|
|
281
|
+
};
|
|
282
|
+
const TIMESTAMPS = [
|
|
283
|
+
{
|
|
284
|
+
label: 'backup-timestamp',
|
|
285
|
+
slot: slot(V2_STORAGE_KEYS.BACKUP_TIMESTAMP, 'identity-secret', V2_BACKUP_KEYCHAIN_SERVICE, false),
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
label: 'legacy-backup-timestamp',
|
|
289
|
+
slot: slot(STORAGE_KEYS.BACKUP_TIMESTAMP, 'identity-secret', undefined, false),
|
|
290
|
+
},
|
|
291
|
+
];
|
|
292
|
+
const PHRASE_SLOT = slot(RECOVERY_MNEMONIC_STORAGE_KEY, 'identity-secret', RECOVERY_MNEMONIC_KEYCHAIN_SERVICE);
|
|
232
293
|
const INDEX_SLOT = slot(ORGANIZATION_INDEX_STORAGE_KEY, 'identity-secret', ORGANIZATION_INDEX_KEYCHAIN_SERVICE, false);
|
|
294
|
+
const RECORD_SLOT = slot(COMPLETION_KEY, 'identity-secret', COMPLETION_SERVICE, false);
|
|
295
|
+
const MIRROR_SLOT = slot(COMPLETION_KEY, 'spending-key', COMPLETION_SERVICE, false);
|
|
233
296
|
/** Union of two indexes; never drops an id either side lists. */
|
|
234
297
|
function mergeIndexes(shared, target) {
|
|
235
298
|
return serializeOrganizationIndex([
|
|
@@ -237,6 +300,45 @@ function mergeIndexes(shared, target) {
|
|
|
237
300
|
...parseOrganizationIndex(target),
|
|
238
301
|
]);
|
|
239
302
|
}
|
|
303
|
+
const ADOPTED = (o) => o === 'moved';
|
|
304
|
+
/** The proven run: everything the marker vouches for, every organization item. */
|
|
305
|
+
async function migrateProven(mover, owner, items) {
|
|
306
|
+
const isOwner = (publicKey) => publicKey === owner;
|
|
307
|
+
let personalExposed = false;
|
|
308
|
+
for (const pair of personalPairs()) {
|
|
309
|
+
items[pair.label] = await mover.movePair(pair.privateSlot, pair.publicSlot, isOwner, {});
|
|
310
|
+
personalExposed || (personalExposed = ADOPTED(items[pair.label]));
|
|
311
|
+
}
|
|
312
|
+
for (const t of TIMESTAMPS)
|
|
313
|
+
items[t.label] = await mover.moveValue(t.slot);
|
|
314
|
+
items.phrase = await mover.movePhrase(PHRASE_SLOT, isOwner, {});
|
|
315
|
+
personalExposed || (personalExposed = ADOPTED(items.phrase));
|
|
316
|
+
// Organizations: every id either copy of the index names.
|
|
317
|
+
const ids = new Set([
|
|
318
|
+
...parseOrganizationIndex(await mover.read(INDEX_SLOT, 'shared')),
|
|
319
|
+
...parseOrganizationIndex(await mover.read(INDEX_SLOT, INDEX_SLOT.group)),
|
|
320
|
+
]);
|
|
321
|
+
const organizations = [];
|
|
322
|
+
let n = 0;
|
|
323
|
+
for (const accountId of [...ids].sort()) {
|
|
324
|
+
n += 1;
|
|
325
|
+
const ring = { accountId };
|
|
326
|
+
const privateSlot = slot(organizationPrivateKeyName(accountId), 'spending-key');
|
|
327
|
+
const publicSlot = slot(organizationPublicKeyName(accountId), 'spending-key', undefined, false);
|
|
328
|
+
// The organization's key is whichever this device keeps after the move:
|
|
329
|
+
// the wallet group's when it has one, else the shared copy.
|
|
330
|
+
const kept = publicKeyOf(await mover.read(privateSlot, privateSlot.group)) ??
|
|
331
|
+
publicKeyOf(await mover.read(privateSlot, 'shared'));
|
|
332
|
+
const isKept = (publicKey) => publicKey === kept;
|
|
333
|
+
items[`organization-${n}`] = await mover.movePair(privateSlot, publicSlot, isKept, ring);
|
|
334
|
+
items[`organization-${n}-phrase`] = await mover.movePhrase(slot(organizationMnemonicName(accountId), 'identity-secret', ORGANIZATION_MNEMONIC_KEYCHAIN_SERVICE), isKept, ring);
|
|
335
|
+
if (ADOPTED(items[`organization-${n}`]))
|
|
336
|
+
organizations.push(accountId);
|
|
337
|
+
}
|
|
338
|
+
// The index last: it is how a later run finds the organizations above.
|
|
339
|
+
items['organization-index'] = await mover.moveValue(INDEX_SLOT, mergeIndexes);
|
|
340
|
+
return { personalExposed, organizations };
|
|
341
|
+
}
|
|
240
342
|
/** Run the migration once. Exported for tests; production code awaits the gate. */
|
|
241
343
|
export async function runKeychainGroupMigration() {
|
|
242
344
|
let access;
|
|
@@ -258,59 +360,121 @@ export async function runKeychainGroupMigration() {
|
|
|
258
360
|
}
|
|
259
361
|
const mover = new Mover(store, access);
|
|
260
362
|
const items = {};
|
|
363
|
+
setHolderSharedSecretFallback(false);
|
|
364
|
+
let record;
|
|
365
|
+
let proven = false;
|
|
261
366
|
try {
|
|
262
|
-
|
|
263
|
-
|
|
367
|
+
const existing = parseRecord(await mover.read(RECORD_SLOT, RECORD_SLOT.group));
|
|
368
|
+
if (existing) {
|
|
369
|
+
// Done before. Re-mirror (the wallet group may have been cleared).
|
|
370
|
+
if ((await mover.read(MIRROR_SLOT, MIRROR_SLOT.group)) === null) {
|
|
371
|
+
await mover.write(MIRROR_SLOT, JSON.stringify({ v: 1, completedAt: existing.completedAt }));
|
|
372
|
+
}
|
|
373
|
+
return { state: 'complete', items };
|
|
264
374
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
...parseOrganizationIndex(await mover.read(INDEX_SLOT, INDEX_SLOT.group)),
|
|
269
|
-
]);
|
|
270
|
-
let n = 0;
|
|
271
|
-
for (const accountId of [...ids].sort()) {
|
|
272
|
-
n += 1;
|
|
273
|
-
items[`organization-${n}`] = await mover.move({
|
|
274
|
-
label: `organization-${n}`,
|
|
275
|
-
kind: 'pair',
|
|
276
|
-
privateSlot: slot(organizationPrivateKeyName(accountId), 'spending-key'),
|
|
277
|
-
publicSlot: slot(organizationPublicKeyName(accountId), 'spending-key', undefined, false),
|
|
278
|
-
});
|
|
279
|
-
items[`organization-${n}-phrase`] = await mover.move({
|
|
280
|
-
label: `organization-${n}-phrase`,
|
|
281
|
-
kind: 'value',
|
|
282
|
-
slot: slot(organizationMnemonicName(accountId), 'identity-secret', ORGANIZATION_MNEMONIC_KEYCHAIN_SERVICE),
|
|
283
|
-
});
|
|
375
|
+
let marker;
|
|
376
|
+
try {
|
|
377
|
+
marker = await readIdentityMarkerStrict();
|
|
284
378
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
379
|
+
catch (error) {
|
|
380
|
+
throw new Deferred(error instanceof Error ? error.message : 'marker unreadable');
|
|
381
|
+
}
|
|
382
|
+
const owner = marker ? canonicalPublic(marker.publicKey) : null;
|
|
383
|
+
record = { v: 1, completedAt: Date.now() };
|
|
384
|
+
if (owner) {
|
|
385
|
+
proven = true;
|
|
386
|
+
const exposure = await migrateProven(mover, owner, items);
|
|
387
|
+
if (exposure.personalExposed)
|
|
388
|
+
record.exposedPublicKey = owner;
|
|
389
|
+
if (exposure.organizations.length > 0)
|
|
390
|
+
record.exposedOrganizations = exposure.organizations;
|
|
391
|
+
}
|
|
392
|
+
// No marker: this Commons wrote nothing that is in the shared group (or
|
|
393
|
+
// cannot prove it did). Nothing there is adopted, now or later.
|
|
292
394
|
}
|
|
293
395
|
catch (error) {
|
|
294
396
|
if (error instanceof Deferred) {
|
|
295
|
-
return { state: 'deferred', items };
|
|
397
|
+
return { state: 'deferred', items, proven };
|
|
296
398
|
}
|
|
297
399
|
throw error;
|
|
298
400
|
}
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
if (!settled) {
|
|
401
|
+
const failed = Object.entries(items).filter(([, o]) => o === 'failed');
|
|
402
|
+
if (failed.length > 0) {
|
|
403
|
+
// Proven copies are still in the shared group: let Commons read them for
|
|
404
|
+
// this launch, and try again on the next one.
|
|
405
|
+
setHolderSharedSecretFallback(true);
|
|
305
406
|
logger.warn('Keychain access-group migration left items in the shared group', {
|
|
306
407
|
component: 'KeychainGroupMigration',
|
|
307
|
-
items:
|
|
308
|
-
.filter(([, o]) => o !== 'absent' && o !== 'moved')
|
|
309
|
-
.map(([label, o]) => `${label}:${o}`)
|
|
310
|
-
.join(','),
|
|
408
|
+
items: failed.map(([label]) => label).join(','),
|
|
311
409
|
});
|
|
410
|
+
return { state: 'incomplete', items, proven };
|
|
411
|
+
}
|
|
412
|
+
if (!(await mover.write(RECORD_SLOT, JSON.stringify(record))) ||
|
|
413
|
+
!(await mover.write(MIRROR_SLOT, JSON.stringify({ v: 1, completedAt: record.completedAt })))) {
|
|
414
|
+
return { state: 'incomplete', items, proven };
|
|
415
|
+
}
|
|
416
|
+
return { state: 'complete', items, proven };
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* What every keychain-backed identity access awaits first, by role: Commons
|
|
420
|
+
* runs (or has run) the migration; the Peable wallet checks once whether
|
|
421
|
+
* Commons' completion record is in the wallet group, and from then on never
|
|
422
|
+
* falls back to the shared group for a secret. Never throws.
|
|
423
|
+
*/
|
|
424
|
+
export async function prepareKeychainAccess(access) {
|
|
425
|
+
if (access.mode !== 'grouped')
|
|
426
|
+
return;
|
|
427
|
+
if (access.role === 'holder') {
|
|
428
|
+
await ensureKeychainGroupsMigrated();
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if (access.role === 'wallet' && !walletSawCompletion) {
|
|
432
|
+
try {
|
|
433
|
+
const store = await loadSecureStore();
|
|
434
|
+
const mirror = await store.getItemAsync(COMPLETION_KEY, {
|
|
435
|
+
keychainService: COMPLETION_SERVICE,
|
|
436
|
+
accessGroup: keychainAccessGroupValue(access, 'wallet'),
|
|
437
|
+
});
|
|
438
|
+
if (parseRecord(mirror)) {
|
|
439
|
+
walletSawCompletion = true;
|
|
440
|
+
closeWalletSharedSecretFallback();
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
catch {
|
|
444
|
+
// Locked: the next access asks again.
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
let walletSawCompletion = false;
|
|
449
|
+
/**
|
|
450
|
+
* Commons: whether this device's identity keys sat in the shared keychain
|
|
451
|
+
* group before the split, where every Oxy iOS app could read them. `publicKey`
|
|
452
|
+
* is the personal key that was exposed (recommend a rotation while it is still
|
|
453
|
+
* the current key), `organizations` the organization accounts whose key was.
|
|
454
|
+
* `null` when nothing was exposed, the migration has not finished, or this is
|
|
455
|
+
* not Commons on iOS.
|
|
456
|
+
*/
|
|
457
|
+
export async function getPreSplitExposure() {
|
|
458
|
+
try {
|
|
459
|
+
const access = await resolveKeychainAccess();
|
|
460
|
+
if (access.mode !== 'grouped' || access.role !== 'holder')
|
|
461
|
+
return null;
|
|
462
|
+
await ensureKeychainGroupsMigrated();
|
|
463
|
+
const store = await loadSecureStore();
|
|
464
|
+
const record = parseRecord(await store.getItemAsync(COMPLETION_KEY, {
|
|
465
|
+
keychainService: COMPLETION_SERVICE,
|
|
466
|
+
accessGroup: keychainAccessGroupValue(access, 'identity'),
|
|
467
|
+
}));
|
|
468
|
+
if (!record || (!record.exposedPublicKey && !record.exposedOrganizations?.length))
|
|
469
|
+
return null;
|
|
470
|
+
return {
|
|
471
|
+
publicKey: record.exposedPublicKey ?? null,
|
|
472
|
+
organizations: record.exposedOrganizations ?? [],
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
catch {
|
|
476
|
+
return null;
|
|
312
477
|
}
|
|
313
|
-
return report;
|
|
314
478
|
}
|
|
315
479
|
let settledReport = null;
|
|
316
480
|
let inFlight = null;
|
|
@@ -345,4 +509,5 @@ export function ensureKeychainGroupsMigrated() {
|
|
|
345
509
|
export function resetKeychainGroupMigrationForTests() {
|
|
346
510
|
settledReport = null;
|
|
347
511
|
inFlight = null;
|
|
512
|
+
walletSawCompletion = false;
|
|
348
513
|
}
|