@btps/sdk 1.1.3 → 1.2.0-rc.1

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 (111) hide show
  1. package/CHANGELOG.md +422 -0
  2. package/bin/verify-evidence-bundle.mjs +96 -0
  3. package/dist/client/btpsAgent.d.ts +10 -1
  4. package/dist/client/btpsAgent.js +116 -18
  5. package/dist/client/btpsClient.d.ts +53 -7
  6. package/dist/client/btpsClient.js +265 -147
  7. package/dist/client/index.d.ts +1 -1
  8. package/dist/client/index.js +3 -1
  9. package/dist/client/libs/schema.js +78 -13
  10. package/dist/client/libs/timeouts.d.ts +6 -0
  11. package/dist/client/libs/timeouts.js +2 -1
  12. package/dist/client/types/index.d.ts +54 -2
  13. package/dist/core/authentication/BtpsAuthentication.d.ts +43 -1
  14. package/dist/core/authentication/BtpsAuthentication.js +152 -36
  15. package/dist/core/authentication/refreshSuccessor.js +47 -0
  16. package/dist/core/authentication/storage/InMemoryTokenStore.d.ts +17 -1
  17. package/dist/core/authentication/storage/InMemoryTokenStore.js +16 -4
  18. package/dist/core/authentication/types.d.ts +82 -0
  19. package/dist/core/crypto/contentKey.d.ts +34 -0
  20. package/dist/core/crypto/contentKey.js +64 -0
  21. package/dist/core/crypto/decryptVerify.d.ts +7 -1
  22. package/dist/core/crypto/decryptVerify.js +23 -5
  23. package/dist/core/crypto/decrypter-interface.d.ts +6 -0
  24. package/dist/core/crypto/index.d.ts +12 -0
  25. package/dist/core/crypto/index.js +7 -1
  26. package/dist/core/crypto/keyResolver.d.ts +48 -0
  27. package/dist/core/crypto/keyResolver.js +46 -0
  28. package/dist/core/crypto/pem-decrypter.d.ts +2 -0
  29. package/dist/core/crypto/pem-decrypter.js +13 -8
  30. package/dist/core/crypto/signEncrypt.d.ts +4 -1
  31. package/dist/core/crypto/signEncrypt.js +20 -5
  32. package/dist/core/crypto/types.d.ts +8 -1
  33. package/dist/core/delegation/BtpsDelegator.d.ts +4 -0
  34. package/dist/core/delegation/BtpsDelegator.js +3 -2
  35. package/dist/core/delegation/verifyTransporterSignature.d.ts +104 -0
  36. package/dist/core/delegation/verifyTransporterSignature.js +224 -0
  37. package/dist/core/error/constant.d.ts +39 -0
  38. package/dist/core/error/constant.js +55 -0
  39. package/dist/core/server/constants/btps-protocol.d.ts +21 -5
  40. package/dist/core/server/constants/btps-protocol.js +46 -8
  41. package/dist/core/server/constants/index.d.ts +7 -3
  42. package/dist/core/server/constants/index.js +19 -4
  43. package/dist/core/server/constants/limits.d.ts +18 -0
  44. package/dist/core/server/constants/limits.js +19 -1
  45. package/dist/core/server/constants/permissions.d.ts +38 -0
  46. package/dist/core/server/constants/permissions.js +38 -0
  47. package/dist/core/server/schemas/artifacts/agentSchema.js +2 -1
  48. package/dist/core/server/schemas/artifacts/artifacts.js +7 -1
  49. package/dist/core/server/schemas/artifacts/controlSchema.js +1 -1
  50. package/dist/core/server/schemas/artifacts/identityLookupSchema.js +10 -1
  51. package/dist/core/server/schemas/artifacts/transporterSchema.js +92 -12
  52. package/dist/core/server/schemas/bounded.js +2 -2
  53. package/dist/core/server/schemas/btpsDocsSchema.d.ts +34185 -1084
  54. package/dist/core/server/schemas/btpsDocsSchema.js +300 -42
  55. package/dist/core/server/schemas/helpers.d.ts +9580 -0
  56. package/dist/core/server/schemas/helpers.js +76 -12
  57. package/dist/core/server/schemas/keyChainSchema.js +53 -0
  58. package/dist/core/server/schemas/responseSchema.js +52 -9
  59. package/dist/core/server/schemas/schema.js +3 -1
  60. package/dist/core/server/schemas/trustValues.d.ts +105 -0
  61. package/dist/core/server/schemas/trustValues.js +117 -0
  62. package/dist/core/server/types.d.ts +104 -6
  63. package/dist/core/storage/AbstractIdentityStore.d.ts +9 -0
  64. package/dist/core/storage/JsonIdentityStore.d.ts +3 -0
  65. package/dist/core/storage/JsonIdentityStore.js +3 -0
  66. package/dist/core/storage/types.d.ts +2 -0
  67. package/dist/core/trust/agreements.d.ts +107 -0
  68. package/dist/core/trust/agreements.js +116 -0
  69. package/dist/core/trust/evidence.d.ts +220 -0
  70. package/dist/core/trust/evidence.js +334 -0
  71. package/dist/core/trust/index.d.ts +10 -0
  72. package/dist/core/trust/index.js +14 -1
  73. package/dist/core/trust/keyRotation.d.ts +148 -0
  74. package/dist/core/trust/keyRotation.js +267 -0
  75. package/dist/core/trust/permissions.d.ts +114 -0
  76. package/dist/core/trust/permissions.js +115 -0
  77. package/dist/core/trust/shaping.d.ts +42 -0
  78. package/dist/core/trust/shaping.js +47 -0
  79. package/dist/core/trust/storage/AbstractTrustStore.d.ts +2 -1
  80. package/dist/core/trust/storage/JsonTrustStore.d.ts +3 -2
  81. package/dist/core/trust/timestamps.d.ts +49 -0
  82. package/dist/core/trust/timestamps.js +78 -0
  83. package/dist/core/trust/trustUtils.d.ts +13 -2
  84. package/dist/core/trust/trustUtils.js +28 -3
  85. package/dist/core/trust/types.d.ts +302 -2
  86. package/dist/core/trust/verification.d.ts +52 -0
  87. package/dist/core/trust/verification.js +55 -0
  88. package/dist/server/btpsServer.d.ts +70 -5
  89. package/dist/server/btpsServer.js +263 -143
  90. package/dist/server/index.d.ts +3 -0
  91. package/dist/server/index.js +17 -1
  92. package/dist/server/libs/agreementOrderCheck.d.ts +29 -0
  93. package/dist/server/libs/agreementOrderCheck.js +29 -0
  94. package/dist/server/libs/artifactTimeWindow.d.ts +2 -2
  95. package/dist/server/libs/artifactTimeWindow.js +2 -1
  96. package/dist/server/libs/detailsRequestLimit.d.ts +27 -0
  97. package/dist/server/libs/detailsRequestLimit.js +28 -0
  98. package/dist/server/libs/detailsResponseCheck.d.ts +27 -0
  99. package/dist/server/libs/detailsResponseCheck.js +27 -0
  100. package/dist/server/libs/eventChainCheck.d.ts +31 -0
  101. package/dist/server/libs/eventChainCheck.js +39 -0
  102. package/dist/server/libs/inFlightLimit.d.ts +10 -0
  103. package/dist/server/libs/inFlightLimit.js +7 -1
  104. package/dist/server/libs/index.d.ts +5 -0
  105. package/dist/server/libs/index.js +10 -0
  106. package/dist/server/libs/keyChains.d.ts +14 -0
  107. package/dist/server/libs/keyChains.js +17 -0
  108. package/dist/server/libs/wrapForAgent.d.ts +20 -0
  109. package/dist/server/libs/wrapForAgent.js +36 -0
  110. package/dist/server/types.d.ts +95 -9
  111. package/package.json +12 -3
