@agentunion/fastaun 0.5.20 → 0.5.21

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 (107) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/_packed_docs/CHANGELOG.md +47 -0
  3. package/_packed_docs/INDEX.md +453 -440
  4. package/_packed_docs/KITE_DOCS_GUIDE.md +164 -161
  5. package/_packed_docs/agent.md/working/AID/347/261/273/345/236/213-owner/345/205/263/347/263/273/344/270/216/346/215/242owner/347/274/226/346/216/222/346/226/271/346/241/210.md +530 -0
  6. package/_packed_docs/agent.md/working/AID/347/261/273/345/236/213-owner/345/205/263/347/263/273/345/256/214/346/225/264/350/256/276/350/256/241/344/270/216/345/256/236/346/226/275/350/256/241/345/210/222.md +685 -0
  7. package/_packed_docs/agent.md/working/agent.md-schema/344/270/216/347/255/276/345/217/221/351/252/214/347/255/276/346/265/201/347/250/213/346/276/204/346/270/205.md +349 -0
  8. package/_packed_docs/agent.md/working//346/234/254/345/234/260Docker/346/265/213/350/257/225Bug/346/270/205/345/215/225.md +40 -0
  9. package/_packed_docs/protocol/06-/346/234/215/345/212/241/345/215/217/350/256/256.md +1433 -1392
  10. package/_packed_docs/protocol/08-AUN-E2EE.md +17 -12
  11. package/_packed_docs/protocol/Ownership/345/215/217/350/256/256/345/256/232/344/271/211.md +2 -0
  12. package/_packed_docs/protocol/Ownership/345/215/217/350/256/256/345/256/232/344/271/2112.1.md +666 -658
  13. package/_packed_docs/sdk/0.5.21-SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +368 -0
  14. package/_packed_docs/sdk/0.5.21-SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +350 -0
  15. package/_packed_docs/sdk/01-/345/277/253/351/200/237/345/274/200/345/247/213.md +1 -1
  16. package/_packed_docs/sdk/02-WebSocket/345/215/217/350/256/256.md +5 -1
  17. package/_packed_docs/sdk/03-/346/240/270/345/277/203/346/246/202/345/277/265.md +64 -64
  18. package/_packed_docs/sdk/04-/350/277/236/346/216/245/344/270/216/350/256/244/350/257/201.md +4 -1
  19. package/_packed_docs/sdk/05-E2EE/345/212/240/345/257/206/351/200/232/344/277/241.md +42 -39
  20. package/_packed_docs/sdk/06-API/346/211/213/345/206/214.md +147 -14
  21. package/_packed_docs/sdk/08-/346/234/200/344/275/263/345/256/236/350/267/265.md +48 -47
  22. package/_packed_docs/sdk/09-group-rpc-manual.md +5 -2
  23. package/_packed_docs/sdk/09-message-rpc-manual.md +250 -160
  24. package/_packed_docs/sdk/09-/345/207/255/350/257/201/346/234/215/345/212/241RPC/346/211/213/345/206/214.md +71 -0
  25. package/_packed_docs/sdk/09-/347/273/204/347/273/207/346/234/215/345/212/241RPC/346/211/213/345/206/214.md +88 -0
  26. package/_packed_docs/sdk/AUN_DOCS_GUIDE.md +134 -130
  27. package/_packed_docs/sdk/INDEX.md +239 -221
  28. package/_packed_docs/sdk/Ownership-SDK/347/274/226/346/216/222/346/214/207/345/215/2272.1.md +930 -922
  29. package/_packed_docs/sdk/Ownership2.1-SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +106 -43
  30. package/_packed_docs/sdk/Ownership2.1-SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +180 -9
  31. package/_packed_docs/sdk/Ownership2.1-TDD/345/210/206/351/230/266/346/256/265/345/256/236/346/226/275/350/256/241/345/210/222.md +708 -708
  32. package/_packed_docs/sdk/Ownership2.1/350/257/246/347/273/206/350/256/276/350/256/241.md +480 -476
  33. package/_packed_docs/sdk//345/207/255/350/257/201/344/270/216/347/273/204/347/273/207/346/234/215/345/212/241SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +1 -1
  34. package/_packed_docs/sdk//345/207/255/350/257/201/344/270/216/347/273/204/347/273/207/346/234/215/345/212/241SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +2 -2
  35. package/dist/agent-md-schema.js +6 -4
  36. package/dist/agent-md-schema.js.map +1 -1
  37. package/dist/agent-md.d.ts +22 -2
  38. package/dist/agent-md.js +84 -67
  39. package/dist/agent-md.js.map +1 -1
  40. package/dist/aid-store.d.ts +13 -0
  41. package/dist/aid-store.js +50 -15
  42. package/dist/aid-store.js.map +1 -1
  43. package/dist/auth.js +4 -0
  44. package/dist/auth.js.map +1 -1
  45. package/dist/cert-utils.d.ts +1 -0
  46. package/dist/cert-utils.js +13 -0
  47. package/dist/cert-utils.js.map +1 -1
  48. package/dist/client/delivery.d.ts +59 -1
  49. package/dist/client/delivery.js +1018 -125
  50. package/dist/client/delivery.js.map +1 -1
  51. package/dist/client/rpc-pipeline.d.ts +4 -1
  52. package/dist/client/rpc-pipeline.js +123 -37
  53. package/dist/client/rpc-pipeline.js.map +1 -1
  54. package/dist/client/v2-e2ee.d.ts +12 -2
  55. package/dist/client/v2-e2ee.js +751 -294
  56. package/dist/client/v2-e2ee.js.map +1 -1
  57. package/dist/client.d.ts +26 -2
  58. package/dist/client.js +379 -63
  59. package/dist/client.js.map +1 -1
  60. package/dist/config.d.ts +4 -0
  61. package/dist/config.js +1 -0
  62. package/dist/config.js.map +1 -1
  63. package/dist/events.d.ts +18 -1
  64. package/dist/events.js +148 -9
  65. package/dist/events.js.map +1 -1
  66. package/dist/facades.d.ts +43 -1
  67. package/dist/facades.js +363 -7
  68. package/dist/facades.js.map +1 -1
  69. package/dist/index.d.ts +5 -3
  70. package/dist/index.js +1 -1
  71. package/dist/index.js.map +1 -1
  72. package/dist/keystore/aid-db.d.ts +6 -1
  73. package/dist/keystore/aid-db.js +184 -42
  74. package/dist/keystore/aid-db.js.map +1 -1
  75. package/dist/keystore/index.d.ts +14 -0
  76. package/dist/keystore/local-token-store.d.ts +9 -1
  77. package/dist/keystore/local-token-store.js +79 -14
  78. package/dist/keystore/local-token-store.js.map +1 -1
  79. package/dist/org-protocol.d.ts +45 -0
  80. package/dist/org-protocol.js +754 -11
  81. package/dist/org-protocol.js.map +1 -1
  82. package/dist/ownership-v21.d.ts +16 -3
  83. package/dist/ownership-v21.js +35 -17
  84. package/dist/ownership-v21.js.map +1 -1
  85. package/dist/ownership-workflow.d.ts +17 -0
  86. package/dist/ownership-workflow.js +511 -26
  87. package/dist/ownership-workflow.js.map +1 -1
  88. package/dist/secret-store/file-store.js +84 -0
  89. package/dist/secret-store/file-store.js.map +1 -1
  90. package/dist/seq-tracker.d.ts +1 -0
  91. package/dist/seq-tracker.js +2 -2
  92. package/dist/seq-tracker.js.map +1 -1
  93. package/dist/tools/cross-sdk-agent.d.ts +66 -3
  94. package/dist/tools/cross-sdk-agent.js +466 -36
  95. package/dist/tools/cross-sdk-agent.js.map +1 -1
  96. package/dist/transport.d.ts +3 -0
  97. package/dist/transport.js +98 -44
  98. package/dist/transport.js.map +1 -1
  99. package/dist/types.d.ts +1 -0
  100. package/dist/v2/e2ee/encrypt-group.js +1 -0
  101. package/dist/v2/e2ee/encrypt-group.js.map +1 -1
  102. package/dist/v2/e2ee/encrypt-p2p.js +1 -0
  103. package/dist/v2/e2ee/encrypt-p2p.js.map +1 -1
  104. package/dist/v2/e2ee/types.d.ts +1 -0
  105. package/dist/version.d.ts +1 -1
  106. package/dist/version.js +1 -1
  107. package/package.json +1 -1
