@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
@@ -7,11 +7,38 @@
7
7
 
8
8
  import { BtpSignatureSchema, BtpEncryptionSchema, BtpDelegationSchema } from "../schema.js";
9
9
  import { identitySchema } from "../shared.js";
10
- import { TRANSPORTER_ACTIONS } from "../../constants/index.js";
10
+ import {
11
+ DETAILS_ARTIFACT_TYPES,
12
+ DETAIL_PERMISSIONS,
13
+ DOCUMENT_TYPES,
14
+ ENTITY_TYPES,
15
+ IDENTITY_DETAIL,
16
+ PERMISSION_ENVELOPE_FIELDS,
17
+ SEND_PERMISSIONS,
18
+ TRANSPORTER_ACTIONS,
19
+ isVersionBelow
20
+ } from "../../constants/index.js";
11
21
  import { z } from "zod";
12
- import { lifecycleEventEnvelopeProblem, processBtpDocSchema } from "../helpers.js";
13
- import { BtpLifecycleEventSchema } from "../btpsDocsSchema.js";
14
- const BtpTransporterArtifactBaseSchema = z.object({
22
+ import {
23
+ agreementEventEnvelopeProblem,
24
+ lifecycleEventEnvelopeProblem,
25
+ processBtpDocSchema,
26
+ sameIdentity
27
+ } from "../helpers.js";
28
+ import { BtpAgreementEventSchema, BtpLifecycleEventSchema, sha256Hex } from "../btpsDocsSchema.js";
29
+ import { boundedArray } from "../bounded.js";
30
+ const BtpSyncOfSchema = z.object({ id: z.string().min(1), sha256: sha256Hex }).strict(), memberOf = (names) => z.custom((name) => typeof name == "string" && names.includes(name)), namesFrom = (names, min = 0) => boundedArray(memberOf(names), names.length, min), PERMISSION_NAMES = [...SEND_PERMISSIONS, ...DETAIL_PERMISSIONS], BtpTrustPermissionsSchema = boundedArray(
31
+ z.object({ name: memberOf(PERMISSION_NAMES), required: z.boolean() }).strict(),
32
+ PERMISSION_NAMES.length
33
+ ).refine((permissions) => permissions.some(({ name }) => name.startsWith("send:")), {
34
+ message: "permissions holds at least one send: name"
35
+ }), BtpTrustRecordPermissionsSchema = z.object({
36
+ requested: BtpTrustPermissionsSchema,
37
+ granted: namesFrom(PERMISSION_NAMES),
38
+ detailsHeld: namesFrom(DETAIL_PERMISSIONS),
39
+ detailsShared: namesFrom(DETAIL_PERMISSIONS),
40
+ pendingChange: z.object({ requested: BtpTrustPermissionsSchema, requestedAt: z.string().datetime() }).strict().optional()
41
+ }).strict(), duplicateOf = (names) => names.find((name, i) => names.indexOf(name) !== i), documentRecord = (document) => typeof document == "object" && document !== null ? document : void 0, BtpTransporterArtifactBaseSchema = z.object({
15
42
  version: z.string(),
16
43
  issuedAt: z.string().datetime(),
17
44
  id: z.string(),
@@ -23,21 +50,71 @@ const BtpTransporterArtifactBaseSchema = z.object({
23
50
  document: z.unknown(),
24
51
  delegation: BtpDelegationSchema.optional(),
25
52
  selector: z.string(),
26
- relatesTo: z.string().min(1).optional()
27
- }), BtpTransporterArtifactSchema = BtpTransporterArtifactBaseSchema.superRefine(
53
+ relatesTo: z.string().min(1).optional(),
54
+ syncOf: BtpSyncOfSchema.optional(),
55
+ permissions: BtpTrustPermissionsSchema.optional(),
56
+ entityType: z.enum(ENTITY_TYPES).optional(),
57
+ given: namesFrom(DETAIL_PERMISSIONS).optional(),
58
+ change: z.literal(!0).optional(),
59
+ granted: namesFrom(PERMISSION_NAMES).optional(),
60
+ documentType: z.enum(DOCUMENT_TYPES).optional(),
61
+ details: namesFrom(DETAIL_PERMISSIONS, 1).optional()
62
+ });
63
+ function permissionEnvelopeProblems(data) {
64
+ const problems = [];
65
+ for (const [field, types] of Object.entries(PERMISSION_ENVELOPE_FIELDS))
66
+ data[field] !== void 0 && (types.includes(data.type) || problems.push([field, `${field} is only on ${types.join(" or ")}`]), isVersionBelow(data.version, "1.2.0") && problems.push([field, `${field} is only in a 1.2.0 artifact`]));
67
+ const lists = [
68
+ ["permissions", data.permissions?.map((p) => p.name)],
69
+ ["given", data.given],
70
+ ["granted", data.granted],
71
+ ["details", data.details]
72
+ ];
73
+ for (const [field, names] of lists) {
74
+ const twice = names && duplicateOf(names);
75
+ twice && problems.push([field, `${twice} appears more than once`]);
76
+ }
77
+ const { entityType } = data, other = entityType && IDENTITY_DETAIL[entityType === "person" ? "organisation" : "person"];
78
+ if (data.permissions && !entityType && problems.push(["entityType", "entityType comes with permissions"]), data.granted && !entityType && problems.push(["entityType", "entityType comes with granted"]), data.permissions && entityType) {
79
+ const given = data.given ?? [], identities = Object.values(IDENTITY_DETAIL);
80
+ for (const { name } of data.permissions) {
81
+ if (!name.startsWith("detail:")) continue;
82
+ const gives = identities.includes(name) ? IDENTITY_DETAIL[entityType] : name;
83
+ given.includes(gives) || problems.push(["given", `asks for ${name} without giving ${gives}`]);
84
+ }
85
+ }
86
+ if (other && data.given?.includes(other) && problems.push(["given", `a ${entityType} never gives ${other}`]), other && data.granted?.includes(other) && problems.push(["granted", `a ${entityType} never grants ${other}`]), DETAILS_ARTIFACT_TYPES.includes(data.type) && data.details === void 0 && problems.push(["details", `a ${data.type} names the details it is about`]), data.type === "DETAILS_RES" && data.encryption === null && problems.push(["encryption", "a DETAILS_RES is always encrypted to the requester"]), data.encryption === null) {
87
+ const document = documentRecord(data.document);
88
+ data.type === "TRUST_REQ" && document?.own !== void 0 && problems.push(["document", "a request with its own values is encrypted"]), data.type === "TRUST_REQ" && document?.verification !== void 0 && problems.push(["document", "a request with a verification is encrypted"]), data.type === "TRUST_RES" && document?.details !== void 0 && problems.push(["document", "a response with details is encrypted"]), data.type === "TRUST_RES" && data.granted && document?.decision !== "accepted" && problems.push(["granted", "only an accepted answer carries granted"]);
89
+ }
90
+ return problems;
91
+ }
92
+ const BtpTransporterArtifactSchema = BtpTransporterArtifactBaseSchema.superRefine(
28
93
  (data, ctx) => {
29
- if (data.type === "BTPS_EVENT" != (data.relatesTo !== void 0) && ctx.addIssue({
94
+ const needsRelatesTo = data.type === "BTPS_EVENT" || data.type === "DETAILS_RES", mayRelate = needsRelatesTo || data.type === "DETAILS_REQ";
95
+ (needsRelatesTo ? data.relatesTo === void 0 : !mayRelate && data.relatesTo !== void 0) && ctx.addIssue({
30
96
  code: z.ZodIssueCode.custom,
31
- message: "relatesTo is required on a BTPS_EVENT and only there",
97
+ message: "relatesTo is required on a BTPS_EVENT and a DETAILS_RES, may be on a DETAILS_REQ, and nowhere else",
32
98
  path: ["relatesTo"]
33
- }), data.encryption)
99
+ }), data.syncOf !== void 0 && (sameIdentity(data.from, data.to) || ctx.addIssue({
100
+ code: z.ZodIssueCode.custom,
101
+ message: "syncOf is only on a sync, to the sender's own address",
102
+ path: ["syncOf"]
103
+ }), isVersionBelow(data.version, "1.2.0") && ctx.addIssue({
104
+ code: z.ZodIssueCode.custom,
105
+ message: "syncOf is only in a 1.2.0 artifact",
106
+ path: ["syncOf"]
107
+ }));
108
+ for (const [path, message] of permissionEnvelopeProblems(data))
109
+ ctx.addIssue({ code: z.ZodIssueCode.custom, message, path: [path] });
110
+ if (data.encryption)
34
111
  typeof data.document != "string" && ctx.addIssue({
35
112
  code: z.ZodIssueCode.custom,
36
113
  message: "When encrypted, document must be a string",
37
114
  path: ["document"]
38
115
  });
39
116
  else {
40
- const schema = processBtpDocSchema(data.type);
117
+ const schema = processBtpDocSchema(data.type, data.version);
41
118
  if (!schema) return !1;
42
119
  if (!schema.safeParse(data.document).success) {
43
120
  ctx.addIssue({
@@ -48,13 +125,16 @@ const BtpTransporterArtifactBaseSchema = z.object({
48
125
  return;
49
126
  }
50
127
  if (data.type === "BTPS_EVENT") {
51
- const event = BtpLifecycleEventSchema.safeParse(data.document), problem = event.success ? lifecycleEventEnvelopeProblem(event.data, data) : void 0;
128
+ const event = BtpLifecycleEventSchema.safeParse(data.document), answer = BtpAgreementEventSchema.safeParse(data.document), problem = event.success ? lifecycleEventEnvelopeProblem(event.data, data) : answer.success ? agreementEventEnvelopeProblem(answer.data, data) : void 0;
52
129
  problem && ctx.addIssue({ code: z.ZodIssueCode.custom, message: problem, path: ["document"] });
53
130
  }
54
131
  }
55
132
  }
56
133
  );
57
134
  export {
135
+ BtpSyncOfSchema,
58
136
  BtpTransporterArtifactBaseSchema,
59
- BtpTransporterArtifactSchema
137
+ BtpTransporterArtifactSchema,
138
+ BtpTrustPermissionsSchema,
139
+ BtpTrustRecordPermissionsSchema
60
140
  };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2025 Bhupendra Tamang
4
+ * Licensed under the Apache License, Version 2.0
5
+ * https://www.apache.org/licenses/LICENSE-2.0
6
+ */
7
+
8
+ import { z } from "zod";
9
+ const PRECHECK_THRESHOLD = 100, tooMany = (ctx, max, unit) => ctx.addIssue({
10
+ code: z.ZodIssueCode.custom,
11
+ message: `must have at most ${max} ${unit}`,
12
+ params: { maximum: max },
13
+ fatal: !0
14
+ }), boundedArray = (element, max, min = 0) => z.preprocess((value, ctx) => {
15
+ if (!Array.isArray(value)) return value;
16
+ if (value.length > max)
17
+ return tooMany(ctx, max, "entries"), value;
18
+ if (value.length <= PRECHECK_THRESHOLD) return value;
19
+ for (let i = 0; i < value.length; i++) {
20
+ const result = element.safeParse(value[i]);
21
+ if (!result.success) {
22
+ const [issue] = result.error.issues;
23
+ ctx.addIssue({
24
+ code: z.ZodIssueCode.custom,
25
+ path: [i, ...issue?.path ?? []],
26
+ message: issue?.message ?? "Invalid",
27
+ fatal: !0
28
+ });
29
+ break;
30
+ }
31
+ }
32
+ return value;
33
+ }, z.array(element).min(min)), boundedRecord = (valueSchema, max, keySchema = z.string()) => z.preprocess(
34
+ (value, ctx) => (typeof value != "object" || value === null || Array.isArray(value) || Object.keys(value).length > max && tooMany(ctx, max, "keys"), value),
35
+ z.record(keySchema, valueSchema)
36
+ );
37
+ export {
38
+ PRECHECK_THRESHOLD,
39
+ boundedArray,
40
+ boundedRecord
41
+ };