@@ -10,6 +10,7 @@ import { CURRENCY_CODES } from "../constants/currency.js";
10
10
  import { BTPS_FIELD_LIMITS } from "../constants/limits.js";
11
11
  import { BtpSignatureSchema, identitySchema } from "./shared.js";
12
12
  import { boundedArray, boundedRecord } from "./bounded.js";
13
+ import { BtpKeyChainSchema } from "./keyChainSchema.js";
13
14
  const {
14
15
  trustRequest,
15
16
  trustResponse,
@@ -17,6 +18,7 @@ const {
17
18
  receipt,
18
19
  payslip,
19
20
  letter,
21
+ agreement,
20
22
  attachments,
21
23
  references,
22
24
  lineItems,
@@ -24,7 +26,8 @@ const {
24
26
  agentInfoValues,
25
27
  recordKeys,
26
28
  documentId,
27
- referenceType
29
+ referenceType,
30
+ note
28
31
  } = BTPS_FIELD_LIMITS, metadataSchema = boundedRecord(z.unknown(), recordKeys), codeSchema = (max) => z.string().max(max).regex(/^[a-z0-9_]+(:[a-z0-9_]+)?$/, "must be a bare or prefixed code"), decimalText = z.string().regex(/^-?\d+(\.\d+)?$/, "must be a decimal written as text"), sha256Hex = z.string().regex(/^[0-9a-f]{64}$/, "must be a SHA-256 in lowercase hex"), BtpAttachmentSchema = z.object({
29
32
  content: z.string(),
30
33
 
@@ -47,7 +50,10 @@ const {
47
50
  ne: z.string().optional(),
48
51
  notIn: boundedArray(z.string(), query.notIn).optional(),
49
52
  notLike: z.string().optional()
50
- }), BtpTrustReqDocSchema = z.object({
53
+ }), BtpNoteSchema = (max) => z.union([
54
+ z.string().max(max),
55
+ z.object({ format: z.enum(["text", "markdown"]), content: z.string().max(max) }).strict()
56
+ ]), BtpTrustReqDocV1_1Schema = z.object({
51
57
  id: z.string().max(trustRequest.id),
52
58
  name: z.string().max(trustRequest.name),
53
59
  email: z.string().max(trustRequest.email).email(),
@@ -60,7 +66,9 @@ const {
60
66
  message: z.string().max(trustRequest.message).optional(),
61
67
  expiresAt: z.string().datetime().optional(),
62
68
  privacyType: z.enum(["unencrypted", "encrypted", "mixed"]).optional()
63
- }), BtpTrustResDocSchema = z.object({
69
+ }), BtpTrustReqDocSchema = BtpTrustReqDocV1_1Schema.extend({
70
+ message: BtpNoteSchema(trustRequest.message).optional()
71
+ }), BtpTrustResDocV1_1Schema = z.object({
64
72
  id: z.string().max(trustResponse.id),
65
73
  decision: z.enum(["accepted", "rejected", "revoked", "blocked"]),
66
74
  decidedAt: z.string().datetime(),
@@ -69,7 +77,9 @@ const {
69
77
  retryAfterDate: z.string().datetime().optional(),
70
78
  message: z.string().max(trustResponse.message).optional(),
71
79
  privacyType: z.enum(["unencrypted", "encrypted", "mixed"]).optional()
72
- }), BtpLineItemsSchema = z.object({
80
+ }), BtpTrustResDocSchema = BtpTrustResDocV1_1Schema.extend({
81
+ message: BtpNoteSchema(trustResponse.message).optional()
82
+ }), BtpDetailsNoteDocSchema = z.object({ message: BtpNoteSchema(BTPS_FIELD_LIMITS.note).optional() }).strict(), BtpLineItemsSchema = z.object({
73
83
  columns: boundedArray(z.string(), lineItems.columns),
74
84
 
75
85
  rows: boundedArray(
@@ -87,7 +97,7 @@ const {
87
97
  recordKeys,
88
98
  z.string().regex(/^[a-z0-9_]+:[a-z0-9_]+$/)
89
99
  ).optional()
90
- }), BtpInvoiceDocSchema = z.object({
100
+ }), BtpInvoiceDocV1_1Schema = z.object({
91
101
  type: z.literal("invoice"),
92
102
  title: z.string(),
93
103
  id: z.string().max(documentId),
@@ -112,7 +122,7 @@ const {
112
122
  metadata: metadataSchema.optional()
113
123
  }).strict(), BtpPositiveMoneySchema = BtpMoneySchema.extend({
114
124
  amount: z.number().int().safe().positive()
115
- }), BtpReceiptDocSchema = BtpInvoiceDocSchema.omit({ dueAt: !0, paymentLink: !0 }).extend({
125
+ }), receiptFields = {
116
126
  type: z.literal("receipt"),
117
127
  lineItems: BtpLineItemsSchema.optional(),
118
128
  payment: z.object({
@@ -122,7 +132,88 @@ const {
122
132
  method: codeSchema(receipt.paymentMethod).optional(),
123
133
  reference: z.string().max(receipt.paymentReference).optional()
124
134
  })
125
- }).strict(), payslipCode = codeSchema(payslip.code), BtpPayslipDocSchema = z.object({
135
+ }, BtpReceiptDocV1_1Schema = BtpInvoiceDocV1_1Schema.omit({
136
+ dueAt: !0,
137
+ paymentLink: !0
138
+ }).extend(receiptFields).strict(), httpsUrl = z.string().max(BTPS_FIELD_LIMITS.url).url().refine((url) => url.startsWith("https://"), "must be an https URL"), semver = z.string().regex(/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/, "must be a semantic version"), { signature } = BTPS_FIELD_LIMITS, HIDDEN = new Set([
139
+ 8203,
140
+ 8204,
141
+ 8205,
142
+ 8206,
143
+ 8207,
144
+ 8234,
145
+ 8235,
146
+ 8236,
147
+ 8237,
148
+ 8238,
149
+ 8294,
150
+ 8295,
151
+ 8296,
152
+ 8297,
153
+ 65279
154
+ ]), isPlainText = (text) => [...text].every((char) => {
155
+ const code = char.codePointAt(0) ?? 0;
156
+ return code >= 32 && code !== 127 && char !== "<" && char !== ">" && !HIDDEN.has(code);
157
+ }), signatureText = z.string().min(1).max(signature.text).refine(isPlainText, "must be plain text"), drawnSignature = z.object({
158
+ strokes: boundedArray(
159
+ boundedArray(z.number().int().min(0), signature.strokePoints * 2, 2),
160
+ signature.strokes,
161
+ 1
162
+ )
163
+ }).strict().superRefine(({ strokes }, ctx) => {
164
+ let points = 0;
165
+ strokes.forEach((stroke, index) => {
166
+ points += stroke.length / 2;
167
+ const onCanvas = stroke.every(
168
+ (value, at) => at % 2 === 0 ? value <= signature.width : value <= signature.height
169
+ );
170
+ (stroke.length % 2 !== 0 || !onCanvas) && ctx.addIssue({
171
+ code: z.ZodIssueCode.custom,
172
+ message: `each stroke is x, y pairs on a ${signature.width} by ${signature.height} canvas`,
173
+ path: ["strokes", index]
174
+ });
175
+ }), points > signature.points && ctx.addIssue({
176
+ code: z.ZodIssueCode.custom,
177
+ message: `a drawn signature has at most ${signature.points} points`,
178
+ path: ["strokes"]
179
+ });
180
+ }), BtpTemplateValuesSchema = z.object({
181
+ "signature.typed": signatureText.optional(),
182
+ "signature.name": signatureText.optional(),
183
+ "signature.title": signatureText.optional(),
184
+ "signature.drawn": drawnSignature.optional()
185
+ }).strict(), templateSchema = (withValues) => z.object({
186
+ name: z.string(),
187
+ data: boundedRecord(z.unknown(), recordKeys).pipe(
188
+ z.object({
189
+ version: semver.optional(),
190
+ sha256: sha256Hex.optional(),
191
+ source: httpsUrl.optional(),
192
+ logo: z.object({ sha256: sha256Hex, source: httpsUrl }).strict().optional(),
193
+ values: withValues ? BtpTemplateValuesSchema.optional() : z.undefined({
194
+ invalid_type_error: "values are only on a letter's or an agreement's template"
195
+ })
196
+ }).catchall(z.unknown()).refine((data) => data.source === void 0 || data.sha256 !== void 0, {
197
+ message: "a source comes with the sha256 of what it serves",
198
+ path: ["sha256"]
199
+ })
200
+ )
201
+ }), BtpTemplateSchema = templateSchema(!1), BtpLetterTemplateSchema = templateSchema(!0), BtpDiscountSchema = z.union([
202
+ z.object({ amount: z.number().int().safe().positive() }).strict(),
203
+ z.object({ percent: z.number().int().min(1).max(1e4) }).strict()
204
+ ]), BtpDiscountedLineItemsSchema = BtpLineItemsSchema.refine(
205
+ ({ rows }) => rows.every(
206
+ (row) => row.discount === void 0 || typeof row.discount == "number" && row.discount >= 0
207
+ ),
208
+ { message: "a line discount is an integer of at least 0", path: ["rows"] }
209
+ ), documentFields1_2 = {
210
+ description: BtpNoteSchema(note).optional(),
211
+ discount: BtpDiscountSchema.optional(),
212
+ template: BtpTemplateSchema.optional()
213
+ }, BtpInvoiceDocSchema = BtpInvoiceDocV1_1Schema.extend({
214
+ ...documentFields1_2,
215
+ lineItems: BtpDiscountedLineItemsSchema
216
+ }).strict(), BtpReceiptDocSchema = BtpInvoiceDocSchema.omit({ dueAt: !0, paymentLink: !0 }).extend({ ...receiptFields, lineItems: BtpDiscountedLineItemsSchema.optional() }).strict(), payslipCode = codeSchema(payslip.code), BtpPayslipDocV1_1Schema = z.object({
126
217
  type: z.literal("payslip"),
127
218
  title: z.string(),
128
219
  id: z.string().max(documentId),
@@ -190,7 +281,7 @@ const {
190
281
  attachments: boundedArray(BtpAttachmentSchema, attachments).optional(),
191
282
  references: boundedArray(BtpDocReferenceSchema, references).optional(),
192
283
  metadata: metadataSchema.optional()
193
- }).strict(), BtpLetterDocSchema = z.object({
284
+ }).strict(), BtpLetterDocV1_1Schema = z.object({
194
285
  type: z.literal("letter"),
195
286
  title: z.string().max(letter.title),
196
287
  id: z.string().max(documentId),
@@ -203,12 +294,44 @@ const {
203
294
  attachments: boundedArray(BtpAttachmentSchema, letter.attachments).optional(),
204
295
  references: boundedArray(BtpDocReferenceSchema, letter.references).optional(),
205
296
  metadata: metadataSchema.optional()
297
+ }).strict(), BtpPayslipDocSchema = BtpPayslipDocV1_1Schema.extend({
298
+ template: BtpTemplateSchema.optional()
299
+ }).strict(), BtpLetterDocSchema = BtpLetterDocV1_1Schema.extend({
300
+ template: BtpLetterTemplateSchema.optional()
301
+ }).strict(), BtpDocumentV1_1Schema = z.discriminatedUnion("type", [
302
+ BtpInvoiceDocV1_1Schema,
303
+ BtpReceiptDocV1_1Schema,
304
+ BtpPayslipDocV1_1Schema,
305
+ BtpLetterDocV1_1Schema
306
+ ]), BtpAcceptanceSchema = z.object({
307
+ by: z.string().datetime().optional(),
308
+ terms: boundedArray(sha256Hex, letter.attachments),
309
+ signers: boundedArray(z.object({ address: identitySchema }).strict(), agreement.signers),
310
+ signingOrder: z.enum(["any", "sequential"])
311
+ }).strict().superRefine(({ terms, signers }, ctx) => {
312
+ const issue = (message2, path) => ctx.addIssue({ code: z.ZodIssueCode.custom, message: message2, path: [path] });
313
+ signers.length === 0 && issue("an agreement has at least one signer", "signers");
314
+ const addresses = signers.map(({ address }) => address.toLowerCase());
315
+ new Set(addresses).size !== addresses.length && issue("each signer appears once", "signers"), new Set(terms).size !== terms.length && issue("each term appears once", "terms");
316
+ }), BtpAgreementDocSchema = BtpLetterDocSchema.extend({
317
+ type: z.literal("agreement"),
318
+ acceptance: BtpAcceptanceSchema,
319
+ originatorSignature: z.object({ selector: z.string().min(1), signature: BtpSignatureSchema }).strict()
206
320
  }).strict(), BtpDocumentSchema = z.discriminatedUnion("type", [
207
321
  BtpInvoiceDocSchema,
208
322
  BtpReceiptDocSchema,
209
323
  BtpPayslipDocSchema,
210
- BtpLetterDocSchema
211
- ]), lifecycleEventFields = {
324
+ BtpLetterDocSchema,
325
+ BtpAgreementDocSchema
326
+ ]).superRefine((document, ctx) => {
327
+ if (document.type !== "agreement") return;
328
+ const attached = new Set(document.attachments?.map(({ sha256 }) => sha256));
329
+ document.acceptance.terms.some((term) => !attached.has(term)) && ctx.addIssue({
330
+ code: z.ZodIssueCode.custom,
331
+ message: "each term is the sha256 of one of its attachments",
332
+ path: ["acceptance", "terms"]
333
+ });
334
+ }), lifecycleEventFields = {
212
335
  type: z.literal("lifecycle_event"),
213
336
  id: z.string().uuid(),
214
337
 
@@ -219,40 +342,150 @@ const {
219
342
  message: z.string().max(event.message).optional()
220
343
  }, BtpEventAmountSchema = BtpMoneySchema.extend({
221
344
  amount: z.number().int().safe().positive()
222
- }), BtpLifecycleEventSchema = z.discriminatedUnion("eventType", [
223
- z.object({ ...lifecycleEventFields, eventType: z.literal("paid") }).strict(),
224
- z.object({
225
- ...lifecycleEventFields,
226
- eventType: z.literal("partially_paid"),
227
- amount: BtpEventAmountSchema
228
- }).strict(),
229
- z.object({
230
- ...lifecycleEventFields,
231
- eventType: z.literal("disputed"),
232
-
233
- reason: codeSchema(event.reason)
234
- }).strict(),
235
- z.object({ ...lifecycleEventFields, eventType: z.literal("refund_requested") }).strict(),
345
+ }), paidEvent = z.object({ ...lifecycleEventFields, eventType: z.literal("paid") }).strict(), partiallyPaidEvent = z.object({
346
+ ...lifecycleEventFields,
347
+ eventType: z.literal("partially_paid"),
348
+ amount: BtpEventAmountSchema
349
+ }).strict(), disputedEvent = z.object({
350
+ ...lifecycleEventFields,
351
+ eventType: z.literal("disputed"),
352
+
353
+ reason: codeSchema(event.reason)
354
+ }).strict(), refundRequestedEvent = z.object({ ...lifecycleEventFields, eventType: z.literal("refund_requested") }).strict(), refundEvent = z.object({
355
+ ...lifecycleEventFields,
356
+ eventType: z.literal("refund"),
357
+ amount: BtpEventAmountSchema,
358
+ inReplyTo: z.string().uuid()
359
+
360
+ }).strict(), responseEvent = z.object({
361
+ ...lifecycleEventFields,
362
+ eventType: z.literal("response"),
363
+ decision: z.enum(["accepted", "rejected"]),
364
+ inReplyTo: z.string().uuid(),
365
+
366
+
367
+ remainingDueDate: z.string().datetime().optional()
368
+ }).strict(), onlyAcceptedHasRemainingDueDate = (event2) => event2.eventType !== "response" || event2.remainingDueDate === void 0 || event2.decision === "accepted", remainingDueDateRule = {
369
+ message: "remainingDueDate is only on an accepted response",
370
+ path: ["remainingDueDate"]
371
+ }, BtpLifecycleEventV1_1Schema = z.discriminatedUnion("eventType", [
372
+ paidEvent,
373
+ partiallyPaidEvent,
374
+ disputedEvent,
375
+ refundRequestedEvent,
376
+ refundEvent,
377
+ responseEvent
378
+ ]).refine(onlyAcceptedHasRemainingDueDate, remainingDueDateRule), follows = { follows: z.string().uuid().optional() }, message = { message: BtpNoteSchema(event.message).optional() }, BtpLifecycleEventSchema = z.discriminatedUnion("eventType", [
379
+ paidEvent.extend({ ...follows, ...message }),
380
+ partiallyPaidEvent.extend({ ...follows, ...message }),
381
+ disputedEvent.extend({ ...follows, ...message }),
382
+ refundRequestedEvent.extend({ ...follows, ...message }),
383
+ refundEvent.extend(message),
384
+ responseEvent.extend(message),
236
385
  z.object({
237
386
  ...lifecycleEventFields,
238
- eventType: z.literal("refund"),
239
- amount: BtpEventAmountSchema,
387
+ ...message,
388
+ eventType: z.literal("withdrawn"),
240
389
  inReplyTo: z.string().uuid()
241
390
 
391
+ }).strict()
392
+ ]).refine(onlyAcceptedHasRemainingDueDate, remainingDueDateRule), BtpAgreementRefSchema = z.object({
393
+ originator: identitySchema,
394
+ id: z.string().min(1).max(documentId),
395
+ contentSha256: sha256Hex
396
+ }).strict(), BtpAgreementStatementSchema = z.object({
397
+ agreement: BtpAgreementRefSchema,
398
+ terms: boundedArray(sha256Hex, letter.attachments),
399
+ signer: identitySchema,
400
+ decision: z.enum(["accepted", "declined"]),
401
+ signedAt: z.string().datetime(),
402
+ previous: sha256Hex.optional()
403
+ }).strict(), BtpStatementRecordSchema = z.object({
404
+ statement: BtpAgreementStatementSchema,
405
+ signature: BtpSignatureSchema,
406
+ selector: z.string().min(1).optional(),
407
+ delegatedArtifact: z.object({
408
+ from: identitySchema,
409
+ signature: BtpSignatureSchema,
410
+ delegation: z.object({
411
+ agentPubKey: z.string(),
412
+ signedBy: identitySchema,
413
+ signature: BtpSignatureSchema
414
+ }).passthrough()
415
+ }).passthrough().optional()
416
+ }).strict().refine(
417
+ (record) => record.selector === void 0 != (record.delegatedArtifact === void 0),
418
+ {
419
+ message: "a record has exactly one of selector and delegatedArtifact"
420
+ }
421
+ ), agreementEventFields = {
422
+ type: z.literal("agreement_event"),
423
+ id: z.string().uuid(),
424
+
425
+ occurredAt: z.string().datetime(),
426
+ agreement: BtpAgreementRefSchema
427
+ }, statements = boundedArray(BtpStatementRecordSchema, agreement.signers), BtpAgreementEventSchema = z.discriminatedUnion("eventType", [
428
+ z.object({
429
+ ...agreementEventFields,
430
+ eventType: z.literal("accepted"),
431
+ statement: BtpStatementRecordSchema
242
432
  }).strict(),
243
433
  z.object({
244
- ...lifecycleEventFields,
245
- eventType: z.literal("response"),
246
- decision: z.enum(["accepted", "rejected"]),
247
- inReplyTo: z.string().uuid(),
248
-
249
-
250
- remainingDueDate: z.string().datetime().optional()
251
- }).strict()
252
- ]).refine(
253
- (event2) => event2.eventType !== "response" || event2.remainingDueDate === void 0 || event2.decision === "accepted",
254
- { message: "remainingDueDate is only on an accepted response", path: ["remainingDueDate"] }
255
- ), BtpAuthReqDocSchema = z.object({
434
+ ...agreementEventFields,
435
+ ...message,
436
+ eventType: z.literal("declined"),
437
+ statement: BtpStatementRecordSchema
438
+ }).strict(),
439
+ z.object({ ...agreementEventFields, eventType: z.literal("agreement_progress"), statements }).strict(),
440
+ z.object({ ...agreementEventFields, eventType: z.literal("agreement_complete"), statements }).strict()
441
+ ]).superRefine((event2, ctx) => {
442
+ "statement" in event2 && event2.statement.statement.decision !== event2.eventType && ctx.addIssue({
443
+ code: z.ZodIssueCode.custom,
444
+ message: "an answer's eventType is its statement's decision",
445
+ path: ["statement"]
446
+ }), "statements" in event2 && event2.statements.length === 0 && ctx.addIssue({
447
+ code: z.ZodIssueCode.custom,
448
+ message: "forwarding carries at least one statement",
449
+ path: ["statements"]
450
+ });
451
+ }), BtpEventSchema = z.union([BtpLifecycleEventSchema, BtpAgreementEventSchema]), BtpEvidenceKeySchema = z.object({
452
+ identity: identitySchema,
453
+ selector: z.string().min(1),
454
+ publicKey: z.string().min(1),
455
+ fingerprint: z.string().min(1)
456
+ }).strict(), evidenceIdentities = agreement.signers * 4 + 2, BtpEvidenceKeyChainSchema = z.object({
457
+ identity: identitySchema,
458
+ records: BtpKeyChainSchema,
459
+ operatorAnswer: z.object({
460
+ signedBy: identitySchema,
461
+ selector: z.string().min(1),
462
+ signature: BtpSignatureSchema,
463
+ document: z.object({ identity: identitySchema, keyChain: BtpKeyChainSchema }).passthrough()
464
+ }).passthrough().optional()
465
+ }).strict(), BtpEvidenceArrivalSchema = z.object({
466
+ statementSha256: sha256Hex,
467
+ arrivedAt: z.string().datetime(),
468
+ receivedBy: identitySchema,
469
+ selector: z.string().min(1),
470
+ signature: BtpSignatureSchema
471
+ }).strict(), BtpEvidenceBundleSchema = z.object({
472
+ format: z.literal("btps-evidence/1"),
473
+ originator: identitySchema,
474
+ agreement: BtpAgreementDocSchema,
475
+ statements,
476
+
477
+
478
+ keys: boundedArray(BtpEvidenceKeySchema, evidenceIdentities),
479
+
480
+ timestamps: boundedArray(
481
+ z.object({ statementSha256: sha256Hex, token: z.string().min(1) }).strict(),
482
+ agreement.signers
483
+ ).optional(),
484
+
485
+ keyChains: boundedArray(BtpEvidenceKeyChainSchema, evidenceIdentities).optional(),
486
+
487
+ arrivals: boundedArray(BtpEvidenceArrivalSchema, agreement.signers).optional()
488
+ }).strict(), BtpAuthReqDocSchema = z.object({
256
489
  identity: identitySchema,
257
490
  authToken: z.string(),
258
491
  publicKey: z.string(),
@@ -270,8 +503,10 @@ const {
270
503
  selector: z.string(),
271
504
  publicKey: z.string(),
272
505
  keyType: z.literal("rsa"),
273
- version: z.string()
274
- }), BtpAgentQueryDocSchema = z.object({
506
+ version: z.string(),
507
+ identity: identitySchema.optional(),
508
+ keyChain: BtpKeyChainSchema.optional()
509
+ }), BtpCreatedResDocSchema = z.object({ id: z.string().min(1).max(documentId) }).strict(), BtpAgentQueryDocSchema = z.object({
275
510
  title: BtpStringQueryFilterSchema.optional(),
276
511
  from: BtpStringQueryFilterSchema.optional(),
277
512
  to: BtpStringQueryFilterSchema.optional()
@@ -279,23 +514,46 @@ const {
279
514
  BtpTrustReqDocSchema,
280
515
  BtpTrustResDocSchema,
281
516
  BtpDocumentSchema,
282
- BtpLifecycleEventSchema
517
+ BtpLifecycleEventSchema,
518
+ BtpAgreementEventSchema,
519
+ BtpTrustReqDocV1_1Schema,
520
+ BtpTrustResDocV1_1Schema,
521
+ BtpDocumentV1_1Schema,
522
+ BtpLifecycleEventV1_1Schema
283
523
  ]);
284
524
  export {
285
525
  BtpAgentQueryDocSchema,
526
+ BtpAgreementDocSchema,
527
+ BtpAgreementEventSchema,
528
+ BtpAgreementStatementSchema,
286
529
  BtpAttachmentSchema,
287
530
  BtpAuthReqDocSchema,
288
531
  BtpAuthResDocSchema,
532
+ BtpCreatedResDocSchema,
533
+ BtpDetailsNoteDocSchema,
289
534
  BtpDocReferenceSchema,
290
535
  BtpDocSchema,
291
536
  BtpDocumentSchema,
537
+ BtpDocumentV1_1Schema,
538
+ BtpEventSchema,
539
+ BtpEvidenceBundleSchema,
292
540
  BtpIdentityResDocSchema,
293
541
  BtpInvoiceDocSchema,
542
+ BtpInvoiceDocV1_1Schema,
294
543
  BtpLetterDocSchema,
544
+ BtpLetterDocV1_1Schema,
295
545
  BtpLifecycleEventSchema,
546
+ BtpLifecycleEventV1_1Schema,
296
547
  BtpPayslipDocSchema,
548
+ BtpPayslipDocV1_1Schema,
297
549
  BtpReceiptDocSchema,
550
+ BtpReceiptDocV1_1Schema,
551
+ BtpStatementRecordSchema,
298
552
  BtpStringQueryFilterSchema,
299
553
  BtpTrustReqDocSchema,
300
- BtpTrustResDocSchema
554
+ BtpTrustReqDocV1_1Schema,
555
+ BtpTrustResDocSchema,
556
+ BtpTrustResDocV1_1Schema,
557
+ httpsUrl,
558
+ sha256Hex
301
559
  };