@btps/sdk 1.1.2 → 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 (112) hide show
  1. package/CHANGELOG.md +475 -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 +117 -19
  5. package/dist/client/btpsClient.d.ts +53 -7
  6. package/dist/client/btpsClient.js +266 -137
  7. package/dist/client/btpsTransporter.js +2 -1
  8. package/dist/client/index.d.ts +1 -1
  9. package/dist/client/index.js +3 -1
  10. package/dist/client/libs/schema.js +92 -15
  11. package/dist/client/libs/timeouts.d.ts +6 -0
  12. package/dist/client/libs/timeouts.js +2 -1
  13. package/dist/client/types/index.d.ts +60 -2
  14. package/dist/core/authentication/BtpsAuthentication.d.ts +43 -1
  15. package/dist/core/authentication/BtpsAuthentication.js +152 -36
  16. package/dist/core/authentication/refreshSuccessor.js +47 -0
  17. package/dist/core/authentication/storage/InMemoryTokenStore.d.ts +17 -1
  18. package/dist/core/authentication/storage/InMemoryTokenStore.js +16 -4
  19. package/dist/core/authentication/types.d.ts +82 -0
  20. package/dist/core/crypto/contentKey.d.ts +34 -0
  21. package/dist/core/crypto/contentKey.js +64 -0
  22. package/dist/core/crypto/decryptVerify.d.ts +7 -1
  23. package/dist/core/crypto/decryptVerify.js +23 -5
  24. package/dist/core/crypto/decrypter-interface.d.ts +6 -0
  25. package/dist/core/crypto/index.d.ts +28 -3
  26. package/dist/core/crypto/index.js +34 -20
  27. package/dist/core/crypto/keyResolver.d.ts +48 -0
  28. package/dist/core/crypto/keyResolver.js +46 -0
  29. package/dist/core/crypto/pem-decrypter.d.ts +2 -0
  30. package/dist/core/crypto/pem-decrypter.js +13 -8
  31. package/dist/core/crypto/signEncrypt.d.ts +4 -1
  32. package/dist/core/crypto/signEncrypt.js +20 -5
  33. package/dist/core/crypto/types.d.ts +8 -1
  34. package/dist/core/delegation/BtpsDelegator.d.ts +4 -0
  35. package/dist/core/delegation/BtpsDelegator.js +3 -2
  36. package/dist/core/delegation/verifyTransporterSignature.d.ts +104 -0
  37. package/dist/core/delegation/verifyTransporterSignature.js +224 -0
  38. package/dist/core/error/constant.d.ts +41 -0
  39. package/dist/core/error/constant.js +60 -0
  40. package/dist/core/server/constants/btps-protocol.d.ts +21 -5
  41. package/dist/core/server/constants/btps-protocol.js +46 -8
  42. package/dist/core/server/constants/index.d.ts +7 -3
  43. package/dist/core/server/constants/index.js +19 -4
  44. package/dist/core/server/constants/limits.d.ts +41 -1
  45. package/dist/core/server/constants/limits.js +45 -2
  46. package/dist/core/server/constants/permissions.d.ts +38 -0
  47. package/dist/core/server/constants/permissions.js +38 -0
  48. package/dist/core/server/schemas/artifacts/agentSchema.js +2 -1
  49. package/dist/core/server/schemas/artifacts/artifacts.js +7 -1
  50. package/dist/core/server/schemas/artifacts/controlSchema.js +1 -1
  51. package/dist/core/server/schemas/artifacts/identityLookupSchema.js +10 -1
  52. package/dist/core/server/schemas/artifacts/transporterSchema.js +92 -12
  53. package/dist/core/server/schemas/bounded.js +41 -0
  54. package/dist/core/server/schemas/btpsDocsSchema.d.ts +34522 -1421
  55. package/dist/core/server/schemas/btpsDocsSchema.js +355 -68
  56. package/dist/core/server/schemas/helpers.d.ts +9580 -0
  57. package/dist/core/server/schemas/helpers.js +76 -12
  58. package/dist/core/server/schemas/keyChainSchema.js +53 -0
  59. package/dist/core/server/schemas/responseSchema.js +88 -12
  60. package/dist/core/server/schemas/schema.js +6 -2
  61. package/dist/core/server/schemas/trustValues.d.ts +105 -0
  62. package/dist/core/server/schemas/trustValues.js +117 -0
  63. package/dist/core/server/types.d.ts +104 -6
  64. package/dist/core/storage/AbstractIdentityStore.d.ts +9 -0
  65. package/dist/core/storage/JsonIdentityStore.d.ts +3 -0
  66. package/dist/core/storage/JsonIdentityStore.js +3 -0
  67. package/dist/core/storage/types.d.ts +2 -0
  68. package/dist/core/trust/agreements.d.ts +107 -0
  69. package/dist/core/trust/agreements.js +116 -0
  70. package/dist/core/trust/evidence.d.ts +220 -0
  71. package/dist/core/trust/evidence.js +334 -0
  72. package/dist/core/trust/index.d.ts +10 -0
  73. package/dist/core/trust/index.js +14 -1
  74. package/dist/core/trust/keyRotation.d.ts +148 -0
  75. package/dist/core/trust/keyRotation.js +267 -0
  76. package/dist/core/trust/permissions.d.ts +114 -0
  77. package/dist/core/trust/permissions.js +115 -0
  78. package/dist/core/trust/shaping.d.ts +42 -0
  79. package/dist/core/trust/shaping.js +47 -0
  80. package/dist/core/trust/storage/AbstractTrustStore.d.ts +2 -1
  81. package/dist/core/trust/storage/JsonTrustStore.d.ts +3 -2
  82. package/dist/core/trust/timestamps.d.ts +49 -0
  83. package/dist/core/trust/timestamps.js +78 -0
  84. package/dist/core/trust/trustUtils.d.ts +13 -2
  85. package/dist/core/trust/trustUtils.js +28 -3
  86. package/dist/core/trust/types.d.ts +302 -2
  87. package/dist/core/trust/verification.d.ts +52 -0
  88. package/dist/core/trust/verification.js +55 -0
  89. package/dist/server/btpsServer.d.ts +72 -6
  90. package/dist/server/btpsServer.js +280 -157
  91. package/dist/server/index.d.ts +3 -0
  92. package/dist/server/index.js +17 -1
  93. package/dist/server/libs/agreementOrderCheck.d.ts +29 -0
  94. package/dist/server/libs/agreementOrderCheck.js +29 -0
  95. package/dist/server/libs/artifactTimeWindow.d.ts +2 -2
  96. package/dist/server/libs/artifactTimeWindow.js +2 -1
  97. package/dist/server/libs/detailsRequestLimit.d.ts +27 -0
  98. package/dist/server/libs/detailsRequestLimit.js +28 -0
  99. package/dist/server/libs/detailsResponseCheck.d.ts +27 -0
  100. package/dist/server/libs/detailsResponseCheck.js +27 -0
  101. package/dist/server/libs/eventChainCheck.d.ts +31 -0
  102. package/dist/server/libs/eventChainCheck.js +39 -0
  103. package/dist/server/libs/inFlightLimit.d.ts +10 -0
  104. package/dist/server/libs/inFlightLimit.js +7 -1
  105. package/dist/server/libs/index.d.ts +5 -0
  106. package/dist/server/libs/index.js +10 -0
  107. package/dist/server/libs/keyChains.d.ts +14 -0
  108. package/dist/server/libs/keyChains.js +17 -0
  109. package/dist/server/libs/wrapForAgent.d.ts +20 -0
  110. package/dist/server/libs/wrapForAgent.js +36 -0
  111. package/dist/server/types.d.ts +95 -9
  112. package/package.json +12 -3
