@interop/wallet-core 0.40.0 → 0.42.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.
Files changed (153) hide show
  1. package/README.md +17 -3
  2. package/dist/clients/listing.d.ts +4 -3
  3. package/dist/clients/listing.d.ts.map +1 -1
  4. package/dist/clients/listing.js.map +1 -1
  5. package/dist/clients/policy.d.ts +3 -2
  6. package/dist/clients/policy.d.ts.map +1 -1
  7. package/dist/clients/policy.js +3 -3
  8. package/dist/clients/policy.js.map +1 -1
  9. package/dist/clients/revocation.d.ts +14 -4
  10. package/dist/clients/revocation.d.ts.map +1 -1
  11. package/dist/clients/revocation.js +29 -14
  12. package/dist/clients/revocation.js.map +1 -1
  13. package/dist/clients/rosterPolicy.d.ts +22 -15
  14. package/dist/clients/rosterPolicy.d.ts.map +1 -1
  15. package/dist/clients/rosterPolicy.js +32 -15
  16. package/dist/clients/rosterPolicy.js.map +1 -1
  17. package/dist/descriptors/index.d.ts +1 -1
  18. package/dist/descriptors/index.d.ts.map +1 -1
  19. package/dist/descriptors/index.js +1 -1
  20. package/dist/descriptors/index.js.map +1 -1
  21. package/dist/descriptors/logSource.d.ts +58 -6
  22. package/dist/descriptors/logSource.d.ts.map +1 -1
  23. package/dist/descriptors/logSource.js +57 -15
  24. package/dist/descriptors/logSource.js.map +1 -1
  25. package/dist/enrollment/enrollment.d.ts +30 -2
  26. package/dist/enrollment/enrollment.d.ts.map +1 -1
  27. package/dist/enrollment/enrollment.js +58 -30
  28. package/dist/enrollment/enrollment.js.map +1 -1
  29. package/dist/enrollment/index.d.ts +4 -1
  30. package/dist/enrollment/index.d.ts.map +1 -1
  31. package/dist/enrollment/index.js +4 -1
  32. package/dist/enrollment/index.js.map +1 -1
  33. package/dist/keyring/fetch.d.ts +12 -0
  34. package/dist/keyring/fetch.d.ts.map +1 -1
  35. package/dist/keyring/fetch.js +0 -2
  36. package/dist/keyring/fetch.js.map +1 -1
  37. package/dist/keyring/index.d.ts +7 -6
  38. package/dist/keyring/index.d.ts.map +1 -1
  39. package/dist/keyring/index.js +7 -6
  40. package/dist/keyring/index.js.map +1 -1
  41. package/dist/keyring/kdf.d.ts +22 -6
  42. package/dist/keyring/kdf.d.ts.map +1 -1
  43. package/dist/keyring/kdf.js +9 -14
  44. package/dist/keyring/kdf.js.map +1 -1
  45. package/dist/keyring/record.d.ts +30 -10
  46. package/dist/keyring/record.d.ts.map +1 -1
  47. package/dist/keyring/record.js +31 -15
  48. package/dist/keyring/record.js.map +1 -1
  49. package/dist/keys/index.d.ts +5 -3
  50. package/dist/keys/index.d.ts.map +1 -1
  51. package/dist/keys/index.js +3 -2
  52. package/dist/keys/index.js.map +1 -1
  53. package/dist/keys/rosterLogStore.d.ts +22 -2
  54. package/dist/keys/rosterLogStore.d.ts.map +1 -1
  55. package/dist/keys/rosterLogStore.js +40 -20
  56. package/dist/keys/rosterLogStore.js.map +1 -1
  57. package/dist/keys/rosterStore.d.ts +13 -1
  58. package/dist/keys/rosterStore.d.ts.map +1 -1
  59. package/dist/keys/rosterStore.js +18 -0
  60. package/dist/keys/rosterStore.js.map +1 -1
  61. package/dist/keys/userKeyCascade.d.ts +10 -3
  62. package/dist/keys/userKeyCascade.d.ts.map +1 -1
  63. package/dist/keys/userKeyCascade.js +10 -4
  64. package/dist/keys/userKeyCascade.js.map +1 -1
  65. package/dist/keys/userKeyRoster.d.ts +29 -21
  66. package/dist/keys/userKeyRoster.d.ts.map +1 -1
  67. package/dist/keys/userKeyRoster.js +47 -23
  68. package/dist/keys/userKeyRoster.js.map +1 -1
  69. package/dist/recovery/index.d.ts +21 -11
  70. package/dist/recovery/index.d.ts.map +1 -1
  71. package/dist/recovery/index.js +19 -10
  72. package/dist/recovery/index.js.map +1 -1
  73. package/dist/recovery/recoveryCode.d.ts +5 -17
  74. package/dist/recovery/recoveryCode.d.ts.map +1 -1
  75. package/dist/recovery/recoveryCode.js +4 -14
  76. package/dist/recovery/recoveryCode.js.map +1 -1
  77. package/dist/recovery/recoveryDelegation.d.ts +151 -0
  78. package/dist/recovery/recoveryDelegation.d.ts.map +1 -0
  79. package/dist/recovery/recoveryDelegation.js +261 -0
  80. package/dist/recovery/recoveryDelegation.js.map +1 -0
  81. package/dist/recovery/recoveryWebvh.d.ts +13 -22
  82. package/dist/recovery/recoveryWebvh.d.ts.map +1 -1
  83. package/dist/recovery/recoveryWebvh.js +53 -139
  84. package/dist/recovery/recoveryWebvh.js.map +1 -1
  85. package/dist/resourceLog/append.d.ts +12 -3
  86. package/dist/resourceLog/append.d.ts.map +1 -1
  87. package/dist/resourceLog/append.js +17 -10
  88. package/dist/resourceLog/append.js.map +1 -1
  89. package/dist/resourceLog/index.d.ts +1 -1
  90. package/dist/resourceLog/index.d.ts.map +1 -1
  91. package/dist/resourceLog/index.js +1 -1
  92. package/dist/resourceLog/index.js.map +1 -1
  93. package/dist/resourceLog/pin.d.ts +46 -8
  94. package/dist/resourceLog/pin.d.ts.map +1 -1
  95. package/dist/resourceLog/pin.js +32 -7
  96. package/dist/resourceLog/pin.js.map +1 -1
  97. package/dist/resourceLog/seal.d.ts +4 -1
  98. package/dist/resourceLog/seal.d.ts.map +1 -1
  99. package/dist/resourceLog/seal.js +6 -2
  100. package/dist/resourceLog/seal.js.map +1 -1
  101. package/dist/unlock/index.d.ts +40 -0
  102. package/dist/unlock/index.d.ts.map +1 -0
  103. package/dist/unlock/index.js +36 -0
  104. package/dist/unlock/index.js.map +1 -0
  105. package/dist/unlock/ladder.d.ts +100 -0
  106. package/dist/unlock/ladder.d.ts.map +1 -0
  107. package/dist/unlock/ladder.js +144 -0
  108. package/dist/unlock/ladder.js.map +1 -0
  109. package/dist/unlock/selfEnroll.d.ts +83 -0
  110. package/dist/unlock/selfEnroll.d.ts.map +1 -0
  111. package/dist/unlock/selfEnroll.js +126 -0
  112. package/dist/unlock/selfEnroll.js.map +1 -0
  113. package/dist/unlock/standingClient.d.ts +68 -0
  114. package/dist/unlock/standingClient.d.ts.map +1 -0
  115. package/dist/unlock/standingClient.js +81 -0
  116. package/dist/unlock/standingClient.js.map +1 -0
  117. package/dist/unlock/standingWebvh.d.ts +153 -0
  118. package/dist/unlock/standingWebvh.d.ts.map +1 -0
  119. package/dist/unlock/standingWebvh.js +370 -0
  120. package/dist/unlock/standingWebvh.js.map +1 -0
  121. package/dist/unlock/unlockRecord.d.ts +207 -0
  122. package/dist/unlock/unlockRecord.d.ts.map +1 -0
  123. package/dist/unlock/unlockRecord.js +435 -0
  124. package/dist/unlock/unlockRecord.js.map +1 -0
  125. package/dist/webvh/didWebvh.d.ts +166 -7
  126. package/dist/webvh/didWebvh.d.ts.map +1 -1
  127. package/dist/webvh/didWebvh.js +286 -31
  128. package/dist/webvh/didWebvh.js.map +1 -1
  129. package/dist/webvh/index.d.ts +15 -3
  130. package/dist/webvh/index.d.ts.map +1 -1
  131. package/dist/webvh/index.js +14 -3
  132. package/dist/webvh/index.js.map +1 -1
  133. package/dist/webvh/keyAgreement.d.ts +72 -0
  134. package/dist/webvh/keyAgreement.d.ts.map +1 -0
  135. package/dist/webvh/keyAgreement.js +47 -0
  136. package/dist/webvh/keyAgreement.js.map +1 -0
  137. package/dist/webvh/listClients.d.ts +69 -20
  138. package/dist/webvh/listClients.d.ts.map +1 -1
  139. package/dist/webvh/listClients.js +102 -14
  140. package/dist/webvh/listClients.js.map +1 -1
  141. package/dist/webvh/revokeClient.d.ts +21 -11
  142. package/dist/webvh/revokeClient.d.ts.map +1 -1
  143. package/dist/webvh/revokeClient.js +31 -14
  144. package/dist/webvh/revokeClient.js.map +1 -1
  145. package/dist/webvh/verifyLog.d.ts +15 -2
  146. package/dist/webvh/verifyLog.d.ts.map +1 -1
  147. package/dist/webvh/verifyLog.js +22 -3
  148. package/dist/webvh/verifyLog.js.map +1 -1
  149. package/package.json +11 -5
  150. package/dist/recovery/recoveryRecord.d.ts +0 -171
  151. package/dist/recovery/recoveryRecord.d.ts.map +0 -1
  152. package/dist/recovery/recoveryRecord.js +0 -290
  153. package/dist/recovery/recoveryRecord.js.map +0 -1
