@andco/protocol 0.0.3 → 0.0.5

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 (113) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +51 -3
  3. package/dist/api-errors.d.ts +114 -0
  4. package/dist/api-errors.d.ts.map +1 -0
  5. package/dist/api-errors.js +116 -0
  6. package/dist/authorization-preparation.d.ts +52 -543
  7. package/dist/authorization-preparation.d.ts.map +1 -1
  8. package/dist/authorization-preparation.js +19 -42
  9. package/dist/authorization.d.ts +64 -22
  10. package/dist/authorization.d.ts.map +1 -1
  11. package/dist/authorization.js +136 -55
  12. package/dist/consent.d.ts +5 -8
  13. package/dist/consent.d.ts.map +1 -1
  14. package/dist/consent.js +5 -62
  15. package/dist/constants.d.ts +19 -3
  16. package/dist/constants.d.ts.map +1 -1
  17. package/dist/constants.js +19 -3
  18. package/dist/errors.d.ts +154 -58
  19. package/dist/errors.d.ts.map +1 -1
  20. package/dist/errors.js +174 -57
  21. package/dist/index.d.ts +4 -4
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +4 -4
  24. package/dist/intent-input.d.ts +1022 -288
  25. package/dist/intent-input.d.ts.map +1 -1
  26. package/dist/intent-input.js +284 -40
  27. package/dist/messages/error.d.ts +1 -2
  28. package/dist/messages/error.d.ts.map +1 -1
  29. package/dist/messages/error.js +5 -2
  30. package/dist/messages/index.d.ts +1 -1
  31. package/dist/native-copy.d.ts +23 -2
  32. package/dist/native-copy.d.ts.map +1 -1
  33. package/dist/native-copy.js +65 -117
  34. package/dist/native-intent.d.ts +126 -75
  35. package/dist/native-intent.d.ts.map +1 -1
  36. package/dist/native-intent.js +140 -48
  37. package/dist/rar/account-matchers.d.ts +579 -0
  38. package/dist/rar/account-matchers.d.ts.map +1 -0
  39. package/dist/rar/account-matchers.js +480 -0
  40. package/dist/rar/bank.d.ts +1549 -0
  41. package/dist/rar/bank.d.ts.map +1 -0
  42. package/dist/rar/bank.js +379 -0
  43. package/dist/rar/catalog.d.ts +1155 -0
  44. package/dist/rar/catalog.d.ts.map +1 -0
  45. package/dist/rar/catalog.js +175 -0
  46. package/dist/rar/detail.d.ts +39 -0
  47. package/dist/rar/detail.d.ts.map +1 -0
  48. package/dist/rar/detail.js +44 -0
  49. package/dist/rar/index.d.ts +9 -0
  50. package/dist/rar/index.d.ts.map +1 -0
  51. package/dist/rar/index.js +8 -0
  52. package/dist/rar/money.d.ts +58 -0
  53. package/dist/rar/money.d.ts.map +1 -0
  54. package/dist/rar/money.js +61 -0
  55. package/dist/rar/project-users.d.ts +43 -0
  56. package/dist/rar/project-users.d.ts.map +1 -0
  57. package/dist/rar/project-users.js +63 -0
  58. package/dist/rar/subject-data.d.ts +170 -0
  59. package/dist/rar/subject-data.d.ts.map +1 -0
  60. package/dist/rar/subject-data.js +127 -0
  61. package/dist/rar/subject.d.ts +166 -0
  62. package/dist/rar/subject.d.ts.map +1 -0
  63. package/dist/rar/subject.js +119 -0
  64. package/dist/scopes.d.ts +5 -30
  65. package/dist/scopes.d.ts.map +1 -1
  66. package/dist/scopes.js +0 -13
  67. package/dist/service-grants.d.ts +44 -0
  68. package/dist/service-grants.d.ts.map +1 -0
  69. package/dist/service-grants.js +45 -0
  70. package/dist/signing.d.ts +56 -0
  71. package/dist/signing.d.ts.map +1 -0
  72. package/dist/signing.js +233 -0
  73. package/dist/transport/financial-events.d.ts +90 -35
  74. package/dist/transport/financial-events.d.ts.map +1 -1
  75. package/dist/transport/financial-events.js +7 -1
  76. package/dist/transport/generated/oauth-api.d.ts +932 -441
  77. package/dist/transport/generated/oauth-api.d.ts.map +1 -1
  78. package/dist/transport/index.d.ts +0 -1
  79. package/dist/transport/index.d.ts.map +1 -1
  80. package/dist/transport/index.js +0 -1
  81. package/dist/transport/intents.d.ts +155 -23
  82. package/dist/transport/intents.d.ts.map +1 -1
  83. package/dist/transport/intents.js +192 -59
  84. package/dist/transport/rest.d.ts +19 -8
  85. package/dist/transport/rest.d.ts.map +1 -1
  86. package/dist/transport/rest.js +1 -1
  87. package/dist/urls.d.ts.map +1 -1
  88. package/dist/urls.js +1 -1
  89. package/dist/userinfo.d.ts +10 -1
  90. package/dist/userinfo.d.ts.map +1 -1
  91. package/dist/userinfo.js +10 -1
  92. package/dist/utils.d.ts +15 -0
  93. package/dist/utils.d.ts.map +1 -1
  94. package/dist/utils.js +31 -0
  95. package/dist/webhooks.d.ts +154 -0
  96. package/dist/webhooks.d.ts.map +1 -0
  97. package/dist/webhooks.js +249 -0
  98. package/package.json +21 -2
  99. package/dist/automatic-charge.d.ts +0 -314
  100. package/dist/automatic-charge.d.ts.map +0 -1
  101. package/dist/automatic-charge.js +0 -126
  102. package/dist/bank-account-authorization.d.ts +0 -6
  103. package/dist/bank-account-authorization.d.ts.map +0 -1
  104. package/dist/bank-account-authorization.js +0 -13
  105. package/dist/transport/webhooks.d.ts +0 -27
  106. package/dist/transport/webhooks.d.ts.map +0 -1
  107. package/dist/transport/webhooks.js +0 -68
  108. package/dist/webhook-input.d.ts +0 -22
  109. package/dist/webhook-input.d.ts.map +0 -1
  110. package/dist/webhook-input.js +0 -11
  111. package/dist/webhook-signature.d.ts +0 -54
  112. package/dist/webhook-signature.d.ts.map +0 -1
  113. package/dist/webhook-signature.js +0 -74
