@dotrino/identity 0.136.0 → 0.136.2
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/package.json +3 -2
- package/src/index.d.ts +60 -2
- package/src/index.js +364 -81
- package/src/node.js +29 -5
- package/types/acta.d.ts +397 -175
- package/types/assertion.d.ts +25 -18
- package/types/avatar.d.ts +14 -3
- package/types/capabilities.d.ts +110 -61
- package/types/content.d.ts +88 -45
- package/types/core.d.ts +578 -383
- package/types/keyid.d.ts +10 -3
- package/types/machine.d.ts +5 -1
- package/types/peerStore.d.ts +80 -0
- package/types/peerSync.d.ts +20 -6
- package/types/remote.d.ts +170 -42
- package/types/session.d.ts +74 -81
- package/types/sessionFlow.d.ts +108 -33
- package/types/vaultSeal.d.ts +21 -9
- package/vault/acta.js +245 -29
- package/vault/assertion.js +13 -4
- package/vault/avatar.js +12 -1
- package/vault/capabilities.js +56 -6
- package/vault/channel.js +18 -2
- package/vault/content.js +60 -6
- package/vault/core.js +419 -88
- package/vault/externalKeys.js +34 -2
- package/vault/facade.js +182 -24
- package/vault/globals.d.ts +60 -0
- package/vault/keyid.js +10 -1
- package/vault/machine.js +7 -1
- package/vault/nativeStore.js +21 -4
- package/vault/originPolicy.js +13 -2
- package/vault/peerStore.js +57 -10
- package/vault/peerSync.js +23 -4
- package/vault/remote.js +132 -32
- package/vault/session.js +24 -7
- package/vault/sessionFlow.js +43 -9
- package/vault/sync.js +59 -15
- package/vault/vault.js +78 -49
- package/vault/vaultSeal.js +18 -7
- package/vault/vendor/compat/VERSION.txt +1 -1
- package/vault/vendor/compat/advisory.js +58 -8
- package/vault/vendor/compat/index.js +57 -6
- package/vault/vendor/compat/ranges.js +12 -2
- package/vault/vendor/proxy-client/VERSION.txt +1 -1
- package/vault/vendor/proxy-client/client.js +41 -19
- package/vault/vendor/roadmap/manifests.js +23 -23
- package/vault/vendor/store/VERSION.txt +1 -1
- package/vault/vendor/vault/VERSION.txt +1 -1
- package/vault/vendor/vault/core/libVersion.js +1 -1
- package/vault/vendor/vault/core/transport.js +15 -3
- package/vault/vendor/vault/core/vault.js +71 -1
- package/vault/vendor/vault/protocol.js +25 -0
package/vault/sync.js
CHANGED
|
@@ -32,12 +32,14 @@ const SS_PASSPHRASE_KEY = 'cc.sync.kHexEphemeral'
|
|
|
32
32
|
|
|
33
33
|
// ---------- low-level helpers ----------
|
|
34
34
|
|
|
35
|
+
/** @param {ArrayBuffer | Uint8Array} buf @returns {string} */
|
|
35
36
|
function bufToBase64 (buf) {
|
|
36
37
|
const bytes = new Uint8Array(buf)
|
|
37
38
|
let s = ''
|
|
38
39
|
for (let i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i])
|
|
39
40
|
return btoa(s)
|
|
40
41
|
}
|
|
42
|
+
/** @param {string} b64 @returns {ArrayBuffer} */
|
|
41
43
|
function base64ToBuf (b64) {
|
|
42
44
|
const s = atob(b64)
|
|
43
45
|
const bytes = new Uint8Array(s.length)
|
|
@@ -52,15 +54,18 @@ function getDeviceId () {
|
|
|
52
54
|
}
|
|
53
55
|
return id
|
|
54
56
|
}
|
|
57
|
+
/** @returns {Record<string, number>} */
|
|
55
58
|
function getDeviceVersions () {
|
|
56
59
|
try { return JSON.parse(localStorage.getItem(LS_LAST_VERSIONS) || '{}') } catch { return {} }
|
|
57
60
|
}
|
|
61
|
+
/** @param {Record<string, number>} v */
|
|
58
62
|
function setDeviceVersions (v) {
|
|
59
63
|
localStorage.setItem(LS_LAST_VERSIONS, JSON.stringify(v))
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
// ---------- crypto: passphrase → key, AES-GCM ----------
|
|
63
67
|
|
|
68
|
+
/** @param {string} passphrase @param {Uint8Array<ArrayBuffer>} salt @returns {Promise<CryptoKey>} */
|
|
64
69
|
async function deriveKey (passphrase, salt) {
|
|
65
70
|
const baseKey = await crypto.subtle.importKey(
|
|
66
71
|
'raw',
|
|
@@ -78,6 +83,7 @@ async function deriveKey (passphrase, salt) {
|
|
|
78
83
|
)
|
|
79
84
|
}
|
|
80
85
|
|
|
86
|
+
/** @param {unknown} plaintextObj @param {string} passphrase */
|
|
81
87
|
async function encryptBlob (plaintextObj, passphrase) {
|
|
82
88
|
const salt = crypto.getRandomValues(new Uint8Array(16))
|
|
83
89
|
const iv = crypto.getRandomValues(new Uint8Array(12))
|
|
@@ -95,6 +101,7 @@ async function encryptBlob (plaintextObj, passphrase) {
|
|
|
95
101
|
}
|
|
96
102
|
}
|
|
97
103
|
|
|
104
|
+
/** @param {any} envelope @param {string} passphrase @returns {Promise<any>} */
|
|
98
105
|
async function decryptBlob (envelope, passphrase) {
|
|
99
106
|
if (!envelope || envelope.v !== 1) throw new Error('Unsupported backup version')
|
|
100
107
|
const salt = new Uint8Array(base64ToBuf(envelope.kdf.salt))
|
|
@@ -115,10 +122,12 @@ async function decryptBlob (envelope, passphrase) {
|
|
|
115
122
|
|
|
116
123
|
// ---------- Google OAuth (GIS Token Client) ----------
|
|
117
124
|
|
|
125
|
+
/** @type {Promise<void> | null} */
|
|
118
126
|
let _gisLoaded = null
|
|
127
|
+
/** @returns {Promise<void>} */
|
|
119
128
|
function loadGis () {
|
|
120
129
|
if (_gisLoaded) return _gisLoaded
|
|
121
|
-
_gisLoaded = new Promise((resolve, reject) => {
|
|
130
|
+
_gisLoaded = new Promise(/** @param {(v?: void) => void} resolve @param {(e: Error) => void} reject */ (resolve, reject) => {
|
|
122
131
|
if (window.google?.accounts?.oauth2) return resolve()
|
|
123
132
|
const s = document.createElement('script')
|
|
124
133
|
s.src = GIS_SCRIPT
|
|
@@ -131,32 +140,44 @@ function loadGis () {
|
|
|
131
140
|
return _gisLoaded
|
|
132
141
|
}
|
|
133
142
|
|
|
143
|
+
/** @type {string | null} */
|
|
134
144
|
let _accessToken = null
|
|
135
145
|
let _accessTokenExpiresAt = 0
|
|
146
|
+
/** @type {GoogleTokenClient | null} */
|
|
136
147
|
let _tokenClient = null
|
|
137
148
|
|
|
149
|
+
/** @param {string} clientId @returns {Promise<GoogleTokenClient>} */
|
|
138
150
|
async function ensureTokenClient (clientId) {
|
|
139
151
|
await loadGis()
|
|
140
152
|
if (_tokenClient && _tokenClient._clientId === clientId) return _tokenClient
|
|
141
|
-
|
|
153
|
+
// `loadGis` acaba de cargar el script: si `google.accounts.oauth2` no está, esto lanza.
|
|
154
|
+
/** @type {GoogleTokenClient} */
|
|
155
|
+
const client = /** @type {any} */ (window).google.accounts.oauth2.initTokenClient({
|
|
142
156
|
client_id: clientId,
|
|
143
157
|
scope: SCOPE,
|
|
144
158
|
prompt: '',
|
|
145
159
|
callback: () => {} // overridden per-request
|
|
146
160
|
})
|
|
147
|
-
|
|
148
|
-
|
|
161
|
+
client._clientId = clientId
|
|
162
|
+
_tokenClient = client
|
|
163
|
+
return client
|
|
149
164
|
}
|
|
150
165
|
|
|
166
|
+
/**
|
|
167
|
+
* @param {string} clientId
|
|
168
|
+
* @param {{ interactive: boolean }} opts
|
|
169
|
+
* @returns {Promise<{ accessToken: string, expiresAt: number }>}
|
|
170
|
+
*/
|
|
151
171
|
async function requestAccessToken (clientId, { interactive }) {
|
|
152
172
|
const client = await ensureTokenClient(clientId)
|
|
153
173
|
return new Promise((resolve, reject) => {
|
|
154
174
|
client.callback = (resp) => {
|
|
155
175
|
if (resp?.error) return reject(new Error(resp.error))
|
|
156
176
|
if (!resp?.access_token) return reject(new Error('No access_token in response'))
|
|
157
|
-
|
|
177
|
+
const accessToken = resp.access_token
|
|
178
|
+
_accessToken = accessToken
|
|
158
179
|
_accessTokenExpiresAt = Date.now() + ((Number(resp.expires_in) || 3600) - 60) * 1000
|
|
159
|
-
resolve({ accessToken
|
|
180
|
+
resolve({ accessToken, expiresAt: _accessTokenExpiresAt })
|
|
160
181
|
}
|
|
161
182
|
try {
|
|
162
183
|
client.requestAccessToken({ prompt: interactive ? 'consent' : '' })
|
|
@@ -164,6 +185,7 @@ async function requestAccessToken (clientId, { interactive }) {
|
|
|
164
185
|
})
|
|
165
186
|
}
|
|
166
187
|
|
|
188
|
+
/** @returns {Promise<string | null>} */
|
|
167
189
|
async function getValidAccessToken () {
|
|
168
190
|
if (_accessToken && Date.now() < _accessTokenExpiresAt) return _accessToken
|
|
169
191
|
const clientId = localStorage.getItem(LS_OAUTH_CLIENT)
|
|
@@ -176,6 +198,7 @@ async function getValidAccessToken () {
|
|
|
176
198
|
|
|
177
199
|
// ---------- Drive REST helpers ----------
|
|
178
200
|
|
|
201
|
+
/** @param {string} token @param {string} fileName @returns {Promise<{ id: string, name: string, headRevisionId?: string, modifiedTime?: string } | null>} */
|
|
179
202
|
async function driveFindFile (token, fileName) {
|
|
180
203
|
const q = encodeURIComponent(`name='${fileName}' and 'appDataFolder' in parents and trashed=false`)
|
|
181
204
|
const url = `${DRIVE_FILES_API}?spaces=appDataFolder&q=${q}&fields=files(id,name,headRevisionId,modifiedTime)`
|
|
@@ -185,6 +208,7 @@ async function driveFindFile (token, fileName) {
|
|
|
185
208
|
return (data.files && data.files[0]) || null
|
|
186
209
|
}
|
|
187
210
|
|
|
211
|
+
/** @param {string} token @param {string} fileId @returns {Promise<{ body: any, etag: string | null }>} */
|
|
188
212
|
async function driveDownload (token, fileId) {
|
|
189
213
|
const url = `${DRIVE_FILES_API}/${fileId}?alt=media`
|
|
190
214
|
const res = await fetch(url, { headers: { Authorization: `Bearer ${token}` } })
|
|
@@ -194,6 +218,7 @@ async function driveDownload (token, fileId) {
|
|
|
194
218
|
return { body, etag }
|
|
195
219
|
}
|
|
196
220
|
|
|
221
|
+
/** @param {string} token @param {string} fileName @param {unknown} contentObj @returns {Promise<string>} */
|
|
197
222
|
async function driveCreate (token, fileName, contentObj) {
|
|
198
223
|
const metadata = { name: fileName, parents: ['appDataFolder'] }
|
|
199
224
|
const boundary = '-------ccsync' + Math.random().toString(36).slice(2)
|
|
@@ -218,7 +243,12 @@ async function driveCreate (token, fileName, contentObj) {
|
|
|
218
243
|
return data.id
|
|
219
244
|
}
|
|
220
245
|
|
|
246
|
+
/**
|
|
247
|
+
* @param {string} token @param {string} fileId @param {unknown} contentObj @param {string | null} ifMatchEtag
|
|
248
|
+
* @returns {Promise<{ etag: string | null }>}
|
|
249
|
+
*/
|
|
221
250
|
async function driveUpdate (token, fileId, contentObj, ifMatchEtag) {
|
|
251
|
+
/** @type {Record<string, string>} */
|
|
222
252
|
const headers = {
|
|
223
253
|
Authorization: `Bearer ${token}`,
|
|
224
254
|
'Content-Type': 'application/json; charset=UTF-8'
|
|
@@ -227,7 +257,7 @@ async function driveUpdate (token, fileId, contentObj, ifMatchEtag) {
|
|
|
227
257
|
const url = `${DRIVE_UPLOAD_API}/${fileId}?uploadType=media`
|
|
228
258
|
const res = await fetch(url, { method: 'PATCH', headers, body: JSON.stringify(contentObj) })
|
|
229
259
|
if (res.status === 412) {
|
|
230
|
-
const err = new Error('etag mismatch')
|
|
260
|
+
const err = /** @type {Error & { code?: string }} */ (new Error('etag mismatch'))
|
|
231
261
|
err.code = 'PRECONDITION_FAILED'
|
|
232
262
|
throw err
|
|
233
263
|
}
|
|
@@ -241,7 +271,7 @@ async function driveUpdate (token, fileId, contentObj, ifMatchEtag) {
|
|
|
241
271
|
/**
|
|
242
272
|
* Creates a sync controller for one logical state (identity OR store).
|
|
243
273
|
*
|
|
244
|
-
* @param {
|
|
274
|
+
* @param {object} cfg
|
|
245
275
|
* @param {string} cfg.fileName - e.g. 'dotrino-identity-backup.json'
|
|
246
276
|
* @param {() => Promise<Object>} cfg.exportLocal - returns plaintext-equivalent JSON
|
|
247
277
|
* @param {(merged: Object) => Promise<void>} cfg.applyMerged - apply merged state locally
|
|
@@ -250,13 +280,19 @@ async function driveUpdate (token, fileId, contentObj, ifMatchEtag) {
|
|
|
250
280
|
*/
|
|
251
281
|
export function createSync (cfg) {
|
|
252
282
|
const deviceId = getDeviceId()
|
|
253
|
-
|
|
283
|
+
/** @type {string | null} kept in module memory after unlock; mirrored to sessionStorage for tab refresh */
|
|
284
|
+
let _passphrase = null
|
|
254
285
|
let _dirty = false
|
|
286
|
+
/** @type {ReturnType<typeof setTimeout> | null} */
|
|
255
287
|
let _debounceTimer = null
|
|
288
|
+
/** @type {ReturnType<typeof setInterval> | null} */
|
|
256
289
|
let _hardTimer = null
|
|
290
|
+
/** @type {ReturnType<typeof setInterval> | null} */
|
|
257
291
|
let _periodicTimer = null
|
|
258
292
|
let _running = false
|
|
293
|
+
/** @type {any} */
|
|
259
294
|
let _lastError = null
|
|
295
|
+
/** @type {Set<(status: Record<string, any>) => void>} */
|
|
260
296
|
let _statusListeners = new Set()
|
|
261
297
|
|
|
262
298
|
// Restore passphrase from sessionStorage if present (per-tab unlock survives refresh)
|
|
@@ -265,6 +301,7 @@ export function createSync (cfg) {
|
|
|
265
301
|
if (saved) _passphrase = saved
|
|
266
302
|
} catch {}
|
|
267
303
|
|
|
304
|
+
/** @param {string} status @param {Record<string, any>} [extra] */
|
|
268
305
|
function emitStatus (status, extra = {}) {
|
|
269
306
|
const payload = { kind: cfg.kind, status, ...extra, ts: Date.now() }
|
|
270
307
|
for (const fn of _statusListeners) {
|
|
@@ -274,13 +311,14 @@ export function createSync (cfg) {
|
|
|
274
311
|
|
|
275
312
|
function isUnlocked () { return !!_passphrase }
|
|
276
313
|
|
|
314
|
+
/** @param {string} passphrase */
|
|
277
315
|
async function unlock (passphrase) {
|
|
278
316
|
if (!passphrase || passphrase.length < 8) throw new Error('Passphrase too short')
|
|
279
317
|
_passphrase = passphrase
|
|
280
318
|
try { sessionStorage.setItem(SS_PASSPHRASE_KEY, passphrase) } catch {}
|
|
281
319
|
emitStatus('unlocked')
|
|
282
320
|
// Trigger an immediate pull on unlock
|
|
283
|
-
try { await pull() } catch (e) { _lastError = e; emitStatus('error', { error: e.message }) }
|
|
321
|
+
try { await pull() } catch (e) { _lastError = e; emitStatus('error', { error: /** @type {any} */ (e).message }) }
|
|
284
322
|
startTimers()
|
|
285
323
|
return { ok: true }
|
|
286
324
|
}
|
|
@@ -292,6 +330,7 @@ export function createSync (cfg) {
|
|
|
292
330
|
emitStatus('locked')
|
|
293
331
|
}
|
|
294
332
|
|
|
333
|
+
/** @param {unknown} exported */
|
|
295
334
|
function buildPlaintext (exported) {
|
|
296
335
|
const versions = getDeviceVersions()
|
|
297
336
|
versions[deviceId] = (versions[deviceId] || 0) + 1
|
|
@@ -305,6 +344,7 @@ export function createSync (cfg) {
|
|
|
305
344
|
}
|
|
306
345
|
}
|
|
307
346
|
|
|
347
|
+
/** @param {Record<string, number>} localV @param {Record<string, number> | null | undefined} remoteV @returns {boolean} */
|
|
308
348
|
function dominates (localV, remoteV) {
|
|
309
349
|
if (!remoteV) return true
|
|
310
350
|
const all = new Set([...Object.keys(localV || {}), ...Object.keys(remoteV)])
|
|
@@ -318,6 +358,7 @@ export function createSync (cfg) {
|
|
|
318
358
|
return strictly
|
|
319
359
|
}
|
|
320
360
|
|
|
361
|
+
/** @returns {Promise<Record<string, any>>} */
|
|
321
362
|
async function pull () {
|
|
322
363
|
if (!isUnlocked()) return { skipped: 'locked' }
|
|
323
364
|
if (_running) return { skipped: 'busy' }
|
|
@@ -330,7 +371,7 @@ export function createSync (cfg) {
|
|
|
330
371
|
if (!file) { emitStatus('synced'); return { remote: null } }
|
|
331
372
|
const { body: envelope, etag } = await driveDownload(token, file.id)
|
|
332
373
|
localStorage.setItem(LS_LAST_ETAG + ':' + cfg.kind, etag || '')
|
|
333
|
-
const plaintext = await decryptBlob(envelope, _passphrase)
|
|
374
|
+
const plaintext = await decryptBlob(envelope, /** @type {string} */ (_passphrase))
|
|
334
375
|
const local = await cfg.exportLocal()
|
|
335
376
|
const remoteVersions = plaintext.deviceVersions || {}
|
|
336
377
|
const localVersions = getDeviceVersions()
|
|
@@ -353,13 +394,14 @@ export function createSync (cfg) {
|
|
|
353
394
|
return { applied: true, changed }
|
|
354
395
|
} catch (e) {
|
|
355
396
|
_lastError = e
|
|
356
|
-
emitStatus('error', { error: e.message })
|
|
397
|
+
emitStatus('error', { error: /** @type {any} */ (e).message })
|
|
357
398
|
throw e
|
|
358
399
|
} finally {
|
|
359
400
|
_running = false
|
|
360
401
|
}
|
|
361
402
|
}
|
|
362
403
|
|
|
404
|
+
/** @param {number} [retries] @returns {Promise<Record<string, any>>} */
|
|
363
405
|
async function push (retries = 3) {
|
|
364
406
|
if (!isUnlocked()) return { skipped: 'locked' }
|
|
365
407
|
if (!_dirty) return { skipped: 'clean' }
|
|
@@ -371,7 +413,7 @@ export function createSync (cfg) {
|
|
|
371
413
|
if (!token) { emitStatus('offline'); return { skipped: 'no-token' } }
|
|
372
414
|
const local = await cfg.exportLocal()
|
|
373
415
|
const plaintext = buildPlaintext(local)
|
|
374
|
-
const envelope = await encryptBlob(plaintext, _passphrase)
|
|
416
|
+
const envelope = await encryptBlob(plaintext, /** @type {string} */ (_passphrase))
|
|
375
417
|
const existing = await driveFindFile(token, cfg.fileName)
|
|
376
418
|
if (!existing) {
|
|
377
419
|
await driveCreate(token, cfg.fileName, envelope)
|
|
@@ -387,7 +429,7 @@ export function createSync (cfg) {
|
|
|
387
429
|
emitStatus('synced')
|
|
388
430
|
return { updated: true }
|
|
389
431
|
} catch (e) {
|
|
390
|
-
if (e.code === 'PRECONDITION_FAILED' && retries > 0) {
|
|
432
|
+
if (/** @type {any} */ (e).code === 'PRECONDITION_FAILED' && retries > 0) {
|
|
391
433
|
emitStatus('conflict')
|
|
392
434
|
_running = false // pull() needs the slot
|
|
393
435
|
await pull()
|
|
@@ -398,7 +440,7 @@ export function createSync (cfg) {
|
|
|
398
440
|
}
|
|
399
441
|
} catch (e) {
|
|
400
442
|
_lastError = e
|
|
401
|
-
emitStatus('error', { error: e.message })
|
|
443
|
+
emitStatus('error', { error: /** @type {any} */ (e).message })
|
|
402
444
|
throw e
|
|
403
445
|
} finally {
|
|
404
446
|
_running = false
|
|
@@ -438,9 +480,11 @@ export function createSync (cfg) {
|
|
|
438
480
|
})
|
|
439
481
|
|
|
440
482
|
return {
|
|
483
|
+
/** @param {(status: Record<string, any>) => void} fn */
|
|
441
484
|
onStatus (fn) { _statusListeners.add(fn); return () => _statusListeners.delete(fn) },
|
|
442
485
|
isUnlocked,
|
|
443
486
|
isConnected: () => !!localStorage.getItem(LS_OAUTH_CLIENT),
|
|
487
|
+
/** @param {string} clientId */
|
|
444
488
|
async connectGoogle (clientId) {
|
|
445
489
|
if (!clientId) throw new Error('clientId required')
|
|
446
490
|
localStorage.setItem(LS_OAUTH_CLIENT, clientId)
|