@ixo/action-protocol 0.4.1 → 0.6.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 +98 -0
- package/README.md +190 -0
- package/package.json +3 -2
- package/src/index.d.ts +286 -4
- package/src/index.js +573 -24
- package/src/operation-schema.js +93 -8
- package/src/outcome-signature.js +217 -0
package/src/index.js
CHANGED
|
@@ -15,6 +15,8 @@ export const OUTCOME_VERSION_V2 = "ixo.auth-outcome/2";
|
|
|
15
15
|
export const CAPABILITIES_VERSION_V1 = "ixo.auth-capabilities/1";
|
|
16
16
|
export const CAPABILITIES_VERSION_V2 = "ixo.auth-capabilities/2";
|
|
17
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}$/;
|
|
18
20
|
/** 0.2.1: mainnet is a valid network value; enablement is a Hub deployment decision. */
|
|
19
21
|
export const NETWORKS = ["ixo mainnet", "ixo testnet", "ixo devnet"];
|
|
20
22
|
/**
|
|
@@ -100,7 +102,14 @@ export async function digestOf(value) {
|
|
|
100
102
|
|
|
101
103
|
export function parseOperation(input) {
|
|
102
104
|
const op = object(input, "operation");
|
|
105
|
+
// 0.6.0: field-level errors first (`Invalid messages[1].toAddress`), then
|
|
106
|
+
// the JSON schema as a backstop.
|
|
107
|
+
if (op.kind === CHAIN_TX_KIND) {
|
|
108
|
+
const field = chainTxField(op);
|
|
109
|
+
if (field) fail(field);
|
|
110
|
+
}
|
|
103
111
|
validateOperationSchema(op);
|
|
112
|
+
if (op.kind === CHAIN_TX_KIND) return op;
|
|
104
113
|
if (op.kind === "statement.sign.v1") {
|
|
105
114
|
utf8(op.statement, "statement");
|
|
106
115
|
return op;
|
|
@@ -211,12 +220,15 @@ export function parseOperation(input) {
|
|
|
211
220
|
if (op.fromAddress === op.toAddress) fail("self transfer");
|
|
212
221
|
pattern(op.amountBaseUnits, INT, "amountBaseUnits");
|
|
213
222
|
if (op.amountBaseUnits === "0") fail("amountBaseUnits");
|
|
214
|
-
|
|
215
|
-
pattern(op.
|
|
223
|
+
// Any bank denom; absent means uixo (0.1/0.2 callers).
|
|
224
|
+
if (op.denom !== undefined) pattern(op.denom, DENOM, "denom");
|
|
225
|
+
// Optional and ignored by the Hub, which simulates and sets the fee.
|
|
226
|
+
if (op.feeBaseUnits !== undefined)
|
|
227
|
+
pattern(op.feeBaseUnits, INT, "feeBaseUnits");
|
|
216
228
|
str(op.memo, "memo", 0, 256);
|
|
217
229
|
// Cosmos SDK x/auth max_memo_characters counts UTF-8 bytes, not UTF-16 units.
|
|
218
230
|
if (new TextEncoder().encode(op.memo).length > MEMO_BYTES) fail("memo");
|
|
219
|
-
return op;
|
|
231
|
+
return op.denom === undefined ? { ...op, denom: "uixo" } : op;
|
|
220
232
|
}
|
|
221
233
|
if (op.kind === "resource.read.v1") {
|
|
222
234
|
exact(
|
|
@@ -299,6 +311,7 @@ export function assertHubReferenceUrl(input, configuredOrigin) {
|
|
|
299
311
|
export function parseReceipt(input) {
|
|
300
312
|
const receipt = object(input, "receipt");
|
|
301
313
|
if (p2ReceiptFields[receipt.kind]) return parseP2Receipt(receipt);
|
|
314
|
+
if (receipt.kind === CHAIN_TX_KIND) return parseChainTxReceipt(receipt);
|
|
302
315
|
if (receipt.kind === "form.submission.v1") {
|
|
303
316
|
exact(
|
|
304
317
|
receipt,
|
|
@@ -334,9 +347,15 @@ export function parseReceipt(input) {
|
|
|
334
347
|
"height",
|
|
335
348
|
"code",
|
|
336
349
|
"reason",
|
|
350
|
+
"denom",
|
|
351
|
+
"amountBaseUnits",
|
|
337
352
|
],
|
|
338
353
|
"receipt",
|
|
339
354
|
);
|
|
355
|
+
// 0.3.0/0.5.0: what moved (optional so earlier receipts still parse).
|
|
356
|
+
if ("denom" in receipt) pattern(receipt.denom, DENOM, "receipt.denom");
|
|
357
|
+
if ("amountBaseUnits" in receipt)
|
|
358
|
+
pattern(receipt.amountBaseUnits, INT, "receipt.amountBaseUnits");
|
|
340
359
|
pattern(receipt.actionId, ID, "receipt.actionId");
|
|
341
360
|
num(receipt.revision, "receipt.revision");
|
|
342
361
|
pattern(receipt.digest, HEX, "receipt.digest");
|
|
@@ -383,10 +402,26 @@ export function parseReviewView(input) {
|
|
|
383
402
|
"resourceUrl",
|
|
384
403
|
"chatUrl",
|
|
385
404
|
"service",
|
|
405
|
+
"transfer",
|
|
406
|
+
"chainTx",
|
|
386
407
|
],
|
|
387
408
|
"view",
|
|
388
409
|
);
|
|
389
410
|
parseReference(view.reference);
|
|
411
|
+
parseOperation(view.operation);
|
|
412
|
+
// 0.6.0: the Hub-computed chain transaction display (chain tx cards only).
|
|
413
|
+
if (view.chainTx !== undefined && view.chainTx !== null) {
|
|
414
|
+
if (view.operation?.kind !== CHAIN_TX_KIND) fail("view.chainTx");
|
|
415
|
+
const display = parseChainTxDisplay(view.chainTx);
|
|
416
|
+
if (
|
|
417
|
+
display.signer !== view.operation.signer ||
|
|
418
|
+
display.steps.length !== view.operation.messages.length ||
|
|
419
|
+
display.steps.some(
|
|
420
|
+
(step, i) => step.type !== view.operation.messages[i].type,
|
|
421
|
+
)
|
|
422
|
+
)
|
|
423
|
+
fail("view.chainTx");
|
|
424
|
+
}
|
|
390
425
|
if (view.service !== undefined && view.service !== null) {
|
|
391
426
|
object(view.service, "view.service");
|
|
392
427
|
exact(
|
|
@@ -399,6 +434,8 @@ export function parseReviewView(input) {
|
|
|
399
434
|
if (view.service.resource !== null)
|
|
400
435
|
parseResourceDescriptor(view.service.resource);
|
|
401
436
|
}
|
|
437
|
+
if (view.transfer !== undefined && view.transfer !== null)
|
|
438
|
+
parseTransferDisplay(view.transfer);
|
|
402
439
|
str(view.requester, "view.requester", 1, 120);
|
|
403
440
|
parseOperation(view.operation);
|
|
404
441
|
if (
|
|
@@ -526,9 +563,13 @@ export function parseOutcome(input) {
|
|
|
526
563
|
)
|
|
527
564
|
fail("outcome.receipt");
|
|
528
565
|
if (!receiptFitsStatus(receipt, outcome.status)) fail("outcome.receipt");
|
|
566
|
+
// v1 envelopes keep the exact 0.4.1 receipt shape.
|
|
529
567
|
if (
|
|
530
568
|
!v2 &&
|
|
531
|
-
("reason" in receipt ||
|
|
569
|
+
("reason" in receipt ||
|
|
570
|
+
"denom" in receipt ||
|
|
571
|
+
"amountBaseUnits" in receipt ||
|
|
572
|
+
!V1_RECEIPT_KINDS.includes(receipt.kind))
|
|
532
573
|
)
|
|
533
574
|
fail("outcome.receipt");
|
|
534
575
|
}
|
|
@@ -560,9 +601,21 @@ export function parseCapabilities(input) {
|
|
|
560
601
|
"operations",
|
|
561
602
|
"forms",
|
|
562
603
|
"transfer",
|
|
604
|
+
"chainTx",
|
|
563
605
|
],
|
|
564
606
|
"capabilities",
|
|
565
607
|
);
|
|
608
|
+
// 0.6.0 (`?version=4`): what a chain transaction may carry. Required when
|
|
609
|
+
// the kind is listed; never present otherwise except as null.
|
|
610
|
+
if (data.chainTx !== undefined && data.chainTx !== null)
|
|
611
|
+
parseChainTxCapabilities(data.chainTx);
|
|
612
|
+
if (
|
|
613
|
+
Array.isArray(data.operations) &&
|
|
614
|
+
data.operations.includes(CHAIN_TX_KIND)
|
|
615
|
+
? data.chainTx === undefined || data.chainTx === null
|
|
616
|
+
: data.chainTx !== undefined && data.chainTx !== null
|
|
617
|
+
)
|
|
618
|
+
fail("capabilities.chainTx");
|
|
566
619
|
if (
|
|
567
620
|
![CAPABILITIES_VERSION_V1, CAPABILITIES_VERSION_V2].includes(data.version)
|
|
568
621
|
)
|
|
@@ -601,21 +654,52 @@ export function parseCapabilities(input) {
|
|
|
601
654
|
fail("capabilities.operations");
|
|
602
655
|
if (data.transfer !== null) {
|
|
603
656
|
object(data.transfer, "capabilities.transfer");
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
"capabilities.transfer"
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
657
|
+
if ("limits" in data.transfer) {
|
|
658
|
+
// 0.3.0/0.5.0 shape (callers that declare ?version=3): optional
|
|
659
|
+
// per-denom amount caps; empty = no cap. The Hub sets the fee.
|
|
660
|
+
exact(data.transfer, ["limits", "maxFeeUixo"], "capabilities.transfer");
|
|
661
|
+
if (
|
|
662
|
+
!Array.isArray(data.transfer.limits) ||
|
|
663
|
+
data.transfer.limits.length > 100
|
|
664
|
+
)
|
|
665
|
+
fail("capabilities.transfer.limits");
|
|
666
|
+
const denoms = new Set();
|
|
667
|
+
for (const limit of data.transfer.limits) {
|
|
668
|
+
object(limit, "capabilities.transfer.limit");
|
|
669
|
+
exact(limit, ["denom", "maxAmountBaseUnits"], "capabilities.transfer.limit");
|
|
670
|
+
pattern(limit.denom, DENOM, "capabilities.transfer.limit.denom");
|
|
671
|
+
pattern(
|
|
672
|
+
limit.maxAmountBaseUnits,
|
|
673
|
+
/^[1-9][0-9]{0,39}$/,
|
|
674
|
+
"capabilities.transfer.limit.maxAmountBaseUnits",
|
|
675
|
+
);
|
|
676
|
+
if (denoms.has(limit.denom)) fail("capabilities.transfer.limit.denom");
|
|
677
|
+
denoms.add(limit.denom);
|
|
678
|
+
}
|
|
679
|
+
if (data.transfer.maxFeeUixo !== undefined)
|
|
680
|
+
pattern(
|
|
681
|
+
data.transfer.maxFeeUixo,
|
|
682
|
+
/^[1-9][0-9]{0,19}$/,
|
|
683
|
+
"capabilities.transfer.maxFeeUixo",
|
|
684
|
+
);
|
|
685
|
+
} else {
|
|
686
|
+
// Legacy 0.1/0.2 shape.
|
|
687
|
+
exact(
|
|
688
|
+
data.transfer,
|
|
689
|
+
["maxAmountUixo", "maxFeeUixo"],
|
|
690
|
+
"capabilities.transfer",
|
|
691
|
+
);
|
|
692
|
+
pattern(
|
|
693
|
+
data.transfer.maxAmountUixo,
|
|
694
|
+
/^[1-9][0-9]{0,19}$/,
|
|
695
|
+
"capabilities.transfer.maxAmountUixo",
|
|
696
|
+
);
|
|
697
|
+
pattern(
|
|
698
|
+
data.transfer.maxFeeUixo,
|
|
699
|
+
/^[1-9][0-9]{0,19}$/,
|
|
700
|
+
"capabilities.transfer.maxFeeUixo",
|
|
701
|
+
);
|
|
702
|
+
}
|
|
619
703
|
}
|
|
620
704
|
if (data.operations.includes("ixo.bank.send.v1") && data.transfer === null)
|
|
621
705
|
fail("capabilities.transfer");
|
|
@@ -728,6 +812,7 @@ const receiptKinds = {
|
|
|
728
812
|
"calendar.event.create.v1": "calendar.event.created.v1",
|
|
729
813
|
"vfs.access.request.v1": "vfs.access.requested.v1",
|
|
730
814
|
"vfs.text.save.v1": "vfs.text.saved.v1",
|
|
815
|
+
"ixo.chain.tx.v1": "ixo.chain.tx.v1",
|
|
731
816
|
};
|
|
732
817
|
export function assertReceiptForOperation(
|
|
733
818
|
operation,
|
|
@@ -751,6 +836,14 @@ export function assertReceiptForOperation(
|
|
|
751
836
|
receipt.kind === "service.failure.v1"
|
|
752
837
|
)
|
|
753
838
|
fail("operation receipt");
|
|
839
|
+
// 0.6.0: a chain tx receipt names the approved chain and steps, in order.
|
|
840
|
+
if (
|
|
841
|
+
operation.kind === CHAIN_TX_KIND &&
|
|
842
|
+
(receipt.chainId !== operation.chainId ||
|
|
843
|
+
receipt.steps.length !== operation.messages.length ||
|
|
844
|
+
receipt.steps.some((step, i) => step.type !== operation.messages[i].type))
|
|
845
|
+
)
|
|
846
|
+
fail("receipt steps");
|
|
754
847
|
// Every kind: the receipt must name the approved connection / target.
|
|
755
848
|
if (
|
|
756
849
|
"connectionId" in operation &&
|
|
@@ -1032,6 +1125,14 @@ export function parseServiceResult(input) {
|
|
|
1032
1125
|
}
|
|
1033
1126
|
export function parseActionOptions(input) {
|
|
1034
1127
|
const v = object(input, "options");
|
|
1128
|
+
if (
|
|
1129
|
+
!["ixo.auth-options/1", "ixo.auth-options/2", "ixo.auth-options/3"].includes(
|
|
1130
|
+
v.version,
|
|
1131
|
+
)
|
|
1132
|
+
)
|
|
1133
|
+
fail("options.version");
|
|
1134
|
+
// v3 (0.5.0) = v2 + `transfer`; v1/v2 keep their exact 0.4.1 key sets.
|
|
1135
|
+
const v3 = v.version === "ixo.auth-options/3";
|
|
1035
1136
|
exact(
|
|
1036
1137
|
v,
|
|
1037
1138
|
[
|
|
@@ -1043,11 +1144,11 @@ export function parseActionOptions(input) {
|
|
|
1043
1144
|
"connections",
|
|
1044
1145
|
"targets",
|
|
1045
1146
|
"resources",
|
|
1147
|
+
...(v3 ? ["transfer"] : []),
|
|
1046
1148
|
],
|
|
1047
1149
|
"options",
|
|
1048
1150
|
);
|
|
1049
|
-
if (
|
|
1050
|
-
fail("options.version");
|
|
1151
|
+
if (v3 && v.transfer !== null) parseOptionsTransfer(v.transfer);
|
|
1051
1152
|
for (const k of ["accountId", "bindingId"]) str(v[k], k, 1, 100);
|
|
1052
1153
|
str(v.ownerDid, "ownerDid");
|
|
1053
1154
|
num(v.bindingRevision, "bindingRevision");
|
|
@@ -1057,13 +1158,13 @@ export function parseActionOptions(input) {
|
|
|
1057
1158
|
object(c, "connection");
|
|
1058
1159
|
exact(
|
|
1059
1160
|
c,
|
|
1060
|
-
v.version
|
|
1161
|
+
v.version !== "ixo.auth-options/1"
|
|
1061
1162
|
? ["service", "id", "label", "scopes"]
|
|
1062
1163
|
: ["id", "label", "scopes"],
|
|
1063
1164
|
"connection",
|
|
1064
1165
|
);
|
|
1065
1166
|
if (
|
|
1066
|
-
v.version
|
|
1167
|
+
v.version !== "ixo.auth-options/1" &&
|
|
1067
1168
|
!["calendar", "gmail"].includes(c.service)
|
|
1068
1169
|
)
|
|
1069
1170
|
fail("connection.service");
|
|
@@ -1081,6 +1182,41 @@ export function parseActionOptions(input) {
|
|
|
1081
1182
|
return v;
|
|
1082
1183
|
}
|
|
1083
1184
|
export const parseHubActionOptions = parseActionOptions;
|
|
1185
|
+
/**
|
|
1186
|
+
* `/options?version=3` `transfer`: the owner's bank balances (one page, at most
|
|
1187
|
+
* 50) with the Hub's trusted display. `verified: false` means the metadata is
|
|
1188
|
+
* not trusted: decimals 0, base units; name the full denom. The model never
|
|
1189
|
+
* does decimal math or guesses ibc hashes: it copies `denom` and base units.
|
|
1190
|
+
*/
|
|
1191
|
+
export function parseOptionsTransfer(input) {
|
|
1192
|
+
const t = object(input, "options.transfer");
|
|
1193
|
+
exact(t, ["tokens"], "options.transfer");
|
|
1194
|
+
if (!Array.isArray(t.tokens) || t.tokens.length > 50)
|
|
1195
|
+
fail("options.transfer.tokens");
|
|
1196
|
+
const seen = new Set();
|
|
1197
|
+
for (const token of t.tokens) {
|
|
1198
|
+
object(token, "token");
|
|
1199
|
+
exact(
|
|
1200
|
+
token,
|
|
1201
|
+
["denom", "symbol", "decimals", "verified", "balanceBaseUnits"],
|
|
1202
|
+
"token",
|
|
1203
|
+
);
|
|
1204
|
+
pattern(token.denom, DENOM, "token.denom");
|
|
1205
|
+
if (seen.has(token.denom)) fail("token.denom");
|
|
1206
|
+
seen.add(token.denom);
|
|
1207
|
+
str(token.symbol, "token.symbol", 1, 128);
|
|
1208
|
+
if (
|
|
1209
|
+
!Number.isSafeInteger(token.decimals) ||
|
|
1210
|
+
token.decimals < 0 ||
|
|
1211
|
+
token.decimals > 18
|
|
1212
|
+
)
|
|
1213
|
+
fail("token.decimals");
|
|
1214
|
+
if (typeof token.verified !== "boolean") fail("token.verified");
|
|
1215
|
+
if (!token.verified && token.decimals !== 0) fail("token.decimals");
|
|
1216
|
+
pattern(token.balanceBaseUnits, INT, "token.balanceBaseUnits");
|
|
1217
|
+
}
|
|
1218
|
+
return t;
|
|
1219
|
+
}
|
|
1084
1220
|
export function parseStatementProofBody(input) {
|
|
1085
1221
|
const v = object(input, "statement proof body");
|
|
1086
1222
|
exact(
|
|
@@ -1154,6 +1290,16 @@ function validCalendarInstant(value) {
|
|
|
1154
1290
|
|
|
1155
1291
|
/** Status rules shared by review views and outcomes. */
|
|
1156
1292
|
function receiptFitsStatus(receipt, status) {
|
|
1293
|
+
// 0.6.0: a chain tx receipt succeeds only included with code 0; a failure
|
|
1294
|
+
// always names its reason (tx_failed = included with code != 0).
|
|
1295
|
+
if (receipt.kind === CHAIN_TX_KIND) {
|
|
1296
|
+
if (status === "succeeded")
|
|
1297
|
+
return (
|
|
1298
|
+
receipt.code === 0 && receipt.height !== null && !("reason" in receipt)
|
|
1299
|
+
);
|
|
1300
|
+
if (status === "failed") return "reason" in receipt;
|
|
1301
|
+
return true;
|
|
1302
|
+
}
|
|
1157
1303
|
const bank = receipt.kind === "ixo.bank.send.v1";
|
|
1158
1304
|
if (status === "succeeded")
|
|
1159
1305
|
return bank
|
|
@@ -1186,5 +1332,408 @@ export function outcomeForVersion(input, version) {
|
|
|
1186
1332
|
!V1_RECEIPT_KINDS.includes(outcome.receipt.kind)))
|
|
1187
1333
|
)
|
|
1188
1334
|
return null;
|
|
1189
|
-
return parseOutcome({
|
|
1335
|
+
return parseOutcome({
|
|
1336
|
+
...outcome,
|
|
1337
|
+
version: OUTCOME_VERSION_V1,
|
|
1338
|
+
receipt: v1Receipt(outcome.receipt),
|
|
1339
|
+
});
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
/**
|
|
1343
|
+
* Hub-computed display for a transfer review (0.3.0/0.5.0, optional): the
|
|
1344
|
+
* model never does decimal math; the Hub reads DenomMetadata and simulates
|
|
1345
|
+
* the fee.
|
|
1346
|
+
*/
|
|
1347
|
+
export function parseTransferDisplay(input) {
|
|
1348
|
+
const t = object(input, "view.transfer");
|
|
1349
|
+
exact(
|
|
1350
|
+
t,
|
|
1351
|
+
[
|
|
1352
|
+
"denom",
|
|
1353
|
+
"symbol",
|
|
1354
|
+
"decimals",
|
|
1355
|
+
"verified",
|
|
1356
|
+
"amountDisplay",
|
|
1357
|
+
"feeBaseUnits",
|
|
1358
|
+
"feeDenom",
|
|
1359
|
+
"feeDisplay",
|
|
1360
|
+
"gasLimit",
|
|
1361
|
+
"feeGranter",
|
|
1362
|
+
],
|
|
1363
|
+
"view.transfer",
|
|
1364
|
+
);
|
|
1365
|
+
pattern(t.denom, DENOM, "view.transfer.denom");
|
|
1366
|
+
// An unverified token's symbol may be its full denom (up to 128).
|
|
1367
|
+
str(t.symbol, "view.transfer.symbol", 1, 128);
|
|
1368
|
+
if (!Number.isSafeInteger(t.decimals) || t.decimals < 0 || t.decimals > 18)
|
|
1369
|
+
fail("view.transfer.decimals");
|
|
1370
|
+
// false: metadata not trusted; show base units, the full denom and a marker.
|
|
1371
|
+
if (typeof t.verified !== "boolean") fail("view.transfer.verified");
|
|
1372
|
+
if (!t.verified && t.decimals !== 0) fail("view.transfer.decimals");
|
|
1373
|
+
str(t.amountDisplay, "view.transfer.amountDisplay", 1, 80);
|
|
1374
|
+
pattern(t.feeBaseUnits, INT, "view.transfer.feeBaseUnits");
|
|
1375
|
+
if (t.feeDenom !== "uixo") fail("view.transfer.feeDenom");
|
|
1376
|
+
str(t.feeDisplay, "view.transfer.feeDisplay", 1, 80);
|
|
1377
|
+
num(t.gasLimit, "view.transfer.gasLimit");
|
|
1378
|
+
if (t.feeGranter !== null) pattern(t.feeGranter, ADDRESS, "view.transfer.feeGranter");
|
|
1379
|
+
return t;
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
// ---------------------------------------------------------------------------
|
|
1383
|
+
// 0.6.0 chain transactions (`ixo.chain.tx.v1`)
|
|
1384
|
+
// ---------------------------------------------------------------------------
|
|
1385
|
+
|
|
1386
|
+
/** One card kind for every chain transaction (1..8 messages, all or nothing). */
|
|
1387
|
+
export const CHAIN_TX_KIND = "ixo.chain.tx.v1";
|
|
1388
|
+
export const CHAIN_TX_MESSAGE_TYPES = [
|
|
1389
|
+
"cosmos.bank.send",
|
|
1390
|
+
"ixo.claims.submit",
|
|
1391
|
+
"ixo.claims.evaluate",
|
|
1392
|
+
"ixo.token.transfer",
|
|
1393
|
+
];
|
|
1394
|
+
export const CHAIN_TX_MAX_MESSAGES = 8;
|
|
1395
|
+
export const CHAIN_TX_MAX_TOKEN_LINES = 32;
|
|
1396
|
+
/** `check_tx_rejected`/`expired_not_included`: never included; `tx_failed`: included with code != 0. */
|
|
1397
|
+
export const CHAIN_TX_FAILURE_REASONS = [
|
|
1398
|
+
"check_tx_rejected",
|
|
1399
|
+
"expired_not_included",
|
|
1400
|
+
"tx_failed",
|
|
1401
|
+
];
|
|
1402
|
+
export const CHAIN_TX_RISK_LEVELS = ["low", "medium", "high", "critical"];
|
|
1403
|
+
/** EvaluationStatus values a chain tx may record (0 pending and 5 flagged are refused). */
|
|
1404
|
+
export const CHAIN_TX_EVALUATION_STATUSES = [1, 2, 3, 4];
|
|
1405
|
+
// The chain's DID rule (x/iid/types/iid.go didValidationRegexpStr): segments of
|
|
1406
|
+
// [A-Za-z0-9._-] or a `%` followed by two hex digits, joined by ':', the last
|
|
1407
|
+
// one non-empty. Anchored at both ends here (the chain's own regex anchors
|
|
1408
|
+
// only its start, so it accepts any suffix; this is stricter, never looser).
|
|
1409
|
+
const DID_TAIL = "(?:(?:[A-Za-z0-9._-]|%[0-9A-Fa-f]{2})*:)*(?:[A-Za-z0-9._-]|%[0-9A-Fa-f]{2})+";
|
|
1410
|
+
const IXO_DID = new RegExp(`^did:ixo:${DID_TAIL}$`);
|
|
1411
|
+
const ANY_DID = new RegExp(`^did:[a-z0-9]+:${DID_TAIL}$`);
|
|
1412
|
+
// The Hub's isIxoAddress: canonical lowercase bech32 with exactly 20 data
|
|
1413
|
+
// bytes (32 payload + 6 checksum characters). The Hub also checks the checksum.
|
|
1414
|
+
const ACCOUNT_ADDRESS = /^ixo1[023456789acdefghjklmnpqrstuvwxyz]{38}$/;
|
|
1415
|
+
const BASE_UNITS = /^[1-9][0-9]{0,77}$/;
|
|
1416
|
+
// A uint64 without leading zeros (checked against UINT64_MAX below).
|
|
1417
|
+
const COLLECTION_ID = /^(0|[1-9][0-9]{0,19})$/;
|
|
1418
|
+
const UINT64_MAX = 18446744073709551615n;
|
|
1419
|
+
const UINT32_MAX = 4294967295;
|
|
1420
|
+
// Claim ids, impact token ids and verification proofs (CIDs, UUIDs, `X:1`).
|
|
1421
|
+
const CHAIN_ID_CHARS = /^[A-Za-z0-9:._/-]+$/;
|
|
1422
|
+
// C0/C1 controls, zero-width and bidi marks (U+061C, U+200B-U+200F,
|
|
1423
|
+
// U+202A-U+202E, U+2066-U+2069), line/paragraph separators and the BOM would
|
|
1424
|
+
// let text render as something else.
|
|
1425
|
+
const CONTROL =
|
|
1426
|
+
/[\u0000-\u001f\u007f-\u009f\u061c\u200b-\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069\ufeff]/;
|
|
1427
|
+
const CHAIN_TX_MESSAGE_KEYS = {
|
|
1428
|
+
"cosmos.bank.send": ["type", "toAddress", "amountBaseUnits", "denom"],
|
|
1429
|
+
"ixo.claims.submit": [
|
|
1430
|
+
"type",
|
|
1431
|
+
"collectionId",
|
|
1432
|
+
"claimId",
|
|
1433
|
+
"agentDid",
|
|
1434
|
+
"adminAddress",
|
|
1435
|
+
"useIntent",
|
|
1436
|
+
],
|
|
1437
|
+
"ixo.claims.evaluate": [
|
|
1438
|
+
"type",
|
|
1439
|
+
"claimId",
|
|
1440
|
+
"collectionId",
|
|
1441
|
+
"oracle",
|
|
1442
|
+
"agentDid",
|
|
1443
|
+
"agentAddress",
|
|
1444
|
+
"adminAddress",
|
|
1445
|
+
"status",
|
|
1446
|
+
"reason",
|
|
1447
|
+
"verificationProof",
|
|
1448
|
+
],
|
|
1449
|
+
"ixo.token.transfer": ["type", "recipient", "tokens"],
|
|
1450
|
+
};
|
|
1451
|
+
const plain = (value) =>
|
|
1452
|
+
value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1453
|
+
const isText = (value, min = 1, max = 200) =>
|
|
1454
|
+
typeof value === "string" && value.length >= min && value.length <= max;
|
|
1455
|
+
const isMatch = (value, regex, max = 200) =>
|
|
1456
|
+
isText(value, 1, max) && regex.test(value);
|
|
1457
|
+
const isId = (value, max) => isMatch(value, CHAIN_ID_CHARS, max);
|
|
1458
|
+
const isCollectionId = (value) =>
|
|
1459
|
+
isMatch(value, COLLECTION_ID, 20) && BigInt(value) <= UINT64_MAX;
|
|
1460
|
+
const utf8Length = (value) => new TextEncoder().encode(value).length;
|
|
1461
|
+
|
|
1462
|
+
/** The first bad field of one message (relative to the message), or null. */
|
|
1463
|
+
function chainTxMessageField(m) {
|
|
1464
|
+
if (m.type === "cosmos.bank.send") {
|
|
1465
|
+
if (!isMatch(m.toAddress, ACCOUNT_ADDRESS)) return "toAddress";
|
|
1466
|
+
if (!isMatch(m.amountBaseUnits, BASE_UNITS, 78)) return "amountBaseUnits";
|
|
1467
|
+
if (m.denom !== undefined && !isMatch(m.denom, DENOM, 128)) return "denom";
|
|
1468
|
+
return null;
|
|
1469
|
+
}
|
|
1470
|
+
if (m.type === "ixo.claims.submit") {
|
|
1471
|
+
if (!isCollectionId(m.collectionId)) return "collectionId";
|
|
1472
|
+
if (!isId(m.claimId, 256)) return "claimId";
|
|
1473
|
+
if (!isMatch(m.agentDid, IXO_DID)) return "agentDid";
|
|
1474
|
+
if (!isMatch(m.adminAddress, ACCOUNT_ADDRESS)) return "adminAddress";
|
|
1475
|
+
if (m.useIntent !== undefined && typeof m.useIntent !== "boolean")
|
|
1476
|
+
return "useIntent";
|
|
1477
|
+
return null;
|
|
1478
|
+
}
|
|
1479
|
+
if (m.type === "ixo.claims.evaluate") {
|
|
1480
|
+
if (!isId(m.claimId, 256)) return "claimId";
|
|
1481
|
+
if (!isCollectionId(m.collectionId)) return "collectionId";
|
|
1482
|
+
if (!isMatch(m.oracle, ANY_DID)) return "oracle";
|
|
1483
|
+
if (!isMatch(m.agentDid, IXO_DID)) return "agentDid";
|
|
1484
|
+
if (!isMatch(m.agentAddress, ACCOUNT_ADDRESS)) return "agentAddress";
|
|
1485
|
+
if (!isMatch(m.adminAddress, ACCOUNT_ADDRESS)) return "adminAddress";
|
|
1486
|
+
if (!CHAIN_TX_EVALUATION_STATUSES.includes(m.status)) return "status";
|
|
1487
|
+
if (
|
|
1488
|
+
m.reason !== undefined &&
|
|
1489
|
+
!(Number.isSafeInteger(m.reason) && m.reason >= 0 && m.reason <= UINT32_MAX)
|
|
1490
|
+
)
|
|
1491
|
+
return "reason";
|
|
1492
|
+
// Optional; omit it rather than sending ''. ≤ 300 so the review shows it whole.
|
|
1493
|
+
if (m.verificationProof !== undefined && !isId(m.verificationProof, 300))
|
|
1494
|
+
return "verificationProof";
|
|
1495
|
+
return null;
|
|
1496
|
+
}
|
|
1497
|
+
// ixo.token.transfer
|
|
1498
|
+
if (!isMatch(m.recipient, ACCOUNT_ADDRESS)) return "recipient";
|
|
1499
|
+
if (
|
|
1500
|
+
!Array.isArray(m.tokens) ||
|
|
1501
|
+
m.tokens.length < 1 ||
|
|
1502
|
+
m.tokens.length > CHAIN_TX_MAX_TOKEN_LINES
|
|
1503
|
+
)
|
|
1504
|
+
return "tokens";
|
|
1505
|
+
for (const [j, token] of m.tokens.entries()) {
|
|
1506
|
+
if (!plain(token)) return `tokens[${j}]`;
|
|
1507
|
+
for (const key of Object.keys(token))
|
|
1508
|
+
if (key !== "id" && key !== "amount") return `tokens[${j}].${key}`;
|
|
1509
|
+
if (!isId(token.id, 128)) return `tokens[${j}].id`;
|
|
1510
|
+
if (!isMatch(token.amount, BASE_UNITS, 78)) return `tokens[${j}].amount`;
|
|
1511
|
+
}
|
|
1512
|
+
return null;
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
/** The first bad field of a chain tx operation (`messages[1].toAddress`), or null. */
|
|
1516
|
+
function chainTxField(op) {
|
|
1517
|
+
for (const key of Object.keys(op))
|
|
1518
|
+
if (!["kind", "chainId", "network", "signer", "memo", "messages"].includes(key))
|
|
1519
|
+
return key;
|
|
1520
|
+
if (!isText(op.chainId, 1, 100)) return "chainId";
|
|
1521
|
+
if (!NETWORKS.includes(op.network)) return "network";
|
|
1522
|
+
if (!isMatch(op.signer, ACCOUNT_ADDRESS)) return "signer";
|
|
1523
|
+
// Cosmos SDK x/auth max_memo_characters counts UTF-8 bytes. The memo is
|
|
1524
|
+
// shown as the "Note", so it carries no control or bidi characters either.
|
|
1525
|
+
if (
|
|
1526
|
+
!isText(op.memo, 0, 256) ||
|
|
1527
|
+
utf8Length(op.memo) > MEMO_BYTES ||
|
|
1528
|
+
CONTROL.test(op.memo)
|
|
1529
|
+
)
|
|
1530
|
+
return "memo";
|
|
1531
|
+
if (
|
|
1532
|
+
!Array.isArray(op.messages) ||
|
|
1533
|
+
op.messages.length < 1 ||
|
|
1534
|
+
op.messages.length > CHAIN_TX_MAX_MESSAGES
|
|
1535
|
+
)
|
|
1536
|
+
return "messages";
|
|
1537
|
+
for (const [i, m] of op.messages.entries()) {
|
|
1538
|
+
const at = `messages[${i}]`;
|
|
1539
|
+
if (!plain(m)) return at;
|
|
1540
|
+
if (!CHAIN_TX_MESSAGE_TYPES.includes(m.type)) return `${at}.type`;
|
|
1541
|
+
for (const key of Object.keys(m))
|
|
1542
|
+
if (!CHAIN_TX_MESSAGE_KEYS[m.type].includes(key)) return `${at}.${key}`;
|
|
1543
|
+
const field = chainTxMessageField(m);
|
|
1544
|
+
if (field) return `${at}.${field}`;
|
|
1545
|
+
}
|
|
1546
|
+
return null;
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
/**
|
|
1550
|
+
* Why a proposed chain tx operation is malformed, as the Hub's 400 `reason`:
|
|
1551
|
+
* `invalid_message: messages[1].toAddress` (0-based message index) or
|
|
1552
|
+
* `invalid_operation: memo`; null when it parses.
|
|
1553
|
+
*/
|
|
1554
|
+
export function chainTxIssue(input) {
|
|
1555
|
+
if (!plain(input) || input.kind !== CHAIN_TX_KIND)
|
|
1556
|
+
return "invalid_operation: kind";
|
|
1557
|
+
const field = chainTxField(input);
|
|
1558
|
+
if (field)
|
|
1559
|
+
return field.startsWith("messages[")
|
|
1560
|
+
? `invalid_message: ${field}`
|
|
1561
|
+
: `invalid_operation: ${field}`;
|
|
1562
|
+
try {
|
|
1563
|
+
validateOperationSchema(input);
|
|
1564
|
+
} catch {
|
|
1565
|
+
return "invalid_operation: operation";
|
|
1566
|
+
}
|
|
1567
|
+
return null;
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
/** `GET /capabilities?version=4` `chainTx`. */
|
|
1571
|
+
export function parseChainTxCapabilities(input) {
|
|
1572
|
+
const c = object(input, "capabilities.chainTx");
|
|
1573
|
+
exact(c, ["messageTypes", "maxMessages"], "capabilities.chainTx");
|
|
1574
|
+
if (
|
|
1575
|
+
!Array.isArray(c.messageTypes) ||
|
|
1576
|
+
c.messageTypes.length < 1 ||
|
|
1577
|
+
new Set(c.messageTypes).size !== c.messageTypes.length ||
|
|
1578
|
+
c.messageTypes.some((type) => !CHAIN_TX_MESSAGE_TYPES.includes(type))
|
|
1579
|
+
)
|
|
1580
|
+
fail("capabilities.chainTx.messageTypes");
|
|
1581
|
+
if (
|
|
1582
|
+
!Number.isSafeInteger(c.maxMessages) ||
|
|
1583
|
+
c.maxMessages < 1 ||
|
|
1584
|
+
c.maxMessages > CHAIN_TX_MAX_MESSAGES
|
|
1585
|
+
)
|
|
1586
|
+
fail("capabilities.chainTx.maxMessages");
|
|
1587
|
+
return c;
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
/** The Hub-computed review display of a chain tx (the only thing a page renders). */
|
|
1591
|
+
export function parseChainTxDisplay(input) {
|
|
1592
|
+
const d = object(input, "view.chainTx");
|
|
1593
|
+
exact(
|
|
1594
|
+
d,
|
|
1595
|
+
[
|
|
1596
|
+
"riskLevel",
|
|
1597
|
+
"confirmTwice",
|
|
1598
|
+
"steps",
|
|
1599
|
+
"consent",
|
|
1600
|
+
"feeBaseUnits",
|
|
1601
|
+
"feeDenom",
|
|
1602
|
+
"feeDisplay",
|
|
1603
|
+
"gasLimit",
|
|
1604
|
+
"feeGranter",
|
|
1605
|
+
"signer",
|
|
1606
|
+
"wrapped",
|
|
1607
|
+
],
|
|
1608
|
+
"view.chainTx",
|
|
1609
|
+
);
|
|
1610
|
+
if (!CHAIN_TX_RISK_LEVELS.includes(d.riskLevel)) fail("view.chainTx.riskLevel");
|
|
1611
|
+
if (
|
|
1612
|
+
typeof d.confirmTwice !== "boolean" ||
|
|
1613
|
+
d.confirmTwice !== (d.riskLevel === "high" || d.riskLevel === "critical")
|
|
1614
|
+
)
|
|
1615
|
+
fail("view.chainTx.confirmTwice");
|
|
1616
|
+
if (
|
|
1617
|
+
!Array.isArray(d.steps) ||
|
|
1618
|
+
d.steps.length < 1 ||
|
|
1619
|
+
d.steps.length > CHAIN_TX_MAX_MESSAGES
|
|
1620
|
+
)
|
|
1621
|
+
fail("view.chainTx.steps");
|
|
1622
|
+
let highest = 0;
|
|
1623
|
+
for (const [i, step] of d.steps.entries()) {
|
|
1624
|
+
object(step, "view.chainTx.step");
|
|
1625
|
+
exact(
|
|
1626
|
+
step,
|
|
1627
|
+
[
|
|
1628
|
+
"index",
|
|
1629
|
+
"type",
|
|
1630
|
+
"title",
|
|
1631
|
+
"whatItDoes",
|
|
1632
|
+
"facts",
|
|
1633
|
+
"riskLevel",
|
|
1634
|
+
"cannotUndo",
|
|
1635
|
+
"transfer",
|
|
1636
|
+
],
|
|
1637
|
+
"view.chainTx.step",
|
|
1638
|
+
);
|
|
1639
|
+
if (step.index !== i + 1) fail("view.chainTx.step.index");
|
|
1640
|
+
if (!CHAIN_TX_MESSAGE_TYPES.includes(step.type)) fail("view.chainTx.step.type");
|
|
1641
|
+
str(step.title, "view.chainTx.step.title", 1, 60);
|
|
1642
|
+
str(step.whatItDoes, "view.chainTx.step.whatItDoes", 1, 160);
|
|
1643
|
+
if (!Array.isArray(step.facts) || step.facts.length > 40)
|
|
1644
|
+
fail("view.chainTx.step.facts");
|
|
1645
|
+
for (const fact of step.facts) {
|
|
1646
|
+
object(fact, "view.chainTx.fact");
|
|
1647
|
+
exact(fact, ["label", "value", "technical"], "view.chainTx.fact");
|
|
1648
|
+
str(fact.label, "view.chainTx.fact.label", 1, 60);
|
|
1649
|
+
str(fact.value, "view.chainTx.fact.value", 1, 300);
|
|
1650
|
+
if (fact.technical !== undefined && typeof fact.technical !== "boolean")
|
|
1651
|
+
fail("view.chainTx.fact.technical");
|
|
1652
|
+
}
|
|
1653
|
+
const risk = CHAIN_TX_RISK_LEVELS.indexOf(step.riskLevel);
|
|
1654
|
+
if (risk < 0) fail("view.chainTx.step.riskLevel");
|
|
1655
|
+
highest = Math.max(highest, risk);
|
|
1656
|
+
if (typeof step.cannotUndo !== "boolean") fail("view.chainTx.step.cannotUndo");
|
|
1657
|
+
if (step.transfer !== undefined) {
|
|
1658
|
+
if (step.type !== "cosmos.bank.send") fail("view.chainTx.step.transfer");
|
|
1659
|
+
parseTransferDisplay(step.transfer);
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
if (CHAIN_TX_RISK_LEVELS[highest] !== d.riskLevel)
|
|
1663
|
+
fail("view.chainTx.riskLevel");
|
|
1664
|
+
str(d.consent, "view.chainTx.consent", 1, 160);
|
|
1665
|
+
pattern(d.feeBaseUnits, INT, "view.chainTx.feeBaseUnits");
|
|
1666
|
+
if (d.feeDenom !== "uixo") fail("view.chainTx.feeDenom");
|
|
1667
|
+
str(d.feeDisplay, "view.chainTx.feeDisplay", 1, 80);
|
|
1668
|
+
num(d.gasLimit, "view.chainTx.gasLimit");
|
|
1669
|
+
if (d.feeGranter !== null)
|
|
1670
|
+
pattern(d.feeGranter, ADDRESS, "view.chainTx.feeGranter");
|
|
1671
|
+
pattern(d.signer, ACCOUNT_ADDRESS, "view.chainTx.signer");
|
|
1672
|
+
if (typeof d.wrapped !== "boolean") fail("view.chainTx.wrapped");
|
|
1673
|
+
return d;
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
function parseChainTxReceipt(r) {
|
|
1677
|
+
exact(
|
|
1678
|
+
r,
|
|
1679
|
+
[
|
|
1680
|
+
"kind",
|
|
1681
|
+
"actionId",
|
|
1682
|
+
"revision",
|
|
1683
|
+
"digest",
|
|
1684
|
+
"chainId",
|
|
1685
|
+
"txHash",
|
|
1686
|
+
"height",
|
|
1687
|
+
"code",
|
|
1688
|
+
"reason",
|
|
1689
|
+
"message",
|
|
1690
|
+
"steps",
|
|
1691
|
+
],
|
|
1692
|
+
"receipt",
|
|
1693
|
+
);
|
|
1694
|
+
pattern(r.actionId, ID, "receipt.actionId");
|
|
1695
|
+
num(r.revision, "receipt.revision");
|
|
1696
|
+
pattern(r.digest, HEX, "receipt.digest");
|
|
1697
|
+
str(r.chainId, "receipt.chainId", 1, 100);
|
|
1698
|
+
pattern(r.txHash, HEX, "receipt.txHash");
|
|
1699
|
+
if (r.height !== null) num(r.height, "receipt.height");
|
|
1700
|
+
if (r.code !== null && (!Number.isSafeInteger(r.code) || r.code < 0))
|
|
1701
|
+
fail("receipt.code");
|
|
1702
|
+
if ("reason" in r) {
|
|
1703
|
+
const failedCode = r.code !== null && r.code !== 0;
|
|
1704
|
+
if (
|
|
1705
|
+
!CHAIN_TX_FAILURE_REASONS.includes(r.reason) ||
|
|
1706
|
+
(r.reason === "check_tx_rejected" && (r.height !== null || !failedCode)) ||
|
|
1707
|
+
(r.reason === "expired_not_included" &&
|
|
1708
|
+
(r.height !== null || r.code !== null)) ||
|
|
1709
|
+
(r.reason === "tx_failed" && (r.height === null || !failedCode))
|
|
1710
|
+
)
|
|
1711
|
+
fail("receipt.reason");
|
|
1712
|
+
}
|
|
1713
|
+
// Plain words for the person, only on a failure.
|
|
1714
|
+
if ("message" in r) {
|
|
1715
|
+
if (!("reason" in r)) fail("receipt.message");
|
|
1716
|
+
str(r.message, "receipt.message", 1, 240);
|
|
1717
|
+
}
|
|
1718
|
+
if (
|
|
1719
|
+
!Array.isArray(r.steps) ||
|
|
1720
|
+
r.steps.length < 1 ||
|
|
1721
|
+
r.steps.length > CHAIN_TX_MAX_MESSAGES
|
|
1722
|
+
)
|
|
1723
|
+
fail("receipt.steps");
|
|
1724
|
+
for (const step of r.steps) {
|
|
1725
|
+
object(step, "receipt.step");
|
|
1726
|
+
exact(step, ["type"], "receipt.step");
|
|
1727
|
+
if (!CHAIN_TX_MESSAGE_TYPES.includes(step.type)) fail("receipt.step.type");
|
|
1728
|
+
}
|
|
1729
|
+
return r;
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
export * from "./outcome-signature.js";
|
|
1733
|
+
|
|
1734
|
+
/** The exact 0.1.0 receipt shape (v1 callers): drops the 0.3.0 bank fields. */
|
|
1735
|
+
export function v1Receipt(receipt) {
|
|
1736
|
+
if (!receipt || receipt.kind !== "ixo.bank.send.v1") return receipt;
|
|
1737
|
+
const { denom: _denom, amountBaseUnits: _amount, ...rest } = receipt;
|
|
1738
|
+
return rest;
|
|
1190
1739
|
}
|