@le-space/orbitdb-identity-provider-webauthn-did 0.10.0 → 0.12.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/CHANGELOG.md CHANGED
@@ -1,5 +1,71 @@
1
1
  # Changes
2
2
 
3
+ ## 0.12.0
4
+
5
+ The vault's keeper, once for every application, and the vault on its own entry.
6
+
7
+ ### Added
8
+
9
+ - **`createVaultKeeper({ storage, key, slotInfo })`** (main entry, `./standalone` and `./vault`):
10
+ where an application keeps its vaults — anything with `getItem` and `setItem` — and what every
11
+ unlock does with them. `open` opens the vault this authenticator has a slot in, else one `find`
12
+ returns from elsewhere (a backup, an index), else makes one holding `create()`; `addPasskey`,
13
+ `removePasskey` (by slot id, so a key not at hand can go), `renew` (new vault key and id, every key
14
+ that stays answers and is checked against its slot, so none is left out by mistake),
15
+ `replacePayload`, `status`, `records`. A key removed here is refused (`VAULT_SLOT_REMOVED`)
16
+ instead of being given new, empty books; one key opens one vault per store; an unreadable store
17
+ is refused (`VAULT_STORE_UNREADABLE`) and never overwritten. Le-Space/belege and Le-Space/invoice
18
+ each built this around the vault for themselves; the store's format is Belege's (#406 there), and
19
+ a record Belege's own code wrote opens unchanged.
20
+ - **`rootPayload(root)`** and **`rootOf(payload)`**: one 32-byte root as vault payload,
21
+ `{"version":1,"root":"<hex>"}`, every other key derived from it with `deriveSubkey` — the bytes
22
+ Belege's vaults hold.
23
+ - **The `./vault` entry**: `readPrfOutput`, `deriveSubkey`, `deriveAesKey`, the vault functions,
24
+ the keeper, `rootPayload`, `rootOf`, `PrfUnavailableError` and `VaultError`, with no package in
25
+ its import graph (`tests/vault-entry.test.js`). For applications without OrbitDB (#93, its first
26
+ step).
27
+ - `VaultError` codes `VAULT_STORE_UNREADABLE`, `VAULT_SLOT_REMOVED` and `VAULT_SLOT_IN_USE`.
28
+
29
+ ### Fixed
30
+
31
+ - **The production audit passes again.** Four `fast-uri` advisories published after 0.11.0
32
+ (GHSA-w74x-v6f9-j3xg and GHSA-wjr9-j795-jxcr moderate, GHSA-xhq7-xmqj-769h and
33
+ GHSA-333x-w4r6-6crp low) affect `fast-uri` below 3.1.9 on `iso-web > iso-kv > conf > ajv`. The
34
+ override `ajv>fast-uri` goes from `^3.1.8` to `^3.1.9`, still on the 3 line ajv 8 asks for. It
35
+ steers this repository's own install; an application resolves `fast-uri` for itself.
36
+ - **Declarations that said what the code does not.** Behaviour is unchanged.
37
+ - `WebAuthnVarsigProvider` had no `static createCredential`, though `docs/API.md` calls it, no
38
+ `isSupported` or `signPayload`, and no options for the constructor or `sign` (#95).
39
+ - `createWebAuthnEd25519Credential` was declared, and documented, to resolve to `null` when the
40
+ registration fails. It has always rejected, and now says so (#95).
41
+ - The varsig identity helpers: `encodeIdentityValue` resolves to `{ hash, bytes }` and was
42
+ declared to return bytes; `createWebAuthnVarsigIdentities` is synchronous and takes
43
+ `(identity, domainLabels?, storage?, fallbackIdentities?)`; `wrapWithVarsigVerification` takes
44
+ `(defaultIdentities, ipfs?, domainLabels?)`; `decodeVarsigIdentityFromBytes` and
45
+ `verifyVarsigIdentity` take `domainLabels`; `createIpfsIdentityStorage` takes the IPFS
46
+ instance, which the declared `Record<string, unknown>` refused for a typed Helia.
47
+ - The README and `docs/API.md` dated the varsig registration's authenticator fix to 0.9.1. It
48
+ shipped in 0.10.0 (#95).
49
+
50
+ ## 0.11.0
51
+
52
+ Another session of the same passkey may still write: OrbitDB's verified-identity cache, worked
53
+ around in the provider so that no application carries a copy.
54
+
55
+ ### Added
56
+
57
+ - **`withoutStaleIdentityCache(identities)`** and **`createIdentities(options)`** (main entry and
58
+ `./keystore`): `@orbitdb/core` 4 caches a verified identity under `signatures.id` and, on a hit,
59
+ compares the whole identity. This provider keeps the id signature the same in every session of a
60
+ passkey (the PRF-derived signing key) while the WebAuthn proof in `signatures.publicKey` is new per
61
+ session — so whichever session's entries are verified first owns the cache, and every other
62
+ session of the same passkey is refused: _"Key … is not allowed to write to the log"_. Seen in
63
+ belege's restore (belege#259) and in simple-todo's archived01, a phone restoring a laptop's
64
+ archive (simple-todo#79). The wrapper re-checks a refused webauthn identity the way an uncached
65
+ one would be: `isIdentity`, the id signature over the public key, this provider's binding. Other
66
+ providers' identities and anything failing those checks stay refused. Upstream
67
+ orbitdb/orbitdb#1258; to be removed once OrbitDB keys its cache by identity hash (#90).
68
+
3
69
  ## 0.10.0
4
70
 
5
71
  An identity that is nobody's passkey, so that every security key of an owner can write as the same
package/README.md CHANGED
@@ -19,7 +19,7 @@ This package provides:
19
19
  - Authentication/assertion requests omit `allowCredentials` by default, so the browser/authenticator can resolve the credential discoverably.
20
20
  - You can switch this centrally with `configureWebAuthn({ discoverableCredentials: true|false })`.
21
21
  - Registration is still the point where this package extracts the credential public key from attestation.
22
- - Registration offers every kind of authenticator — the device's own, a security key such as a YubiKey, a phone — unless `authenticatorType: 'platform' | 'cross-platform'` narrows it, in `createCredential` as in the varsig registration and the standalone signers (honoured since 0.9.1). It always requires user verification.
22
+ - Registration offers every kind of authenticator — the device's own, a security key such as a YubiKey, a phone — unless `authenticatorType: 'platform' | 'cross-platform'` narrows it — honoured by `createCredential` since 0.9.1, and by the varsig registration and the standalone signers since 0.10.0. It always requires user verification.
23
23
  - Later `navigator.credentials.get()` assertions do not reliably return the public key again, so identity reconstruction still needs metadata from somewhere else.
24
24
 
25
25
  Since **0.6.0** the passkey alone is enough, and stored metadata is a convenience rather than a requirement:
@@ -98,6 +98,30 @@ const identity = await identities.createIdentity({
98
98
  });
99
99
  ```
100
100
 
101
+ ### Identities for a passkey: `createIdentities`
102
+
103
+ OrbitDB keeps a cache of verified identities keyed by `signatures.id`. This provider keeps that
104
+ signature the same in every session of a passkey (the signing key is derived from the PRF output),
105
+ while the WebAuthn proof in `signatures.publicKey` is a fresh assertion per session. So whichever
106
+ session's entries a peer verifies first owns its cache, and every other session of the same passkey
107
+ is refused: _"Key … is not allowed to write to the log"_ — a phone restoring a laptop's backup, a
108
+ browser that gets its own earlier entries back from a relay ([orbitdb/orbitdb#1258](https://github.com/orbitdb/orbitdb/issues/1258),
109
+ [#90](https://github.com/Le-Space/orbitdb-identity-provider-webauthn-did/issues/90)).
110
+
111
+ Until OrbitDB keys its cache by identity hash, build the identities with the provider's wrapper:
112
+
113
+ ```javascript
114
+ import { createIdentities } from '@le-space/orbitdb-identity-provider-webauthn-did';
115
+
116
+ const identities = await createIdentities({ ipfs, keystore }); // Identities(options), wrapped
117
+ const orbitdb = await createOrbitDB({ ipfs, identities, identity });
118
+ ```
119
+
120
+ or wrap an instance you already have with `withoutStaleIdentityCache(identities)`. Where OrbitDB
121
+ says no, a webauthn identity is checked again the way an uncached one would be — the shape, the id
122
+ signature over the public key, and this provider's binding of the key to the DID. Other providers'
123
+ identities, and anything that fails those checks, stay refused.
124
+
101
125
  ### Discoverable Recovery Notes
102
126
 
103
127
  For the DID-based flow:
@@ -228,6 +252,27 @@ const withSecondKey = await addSlot(vault, vaultKey, secondKeysSlot);
228
252
 
229
253
  How it works, and what a vault does not do: [docs/API.md](docs/API.md#prf-keys-and-vaults).
230
254
 
255
+ Since **0.12.0**, `createVaultKeeper({ storage, key, slotInfo })` keeps an application's vaults and does what every unlock needs: open this key's vault or make one, give a second key a slot, take one out, renew the vault key. A key taken out is refused rather than given new, empty books, and an unreadable store is never overwritten. `rootPayload(root)` holds one random root, every other key derived from it with `deriveSubkey`. The `./vault` entry offers all of this with no package in its import graph, for applications without OrbitDB. See [docs/API.md](docs/API.md#keeping-vaults-createvaultkeeper-storage-key-slotinfo-).
256
+
257
+ ```javascript
258
+ import {
259
+ createVaultKeeper,
260
+ readPrfOutput,
261
+ rootPayload,
262
+ } from '@le-space/orbitdb-identity-provider-webauthn-did/vault';
263
+
264
+ const keeper = createVaultKeeper({
265
+ storage: localStorage,
266
+ key: 'myapp.vaults.v1',
267
+ slotInfo: 'myapp/vault-slot/v1',
268
+ });
269
+ const opened = await keeper.open({
270
+ prfOutput: await readPrfOutput(credential),
271
+ rawCredentialId: credential.rawCredentialId,
272
+ create: () => rootPayload(crypto.getRandomValues(new Uint8Array(32))),
273
+ });
274
+ ```
275
+
231
276
  Since **0.10.0**, a secret kept in that vault can be an identity of its own: `createSecretSigner(secret, { info })` turns it into an Ed25519 signer for the provider's `signer` option, so every key that opens the vault writes as the same identity — the root of a database's access controller, which a single passkey's identity would hold for good. See [docs/API.md](docs/API.md#an-identity-that-is-nobodys-passkey-createsecretsignersecret--info-).
232
277
 
233
278
  ### Keystore-based DID (WebAuthn + OrbitDB keystore)
package/docs/API.md CHANGED
@@ -6,6 +6,7 @@ public API surface:
6
6
  - root export: `@le-space/orbitdb-identity-provider-webauthn-did`
7
7
  - standalone export: `@le-space/orbitdb-identity-provider-webauthn-did/standalone`
8
8
  - keystore export: `@le-space/orbitdb-identity-provider-webauthn-did/keystore`
9
+ - vault export: `@le-space/orbitdb-identity-provider-webauthn-did/vault`
9
10
  - verification export: `@le-space/orbitdb-identity-provider-webauthn-did/verification`
10
11
 
11
12
  ## Root Export
@@ -386,6 +387,83 @@ What a vault does not do:
386
387
  `tests/vault-two-authenticators.test.js` runs this in Chromium with two virtual
387
388
  authenticators, each with its own PRF secret as two YubiKeys have.
388
389
 
390
+ #### Keeping vaults: `createVaultKeeper({ storage, key, slotInfo })`
391
+
392
+ Since 0.12.0. Where an application keeps its vaults, and what every unlock
393
+ does with them. Le-Space/belege and Le-Space/invoice each wrote this for
394
+ themselves around the same vault; the keeper is that code once, with the
395
+ rules each of them had to learn:
396
+
397
+ ```js
398
+ const keeper = createVaultKeeper({
399
+ storage: localStorage, // anything with getItem and setItem
400
+ key: 'myapp.vaults.v1',
401
+ slotInfo: 'myapp/vault-slot/v1',
402
+ });
403
+
404
+ const prfOutput = await readPrfOutput(credential);
405
+ const opened = await keeper.open({
406
+ prfOutput,
407
+ rawCredentialId: credential.rawCredentialId,
408
+ label: 'YubiKey 5C',
409
+ create: () => rootPayload(crypto.getRandomValues(new Uint8Array(32))),
410
+ });
411
+ const root = rootOf(opened.payload);
412
+ const listKey = await deriveSubkey(root, 'myapp/list-key/v1');
413
+
414
+ // With the vault open, touch the second key and give it a slot of its own.
415
+ await keeper.addPasskey(opened, {
416
+ prfOutput: secondPrfOutput,
417
+ rawCredentialId,
418
+ label,
419
+ });
420
+ ```
421
+
422
+ - `open({ prfOutput, rawCredentialId, label?, create?, find? })` opens the
423
+ vault this authenticator has a slot in. Without one it asks `find(kid)` for
424
+ a record kept elsewhere — a backup, an index — and keeps it here once it has
425
+ opened; without that it makes a new vault holding `create()`, with this
426
+ authenticator's slot. It returns `{ record, payload, vaultKey, kid, created,
427
+ found }`; keep that in memory while the vault is open, it holds the vault key.
428
+ - Pass `find` on every unlock where the vault may be kept elsewhere, not only
429
+ on a restore path. A synced passkey on a new device otherwise makes a vault
430
+ of its own, and from then on `open` finds that one here and never asks
431
+ `find` again (Le-Space/belege#524).
432
+ - An authenticator removed here is refused (`VAULT_SLOT_REMOVED`), never given
433
+ a new vault: for an application whose books existed before the vault, that
434
+ would be a stale copy that looks like the real books.
435
+ - `addPasskey(opened, { prfOutput, rawCredentialId, label? })` gives one more
436
+ authenticator a slot. One key opens one vault per store: a key that already
437
+ opens another vault here is refused (`VAULT_SLOT_EXISTS`).
438
+ - `removePasskey(opened, kid)` takes a slot out by its id, so a key that is not
439
+ at hand can go too. It revokes nothing already opened.
440
+ - `renew(opened, { answers, removed?, payload? })` makes a new vault key and
441
+ vault id, with a slot for every authenticator that stays. Every one of them
442
+ answers with its PRF output, checked against its slot, so none is left out
443
+ by mistake; the ones in `removed` are taken out. A new `payload` replaces
444
+ keys a removed authenticator must not know.
445
+ - `replacePayload(opened, payload)`, `status(rawCredentialId)` (is there a
446
+ vault here for this key, without asking it?) and `records()`.
447
+
448
+ The store holds a JSON array under `key`, one record per vault:
449
+ `{ vault, slots: [{ kid, label, credentialId, addedAt }], removed: [kid] }`.
450
+ `slots` names in public which authenticator each slot belongs to; `removed`
451
+ remembers the keys taken out here; fields the keeper does not know are kept.
452
+ It is the format Belege has kept since Le-Space/belege#406, and
453
+ `tests/vault-keeper.test.js` opens a record Belege's own code wrote. An
454
+ unreadable store is refused (`VAULT_STORE_UNREADABLE`) and never overwritten:
455
+ it may hold the only copy of a vault. The keeper holds no secret, and two pages
456
+ writing one store at once means the last write wins.
457
+
458
+ `rootPayload(root)` and `rootOf(payload)` write and read the simplest payload,
459
+ `{"version":1,"root":"<hex>"}`: one 32-byte root, every other key derived from
460
+ it with `deriveSubkey(root, info)`, so the vault never changes when the
461
+ application needs one more key. Belege's vaults hold exactly that.
462
+
463
+ More refusals from the keeper, as `VaultError` codes: `VAULT_STORE_UNREADABLE`,
464
+ `VAULT_SLOT_REMOVED`, and `VAULT_SLOT_IN_USE` — the authenticator that opened
465
+ the vault cannot remove itself, or be left out of a renewal.
466
+
389
467
  #### An identity that is nobody's passkey: `createSecretSigner(secret, { info })`
390
468
 
391
469
  Since 0.10.0. A database whose access controller is rooted at one passkey's
@@ -523,12 +601,13 @@ Key exports:
523
601
 
524
602
  - `WebAuthnVarsigProvider`
525
603
  - `createWebAuthnVarsigIdentity(options)`
526
- - `createWebAuthnVarsigIdentities(options)`
527
- - `encodeIdentityValue(identity)`
528
- - `decodeVarsigIdentityFromBytes(bytes)`
529
- - `verifyVarsigIdentity(identity)`
530
- - `createIpfsIdentityStorage(options)`
531
- - `wrapWithVarsigVerification(value)`
604
+ - `createWebAuthnVarsigIdentities(identity, domainLabels?, storage?, fallbackIdentities?)`
605
+ — synchronous
606
+ - `encodeIdentityValue(value)` — resolves to `{ hash, bytes }`
607
+ - `decodeVarsigIdentityFromBytes(bytes, domainLabels?)`
608
+ - `verifyVarsigIdentity(identity, domainLabels?)`
609
+ - `createIpfsIdentityStorage(ipfs)`
610
+ - `wrapWithVarsigVerification(defaultIdentities, ipfs?, domainLabels?)`
532
611
  - `DEFAULT_DOMAIN_LABELS`
533
612
  - `storeWebAuthnVarsigCredential(credential, key?)`
534
613
  - `loadWebAuthnVarsigCredential(key?)`
@@ -549,8 +628,8 @@ const identity = await createWebAuthnVarsigIdentity({ credential });
549
628
  `WebAuthnVarsigProvider.createCredential(options)` takes `userId`,
550
629
  `displayName`, `domain`, `discoverableCredentials`, `forceP256` (offer only
551
630
  P-256), and `authenticatorType` / `authenticatorAttachment` as
552
- `WebAuthnDIDProvider.createCredential` does. Before 0.9.1 it dropped the choice
553
- of authenticator the same way and offered every kind.
631
+ `WebAuthnDIDProvider.createCredential` does. Before 0.10.0 it dropped the
632
+ choice of authenticator the same way and offered every kind.
554
633
 
555
634
  It always requires user verification, whatever the options say. A varsig
556
635
  signature without the UV flag is refused, when it is made and when it is
@@ -582,7 +661,9 @@ import {
582
661
 
583
662
  The `create…Signer` functions register as
584
663
  `WebAuthnVarsigProvider.createCredential` does and take its options;
585
- `createWebAuthnEd25519Credential` passes on `options.authenticatorType` alone.
664
+ `createWebAuthnEd25519Credential` passes on `options.authenticatorType` alone,
665
+ and rejects when the registration fails — it never resolved to `null`, though
666
+ its JSDoc and types said so.
586
667
 
587
668
  ```js
588
669
  const signer = await createWebAuthnSigner({
@@ -621,9 +702,9 @@ const ucanSigner = signer.toUcantoSigner({
621
702
 
622
703
  The same functions as the root export, without OrbitDB: `readPrfOutput`,
623
704
  `deriveSubkey`, `deriveAesKey`, `createVault`, `openVault`, `addSlot`,
624
- `removeSlot`, `replacePayload`, `slotIdFor`, `createSecretSigner`,
625
- `PrfUnavailableError` and `VaultError`. See
626
- [PRF Keys and Vaults](#prf-keys-and-vaults).
705
+ `removeSlot`, `replacePayload`, `slotIdFor`, `createVaultKeeper`,
706
+ `rootPayload`, `rootOf`, `createSecretSigner`, `PrfUnavailableError` and
707
+ `VaultError`. See [PRF Keys and Vaults](#prf-keys-and-vaults).
627
708
 
628
709
  ### P-256 Wallet Primitives
629
710
 
@@ -668,6 +749,25 @@ const { did, publicKey, archive } = await client.generateEd25519Identity();
668
749
  const signature = await client.sign(new TextEncoder().encode('entry'));
669
750
  ```
670
751
 
752
+ ## Vault Export
753
+
754
+ Since 0.12.0. The passkey vault on its own, for an application that keeps its
755
+ data outside OrbitDB: `readPrfOutput`, `deriveSubkey`, `deriveAesKey`,
756
+ `createVault`, `openVault`, `addSlot`, `removeSlot`, `replacePayload`,
757
+ `slotIdFor`, `createVaultKeeper`, `rootPayload`, `rootOf`,
758
+ `PrfUnavailableError` and `VaultError`. Its import graph holds no package at
759
+ all — WebCrypto and `navigator.credentials` are all it needs, and
760
+ `tests/vault-entry.test.js` keeps it that way. `createSecretSigner` is not in
761
+ it: it needs `@libp2p/crypto`.
762
+
763
+ ```js
764
+ import {
765
+ createVaultKeeper,
766
+ readPrfOutput,
767
+ rootPayload,
768
+ } from '@le-space/orbitdb-identity-provider-webauthn-did/vault';
769
+ ```
770
+
671
771
  ## Verification Export
672
772
 
673
773
  ```js
@@ -699,6 +799,7 @@ The package publishes declaration files:
699
799
  - `types/index.d.ts`
700
800
  - `types/keystore.d.ts`
701
801
  - `types/standalone.d.ts`
802
+ - `types/vault.d.ts`
702
803
  - `types/verification.d.ts`
703
804
 
704
805
  They are connected through `package.json` `exports.types`, so TypeScript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@le-space/orbitdb-identity-provider-webauthn-did",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "WebAuthn-based DID identity provider for OrbitDB for hardware-secured wallets and biometric Passkey authentication",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
@@ -21,6 +21,10 @@
21
21
  "./keystore": {
22
22
  "types": "./types/keystore.d.ts",
23
23
  "import": "./src/keystore/index.js"
24
+ },
25
+ "./vault": {
26
+ "types": "./types/vault.d.ts",
27
+ "import": "./src/vault/index.js"
24
28
  }
25
29
  },
26
30
  "scripts": {
@@ -52,7 +56,8 @@
52
56
  "version": "git add -A",
53
57
  "postversion": "git push && git push --tags",
54
58
  "test:two-peer": "playwright test tests/webauthn-two-peer-replication.test.js --project=chromium --reporter=line",
55
- "test:node": "playwright test --config=playwright.node.config.js"
59
+ "test:node": "playwright test --config=playwright.node.config.js",
60
+ "test:wallet-fork": "PASSKEY_WALLET_FORK_URL=${PASSKEY_WALLET_FORK_URL:-https://sepolia.drpc.org} playwright test --config=playwright.fork.config.js"
56
61
  },
57
62
  "keywords": [
58
63
  "orbitdb",
@@ -146,7 +151,7 @@
146
151
  "iso-web": "^3.1.2",
147
152
  "iso-webauthn-varsig": "npm:@le-space/iso-webauthn-varsig@^0.3.0",
148
153
  "conf>ajv": "^8.18.0",
149
- "ajv>fast-uri": "^3.1.8"
154
+ "ajv>fast-uri": "^3.1.9"
150
155
  },
151
156
  "onlyBuiltDependencies": [
152
157
  "@ipshipyard/node-datachannel",
package/src/errors.js CHANGED
@@ -11,6 +11,9 @@ export const ERROR_CODES = Object.freeze({
11
11
  VAULT_LOCKED: 'VAULT_LOCKED',
12
12
  VAULT_SLOT_EXISTS: 'VAULT_SLOT_EXISTS',
13
13
  VAULT_LAST_SLOT: 'VAULT_LAST_SLOT',
14
+ VAULT_STORE_UNREADABLE: 'VAULT_STORE_UNREADABLE',
15
+ VAULT_SLOT_REMOVED: 'VAULT_SLOT_REMOVED',
16
+ VAULT_SLOT_IN_USE: 'VAULT_SLOT_IN_USE',
14
17
  });
15
18
 
16
19
  export class WebAuthnIdentityError extends Error {
@@ -96,6 +99,10 @@ export class PrfUnavailableError extends KeystoreEncryptionError {
96
99
  * has no slot in it (`VAULT_NO_SLOT`), the key does not open it or it was
97
100
  * altered (`VAULT_LOCKED`), the authenticator already has a slot
98
101
  * (`VAULT_SLOT_EXISTS`), or the slot is the last one (`VAULT_LAST_SLOT`).
102
+ * From the vault keeper also: the vaults kept in a store cannot be read
103
+ * (`VAULT_STORE_UNREADABLE`), the authenticator was removed from the vault
104
+ * kept there (`VAULT_SLOT_REMOVED`), or it is the one that opened the vault
105
+ * and so cannot remove itself (`VAULT_SLOT_IN_USE`).
99
106
  */
100
107
  export class VaultError extends KeystoreEncryptionError {
101
108
  constructor(message, options = {}) {
package/src/index.js CHANGED
@@ -308,6 +308,12 @@ export {
308
308
  recoverPublicKeyCandidates,
309
309
  } from './webauthn/recovery.js';
310
310
  export { restoreIdentityFromAuthenticator } from './webauthn/restore.js';
311
+ // OrbitDB's identity cache refuses another session of the same passkey
312
+ // (orbitdb/orbitdb#1258); this is the provider's answer until it is fixed there.
313
+ export {
314
+ withoutStaleIdentityCache,
315
+ createIdentities,
316
+ } from './keystore/stale-identity-cache.js';
311
317
  // The two halves of it, for a caller that already holds one: a PRF output, or
312
318
  // a credential it wants the output for.
313
319
  export {
@@ -329,6 +335,12 @@ export {
329
335
  replacePayload,
330
336
  slotIdFor,
331
337
  } from './keystore/vault.js';
338
+ // Where the vaults are kept, and what every unlock does with them.
339
+ export {
340
+ createVaultKeeper,
341
+ rootPayload,
342
+ rootOf,
343
+ } from './keystore/vault-keeper.js';
332
344
  // An identity that is nobody's passkey — the books every key of their owner
333
345
  // writes as, its secret kept in a vault.
334
346
  export { createSecretSigner } from './keystore/secret-signer.js';
@@ -6,4 +6,8 @@ export { default } from './encryption.js';
6
6
  export * from './provider.js';
7
7
  export * from './session-keystore.js';
8
8
  export { createSecretSigner } from './secret-signer.js';
9
+ export {
10
+ withoutStaleIdentityCache,
11
+ createIdentities,
12
+ } from './stale-identity-cache.js';
9
13
  export { PrfUnavailableError } from '../errors.js';
@@ -0,0 +1,97 @@
1
+ /**
2
+ * OrbitDB's verified-identity cache keyed by the wrong thing, worked around.
3
+ *
4
+ * `Identities.verifyIdentity` (@orbitdb/core 4) remembers a verified identity
5
+ * under `signatures.id` and, on a hit, answers `isEqual(identity, cached)`.
6
+ * With this provider the PRF-derived signing key gives the *same*
7
+ * `signatures.id` in every session of a passkey, while `signatures.publicKey`
8
+ * — the WebAuthn assertion that binds the key to the DID — is new per session.
9
+ * So once one session's identity is in the cache, every other session of the
10
+ * same passkey is refused: "Could not append entry: Key … is not allowed to
11
+ * write to the log". Whichever session's entries are verified first owns the
12
+ * cache: after a join (a restore from backup), after a sync (a peer that held
13
+ * the list), or simply the browser's own earlier entries coming back.
14
+ *
15
+ * Seen in Le-Space/belege's restore (belege#259) and in simple-todo's
16
+ * archived01 (a phone restoring a laptop's archive, Le-Space/simple-todo#79).
17
+ * Upstream: orbitdb/orbitdb#1258. The right fix is a cache keyed by identity
18
+ * hash; until OrbitDB has it, this is the provider's own answer, so that no
19
+ * application has to carry a copy.
20
+ *
21
+ * Where OrbitDB says no, an identity of this provider is checked again the way
22
+ * an uncached one would be: the shape (`isIdentity`), the id signature over
23
+ * the public key (`identities.verify`), and this provider's binding of the key
24
+ * to the DID (`verifyIdentity`, the check GHSA-326j-4cc3-4rrg added). Other
25
+ * providers' identities, and anything that fails those checks, stay refused.
26
+ */
27
+ import { OrbitDBWebAuthnIdentityProviderFunction } from './provider.js';
28
+
29
+ /**
30
+ * OrbitDB's own shape check (`identities/identity.js`, `isIdentity`), kept
31
+ * here rather than imported: this file is reachable from the `./keystore`
32
+ * entry, which does not load `@orbitdb/core` at import time — the peer
33
+ * dependency is the application's, and @orbitdb/core 4.0.0 cannot even be
34
+ * imported from a bare install (it uses `it-drain` without declaring it).
35
+ *
36
+ * @param {any} identity
37
+ */
38
+ function isIdentity(identity) {
39
+ return Boolean(
40
+ identity &&
41
+ identity.id &&
42
+ identity.hash &&
43
+ identity.bytes &&
44
+ identity.publicKey &&
45
+ identity.signatures &&
46
+ identity.signatures.id &&
47
+ identity.signatures.publicKey &&
48
+ identity.type
49
+ );
50
+ }
51
+
52
+ /**
53
+ * Wrap an `Identities` instance so that another session of the same passkey
54
+ * is verified rather than refused. Idempotent in effect; wrapping twice only
55
+ * checks twice.
56
+ *
57
+ * @template {{ verifyIdentity: (identity: any) => Promise<boolean>, verify: (signature: string, publicKey: string, data: string) => Promise<boolean> }} T
58
+ * @param {T} identities - from `Identities(...)` (or `createIdentities` below)
59
+ * @returns {T} the same instance, its `verifyIdentity` replaced
60
+ */
61
+ export function withoutStaleIdentityCache(identities) {
62
+ if (
63
+ !identities ||
64
+ typeof identities.verifyIdentity !== 'function' ||
65
+ typeof identities.verify !== 'function'
66
+ ) {
67
+ throw new TypeError(
68
+ 'withoutStaleIdentityCache needs an OrbitDB Identities instance (verifyIdentity, verify)'
69
+ );
70
+ }
71
+ const cached = identities.verifyIdentity;
72
+ identities.verifyIdentity = async (identity) => {
73
+ if (await cached(identity)) return true;
74
+ if (
75
+ !isIdentity(identity) ||
76
+ identity.type !== OrbitDBWebAuthnIdentityProviderFunction.type
77
+ ) {
78
+ return false;
79
+ }
80
+ const { id, publicKey, signatures } = identity;
81
+ if (!(await identities.verify(signatures.id, publicKey, id))) return false;
82
+ return OrbitDBWebAuthnIdentityProviderFunction.verifyIdentity(identity);
83
+ };
84
+ return identities;
85
+ }
86
+
87
+ /**
88
+ * `Identities(options)` from @orbitdb/core, already wrapped: what an
89
+ * application that builds identities for a passkey should call. The peer
90
+ * dependency is loaded when this is called, not when the module is.
91
+ *
92
+ * @param {Record<string, unknown>} [options] - `{ ipfs, keystore, storage, path }`, as OrbitDB takes them
93
+ */
94
+ export async function createIdentities(options) {
95
+ const { Identities } = await import('@orbitdb/core');
96
+ return withoutStaleIdentityCache(await Identities(options));
97
+ }