@@ -9,7 +9,26 @@ import { z } from "zod";
9
9
  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
- const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId, referenceType } = BTPS_FIELD_LIMITS, 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({
12
+ import { boundedArray, boundedRecord } from "./bounded.js";
13
+ import { BtpKeyChainSchema } from "./keyChainSchema.js";
14
+ const {
15
+ trustRequest,
16
+ trustResponse,
17
+ event,
18
+ receipt,
19
+ payslip,
20
+ letter,
21
+ agreement,
22
+ attachments,
23
+ references,
24
+ lineItems,
25
+ query,
26
+ agentInfoValues,
27
+ recordKeys,
28
+ documentId,
29
+ referenceType,
30
+ note
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({
13
32
  content: z.string(),
14
33
 
15
34
  type: z.enum(["application/pdf", "image/jpeg", "image/png"]),
@@ -26,12 +45,15 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
26
45
  sha256: sha256Hex
27
46
  }), BtpStringQueryFilterSchema = z.object({
28
47
  like: z.string().optional(),
29
- in: z.array(z.string()).optional(),
48
+ in: boundedArray(z.string(), query.in).optional(),
30
49
  eq: z.string().optional(),
31
50
  ne: z.string().optional(),
32
- notIn: z.array(z.string()).optional(),
51
+ notIn: boundedArray(z.string(), query.notIn).optional(),
33
52
  notLike: z.string().optional()
34
- }), 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({
35
57
  id: z.string().max(trustRequest.id),
36
58
  name: z.string().max(trustRequest.name),
37
59
  email: z.string().max(trustRequest.email).email(),
@@ -44,7 +66,9 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
44
66
  message: z.string().max(trustRequest.message).optional(),
45
67
  expiresAt: z.string().datetime().optional(),
46
68
  privacyType: z.enum(["unencrypted", "encrypted", "mixed"]).optional()
47
- }), BtpTrustResDocSchema = z.object({
69
+ }), BtpTrustReqDocSchema = BtpTrustReqDocV1_1Schema.extend({
70
+ message: BtpNoteSchema(trustRequest.message).optional()
71
+ }), BtpTrustResDocV1_1Schema = z.object({
48
72
  id: z.string().max(trustResponse.id),
49
73
  decision: z.enum(["accepted", "rejected", "revoked", "blocked"]),
50
74
  decidedAt: z.string().datetime(),
@@ -53,18 +77,27 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
53
77
  retryAfterDate: z.string().datetime().optional(),
54
78
  message: z.string().max(trustResponse.message).optional(),
55
79
  privacyType: z.enum(["unencrypted", "encrypted", "mixed"]).optional()
56
- }), BtpLineItemsSchema = z.object({
57
- columns: z.array(z.string()),
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({
83
+ columns: boundedArray(z.string(), lineItems.columns),
58
84
 
59
- rows: z.array(z.record(z.union([z.string(), z.number().int().safe()])))
85
+ rows: boundedArray(
86
+ boundedRecord(z.union([z.string(), z.number().int().safe()]), lineItems.columns),
87
+ lineItems.rows
88
+ )
60
89
  }), BtpIssuerSchema = z.object({
61
90
  name: z.string(),
62
91
  email: z.string().email().optional(),
63
92
  phone: z.string().optional(),
64
93
  address: z.string().optional(),
65
94
 
66
- identifiers: z.record(z.string().regex(/^[a-z0-9_]+:[a-z0-9_]+$/), z.string()).optional()
67
- }), BtpInvoiceDocSchema = z.object({
95
+ identifiers: boundedRecord(
96
+ z.string(),
97
+ recordKeys,
98
+ z.string().regex(/^[a-z0-9_]+:[a-z0-9_]+$/)
99
+ ).optional()
100
+ }), BtpInvoiceDocV1_1Schema = z.object({
68
101
  type: z.literal("invoice"),
69
102
  title: z.string(),
70
103
  id: z.string().max(documentId),
@@ -79,18 +112,17 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
79
112
  url: z.string().url()
80
113
  }).optional(),
81
114
  description: z.string().optional(),
82
- attachments: z.array(BtpAttachmentSchema).optional(),
115
+ attachments: boundedArray(BtpAttachmentSchema, attachments).optional(),
83
116
 
84
- references: z.array(BtpDocReferenceSchema).optional(),
117
+ references: boundedArray(BtpDocReferenceSchema, references).optional(),
85
118
  template: z.object({
86
119
  name: z.string(),
87
- data: z.record(z.unknown())
120
+ data: boundedRecord(z.unknown(), recordKeys)
88
121
  }).optional(),
89
-
90
- metadata: z.record(z.unknown()).optional()
122
+ metadata: metadataSchema.optional()
91
123
  }).strict(), BtpPositiveMoneySchema = BtpMoneySchema.extend({
92
124
  amount: z.number().int().safe().positive()
93
- }), BtpReceiptDocSchema = BtpInvoiceDocSchema.omit({ dueAt: !0, paymentLink: !0 }).extend({
125
+ }), receiptFields = {
94
126
  type: z.literal("receipt"),
95
127
  lineItems: BtpLineItemsSchema.optional(),
96
128
  payment: z.object({
@@ -100,7 +132,88 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
100
132
  method: codeSchema(receipt.paymentMethod).optional(),
101
133
  reference: z.string().max(receipt.paymentReference).optional()
102
134
  })
103
- }).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({
104
217
  type: z.literal("payslip"),
105
218
  title: z.string(),
106
219
  id: z.string().max(documentId),
@@ -116,7 +229,7 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
116
229
  payPeriod: z.object({ start: z.string().datetime(), end: z.string().datetime() }).optional(),
117
230
  payDate: z.string().datetime().optional(),
118
231
  payRate: z.object({ amount: BtpMoneySchema, unit: payslipCode, asAt: z.string().datetime() }).optional(),
119
- earnings: z.array(
232
+ earnings: boundedArray(
120
233
  z.object({
121
234
  category: payslipCode,
122
235
  description: z.string().optional(),
@@ -124,18 +237,20 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
124
237
  rateUnit: payslipCode.optional(),
125
238
  quantity: decimalText.optional(),
126
239
  amount: BtpMoneySchema
127
- })
240
+ }),
241
+ payslip.earnings
128
242
  ),
129
- deductions: z.array(
243
+ deductions: boundedArray(
130
244
  z.object({
131
245
  category: payslipCode,
132
246
  description: z.string().optional(),
133
247
  amount: BtpMoneySchema,
134
248
  payee: z.object({ name: z.string(), accountRef: z.string().optional() }).optional()
135
- })
249
+ }),
250
+ payslip.deductions
136
251
  ),
137
252
 
138
- contributions: z.array(
253
+ contributions: boundedArray(
139
254
  z.object({
140
255
  category: payslipCode.optional(),
141
256
  amount: BtpMoneySchema,
@@ -144,9 +259,10 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
144
259
  memberNumber: z.string().optional(),
145
260
  accountRef: z.string().optional()
146
261
  }).optional()
147
- })
262
+ }),
263
+ payslip.contributions
148
264
  ),