@@ -0,0 +1,480 @@
1
+ import * as v from "valibot";
2
+ /**
3
+ * Syntax of an Account Alias: the name that identifies one Cuenta Vista among the accounts of its
4
+ * Subject, unique per Subject regardless of which Project opened it.
5
+ */
6
+ export const ACCOUNT_ALIAS_SYNTAX = "^[a-z][a-z0-9_-]{0,63}$";
7
+ /**
8
+ * Syntax of the `pattern` carried by an `alias` matcher, and the entire grammar of the language:
9
+ * one alias literal (`caja-chica-1`), one alias literal followed by a single trailing `*`
10
+ * (`caja-chica-*`), or `*` alone. There is no other metacharacter, no anchor and no alternation.
11
+ *
12
+ * Regular expressions were rejected on a decisive technical ground, not on taste. The
13
+ * authorization decision has to answer whether one resolved detail is *contained* in the requested
14
+ * one, and containment of regular languages is PSPACE-complete; with prefix globs it is the three
15
+ * lines of {@link accountAliasPatternContains}. Two further reasons hold independently: the
16
+ * decision runs in Postgres while the schema is validated in JavaScript, and POSIX and ECMAScript
17
+ * are not the same language, so one permission would mean two things depending on where it ran;
18
+ * and Postgres `~` is unanchored, so `cliente-1` would silently also authorize `evil-cliente-123`,
19
+ * failing towards granting more.
20
+ */
21
+ export const ACCOUNT_ALIAS_PATTERN_SYNTAX = "^(?:\\*|[a-z][a-z0-9_-]{0,63}\\*?)$";
22
+ /** Syntax of an Andco account identifier: a UUID. */
23
+ export const ACCOUNT_ID_PATTERN = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$";
24
+ /** An Account Alias, exactly as its holder chose it. */
25
+ export const AccountAliasSchema = /* @__PURE__ */ v.pipe(v.string(), v.regex(new RegExp(ACCOUNT_ALIAS_SYNTAX)));
26
+ /** A prefix glob over Account Aliases. See {@link ACCOUNT_ALIAS_PATTERN_SYNTAX} for the grammar. */
27
+ export const AccountAliasPatternSchema = /* @__PURE__ */ v.pipe(v.string(), v.regex(new RegExp(ACCOUNT_ALIAS_PATTERN_SYNTAX)));
28
+ /** Upper bound of every matcher list. */
29
+ export const ACCOUNT_MATCHER_LIST_CEILING = 100;
30
+ /** Upper bound of each optional field of an `external` matcher. */
31
+ const EXTERNAL_FIELD_CEILING = 128;
32
+ /** The fields an `external` matcher may fix, each narrowing the accounts it admits. */
33
+ export const ACCOUNT_EXTERNAL_FIELDS = ["bank_id", "account_type", "account_id", "holder_id", "holder_name"];
34
+ const accountId = /* @__PURE__ */ v.pipe(v.string(), v.regex(new RegExp(ACCOUNT_ID_PATTERN)));
35
+ const externalField = /* @__PURE__ */ v.pipe(v.string(), v.minLength(1), v.maxLength(EXTERNAL_FIELD_CEILING), v.regex(/\S/));
36
+ const editable = /* @__PURE__ */ v.nullish(v.boolean());
37
+ /**
38
+ * How many accounts the person picks at consent for an `editable` matcher of a list she narrows:
39
+ * `"multiple"` (the default) lets her keep several, `"single"` asks for exactly one, which the
40
+ * Grant then fixes as one `account` matcher. Only an `editable` own matcher (`alias` or
41
+ * `any`/`own`) of a source, `accounts` or Deposit destination list may carry it, and every editable
42
+ * matcher of that list must agree.
43
+ *
44
+ * @example
45
+ * // Firma Ya charges from exactly one account the person chooses.
46
+ * const sources: OwnAccountMatcher[] = [{ type: "any", limit: "own", editable: true, selection: "single" }];
47
+ */
48
+ export const ACCOUNT_SELECTIONS = ["single", "multiple"];
49
+ const selection = /* @__PURE__ */ v.nullish(v.picklist(ACCOUNT_SELECTIONS));
50
+ // -- Grant Request matchers ---------------------------------------------------------------------
51
+ /**
52
+ * Accounts of the Subject whose Account Alias matches `pattern` (literal, `prefix*` or `*`),
53
+ * including accounts that will exist tomorrow. `type` may be omitted. With `editable`, the person
54
+ * may narrow it at consent to accounts it currently matches, which the Grant then fixes as
55
+ * `account` matchers.
56
+ *
57
+ * @example
58
+ * const caja: AccountAlias = { type: "alias", pattern: "caja-*", editable: true };
59
+ * const short: AccountAlias = { pattern: "ventas" };
60
+ */
61
+ export const AccountAliasMatcherSchema = /* @__PURE__ */ v.strictObject({
62
+ type: v.nullish(v.literal("alias")),
63
+ pattern: AccountAliasPatternSchema,
64
+ editable,
65
+ selection,
66
+ });
67
+ /**
68
+ * One exact Andco account. As a source or in `accounts` it must be an account of the Subject; the
69
+ * gate checks ownership, since the schema cannot.
70
+ */
71
+ export const AccountInternalMatcherSchema = /* @__PURE__ */ v.strictObject({
72
+ type: v.literal("account"),
73
+ id: accountId,
74
+ editable,
75
+ });
76
+ /** Every account of the Subject. With `editable`, the person picks which ones (see {@link ACCOUNT_SELECTIONS}). */
77
+ export const AnyOwnMatcherSchema = /* @__PURE__ */ v.strictObject({
78
+ type: v.literal("any"),
79
+ limit: v.literal("own"),
80
+ editable,
81
+ selection,
82
+ });
83
+ /** Every Andco account, of any Subject. Only a destination. */
84
+ export const AnyLocalMatcherSchema = /* @__PURE__ */ v.strictObject({
85
+ type: v.literal("any"),
86
+ limit: v.literal("local"),
87
+ editable,
88
+ });
89
+ /** Every account, at Andco or at another bank. Only a destination, and never `editable`. */
90
+ export const AnyAllMatcherSchema = /* @__PURE__ */ v.strictObject({
91
+ type: v.literal("any"),
92
+ limit: v.literal("all"),
93
+ });
94
+ /**
95
+ * Accounts at other banks, narrowed by every field it fixes. `{ type: "external" }` alone admits
96
+ * every account outside Andco. Only a destination.
97
+ *
98
+ * @example
99
+ * const payroll: AccountExternal = { type: "external", bank_id: "banco_de_chile", account_id: "00123456" };
100
+ */
101
+ export const AccountExternalMatcherSchema = /* @__PURE__ */ v.strictObject({
102
+ type: v.literal("external"),
103
+ bank_id: v.nullish(externalField),
104
+ account_type: v.nullish(externalField),
105
+ account_id: v.nullish(externalField),
106
+ holder_id: v.nullish(externalField),
107
+ holder_name: v.nullish(externalField),
108
+ editable,
109
+ });
110
+ /**
111
+ * A matcher bounded by the Subject: the only kinds a source, an `accounts` list or a Deposit
112
+ * destination accepts, so that no permission can debit, read or collect into someone else's account.
113
+ */
114
+ export const OwnAccountMatcherSchema = /* @__PURE__ */ v.union([
115
+ AccountAliasMatcherSchema,
116
+ AccountInternalMatcherSchema,
117
+ AnyOwnMatcherSchema,
118
+ ]);
119
+ /** Every matcher kind; what a Transfer destination accepts. */
120
+ export const DestinationAccountMatcherSchema = /* @__PURE__ */ v.union([
121
+ AccountAliasMatcherSchema,
122
+ AccountInternalMatcherSchema,
123
+ AnyOwnMatcherSchema,
124
+ AnyLocalMatcherSchema,
125
+ AnyAllMatcherSchema,
126
+ AccountExternalMatcherSchema,
127
+ ]);
128
+ // -- Grant matchers -----------------------------------------------------------------------------
129
+ /** {@link AccountAliasMatcherSchema} as granted: `type` present, no `editable`. */
130
+ export const GrantedAccountAliasSchema = /* @__PURE__ */ v.strictObject({
131
+ type: v.literal("alias"),
132
+ pattern: AccountAliasPatternSchema,
133
+ });
134
+ /** {@link AccountInternalMatcherSchema} as granted. */
135
+ export const GrantedAccountInternalSchema = /* @__PURE__ */ v.strictObject({
136
+ type: v.literal("account"),
137
+ id: accountId,
138
+ });
139
+ /** {@link AnyOwnMatcherSchema} as granted. */
140
+ export const GrantedAnyOwnSchema = /* @__PURE__ */ v.strictObject({ type: v.literal("any"), limit: v.literal("own") });
141
+ /** {@link AnyLocalMatcherSchema} as granted. */
142
+ export const GrantedAnyLocalSchema = /* @__PURE__ */ v.strictObject({
143
+ type: v.literal("any"),
144
+ limit: v.literal("local"),
145
+ });
146
+ /** {@link AnyAllMatcherSchema} as granted. */
147
+ export const GrantedAnyAllSchema = AnyAllMatcherSchema;
148
+ /** {@link AccountExternalMatcherSchema} as granted: absent fields are unrestricted, never `null`. */
149
+ export const GrantedAccountExternalSchema = /* @__PURE__ */ v.strictObject({
150
+ type: v.literal("external"),
151
+ bank_id: v.optional(externalField),
152
+ account_type: v.optional(externalField),
153
+ account_id: v.optional(externalField),
154
+ holder_id: v.optional(externalField),
155
+ holder_name: v.optional(externalField),
156
+ });
157
+ /** {@link OwnAccountMatcherSchema} as granted. */
158
+ export const GrantedOwnAccountMatcherSchema = /* @__PURE__ */ v.union([
159
+ GrantedAccountAliasSchema,
160
+ GrantedAccountInternalSchema,
161
+ GrantedAnyOwnSchema,
162
+ ]);
163
+ /** {@link DestinationAccountMatcherSchema} as granted. */
164
+ export const GrantedDestinationAccountMatcherSchema = /* @__PURE__ */ v.union([
165
+ GrantedAccountAliasSchema,
166
+ GrantedAccountInternalSchema,
167
+ GrantedAnyOwnSchema,
168
+ GrantedAnyLocalSchema,
169
+ GrantedAnyAllSchema,
170
+ GrantedAccountExternalSchema,
171
+ ]);
172
+ // -- Lists --------------------------------------------------------------------------------------
173
+ /**
174
+ * Identity of a matcher inside one list, used to reject duplicates without rejecting overlap.
175
+ * `editable` is not part of it: two entries differing only there say the same thing twice.
176
+ */
177
+ export function accountMatcherKey(matcher) {
178
+ switch (matcher.type) {
179
+ case "account":
180
+ return `account:${matcher.id}`;
181
+ case "any":
182
+ return `any:${matcher.limit}`;
183
+ case "external":
184
+ return `external:${ACCOUNT_EXTERNAL_FIELDS.map((field) => matcher[field] ?? "").join("\u0000")}`;
185
+ default:
186
+ return `alias:${matcher.pattern}`;
187
+ }
188
+ }
189
+ /**
190
+ * A non-empty list of distinct matchers. Empty is rejected rather than read as "everything": a
191
+ * permission that listed no account authorizes zero, never all. Absent or `null` is how a Grant
192
+ * Request asks for the type's default.
193
+ */
194
+ export function accountMatcherList(schema) {
195
+ return v.pipe(v.array(schema), v.minLength(1), v.maxLength(ACCOUNT_MATCHER_LIST_CEILING), v.check((matchers) => new Set(matchers.map((one) => accountMatcherKey(one))).size === matchers.length, "duplicate_account_matcher"));
196
+ }
197
+ /**
198
+ * Whether the `selection` of a list's matchers is coherent: it only sits on `editable` matchers, and
199
+ * every editable matcher of the list asks for the same one. Lists that are never picked from (the
200
+ * aliases of accounts to open, a Transfer's destinations) must carry none: {@link accountSelectionIsAbsent}.
201
+ *
202
+ * @example
203
+ * accountSelectionIsCoherent([{ type: "any", limit: "own", editable: true, selection: "single" }]); // true
204
+ * accountSelectionIsCoherent([{ type: "any", limit: "own", selection: "single" }]); // false
205
+ */
206
+ export function accountSelectionIsCoherent(matchers) {
207
+ const editableSelections = new Set();
208
+ for (const matcher of matchers) {
209
+ const picked = "selection" in matcher ? matcher.selection : undefined;
210
+ const isEditable = "editable" in matcher && matcher.editable === true;
211
+ if (picked !== undefined && picked !== null && !isEditable)
212
+ return false;
213
+ if (isEditable)
214
+ editableSelections.add(picked ?? "multiple");
215
+ }
216
+ return editableSelections.size <= 1;
217
+ }
218
+ /** Whether no matcher of a list carries a `selection`: what a list nobody picks from must satisfy. */
219
+ export function accountSelectionIsAbsent(matchers) {
220
+ return matchers.every((matcher) => !("selection" in matcher) || matcher.selection === undefined || matcher.selection === null);
221
+ }
222
+ /** Whether the person picks exactly one account for this list's editable matchers. */
223
+ export function accountSelectionIsSingle(matchers) {
224
+ return matchers.some((matcher) => "selection" in matcher && matcher.selection === "single");
225
+ }
226
+ // -- Defaults -----------------------------------------------------------------------------------
227
+ /** What an absent or `null` source list, `accounts` list or deposit destination list means. */
228
+ export const DEFAULT_OWN_ACCOUNT_MATCHERS = [{ type: "any", limit: "own" }];
229
+ /** What an absent or `null` Transfer destination list means. */
230
+ export const DEFAULT_DESTINATION_ACCOUNT_MATCHERS = [
231
+ { type: "any", limit: "all" },
232
+ ];
233
+ // -- JSON Schema mirrors ------------------------------------------------------------------------
234
+ const nullable = (schema) => ({ anyOf: [{ type: "null" }, schema] });
235
+ const editableJson = nullable({ type: "boolean" });
236
+ const selectionJson = nullable({ enum: [...ACCOUNT_SELECTIONS] });
237
+ const externalFieldJson = { type: "string", minLength: 1, maxLength: EXTERNAL_FIELD_CEILING, pattern: "\\S" };
238
+ const aliasRequestJson = {
239
+ type: "object",
240
+ additionalProperties: false,
241
+ required: ["pattern"],
242
+ properties: {
243
+ type: nullable({ const: "alias" }),
244
+ pattern: { type: "string", pattern: ACCOUNT_ALIAS_PATTERN_SYNTAX },
245
+ editable: editableJson,
246
+ selection: selectionJson,
247
+ },
248
+ };
249
+ const accountRequestJson = {
250
+ type: "object",
251
+ additionalProperties: false,
252
+ required: ["type", "id"],
253
+ properties: {
254
+ type: { const: "account" },
255
+ id: { type: "string", pattern: ACCOUNT_ID_PATTERN },
256
+ editable: editableJson,
257
+ },
258
+ };
259
+ const anyRequestJson = (limit) => ({
260
+ type: "object",
261
+ additionalProperties: false,
262
+ required: ["type", "limit"],
263
+ properties: limit === "own"
264
+ ? { type: { const: "any" }, limit: { const: limit }, editable: editableJson, selection: selectionJson }
265
+ : { type: { const: "any" }, limit: { const: limit }, editable: editableJson },
266
+ });
267
+ const anyAllJson = {
268
+ type: "object",
269
+ additionalProperties: false,
270
+ required: ["type", "limit"],
271
+ properties: { type: { const: "any" }, limit: { const: "all" } },
272
+ };
273
+ const externalRequestJson = {
274
+ type: "object",
275
+ additionalProperties: false,
276
+ required: ["type"],
277
+ properties: {
278
+ type: { const: "external" },
279
+ ...Object.fromEntries(ACCOUNT_EXTERNAL_FIELDS.map((field) => [field, nullable(externalFieldJson)])),
280
+ editable: editableJson,
281
+ },
282
+ };
283
+ const aliasGrantJson = {
284
+ type: "object",
285
+ additionalProperties: false,
286
+ required: ["type", "pattern"],
287
+ properties: { type: { const: "alias" }, pattern: { type: "string", pattern: ACCOUNT_ALIAS_PATTERN_SYNTAX } },
288
+ };
289
+ const accountGrantJson = {
290
+ type: "object",
291
+ additionalProperties: false,
292
+ required: ["type", "id"],
293
+ properties: { type: { const: "account" }, id: { type: "string", pattern: ACCOUNT_ID_PATTERN } },
294
+ };
295
+ const anyGrantJson = (limit) => ({
296
+ type: "object",
297
+ additionalProperties: false,
298
+ required: ["type", "limit"],
299
+ properties: { type: { const: "any" }, limit: { const: limit } },
300
+ });
301
+ const externalGrantJson = {
302
+ type: "object",
303
+ additionalProperties: false,
304
+ required: ["type"],
305
+ properties: {
306
+ type: { const: "external" },
307
+ ...Object.fromEntries(ACCOUNT_EXTERNAL_FIELDS.map((field) => [field, externalFieldJson])),
308
+ },
309
+ };
310
+ const listJson = (items) => ({
311
+ type: "array",
312
+ minItems: 1,
313
+ maxItems: ACCOUNT_MATCHER_LIST_CEILING,
314
+ uniqueItems: true,
315
+ items,
316
+ });
317
+ /** JSON Schema mirror of {@link OwnAccountMatcherSchema}. */
318
+ export const OWN_ACCOUNT_MATCHER_JSON_SCHEMA = { anyOf: [aliasRequestJson, accountRequestJson, anyRequestJson("own")] };
319
+ /** JSON Schema mirror of {@link DestinationAccountMatcherSchema}. */
320
+ export const DESTINATION_ACCOUNT_MATCHER_JSON_SCHEMA = {
321
+ anyOf: [
322
+ aliasRequestJson,
323
+ accountRequestJson,
324
+ anyRequestJson("own"),
325
+ anyRequestJson("local"),
326
+ anyAllJson,
327
+ externalRequestJson,
328
+ ],
329
+ };
330
+ /** JSON Schema mirror of {@link GrantedOwnAccountMatcherSchema}. */
331
+ export const GRANTED_OWN_ACCOUNT_MATCHER_JSON_SCHEMA = {
332
+ anyOf: [aliasGrantJson, accountGrantJson, anyGrantJson("own")],
333
+ };
334
+ /** JSON Schema mirror of {@link GrantedDestinationAccountMatcherSchema}. */
335
+ export const GRANTED_DESTINATION_ACCOUNT_MATCHER_JSON_SCHEMA = {
336
+ anyOf: [aliasGrantJson, accountGrantJson, anyGrantJson("own"), anyGrantJson("local"), anyAllJson, externalGrantJson],
337
+ };
338
+ /**
339
+ * JSON Schema mirror of {@link accountMatcherList}. `uniqueItems` compares whole objects, so it is
340
+ * looser than the valibot check, which ignores `editable` and an omitted alias `type`; the gate's
341
+ * own comparison closes the gap.
342
+ */
343
+ export function accountMatcherListJsonSchema(items) {
344
+ return listJson(items);
345
+ }
346
+ // -- Alias patterns -----------------------------------------------------------------------------
347
+ /**
348
+ * Decides whether `alias` belongs to the set described by `pattern`.
349
+ *
350
+ * The grammar of {@link ACCOUNT_ALIAS_PATTERN_SYNTAX} leaves only two cases: a literal matches by
351
+ * equality, and a trailing `*` matches by prefix. `*` alone has the empty prefix and therefore
352
+ * matches every alias — but only an alias: an account that carries none is matched by nothing.
353
+ * A malformed pattern can only match fewer aliases, never more: `caja*chica` has no trailing `*`,
354
+ * so it is compared as a literal.
355
+ *
356
+ * @example
357
+ * accountAliasPatternMatches("caja-*", "caja-chica"); // true
358
+ * accountAliasPatternMatches("caja-chica", "caja-chica-1"); // false
359
+ */
360
+ export function accountAliasPatternMatches(pattern, alias) {
361
+ if (!pattern.endsWith("*"))
362
+ return pattern === alias;
363
+ return alias.startsWith(pattern.slice(0, -1));
364
+ }
365
+ /** The literal part of a pattern: the whole pattern for a literal, everything before the `*` otherwise. */
366
+ function literalPrefixOf(pattern) {
367
+ return pattern.endsWith("*") ? pattern.slice(0, -1) : pattern;
368
+ }
369
+ /**
370
+ * Decides whether every alias matched by `inner` is also matched by `outer`, for every possible
371
+ * set of aliases rather than for the ones that happen to exist right now.
372
+ *
373
+ * A literal `outer` denotes a single alias, so it can only contain itself — in particular
374
+ * `caja-chica` does not contain `caja-chica*`, whose language is infinite. A glob `outer` contains
375
+ * `inner` exactly when `inner`'s literal prefix starts with `outer`'s.
376
+ *
377
+ * @example
378
+ * accountAliasPatternContains("caja-*", "caja-chica-*"); // true
379
+ * accountAliasPatternContains("caja-chica", "caja-chica*"); // false
380
+ */
381
+ export function accountAliasPatternContains(outer, inner) {
382
+ if (!outer.endsWith("*"))
383
+ return inner === outer;
384
+ return literalPrefixOf(inner).startsWith(literalPrefixOf(outer));
385
+ }
386
+ /** Classifies a matcher; an alias matcher may omit its `type`. */
387
+ export function accountMatcherKind(matcher) {
388
+ if (matcher.type === "any")
389
+ return matcher.limit;
390
+ if (matcher.type === "account" || matcher.type === "external")
391
+ return matcher.type;
392
+ return "alias";
393
+ }
394
+ /** Reads an own-account fact without ever reaching the prototype chain. */
395
+ function ownAccountAlias(facts, id) {
396
+ const record = facts.ownAccounts;
397
+ if (record === undefined || !Object.hasOwn(record, id))
398
+ return { own: false, alias: null };
399
+ const alias = record[id];
400
+ return { own: true, alias: typeof alias === "string" ? alias : null };
401
+ }
402
+ /** An `external` contains another when every field it fixes is fixed to the same value there. */
403
+ function externalContains(outer, inner) {
404
+ return ACCOUNT_EXTERNAL_FIELDS.every((field) => {
405
+ const fixed = outer[field];
406
+ return fixed === undefined || fixed === null || inner[field] === fixed;
407
+ });
408
+ }
409
+ /**
410
+ * Decides whether every account admitted by `inner` is also admitted by `outer`. The Grant's (or the
411
+ * Grant Request's) matcher is `outer`; the narrower Grant, or the account an operation names, is
412
+ * `inner`. Containment must keep holding as accounts and aliases change.
413
+ *
414
+ * The order is `any(all) ⊇ any(local) ⊇ any(own) ⊇ alias ⊇ account`, and `external` only under
415
+ * `all`:
416
+ *
417
+ * - `any(all)` contains every matcher; only `any(all)` contains `any(all)`.
418
+ * - `external` is contained by `any(all)`, and by an `external` whose every fixed field `inner`
419
+ * fixes to the same value. Nothing else contains it, and it contains nothing but such an
420
+ * `external`.
421
+ * - `any(local)` contains every Andco matcher: `any(local)`, `any(own)`, `alias` and `account`.
422
+ * - `any(own)` contains `any(own)` and `alias` (an alias names an account of the Subject by
423
+ * construction), and an `account` only when `facts.ownAccounts` says it is the Subject's.
424
+ * - `alias:q` contains `alias:p` per {@link accountAliasPatternContains}, and an `account` only when
425
+ * `facts.ownAccounts` gives it an alias that `q` matches.
426
+ * - `account:id` contains only `account:id`. It never contains a pattern, not even one matching
427
+ * exactly one account today, because it could match two tomorrow.
428
+ *
429
+ * `editable` never changes the answer. The relation is mirrored in SQL by the gate; the cases in
430
+ * `scripts/account-matcher-containment-cases.mjs` pin both to the same table.
431
+ *
432
+ * @example
433
+ * accountMatcherContains({ type: "any", limit: "local" }, { type: "alias", pattern: "caja-*" }); // true
434
+ * accountMatcherContains({ type: "any", limit: "local" }, { type: "external" }); // false
435
+ * accountMatcherContains(
436
+ * { type: "alias", pattern: "caja-*" },
437
+ * { type: "account", id: "00000000-0000-4000-8000-000000000001" },
438
+ * { ownAccounts: { "00000000-0000-4000-8000-000000000001": "caja-chica" } },
439
+ * ); // true
440
+ */
441
+ export function accountMatcherContains(outer, inner, facts = {}) {
442
+ const outerKind = accountMatcherKind(outer);
443
+ const innerKind = accountMatcherKind(inner);
444
+ if (outerKind === "all")
445
+ return true;
446
+ if (innerKind === "all")
447
+ return false;
448
+ if (outer.type === "external" || inner.type === "external") {
449
+ return outer.type === "external" && inner.type === "external" && externalContains(outer, inner);
450
+ }
451
+ if (outerKind === "local")
452
+ return true;
453
+ if (innerKind === "local")
454
+ return false;
455
+ if (outerKind === "own") {
456
+ if (inner.type === "account")
457
+ return ownAccountAlias(facts, inner.id).own;
458
+ return true;
459
+ }
460
+ if (innerKind === "own")
461
+ return false;
462
+ if (outer.type === "account")
463
+ return inner.type === "account" && inner.id === outer.id;
464
+ if (inner.type === "account") {
465
+ const { alias } = ownAccountAlias(facts, inner.id);
466
+ return alias !== null && "pattern" in outer && accountAliasPatternMatches(outer.pattern, alias);
467
+ }
468
+ return "pattern" in outer && "pattern" in inner && accountAliasPatternContains(outer.pattern, inner.pattern);
469
+ }
470
+ /**
471
+ * Decides whether the set described by the `inner` list is contained in the one described by
472
+ * `outer`: every `inner` matcher must be contained in at least one `outer` matcher.
473
+ *
474
+ * An empty `inner` is contained in anything, since it admits no account at all, and a non-empty
475
+ * `inner` is never contained in an empty `outer`. Containment is not distributed across `outer`:
476
+ * `[alias:a-*, alias:b-*]` does not contain `alias:*`, and refusing it is the sound answer.
477
+ */
478
+ export function accountMatchersContain(outer, inner, facts = {}) {
479
+ return inner.every((one) => outer.some((candidate) => accountMatcherContains(candidate, one, facts)));
480
+ }