@btps/sdk 1.1.3 → 1.2.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/CHANGELOG.md +461 -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 +99 -12
  52. package/dist/core/server/schemas/bounded.js +2 -2
  53. package/dist/core/server/schemas/btpsDocsSchema.d.ts +36009 -1083
  54. package/dist/core/server/schemas/btpsDocsSchema.js +305 -42
  55. package/dist/core/server/schemas/helpers.d.ts +10090 -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 +312 -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 +79 -5
  89. package/dist/server/btpsServer.js +297 -145
  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 +103 -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,89 @@ 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"), strokes = boundedArray(
158
+ boundedArray(z.number().int().min(0), signature.strokePoints * 2, 2),
159
+ signature.strokes,
160
+ 1
161
+ ), strokesOnCanvas = ({ strokes: strokes2 }, ctx) => {
162
+ let points = 0;
163
+ strokes2.forEach((stroke, index) => {
164
+ points += stroke.length / 2;
165
+ const onCanvas = stroke.every(
166
+ (value, at) => at % 2 === 0 ? value <= signature.width : value <= signature.height
167
+ );
168
+ (stroke.length % 2 !== 0 || !onCanvas) && ctx.addIssue({
169
+ code: z.ZodIssueCode.custom,
170
+ message: `each stroke is x, y pairs on a ${signature.width} by ${signature.height} canvas`,
171
+ path: ["strokes", index]
172
+ });
173
+ }), points > signature.points && ctx.addIssue({
174
+ code: z.ZodIssueCode.custom,
175
+ message: `a drawn signature has at most ${signature.points} points`,
176
+ path: ["strokes"]
177
+ });
178
+ }, drawnSignature = z.object({ strokes }).strict().superRefine(strokesOnCanvas), BtpVisualSignatureSchema = z.union([
179
+ z.object({ type: z.literal("typed"), text: signatureText }).strict(),
180
+ z.object({ type: z.literal("drawn"), strokes }).strict().superRefine(strokesOnCanvas)
181
+ ]), BtpTemplateValuesSchema = z.object({
182
+ "signature.typed": signatureText.optional(),
183
+ "signature.name": signatureText.optional(),
184
+ "signature.title": signatureText.optional(),
185
+ "signature.drawn": drawnSignature.optional()
186
+ }).strict(), templateSchema = (withValues) => z.object({
187
+ name: z.string(),
188
+ data: boundedRecord(z.unknown(), recordKeys).pipe(
189
+ z.object({
190
+ version: semver.optional(),
191
+ sha256: sha256Hex.optional(),
192
+ source: httpsUrl.optional(),
193
+ logo: z.object({ sha256: sha256Hex, source: httpsUrl }).strict().optional(),
194
+ values: withValues ? BtpTemplateValuesSchema.optional() : z.undefined({
195
+ invalid_type_error: "values are only on a letter's or an agreement's template"
196
+ })
197
+ }).catchall(z.unknown()).refine((data) => data.source === void 0 || data.sha256 !== void 0, {
198
+ message: "a source comes with the sha256 of what it serves",
199
+ path: ["sha256"]
200
+ })
201
+ )
202
+ }), BtpTemplateSchema = templateSchema(!1), BtpLetterTemplateSchema = templateSchema(!0), BtpDiscountSchema = z.union([
203
+ z.object({ amount: z.number().int().safe().positive() }).strict(),
204
+ z.object({ percent: z.number().int().min(1).max(1e4) }).strict()
205
+ ]), BtpDiscountedLineItemsSchema = BtpLineItemsSchema.refine(
206
+ ({ rows }) => rows.every(
207
+ (row) => row.discount === void 0 || typeof row.discount == "number" && row.discount >= 0
208
+ ),
209
+ { message: "a line discount is an integer of at least 0", path: ["rows"] }
210
+ ), documentFields1_2 = {
211
+ description: BtpNoteSchema(note).optional(),
212
+ discount: BtpDiscountSchema.optional(),
213
+ template: BtpTemplateSchema.optional()
214
+ }, BtpInvoiceDocSchema = BtpInvoiceDocV1_1Schema.extend({
215
+ ...documentFields1_2,
216
+ lineItems: BtpDiscountedLineItemsSchema
217
+ }).strict(), BtpReceiptDocSchema = BtpInvoiceDocSchema.omit({ dueAt: !0, paymentLink: !0 }).extend({ ...receiptFields, lineItems: BtpDiscountedLineItemsSchema.optional() }).strict(), payslipCode = codeSchema(payslip.code), BtpPayslipDocV1_1Schema = z.object({
126
218
  type: z.literal("payslip"),
127
219
  title: z.string(),
128
220
  id: z.string().max(documentId),
@@ -190,7 +282,7 @@ const {
190
282
  attachments: boundedArray(BtpAttachmentSchema, attachments).optional(),
191
283
  references: boundedArray(BtpDocReferenceSchema, references).optional(),
192
284
  metadata: metadataSchema.optional()
193
- }).strict(), BtpLetterDocSchema = z.object({
285
+ }).strict(), BtpLetterDocV1_1Schema = z.object({
194
286
  type: z.literal("letter"),
195
287
  title: z.string().max(letter.title),
196
288
  id: z.string().max(documentId),
@@ -203,12 +295,44 @@ const {
203
295
  attachments: boundedArray(BtpAttachmentSchema, letter.attachments).optional(),
204
296
  references: boundedArray(BtpDocReferenceSchema, letter.references).optional(),
205
297
  metadata: metadataSchema.optional()
298
+ }).strict(), BtpPayslipDocSchema = BtpPayslipDocV1_1Schema.extend({
299
+ template: BtpTemplateSchema.optional()
300
+ }).strict(), BtpLetterDocSchema = BtpLetterDocV1_1Schema.extend({
301
+ template: BtpLetterTemplateSchema.optional()
302
+ }).strict(), BtpDocumentV1_1Schema = z.discriminatedUnion("type", [
303
+ BtpInvoiceDocV1_1Schema,
304
+ BtpReceiptDocV1_1Schema,
305
+ BtpPayslipDocV1_1Schema,
306
+ BtpLetterDocV1_1Schema
307
+ ]), BtpAcceptanceSchema = z.object({
308
+ by: z.string().datetime().optional(),
309
+ terms: boundedArray(sha256Hex, letter.attachments),
310
+ signers: boundedArray(z.object({ address: identitySchema }).strict(), agreement.signers),
311
+ signingOrder: z.enum(["any", "sequential"])
312
+ }).strict().superRefine(({ terms, signers }, ctx) => {
313
+ const issue = (message2, path) => ctx.addIssue({ code: z.ZodIssueCode.custom, message: message2, path: [path] });
314
+ signers.length === 0 && issue("an agreement has at least one signer", "signers");
315
+ const addresses = signers.map(({ address }) => address.toLowerCase());
316
+ new Set(addresses).size !== addresses.length && issue("each signer appears once", "signers"), new Set(terms).size !== terms.length && issue("each term appears once", "terms");
317
+ }), BtpAgreementDocSchema = BtpLetterDocSchema.extend({
318
+ type: z.literal("agreement"),
319
+ acceptance: BtpAcceptanceSchema,
320
+ originatorSignature: z.object({ selector: z.string().min(1), signature: BtpSignatureSchema }).strict()
206
321
  }).strict(), BtpDocumentSchema = z.discriminatedUnion("type", [
207
322
  BtpInvoiceDocSchema,
208
323
  BtpReceiptDocSchema,
209
324
  BtpPayslipDocSchema,
210
- BtpLetterDocSchema
211
- ]), lifecycleEventFields = {
325
+ BtpLetterDocSchema,
326
+ BtpAgreementDocSchema
327
+ ]).superRefine((document, ctx) => {
328
+ if (document.type !== "agreement") return;
329
+ const attached = new Set(document.attachments?.map(({ sha256 }) => sha256));
330
+ document.acceptance.terms.some((term) => !attached.has(term)) && ctx.addIssue({
331
+ code: z.ZodIssueCode.custom,
332
+ message: "each term is the sha256 of one of its attachments",
333
+ path: ["acceptance", "terms"]
334
+ });
335
+ }), lifecycleEventFields = {
212
336
  type: z.literal("lifecycle_event"),
213
337
  id: z.string().uuid(),
214
338
 
@@ -219,40 +343,154 @@ const {
219
343
  message: z.string().max(event.message).optional()
220
344
  }, BtpEventAmountSchema = BtpMoneySchema.extend({
221
345
  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(),
346
+ }), paidEvent = z.object({ ...lifecycleEventFields, eventType: z.literal("paid") }).strict(), partiallyPaidEvent = z.object({
347
+ ...lifecycleEventFields,
348
+ eventType: z.literal("partially_paid"),
349
+ amount: BtpEventAmountSchema
350
+ }).strict(), disputedEvent = z.object({
351
+ ...lifecycleEventFields,
352
+ eventType: z.literal("disputed"),
353
+
354
+ reason: codeSchema(event.reason)
355
+ }).strict(), refundRequestedEvent = z.object({ ...lifecycleEventFields, eventType: z.literal("refund_requested") }).strict(), refundEvent = z.object({
356
+ ...lifecycleEventFields,
357
+ eventType: z.literal("refund"),
358
+ amount: BtpEventAmountSchema,
359
+ inReplyTo: z.string().uuid()
360
+
361
+ }).strict(), responseEvent = z.object({
362
+ ...lifecycleEventFields,
363
+ eventType: z.literal("response"),
364
+ decision: z.enum(["accepted", "rejected"]),
365
+ inReplyTo: z.string().uuid(),
366
+
367
+
368
+ remainingDueDate: z.string().datetime().optional()
369
+ }).strict(), onlyAcceptedHasRemainingDueDate = (event2) => event2.eventType !== "response" || event2.remainingDueDate === void 0 || event2.decision === "accepted", remainingDueDateRule = {
370
+ message: "remainingDueDate is only on an accepted response",
371
+ path: ["remainingDueDate"]
372
+ }, BtpLifecycleEventV1_1Schema = z.discriminatedUnion("eventType", [
373
+ paidEvent,
374
+ partiallyPaidEvent,
375
+ disputedEvent,
376
+ refundRequestedEvent,
377
+ refundEvent,
378
+ responseEvent
379
+ ]).refine(onlyAcceptedHasRemainingDueDate, remainingDueDateRule), follows = { follows: z.string().uuid().optional() }, message = { message: BtpNoteSchema(event.message).optional() }, BtpLifecycleEventSchema = z.discriminatedUnion("eventType", [
380
+ paidEvent.extend({ ...follows, ...message }),
381
+ partiallyPaidEvent.extend({ ...follows, ...message }),
382
+ disputedEvent.extend({ ...follows, ...message }),
383
+ refundRequestedEvent.extend({ ...follows, ...message }),
384
+ refundEvent.extend(message),
385
+ responseEvent.extend(message),
236
386
  z.object({
237
387
  ...lifecycleEventFields,
238
- eventType: z.literal("refund"),
239
- amount: BtpEventAmountSchema,
388
+ ...message,
389
+ eventType: z.literal("withdrawn"),
240
390
  inReplyTo: z.string().uuid()
241
391
 
392
+ }).strict()
393
+ ]).refine(onlyAcceptedHasRemainingDueDate, remainingDueDateRule), BtpAgreementRefSchema = z.object({
394
+ originator: identitySchema,
395
+ id: z.string().min(1).max(documentId),
396
+ contentSha256: sha256Hex
397
+ }).strict(), BtpAgreementStatementSchema = z.object({
398
+ agreement: BtpAgreementRefSchema,
399
+ terms: boundedArray(sha256Hex, letter.attachments),
400
+ signer: identitySchema,
401
+ decision: z.enum(["accepted", "declined"]),
402
+ signedAt: z.string().datetime(),
403
+ previous: sha256Hex.optional(),
404
+ visualSignature: BtpVisualSignatureSchema.optional()
405
+ }).strict().refine((s) => s.visualSignature === void 0 || s.decision === "accepted", {
406
+ message: "only an acceptance carries a visualSignature",
407
+ path: ["visualSignature"]
408
+ }), BtpStatementRecordSchema = z.object({
409
+ statement: BtpAgreementStatementSchema,
410
+ signature: BtpSignatureSchema,
411
+ selector: z.string().min(1).optional(),
412
+ delegatedArtifact: z.object({
413
+ from: identitySchema,
414
+ signature: BtpSignatureSchema,
415
+ delegation: z.object({
416
+ agentPubKey: z.string(),
417
+ signedBy: identitySchema,
418
+ signature: BtpSignatureSchema
419
+ }).passthrough()
420
+ }).passthrough().optional()
421
+ }).strict().refine(
422
+ (record) => record.selector === void 0 != (record.delegatedArtifact === void 0),
423
+ {
424
+ message: "a record has exactly one of selector and delegatedArtifact"
425
+ }
426
+ ), agreementEventFields = {
427
+ type: z.literal("agreement_event"),
428
+ id: z.string().uuid(),
429
+
430
+ occurredAt: z.string().datetime(),
431
+ agreement: BtpAgreementRefSchema
432
+ }, statements = boundedArray(BtpStatementRecordSchema, agreement.signers), BtpAgreementEventSchema = z.discriminatedUnion("eventType", [
433
+ z.object({
434
+ ...agreementEventFields,
435
+ eventType: z.literal("accepted"),
436
+ statement: BtpStatementRecordSchema
242
437
  }).strict(),
243
438
  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({
439
+ ...agreementEventFields,
440
+ ...message,
441
+ eventType: z.literal("declined"),
442
+ statement: BtpStatementRecordSchema
443
+ }).strict(),
444
+ z.object({ ...agreementEventFields, eventType: z.literal("agreement_progress"), statements }).strict(),
445
+ z.object({ ...agreementEventFields, eventType: z.literal("agreement_complete"), statements }).strict()
446
+ ]).superRefine((event2, ctx) => {
447
+ "statement" in event2 && event2.statement.statement.decision !== event2.eventType && ctx.addIssue({
448
+ code: z.ZodIssueCode.custom,
449
+ message: "an answer's eventType is its statement's decision",
450
+ path: ["statement"]
451
+ }), "statements" in event2 && event2.statements.length === 0 && ctx.addIssue({
452
+ code: z.ZodIssueCode.custom,
453
+ message: "forwarding carries at least one statement",
454
+ path: ["statements"]
455
+ });
456
+ }), BtpEventSchema = z.union([BtpLifecycleEventSchema, BtpAgreementEventSchema]), BtpEvidenceKeySchema = z.object({
457
+ identity: identitySchema,
458
+ selector: z.string().min(1),
459
+ publicKey: z.string().min(1),
460
+ fingerprint: z.string().min(1)
461
+ }).strict(), evidenceIdentities = agreement.signers * 4 + 2, BtpEvidenceKeyChainSchema = z.object({
462
+ identity: identitySchema,
463
+ records: BtpKeyChainSchema,
464
+ operatorAnswer: z.object({
465
+ signedBy: identitySchema,
466
+ selector: z.string().min(1),
467
+ signature: BtpSignatureSchema,
468
+ document: z.object({ identity: identitySchema, keyChain: BtpKeyChainSchema }).passthrough()
469
+ }).passthrough().optional()
470
+ }).strict(), BtpEvidenceArrivalSchema = z.object({
471
+ statementSha256: sha256Hex,
472
+ arrivedAt: z.string().datetime(),
473
+ receivedBy: identitySchema,
474
+ selector: z.string().min(1),
475
+ signature: BtpSignatureSchema
476
+ }).strict(), BtpEvidenceBundleSchema = z.object({
477
+ format: z.literal("btps-evidence/1"),
478
+ originator: identitySchema,
479
+ agreement: BtpAgreementDocSchema,
480
+ statements,
481
+
482
+
483
+ keys: boundedArray(BtpEvidenceKeySchema, evidenceIdentities),
484
+
485
+ timestamps: boundedArray(
486
+ z.object({ statementSha256: sha256Hex, token: z.string().min(1) }).strict(),
487
+ agreement.signers
488
+ ).optional(),
489
+
490
+ keyChains: boundedArray(BtpEvidenceKeyChainSchema, evidenceIdentities).optional(),
491
+
492
+ arrivals: boundedArray(BtpEvidenceArrivalSchema, agreement.signers).optional()
493
+ }).strict(), BtpAuthReqDocSchema = z.object({
256
494
  identity: identitySchema,
257
495
  authToken: z.string(),
258
496
  publicKey: z.string(),
@@ -270,8 +508,10 @@ const {
270
508
  selector: z.string(),
271
509
  publicKey: z.string(),
272
510
  keyType: z.literal("rsa"),
273
- version: z.string()
274
- }), BtpAgentQueryDocSchema = z.object({
511
+ version: z.string(),
512
+ identity: identitySchema.optional(),
513
+ keyChain: BtpKeyChainSchema.optional()
514
+ }), BtpCreatedResDocSchema = z.object({ id: z.string().min(1).max(documentId) }).strict(), BtpAgentQueryDocSchema = z.object({
275
515
  title: BtpStringQueryFilterSchema.optional(),
276
516
  from: BtpStringQueryFilterSchema.optional(),
277
517
  to: BtpStringQueryFilterSchema.optional()
@@ -279,23 +519,46 @@ const {
279
519
  BtpTrustReqDocSchema,
280
520
  BtpTrustResDocSchema,
281
521
  BtpDocumentSchema,
282
- BtpLifecycleEventSchema
522
+ BtpLifecycleEventSchema,
523
+ BtpAgreementEventSchema,
524
+ BtpTrustReqDocV1_1Schema,
525
+ BtpTrustResDocV1_1Schema,
526
+ BtpDocumentV1_1Schema,
527
+ BtpLifecycleEventV1_1Schema
283
528
  ]);
284
529
  export {
285
530
  BtpAgentQueryDocSchema,
531
+ BtpAgreementDocSchema,
532
+ BtpAgreementEventSchema,
533
+ BtpAgreementStatementSchema,
286
534
  BtpAttachmentSchema,
287
535
  BtpAuthReqDocSchema,
288
536
  BtpAuthResDocSchema,
537
+ BtpCreatedResDocSchema,
538
+ BtpDetailsNoteDocSchema,
289
539
  BtpDocReferenceSchema,
290
540
  BtpDocSchema,
291
541
  BtpDocumentSchema,
542
+ BtpDocumentV1_1Schema,
543
+ BtpEventSchema,
544
+ BtpEvidenceBundleSchema,
292
545
  BtpIdentityResDocSchema,
293
546
  BtpInvoiceDocSchema,
547
+ BtpInvoiceDocV1_1Schema,
294
548
  BtpLetterDocSchema,
549
+ BtpLetterDocV1_1Schema,
295
550
  BtpLifecycleEventSchema,
551
+ BtpLifecycleEventV1_1Schema,
296
552
  BtpPayslipDocSchema,
553
+ BtpPayslipDocV1_1Schema,
297
554
  BtpReceiptDocSchema,
555
+ BtpReceiptDocV1_1Schema,
556
+ BtpStatementRecordSchema,
298
557
  BtpStringQueryFilterSchema,
299
558
  BtpTrustReqDocSchema,
300
- BtpTrustResDocSchema
559
+ BtpTrustReqDocV1_1Schema,
560
+ BtpTrustResDocSchema,
561
+ BtpTrustResDocV1_1Schema,
562
+ httpsUrl,
563
+ sha256Hex
301
564
  };