149
- leave: z.array(
265
+ leave: boundedArray(
150
266
  z.object({
151
267
  category: payslipCode,
152
268
  description: z.string().optional(),
@@ -155,16 +271,17 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
155
271
  balance: decimalText,
156
272
  accrued: decimalText.optional(),
157
273
  taken: decimalText.optional()
158
- })
274
+ }),
275
+ payslip.leave
159
276
  ).optional(),
160
277
  taxWithheld: BtpMoneySchema.optional(),
161
278
  gross: BtpMoneySchema,
162
279
  net: BtpMoneySchema,
163
- yearToDate: z.record(BtpMoneySchema).optional(),
164
- attachments: z.array(BtpAttachmentSchema).optional(),
165
- references: z.array(BtpDocReferenceSchema).optional(),
166
- metadata: z.record(z.unknown()).optional()
167
- }).strict(), BtpLetterDocSchema = z.object({
280
+ yearToDate: boundedRecord(BtpMoneySchema, recordKeys).optional(),
281
+ attachments: boundedArray(BtpAttachmentSchema, attachments).optional(),
282
+ references: boundedArray(BtpDocReferenceSchema, references).optional(),
283
+ metadata: metadataSchema.optional()
284
+ }).strict(), BtpLetterDocV1_1Schema = z.object({
168
285
  type: z.literal("letter"),
169
286
  title: z.string().max(letter.title),
170
287
  id: z.string().max(documentId),
@@ -174,15 +291,47 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
174
291
  content: z.string().max(letter.content)
175
292
  }),
