@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.
- package/CHANGELOG.md +461 -0
- package/bin/verify-evidence-bundle.mjs +96 -0
- package/dist/client/btpsAgent.d.ts +10 -1
- package/dist/client/btpsAgent.js +116 -18
- package/dist/client/btpsClient.d.ts +53 -7
- package/dist/client/btpsClient.js +265 -147
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js +3 -1
- package/dist/client/libs/schema.js +78 -13
- package/dist/client/libs/timeouts.d.ts +6 -0
- package/dist/client/libs/timeouts.js +2 -1
- package/dist/client/types/index.d.ts +54 -2
- package/dist/core/authentication/BtpsAuthentication.d.ts +43 -1
- package/dist/core/authentication/BtpsAuthentication.js +152 -36
- package/dist/core/authentication/refreshSuccessor.js +47 -0
- package/dist/core/authentication/storage/InMemoryTokenStore.d.ts +17 -1
- package/dist/core/authentication/storage/InMemoryTokenStore.js +16 -4
- package/dist/core/authentication/types.d.ts +82 -0
- package/dist/core/crypto/contentKey.d.ts +34 -0
- package/dist/core/crypto/contentKey.js +64 -0
- package/dist/core/crypto/decryptVerify.d.ts +7 -1
- package/dist/core/crypto/decryptVerify.js +23 -5
- package/dist/core/crypto/decrypter-interface.d.ts +6 -0
- package/dist/core/crypto/index.d.ts +12 -0
- package/dist/core/crypto/index.js +7 -1
- package/dist/core/crypto/keyResolver.d.ts +48 -0
- package/dist/core/crypto/keyResolver.js +46 -0
- package/dist/core/crypto/pem-decrypter.d.ts +2 -0
- package/dist/core/crypto/pem-decrypter.js +13 -8
- package/dist/core/crypto/signEncrypt.d.ts +4 -1
- package/dist/core/crypto/signEncrypt.js +20 -5
- package/dist/core/crypto/types.d.ts +8 -1
- package/dist/core/delegation/BtpsDelegator.d.ts +4 -0
- package/dist/core/delegation/BtpsDelegator.js +3 -2
- package/dist/core/delegation/verifyTransporterSignature.d.ts +104 -0
- package/dist/core/delegation/verifyTransporterSignature.js +224 -0
- package/dist/core/error/constant.d.ts +39 -0
- package/dist/core/error/constant.js +55 -0
- package/dist/core/server/constants/btps-protocol.d.ts +21 -5
- package/dist/core/server/constants/btps-protocol.js +46 -8
- package/dist/core/server/constants/index.d.ts +7 -3
- package/dist/core/server/constants/index.js +19 -4
- package/dist/core/server/constants/limits.d.ts +18 -0
- package/dist/core/server/constants/limits.js +19 -1
- package/dist/core/server/constants/permissions.d.ts +38 -0
- package/dist/core/server/constants/permissions.js +38 -0
- package/dist/core/server/schemas/artifacts/agentSchema.js +2 -1
- package/dist/core/server/schemas/artifacts/artifacts.js +7 -1
- package/dist/core/server/schemas/artifacts/controlSchema.js +1 -1
- package/dist/core/server/schemas/artifacts/identityLookupSchema.js +10 -1
- package/dist/core/server/schemas/artifacts/transporterSchema.js +99 -12
- package/dist/core/server/schemas/bounded.js +2 -2
- package/dist/core/server/schemas/btpsDocsSchema.d.ts +36009 -1083
- package/dist/core/server/schemas/btpsDocsSchema.js +305 -42
- package/dist/core/server/schemas/helpers.d.ts +10090 -0
- package/dist/core/server/schemas/helpers.js +76 -12
- package/dist/core/server/schemas/keyChainSchema.js +53 -0
- package/dist/core/server/schemas/responseSchema.js +52 -9
- package/dist/core/server/schemas/schema.js +3 -1
- package/dist/core/server/schemas/trustValues.d.ts +105 -0
- package/dist/core/server/schemas/trustValues.js +117 -0
- package/dist/core/server/types.d.ts +104 -6
- package/dist/core/storage/AbstractIdentityStore.d.ts +9 -0
- package/dist/core/storage/JsonIdentityStore.d.ts +3 -0
- package/dist/core/storage/JsonIdentityStore.js +3 -0
- package/dist/core/storage/types.d.ts +2 -0
- package/dist/core/trust/agreements.d.ts +107 -0
- package/dist/core/trust/agreements.js +116 -0
- package/dist/core/trust/evidence.d.ts +220 -0
- package/dist/core/trust/evidence.js +334 -0
- package/dist/core/trust/index.d.ts +10 -0
- package/dist/core/trust/index.js +14 -1
- package/dist/core/trust/keyRotation.d.ts +148 -0
- package/dist/core/trust/keyRotation.js +267 -0
- package/dist/core/trust/permissions.d.ts +114 -0
- package/dist/core/trust/permissions.js +115 -0
- package/dist/core/trust/shaping.d.ts +42 -0
- package/dist/core/trust/shaping.js +47 -0
- package/dist/core/trust/storage/AbstractTrustStore.d.ts +2 -1
- package/dist/core/trust/storage/JsonTrustStore.d.ts +3 -2
- package/dist/core/trust/timestamps.d.ts +49 -0
- package/dist/core/trust/timestamps.js +78 -0
- package/dist/core/trust/trustUtils.d.ts +13 -2
- package/dist/core/trust/trustUtils.js +28 -3
- package/dist/core/trust/types.d.ts +312 -2
- package/dist/core/trust/verification.d.ts +52 -0
- package/dist/core/trust/verification.js +55 -0
- package/dist/server/btpsServer.d.ts +79 -5
- package/dist/server/btpsServer.js +297 -145
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +17 -1
- package/dist/server/libs/agreementOrderCheck.d.ts +29 -0
- package/dist/server/libs/agreementOrderCheck.js +29 -0
- package/dist/server/libs/artifactTimeWindow.d.ts +2 -2
- package/dist/server/libs/artifactTimeWindow.js +2 -1
- package/dist/server/libs/detailsRequestLimit.d.ts +27 -0
- package/dist/server/libs/detailsRequestLimit.js +28 -0
- package/dist/server/libs/detailsResponseCheck.d.ts +27 -0
- package/dist/server/libs/detailsResponseCheck.js +27 -0
- package/dist/server/libs/eventChainCheck.d.ts +31 -0
- package/dist/server/libs/eventChainCheck.js +39 -0
- package/dist/server/libs/inFlightLimit.d.ts +10 -0
- package/dist/server/libs/inFlightLimit.js +7 -1
- package/dist/server/libs/index.d.ts +5 -0
- package/dist/server/libs/index.js +10 -0
- package/dist/server/libs/keyChains.d.ts +14 -0
- package/dist/server/libs/keyChains.js +17 -0
- package/dist/server/libs/wrapForAgent.d.ts +20 -0
- package/dist/server/libs/wrapForAgent.js +36 -0
- package/dist/server/types.d.ts +103 -9
- 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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
};
|
|
@@ -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 {
|
|
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 {
|
|
13
|
-
|
|
14
|
-
|
|
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,77 @@ 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
|
-
|
|
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 detailsOwed(permissions, entityType, given = []) {
|
|
64
|
+
const identities = Object.values(IDENTITY_DETAIL);
|
|
65
|
+
return permissions.filter(({ name }) => name.startsWith("detail:")).map(({ name }) => ({
|
|
66
|
+
asked: name,
|
|
67
|
+
owed: identities.includes(name) ? IDENTITY_DETAIL[entityType] : name
|
|
68
|
+
})).filter(({ owed }) => !given.includes(owed));
|
|
69
|
+
}
|
|
70
|
+
function permissionEnvelopeProblems(data) {
|
|
71
|
+
const problems = [];
|
|
72
|
+
for (const [field, types] of Object.entries(PERMISSION_ENVELOPE_FIELDS))
|
|
73
|
+
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`]));
|
|
74
|
+
const lists = [
|
|
75
|
+
["permissions", data.permissions?.map((p) => p.name)],
|
|
76
|
+
["given", data.given],
|
|
77
|
+
["granted", data.granted],
|
|
78
|
+
["details", data.details]
|
|
79
|
+
];
|
|
80
|
+
for (const [field, names] of lists) {
|
|
81
|
+
const twice = names && duplicateOf(names);
|
|
82
|
+
twice && problems.push([field, `${twice} appears more than once`]);
|
|
83
|
+
}
|
|
84
|
+
data.type === "TRUST_REQ" && !isVersionBelow(data.version, "1.2.0") && !data.permissions && problems.push([
|
|
85
|
+
"permissions",
|
|
86
|
+
"a 1.2.0 TRUST_REQ asks for permissions; a request for everything is a 1.1.0 one"
|
|
87
|
+
]);
|
|
88
|
+
const { entityType } = data, other = entityType && IDENTITY_DETAIL[entityType === "person" ? "organisation" : "person"];
|
|
89
|
+
if (data.permissions && !entityType && problems.push(["entityType", "entityType comes with permissions"]), data.granted && !entityType && problems.push(["entityType", "entityType comes with granted"]), data.permissions && entityType === "organisation")
|
|
90
|
+
for (const { asked, owed } of detailsOwed(data.permissions, entityType, data.given))
|
|
91
|
+
problems.push(["given", `asks for ${asked} without giving ${owed}`]);
|
|
92
|
+
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) {
|
|
93
|
+
const document = documentRecord(data.document);
|
|
94
|
+
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"]);
|
|
95
|
+
}
|
|
96
|
+
return problems;
|
|
97
|
+
}
|
|
98
|
+
const BtpTransporterArtifactSchema = BtpTransporterArtifactBaseSchema.superRefine(
|
|
28
99
|
(data, ctx) => {
|
|
29
|
-
|
|
100
|
+
const needsRelatesTo = data.type === "BTPS_EVENT" || data.type === "DETAILS_RES", mayRelate = needsRelatesTo || data.type === "DETAILS_REQ";
|
|
101
|
+
(needsRelatesTo ? data.relatesTo === void 0 : !mayRelate && data.relatesTo !== void 0) && ctx.addIssue({
|
|
30
102
|
code: z.ZodIssueCode.custom,
|
|
31
|
-
message: "relatesTo is required on a BTPS_EVENT and
|
|
103
|
+
message: "relatesTo is required on a BTPS_EVENT and a DETAILS_RES, may be on a DETAILS_REQ, and nowhere else",
|
|
32
104
|
path: ["relatesTo"]
|
|
33
|
-
}), data.
|
|
105
|
+
}), data.syncOf !== void 0 && (sameIdentity(data.from, data.to) || ctx.addIssue({
|
|
106
|
+
code: z.ZodIssueCode.custom,
|
|
107
|
+
message: "syncOf is only on a sync, to the sender's own address",
|
|
108
|
+
path: ["syncOf"]
|
|
109
|
+
}), isVersionBelow(data.version, "1.2.0") && ctx.addIssue({
|
|
110
|
+
code: z.ZodIssueCode.custom,
|
|
111
|
+
message: "syncOf is only in a 1.2.0 artifact",
|
|
112
|
+
path: ["syncOf"]
|
|
113
|
+
}));
|
|
114
|
+
for (const [path, message] of permissionEnvelopeProblems(data))
|
|
115
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, message, path: [path] });
|
|
116
|
+
if (data.encryption)
|
|
34
117
|
typeof data.document != "string" && ctx.addIssue({
|
|
35
118
|
code: z.ZodIssueCode.custom,
|
|
36
119
|
message: "When encrypted, document must be a string",
|
|
37
120
|
path: ["document"]
|
|
38
121
|
});
|
|
39
122
|
else {
|
|
40
|
-
const schema = processBtpDocSchema(data.type);
|
|
123
|
+
const schema = processBtpDocSchema(data.type, data.version);
|
|
41
124
|
if (!schema) return !1;
|
|
42
125
|
if (!schema.safeParse(data.document).success) {
|
|
43
126
|
ctx.addIssue({
|
|
@@ -48,13 +131,17 @@ const BtpTransporterArtifactBaseSchema = z.object({
|
|
|
48
131
|
return;
|
|
49
132
|
}
|
|
50
133
|
if (data.type === "BTPS_EVENT") {
|
|
51
|
-
const event = BtpLifecycleEventSchema.safeParse(data.document), problem = event.success ? lifecycleEventEnvelopeProblem(event.data, data) : void 0;
|
|
134
|
+
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
135
|
problem && ctx.addIssue({ code: z.ZodIssueCode.custom, message: problem, path: ["document"] });
|
|
53
136
|
}
|
|
54
137
|
}
|
|
55
138
|
}
|
|
56
139
|
);
|
|
57
140
|
export {
|
|
141
|
+
BtpSyncOfSchema,
|
|
58
142
|
BtpTransporterArtifactBaseSchema,
|
|
59
|
-
BtpTransporterArtifactSchema
|
|
143
|
+
BtpTransporterArtifactSchema,
|
|
144
|
+
BtpTrustPermissionsSchema,
|
|
145
|
+
BtpTrustRecordPermissionsSchema,
|
|
146
|
+
detailsOwed
|
|
60
147
|
};
|
|
@@ -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
|
);
|