@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
@@ -8,9 +8,10 @@ export * from './btps-protocol.js';
8
8
  export * from './control.js';
9
9
  export * from './currency.js';
10
10
  export * from './limits.js';
11
+ export * from './permissions.js';
11
12
  export declare const AUTH_ACTIONS: readonly ["auth.request", "auth.refresh"];
12
- export declare const AGENT_ACTIONS: readonly ["trust.request", "trust.respond", "trust.update", "trust.delete", "trust.fetch", "inbox.fetch", "inbox.delete", "inbox.seen", "outbox.fetch", "outbox.cancel", "sentbox.fetch", "sentbox.delete", "draft.fetch", "draft.create", "draft.update", "draft.delete", "system.ping", "auth.request", "auth.refresh", "artifact.send", "event.send", "trash.fetch", "trash.delete"];
13
- export declare const TRANSPORTER_ACTIONS: readonly ["TRUST_REQ", "TRUST_RES", "BTPS_DOC", "BTPS_EVENT"];
13
+ export declare const AGENT_ACTIONS: readonly ["trust.request", "trust.respond", "trust.update", "trust.delete", "trust.fetch", "inbox.fetch", "inbox.delete", "inbox.seen", "outbox.fetch", "outbox.cancel", "sentbox.fetch", "sentbox.delete", "draft.fetch", "draft.create", "draft.update", "draft.delete", "system.ping", "auth.request", "auth.refresh", "artifact.send", "event.send", "trash.fetch", "trash.delete", "details.changed", "details.request", "details.respond"];
14
+ export declare const TRANSPORTER_ACTIONS: readonly ["TRUST_REQ", "TRUST_RES", "BTPS_DOC", "BTPS_EVENT", "DETAILS_CHANGED", "DETAILS_REQ", "DETAILS_RES"];
14
15
  /**
15
16
  * Agent actions that carry a transporter artifact, built by the device, and its
16
17
  * type. trust.update and trust.delete are not among them: they edit the user's
@@ -21,6 +22,9 @@ export declare const AGENT_TRANSPORTER_ACTIONS: {
21
22
  readonly 'trust.respond': "TRUST_RES";
22
23
  readonly 'artifact.send': "BTPS_DOC";
23
24
  readonly 'event.send': "BTPS_EVENT";
25
+ readonly 'details.changed': "DETAILS_CHANGED";
26
+ readonly 'details.request': "DETAILS_REQ";
27
+ readonly 'details.respond': "DETAILS_RES";
24
28
  };
25
29
  export declare const IMMEDIATE_ACTIONS: readonly ["system.ping", "auth.request", "auth.refresh", "inbox.fetch", "inbox.delete", "inbox.seen", "outbox.fetch", "outbox.cancel", "draft.fetch", "draft.create", "draft.update", "draft.delete", "trust.update", "trust.delete", "trust.fetch", "sentbox.fetch", "sentbox.delete", "trash.fetch", "trash.delete"];
26
- export declare const AGENT_ACTIONS_REQUIRING_DOCUMENT: readonly ["trust.request", "trust.respond", "trust.update", "trust.delete", "artifact.send", "event.send", "auth.request", "auth.refresh", "inbox.seen", "inbox.delete", "sentbox.delete", "outbox.cancel", "draft.create", "draft.update", "draft.delete", "trash.delete"];
30
+ export declare const AGENT_ACTIONS_REQUIRING_DOCUMENT: readonly ["trust.request", "trust.respond", "trust.update", "trust.delete", "artifact.send", "event.send", "details.changed", "details.request", "details.respond", "auth.request", "auth.refresh", "inbox.seen", "inbox.delete", "sentbox.delete", "outbox.cancel", "draft.create", "draft.update", "draft.delete", "trash.delete"];
@@ -9,7 +9,8 @@ export * from "./btps-protocol.js";
9
9
  export * from "./control.js";
10
10
  export * from "./currency.js";
11
11
  export * from "./limits.js";
12
- const TRUST_ACTIONS = ["trust.request", "trust.respond"], TRUST_IMMEDIATE_ACTIONS = ["trust.update", "trust.delete", "trust.fetch"], INBOX_ACTIONS = ["inbox.fetch", "inbox.delete", "inbox.seen"], OUTBOX_ACTIONS = ["outbox.fetch", "outbox.cancel"], SENTBOX_ACTIONS = ["sentbox.fetch", "sentbox.delete"], DRAFT_ACTIONS = ["draft.fetch", "draft.create", "draft.update", "draft.delete"], TRASH_ACTIONS = ["trash.fetch", "trash.delete"], SYSTEM_ACTIONS = ["system.ping"], AUTH_ACTIONS = ["auth.request", "auth.refresh"], ARTIFACT_ACTIONS = ["artifact.send"], EVENT_ACTIONS = ["event.send"], AGENT_ACTIONS = [
12
+ export * from "./permissions.js";
13
+ const TRUST_ACTIONS = ["trust.request", "trust.respond"], TRUST_IMMEDIATE_ACTIONS = ["trust.update", "trust.delete", "trust.fetch"], INBOX_ACTIONS = ["inbox.fetch", "inbox.delete", "inbox.seen"], OUTBOX_ACTIONS = ["outbox.fetch", "outbox.cancel"], SENTBOX_ACTIONS = ["sentbox.fetch", "sentbox.delete"], DRAFT_ACTIONS = ["draft.fetch", "draft.create", "draft.update", "draft.delete"], TRASH_ACTIONS = ["trash.fetch", "trash.delete"], SYSTEM_ACTIONS = ["system.ping"], AUTH_ACTIONS = ["auth.request", "auth.refresh"], ARTIFACT_ACTIONS = ["artifact.send"], EVENT_ACTIONS = ["event.send"], DETAILS_ACTIONS = ["details.changed", "details.request", "details.respond"], AGENT_ACTIONS = [
13
14
  ...TRUST_ACTIONS,
14
15
  ...TRUST_IMMEDIATE_ACTIONS,
15
16
  ...INBOX_ACTIONS,
@@ -20,12 +21,25 @@ const TRUST_ACTIONS = ["trust.request", "trust.respond"], TRUST_IMMEDIATE_ACTION
20
21
  ...AUTH_ACTIONS,
21
22
  ...ARTIFACT_ACTIONS,
22
23
  ...EVENT_ACTIONS,
23
- ...TRASH_ACTIONS
24
- ], TRANSPORTER_ACTIONS = ["TRUST_REQ", "TRUST_RES", "BTPS_DOC", "BTPS_EVENT"], AGENT_TRANSPORTER_ACTIONS = {
24
+ ...TRASH_ACTIONS,
25
+ ...DETAILS_ACTIONS
26
+ ], TRANSPORTER_ACTIONS = [
27
+ "TRUST_REQ",
28
+ "TRUST_RES",
29
+ "BTPS_DOC",
30
+ "BTPS_EVENT",
31
+
32
+ "DETAILS_CHANGED",
33
+ "DETAILS_REQ",
34
+ "DETAILS_RES"
35
+ ], AGENT_TRANSPORTER_ACTIONS = {
25
36
  "trust.request": "TRUST_REQ",
26
37
  "trust.respond": "TRUST_RES",
27
38
  "artifact.send": "BTPS_DOC",
28
- "event.send": "BTPS_EVENT"
39
+ "event.send": "BTPS_EVENT",
40
+ "details.changed": "DETAILS_CHANGED",
41
+ "details.request": "DETAILS_REQ",
42
+ "details.respond": "DETAILS_RES"
29
43
  }, IMMEDIATE_ACTIONS = [
30
44
  ...SYSTEM_ACTIONS,
31
45
  ...AUTH_ACTIONS,
@@ -41,6 +55,7 @@ const TRUST_ACTIONS = ["trust.request", "trust.respond"], TRUST_IMMEDIATE_ACTION
41
55
  "trust.delete",
42
56
  ...ARTIFACT_ACTIONS,
43
57
  ...EVENT_ACTIONS,
58
+ ...DETAILS_ACTIONS,
44
59
  ...AUTH_ACTIONS,
45
60
  "inbox.seen",
46
61
  "inbox.delete",
@@ -50,6 +50,13 @@ export declare const BTPS_FIELD_LIMITS: {
50
50
  readonly attachments: 10;
51
51
  readonly references: 20;
52
52
  };
53
+ readonly agreement: {
54
+ readonly signers: 20;
55
+ };
56
+ readonly keyChain: {
57
+ readonly records: 100;
58
+ readonly publicKey: 2048;
59
+ };
53
60
  readonly attachments: 10;
54
61
  readonly references: 20;
55
62
  readonly lineItems: {
@@ -69,4 +76,15 @@ export declare const BTPS_FIELD_LIMITS: {
69
76
  readonly recordKeys: 100;
70
77
  readonly documentId: 100;
71
78
  readonly referenceType: 64;
79
+ readonly note: 5000;
80
+ readonly url: 2048;
81
+ readonly signature: {
82
+ readonly text: 100;
83
+ readonly strokes: 32;
84
+ readonly strokePoints: 512;
85
+ readonly points: 2048;
86
+ readonly width: 1000;
87
+ readonly height: 300;
88
+ };
89
+ readonly detailNumber: 64;
72
90
  };
@@ -46,6 +46,15 @@ const BTPS_FIELD_LIMITS = {
46
46
  references: 20
47
47
  },
48
48
 
49
+ agreement: {
50
+ signers: 20
51
+ },
52
+
53
+ keyChain: {
54
+ records: 100,
55
+ publicKey: 2048
56
+ },
57
+
49
58
  attachments: 10,
50
59
  references: 20,
51
60
 
@@ -68,7 +77,16 @@ const BTPS_FIELD_LIMITS = {
68
77
  recordKeys: 100,
69
78
 
70
79
  documentId: 100,
71
- referenceType: 64
80
+ referenceType: 64,
81
+
82
+ note: 5e3,
83
+
84
+ url: 2048,
85
+
86
+
87
+ signature: { text: 100, strokes: 32, strokePoints: 512, points: 2048, width: 1e3, height: 300 },
88
+
89
+ detailNumber: 64
72
90
  };
73
91
  export {
74
92
  BTPS_FIELD_LIMITS
@@ -0,0 +1,38 @@
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
+ * Since 1.2: the document types a `BTPS_DOC` carries. Each names a `send:`
9
+ * permission and a `documentType`, so a new type is added here, with its schema.
10
+ */
11
+ export declare const DOCUMENT_TYPES: readonly ["invoice", "receipt", "payslip", "letter", "agreement"];
12
+ /** Since 1.2: the details a trust may ask for, each a `detail:` permission. */
13
+ export declare const DETAIL_TYPES: readonly ["legalName", "profile", "email", "phone", "address", "taxNo", "dob", "registrationNumber"];
14
+ /** Since 1.2: what owns a BTPS address. */
15
+ export declare const ENTITY_TYPES: readonly ["person", "organisation"];
16
+ export declare const SEND_PERMISSIONS: ("send:payslip" | "send:letter" | "send:agreement" | "send:invoice" | "send:receipt")[];
17
+ export declare const DETAIL_PERMISSIONS: ("detail:legalName" | "detail:profile" | "detail:email" | "detail:phone" | "detail:address" | "detail:taxNo" | "detail:dob" | "detail:registrationNumber")[];
18
+ /** Since 1.2: details refresh: a notice that details changed, a request for some, and its answer. */
19
+ export declare const DETAILS_ARTIFACT_TYPES: readonly ["DETAILS_CHANGED", "DETAILS_REQ", "DETAILS_RES"];
20
+ /** Since 1.2: the envelope fields of trust permissions, and the artifact types each is on. */
21
+ export declare const PERMISSION_ENVELOPE_FIELDS: {
22
+ readonly permissions: readonly ["TRUST_REQ"];
23
+ readonly entityType: readonly ["TRUST_REQ", "TRUST_RES"];
24
+ readonly given: readonly ["TRUST_REQ"];
25
+ readonly change: readonly ["TRUST_REQ"];
26
+ readonly granted: readonly ["TRUST_RES"];
27
+ readonly documentType: readonly ["BTPS_DOC"];
28
+ readonly details: readonly ["DETAILS_CHANGED", "DETAILS_REQ", "DETAILS_RES"];
29
+ };
30
+ /**
31
+ * The one detail each kind gives that the other never does: a person its date
32
+ * of birth, an organisation its registration number. Asking for either is met
33
+ * by giving your own.
34
+ */
35
+ export declare const IDENTITY_DETAIL: {
36
+ readonly person: "detail:dob";
37
+ readonly organisation: "detail:registrationNumber";
38
+ };
@@ -0,0 +1,38 @@
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
+ const DOCUMENT_TYPES = ["invoice", "receipt", "payslip", "letter", "agreement"], DETAIL_TYPES = [
9
+ "legalName",
10
+ "profile",
11
+ "email",
12
+ "phone",
13
+ "address",
14
+ "taxNo",
15
+ "dob",
16
+ "registrationNumber"
17
+ ], ENTITY_TYPES = ["person", "organisation"], SEND_PERMISSIONS = DOCUMENT_TYPES.map((type) => `send:${type}`), DETAIL_PERMISSIONS = DETAIL_TYPES.map((type) => `detail:${type}`), DETAILS_ARTIFACT_TYPES = ["DETAILS_CHANGED", "DETAILS_REQ", "DETAILS_RES"], PERMISSION_ENVELOPE_FIELDS = {
18
+ permissions: ["TRUST_REQ"],
19
+ entityType: ["TRUST_REQ", "TRUST_RES"],
20
+ given: ["TRUST_REQ"],
21
+ change: ["TRUST_REQ"],
22
+ granted: ["TRUST_RES"],
23
+ documentType: ["BTPS_DOC"],
24
+ details: DETAILS_ARTIFACT_TYPES
25
+ }, IDENTITY_DETAIL = {
26
+ person: "detail:dob",
27
+ organisation: "detail:registrationNumber"
28
+ };
29
+ export {
30
+ DETAILS_ARTIFACT_TYPES,
31
+ DETAIL_PERMISSIONS,
32
+ DETAIL_TYPES,
33
+ DOCUMENT_TYPES,
34
+ ENTITY_TYPES,
35
+ IDENTITY_DETAIL,
36
+ PERMISSION_ENVELOPE_FIELDS,
37
+ SEND_PERMISSIONS
38
+ };
@@ -44,7 +44,8 @@ const BtpAgentArtifactSchema = z.object({
44
44
  if (!(action in AGENT_TRANSPORTER_ACTIONS))
45
45
  return validateAgentDocument(action, encryption, document);
46
46
  const carried = BtpTransporterArtifactSchema.safeParse(document);
47
- return carried.success && carried.data.type === AGENT_TRANSPORTER_ACTIONS[action];
47
+ return carried.success && carried.data.type === AGENT_TRANSPORTER_ACTIONS[action] &&
48
+ (carried.data.syncOf === void 0 || action === "artifact.send");
48
49
  };
49
50
  export {
50
51
  BtpAgentArtifactSchema
@@ -10,12 +10,18 @@ import { BtpAgentArtifactSchema } from "./agentSchema.js";
10
10
  import { BtpTransporterArtifactSchema } from "./transporterSchema.js";
11
11
  import { BtpControlArtifactSchema } from "./controlSchema.js";
12
12
  import { BtpIdentityLookupRequestSchema } from "./identityLookupSchema.js";
13
- const BtpArtifactServerSchema = z.union([
13
+ const BtpArtifactSchemaByType = {
14
+ agent: BtpAgentArtifactSchema,
15
+ transporter: BtpTransporterArtifactSchema,
16
+ control: BtpControlArtifactSchema,
17
+ identityLookup: BtpIdentityLookupRequestSchema
18
+ }, BtpArtifactServerSchema = z.union([
14
19
  BtpAgentArtifactSchema,
15
20
  BtpTransporterArtifactSchema,
16
21
  BtpControlArtifactSchema,
17
22
  BtpIdentityLookupRequestSchema
18
23
  ]);
19
24
  export {
25
+ BtpArtifactSchemaByType,
20
26
  BtpArtifactServerSchema
21
27
  };
@@ -12,7 +12,7 @@ const BtpControlArtifactSchema = z.object({
12
12
  id: z.string(),
13
13
  issuedAt: z.string().datetime(),
14
14
  action: z.enum(CONTROL_ACTIONS)
15
- });
15
+ }).strict();
16
16
  export {
17
17
  BtpControlArtifactSchema
18
18
  };
@@ -7,6 +7,7 @@
7
7
 
8
8
  import { z } from "zod";
9
9
  import { identitySchema } from "../shared.js";
10
+ import { isVersionBelow } from "../../constants/index.js";
10
11
  const BtpIdentityLookupRequestSchema = z.object({
11
12
  version: z.string(),
12
13
  id: z.string(),
@@ -14,7 +15,15 @@ const BtpIdentityLookupRequestSchema = z.object({
14
15
  identity: identitySchema,
15
16
  from: identitySchema,
16
17
  hostSelector: z.string(),
17
- identitySelector: z.string().optional()
18
+ identitySelector: z.string().optional(),
19
+
20
+ keyChain: z.literal(!0).optional()
21
+ }).strict().superRefine((data, ctx) => {
22
+ data.keyChain !== void 0 && isVersionBelow(data.version, "1.2.0") && ctx.addIssue({
23
+ path: ["keyChain"],
24
+ code: z.ZodIssueCode.custom,
25
+ message: "keyChain is only in a 1.2.0 lookup"
26
+ });
18
27
  });
19
28
  export {
20
29
  BtpIdentityLookupRequestSchema
@@ -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
  };
@@ -11,7 +11,7 @@ const PRECHECK_THRESHOLD = 100, tooMany = (ctx, max, unit) => ctx.addIssue({
11
11
  message: `must have at most ${max} ${unit}`,
12
12
  params: { maximum: max },
13
13
  fatal: !0
14
- }), boundedArray = (element, max) => z.preprocess((value, ctx) => {
14
+ }), boundedArray = (element, max, min = 0) => z.preprocess((value, ctx) => {
15
15
  if (!Array.isArray(value)) return value;
16
16
  if (value.length > max)
17
17
  return tooMany(ctx, max, "entries"), value;
@@ -30,7 +30,7 @@ const PRECHECK_THRESHOLD = 100, tooMany = (ctx, max, unit) => ctx.addIssue({
30
30
  }
31
31
  }
32
32
  return value;
33
- }, z.array(element)), boundedRecord = (valueSchema, max, keySchema = z.string()) => z.preprocess(
33
+ }, z.array(element).min(min)), boundedRecord = (valueSchema, max, keySchema = z.string()) => z.preprocess(
34
34
  (value, ctx) => (typeof value != "object" || value === null || Array.isArray(value) || Object.keys(value).length > max && tooMany(ctx, max, "keys"), value),
35
35
  z.record(keySchema, valueSchema)
36
36
  );