@@ -0,0 +1,435 @@
1
+ /*!
2
+ * Copyright (c) 2026 Interop Alliance. All rights reserved.
3
+ */
4
+ /**
5
+ * The unlock record codec for credentials with standing authority: the
6
+ * envelope stored as the one resource of a credential's unlock Space, for
7
+ * every unlock method in the standing posture -- a passphrase, a passkey PRF
8
+ * output, or a recovery code. Its frame is the ordinary keyring record's
9
+ * (`{ version, encryption, wrapped, proof }`), extended with three
10
+ * record-kind members the proof also covers:
11
+ *
12
+ * - `wrapped` (the SHELL): the account core -- controller, optional email,
13
+ * pointer, bind timestamp -- sealed to the credential's unlock KAK. Carried
14
+ * VERBATIM through every re-mint, so the bind timestamp (which apps pin
15
+ * freshness on) and the email survive a re-mint that cannot decrypt them.
16
+ * - `bridge`: the pre-minted PUT-on-`did.jsonl` delegation, sealed in its own
17
+ * self-contained `{ encryption, wrapped }` member. The one member a re-mint
18
+ * replaces: revoking the client that signed the delegation rots it, and the
19
+ * revocation cascade re-seals a fresh one to the credential's unlock KAK
20
+ * public half ({@link remintUnlockRecordBridge}).
21
+ * - `ladder` (standing credentials only): the random 32-byte update-key
22
+ * ladder seed (`./ladder`), sealed in its own `{ encryption, wrapped }`
23
+ * member and carried VERBATIM through re-mints -- a re-mint can never read
24
+ * or replace a credential's update authority. A recovery code carries none
25
+ * (its single update key derives from the code bytes).
26
+ *
27
+ * The record splits into a credential-authenticated core and a re-mintable
28
+ * shell exactly as the recovery record did. The core is the account binding
29
+ * plus the ladder seed, authenticated by the `binding` frame member: an HMAC
30
+ * tag under a key derived from the credential itself, computed at bind time
31
+ * and verified BEFORE the pointer is trusted. Only the binder and the
32
+ * credential holder ever hold that key -- the storage host never does -- so a
33
+ * host-forged record redirecting login (or a host-substituted ladder seed)
34
+ * fails the tag however it is encrypted or signed. The tag rides the frame in
35
+ * the clear (it reveals nothing), so the re-mint path, which cannot decrypt
36
+ * the record or recompute the tag, preserves it verbatim. The consequence: a
37
+ * re-mint can never move the record to another account, and an account that
38
+ * moves hosts must rebind its credentials.
39
+ *
40
+ * The shell's signer is mixed, as before: at bind time the record is signed
41
+ * by the credential-derived unlock key (verified before decryption); a
42
+ * re-mint signs with the acting client's account verification method, which
43
+ * comes back as a pending proof state the caller settles against the account
44
+ * document the credential-authenticated pointer names.
45
+ */
46
+ import { base64urlnopad } from '@scure/base';
47
+ import { equalBytes } from '@noble/ciphers/utils.js';
48
+ import { hmac } from '@noble/hashes/hmac.js';
49
+ import { sha256 } from '@noble/hashes/sha2.js';
50
+ import { KEYRING_RECORD_VERSION, mintRecordEncryption, parseRecordCreatedAt, parseRecordFrame, parseRecordPointer, recordCipher, recordCreatedAtStamp, recordProofKeyMultibase, recordSealCipher, signRecordFrame, verifyRecordProof } from '../keyring/record.js';
51
+ import { LADDER_SEED_BYTES } from './ladder.js';
52
+ /**
53
+ * The context label mixed into the binding MAC input, versioning the tag
54
+ * construction. Permanent -- changing it orphans every bound credential.
55
+ * Supersedes the recovery record's `freewallet/recovery/binding/v1` (the
56
+ * standing layout is a re-provision boundary; issued codes are re-issued).
57
+ */
58
+ const UNLOCK_BINDING_CONTEXT = 'freewallet/unlock/binding/v2';
59
+ /**
60
+ * An unlock record's account binding is absent, malformed, or does not verify
61
+ * under the credential's binding MAC key. Its own class, distinct from a
62
+ * proof or decrypt failure: this is the refusal that says the record's
63
+ * account core (and ladder seed, where one rides) was not written by a holder
64
+ * of this credential -- a forged record redirecting login at another account,
65
+ * or a record from before the account moved hosts (either way the credential
66
+ * must be rebound).
67
+ */
68
+ export class UnlockBindingError extends Error {
69
+ constructor(message) {
70
+ super(message);
71
+ this.name = 'UnlockBindingError';
72
+ }
73
+ }
74
+ /**
75
+ * The deterministic MAC input over a record's credential-authenticated core:
76
+ * a JSON array of the context label, the binding values, and the ladder seed
77
+ * (base64url, empty for a credential that carries none), so no delimiter
78
+ * ambiguity can make two cores collide.
79
+ */
80
+ function bindingMacInput({ controller, pointer, ladderSeed }) {
81
+ return new TextEncoder().encode(JSON.stringify([
82
+ UNLOCK_BINDING_CONTEXT,
83
+ controller,
84
+ pointer.did ?? '',
85
+ pointer.spaceId,
86
+ pointer.host,
87
+ ladderSeed ? base64urlnopad.encode(ladderSeed) : ''
88
+ ]));
89
+ }
90
+ /**
91
+ * Computes the account-binding tag: HMAC-SHA-256 over the core values under
92
+ * the credential-derived binding MAC key, base64url (no pad). Bind time calls
93
+ * it to stamp the record; login recomputes it to verify.
94
+ *
95
+ * @param options {object}
96
+ * @param options.bindingMacKey {Uint8Array} the credential-derived MAC key
97
+ * @param options.controller {string} the account controller
98
+ * @param options.pointer {AccountPointer} the account pointer
99
+ * @param [options.ladderSeed] {Uint8Array} the ladder seed, for a standing
100
+ * credential
101
+ * @returns {string}
102
+ */
103
+ export function computeUnlockBinding({ bindingMacKey, controller, pointer, ladderSeed }) {
104
+ return base64urlnopad.encode(hmac(sha256, bindingMacKey, bindingMacInput({
105
+ controller,
106
+ pointer,
107
+ ...(ladderSeed ? { ladderSeed } : {})
108
+ })));
109
+ }
110
+ /**
111
+ * Reads the `binding` frame member off a stored unlock record without
112
+ * decrypting anything. The re-mint path uses it to confirm the record is
113
+ * re-mintable before touching it. Refuses a record with no binding: such a
114
+ * record predates the credential-authenticated core and cannot be re-minted
115
+ * -- its credential must be rebound.
116
+ *
117
+ * @param options {object}
118
+ * @param options.record {unknown} the stored record envelope
119
+ * @returns {string}
120
+ */
121
+ export function unlockRecordBinding({ record }) {
122
+ const { binding } = (record ?? {});
123
+ if (typeof binding !== 'string' || !binding) {
124
+ throw new UnlockBindingError('The unlock record carries no credential-authenticated account ' +
125
+ 'binding; the credential must be rebound.');
126
+ }
127
+ return binding;
128
+ }
129
+ /**
130
+ * Shape-checks a sealed record member (`{ encryption, wrapped }`).
131
+ *
132
+ * @param options {object}
133
+ * @param options.value {unknown} the frame member
134
+ * @param options.name {string} names the member in the refusal
135
+ * @returns {SealedRecordMember}
136
+ */
137
+ function parseSealedMember({ value, name }) {
138
+ const member = value;
139
+ if (member === null ||
140
+ typeof member !== 'object' ||
141
+ member.encryption === null ||
142
+ typeof member.encryption !== 'object' ||
143
+ member.wrapped === undefined ||
144
+ member.wrapped === null) {
145
+ throw new Error(`The unlock record's ${name} member is malformed.`);
146
+ }
147
+ return member;
148
+ }
149
+ /**
150
+ * Seals one self-contained record member to a KAK: a fresh one-epoch
151
+ * descriptor plus the envelope. Sealing needs no key-agreement secret, which
152
+ * is what lets the re-mint path (holding only the credential's unlock KAK
153
+ * public half) build a fresh bridge member.
154
+ *
155
+ * @param options {object}
156
+ * @param options.data {object} the member plaintext
157
+ * @param options.keyAgreementKey {IKeyAgreementKey} the credential's unlock
158
+ * KAK (public half suffices)
159
+ * @returns {Promise<SealedRecordMember>}
160
+ */
161
+ async function sealMember({ data, keyAgreementKey }) {
162
+ const encryption = await mintRecordEncryption({ keyAgreementKey });
163
+ const cipher = await recordSealCipher({ encryption });
164
+ const { envelope } = await cipher.encrypt({
165
+ data: data
166
+ });
167
+ return { encryption, wrapped: envelope };
168
+ }
169
+ /**
170
+ * Opens one self-contained record member with the credential's unlock KAK.
171
+ *
172
+ * @param options {object}
173
+ * @param options.member {SealedRecordMember}
174
+ * @param options.keyAgreementKey {IKeyAgreementKey}
175
+ * @param options.keyResolver {IKeyResolver}
176
+ * @returns {Promise<unknown>} the member plaintext
177
+ */
178
+ async function openMember({ member, keyAgreementKey, keyResolver }) {
179
+ const cipher = await recordCipher({
180
+ keyAgreementKey,
181
+ keyResolver,
182
+ encryption: member.encryption
183
+ });
184
+ return cipher.decrypt({ envelope: member.wrapped });
185
+ }
186
+ /**
187
+ * Wraps an unlock record at bind time: the shell (controller, optional
188
+ * email, pointer, timestamp) sealed to the credential's unlock KAK, the
189
+ * bridge delegation and the optional ladder seed sealed as their own
190
+ * members, the credential-authenticated binding computed over the core, and
191
+ * the whole frame signed -- by the credential-derived unlock key at bind
192
+ * time, or by an acting client's account key where a ceremony rebuilds a
193
+ * record whole (the reader settles the mixed-signer policy either way).
194
+ *
195
+ * @param options {object}
196
+ * @param options.controller {string} the account controller
197
+ * @param [options.email] {string} the account email, when known
198
+ * @param options.pointer {AccountPointer} the account pointer
199
+ * @param options.delegation {IZcap} the PUT-on-`did.jsonl` delegation to
200
+ * the credential-derived signing DID
201
+ * @param [options.ladderSeed] {Uint8Array} the update-key ladder seed, for
202
+ * a standing credential (a recovery code carries none)
203
+ * @param options.keyAgreementKey {IKeyAgreementKey} the credential's unlock
204
+ * KAK -- its public half is all the wrap uses
205
+ * @param options.signer {RecordSigner} the signing key
206
+ * @param options.bindingMacKey {Uint8Array} the credential-derived binding
207
+ * MAC key
208
+ * @param [options.createdAt] {string} the bind timestamp to stamp, as an
209
+ * ISO string; defaults to now. Supplied by a caller that pins record
210
+ * freshness.
211
+ * @returns {Promise<SignedUnlockRecord>}
212
+ */
213
+ export async function wrapUnlockRecord({ controller, email, pointer, delegation, ladderSeed, keyAgreementKey, signer, bindingMacKey, createdAt }) {
214
+ if (ladderSeed && ladderSeed.length !== LADDER_SEED_BYTES) {
215
+ throw new Error('The ladder seed must be 32 bytes.');
216
+ }
217
+ const binding = computeUnlockBinding({
218
+ bindingMacKey,
219
+ controller,
220
+ pointer,
221
+ ...(ladderSeed ? { ladderSeed } : {})
222
+ });
223
+ const shell = await sealMember({
224
+ data: {
225
+ controller,
226
+ ...(email ? { email } : {}),
227
+ pointer: {
228
+ ...(pointer.did ? { did: pointer.did } : {}),
229
+ spaceId: pointer.spaceId,
230
+ host: pointer.host
231
+ },
232
+ createdAt: recordCreatedAtStamp({ createdAt })
233
+ },
234
+ keyAgreementKey
235
+ });
236
+ const bridge = await sealMember({ data: { delegation }, keyAgreementKey });
237
+ const ladder = ladderSeed
238
+ ? await sealMember({
239
+ data: { ladderSeed: base64urlnopad.encode(ladderSeed) },
240
+ keyAgreementKey
241
+ })
242
+ : undefined;
243
+ return (await signRecordFrame({
244
+ version: KEYRING_RECORD_VERSION,
245
+ encryption: shell.encryption,
246
+ wrapped: shell.wrapped,
247
+ signer,
248
+ members: { binding, bridge, ...(ladder ? { ladder } : {}) }
249
+ }));
250
+ }
251
+ /**
252
+ * Unwraps and validates an unlock record: the ordinary keyring-record frame
253
+ * checks plus the required pointer, bridge delegation, and
254
+ * credential-authenticated binding. A record without a bridge member is not
255
+ * an unlock record in the standing layout (an ordinary keyring record found
256
+ * under a credential's unlock Space would mean a torn bind) and is refused.
257
+ *
258
+ * The binding is verified before the contents are returned: the decrypted
259
+ * core must carry the tag the credential-derived MAC key computes over it, or
260
+ * the record is refused as forged ({@link UnlockBindingError}) -- the check
261
+ * that closes the host-forgery redirect, since the host never holds the MAC
262
+ * key. Nothing downstream may trust the pointer (or the ladder seed) before
263
+ * this returns.
264
+ *
265
+ * Proof verification is mixed-signer and ordered deliberately, exactly as the
266
+ * recovery record's was: a proof by the credential's own unlock key is
267
+ * verified BEFORE decryption; a proof by any other key comes back as a
268
+ * pending state naming the signer, for the caller to settle with
269
+ * `verifyRecordProof` against the verified document of the account the
270
+ * credential-authenticated pointer names.
271
+ *
272
+ * @param options {object}
273
+ * @param options.record {unknown} the stored record envelope
274
+ * @param options.keyAgreementKey {IKeyAgreementKey} the credential's unlock
275
+ * KAK
276
+ * @param options.keyResolver {IKeyResolver}
277
+ * @param options.expectedKeyMultibase {string} the credential-derived
278
+ * unlock signing key's multibase
279
+ * @param options.bindingMacKey {Uint8Array} the credential-derived binding
280
+ * MAC key the record's account binding must verify under
281
+ * @returns {Promise<{ contents: UnlockRecordContents,
282
+ * proofState: UnlockRecordProofState }>}
283
+ */
284
+ export async function unwrapUnlockRecord({ record, keyAgreementKey, keyResolver, expectedKeyMultibase, bindingMacKey }) {
285
+ const { encryption, wrapped, proof } = parseRecordFrame({
286
+ record,
287
+ label: 'unlock'
288
+ });
289
+ const binding = unlockRecordBinding({ record });
290
+ const { bridge: rawBridge, ladder: rawLadder } = record;
291
+ if (rawBridge === undefined) {
292
+ throw new Error('The unlock record carries no bridge member; it predates the standing ' +
293
+ 'layout and the credential must be rebound.');
294
+ }
295
+ const bridge = parseSealedMember({ value: rawBridge, name: 'bridge' });
296
+ const ladder = rawLadder === undefined
297
+ ? undefined
298
+ : parseSealedMember({ value: rawLadder, name: 'ladder' });
299
+ // `parseRecordFrame` shape-checks the proof of a current-version frame, so
300
+ // it is present here.
301
+ const verificationMethod = proof.verificationMethod;
302
+ const keyMultibase = recordProofKeyMultibase({
303
+ verificationMethod,
304
+ label: 'unlock'
305
+ });
306
+ let proofState = {
307
+ pending: { verificationMethod, keyMultibase }
308
+ };
309
+ if (keyMultibase === expectedKeyMultibase) {
310
+ await verifyRecordProof({
311
+ record,
312
+ allowedKeyMultibases: expectedKeyMultibase,
313
+ label: 'unlock'
314
+ });
315
+ proofState = 'verified';
316
+ }
317
+ const shell = (await openMember({
318
+ member: { encryption, wrapped },
319
+ keyAgreementKey,
320
+ keyResolver
321
+ }));
322
+ if (typeof shell.controller !== 'string' || !shell.controller) {
323
+ throw new Error('Unlock record is missing a controller.');
324
+ }
325
+ const pointer = parseRecordPointer(shell.pointer);
326
+ if (!pointer) {
327
+ throw new Error('Unlock record is missing its account pointer.');
328
+ }
329
+ const createdAt = parseRecordCreatedAt({
330
+ value: shell.createdAt,
331
+ label: 'Unlock'
332
+ });
333
+ const bridgePlaintext = (await openMember({
334
+ member: bridge,
335
+ keyAgreementKey,
336
+ keyResolver
337
+ }));
338
+ if (bridgePlaintext.delegation === null ||
339
+ typeof bridgePlaintext.delegation !== 'object') {
340
+ throw new Error('Unlock record is missing its did.jsonl delegation.');
341
+ }
342
+ let ladderSeed;
343
+ if (ladder) {
344
+ const ladderPlaintext = (await openMember({
345
+ member: ladder,
346
+ keyAgreementKey,
347
+ keyResolver
348
+ }));
349
+ if (typeof ladderPlaintext.ladderSeed !== 'string') {
350
+ throw new Error('Unlock record has a malformed ladder member.');
351
+ }
352
+ try {
353
+ ladderSeed = base64urlnopad.decode(ladderPlaintext.ladderSeed);
354
+ }
355
+ catch {
356
+ throw new Error('Unlock record has a malformed ladder member.');
357
+ }
358
+ if (ladderSeed.length !== LADDER_SEED_BYTES) {
359
+ throw new Error('Unlock record has a malformed ladder member.');
360
+ }
361
+ }
362
+ const expectedTag = hmac(sha256, bindingMacKey, bindingMacInput({
363
+ controller: shell.controller,
364
+ pointer,
365
+ ...(ladderSeed ? { ladderSeed } : {})
366
+ }));
367
+ let servedTag;
368
+ try {
369
+ servedTag = base64urlnopad.decode(binding);
370
+ }
371
+ catch {
372
+ servedTag = null;
373
+ }
374
+ // `equalBytes` is @noble/ciphers' authentication-tag comparison (no early
375
+ // exit), so the check leaks nothing through timing.
376
+ if (servedTag === null || !equalBytes(servedTag, expectedTag)) {
377
+ throw new UnlockBindingError("The unlock record's account binding does not verify under this " +
378
+ 'credential; the record is refused as forged.');
379
+ }
380
+ return {
381
+ contents: {
382
+ controller: shell.controller,
383
+ ...(typeof shell.email === 'string' && shell.email
384
+ ? { email: shell.email }
385
+ : {}),
386
+ pointer,
387
+ delegation: bridgePlaintext.delegation,
388
+ ...(ladderSeed ? { ladderSeed } : {}),
389
+ createdAt
390
+ },
391
+ proofState
392
+ };
393
+ }
394
+ /**
395
+ * Re-mints an unlock record's bridge: the revocation-cascade path that
396
+ * replaces a rotted or expiring delegation while touching nothing else. The
397
+ * shell, the ladder member, and the binding are carried VERBATIM (the re-mint
398
+ * cannot decrypt any of them and does not need to -- each is self-contained
399
+ * and the binding covers the core), a fresh bridge is sealed to the
400
+ * credential's unlock KAK public half, and the frame is re-signed by the
401
+ * acting client's account key -- the mixed-signer case the reader settles
402
+ * against the verified document.
403
+ *
404
+ * A record with no binding cannot be re-minted ({@link UnlockBindingError});
405
+ * its credential must be rebound.
406
+ *
407
+ * @param options {object}
408
+ * @param options.record {unknown} the standing stored record
409
+ * @param options.delegation {IZcap} the freshly minted `did.jsonl`
410
+ * delegation
411
+ * @param options.keyAgreementKey {IKeyAgreementKey} the credential's unlock
412
+ * KAK, public half only
413
+ * @param options.signer {RecordSigner} the acting client's account key
414
+ * @returns {Promise<SignedUnlockRecord>}
415
+ */
416
+ export async function remintUnlockRecordBridge({ record, delegation, keyAgreementKey, signer }) {
417
+ const { encryption, wrapped } = parseRecordFrame({
418
+ record,
419
+ label: 'unlock'
420
+ });
421
+ const binding = unlockRecordBinding({ record });
422
+ const { ladder: rawLadder } = record;
423
+ const ladder = rawLadder === undefined
424
+ ? undefined
425
+ : parseSealedMember({ value: rawLadder, name: 'ladder' });
426
+ const bridge = await sealMember({ data: { delegation }, keyAgreementKey });
427
+ return (await signRecordFrame({
428
+ version: KEYRING_RECORD_VERSION,
429
+ encryption,
430
+ wrapped,
431
+ signer,
432
+ members: { binding, bridge, ...(ladder ? { ladder } : {}) }
433
+ }));
434
+ }
435
+ //# sourceMappingURL=unlockRecord.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unlockRecord.js","sourceRoot":"","sources":["../../src/unlock/unlockRecord.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAO9C,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,oBAAoB,EACpB,gBAAgB,EAChB,kBAAkB,EAClB,YAAY,EACZ,oBAAoB,EACpB,uBAAuB,EACvB,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EAClB,MAAM,sBAAsB,CAAA;AAM7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAE/C;;;;;GAKG;AACH,MAAM,sBAAsB,GAAG,8BAA8B,CAAA;AAE7D;;;;;;;;GAQG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAA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