@@ -44,6 +44,740 @@ export class ProtocolError extends Error {
44
44
  this.retryable = protocolErrorRetryable(code);
45
45
  }
46
46
  }
47
+ export class CredentialRenewal {
48
+ tail = Promise.resolve();
49
+ ports;
50
+ scanGuard;
51
+ constructor(ports) {
52
+ this.ports = {
53
+ ...ports,
54
+ currentAid: () => { this.checkScan(); return ports.currentAid(); },
55
+ load: (aid) => this.scanOperation(() => ports.load(aid)),
56
+ save: (aid, records, guard) => {
57
+ const scan = this.scanGuard;
58
+ return this.scanOperation(() => ports.save(aid, records, scan ? { current: () => scan.current() && (!guard || guard.current()), signal: scan.signal } : guard));
59
+ },
60
+ signer: (aid) => this.scanOperation(() => ports.signer(aid, this.scanGuard)),
61
+ certificate: (aid, fingerprint, signedAt) => this.scanOperation(() => ports.certificate(aid, fingerprint, signedAt)),
62
+ call: (method, params, onSent) => this.scanOperation(() => onSent ? ports.call(method, params, onSent) : ports.call(method, params)),
63
+ send: (to, payload) => this.scanOperation(() => ports.send(to, payload)),
64
+ };
65
+ }
66
+ checkScan() {
67
+ if (this.scanGuard && !this.scanGuard.current())
68
+ throw invalid('credential renewal was superseded');
69
+ }
70
+ async scanOperation(operation) {
71
+ this.checkScan();
72
+ const result = await operation();
73
+ this.checkScan();
74
+ return result;
75
+ }
76
+ serial(operation) {
77
+ const result = this.tail.then(operation);
78
+ this.tail = result.catch(() => { });
79
+ return result;
80
+ }
81
+ remember(credential, material = {}) {
82
+ return this.serial(() => this.rememberNow(credential, material));
83
+ }
84
+ current(input) {
85
+ return this.serial(async () => {
86
+ const records = await this.ports.load(this.ports.currentAid());
87
+ let cid = typeof input === 'string' ? input : String(input.cid);
88
+ const seen = new Set();
89
+ while (records[cid]?.successor_cid && !seen.has(cid)) {
90
+ seen.add(cid);
91
+ cid = String(records[cid].successor_cid);
92
+ }
93
+ return records[cid] ? structuredClone(records[cid]) : null;
94
+ });
95
+ }
96
+ async rememberNow(credential, material, actor = this.ports.currentAid(), guard) {
97
+ validateCredential(credential);
98
+ if (!material.long_term && !guard)
99
+ return;
100
+ if (guard && !guard.current())
101
+ throw invalid('membership event receiver identity changed');
102
+ const accounts = new Set();
103
+ if (credential.sub === actor)
104
+ accounts.add(actor);
105
+ if (credential.iss === actor || material.issued)
106
+ accounts.add(String(credential.iss));
107
+ for (const aid of accounts) {
108
+ const records = structuredClone(await this.ports.load(aid));
109
+ if (guard && !guard.current())
110
+ throw invalid('membership event receiver identity changed');
111
+ const previous = records[String(credential.cid)] ?? {};
112
+ const defined = Object.fromEntries(Object.entries(material).filter(([, value]) => value !== undefined));
113
+ records[String(credential.cid)] = { ...previous, ...structuredClone(defined), credential: structuredClone(credential), long_term: material.long_term === true };
114
+ if (guard)
115
+ await this.ports.save(aid, records, guard);
116
+ else
117
+ await this.ports.save(aid, records);
118
+ if (guard && !guard.current())
119
+ throw invalid('membership event receiver identity changed');
120
+ }
121
+ }
122
+ importMembershipPackage(payload, guard) {
123
+ const actor = this.ports.currentAid();
124
+ return this.serial(async () => {
125
+ const check = () => { if (!guard.current())
126
+ throw invalid('membership event receiver identity changed'); };
127
+ check();
128
+ const material = payload.membership_package;
129
+ if (!isRecord(material) || Object.keys(material).length !== 5 || !['credential', 'membership_binding', 'registrationReceipt', 'terms', 'org_signer_aid'].every((key) => key in material)
130
+ || payload.member_status !== 'active' || !isInt(payload.version) || payload.version < 1)
131
+ throw invalid('membership package is invalid');
132
+ const { credential, membership_binding: binding, registrationReceipt: receipt, terms, org_signer_aid: orgSigner } = material;
133
+ if (!isRecord(credential) || !isRecord(binding) || !isRecord(receipt) || !isRecord(terms) || !isRecord(credential.ext)
134
+ || credential.iss !== payload.org_id || credential.sub !== payload.member_aid || binding.org_id !== payload.org_id || binding.member_aid !== payload.member_aid
135
+ || binding.membership_id !== payload.membership_id || credential.cid !== payload.credential_cid || digest(terms) !== binding.terms_hash
136
+ || credential.ext.credential_type !== 'member' || credential.ext.bound_to_membership !== true || credential.ext.org_id !== payload.org_id
137
+ || credential.ext.membership_id !== binding.membership_id || credential.ext.binding_version !== binding.binding_version || credential.ext.membership_terms_hash !== binding.terms_hash
138
+ || credential.parent != null || credential.delegate !== false || !Array.isArray(credential.cap) || credential.cap.length !== 1
139
+ || credential.cap[0].ns !== 'org' || credential.cap[0].action !== 'role.' + String(binding.role) || credential.cap[0].resource !== payload.org_id)
140
+ throw invalid('membership package does not match the event');
141
+ aid(orgSigner, 'membership organization signer');
142
+ validateCredential(credential);
143
+ validateMembershipBinding(binding);
144
+ const certificates = new Map();
145
+ for (const [signerAid, signed] of [[credential.iss, credential.signature], [binding.member_aid, binding.member_signature], [orgSigner, binding.org_signature], ['credential.' + String(payload.org_id).split('.').slice(1).join('.'), receipt.signature]]) {
146
+ if (!isRecord(signed))
147
+ throw invalid('membership package signature is missing');
148
+ const certificate = await this.ports.certificate(String(signerAid), String(signed.cert_fingerprint));
149
+ check();
150
+ certificates.set(String(signed.cert_fingerprint), certificate);
151
+ }
152
+ const resolve = (fingerprint) => certificates.get(fingerprint);
153
+ const now = Math.trunc(Date.now() / 1000);
154
+ verifyCredentialSignature(credential, resolve, now, String(payload.org_id));
155
+ verifyMembershipBinding(binding, resolve, now, String(orgSigner), true);
156
+ check();
157
+ const service = 'credential.' + String(payload.org_id).split('.').slice(1).join('.');
158
+ if (!isInt(receipt.registered_at) || !isInt(receipt.exp) || receipt.exp > receipt.registered_at + 300 || receipt.registered_at > now)
159
+ throw invalid('membership receipt window is invalid');
160
+ const late = now >= receipt.exp;
161
+ verifyRegistrationReceipt(receipt, credential, { request_id: binding.request_id, nonce: binding.nonce, registrar_aid: payload.org_id }, resolve, service, late ? receipt.registered_at : now);
162
+ check();
163
+ const validUntil = Math.min(Number(credential.exp), late ? await this.status(credential, actor, false, receipt.registered_at) : receipt.exp);
164
+ check();
165
+ const completedAt = Math.trunc(Date.now() / 1000);
166
+ verifyCredentialSignature(credential, resolve, completedAt, String(payload.org_id));
167
+ check();
168
+ if (actor !== payload.member_aid)
169
+ return;
170
+ const issuerCertificate = certificates.get(String(credential.signature.cert_fingerprint));
171
+ await this.rememberNow(credential, { membership_binding: binding, registrationReceipt: receipt, terms, version: payload.version,
172
+ service_aid: 'org.' + String(payload.org_id).split('.').slice(1).join('.'), long_term: credential.exp === Math.floor(Date.parse(new X509Certificate(issuerCertificate).validTo) / 1000) }, actor, { ...guard, current: () => guard.current() && Date.now() / 1000 < validUntil });
173
+ });
174
+ }
175
+ observe(method, params, result) {
176
+ if (method !== 'credential.register' && !method.startsWith('org.'))
177
+ return Promise.resolve();
178
+ return this.serial(async () => {
179
+ if (!isRecord(result))
180
+ return;
181
+ const values = [params, ...['previous_owner', 'new_owner'].map((key) => params[key]).filter(isRecord),
182
+ ...Object.entries(params.credential_templates ?? {}).map(([aid, credential]) => ({ credential_template: credential, membership_binding: params.membership_bindings?.[aid], terms: params.terms }))];
183
+ for (const value of values) {
184
+ const credential = value.credential_template ?? value.credential;
185
+ if (!isRecord(credential))
186
+ continue;
187
+ const member = isRecord(credential.ext) && credential.ext.credential_type === 'member';
188
+ const actor = this.ports.currentAid();
189
+ const existing = (await this.ports.load(actor))[String(credential.cid)];
190
+ let longTerm = existing?.long_term === true;
191
+ if (!longTerm && isRecord(credential.signature)) {
192
+ const cert = await this.ports.certificate(String(credential.iss), String(credential.signature.cert_fingerprint));
193
+ longTerm = credential.exp === Math.min(Math.floor(new Date(new X509Certificate(cert).validTo).getTime() / 1000), Number(credential.proof_chain?.[0]?.exp ?? Infinity));
194
+ }
195
+ await this.rememberNow(credential, {
196
+ long_term: longTerm, issued: true,
197
+ ...(member ? { membership_binding: result.membership_binding ?? value.membership_binding,
198
+ terms: value.terms ?? (value.membership_binding?.role === 'owner' ? { org_id: credential.iss, owner_aid: credential.sub } : undefined),
199
+ version: result.member?.version ?? result.version ?? value.expected_version ?? (['org.member.approveJoin', 'org.member.inviteAccept'].includes(method) ? 1 : undefined),
200
+ service_aid: 'org.' + String(credential.iss).split('.').slice(1).join('.') } : {}),
201
+ });
202
+ }
203
+ });
204
+ }
205
+ resume(guard) {
206
+ return this.serial(async () => {
207
+ this.scanGuard = guard;
208
+ try {
209
+ const actor = this.ports.currentAid();
210
+ const records = await this.ports.load(actor);
211
+ for (const [cid, initial] of Object.entries(records)) {
212
+ if (!isRecord(initial.credential))
213
+ continue;
214
+ try {
215
+ if (initial.response?.registration_request && !initial.response.registrationReceipt && Number(initial.response.registration_request.request_binding?.exp) <= Math.trunc(Date.now() / 1000)) {
216
+ if (!initial.registration_confirmation)
217
+ await this.recoverRegistration(actor, cid, initial, records);
218
+ continue;
219
+ }
220
+ if (initial.submission && initial.received_result && !initial.successor_cid)
221
+ await this.receiveNow(String(initial.credential.iss), initial.received_result, undefined, true);
222
+ else if (initial.incoming?.payload.exp > Math.trunc(Date.now() / 1000))
223
+ await this.receiveNow(initial.incoming.from, initial.incoming.payload);
224
+ const record = (await this.ports.load(actor))[cid];
225
+ if (record.commit_pending)
226
+ await this.sendCommit(record, actor);
227
+ if (record.revoke_pending)
228
+ await this.revokePrevious(record, actor);
229
+ const credential = record.credential;
230
+ const now = Math.trunc(Date.now() / 1000);
231
+ if (record.successor_cid || !record.long_term || credential.sub !== actor || !record.membership_binding && !record.pending && now >= credential.exp || credential.exp - now >= (credential.exp - credential.iat) / 2)
232
+ continue;
233
+ let request = record.pending;
234
+ if (request?.membership_binding && request.membership_binding.exp <= now)
235
+ throw invalid('renewal member consent has expired');
236
+ if (!request || request.exp <= now) {
237
+ const signer = await this.ports.signer(actor);
238
+ if (!signer)
239
+ continue;
240
+ request = request ? { ...request, iat: now, exp: now + 300 } : { type: 'credential.renewal.request', ver: 'aun-credential-renewal/1', action: 'renew', issuer_aid: credential.iss,
241
+ subject_aid: actor, previous_credential_cid: cid, credential_hash: digest(credential), request_id: organizationIdentifier(), nonce: organizationNonce(), iat: now, exp: now + 300 };
242
+ delete request.signature;
243
+ if (record.membership_binding && !request.membership_binding) {
244
+ if (now < credential.exp) {
245
+ const credentials = { [record.membership_binding.role === 'admin' ? 'approval_credential' : 'member_credential']: credential, membership_binding: record.membership_binding };
246
+ const query = prepareOrganizationRequest('org.member.get', { org_id: credential.iss, member_aid: actor, credentials }, record.service_aid, signer);
247
+ const current = await this.ports.call('org.member.get', query);
248
+ const member = current.member ?? current;
249
+ if (!isInt(member.version) || member.credential_cid !== undefined && member.credential_cid !== cid)
250
+ throw invalid('current membership differs from managed credential');
251
+ record.version = member.version;
252
+ }
253
+ if (!isInt(record.version))
254
+ throw invalid('current membership version is unavailable');
255
+ const binding = { ...record.membership_binding, binding_version: Number(record.membership_binding.binding_version) + 1,
256
+ request_id: request.request_id, nonce: request.nonce, iat: now, exp: now + 3600 };
257
+ delete binding.member_signature;
258
+ delete binding.org_signature;
259
+ request.membership_binding = signMembershipBinding(binding, signer, 'member');
260
+ request.expected_version = record.version;
261
+ }
262
+ request.signature = signProtocolPayload(request, signer);
263
+ record.pending = request;
264
+ const latest = await this.ports.load(actor);
265
+ latest[cid] = record;
266
+ await this.ports.save(actor, latest);
267
+ }
268
+ if (this.ports.currentAid() !== actor)
269
+ return;
270
+ const localIssuer = record.membership_binding && await this.ports.signer(String(credential.iss));
271
+ if (localIssuer) {
272
+ await this.receiveNow(actor, request, String(credential.iss));
273
+ const issued = (await this.ports.load(String(credential.iss)))[cid]?.response;
274
+ if (issued)
275
+ await this.receiveNow(String(credential.iss), issued);
276
+ }
277
+ else
278
+ await this.ports.send(String(credential.iss), request);
279
+ }
280
+ catch (error) {
281
+ const latest = await this.ports.load(actor);
282
+ if (latest[cid]) {
283
+ latest[cid].last_error = error instanceof Error ? error.message : String(error);
284
+ await this.ports.save(actor, latest);
285
+ }
286
+ }
287
+ }
288
+ }
289
+ finally {
290
+ this.scanGuard = undefined;
291
+ }
292
+ });
293
+ }
294
+ receive(from, payload) {
295
+ return this.serial(() => this.receiveNow(from, payload));
296
+ }
297
+ async verifyEnvelope(from, payload, expected, replay = false) {
298
+ const now = Math.trunc(Date.now() / 1000);
299
+ if (from !== expected || payload.ver !== 'aun-credential-renewal/1' || !isInt(payload.iat) || !isInt(payload.exp) || payload.iat > (replay ? payload.iat : now) || payload.exp <= (replay ? payload.iat : now) || payload.exp > payload.iat + 300 || !isRecord(payload.signature))
300
+ throw invalid('renewal message identity or validity is invalid');
301
+ const cert = await this.ports.certificate(expected, String(payload.signature.cert_fingerprint));
302
+ const unsigned = { ...payload };
303
+ delete unsigned.signature;
304
+ verifyAgentSignature(payload.signature, unsigned, expected, () => cert, now);
305
+ }
306
+ async status(credential, actor, allowExpired = false, registrationConfirmedAt, confirmation) {
307
+ const target = 'credential.' + String(credential.iss).split('.').slice(1).join('.');
308
+ const query = { credential_cid: credential.cid, request_id: organizationIdentifier(), nonce: organizationNonce(), include_chain: true };
309
+ const requestedAt = Math.trunc(Date.now() / 1000);
310
+ query.request_binding = buildRequestBinding('credential.status', target, query, String(credential.cid), String(query.nonce), String(query.request_id), requestedAt, requestedAt + 300);
311
+ const proof = await this.ports.call('credential.status', query);
312
+ const now = Math.trunc(Date.now() / 1000);
313
+ if (!isRecord(proof) || proof.ver !== 'aun-credential-status/1' || proof.credentialCid !== credential.cid || proof.credential_hash !== digest(credential)
314
+ || proof.issuerAid !== credential.iss || proof.subjectAid !== credential.sub || proof.expiresAt !== credential.exp || proof.responder !== target
315
+ || proof.request_id !== query.request_id || proof.nonce !== query.nonce || proof.target !== actor
316
+ || !isInt(proof.asOf) || !isInt(proof.proofExpiresAt) || proof.asOf > now + 300 || proof.proofExpiresAt <= proof.asOf || proof.proofExpiresAt <= now || proof.proofExpiresAt > proof.asOf + 300
317
+ || !isRecord(proof.signature) || proof.signature.timestamp !== proof.asOf)
318
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'renewal status proof does not match the query');
319
+ const cert = await this.ports.certificate(target, String(proof.signature.cert_fingerprint));
320
+ const unsigned = { ...proof };
321
+ delete unsigned.signature;
322
+ verifyAgentSignature(proof.signature, unsigned, target, () => cert, now, false);
323
+ if (proof.status !== 'active' && !(allowExpired && proof.status === 'expired' && proof.credentialStatus === 'expired' && Array.isArray(proof.chain) && proof.chain.every((item) => item.status === 'active')))
324
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'renewal credential is not active');
325
+ if (registrationConfirmedAt !== undefined && (proof.asOf < requestedAt || proof.asOf > now || !isInt(proof.registrationConfirmedAt) || proof.registrationConfirmedAt < registrationConfirmedAt || proof.registrationConfirmedAt > proof.asOf || proof.credentialStatus !== 'active' || proof.proofExpiresAt <= Math.trunc(Date.now() / 1000)))
326
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'membership registration confirmation is invalid');
327
+ if (confirmation)
328
+ Object.assign(confirmation, structuredClone(proof));
329
+ return proof.proofExpiresAt;
330
+ }
331
+ async receiveNow(from, value, localIssuer, replay = false) {
332
+ if (!isRecord(value) || !['credential.renewal.request', 'credential.renewal.result'].includes(String(value.type)))
333
+ return false;
334
+ const payload = value;
335
+ if (!replay && isInt(payload.exp) && payload.exp <= Math.trunc(Date.now() / 1000))
336
+ return true;
337
+ const isRequest = payload.type === 'credential.renewal.request';
338
+ const actor = localIssuer ?? this.ports.currentAid();
339
+ await this.verifyEnvelope(from, payload, String(isRequest ? payload.subject_aid : payload.issuer_aid), replay);
340
+ if (actor !== (isRequest ? payload.issuer_aid : payload.subject_aid))
341
+ throw invalid('renewal message has a different recipient');
342
+ let records = await this.ports.load(actor);
343
+ const cid = String(payload.previous_credential_cid);
344
+ const record = records[cid];
345
+ if (!record || !record.long_term || payload.credential_hash !== digest(record.credential) || payload.issuer_aid !== record.credential.iss || payload.subject_aid !== record.credential.sub)
346
+ throw invalid('renewal does not match a known grant');
347
+ if (replay && (!record.submission || digest(record.received_result) !== digest(payload)))
348
+ throw invalid('renewal replay differs from the saved result');
349
+ record.incoming = { from, payload: structuredClone(payload) };
350
+ await this.ports.save(actor, records);
351
+ if (isRequest && payload.action === 'commit') {
352
+ if (!record.response || record.response.request_id !== payload.request_id || record.response.nonce !== payload.nonce || record.response.credential.cid !== payload.new_credential_cid)
353
+ throw invalid('renewal acknowledgement differs from issued result');
354
+ record.revoke_pending = true;
355
+ await this.ports.save(actor, records);
356
+ await this.revokePrevious(record, actor);
357
+ }
358
+ else if (isRequest && payload.action === 'renew') {
359
+ if (payload.exp > payload.iat + 300)
360
+ throw invalid('renewal request window is too long');
361
+ if (record.successor_cid && record.response?.request_id !== payload.request_id)
362
+ throw invalid('grant was already replaced');
363
+ if (!record.response || record.response.request_id !== payload.request_id) {
364
+ const checkedAt = Math.trunc(Date.now() / 1000);
365
+ if (record.credential.exp - checkedAt >= (record.credential.exp - record.credential.iat) / 2)
366
+ throw invalid('credential renewal threshold is not reached');
367
+ await this.status(record.credential, this.ports.currentAid(), !!record.membership_binding);
368
+ const signer = await this.ports.signer(actor);
369
+ if (!signer)
370
+ throw invalid('issuer signing identity is unavailable');
371
+ const now = Math.trunc(Date.now() / 1000);
372
+ const response = { type: 'credential.renewal.result', ver: payload.ver, action: 'issued', issuer_aid: actor, subject_aid: from,
373
+ previous_credential_cid: cid, credential_hash: payload.credential_hash, request_id: payload.request_id, nonce: payload.nonce, iat: now };
374
+ if (record.membership_binding) {
375
+ const binding = payload.membership_binding;
376
+ const previous = record.membership_binding;
377
+ if (!isRecord(binding) || binding.binding_version !== Number(previous.binding_version) + 1 || binding.request_id !== payload.request_id || binding.nonce !== payload.nonce
378
+ || ['org_id', 'member_aid', 'membership_id', 'member_type', 'category', 'role', 'ownership', 'visibility', 'terms_hash'].some((key) => binding[key] !== previous[key]))
379
+ throw invalid('renewal changes membership terms');
380
+ const memberSignature = binding.member_signature;
381
+ const memberCert = await this.ports.certificate(from, String(memberSignature?.cert_fingerprint));
382
+ const unsigned = { ...binding };
383
+ delete unsigned.member_signature;
384
+ delete unsigned.org_signature;
385
+ verifyAgentSignature(memberSignature, unsigned, from, () => memberCert, now);
386
+ const credentialOptions = Object.fromEntries(['security_level', 'revocation_endpoint', 'credential_service'].filter((key) => record.credential[key] !== undefined).map((key) => [key, record.credential[key]]));
387
+ const prepared = prepareOrganizationRequest('org.member.renewCredential', { org_id: actor, member_aid: from, previous_credential_cid: cid,
388
+ expected_version: payload.expected_version, terms: record.terms, membership_binding: binding }, record.service_aid, signer, { orgSigner: signer, memberSigner: signer, credentialOptions });
389
+ delete prepared.request_binding;
390
+ response.renewal_params = prepared;
391
+ response.credential = prepared.credential_template;
392
+ response.exp = Math.min(Number(binding.exp), Number(response.credential.exp));
393
+ }
394
+ else {
395
+ if (actor !== this.ports.currentAid())
396
+ throw invalid('generic renewal requires the issuer session');
397
+ let options = { ...record.credential };
398
+ for (const key of ['signature', 'cid', 'iat', 'nbf', 'exp'])
399
+ delete options[key];
400
+ if (options.parent) {
401
+ let parent = records[String(options.parent)];
402
+ const visited = new Set();
403
+ while (parent?.successor_cid && !visited.has(String(parent.credential.cid))) {
404
+ visited.add(String(parent.credential.cid));
405
+ parent = records[String(parent.successor_cid)];
406
+ }
407
+ const successor = parent?.credential;
408
+ if (successor) {
409
+ options.parent = successor.cid;
410
+ options.proof_chain = [successor, ...(successor.proof_chain ?? [])];
411
+ }
412
+ }
413
+ options.key_version = signer.certFingerprint;
414
+ options.iat = now;
415
+ options.nbf = Math.max(Number(record.credential.nbf), now, Number(options.proof_chain?.[0]?.nbf ?? 0));
416
+ response.credential = createCredential(options, signer);
417
+ response.exp = Math.min(now + 300, response.credential.exp);
418
+ const registration = { request_id: payload.request_id, nonce: payload.nonce, registrar_aid: actor, credential: response.credential };
419
+ registration.request_binding = buildRequestBinding('credential.register', 'credential.' + actor.split('.').slice(1).join('.'), registration, String(response.credential.cid), payload.nonce, payload.request_id, now, now + 300);
420
+ response.registration_request = registration;
421
+ record.response = response;
422
+ await this.ports.save(actor, records);
423
+ }
424
+ record.response = response;
425
+ await this.ports.save(actor, records);
426
+ }
427
+ const response = record.response;
428
+ if (response.registration_request && !response.registrationReceipt) {
429
+ if (Number(response.registration_request.request_binding?.exp) <= Math.trunc(Date.now() / 1000)) {
430
+ if (!record.registration_confirmation)
431
+ await this.recoverRegistration(actor, cid, record, records);
432
+ return true;
433
+ }
434
+ const registered = await this.ports.call('credential.register', response.registration_request, async () => {
435
+ const sentAt = Math.trunc(Date.now() / 1000);
436
+ if (this.ports.currentAid() !== actor || sentAt >= Number(response.registration_request.request_binding.exp))
437
+ throw invalid('registration expired before it was sent');
438
+ if (this.ports.atomicCheckpointSave?.() !== true)
439
+ return;
440
+ const deadline = Number(response.registration_request.request_binding.exp), hash = digest(response.registration_request);
441
+ record.registration_submitted_at ??= sentAt;
442
+ record.registration_submission_hash = hash;
443
+ await this.ports.save(actor, records, { current: () => this.ports.currentAid() === actor && Date.now() / 1000 < deadline && digest(response.registration_request) === hash });
444
+ });
445
+ await this.receipt(response.credential, registered.registrationReceipt, response.registration_request);
446
+ await this.status(response.credential, actor);
447
+ response.registrationReceipt = registered.registrationReceipt;
448
+ }
449
+ const responseNow = Math.trunc(Date.now() / 1000);
450
+ const responseLimit = Math.min(responseNow + 300, Number(response.credential.exp), ...['membership_binding', 'credential_template', 'registration_authorization'].map((key) => Number(response.renewal_params?.[key]?.exp ?? Infinity)));
451
+ if (responseLimit <= responseNow)
452
+ throw invalid('renewal result authorization has expired');
453
+ if (!response.signature || response.exp <= responseNow || response.exp > responseLimit) {
454
+ response.iat = responseNow;
455
+ response.exp = responseLimit;
456
+ delete response.signature;
457
+ response.signature = signProtocolPayload(response, (await this.ports.signer(actor)));
458
+ }
459
+ records[String(response.credential.cid)] = { ...records[String(response.credential.cid)], credential: structuredClone(response.credential), long_term: true,
460
+ ...(record.membership_binding ? { membership_binding: structuredClone(response.renewal_params.membership_binding), terms: record.terms, version: Number(payload.expected_version) + 1, service_aid: record.service_aid } : {}) };
461
+ await this.ports.save(actor, records);
462
+ if (!localIssuer)
463
+ await this.ports.send(from, response);
464
+ }
465
+ else if (!isRequest && payload.action === 'issued') {
466
+ if (!record.pending || record.pending.request_id !== payload.request_id || record.pending.nonce !== payload.nonce || record.successor_cid && record.successor_cid !== payload.credential?.cid)
467
+ throw invalid('renewal result does not match pending request');
468
+ if (record.successor_cid) {
469
+ if (record.commit_pending)
470
+ await this.sendCommit(record, actor);
471
+ records = await this.ports.load(actor);
472
+ delete records[cid].incoming;
473
+ await this.ports.save(actor, records);
474
+ return true;
475
+ }
476
+ const credential = payload.credential;
477
+ if (!isRecord(credential) || credential.cid === cid || Number(credential.nbf) < record.credential.nbf || ['iss', 'sub', 'aud', 'cap', 'delegate', 'delegation_depth', 'security_level', 'revocation_endpoint', 'credential_service'].some((key) => canonicalText(credential[key] ?? null) !== canonicalText(record.credential[key] ?? null)))
478
+ throw invalid('renewal result changes the grant');
479
+ const expectedExt = record.membership_binding ? { ...record.credential.ext, binding_version: Number(record.membership_binding.binding_version) + 1 } : record.credential.ext;
480
+ if (canonicalText(credential.ext ?? null) !== canonicalText(expectedExt ?? null))
481
+ throw invalid('renewal result changes grant extensions');
482
+ const credentialSignature = credential.signature;
483
+ const certificate = await this.ports.certificate(from, String(credentialSignature?.cert_fingerprint));
484
+ verifyCredentialSignature(credential, () => certificate, Math.trunc(Date.now() / 1000));
485
+ const previousChain = credentialChainDocuments(record.credential);
486
+ const renewedChain = credentialChainDocuments(credential);
487
+ if (previousChain.length !== renewedChain.length)
488
+ throw invalid('renewal changes the authorization chain');
489
+ for (let index = 1; index < renewedChain.length; index++) {
490
+ const parent = renewedChain[index], previous = previousChain[index];
491
+ if (Number(parent.nbf) < Number(previous.nbf) || ['iss', 'sub', 'aud', 'cap', 'delegate', 'delegation_depth', 'ext', 'security_level', 'revocation_endpoint', 'credential_service'].some((key) => canonicalText(parent[key] ?? null) !== canonicalText(previous[key] ?? null)))
492
+ throw invalid('renewal changes parent authorization');
493
+ const cert = await this.ports.certificate(String(parent.iss), String(parent.signature.cert_fingerprint));
494
+ verifyCredentialSignature(parent, () => cert, Math.trunc(Date.now() / 1000), undefined, renewedChain.slice(index + 1));
495
+ }
496
+ let material = {};
497
+ let recoveryValidUntil = Infinity;
498
+ if (record.membership_binding) {
499
+ const params = payload.renewal_params;
500
+ if (!isRecord(params) || digest(params.credential_template) !== digest(credential) || params.previous_credential_cid !== cid
501
+ || !isRecord(params.membership_binding) || digest(params.membership_binding.member_signature) !== digest(record.pending.membership_binding.member_signature)
502
+ || digest(params.terms) !== digest(record.terms))
503
+ throw invalid('renewal membership result is invalid');
504
+ const agreedBinding = { ...params.membership_binding };
505
+ delete agreedBinding.org_signature;
506
+ if (digest(agreedBinding) !== digest(record.pending.membership_binding) || params.org_id !== record.credential.iss || params.member_aid !== actor || params.expected_version !== record.pending.expected_version)
507
+ throw invalid('renewal differs from the signed membership request');
508
+ const signer = await this.ports.signer(actor);
509
+ if (!signer)
510
+ throw invalid('member signing identity is unavailable');
511
+ const consentEnd = Math.min(Number(payload.exp), Number(params.membership_binding.exp), Number(params.registration_authorization?.exp), Number(credential.exp));
512
+ if (replay && consentEnd <= Math.trunc(Date.now() / 1000)) {
513
+ if (this.ports.atomicCheckpointSave?.() !== true)
514
+ throw invalid('atomic checkpoint persistence is required for renewal recovery');
515
+ if (!isInt(record.submitted_at) || record.submitted_at < payload.iat || record.submitted_at >= consentEnd || record.submission_hash !== digest(record.submission))
516
+ throw invalid('renewal operation was not submitted within its authorization window');
517
+ const original = { ...record.submission };
518
+ delete original.request_binding;
519
+ delete original.client_signature;
520
+ const expected = { ...params };
521
+ delete expected.request_binding;
522
+ delete expected.client_signature;
523
+ if (digest(original) !== digest(expected))
524
+ throw invalid('renewal submission differs from the original result');
525
+ const credentialProof = {};
526
+ const proofExpiresAt = await this.status(credential, actor, false, record.submitted_at, credentialProof);
527
+ const query = prepareOrganizationRequest('org.member.get', { org_id: credential.iss, member_aid: actor,
528
+ credentials: { [params.membership_binding.role === 'admin' ? 'approval_credential' : 'member_credential']: credential, membership_binding: params.membership_binding } }, record.service_aid, signer);
529
+ const current = await this.ports.call('org.member.get', query);
530
+ const proof = current.membership_status;
531
+ const now = Math.trunc(Date.now() / 1000);
532
+ if (!isRecord(proof) || proof.ver !== 'aun-membership-status/1' || proof.org_id !== credential.iss || proof.member_aid !== actor || proof.credential_cid !== credential.cid
533
+ || proof.membership_id !== params.membership_binding.membership_id || proof.role !== params.membership_binding.role || proof.version !== Number(params.expected_version) + 1
534
+ || proof.status !== 'active' || proof.credential_status !== 'active' || proof.responder !== record.service_aid || proof.target !== actor || proof.request_id !== query.request_id || proof.nonce !== query.nonce
535
+ || !isInt(proof.as_of) || proof.as_of < Number(query.request_binding.iat) || proof.as_of > now || !isInt(proof.expires_at) || proof.expires_at <= now || proof.expires_at > proof.as_of + 300
536
+ || !isRecord(proof.signature) || proof.signature.timestamp !== proof.as_of || proofExpiresAt <= now)
537
+ throw invalid('renewal membership confirmation is invalid');
538
+ const cert = await this.ports.certificate(record.service_aid, String(proof.signature.cert_fingerprint));
539
+ const unsigned = { ...proof };
540
+ delete unsigned.signature;
541
+ verifyAgentSignature(proof.signature, unsigned, record.service_aid, () => cert, now, false);
542
+ recoveryValidUntil = Math.min(proofExpiresAt, proof.expires_at, Number(credential.exp));
543
+ material = { membership_binding: params.membership_binding, terms: record.terms, version: proof.version, service_aid: record.service_aid,
544
+ recovery_confirmation: { membership_status: proof, credential_status: credentialProof } };
545
+ }
546
+ else {
547
+ const prepared = record.submission ? structuredClone(record.submission) : structuredClone(params);
548
+ delete prepared.request_binding;
549
+ delete prepared.client_signature;
550
+ const submittedAt = Math.trunc(Date.now() / 1000);
551
+ prepared.request_binding = buildRequestBinding('org.member.renewCredential', record.service_aid, prepared, String(credential.cid), String(prepared.nonce), String(prepared.request_id), submittedAt, Math.min(submittedAt + 300, Number(credential.exp)));
552
+ record.submission = structuredClone(prepared);
553
+ record.received_result = structuredClone(payload);
554
+ records[cid] = record;
555
+ await this.ports.save(actor, records);
556
+ const result = await this.ports.call('org.member.renewCredential', prepared, async () => {
557
+ const sentAt = Math.trunc(Date.now() / 1000);
558
+ if (this.ports.currentAid() !== actor || sentAt >= Math.min(consentEnd, Number(prepared.request_binding.exp)))
559
+ throw invalid('renewal authorization expired before it was sent');
560
+ if (this.ports.atomicCheckpointSave?.() !== true)
561
+ return;
562
+ const deadline = Math.min(consentEnd, Number(prepared.request_binding.exp)), hash = digest(record.submission);
563
+ record.submitted_at ??= sentAt;
564
+ record.submission_hash = hash;
565
+ await this.ports.save(actor, records, { current: () => this.ports.currentAid() === actor && Date.now() / 1000 < deadline && digest(record.submission) === hash });
566
+ });
567
+ await this.receipt(credential, result.registrationReceipt, prepared);
568
+ await this.status(credential, actor);
569
+ material = { membership_binding: result.membership_binding ?? params.membership_binding, terms: record.terms, version: result.version, service_aid: record.service_aid };
570
+ }
571
+ }
572
+ else {
573
+ await this.receipt(credential, payload.registrationReceipt, payload.registration_request);
574
+ await this.status(credential, actor);
575
+ }
576
+ records = await this.ports.load(actor);
577
+ records[String(credential.cid)] = { credential: structuredClone(credential), long_term: true, ...material };
578
+ records[cid].successor_cid = credential.cid;
579
+ records[cid].response = structuredClone(payload);
580
+ records[cid].commit_pending = true;
581
+ const recoveryGuard = { current: () => Date.now() / 1000 >= Number(credential.nbf) && Date.now() / 1000 < Math.min(recoveryValidUntil, Number(credential.exp)) };
582
+ if (!recoveryGuard.current())
583
+ throw invalid('renewal recovery confirmation has expired');
584
+ await this.ports.save(actor, records, recoveryGuard);
585
+ await this.sendCommit(records[cid], actor);
586
+ }
587
+ else
588
+ throw invalid('renewal message action is invalid');
589
+ records = await this.ports.load(actor);
590
+ delete records[cid].incoming;
591
+ await this.ports.save(actor, records);
592
+ return true;
593
+ }
594
+ async recoverRegistration(actor, cid, record, records) {
595
+ if (this.ports.atomicCheckpointSave?.() !== true)
596
+ throw invalid('atomic checkpoint persistence is required for registration recovery');
597
+ const response = record.response, request = response.registration_request, binding = request.request_binding;
598
+ if (!isInt(record.registration_submitted_at) || record.registration_submitted_at < binding.iat || record.registration_submitted_at >= binding.exp
599
+ || record.registration_submission_hash !== digest(request) || digest(request.credential) !== digest(response.credential))
600
+ throw invalid('registration was not sent within its authorization window');
601
+ const credential = response.credential;
602
+ const certificate = await this.ports.certificate(String(credential.iss), String(credential.signature?.cert_fingerprint));
603
+ verifyCredentialSignature(credential, () => certificate, Math.trunc(Date.now() / 1000));
604
+ const confirmation = {};
605
+ const validUntil = await this.status(credential, actor, false, record.registration_submitted_at, confirmation);
606
+ records[String(credential.cid)] = { credential: structuredClone(credential), long_term: true, issued: true };
607
+ records[cid].registration_confirmation = confirmation;
608
+ const guard = { current: () => Date.now() / 1000 >= Number(credential.nbf) && Date.now() / 1000 < Math.min(validUntil, Number(credential.exp)) };
609
+ if (!guard.current())
610
+ throw invalid('registration recovery confirmation has expired');
611
+ await this.ports.save(actor, records, guard);
612
+ }
613
+ async receipt(credential, receipt, request) {
614
+ const service = 'credential.' + String(credential.iss).split('.').slice(1).join('.');
615
+ if (!isRecord(receipt) || !isRecord(receipt.signature))
616
+ throw invalid('renewal registration receipt is missing');
617
+ const cert = await this.ports.certificate(service, String(receipt.signature.cert_fingerprint));
618
+ verifyRegistrationReceipt(receipt, credential, request, () => cert, service, Math.trunc(Date.now() / 1000));
619
+ }
620
+ async sendCommit(record, actor) {
621
+ const signer = await this.ports.signer(actor);
622
+ if (!signer)
623
+ throw invalid('renewal holder signing identity is unavailable');
624
+ const now = Math.trunc(Date.now() / 1000);
625
+ const payload = { ...record.pending, action: 'commit', new_credential_cid: record.successor_cid, iat: now, exp: now + 300 };
626
+ delete payload.signature;
627
+ payload.signature = signProtocolPayload(payload, signer);
628
+ if (record.membership_binding && await this.ports.signer(String(record.credential.iss)))
629
+ await this.receiveNow(actor, payload, String(record.credential.iss));
630
+ else
631
+ await this.ports.send(String(record.credential.iss), payload);
632
+ const records = await this.ports.load(actor);
633
+ delete records[String(record.credential.cid)].commit_pending;
634
+ await this.ports.save(actor, records);
635
+ }
636
+ async revokePrevious(record, actor) {
637
+ await this.status(record.response.credential, this.ports.currentAid());
638
+ if (!record.membership_binding && !record.credential.delegate) {
639
+ const now = Math.trunc(Date.now() / 1000);
640
+ const request = { credential_cid: record.credential.cid, registrar_aid: actor,
641
+ request_id: record.response.request_id + '-revoke', nonce: record.response.nonce + '-revoke', reason_code: 'credential_renewed', cascade: false };
642
+ request.request_binding = buildRequestBinding('credential.revoke', 'credential.' + actor.split('.').slice(1).join('.'), request, String(record.credential.cid), String(request.nonce), String(request.request_id), now, now + 300);
643
+ await this.ports.call('credential.revoke', request);
644
+ }
645
+ const records = await this.ports.load(actor);
646
+ records[String(record.credential.cid)].successor_cid = record.response.credential.cid;
647
+ delete records[String(record.credential.cid)].revoke_pending;
648
+ await this.ports.save(actor, records);
649
+ }
650
+ revocationAuthority(document, record) {
651
+ const derived = 'credential.' + String(document.iss).split('.').slice(1).join('.');
652
+ const authority = record?.credential_authority ?? derived;
653
+ if (typeof authority !== 'string' || !/^credential\.[^\s/]+$/.test(authority) || !AID_RE.test(String(document.iss)))
654
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential authority is unavailable');
655
+ return authority;
656
+ }
657
+ revocationState(value) {
658
+ if (value == null || isRecord(value) && Object.keys(value).length === 0)
659
+ return { version: 1, authorities: {} };
660
+ if (!isRecord(value) || value.version !== 1 || !isRecord(value.authorities))
661
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential revocation storage is damaged');
662
+ for (const [authority, partition] of Object.entries(value.authorities)) {
663
+ if (!/^credential\.[^\s/]+$/.test(authority) || !isRecord(partition) || !isRecord(partition.revoked) || !isInt(partition.since_event_seq) || partition.since_event_seq < 0
664
+ || typeof partition.cursor !== 'string' || partition.cursor !== '' && (!/^(0|[1-9][0-9]*)$/.test(partition.cursor) || !isInt(Number(partition.cursor)) || Number(partition.cursor) !== partition.since_event_seq)
665
+ || !isInt(partition.as_of) || !isInt(partition.expires_at) || partition.expires_at <= partition.as_of
666
+ || Object.values(partition.revoked).some((sequence) => !isInt(sequence) || sequence <= 0 || sequence > Number(partition.since_event_seq)))
667
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential revocation storage is damaged');
668
+ }
669
+ return value;
670
+ }
671
+ _assertNotRevoked(credential, proofChain) {
672
+ if (!this.ports.loadRevocations)
673
+ return;
674
+ const actor = this.ports.currentAid();
675
+ const state = this.revocationState(this.ports.loadRevocations(actor));
676
+ const records = this.ports.load(actor);
677
+ if (!isRecord(records))
678
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'managed credential storage is unavailable');
679
+ const documents = [credential, ...(proofChain ?? [])];
680
+ const seen = new Set();
681
+ for (let index = 0; index < documents.length; index++) {
682
+ const document = documents[index];
683
+ if (!isRecord(document) || seen.has(document))
684
+ continue;
685
+ seen.add(document);
686
+ if (seen.size > 128)
687
+ throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'credential chain is too deep');
688
+ const authority = this.revocationAuthority(document, records[String(document.cid)]);
689
+ const revoked = state.authorities[authority]?.revoked;
690
+ if (revoked && Object.hasOwn(revoked, String(document.cid)))
691
+ throw new ProtocolError('CREDENTIAL_REVOKED', 'credential was revoked');
692
+ if (Array.isArray(document.proof_chain))
693
+ documents.push(...document.proof_chain);
694
+ }
695
+ if (this.ports.currentAid() !== actor)
696
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential receiver identity changed');
697
+ }
698
+ _syncRevocations(guard) {
699
+ if (!this.ports.loadRevocations || !this.ports.saveRevocations)
700
+ return Promise.resolve();
701
+ return this.serial(async () => {
702
+ const actor = this.ports.currentAid();
703
+ const check = () => { if (this.ports.currentAid() !== actor || guard && !guard.current())
704
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential revocation synchronization was superseded'); };
705
+ check();
706
+ let state = structuredClone(this.revocationState(await this.ports.loadRevocations(actor)));
707
+ check();
708
+ const records = await this.ports.load(actor);
709
+ check();
710
+ const authorities = new Set();
711
+ if (AID_RE.test(actor))
712
+ authorities.add('credential.' + actor.split('.').slice(1).join('.'));
713
+ for (const record of Object.values(records)) {
714
+ if (!isRecord(record) || !isRecord(record.credential))
715
+ continue;
716
+ validateCredential(record.credential);
717
+ const documents = [record.credential];
718
+ const seen = new Set();
719
+ for (let index = 0; index < documents.length; index++) {
720
+ const document = documents[index];
721
+ if (!isRecord(document) || seen.has(document))
722
+ continue;
723
+ seen.add(document);
724
+ if (seen.size > 128)
725
+ throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'credential chain is too deep');
726
+ authorities.add(this.revocationAuthority(document, document === record.credential ? record : undefined));
727
+ if (Array.isArray(document.proof_chain))
728
+ documents.push(...document.proof_chain);
729
+ }
730
+ }
731
+ for (const authority of authorities) {
732
+ for (let page = 0; page < 32; page++) {
733
+ check();
734
+ const previous = state.authorities[authority] ?? { revoked: {}, since_event_seq: 0, cursor: '' };
735
+ const since = previous.since_event_seq;
736
+ const query = { cursor: previous.cursor || String(since), limit: 1000, request_id: organizationIdentifier(), nonce: organizationNonce() };
737
+ const requestedAt = Math.trunc(Date.now() / 1000);
738
+ query.request_binding = buildRequestBinding('credential.revocationList', authority, query, '', String(query.nonce), String(query.request_id), requestedAt, requestedAt + 300);
739
+ const proof = await this.ports.call('credential.revocationList', query);
740
+ check();
741
+ const now = Math.trunc(Date.now() / 1000);
742
+ if (!isRecord(proof) || proof.responder !== authority || proof.target !== actor || proof.request_id !== query.request_id || proof.nonce !== query.nonce || proof.issuer_aid !== ''
743
+ || !Array.isArray(proof.events) || proof.events.length > 1000 || typeof proof.next_cursor !== 'string'
744
+ || !isInt(proof.as_of) || proof.as_of <= 0 || proof.as_of > now + 300 || !isInt(proof.expires_at) || proof.expires_at <= now || proof.expires_at <= proof.as_of || proof.expires_at > proof.as_of + 300
745
+ || !isRecord(proof.signature) || proof.signature.timestamp !== proof.as_of)
746
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation list does not match the query');
747
+ let last = since;
748
+ for (const event of proof.events) {
749
+ if (!isRecord(event) || !isInt(event.event_seq) || event.event_seq <= last || typeof event.cid !== 'string' || !event.cid
750
+ || typeof event.reason_code !== 'string' || !event.reason_code || !isInt(event.revoked_at) || event.revoked_at < 0 || event.revoked_at > proof.as_of
751
+ || typeof event.revoked_by_aid !== 'string' || !AID_RE.test(event.revoked_by_aid))
752
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation event sequence is invalid');
753
+ last = event.event_seq;
754
+ }
755
+ if (proof.next_cursor !== '' && (!/^[1-9][0-9]*$/.test(proof.next_cursor) || !isInt(Number(proof.next_cursor)) || Number(proof.next_cursor) <= since || Number(proof.next_cursor) < last))
756
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation cursor did not advance');
757
+ const certificate = await this.ports.certificate(authority, String(proof.signature.cert_fingerprint));
758
+ check();
759
+ const unsigned = { ...proof };
760
+ delete unsigned.signature;
761
+ verifyAgentSignature(proof.signature, unsigned, authority, () => certificate, Math.trunc(Date.now() / 1000), false);
762
+ const next = structuredClone(state);
763
+ const revoked = { ...previous.revoked };
764
+ for (const event of proof.events)
765
+ Object.defineProperty(revoked, String(event.cid), { value: event.event_seq, enumerable: true, configurable: true, writable: true });
766
+ next.authorities[authority] = { revoked, since_event_seq: proof.next_cursor === '' ? last : Number(proof.next_cursor), cursor: proof.next_cursor, as_of: proof.as_of, expires_at: proof.expires_at };
767
+ const commitGuard = { ...guard, current: () => this.ports.currentAid() === actor && (!guard || guard.current()) && Date.now() / 1000 < Number(proof.expires_at) };
768
+ if (!commitGuard.current())
769
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation list expired before persistence');
770
+ await this.ports.saveRevocations(actor, next, commitGuard);
771
+ if (!commitGuard.current())
772
+ throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation synchronization was superseded');
773
+ state = next;
774
+ if (proof.next_cursor === '')
775
+ break;
776
+ }
777
+ }
778
+ });
779
+ }
780
+ }
47
781
  function certificatePem(material) {
48
782
  if (!material)
49
783
  throw new Error('certificate is missing');
@@ -554,12 +1288,12 @@ export function validateCredential(credential, chainDepth = 0, externalProofChai
554
1288
  throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'proof_chain is too deep');
555
1289
  for (let index = 0; index < chain.length; index++) {
556
1290
  const item = chain[index];
557
- validateCredential(item, chainDepth + 1);
558
- if (canonicalText(item.proof_chain ?? []) !== canonicalText(chain.slice(index + 1)))
1291
+ validateCredential(item, chainDepth + 1, chain.slice(index + 1));
1292
+ if (item.proof_chain !== undefined && canonicalText(item.proof_chain) !== canonicalText(chain.slice(index + 1)))
559
1293
  throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'embedded proof_chain conflicts with outer chain');
560
1294
  }