176
293
  issuer: BtpIssuerSchema.optional(),
177
- attachments: z.array(BtpAttachmentSchema).max(letter.attachments).optional(),
178
- references: z.array(BtpDocReferenceSchema).max(letter.references).optional(),
179
- metadata: z.record(z.unknown()).optional()
294
+ attachments: boundedArray(BtpAttachmentSchema, letter.attachments).optional(),
295
+ references: boundedArray(BtpDocReferenceSchema, letter.references).optional(),
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()
180
320
  }).strict(), BtpDocumentSchema = z.discriminatedUnion("type", [
181
321
  BtpInvoiceDocSchema,
182
322
  BtpReceiptDocSchema,
183
323
  BtpPayslipDocSchema,
184
- BtpLetterDocSchema
185
- ]), 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 = {
186
335
  type: z.literal("lifecycle_event"),
187
336
  id: z.string().uuid(),
188
337
 
@@ -193,44 +342,157 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
193
342
  message: z.string().max(event.message).optional()
194
343
  }, BtpEventAmountSchema = BtpMoneySchema.extend({
195
344
  amount: z.number().int().safe().positive()
196
- }), BtpLifecycleEventSchema = z.discriminatedUnion("eventType", [
197
- z.object({ ...lifecycleEventFields, eventType: z.literal("paid") }).strict(),
198
- z.object({
199
- ...lifecycleEventFields,
200
- eventType: z.literal("partially_paid"),
201
- amount: BtpEventAmountSchema
202
- }).strict(),
203
- z.object({
204
- ...lifecycleEventFields,
205
- eventType: z.literal("disputed"),
206
-
207
- reason: codeSchema(event.reason)
208
- }).strict(),
209
- 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),
210
385
  z.object({
211
386
  ...lifecycleEventFields,
212
- eventType: z.literal("refund"),
213
- amount: BtpEventAmountSchema,
387
+ ...message,
388
+ eventType: z.literal("withdrawn"),
214
389
  inReplyTo: z.string().uuid()
215
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
216
432
  }).strict(),
217
433
  z.object({
218
- ...lifecycleEventFields,
219
- eventType: z.literal("response"),
220
- decision: z.enum(["accepted", "rejected"]),
221
- inReplyTo: z.string().uuid(),
222
-
223
-
224
- remainingDueDate: z.string().datetime().optional()
225
- }).strict()
226
- ]).refine(
227
- (event2) => event2.eventType !== "response" || event2.remainingDueDate === void 0 || event2.decision === "accepted",
228
- { message: "remainingDueDate is only on an accepted response", path: ["remainingDueDate"] }
229
- ), 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({
230
489
  identity: identitySchema,
231
490
  authToken: z.string(),
232
491
  publicKey: z.string(),
233
- agentInfo: z.record(z.union([z.string(), z.array(z.string())])).optional(),
492
+ agentInfo: boundedRecord(
493
+ z.union([z.string(), boundedArray(z.string(), agentInfoValues)]),
494
+ recordKeys
495
+ ).optional(),
234
496
  keyProof: BtpSignatureSchema.optional()
235
497
  }), BtpAuthResDocSchema = z.object({
236
498
  agentId: z.string(),
@@ -241,8 +503,10 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
241
503
  selector: z.string(),
242
504
  publicKey: z.string(),
243
505
  keyType: z.literal("rsa"),
244
- version: z.string()
245
- }), 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({
246
510
  title: BtpStringQueryFilterSchema.optional(),
247
511
  from: BtpStringQueryFilterSchema.optional(),
248
512
  to: BtpStringQueryFilterSchema.optional()
@@ -250,23 +514,46 @@ const { trustRequest, trustResponse, event, receipt, payslip, letter, documentId
250
514
  BtpTrustReqDocSchema,
251
515
  BtpTrustResDocSchema,
252
516
  BtpDocumentSchema,
253
- BtpLifecycleEventSchema
517
+ BtpLifecycleEventSchema,
518
+ BtpAgreementEventSchema,
519
+ BtpTrustReqDocV1_1Schema,
520
+ BtpTrustResDocV1_1Schema,
521
+ BtpDocumentV1_1Schema,
522
+ BtpLifecycleEventV1_1Schema
254
523
  ]);
255
524
  export {
256
525
  BtpAgentQueryDocSchema,
526
+ BtpAgreementDocSchema,
527
+ BtpAgreementEventSchema,
528
+ BtpAgreementStatementSchema,
257
529
  BtpAttachmentSchema,
258
530
  BtpAuthReqDocSchema,
259
531
  BtpAuthResDocSchema,
532
+ BtpCreatedResDocSchema,
533
+ BtpDetailsNoteDocSchema,
260
534
  BtpDocReferenceSchema,
261
535
  BtpDocSchema,
262
536
  BtpDocumentSchema,
537
+ BtpDocumentV1_1Schema,
538
+ BtpEventSchema,
539
+ BtpEvidenceBundleSchema,
263
540
  BtpIdentityResDocSchema,
264
541
  BtpInvoiceDocSchema,
542
+ BtpInvoiceDocV1_1Schema,
265
543
  BtpLetterDocSchema,
544
+ BtpLetterDocV1_1Schema,
266
545
  BtpLifecycleEventSchema,
546
+ BtpLifecycleEventV1_1Schema,
267
547
  BtpPayslipDocSchema,
548
+ BtpPayslipDocV1_1Schema,
268
549
  BtpReceiptDocSchema,
550
+ BtpReceiptDocV1_1Schema,
551
+ BtpStatementRecordSchema,
269
552
  BtpStringQueryFilterSchema,
270
553
  BtpTrustReqDocSchema,
271
- BtpTrustResDocSchema
554
+ BtpTrustReqDocV1_1Schema,
555
+ BtpTrustResDocSchema,
556
+ BtpTrustResDocV1_1Schema,
557
+ httpsUrl,
558
+ sha256Hex
272
559
  };