@ixo/action-protocol 0.0.0-stage → 0.5.0
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 +46 -0
- package/README.md +99 -2
- package/package.json +30 -4
- package/src/index.d.ts +559 -0
- package/src/index.js +1335 -0
- package/src/operation-schema.js +181 -0
- package/src/outcome-signature.js +217 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,1335 @@
|
|
|
1
|
+
import {
|
|
2
|
+
operationJsonSchema,
|
|
3
|
+
operationKinds,
|
|
4
|
+
validateOperationSchema,
|
|
5
|
+
} from "./operation-schema.js";
|
|
6
|
+
export { operationJsonSchema, operationKinds };
|
|
7
|
+
const ID = /^[A-Za-z0-9_-]{32}$/;
|
|
8
|
+
const HEX = /^[a-f0-9]{64}$/;
|
|
9
|
+
const ADDRESS = /^ixo1[023456789acdefghjklmnpqrstuvwxyz]{38,59}$/;
|
|
10
|
+
const INT = /^(0|[1-9][0-9]*)$/;
|
|
11
|
+
const FIELD = /^[a-z][a-z0-9_]{0,39}$/;
|
|
12
|
+
/** Envelope tags. v1 carries only the 0.1 status/receipt set; v2 adds terminal non-execution outcomes. */
|
|
13
|
+
export const OUTCOME_VERSION_V1 = "ixo.auth-outcome/1";
|
|
14
|
+
export const OUTCOME_VERSION_V2 = "ixo.auth-outcome/2";
|
|
15
|
+
export const CAPABILITIES_VERSION_V1 = "ixo.auth-capabilities/1";
|
|
16
|
+
export const CAPABILITIES_VERSION_V2 = "ixo.auth-capabilities/2";
|
|
17
|
+
const MEMO_BYTES = 256;
|
|
18
|
+
/** 0.3.0/0.5.0: any Cosmos bank denom (native, ibc/<hash>, factory/...). */
|
|
19
|
+
export const DENOM = /^[a-zA-Z][a-zA-Z0-9/:._-]{2,127}$/;
|
|
20
|
+
/** 0.2.1: mainnet is a valid network value; enablement is a Hub deployment decision. */
|
|
21
|
+
export const NETWORKS = ["ixo mainnet", "ixo testnet", "ixo devnet"];
|
|
22
|
+
/**
|
|
23
|
+
* 0.4.1: authorization / statement-proof `network` (bare names). Every ixo
|
|
24
|
+
* network is valid; the receiver must require its own deployment's network.
|
|
25
|
+
*/
|
|
26
|
+
export const SERVICE_NETWORKS = ["mainnet", "testnet", "devnet"];
|
|
27
|
+
const V1_OUTCOME_STATUSES = [
|
|
28
|
+
"authorized",
|
|
29
|
+
"declined",
|
|
30
|
+
"submitted",
|
|
31
|
+
"succeeded",
|
|
32
|
+
"failed",
|
|
33
|
+
"unknown",
|
|
34
|
+
];
|
|
35
|
+
/** The 0.1.0 receipt and operation sets: the only ones a v1 envelope may carry. */
|
|
36
|
+
const V1_RECEIPT_KINDS = ["form.submission.v1", "ixo.bank.send.v1"];
|
|
37
|
+
export const V1_OPERATION_KINDS = [
|
|
38
|
+
"form.submit.v1",
|
|
39
|
+
"ixo.bank.send.v1",
|
|
40
|
+
"resource.read.v1",
|
|
41
|
+
];
|
|
42
|
+
/** Terminal outcomes for an action that never executed (v2 only, receipt always null). */
|
|
43
|
+
const V2_ONLY_OUTCOME_STATUSES = ["expired", "cancelled"];
|
|
44
|
+
export const OUTCOME_SEQUENCE = {
|
|
45
|
+
authorized: 1,
|
|
46
|
+
declined: 1,
|
|
47
|
+
submitted: 2,
|
|
48
|
+
unknown: 3,
|
|
49
|
+
succeeded: 4,
|
|
50
|
+
failed: 4,
|
|
51
|
+
expired: 5,
|
|
52
|
+
cancelled: 5,
|
|
53
|
+
};
|
|
54
|
+
/** A transfer that was signed but never included in a block (`height` is null). */
|
|
55
|
+
export const BANK_NOT_INCLUDED_REASONS = [
|
|
56
|
+
"check_tx_rejected",
|
|
57
|
+
"expired_not_included",
|
|
58
|
+
];
|
|
59
|
+
const fail = (field) => {
|
|
60
|
+
throw new TypeError(`Invalid ${field}`);
|
|
61
|
+
};
|
|
62
|
+
const object = (value, field) =>
|
|
63
|
+
value && typeof value === "object" && !Array.isArray(value)
|
|
64
|
+
? value
|
|
65
|
+
: fail(field);
|
|
66
|
+
const exact = (value, keys, field) => {
|
|
67
|
+
for (const key of Object.keys(value)) if (!keys.includes(key)) fail(field);
|
|
68
|
+
};
|
|
69
|
+
const str = (value, field, min = 1, max = 200) =>
|
|
70
|
+
typeof value === "string" && value.length >= min && value.length <= max
|
|
71
|
+
? value
|
|
72
|
+
: fail(field);
|
|
73
|
+
const num = (value, field) =>
|
|
74
|
+
Number.isSafeInteger(value) && value > 0 ? value : fail(field);
|
|
75
|
+
const pattern = (value, regex, field) =>
|
|
76
|
+
regex.test(str(value, field)) ? value : fail(field);
|
|
77
|
+
|
|
78
|
+
export function canonicalJson(value) {
|
|
79
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
80
|
+
return JSON.stringify(value);
|
|
81
|
+
if (typeof value === "number" && Number.isSafeInteger(value))
|
|
82
|
+
return JSON.stringify(value);
|
|
83
|
+
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
|
|
84
|
+
if (
|
|
85
|
+
value &&
|
|
86
|
+
typeof value === "object" &&
|
|
87
|
+
Object.getPrototypeOf(value) === Object.prototype
|
|
88
|
+
) {
|
|
89
|
+
const keys = Object.keys(value).sort();
|
|
90
|
+
return `{${keys.map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`).join(",")}}`;
|
|
91
|
+
}
|
|
92
|
+
return fail("canonical JSON");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function digestOf(value) {
|
|
96
|
+
const bytes = new TextEncoder().encode(canonicalJson(value));
|
|
97
|
+
const hash = await crypto.subtle.digest("SHA-256", bytes);
|
|
98
|
+
return Array.from(new Uint8Array(hash), (byte) =>
|
|
99
|
+
byte.toString(16).padStart(2, "0"),
|
|
100
|
+
).join("");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function parseOperation(input) {
|
|
104
|
+
const op = object(input, "operation");
|
|
105
|
+
validateOperationSchema(op);
|
|
106
|
+
if (op.kind === "statement.sign.v1") {
|
|
107
|
+
utf8(op.statement, "statement");
|
|
108
|
+
return op;
|
|
109
|
+
}
|
|
110
|
+
if (op.kind === "vfs.text.save.v1") {
|
|
111
|
+
utf8(op.text, "text");
|
|
112
|
+
privateTextPath("A".repeat(32), op.filename);
|
|
113
|
+
return op;
|
|
114
|
+
}
|
|
115
|
+
if (op.kind.startsWith("gmail.")) {
|
|
116
|
+
for (const field of [
|
|
117
|
+
"accountLabel",
|
|
118
|
+
...(op.kind === "gmail.message.send.v1" ? ["to"] : []),
|
|
119
|
+
]) {
|
|
120
|
+
if (op[field] !== null && op[field].split("@")[0].length > 64)
|
|
121
|
+
fail(field);
|
|
122
|
+
}
|
|
123
|
+
if (op.kind === "gmail.message.send.v1") {
|
|
124
|
+
utf8(op.subject, "subject");
|
|
125
|
+
utf8(op.body, "body");
|
|
126
|
+
}
|
|
127
|
+
return op;
|
|
128
|
+
}
|
|
129
|
+
if (op.kind === "calendar.event.create.v1") {
|
|
130
|
+
if (!validCalendarInstant(op.startsAt) || !validCalendarInstant(op.endsAt))
|
|
131
|
+
fail("calendar instant");
|
|
132
|
+
const start = Date.parse(op.startsAt),
|
|
133
|
+
end = Date.parse(op.endsAt);
|
|
134
|
+
if (
|
|
135
|
+
!Number.isFinite(start) ||
|
|
136
|
+
!Number.isFinite(end) ||
|
|
137
|
+
end <= start ||
|
|
138
|
+
end - start > 86400000
|
|
139
|
+
)
|
|
140
|
+
fail("event duration");
|
|
141
|
+
try {
|
|
142
|
+
new Intl.DateTimeFormat("en", { timeZone: op.timeZone });
|
|
143
|
+
} catch {
|
|
144
|
+
fail("timeZone");
|
|
145
|
+
}
|
|
146
|
+
return op;
|
|
147
|
+
}
|
|
148
|
+
if (
|
|
149
|
+
[
|
|
150
|
+
"calendar.connect.v1",
|
|
151
|
+
"calendar.disconnect.v1",
|
|
152
|
+
"vfs.access.request.v1",
|
|
153
|
+
].includes(op.kind)
|
|
154
|
+
)
|
|
155
|
+
return op;
|
|
156
|
+
if (op.kind === "form.submit.v1") {
|
|
157
|
+
exact(
|
|
158
|
+
op,
|
|
159
|
+
["kind", "formId", "destination", "fields", "declaration"],
|
|
160
|
+
"operation",
|
|
161
|
+
);
|
|
162
|
+
str(op.formId, "formId", 1, 120);
|
|
163
|
+
str(op.destination, "destination", 1, 200);
|
|
164
|
+
if (
|
|
165
|
+
!Array.isArray(op.fields) ||
|
|
166
|
+
op.fields.length < 1 ||
|
|
167
|
+
op.fields.length > 20
|
|
168
|
+
)
|
|
169
|
+
fail("fields");
|
|
170
|
+
const ids = new Set();
|
|
171
|
+
for (const field of op.fields) {
|
|
172
|
+
object(field, "field");
|
|
173
|
+
exact(
|
|
174
|
+
field,
|
|
175
|
+
["id", "label", "type", "required", "value", "source"],
|
|
176
|
+
"field",
|
|
177
|
+
);
|
|
178
|
+
pattern(field.id, FIELD, "field.id");
|
|
179
|
+
if (ids.has(field.id)) fail("duplicate field");
|
|
180
|
+
ids.add(field.id);
|
|
181
|
+
str(field.label, "field.label", 1, 120);
|
|
182
|
+
if (!["text", "email", "tel", "textarea"].includes(field.type))
|
|
183
|
+
fail("field.type");
|
|
184
|
+
if (typeof field.required !== "boolean") fail("field.required");
|
|
185
|
+
str(field.value, "field.value", 0, 1000);
|
|
186
|
+
if (!["user", "record", "suggestion"].includes(field.source))
|
|
187
|
+
fail("field.source");
|
|
188
|
+
}
|
|
189
|
+
if (op.declaration !== null) str(op.declaration, "declaration", 1, 120);
|
|
190
|
+
return op;
|
|
191
|
+
}
|
|
192
|
+
if (op.kind === "ixo.bank.send.v1") {
|
|
193
|
+
exact(
|
|
194
|
+
op,
|
|
195
|
+
[
|
|
196
|
+
"kind",
|
|
197
|
+
"chainId",
|
|
198
|
+
"network",
|
|
199
|
+
"fromAddress",
|
|
200
|
+
"toAddress",
|
|
201
|
+
"amountBaseUnits",
|
|
202
|
+
"denom",
|
|
203
|
+
"feeBaseUnits",
|
|
204
|
+
"memo",
|
|
205
|
+
],
|
|
206
|
+
"operation",
|
|
207
|
+
);
|
|
208
|
+
str(op.chainId, "chainId", 1, 100);
|
|
209
|
+
// Every ixo network is expressible; a Hub executes only its own chain.
|
|
210
|
+
if (!NETWORKS.includes(op.network)) fail("network");
|
|
211
|
+
pattern(op.fromAddress, ADDRESS, "fromAddress");
|
|
212
|
+
pattern(op.toAddress, ADDRESS, "toAddress");
|
|
213
|
+
if (op.fromAddress === op.toAddress) fail("self transfer");
|
|
214
|
+
pattern(op.amountBaseUnits, INT, "amountBaseUnits");
|
|
215
|
+
if (op.amountBaseUnits === "0") fail("amountBaseUnits");
|
|
216
|
+
// Any bank denom; absent means uixo (0.1/0.2 callers).
|
|
217
|
+
if (op.denom !== undefined) pattern(op.denom, DENOM, "denom");
|
|
218
|
+
// Optional and ignored by the Hub, which simulates and sets the fee.
|
|
219
|
+
if (op.feeBaseUnits !== undefined)
|
|
220
|
+
pattern(op.feeBaseUnits, INT, "feeBaseUnits");
|
|
221
|
+
str(op.memo, "memo", 0, 256);
|
|
222
|
+
// Cosmos SDK x/auth max_memo_characters counts UTF-8 bytes, not UTF-16 units.
|
|
223
|
+
if (new TextEncoder().encode(op.memo).length > MEMO_BYTES) fail("memo");
|
|
224
|
+
return op.denom === undefined ? { ...op, denom: "uixo" } : op;
|
|
225
|
+
}
|
|
226
|
+
if (op.kind === "resource.read.v1") {
|
|
227
|
+
exact(
|
|
228
|
+
op,
|
|
229
|
+
["kind", "resourceId", "ownerDid", "url", "version", "contentDigest"],
|
|
230
|
+
"operation",
|
|
231
|
+
);
|
|
232
|
+
str(op.resourceId, "resourceId", 1, 200);
|
|
233
|
+
str(op.ownerDid, "ownerDid", 1, 200);
|
|
234
|
+
const url = new URL(str(op.url, "resource.url", 1, 2000));
|
|
235
|
+
if (url.protocol !== "https:" || url.username || url.password || url.hash)
|
|
236
|
+
fail("resource.url");
|
|
237
|
+
str(op.version, "resource.version", 1, 200);
|
|
238
|
+
pattern(op.contentDigest, HEX, "resource.contentDigest");
|
|
239
|
+
return op;
|
|
240
|
+
}
|
|
241
|
+
return fail("operation.kind");
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function parseReference(input) {
|
|
245
|
+
const ref = object(input, "reference");
|
|
246
|
+
exact(
|
|
247
|
+
ref,
|
|
248
|
+
[
|
|
249
|
+
"version",
|
|
250
|
+
"id",
|
|
251
|
+
"accountId",
|
|
252
|
+
"bindingId",
|
|
253
|
+
"bindingRevision",
|
|
254
|
+
"revision",
|
|
255
|
+
"digest",
|
|
256
|
+
"url",
|
|
257
|
+
"expiresAt",
|
|
258
|
+
"title",
|
|
259
|
+
],
|
|
260
|
+
"reference",
|
|
261
|
+
);
|
|
262
|
+
if (ref.version !== "ixo.auth-action/1") fail("reference.version");
|
|
263
|
+
pattern(ref.id, ID, "reference.id");
|
|
264
|
+
str(ref.accountId, "reference.accountId", 1, 100);
|
|
265
|
+
str(ref.bindingId, "reference.bindingId", 1, 100);
|
|
266
|
+
num(ref.bindingRevision, "reference.bindingRevision");
|
|
267
|
+
num(ref.revision, "reference.revision");
|
|
268
|
+
pattern(ref.digest, HEX, "reference.digest");
|
|
269
|
+
const url = new URL(str(ref.url, "reference.url", 1, 2000));
|
|
270
|
+
if (
|
|
271
|
+
!(
|
|
272
|
+
url.protocol === "https:" ||
|
|
273
|
+
(url.protocol === "http:" && url.hostname === "localhost")
|
|
274
|
+
) ||
|
|
275
|
+
url.username ||
|
|
276
|
+
url.password ||
|
|
277
|
+
url.search ||
|
|
278
|
+
url.hash ||
|
|
279
|
+
url.pathname !== `/a/${ref.id}`
|
|
280
|
+
)
|
|
281
|
+
fail("reference.url");
|
|
282
|
+
num(ref.expiresAt, "reference.expiresAt");
|
|
283
|
+
str(ref.title, "reference.title", 1, 80);
|
|
284
|
+
return ref;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function assertHubReferenceUrl(input, configuredOrigin) {
|
|
288
|
+
const ref = parseReference(input);
|
|
289
|
+
const configured = new URL(configuredOrigin);
|
|
290
|
+
if (
|
|
291
|
+
!(
|
|
292
|
+
configured.protocol === "https:" ||
|
|
293
|
+
(configured.protocol === "http:" && configured.hostname === "localhost")
|
|
294
|
+
) ||
|
|
295
|
+
configured.pathname !== "/" ||
|
|
296
|
+
configured.search ||
|
|
297
|
+
configured.hash ||
|
|
298
|
+
new URL(ref.url).origin !== configured.origin
|
|
299
|
+
)
|
|
300
|
+
fail("reference.url.origin");
|
|
301
|
+
return ref;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export function parseReceipt(input) {
|
|
305
|
+
const receipt = object(input, "receipt");
|
|
306
|
+
if (p2ReceiptFields[receipt.kind]) return parseP2Receipt(receipt);
|
|
307
|
+
if (receipt.kind === "form.submission.v1") {
|
|
308
|
+
exact(
|
|
309
|
+
receipt,
|
|
310
|
+
[
|
|
311
|
+
"kind",
|
|
312
|
+
"actionId",
|
|
313
|
+
"revision",
|
|
314
|
+
"digest",
|
|
315
|
+
"valuesDigest",
|
|
316
|
+
"submittedAt",
|
|
317
|
+
"submissionId",
|
|
318
|
+
],
|
|
319
|
+
"receipt",
|
|
320
|
+
);
|
|
321
|
+
pattern(receipt.actionId, ID, "receipt.actionId");
|
|
322
|
+
num(receipt.revision, "receipt.revision");
|
|
323
|
+
pattern(receipt.digest, HEX, "receipt.digest");
|
|
324
|
+
pattern(receipt.valuesDigest, HEX, "receipt.valuesDigest");
|
|
325
|
+
num(receipt.submittedAt, "receipt.submittedAt");
|
|
326
|
+
str(receipt.submissionId, "receipt.submissionId", 1, 100);
|
|
327
|
+
return receipt;
|
|
328
|
+
}
|
|
329
|
+
if (receipt.kind === "ixo.bank.send.v1") {
|
|
330
|
+
exact(
|
|
331
|
+
receipt,
|
|
332
|
+
[
|
|
333
|
+
"kind",
|
|
334
|
+
"actionId",
|
|
335
|
+
"revision",
|
|
336
|
+
"digest",
|
|
337
|
+
"chainId",
|
|
338
|
+
"txHash",
|
|
339
|
+
"height",
|
|
340
|
+
"code",
|
|
341
|
+
"reason",
|
|
342
|
+
"denom",
|
|
343
|
+
"amountBaseUnits",
|
|
344
|
+
],
|
|
345
|
+
"receipt",
|
|
346
|
+
);
|
|
347
|
+
// 0.3.0/0.5.0: what moved (optional so earlier receipts still parse).
|
|
348
|
+
if ("denom" in receipt) pattern(receipt.denom, DENOM, "receipt.denom");
|
|
349
|
+
if ("amountBaseUnits" in receipt)
|
|
350
|
+
pattern(receipt.amountBaseUnits, INT, "receipt.amountBaseUnits");
|
|
351
|
+
pattern(receipt.actionId, ID, "receipt.actionId");
|
|
352
|
+
num(receipt.revision, "receipt.revision");
|
|
353
|
+
pattern(receipt.digest, HEX, "receipt.digest");
|
|
354
|
+
str(receipt.chainId, "receipt.chainId", 1, 100);
|
|
355
|
+
pattern(receipt.txHash, HEX, "receipt.txHash");
|
|
356
|
+
if (receipt.height !== null) num(receipt.height, "receipt.height");
|
|
357
|
+
if (
|
|
358
|
+
receipt.code !== null &&
|
|
359
|
+
(!Number.isSafeInteger(receipt.code) || receipt.code < 0)
|
|
360
|
+
)
|
|
361
|
+
fail("receipt.code");
|
|
362
|
+
if ("reason" in receipt) {
|
|
363
|
+
if (
|
|
364
|
+
!BANK_NOT_INCLUDED_REASONS.includes(receipt.reason) ||
|
|
365
|
+
receipt.height !== null ||
|
|
366
|
+
(receipt.reason === "check_tx_rejected" &&
|
|
367
|
+
(receipt.code === null || receipt.code === 0)) ||
|
|
368
|
+
(receipt.reason === "expired_not_included" && receipt.code !== null)
|
|
369
|
+
)
|
|
370
|
+
fail("receipt.reason");
|
|
371
|
+
}
|
|
372
|
+
return receipt;
|
|
373
|
+
}
|
|
374
|
+
return fail("receipt.kind");
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function parseReviewView(input) {
|
|
378
|
+
const view = object(input, "view");
|
|
379
|
+
exact(
|
|
380
|
+
view,
|
|
381
|
+
[
|
|
382
|
+
"reference",
|
|
383
|
+
"requester",
|
|
384
|
+
"operation",
|
|
385
|
+
"reviewStatus",
|
|
386
|
+
"operationStatus",
|
|
387
|
+
"revision",
|
|
388
|
+
"digest",
|
|
389
|
+
"canEdit",
|
|
390
|
+
"canApprove",
|
|
391
|
+
"canSign",
|
|
392
|
+
"receipt",
|
|
393
|
+
"signedTxHash",
|
|
394
|
+
"resourceUrl",
|
|
395
|
+
"chatUrl",
|
|
396
|
+
"service",
|
|
397
|
+
"transfer",
|
|
398
|
+
],
|
|
399
|
+
"view",
|
|
400
|
+
);
|
|
401
|
+
parseReference(view.reference);
|
|
402
|
+
if (view.service !== undefined && view.service !== null) {
|
|
403
|
+
object(view.service, "view.service");
|
|
404
|
+
exact(
|
|
405
|
+
view.service,
|
|
406
|
+
["continuationUrl", "accountLabel", "resource"],
|
|
407
|
+
"view.service",
|
|
408
|
+
);
|
|
409
|
+
nullableUrl(view.service.continuationUrl);
|
|
410
|
+
nullableString(view.service.accountLabel, "accountLabel", 200);
|
|
411
|
+
if (view.service.resource !== null)
|
|
412
|
+
parseResourceDescriptor(view.service.resource);
|
|
413
|
+
}
|
|
414
|
+
if (view.transfer !== undefined && view.transfer !== null)
|
|
415
|
+
parseTransferDisplay(view.transfer);
|
|
416
|
+
str(view.requester, "view.requester", 1, 120);
|
|
417
|
+
parseOperation(view.operation);
|
|
418
|
+
if (
|
|
419
|
+
![
|
|
420
|
+
"prepared",
|
|
421
|
+
"superseded",
|
|
422
|
+
"declined",
|
|
423
|
+
"expired",
|
|
424
|
+
"revoked",
|
|
425
|
+
"signing",
|
|
426
|
+
"authorized",
|
|
427
|
+
].includes(view.reviewStatus)
|
|
428
|
+
)
|
|
429
|
+
fail("view.reviewStatus");
|
|
430
|
+
if (
|
|
431
|
+
![
|
|
432
|
+
"awaiting_authorization",
|
|
433
|
+
"admitted",
|
|
434
|
+
"submitted",
|
|
435
|
+
"succeeded",
|
|
436
|
+
"failed",
|
|
437
|
+
"requires_review",
|
|
438
|
+
"unknown",
|
|
439
|
+
"not_started",
|
|
440
|
+
].includes(view.operationStatus)
|
|
441
|
+
)
|
|
442
|
+
fail("view.operationStatus");
|
|
443
|
+
num(view.revision, "view.revision");
|
|
444
|
+
pattern(view.digest, HEX, "view.digest");
|
|
445
|
+
for (const key of ["canEdit", "canApprove", "canSign"])
|
|
446
|
+
if (typeof view[key] !== "boolean") fail(`view.${key}`);
|
|
447
|
+
if (view.receipt !== null) {
|
|
448
|
+
const receipt = parseReceipt(view.receipt);
|
|
449
|
+
if (
|
|
450
|
+
receipt.actionId !== view.reference.id ||
|
|
451
|
+
receipt.revision !== view.revision ||
|
|
452
|
+
receipt.digest !== view.digest
|
|
453
|
+
)
|
|
454
|
+
fail("view.receipt");
|
|
455
|
+
assertReceiptForOperation(view.operation, receipt, view.operationStatus);
|
|
456
|
+
if (
|
|
457
|
+
view.operation.kind === "form.submit.v1" &&
|
|
458
|
+
receipt.kind !== "form.submission.v1"
|
|
459
|
+
)
|
|
460
|
+
fail("view.receipt");
|
|
461
|
+
if (
|
|
462
|
+
view.operation.kind === "ixo.bank.send.v1" &&
|
|
463
|
+
receipt.kind !== "ixo.bank.send.v1"
|
|
464
|
+
)
|
|
465
|
+
fail("view.receipt");
|
|
466
|
+
if (!receiptFitsStatus(receipt, view.operationStatus)) fail("view.receipt");
|
|
467
|
+
}
|
|
468
|
+
if (
|
|
469
|
+
["succeeded", "failed"].includes(view.operationStatus) &&
|
|
470
|
+
view.receipt === null
|
|
471
|
+
)
|
|
472
|
+
fail("view.receipt");
|
|
473
|
+
// `not_started`: terminal, nothing was executed (for example a decline).
|
|
474
|
+
if (view.operationStatus === "not_started" && view.receipt !== null)
|
|
475
|
+
fail("view.receipt");
|
|
476
|
+
if (view.signedTxHash !== null)
|
|
477
|
+
pattern(view.signedTxHash, HEX, "view.signedTxHash");
|
|
478
|
+
if (view.resourceUrl !== null) {
|
|
479
|
+
const url = new URL(str(view.resourceUrl, "view.resourceUrl", 1, 2000));
|
|
480
|
+
if (
|
|
481
|
+
!(
|
|
482
|
+
url.protocol === "https:" ||
|
|
483
|
+
(url.protocol === "http:" && url.hostname === "localhost")
|
|
484
|
+
) ||
|
|
485
|
+
url.username ||
|
|
486
|
+
url.password ||
|
|
487
|
+
url.hash
|
|
488
|
+
)
|
|
489
|
+
fail("view.resourceUrl");
|
|
490
|
+
}
|
|
491
|
+
const chat = new URL(str(view.chatUrl, "view.chatUrl", 1, 2000));
|
|
492
|
+
if (chat.protocol !== "https:" || chat.username || chat.password)
|
|
493
|
+
fail("view.chatUrl");
|
|
494
|
+
return view;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
export function parseOutcome(input) {
|
|
498
|
+
const outcome = object(input, "outcome");
|
|
499
|
+
exact(
|
|
500
|
+
outcome,
|
|
501
|
+
[
|
|
502
|
+
"version",
|
|
503
|
+
"eventId",
|
|
504
|
+
"reference",
|
|
505
|
+
"ownerDid",
|
|
506
|
+
"authorizedRevision",
|
|
507
|
+
"authorizedDigest",
|
|
508
|
+
"sequence",
|
|
509
|
+
"status",
|
|
510
|
+
"receipt",
|
|
511
|
+
"occurredAt",
|
|
512
|
+
],
|
|
513
|
+
"outcome",
|
|
514
|
+
);
|
|
515
|
+
if (![OUTCOME_VERSION_V1, OUTCOME_VERSION_V2].includes(outcome.version))
|
|
516
|
+
fail("outcome.version");
|
|
517
|
+
const v2 = outcome.version === OUTCOME_VERSION_V2;
|
|
518
|
+
str(outcome.eventId, "outcome.eventId", 1, 100);
|
|
519
|
+
const ref = parseReference(outcome.reference);
|
|
520
|
+
str(outcome.ownerDid, "outcome.ownerDid", 1, 200);
|
|
521
|
+
num(outcome.authorizedRevision, "outcome.authorizedRevision");
|
|
522
|
+
pattern(outcome.authorizedDigest, HEX, "outcome.authorizedDigest");
|
|
523
|
+
num(outcome.sequence, "outcome.sequence");
|
|
524
|
+
if (
|
|
525
|
+
!V1_OUTCOME_STATUSES.includes(outcome.status) &&
|
|
526
|
+
!(v2 && V2_ONLY_OUTCOME_STATUSES.includes(outcome.status))
|
|
527
|
+
)
|
|
528
|
+
fail("outcome.status");
|
|
529
|
+
if (
|
|
530
|
+
["succeeded", "failed"].includes(outcome.status) &&
|
|
531
|
+
outcome.receipt === null
|
|
532
|
+
)
|
|
533
|
+
fail("outcome.receipt");
|
|
534
|
+
if (outcome.receipt !== null) {
|
|
535
|
+
const receipt = parseReceipt(outcome.receipt);
|
|
536
|
+
if (
|
|
537
|
+
receipt.actionId !== ref.id ||
|
|
538
|
+
receipt.revision !== outcome.authorizedRevision ||
|
|
539
|
+
receipt.digest !== outcome.authorizedDigest
|
|
540
|
+
)
|
|
541
|
+
fail("outcome.receipt");
|
|
542
|
+
if (!receiptFitsStatus(receipt, outcome.status)) fail("outcome.receipt");
|
|
543
|
+
// v1 envelopes keep the exact 0.4.1 receipt shape.
|
|
544
|
+
if (
|
|
545
|
+
!v2 &&
|
|
546
|
+
("reason" in receipt ||
|
|
547
|
+
"denom" in receipt ||
|
|
548
|
+
"amountBaseUnits" in receipt ||
|
|
549
|
+
!V1_RECEIPT_KINDS.includes(receipt.kind))
|
|
550
|
+
)
|
|
551
|
+
fail("outcome.receipt");
|
|
552
|
+
}
|
|
553
|
+
if (
|
|
554
|
+
[
|
|
555
|
+
"authorized",
|
|
556
|
+
"declined",
|
|
557
|
+
"submitted",
|
|
558
|
+
"unknown",
|
|
559
|
+
...V2_ONLY_OUTCOME_STATUSES,
|
|
560
|
+
].includes(outcome.status) &&
|
|
561
|
+
outcome.receipt !== null
|
|
562
|
+
)
|
|
563
|
+
fail("outcome.receipt");
|
|
564
|
+
num(outcome.occurredAt, "outcome.occurredAt");
|
|
565
|
+
return outcome;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
export function parseCapabilities(input) {
|
|
569
|
+
const data = object(input, "capabilities");
|
|
570
|
+
exact(
|
|
571
|
+
data,
|
|
572
|
+
[
|
|
573
|
+
"version",
|
|
574
|
+
"accountId",
|
|
575
|
+
"oracleDid",
|
|
576
|
+
"network",
|
|
577
|
+
"resourceOrigin",
|
|
578
|
+
"operations",
|
|
579
|
+
"forms",
|
|
580
|
+
"transfer",
|
|
581
|
+
],
|
|
582
|
+
"capabilities",
|
|
583
|
+
);
|
|
584
|
+
if (
|
|
585
|
+
![CAPABILITIES_VERSION_V1, CAPABILITIES_VERSION_V2].includes(data.version)
|
|
586
|
+
)
|
|
587
|
+
fail("capabilities.version");
|
|
588
|
+
str(data.accountId, "capabilities.accountId", 1, 100);
|
|
589
|
+
str(data.oracleDid, "capabilities.oracleDid", 1, 200);
|
|
590
|
+
if (
|
|
591
|
+
data.network !== null &&
|
|
592
|
+
!NETWORKS.includes(data.network)
|
|
593
|
+
)
|
|
594
|
+
fail("capabilities.network");
|
|
595
|
+
if (data.resourceOrigin !== null) {
|
|
596
|
+
const url = new URL(
|
|
597
|
+
str(data.resourceOrigin, "capabilities.resourceOrigin", 1, 2000),
|
|
598
|
+
);
|
|
599
|
+
if (
|
|
600
|
+
url.protocol !== "https:" ||
|
|
601
|
+
url.pathname !== "/" ||
|
|
602
|
+
url.search ||
|
|
603
|
+
url.hash
|
|
604
|
+
)
|
|
605
|
+
fail("capabilities.resourceOrigin");
|
|
606
|
+
}
|
|
607
|
+
if (
|
|
608
|
+
!Array.isArray(data.operations) ||
|
|
609
|
+
new Set(data.operations).size !== data.operations.length ||
|
|
610
|
+
data.operations.some(
|
|
611
|
+
(kind) =>
|
|
612
|
+
!(
|
|
613
|
+
data.version === CAPABILITIES_VERSION_V1
|
|
614
|
+
? V1_OPERATION_KINDS
|
|
615
|
+
: operationKinds
|
|
616
|
+
).includes(kind),
|
|
617
|
+
)
|
|
618
|
+
)
|
|
619
|
+
fail("capabilities.operations");
|
|
620
|
+
if (data.transfer !== null) {
|
|
621
|
+
object(data.transfer, "capabilities.transfer");
|
|
622
|
+
if ("limits" in data.transfer) {
|
|
623
|
+
// 0.3.0/0.5.0 shape (callers that declare ?version=3): optional
|
|
624
|
+
// per-denom amount caps; empty = no cap. The Hub sets the fee.
|
|
625
|
+
exact(data.transfer, ["limits", "maxFeeUixo"], "capabilities.transfer");
|
|
626
|
+
if (
|
|
627
|
+
!Array.isArray(data.transfer.limits) ||
|
|
628
|
+
data.transfer.limits.length > 100
|
|
629
|
+
)
|
|
630
|
+
fail("capabilities.transfer.limits");
|
|
631
|
+
const denoms = new Set();
|
|
632
|
+
for (const limit of data.transfer.limits) {
|
|
633
|
+
object(limit, "capabilities.transfer.limit");
|
|
634
|
+
exact(limit, ["denom", "maxAmountBaseUnits"], "capabilities.transfer.limit");
|
|
635
|
+
pattern(limit.denom, DENOM, "capabilities.transfer.limit.denom");
|
|
636
|
+
pattern(
|
|
637
|
+
limit.maxAmountBaseUnits,
|
|
638
|
+
/^[1-9][0-9]{0,39}$/,
|
|
639
|
+
"capabilities.transfer.limit.maxAmountBaseUnits",
|
|
640
|
+
);
|
|
641
|
+
if (denoms.has(limit.denom)) fail("capabilities.transfer.limit.denom");
|
|
642
|
+
denoms.add(limit.denom);
|
|
643
|
+
}
|
|
644
|
+
if (data.transfer.maxFeeUixo !== undefined)
|
|
645
|
+
pattern(
|
|
646
|
+
data.transfer.maxFeeUixo,
|
|
647
|
+
/^[1-9][0-9]{0,19}$/,
|
|
648
|
+
"capabilities.transfer.maxFeeUixo",
|
|
649
|
+
);
|
|
650
|
+
} else {
|
|
651
|
+
// Legacy 0.1/0.2 shape.
|
|
652
|
+
exact(
|
|
653
|
+
data.transfer,
|
|
654
|
+
["maxAmountUixo", "maxFeeUixo"],
|
|
655
|
+
"capabilities.transfer",
|
|
656
|
+
);
|
|
657
|
+
pattern(
|
|
658
|
+
data.transfer.maxAmountUixo,
|
|
659
|
+
/^[1-9][0-9]{0,19}$/,
|
|
660
|
+
"capabilities.transfer.maxAmountUixo",
|
|
661
|
+
);
|
|
662
|
+
pattern(
|
|
663
|
+
data.transfer.maxFeeUixo,
|
|
664
|
+
/^[1-9][0-9]{0,19}$/,
|
|
665
|
+
"capabilities.transfer.maxFeeUixo",
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
if (data.operations.includes("ixo.bank.send.v1") && data.transfer === null)
|
|
670
|
+
fail("capabilities.transfer");
|
|
671
|
+
if (!Array.isArray(data.forms)) fail("capabilities.forms");
|
|
672
|
+
for (const form of data.forms) {
|
|
673
|
+
object(form, "capabilities.form");
|
|
674
|
+
exact(
|
|
675
|
+
form,
|
|
676
|
+
["formId", "destination", "fields", "declaration"],
|
|
677
|
+
"capabilities.form",
|
|
678
|
+
);
|
|
679
|
+
str(form.formId, "formId", 1, 120);
|
|
680
|
+
str(form.destination, "destination", 1, 200);
|
|
681
|
+
if (form.declaration !== null) str(form.declaration, "declaration", 1, 120);
|
|
682
|
+
if (
|
|
683
|
+
!Array.isArray(form.fields) ||
|
|
684
|
+
form.fields.length < 1 ||
|
|
685
|
+
form.fields.length > 20
|
|
686
|
+
)
|
|
687
|
+
fail("form.fields");
|
|
688
|
+
const ids = new Set();
|
|
689
|
+
for (const field of form.fields) {
|
|
690
|
+
object(field, "field");
|
|
691
|
+
exact(field, ["id", "label", "type", "required"], "field");
|
|
692
|
+
pattern(field.id, FIELD, "field.id");
|
|
693
|
+
str(field.label, "field.label", 1, 120);
|
|
694
|
+
if (
|
|
695
|
+
!["text", "email", "tel", "textarea"].includes(field.type) ||
|
|
696
|
+
typeof field.required !== "boolean" ||
|
|
697
|
+
ids.has(field.id)
|
|
698
|
+
)
|
|
699
|
+
fail("field");
|
|
700
|
+
ids.add(field.id);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return data;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
const utf8 = (value, field) => {
|
|
707
|
+
str(value, field, 0, 8192);
|
|
708
|
+
if (
|
|
709
|
+
new TextEncoder().encode(value).length > 8192 ||
|
|
710
|
+
/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/u.test(
|
|
711
|
+
value,
|
|
712
|
+
)
|
|
713
|
+
)
|
|
714
|
+
fail(field);
|
|
715
|
+
return value;
|
|
716
|
+
};
|
|
717
|
+
const nullableString = (v, f, max = 200) =>
|
|
718
|
+
v === null ? null : str(v, f, 1, max);
|
|
719
|
+
const urlValue = (v) => {
|
|
720
|
+
const u = new URL(str(v, "url", 1, 2000));
|
|
721
|
+
if (
|
|
722
|
+
(u.protocol !== "https:" &&
|
|
723
|
+
!(
|
|
724
|
+
u.protocol === "http:" &&
|
|
725
|
+
["localhost", "127.0.0.1"].includes(u.hostname)
|
|
726
|
+
)) ||
|
|
727
|
+
u.username ||
|
|
728
|
+
u.password ||
|
|
729
|
+
u.hash
|
|
730
|
+
)
|
|
731
|
+
fail("url");
|
|
732
|
+
return v;
|
|
733
|
+
};
|
|
734
|
+
const nullableUrl = (v) => (v === null ? null : urlValue(v));
|
|
735
|
+
const scopeList = (v) => {
|
|
736
|
+
if (!Array.isArray(v) || v.length > 30 || new Set(v).size !== v.length)
|
|
737
|
+
fail("scopes");
|
|
738
|
+
for (const s of v) str(s, "scope", 1, 300);
|
|
739
|
+
return v;
|
|
740
|
+
};
|
|
741
|
+
const tuple = (v) => {
|
|
742
|
+
pattern(v.actionId, ID, "actionId");
|
|
743
|
+
num(v.revision, "revision");
|
|
744
|
+
pattern(v.digest, HEX, "digest");
|
|
745
|
+
};
|
|
746
|
+
export function privateTextPath(actionId, filename) {
|
|
747
|
+
pattern(actionId, ID, "actionId");
|
|
748
|
+
if (
|
|
749
|
+
typeof filename !== "string" ||
|
|
750
|
+
filename.length > 80 ||
|
|
751
|
+
!/^[A-Za-z0-9][A-Za-z0-9 _.-]*\.txt$/.test(filename) ||
|
|
752
|
+
filename.includes("..") ||
|
|
753
|
+
filename.endsWith(" .txt")
|
|
754
|
+
)
|
|
755
|
+
fail("filename");
|
|
756
|
+
return `/${actionId}-${filename}`;
|
|
757
|
+
}
|
|
758
|
+
export function serviceForOperation(op) {
|
|
759
|
+
const kind = typeof op === "string" ? op : op.kind;
|
|
760
|
+
return kind.startsWith("gmail.")
|
|
761
|
+
? "gmail"
|
|
762
|
+
: kind.startsWith("calendar.")
|
|
763
|
+
? "calendar"
|
|
764
|
+
: kind.startsWith("vfs.")
|
|
765
|
+
? "vfs"
|
|
766
|
+
: null;
|
|
767
|
+
}
|
|
768
|
+
const receiptKinds = {
|
|
769
|
+
"form.submit.v1": "form.submission.v1",
|
|
770
|
+
"ixo.bank.send.v1": "ixo.bank.send.v1",
|
|
771
|
+
"statement.sign.v1": "statement.signed.v1",
|
|
772
|
+
"gmail.connect.v1": "gmail.connected.v1",
|
|
773
|
+
"gmail.disconnect.v1": "gmail.disconnected.v1",
|
|
774
|
+
"gmail.message.send.v1": "gmail.message.accepted.v1",
|
|
775
|
+
"calendar.connect.v1": "calendar.connected.v1",
|
|
776
|
+
"calendar.disconnect.v1": "calendar.disconnected.v1",
|
|
777
|
+
"calendar.event.create.v1": "calendar.event.created.v1",
|
|
778
|
+
"vfs.access.request.v1": "vfs.access.requested.v1",
|
|
779
|
+
"vfs.text.save.v1": "vfs.text.saved.v1",
|
|
780
|
+
};
|
|
781
|
+
export function assertReceiptForOperation(
|
|
782
|
+
operation,
|
|
783
|
+
receipt,
|
|
784
|
+
status = "succeeded",
|
|
785
|
+
) {
|
|
786
|
+
parseReceipt(receipt);
|
|
787
|
+
if (
|
|
788
|
+
["succeeded", "failed"].includes(status) &&
|
|
789
|
+
!receiptFitsStatus(receipt, status)
|
|
790
|
+
)
|
|
791
|
+
fail("operation receipt");
|
|
792
|
+
if (
|
|
793
|
+
status === "failed" &&
|
|
794
|
+
receipt.kind === "service.failure.v1" &&
|
|
795
|
+
receipt.service === serviceForOperation(operation)
|
|
796
|
+
)
|
|
797
|
+
return receipt;
|
|
798
|
+
if (
|
|
799
|
+
receiptKinds[operation.kind] !== receipt.kind ||
|
|
800
|
+
receipt.kind === "service.failure.v1"
|
|
801
|
+
)
|
|
802
|
+
fail("operation receipt");
|
|
803
|
+
// Every kind: the receipt must name the approved connection / target.
|
|
804
|
+
if (
|
|
805
|
+
"connectionId" in operation &&
|
|
806
|
+
operation.connectionId !== null &&
|
|
807
|
+
"connectionId" in receipt &&
|
|
808
|
+
receipt.connectionId !== operation.connectionId
|
|
809
|
+
)
|
|
810
|
+
fail("receipt connection");
|
|
811
|
+
if (
|
|
812
|
+
"targetId" in operation &&
|
|
813
|
+
"targetId" in receipt &&
|
|
814
|
+
receipt.targetId !== operation.targetId
|
|
815
|
+
)
|
|
816
|
+
fail("receipt target");
|
|
817
|
+
return receipt;
|
|
818
|
+
}
|
|
819
|
+
const p2ReceiptFields = {
|
|
820
|
+
"statement.signed.v1": [
|
|
821
|
+
"statementDigest",
|
|
822
|
+
"proofDigest",
|
|
823
|
+
"signerDid",
|
|
824
|
+
"signedAt",
|
|
825
|
+
],
|
|
826
|
+
"gmail.connected.v1": ["connectionId", "scopesDigest", "verifiedAt"],
|
|
827
|
+
"gmail.disconnected.v1": [
|
|
828
|
+
"connectionId",
|
|
829
|
+
"localRevokedAt",
|
|
830
|
+
"providerDisconnected",
|
|
831
|
+
"verifiedAt",
|
|
832
|
+
],
|
|
833
|
+
"gmail.message.accepted.v1": [
|
|
834
|
+
"connectionId",
|
|
835
|
+
"messageId",
|
|
836
|
+
"contentDigest",
|
|
837
|
+
"mimeDigest",
|
|
838
|
+
"acceptedAt",
|
|
839
|
+
],
|
|
840
|
+
"calendar.connected.v1": ["connectionId", "scopesDigest", "verifiedAt"],
|
|
841
|
+
"calendar.disconnected.v1": [
|
|
842
|
+
"connectionId",
|
|
843
|
+
"localRevokedAt",
|
|
844
|
+
"providerDisconnected",
|
|
845
|
+
"verifiedAt",
|
|
846
|
+
],
|
|
847
|
+
"calendar.event.created.v1": [
|
|
848
|
+
"connectionId",
|
|
849
|
+
"eventId",
|
|
850
|
+
"eventDigest",
|
|
851
|
+
"verifiedAt",
|
|
852
|
+
],
|
|
853
|
+
"vfs.access.requested.v1": ["requestId", "targetId", "state", "requestedAt"],
|
|
854
|
+
"vfs.text.saved.v1": [
|
|
855
|
+
"fileId",
|
|
856
|
+
"cid",
|
|
857
|
+
"version",
|
|
858
|
+
"contentDigest",
|
|
859
|
+
"private",
|
|
860
|
+
"verifiedAt",
|
|
861
|
+
],
|
|
862
|
+
"service.failure.v1": ["service", "code", "failedAt"],
|
|
863
|
+
};
|
|
864
|
+
function parseP2Receipt(v) {
|
|
865
|
+
exact(
|
|
866
|
+
v,
|
|
867
|
+
["kind", "actionId", "revision", "digest", ...p2ReceiptFields[v.kind]],
|
|
868
|
+
"receipt",
|
|
869
|
+
);
|
|
870
|
+
tuple(v);
|
|
871
|
+
for (const k of p2ReceiptFields[v.kind]) {
|
|
872
|
+
if (k.endsWith("Digest")) pattern(v[k], HEX, k);
|
|
873
|
+
else if (k.endsWith("At") || k === "version") num(v[k], k);
|
|
874
|
+
else if (k === "private") {
|
|
875
|
+
if (v[k] !== true) fail(k);
|
|
876
|
+
} else if (k === "providerDisconnected") {
|
|
877
|
+
if (v[k] !== null && typeof v[k] !== "boolean") fail(k);
|
|
878
|
+
} else str(v[k], k, 1, k === "cid" ? 200 : 200);
|
|
879
|
+
}
|
|
880
|
+
if (v.kind === "vfs.access.requested.v1" && v.state !== "pending_owner")
|
|
881
|
+
fail("state");
|
|
882
|
+
if (
|
|
883
|
+
v.kind === "service.failure.v1" &&
|
|
884
|
+
(!["calendar", "gmail", "vfs"].includes(v.service) ||
|
|
885
|
+
![
|
|
886
|
+
"permission_denied",
|
|
887
|
+
"changed",
|
|
888
|
+
"rejected",
|
|
889
|
+
"conflict",
|
|
890
|
+
// 0.4.0: expired before anything was sent; service unavailable.
|
|
891
|
+
"expired",
|
|
892
|
+
"unavailable",
|
|
893
|
+
].includes(
|
|
894
|
+
v.code,
|
|
895
|
+
))
|
|
896
|
+
)
|
|
897
|
+
fail("service failure");
|
|
898
|
+
return v;
|
|
899
|
+
}
|
|
900
|
+
export function parseResourceDescriptor(input) {
|
|
901
|
+
const v = object(input, "resource");
|
|
902
|
+
exact(
|
|
903
|
+
v,
|
|
904
|
+
[
|
|
905
|
+
"id",
|
|
906
|
+
"title",
|
|
907
|
+
"ownerDid",
|
|
908
|
+
"url",
|
|
909
|
+
"version",
|
|
910
|
+
"contentDigest",
|
|
911
|
+
"cid",
|
|
912
|
+
"fileId",
|
|
913
|
+
],
|
|
914
|
+
"resource",
|
|
915
|
+
);
|
|
916
|
+
for (const k of ["id", "title", "ownerDid", "version", "cid", "fileId"])
|
|
917
|
+
str(v[k], k, 1, 200);
|
|
918
|
+
pattern(v.contentDigest, HEX, "contentDigest");
|
|
919
|
+
urlValue(v.url);
|
|
920
|
+
if (new URL(v.url).search) fail("resource.url");
|
|
921
|
+
return v;
|
|
922
|
+
}
|
|
923
|
+
export function parseServiceAuthorization(input) {
|
|
924
|
+
const v = object(input, "authorization");
|
|
925
|
+
exact(
|
|
926
|
+
v,
|
|
927
|
+
[
|
|
928
|
+
"version",
|
|
929
|
+
"mode",
|
|
930
|
+
"service",
|
|
931
|
+
"hubOrigin",
|
|
932
|
+
"accountId",
|
|
933
|
+
"actionId",
|
|
934
|
+
"ownerDid",
|
|
935
|
+
"oracleDid",
|
|
936
|
+
"bindingId",
|
|
937
|
+
"bindingRevision",
|
|
938
|
+
"consentVersion",
|
|
939
|
+
"policyVersion",
|
|
940
|
+
"revision",
|
|
941
|
+
"digest",
|
|
942
|
+
"network",
|
|
943
|
+
"operation",
|
|
944
|
+
"issuedAt",
|
|
945
|
+
"expiresAt",
|
|
946
|
+
"nonce",
|
|
947
|
+
],
|
|
948
|
+
"authorization",
|
|
949
|
+
);
|
|
950
|
+
if (
|
|
951
|
+
v.version !== "ixo.auth-authorization/1" ||
|
|
952
|
+
!["prepare", "execute"].includes(v.mode) ||
|
|
953
|
+
!["calendar", "gmail", "vfs"].includes(v.service) ||
|
|
954
|
+
!SERVICE_NETWORKS.includes(v.network)
|
|
955
|
+
)
|
|
956
|
+
fail("authorization");
|
|
957
|
+
tuple(v);
|
|
958
|
+
for (const k of ["accountId", "bindingId", "consentVersion", "nonce"])
|
|
959
|
+
str(v[k], k, 1, 100);
|
|
960
|
+
for (const k of ["ownerDid", "oracleDid"]) str(v[k], k, 1, 200);
|
|
961
|
+
for (const k of ["bindingRevision", "policyVersion", "issuedAt", "expiresAt"])
|
|
962
|
+
num(v[k], k);
|
|
963
|
+
if (v.expiresAt <= v.issuedAt) fail("expiresAt");
|
|
964
|
+
urlValue(v.hubOrigin);
|
|
965
|
+
if (new URL(v.hubOrigin).origin !== v.hubOrigin) fail("hubOrigin");
|
|
966
|
+
parseOperation(v.operation);
|
|
967
|
+
if (
|
|
968
|
+
serviceForOperation(v.operation) !== v.service ||
|
|
969
|
+
(v.mode === "prepare" &&
|
|
970
|
+
!["calendar.connect.v1", "gmail.connect.v1"].includes(v.operation.kind))
|
|
971
|
+
)
|
|
972
|
+
fail("service operation");
|
|
973
|
+
return v;
|
|
974
|
+
}
|
|
975
|
+
export function parseServiceCommand(input) {
|
|
976
|
+
const v = object(input, "command");
|
|
977
|
+
exact(
|
|
978
|
+
v,
|
|
979
|
+
["authorization", "ownerInvocation", "subscriptionDelegation"],
|
|
980
|
+
"command",
|
|
981
|
+
);
|
|
982
|
+
parseServiceAuthorization(v.authorization);
|
|
983
|
+
str(v.ownerInvocation, "ownerInvocation", 1, 65536);
|
|
984
|
+
if (v.subscriptionDelegation !== null)
|
|
985
|
+
str(v.subscriptionDelegation, "subscriptionDelegation", 1, 65536);
|
|
986
|
+
if (
|
|
987
|
+
["calendar", "gmail"].includes(v.authorization.service) &&
|
|
988
|
+
v.subscriptionDelegation === null
|
|
989
|
+
)
|
|
990
|
+
fail("subscriptionDelegation");
|
|
991
|
+
if (v.authorization.service === "vfs" && v.subscriptionDelegation !== null)
|
|
992
|
+
fail("subscriptionDelegation");
|
|
993
|
+
return v;
|
|
994
|
+
}
|
|
995
|
+
export function parseServicePreparation(input) {
|
|
996
|
+
const v = object(input, "preparation");
|
|
997
|
+
exact(
|
|
998
|
+
v,
|
|
999
|
+
[
|
|
1000
|
+
"version",
|
|
1001
|
+
"accountId",
|
|
1002
|
+
"actionId",
|
|
1003
|
+
"revision",
|
|
1004
|
+
"digest",
|
|
1005
|
+
"status",
|
|
1006
|
+
"operation",
|
|
1007
|
+
"continuationUrl",
|
|
1008
|
+
],
|
|
1009
|
+
"preparation",
|
|
1010
|
+
);
|
|
1011
|
+
if (
|
|
1012
|
+
v.version !== "ixo.auth-preparation/1" ||
|
|
1013
|
+
!["pending", "ready", "unknown"].includes(v.status)
|
|
1014
|
+
)
|
|
1015
|
+
fail("preparation");
|
|
1016
|
+
tuple(v);
|
|
1017
|
+
str(v.accountId, "accountId", 1, 100);
|
|
1018
|
+
parseOperation(v.operation);
|
|
1019
|
+
if (!["calendar.connect.v1", "gmail.connect.v1"].includes(v.operation.kind))
|
|
1020
|
+
fail("preparation operation");
|
|
1021
|
+
nullableUrl(v.continuationUrl);
|
|
1022
|
+
if (
|
|
1023
|
+
v.status === "ready" &&
|
|
1024
|
+
(!v.operation.connectionId ||
|
|
1025
|
+
!v.operation.accountLabel ||
|
|
1026
|
+
!v.operation.scopes.length ||
|
|
1027
|
+
v.continuationUrl !== null)
|
|
1028
|
+
)
|
|
1029
|
+
fail("ready preparation");
|
|
1030
|
+
return v;
|
|
1031
|
+
}
|
|
1032
|
+
export function parseServiceResult(input) {
|
|
1033
|
+
const v = object(input, "service result");
|
|
1034
|
+
exact(
|
|
1035
|
+
v,
|
|
1036
|
+
[
|
|
1037
|
+
"version",
|
|
1038
|
+
"accountId",
|
|
1039
|
+
"actionId",
|
|
1040
|
+
"revision",
|
|
1041
|
+
"digest",
|
|
1042
|
+
"authorizationDigest",
|
|
1043
|
+
"sequence",
|
|
1044
|
+
"status",
|
|
1045
|
+
"receipt",
|
|
1046
|
+
"continuationUrl",
|
|
1047
|
+
"accountLabel",
|
|
1048
|
+
"resource",
|
|
1049
|
+
"checkedAt",
|
|
1050
|
+
],
|
|
1051
|
+
"service result",
|
|
1052
|
+
);
|
|
1053
|
+
if (
|
|
1054
|
+
v.version !== "ixo.auth-service-result/1" ||
|
|
1055
|
+
!["pending", "succeeded", "failed", "unknown"].includes(v.status)
|
|
1056
|
+
)
|
|
1057
|
+
fail("service result");
|
|
1058
|
+
tuple(v);
|
|
1059
|
+
str(v.accountId, "accountId", 1, 100);
|
|
1060
|
+
pattern(v.authorizationDigest, HEX, "authorizationDigest");
|
|
1061
|
+
num(v.sequence, "sequence");
|
|
1062
|
+
num(v.checkedAt, "checkedAt");
|
|
1063
|
+
nullableUrl(v.continuationUrl);
|
|
1064
|
+
nullableString(v.accountLabel, "accountLabel");
|
|
1065
|
+
if (v.resource !== null) parseResourceDescriptor(v.resource);
|
|
1066
|
+
if (["pending", "unknown"].includes(v.status)) {
|
|
1067
|
+
if (v.receipt !== null) fail("receipt");
|
|
1068
|
+
} else {
|
|
1069
|
+
const r = parseReceipt(v.receipt);
|
|
1070
|
+
if (
|
|
1071
|
+
r.actionId !== v.actionId ||
|
|
1072
|
+
r.revision !== v.revision ||
|
|
1073
|
+
r.digest !== v.digest ||
|
|
1074
|
+
(v.status === "failed"
|
|
1075
|
+
? r.kind !== "service.failure.v1"
|
|
1076
|
+
: r.kind === "service.failure.v1")
|
|
1077
|
+
)
|
|
1078
|
+
fail("receipt");
|
|
1079
|
+
}
|
|
1080
|
+
return v;
|
|
1081
|
+
}
|
|
1082
|
+
export function parseActionOptions(input) {
|
|
1083
|
+
const v = object(input, "options");
|
|
1084
|
+
if (
|
|
1085
|
+
!["ixo.auth-options/1", "ixo.auth-options/2", "ixo.auth-options/3"].includes(
|
|
1086
|
+
v.version,
|
|
1087
|
+
)
|
|
1088
|
+
)
|
|
1089
|
+
fail("options.version");
|
|
1090
|
+
// v3 (0.5.0) = v2 + `transfer`; v1/v2 keep their exact 0.4.1 key sets.
|
|
1091
|
+
const v3 = v.version === "ixo.auth-options/3";
|
|
1092
|
+
exact(
|
|
1093
|
+
v,
|
|
1094
|
+
[
|
|
1095
|
+
"version",
|
|
1096
|
+
"accountId",
|
|
1097
|
+
"ownerDid",
|
|
1098
|
+
"bindingId",
|
|
1099
|
+
"bindingRevision",
|
|
1100
|
+
"connections",
|
|
1101
|
+
"targets",
|
|
1102
|
+
"resources",
|
|
1103
|
+
...(v3 ? ["transfer"] : []),
|
|
1104
|
+
],
|
|
1105
|
+
"options",
|
|
1106
|
+
);
|
|
1107
|
+
if (v3 && v.transfer !== null) parseOptionsTransfer(v.transfer);
|
|
1108
|
+
for (const k of ["accountId", "bindingId"]) str(v[k], k, 1, 100);
|
|
1109
|
+
str(v.ownerDid, "ownerDid");
|
|
1110
|
+
num(v.bindingRevision, "bindingRevision");
|
|
1111
|
+
for (const k of ["connections", "targets", "resources"])
|
|
1112
|
+
if (!Array.isArray(v[k]) || v[k].length > 100) fail(k);
|
|
1113
|
+
for (const c of v.connections) {
|
|
1114
|
+
object(c, "connection");
|
|
1115
|
+
exact(
|
|
1116
|
+
c,
|
|
1117
|
+
v.version !== "ixo.auth-options/1"
|
|
1118
|
+
? ["service", "id", "label", "scopes"]
|
|
1119
|
+
: ["id", "label", "scopes"],
|
|
1120
|
+
"connection",
|
|
1121
|
+
);
|
|
1122
|
+
if (
|
|
1123
|
+
v.version !== "ixo.auth-options/1" &&
|
|
1124
|
+
!["calendar", "gmail"].includes(c.service)
|
|
1125
|
+
)
|
|
1126
|
+
fail("connection.service");
|
|
1127
|
+
str(c.id, "id", 1, 100);
|
|
1128
|
+
str(c.label, "label");
|
|
1129
|
+
scopeList(c.scopes);
|
|
1130
|
+
}
|
|
1131
|
+
for (const t of v.targets) {
|
|
1132
|
+
object(t, "target");
|
|
1133
|
+
exact(t, ["id", "title"], "target");
|
|
1134
|
+
str(t.id, "id", 1, 100);
|
|
1135
|
+
str(t.title, "title");
|
|
1136
|
+
}
|
|
1137
|
+
v.resources.forEach(parseResourceDescriptor);
|
|
1138
|
+
return v;
|
|
1139
|
+
}
|
|
1140
|
+
export const parseHubActionOptions = parseActionOptions;
|
|
1141
|
+
/**
|
|
1142
|
+
* `/options?version=3` `transfer`: the owner's bank balances (one page, at most
|
|
1143
|
+
* 50) with the Hub's trusted display. `verified: false` means the metadata is
|
|
1144
|
+
* not trusted: decimals 0, base units; name the full denom. The model never
|
|
1145
|
+
* does decimal math or guesses ibc hashes: it copies `denom` and base units.
|
|
1146
|
+
*/
|
|
1147
|
+
export function parseOptionsTransfer(input) {
|
|
1148
|
+
const t = object(input, "options.transfer");
|
|
1149
|
+
exact(t, ["tokens"], "options.transfer");
|
|
1150
|
+
if (!Array.isArray(t.tokens) || t.tokens.length > 50)
|
|
1151
|
+
fail("options.transfer.tokens");
|
|
1152
|
+
const seen = new Set();
|
|
1153
|
+
for (const token of t.tokens) {
|
|
1154
|
+
object(token, "token");
|
|
1155
|
+
exact(
|
|
1156
|
+
token,
|
|
1157
|
+
["denom", "symbol", "decimals", "verified", "balanceBaseUnits"],
|
|
1158
|
+
"token",
|
|
1159
|
+
);
|
|
1160
|
+
pattern(token.denom, DENOM, "token.denom");
|
|
1161
|
+
if (seen.has(token.denom)) fail("token.denom");
|
|
1162
|
+
seen.add(token.denom);
|
|
1163
|
+
str(token.symbol, "token.symbol", 1, 128);
|
|
1164
|
+
if (
|
|
1165
|
+
!Number.isSafeInteger(token.decimals) ||
|
|
1166
|
+
token.decimals < 0 ||
|
|
1167
|
+
token.decimals > 18
|
|
1168
|
+
)
|
|
1169
|
+
fail("token.decimals");
|
|
1170
|
+
if (typeof token.verified !== "boolean") fail("token.verified");
|
|
1171
|
+
if (!token.verified && token.decimals !== 0) fail("token.decimals");
|
|
1172
|
+
pattern(token.balanceBaseUnits, INT, "token.balanceBaseUnits");
|
|
1173
|
+
}
|
|
1174
|
+
return t;
|
|
1175
|
+
}
|
|
1176
|
+
export function parseStatementProofBody(input) {
|
|
1177
|
+
const v = object(input, "statement proof body");
|
|
1178
|
+
exact(
|
|
1179
|
+
v,
|
|
1180
|
+
[
|
|
1181
|
+
"version",
|
|
1182
|
+
"accountId",
|
|
1183
|
+
"actionId",
|
|
1184
|
+
"revision",
|
|
1185
|
+
"digest",
|
|
1186
|
+
"ownerDid",
|
|
1187
|
+
"network",
|
|
1188
|
+
"audience",
|
|
1189
|
+
"purpose",
|
|
1190
|
+
"statementDigest",
|
|
1191
|
+
"issuedAt",
|
|
1192
|
+
"nonce",
|
|
1193
|
+
],
|
|
1194
|
+
"statement proof body",
|
|
1195
|
+
);
|
|
1196
|
+
if (
|
|
1197
|
+
v.version !== "ixo.statement-proof/1" ||
|
|
1198
|
+
!SERVICE_NETWORKS.includes(v.network)
|
|
1199
|
+
)
|
|
1200
|
+
fail("statement proof body");
|
|
1201
|
+
tuple(v);
|
|
1202
|
+
str(v.accountId, "accountId", 1, 100);
|
|
1203
|
+
str(v.ownerDid, "ownerDid");
|
|
1204
|
+
str(v.audience, "audience", 1, 2000);
|
|
1205
|
+
str(v.purpose, "purpose", 1, 120);
|
|
1206
|
+
pattern(v.statementDigest, HEX, "statementDigest");
|
|
1207
|
+
num(v.issuedAt, "issuedAt");
|
|
1208
|
+
str(v.nonce, "nonce", 1, 100);
|
|
1209
|
+
return v;
|
|
1210
|
+
}
|
|
1211
|
+
export function parseStatementProof(input) {
|
|
1212
|
+
const v = object(input, "statement proof");
|
|
1213
|
+
exact(v, ["body", "publicKey", "signature", "statement"], "statement proof");
|
|
1214
|
+
parseStatementProofBody(v.body);
|
|
1215
|
+
pattern(v.publicKey, /^[a-f0-9]{64}$/, "publicKey");
|
|
1216
|
+
pattern(v.signature, /^[a-f0-9]{128}$/, "signature");
|
|
1217
|
+
utf8(v.statement, "statement");
|
|
1218
|
+
return v;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
function validCalendarInstant(value) {
|
|
1222
|
+
const parts =
|
|
1223
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,3})?(?:Z|[+-](\d{2}):(\d{2}))$/.exec(
|
|
1224
|
+
value,
|
|
1225
|
+
);
|
|
1226
|
+
if (!parts) return false;
|
|
1227
|
+
const [, year, month, day, hour, minute, second, offsetHour, offsetMinute] =
|
|
1228
|
+
parts;
|
|
1229
|
+
const y = Number(year),
|
|
1230
|
+
m = Number(month),
|
|
1231
|
+
d = Number(day);
|
|
1232
|
+
const leap = y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0);
|
|
1233
|
+
const days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
1234
|
+
return (
|
|
1235
|
+
m >= 1 &&
|
|
1236
|
+
m <= 12 &&
|
|
1237
|
+
d >= 1 &&
|
|
1238
|
+
d <= days[m - 1] &&
|
|
1239
|
+
Number(hour) <= 23 &&
|
|
1240
|
+
Number(minute) <= 59 &&
|
|
1241
|
+
Number(second) <= 59 &&
|
|
1242
|
+
(offsetHour === undefined ||
|
|
1243
|
+
(Number(offsetHour) <= 23 && Number(offsetMinute) <= 59))
|
|
1244
|
+
);
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
/** Status rules shared by review views and outcomes. */
|
|
1248
|
+
function receiptFitsStatus(receipt, status) {
|
|
1249
|
+
const bank = receipt.kind === "ixo.bank.send.v1";
|
|
1250
|
+
if (status === "succeeded")
|
|
1251
|
+
return bank
|
|
1252
|
+
? receipt.code === 0 && receipt.height !== null && !("reason" in receipt)
|
|
1253
|
+
: receipt.kind !== "service.failure.v1";
|
|
1254
|
+
if (status === "failed") {
|
|
1255
|
+
if (receipt.kind === "service.failure.v1") return true;
|
|
1256
|
+
if (!bank) return false;
|
|
1257
|
+
if ("reason" in receipt) return true; // checked by parseReceipt
|
|
1258
|
+
return (
|
|
1259
|
+
receipt.code !== null && receipt.code !== 0 && receipt.height !== null
|
|
1260
|
+
);
|
|
1261
|
+
}
|
|
1262
|
+
return true;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
/**
|
|
1266
|
+
* Re-express a v2 outcome for a v1 caller, or return null when v1 cannot
|
|
1267
|
+
* represent it (expired/cancelled, or a not-included bank receipt).
|
|
1268
|
+
*/
|
|
1269
|
+
export function outcomeForVersion(input, version) {
|
|
1270
|
+
const outcome = parseOutcome(input);
|
|
1271
|
+
if (version === OUTCOME_VERSION_V2)
|
|
1272
|
+
return { ...outcome, version: OUTCOME_VERSION_V2 };
|
|
1273
|
+
if (version !== OUTCOME_VERSION_V1) return fail("outcome.version");
|
|
1274
|
+
if (
|
|
1275
|
+
!V1_OUTCOME_STATUSES.includes(outcome.status) ||
|
|
1276
|
+
(outcome.receipt &&
|
|
1277
|
+
("reason" in outcome.receipt ||
|
|
1278
|
+
!V1_RECEIPT_KINDS.includes(outcome.receipt.kind)))
|
|
1279
|
+
)
|
|
1280
|
+
return null;
|
|
1281
|
+
return parseOutcome({
|
|
1282
|
+
...outcome,
|
|
1283
|
+
version: OUTCOME_VERSION_V1,
|
|
1284
|
+
receipt: v1Receipt(outcome.receipt),
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/**
|
|
1289
|
+
* Hub-computed display for a transfer review (0.3.0/0.5.0, optional): the
|
|
1290
|
+
* model never does decimal math; the Hub reads DenomMetadata and simulates
|
|
1291
|
+
* the fee.
|
|
1292
|
+
*/
|
|
1293
|
+
export function parseTransferDisplay(input) {
|
|
1294
|
+
const t = object(input, "view.transfer");
|
|
1295
|
+
exact(
|
|
1296
|
+
t,
|
|
1297
|
+
[
|
|
1298
|
+
"denom",
|
|
1299
|
+
"symbol",
|
|
1300
|
+
"decimals",
|
|
1301
|
+
"verified",
|
|
1302
|
+
"amountDisplay",
|
|
1303
|
+
"feeBaseUnits",
|
|
1304
|
+
"feeDenom",
|
|
1305
|
+
"feeDisplay",
|
|
1306
|
+
"gasLimit",
|
|
1307
|
+
"feeGranter",
|
|
1308
|
+
],
|
|
1309
|
+
"view.transfer",
|
|
1310
|
+
);
|
|
1311
|
+
pattern(t.denom, DENOM, "view.transfer.denom");
|
|
1312
|
+
// An unverified token's symbol may be its full denom (up to 128).
|
|
1313
|
+
str(t.symbol, "view.transfer.symbol", 1, 128);
|
|
1314
|
+
if (!Number.isSafeInteger(t.decimals) || t.decimals < 0 || t.decimals > 18)
|
|
1315
|
+
fail("view.transfer.decimals");
|
|
1316
|
+
// false: metadata not trusted; show base units, the full denom and a marker.
|
|
1317
|
+
if (typeof t.verified !== "boolean") fail("view.transfer.verified");
|
|
1318
|
+
if (!t.verified && t.decimals !== 0) fail("view.transfer.decimals");
|
|
1319
|
+
str(t.amountDisplay, "view.transfer.amountDisplay", 1, 80);
|
|
1320
|
+
pattern(t.feeBaseUnits, INT, "view.transfer.feeBaseUnits");
|
|
1321
|
+
if (t.feeDenom !== "uixo") fail("view.transfer.feeDenom");
|
|
1322
|
+
str(t.feeDisplay, "view.transfer.feeDisplay", 1, 80);
|
|
1323
|
+
num(t.gasLimit, "view.transfer.gasLimit");
|
|
1324
|
+
if (t.feeGranter !== null) pattern(t.feeGranter, ADDRESS, "view.transfer.feeGranter");
|
|
1325
|
+
return t;
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
export * from "./outcome-signature.js";
|
|
1329
|
+
|
|
1330
|
+
/** The exact 0.1.0 receipt shape (v1 callers): drops the 0.3.0 bank fields. */
|
|
1331
|
+
export function v1Receipt(receipt) {
|
|
1332
|
+
if (!receipt || receipt.kind !== "ixo.bank.send.v1") return receipt;
|
|
1333
|
+
const { denom: _denom, amountBaseUnits: _amount, ...rest } = receipt;
|
|
1334
|
+
return rest;
|
|
1335
|
+
}
|