561
1295
  }
562
- else if ('proof_chain' in credential && JSON.stringify(credential.proof_chain) !== '[]') {
1296
+ else if ('proof_chain' in credential && JSON.stringify(credential.proof_chain) !== '[]' || parent == null && externalProofChain !== undefined && JSON.stringify(externalProofChain) !== '[]') {
563
1297
  throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'root proof_chain must be empty');
564
1298
  }
565
1299
  if (!['low', 'medium', 'high'].includes(Object.hasOwn(credential, 'security_level') ? credential.security_level : 'medium'))
@@ -598,6 +1332,10 @@ export function createCredential(options, signer) {
598
1332
  throw invalid('issuer does not match signing AID');
599
1333
  const chain = value.proof_chain;
600
1334
  const parentDepth = Array.isArray(chain) && isRecord(chain[0]) ? chain[0].delegation_depth ?? 0 : 0;
1335
+ if (!('exp' in value)) {
1336
+ const certExp = Math.floor(new Date(new X509Certificate(signer.certPem).validTo).getTime() / 1000);
1337
+ value.exp = value.parent != null && Array.isArray(chain) && isRecord(chain[0]) && isInt(chain[0].exp) ? Math.min(certExp, chain[0].exp) : certExp;
1338
+ }
601
1339
  if (!('delegation_depth' in value))
602
1340
  value.delegation_depth = value.parent !== undefined && value.parent !== null && isInt(parentDepth) ? parentDepth + 1 : 0;
603
1341
  if (Array.isArray(value.cap))
@@ -823,7 +1561,7 @@ export function prepareOrganizationRequest(method, params, target, signer, optio
823
1561
  throw invalid('organization signer or signed member CAP is required');
824
1562
  value.credential_template = createMemberCredential(binding, options.orgSigner, { key_version: options.orgSigner.certFingerprint, ...options.credentialOptions });
825
1563
  }
826
- if (method !== 'org.member.inviteAccept' && isRecord(value.credential_template) && !('registration_authorization' in value)) {
1564
+ if (!['org.member.inviteAccept', 'org.member.inviteReject'].includes(method) && isRecord(value.credential_template) && !('registration_authorization' in value)) {
827
1565
  if (!binding)
828
1566
  throw invalid('membership binding is required for registration');
829
1567
  const issuerSigner = options.orgSigner ?? (signer.aid === value.credential_template.iss ? signer : undefined);
@@ -856,9 +1594,14 @@ export function prepareOrganizationRequest(method, params, target, signer, optio
856
1594
  const credentials = isRecord(value.credentials) ? value.credentials : {};
857
1595
  if ('credentials' in value && !isRecord(value.credentials))
858
1596
  throw invalid('credentials must be an object');
859
- if (['org.member.join', 'org.member.inviteAccept'].includes(method)) {
860
- if (!binding || ('for' in value ? value.for : signer.aid) !== binding.member_aid || value.member_aid !== binding.member_aid || binding.org_id !== value.org_id || target !== 'org.' + String(binding.org_id).slice(String(binding.org_id).indexOf('.') + 1) || !isRecord(binding.member_signature))
861
- throw representationBindingInvalid('admission requires the target member signature');
1597
+ if (['org.member.join', 'org.member.inviteAccept', 'org.member.inviteReject'].includes(method)) {
1598
+ const rejection = method === 'org.member.inviteReject';
1599
+ if (rejection && binding && !('member_aid' in value))
1600
+ value.member_aid = binding.member_aid;
1601
+ if (!binding || ('for' in value ? value.for : signer.aid) !== binding.member_aid || value.member_aid !== binding.member_aid || binding.org_id !== value.org_id || target !== 'org.' + String(binding.org_id).slice(String(binding.org_id).indexOf('.') + 1) || !isRecord(rejection ? binding.org_signature : binding.member_signature))
1602
+ throw representationBindingInvalid('admission requires the original membership signature');
1603
+ if (rejection && (value.request_id === binding.request_id || value.nonce === binding.nonce))
1604
+ throw representationBindingInvalid('invitation rejection requires a distinct action identity');
862
1605
  value.credentials = credentials;
863
1606
  const now = Math.trunc(Date.now() / 1000);
864
1607
  if (now >= Number(binding.exp))
@@ -927,7 +1670,7 @@ export function createMemberCredential(binding, signer, options) {
927
1670
  const text = (value) => new TextDecoder().decode(canonicalJson(value));
928
1671
  if (!isRecord(options) || Object.entries(fixed).some(([key, value]) => key in options && text(options[key]) !== text(value)) || options.parent !== undefined && options.parent !== null)
929
1672
  throw invalid('member credential options conflict with binding');
930
- return createCredential({ exp: binding.exp, ...options, ...fixed }, signer);
1673
+ return createCredential({ ...options, ...fixed }, signer);
931
1674
  }
932
1675
  export function verifyCredential(credential, context) {
933
1676
  if (!context || !context.audience || !context.subject)
@@ -1051,8 +1794,8 @@ export function verifyCredential(credential, context) {
1051
1794
  }
1052
1795
  if (context.now === undefined) {
1053
1796
  const completedAt = Math.trunc(Date.now() / 1000);
1054
- for (const document of documents)
1055
- validateCredentialAt(document, completedAt, context.audience);
1797
+ for (const [index, document] of documents.entries())
1798
+ validateCredentialAt(document, completedAt, context.audience, documents.slice(index + 1));
1056
1799
  validateCredentialCapability(credential, { ns: context.ns, action: context.action, resource: context.resource, params: context.params, units: context.units, now: completedAt, client_ip: context.clientIp, proof_chain: context.proofChain }, () => true);
1057
1800
  validateRequestBinding(context.requestBinding, context.method, context.target, context.params ?? {}, String(value.cid), context.nonce ?? '', context.requestId ?? '', completedAt);
1058
1801
  for (const proof of statusProofs) {
@@ -1374,7 +2117,7 @@ export function verifyCredentialChain(credential, resolveCertificate, resolveSta
1374
2117
  throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential status is unavailable');
1375
2118
  }
1376
2119
  for (const [index, document] of documents.entries())
1377
- verifyCredentialSignature(document, resolveCertificate, now, audience, index === 0 ? proofChain : undefined);
2120
+ verifyCredentialSignature(document, resolveCertificate, now, audience, documents.slice(index + 1));
1378
2121
  }
1379
2122
  function validateMembershipBindingPayload(binding, allowPartial = false) {
1380
2123
  if (!isRecord(binding))