@ixo/action-protocol 0.7.0 → 0.9.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 +415 -0
- package/README.md +35 -4
- package/package.json +1 -1
- package/src/index.d.ts +810 -4
- package/src/index.js +1101 -39
- package/src/operation-schema.js +392 -2
package/src/index.js
CHANGED
|
@@ -445,14 +445,15 @@ export function parseReceipt(input) {
|
|
|
445
445
|
tuple(receipt);
|
|
446
446
|
if (!SUBMISSION_FAILURE_STEPS.includes(receipt.step)) fail("receipt.step");
|
|
447
447
|
// Plain words the Hub chose (never raw text): shown to the person and
|
|
448
|
-
// relayed by gateways and runtimes. Already in sanitized form.
|
|
449
|
-
|
|
448
|
+
// relayed by gateways and runtimes. Already in sanitized form (the 0.7.0
|
|
449
|
+
// rule, so receipts stored before 0.8.0 keep parsing; the Hub's 0.8.0
|
|
450
|
+
// sanitizer also drops tag characters, and its output passes this too).
|
|
451
|
+
if (!isLegacyClean(str(receipt.message, "receipt.message", 1, 500)))
|
|
450
452
|
fail("receipt.message");
|
|
451
453
|
// Optional technical text, for the review page's closed disclosure only.
|
|
452
454
|
if (
|
|
453
455
|
"detail" in receipt &&
|
|
454
|
-
|
|
455
|
-
receipt.detail
|
|
456
|
+
!isLegacyClean(str(receipt.detail, "receipt.detail", 1, SUBMISSION_TEXT_MAX))
|
|
456
457
|
)
|
|
457
458
|
fail("receipt.detail");
|
|
458
459
|
num(receipt.failedAt, "receipt.failedAt");
|
|
@@ -498,7 +499,15 @@ export function parseReviewView(input) {
|
|
|
498
499
|
display.signer !== view.operation.signer ||
|
|
499
500
|
display.steps.length !== view.operation.messages.length ||
|
|
500
501
|
display.steps.some(
|
|
501
|
-
(step, i) =>
|
|
502
|
+
(step, i) =>
|
|
503
|
+
step.type !== view.operation.messages[i].type ||
|
|
504
|
+
// 0.9.0: a new proposal's step shows exactly its actions, in order.
|
|
505
|
+
(step.type === "daodao.proposal.create" &&
|
|
506
|
+
(step.actions.length !== view.operation.messages[i].actions.length ||
|
|
507
|
+
step.actions.some(
|
|
508
|
+
(action, j) =>
|
|
509
|
+
action.type !== view.operation.messages[i].actions[j].type,
|
|
510
|
+
))),
|
|
502
511
|
)
|
|
503
512
|
)
|
|
504
513
|
fail("view.chainTx");
|
|
@@ -511,7 +520,7 @@ export function parseReviewView(input) {
|
|
|
511
520
|
// 0.7.0: the collection's name as the Hub read it when the card was made.
|
|
512
521
|
if (view.collectionName !== undefined && view.collectionName !== null) {
|
|
513
522
|
if (!SUBMISSION_KINDS.includes(view.operation?.kind)) fail("view.collectionName");
|
|
514
|
-
if (
|
|
523
|
+
if (!isLegacyClean(str(view.collectionName, "view.collectionName", 1, 120)))
|
|
515
524
|
fail("view.collectionName");
|
|
516
525
|
}
|
|
517
526
|
if (view.service !== undefined && view.service !== null) {
|
|
@@ -1022,9 +1031,16 @@ export const SUBMISSION_TEXT_MAX = 500;
|
|
|
1022
1031
|
const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/gu;
|
|
1023
1032
|
// Invisible and direction-changing characters are dropped: zero-width
|
|
1024
1033
|
// characters, bidi embeddings, overrides and isolates, word joiners and
|
|
1025
|
-
// invisible operators, variation selectors, the BOM, soft hyphen
|
|
1026
|
-
// Hangul and Mongolian fillers.
|
|
1034
|
+
// invisible operators, variation selectors, the BOM, soft hyphen, the
|
|
1035
|
+
// Hangul and Mongolian fillers, and (0.8.0) the Unicode tag characters
|
|
1036
|
+
// (U+E0000-E007F, which can carry text no one sees) and the musical-symbol
|
|
1037
|
+
// format characters (U+1D173-1D17A).
|
|
1027
1038
|
const INVISIBLE_CHARACTERS =
|
|
1039
|
+
/[\u00ad\u034f\u061c\u115f\u1160\u17b4\u17b5\u180b-\u180f\u200b-\u200f\u202a-\u202e\u2060-\u206f\u3164\ufe00-\ufe0f\ufeff\uffa0\u{1d173}-\u{1d17a}\u{e0000}-\u{e007f}]/gu;
|
|
1040
|
+
// The 0.7.0 set (without the tag characters and the musical-symbol format
|
|
1041
|
+
// characters 0.8.0 adds): a value the 0.7.0 sanitizer left unchanged still
|
|
1042
|
+
// parses, so receipts and views stored before 0.8.0 keep parsing.
|
|
1043
|
+
const LEGACY_INVISIBLE_CHARACTERS =
|
|
1028
1044
|
/[\u00ad\u034f\u061c\u115f\u1160\u17b4\u17b5\u180b-\u180f\u200b-\u200f\u202a-\u202e\u2060-\u206f\u3164\ufe00-\ufe0f\ufeff\uffa0]/gu;
|
|
1029
1045
|
// Anything a chat app or browser could turn into a link: a URL with a
|
|
1030
1046
|
// scheme, a www. host, an email address, or a dotted host name ending in a
|
|
@@ -1034,6 +1050,12 @@ const INVISIBLE_CHARACTERS =
|
|
|
1034
1050
|
// backtracking); the input is also cut to SANITIZE_INPUT_MAX first.
|
|
1035
1051
|
const LINK_LIKE =
|
|
1036
1052
|
/\b[a-z][a-z0-9+.-]{0,31}:\/\/\S{0,2048}|\bwww\.\S{0,2048}|\b[^\s@]{1,64}@[^\s@]{1,255}\.[^\s@]{1,63}|\b(?:[a-z0-9-]{1,63}\.){1,16}[a-z]{2,63}\b(?::\d{1,5})?(?:\/\S{0,2048})?/giu;
|
|
1053
|
+
// 0.8.0: what counts as a link in free text published on chain (SHOWN_AS_IS):
|
|
1054
|
+
// a URL with a scheme, a www. host, an email address, or a dotted host
|
|
1055
|
+
// followed by a path ("pay.example/now"). A plain file name ("Annual
|
|
1056
|
+
// Report.pdf") or a version ("v2.0.1") is not one. Bounded like LINK_LIKE.
|
|
1057
|
+
const PUBLISHED_LINK_LIKE =
|
|
1058
|
+
/\b[a-z][a-z0-9+.-]{0,31}:\/\/\S{0,2048}|\bwww\.\S{0,2048}|\b[^\s@]{1,64}@[^\s@]{1,255}\.[^\s@]{1,63}|\b(?:[a-z0-9-]{1,63}\.){1,16}[a-z]{2,63}(?::\d{1,5})?\/\S{0,2048}/giu;
|
|
1037
1059
|
/** At most this much input is looked at (4 x SUBMISSION_TEXT_MAX). */
|
|
1038
1060
|
const SANITIZE_INPUT_MAX = 2000;
|
|
1039
1061
|
/**
|
|
@@ -1044,30 +1066,63 @@ const SANITIZE_INPUT_MAX = 2000;
|
|
|
1044
1066
|
* comes out unchanged, so a parser can check a value is already safe.
|
|
1045
1067
|
*/
|
|
1046
1068
|
export function sanitizeSubmissionText(text) {
|
|
1069
|
+
return sanitizeWith(text, LINK_LIKE, INVISIBLE_CHARACTERS);
|
|
1070
|
+
}
|
|
1071
|
+
/**
|
|
1072
|
+
* 0.8.0: free text published on chain as the review page shows it
|
|
1073
|
+
* (SHOWN_AS_IS): as sanitizeSubmissionText, but only real links become
|
|
1074
|
+
* `[link]` (a scheme, www., an email address, or a host followed by a path),
|
|
1075
|
+
* so a file name such as "Annual Report.pdf" stays as it is. Idempotent.
|
|
1076
|
+
*/
|
|
1077
|
+
export function sanitizePublishedText(text) {
|
|
1078
|
+
return sanitizeWith(text, PUBLISHED_LINK_LIKE, INVISIBLE_CHARACTERS);
|
|
1079
|
+
}
|
|
1080
|
+
/** True when the 0.7.0 sanitizer leaves `text` unchanged (stored data still parses). */
|
|
1081
|
+
const isLegacyClean = (text) =>
|
|
1082
|
+
sanitizeWith(text, LINK_LIKE, LEGACY_INVISIBLE_CHARACTERS) === text;
|
|
1083
|
+
function sanitizeWith(text, linkLike, invisible) {
|
|
1047
1084
|
// Bounded input before any expression runs; then unpaired surrogates (also
|
|
1048
1085
|
// one the cut split) are dropped, so every pass sees well-formed text.
|
|
1049
|
-
let out = sanitizeOnce(
|
|
1086
|
+
let out = sanitizeOnce(
|
|
1087
|
+
String(text).slice(0, SANITIZE_INPUT_MAX),
|
|
1088
|
+
linkLike,
|
|
1089
|
+
invisible,
|
|
1090
|
+
);
|
|
1050
1091
|
// A pass can only change the text again at its cut tail (a new link-like
|
|
1051
1092
|
// tail, or a "[link]" pushed over the limit), so it settles within a few
|
|
1052
1093
|
// passes; SANITIZE_PASSES is a hard bound. Should it ever not settle, the
|
|
1053
1094
|
// empty string (itself stable) is returned: the result is ALWAYS a fixed
|
|
1054
1095
|
// point, so sanitize(sanitize(x)) === sanitize(x) and a parser can check it.
|
|
1055
1096
|
for (let pass = 0; pass < SANITIZE_PASSES; pass++) {
|
|
1056
|
-
const next = sanitizeOnce(out);
|
|
1097
|
+
const next = sanitizeOnce(out, linkLike, invisible);
|
|
1057
1098
|
if (next === out) return out;
|
|
1058
1099
|
out = next;
|
|
1059
1100
|
}
|
|
1060
1101
|
return "";
|
|
1061
1102
|
}
|
|
1062
1103
|
const SANITIZE_PASSES = 8;
|
|
1104
|
+
/**
|
|
1105
|
+
* 0.8.0: a raw value (an id, an address, a type URL) without anything that
|
|
1106
|
+
* could make it render as something else, for a technical fact that must
|
|
1107
|
+
* show it exactly: control characters become spaces and invisible or
|
|
1108
|
+
* direction-changing characters are dropped (the sets
|
|
1109
|
+
* `sanitizeSubmissionText` uses), but nothing link-like is replaced and
|
|
1110
|
+
* nothing is cut.
|
|
1111
|
+
*/
|
|
1112
|
+
export function stripHiddenCharacters(text) {
|
|
1113
|
+
return String(text)
|
|
1114
|
+
.replace(UNPAIRED_SURROGATES, "")
|
|
1115
|
+
.replace(CONTROL_CHARACTERS, " ")
|
|
1116
|
+
.replace(INVISIBLE_CHARACTERS, "");
|
|
1117
|
+
}
|
|
1063
1118
|
// A high surrogate not followed by a low one, or a low one not after a high one.
|
|
1064
1119
|
const UNPAIRED_SURROGATES = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g;
|
|
1065
|
-
function sanitizeOnce(text) {
|
|
1120
|
+
function sanitizeOnce(text, linkLike, invisible) {
|
|
1066
1121
|
const cleaned = text
|
|
1067
1122
|
.replace(UNPAIRED_SURROGATES, "")
|
|
1068
1123
|
.replace(CONTROL_CHARACTERS, " ")
|
|
1069
|
-
.replace(
|
|
1070
|
-
.replace(
|
|
1124
|
+
.replace(invisible, "")
|
|
1125
|
+
.replace(linkLike, "[link]")
|
|
1071
1126
|
.replace(/\s+/gu, " ")
|
|
1072
1127
|
.trim();
|
|
1073
1128
|
if (cleaned.length <= SUBMISSION_TEXT_MAX) return cleaned;
|
|
@@ -1571,6 +1626,12 @@ function receiptFitsStatus(receipt, status) {
|
|
|
1571
1626
|
// 0.6.0: a chain tx receipt succeeds only included with code 0; a failure
|
|
1572
1627
|
// always names its reason (tx_failed = included with code != 0).
|
|
1573
1628
|
if (receipt.kind === CHAIN_TX_KIND) {
|
|
1629
|
+
// 0.9.0: a proposal id or group result belongs to a succeeded receipt only.
|
|
1630
|
+
if (
|
|
1631
|
+
status !== "succeeded" &&
|
|
1632
|
+
receipt.steps.some((step) => "proposalId" in step || "groupActions" in step)
|
|
1633
|
+
)
|
|
1634
|
+
return false;
|
|
1574
1635
|
if (status === "succeeded")
|
|
1575
1636
|
return (
|
|
1576
1637
|
receipt.code === 0 && receipt.height !== null && !("reason" in receipt)
|
|
@@ -1668,12 +1729,119 @@ export function parseTransferDisplay(input) {
|
|
|
1668
1729
|
|
|
1669
1730
|
/** One card kind for every chain transaction (1..8 messages, all or nothing). */
|
|
1670
1731
|
export const CHAIN_TX_KIND = "ixo.chain.tx.v1";
|
|
1671
|
-
|
|
1732
|
+
/** 0.6.0: the message types wire levels 4 and 5 may see (exactly these four). */
|
|
1733
|
+
export const CHAIN_TX_LEVEL4_MESSAGE_TYPES = Object.freeze([
|
|
1672
1734
|
"cosmos.bank.send",
|
|
1673
1735
|
"ixo.claims.submit",
|
|
1674
1736
|
"ixo.claims.evaluate",
|
|
1675
1737
|
"ixo.token.transfer",
|
|
1676
|
-
];
|
|
1738
|
+
]);
|
|
1739
|
+
/**
|
|
1740
|
+
* 0.9.0: the 33 message types wire level 6 may see: the level-4 types, then
|
|
1741
|
+
* the 29 types of 0.8.0.
|
|
1742
|
+
*/
|
|
1743
|
+
export const CHAIN_TX_LEVEL6_MESSAGE_TYPES = Object.freeze([
|
|
1744
|
+
...CHAIN_TX_LEVEL4_MESSAGE_TYPES,
|
|
1745
|
+
"ixo.token.retire",
|
|
1746
|
+
"cosmos.staking.delegate",
|
|
1747
|
+
"cosmos.staking.undelegate",
|
|
1748
|
+
"cosmos.staking.redelegate",
|
|
1749
|
+
"cosmos.distribution.withdraw-rewards",
|
|
1750
|
+
"cosmos.gov.vote",
|
|
1751
|
+
"ixo.claims.claim-intent",
|
|
1752
|
+
"ixo.claims.dispute",
|
|
1753
|
+
"ixo.claims.update-collection-state",
|
|
1754
|
+
"ixo.claims.update-collection-dates",
|
|
1755
|
+
"ixo.claims.update-collection-quota",
|
|
1756
|
+
"ixo.claims.create-claim-authorization",
|
|
1757
|
+
"ixo.claims.update-collection-payments",
|
|
1758
|
+
"ixo.claims.create-collection",
|
|
1759
|
+
"ixo.entity.create-account",
|
|
1760
|
+
"ixo.entity.update",
|
|
1761
|
+
"ixo.entity.revoke-account-authz",
|
|
1762
|
+
"ixo.entity.grant-account-authz",
|
|
1763
|
+
"ixo.entity.create",
|
|
1764
|
+
"ixo.iid.add-service",
|
|
1765
|
+
"ixo.iid.delete-service",
|
|
1766
|
+
"ixo.iid.add-linked-resource",
|
|
1767
|
+
"ixo.iid.delete-linked-resource",
|
|
1768
|
+
"ixo.iid.add-linked-claim",
|
|
1769
|
+
"ixo.iid.delete-linked-claim",
|
|
1770
|
+
"ixo.iid.add-linked-entity",
|
|
1771
|
+
"ixo.iid.delete-linked-entity",
|
|
1772
|
+
"ixo.iid.add-context",
|
|
1773
|
+
"ixo.iid.delete-context",
|
|
1774
|
+
]);
|
|
1775
|
+
/**
|
|
1776
|
+
* 0.9.0 (wire level 7): steps on a DAO DAO group's single-choice proposals
|
|
1777
|
+
* (contracts v2.0.3). The person signs; a card holding one holds nothing
|
|
1778
|
+
* else (D5).
|
|
1779
|
+
*/
|
|
1780
|
+
export const CHAIN_TX_GROUP_MESSAGE_TYPES = Object.freeze([
|
|
1781
|
+
"daodao.proposal.vote",
|
|
1782
|
+
"daodao.proposal.execute",
|
|
1783
|
+
"daodao.proposal.close",
|
|
1784
|
+
"daodao.proposal.create",
|
|
1785
|
+
]);
|
|
1786
|
+
/**
|
|
1787
|
+
* Every message type this package parses: the 33 types of wire level 6,
|
|
1788
|
+
* then the four group types of 0.9.0 (wire level 7). What a caller may see
|
|
1789
|
+
* or create is `chainTxMessageTypesForLevel(level)`, never this list.
|
|
1790
|
+
*/
|
|
1791
|
+
export const CHAIN_TX_MESSAGE_TYPES = Object.freeze([
|
|
1792
|
+
...CHAIN_TX_LEVEL6_MESSAGE_TYPES,
|
|
1793
|
+
...CHAIN_TX_GROUP_MESSAGE_TYPES,
|
|
1794
|
+
]);
|
|
1795
|
+
/** The wire level that adds the 29 types of 0.8.0. */
|
|
1796
|
+
export const CHAIN_TX_LEVEL6 = 6;
|
|
1797
|
+
/** The wire level that adds the four group types of 0.9.0. */
|
|
1798
|
+
export const CHAIN_TX_LEVEL7 = 7;
|
|
1799
|
+
const NO_CHAIN_TX_TYPES = Object.freeze([]);
|
|
1800
|
+
/**
|
|
1801
|
+
* The chain tx message types a caller at wire `level` may see in
|
|
1802
|
+
* capabilities and create: none below 4 (no chain tx), the four 0.6.0 types
|
|
1803
|
+
* at 4 and 5, the 33 at 6, all 37 from 7. One rule for the Hub, runtimes and
|
|
1804
|
+
* tests.
|
|
1805
|
+
*/
|
|
1806
|
+
export function chainTxMessageTypesForLevel(level) {
|
|
1807
|
+
if (!Number.isSafeInteger(level) || level < 1) fail("level");
|
|
1808
|
+
if (level < 4) return NO_CHAIN_TX_TYPES;
|
|
1809
|
+
if (level < CHAIN_TX_LEVEL6) return CHAIN_TX_LEVEL4_MESSAGE_TYPES;
|
|
1810
|
+
if (level < CHAIN_TX_LEVEL7) return CHAIN_TX_LEVEL6_MESSAGE_TYPES;
|
|
1811
|
+
return CHAIN_TX_MESSAGE_TYPES;
|
|
1812
|
+
}
|
|
1813
|
+
/**
|
|
1814
|
+
* 0.9.0 D11: what a group's own account may do in a proposal
|
|
1815
|
+
* (`daodao.proposal.create` `actions`): every signer field of these can be
|
|
1816
|
+
* the group's address, and none writes a value read from chain at propose
|
|
1817
|
+
* time (it runs days later). The person's own identity (claims, disputes),
|
|
1818
|
+
* a relayer (entity create), an identity document's keys (iid) and whole
|
|
1819
|
+
* writes from reads (payments, grants, entity update) are left out.
|
|
1820
|
+
*/
|
|
1821
|
+
export const CHAIN_TX_GROUP_ACTION_TYPES = Object.freeze([
|
|
1822
|
+
"cosmos.bank.send",
|
|
1823
|
+
"ixo.token.transfer",
|
|
1824
|
+
"ixo.token.retire",
|
|
1825
|
+
"cosmos.staking.delegate",
|
|
1826
|
+
"cosmos.staking.undelegate",
|
|
1827
|
+
"cosmos.staking.redelegate",
|
|
1828
|
+
"cosmos.distribution.withdraw-rewards",
|
|
1829
|
+
"cosmos.gov.vote",
|
|
1830
|
+
"ixo.claims.update-collection-state",
|
|
1831
|
+
"ixo.claims.update-collection-dates",
|
|
1832
|
+
"ixo.claims.update-collection-quota",
|
|
1833
|
+
"ixo.claims.create-collection",
|
|
1834
|
+
"ixo.entity.create-account",
|
|
1835
|
+
"ixo.entity.revoke-account-authz",
|
|
1836
|
+
]);
|
|
1837
|
+
/** D12: at most this many actions in a new proposal. */
|
|
1838
|
+
export const CHAIN_TX_MAX_GROUP_ACTIONS = 4;
|
|
1839
|
+
/** 0.9.0 §7: at most this many of a passed proposal's actions on a carry-out step. */
|
|
1840
|
+
export const CHAIN_TX_MAX_SHOWN_GROUP_ACTIONS = 10;
|
|
1841
|
+
/** `daodao.proposal.vote` `vote` (single choice: no veto option). */
|
|
1842
|
+
export const CHAIN_TX_GROUP_VOTE_OPTIONS = Object.freeze(["yes", "no", "abstain"]);
|
|
1843
|
+
/** Receipt `groupActions` of a carry-out: whether the group's actions ran (D8). */
|
|
1844
|
+
export const CHAIN_TX_GROUP_ACTION_RESULTS = Object.freeze(["done", "failed", "unknown"]);
|
|
1677
1845
|
export const CHAIN_TX_MAX_MESSAGES = 8;
|
|
1678
1846
|
export const CHAIN_TX_MAX_TOKEN_LINES = 32;
|
|
1679
1847
|
/** `check_tx_rejected`/`expired_not_included`: never included; `tx_failed`: included with code != 0. */
|
|
@@ -1685,6 +1853,163 @@ export const CHAIN_TX_FAILURE_REASONS = [
|
|
|
1685
1853
|
export const CHAIN_TX_RISK_LEVELS = ["low", "medium", "high", "critical"];
|
|
1686
1854
|
/** EvaluationStatus values a chain tx may record (0 pending and 5 flagged are refused). */
|
|
1687
1855
|
export const CHAIN_TX_EVALUATION_STATUSES = [1, 2, 3, 4];
|
|
1856
|
+
// 0.8.0 enums: names on the wire; the Hub maps them to the chain's numbers.
|
|
1857
|
+
/** `cosmos.gov.vote` `option`: YES 1, NO 3, ABSTAIN 2, NO_WITH_VETO 4. */
|
|
1858
|
+
export const CHAIN_TX_VOTE_OPTIONS = Object.freeze([
|
|
1859
|
+
"yes",
|
|
1860
|
+
"no",
|
|
1861
|
+
"abstain",
|
|
1862
|
+
"no_with_veto",
|
|
1863
|
+
]);
|
|
1864
|
+
/** `ixo.claims.dispute` `reason` (Portal's four; `check_wrong` targets the checker). */
|
|
1865
|
+
export const CHAIN_TX_DISPUTE_REASONS = Object.freeze([
|
|
1866
|
+
"not_as_asked",
|
|
1867
|
+
"not_delivered",
|
|
1868
|
+
"check_wrong",
|
|
1869
|
+
"other",
|
|
1870
|
+
]);
|
|
1871
|
+
/** Collection `state`: OPEN 0, PAUSED 1, CLOSED 2. */
|
|
1872
|
+
export const CHAIN_TX_COLLECTION_STATES = Object.freeze([
|
|
1873
|
+
"open",
|
|
1874
|
+
"paused",
|
|
1875
|
+
"closed",
|
|
1876
|
+
]);
|
|
1877
|
+
/** `ixo.claims.create-collection` `reservations` (intents): ALLOW 0, DENY 1, REQUIRED 2. */
|
|
1878
|
+
export const CHAIN_TX_RESERVATION_MODES = Object.freeze([
|
|
1879
|
+
"allow",
|
|
1880
|
+
"deny",
|
|
1881
|
+
"required",
|
|
1882
|
+
]);
|
|
1883
|
+
/** `ixo.claims.create-claim-authorization` `role`: SUBMIT 1, EVALUATE 2. */
|
|
1884
|
+
export const CHAIN_TX_CLAIM_ROLES = Object.freeze(["file-claims", "check-claims"]);
|
|
1885
|
+
/**
|
|
1886
|
+
* `ixo.entity.grant-account-authz` / `revoke-account-authz` `permission`: the
|
|
1887
|
+
* only grants an entity's `admin` account can give through a card.
|
|
1888
|
+
*/
|
|
1889
|
+
export const CHAIN_TX_ENTITY_PERMISSIONS = Object.freeze([
|
|
1890
|
+
"change-collection-state",
|
|
1891
|
+
"change-collection-dates",
|
|
1892
|
+
"change-collection-quota",
|
|
1893
|
+
"change-collection-payments",
|
|
1894
|
+
"authorize-claim-agents",
|
|
1895
|
+
"file-claims",
|
|
1896
|
+
"check-claims",
|
|
1897
|
+
]);
|
|
1898
|
+
/** Collection payment legs (`payments.<leg>`). */
|
|
1899
|
+
export const CHAIN_TX_PAYMENT_LEGS = Object.freeze([
|
|
1900
|
+
"submission",
|
|
1901
|
+
"evaluation",
|
|
1902
|
+
"approval",
|
|
1903
|
+
"rejection",
|
|
1904
|
+
]);
|
|
1905
|
+
/** At most this many coins per payment leg (distinct denoms). */
|
|
1906
|
+
export const CHAIN_TX_MAX_PAYMENT_COINS = 4;
|
|
1907
|
+
/** `payAfterSeconds` / `reservationSeconds`: 0 .. 365 days. */
|
|
1908
|
+
export const CHAIN_TX_MAX_SECONDS = 31536000;
|
|
1909
|
+
/** `maxClaims` of a claim permission: 1 .. 1 000 000 000. */
|
|
1910
|
+
export const CHAIN_TX_MAX_CLAIM_PERMISSION = 1000000000;
|
|
1911
|
+
/**
|
|
1912
|
+
* 0.8.0: why the Hub would not make a chain tx card from what it read on
|
|
1913
|
+
* chain (or from what the node's simulation said about one message). The
|
|
1914
|
+
* `reason` of the 400 then ends with one of these codes after the field:
|
|
1915
|
+
* `invalid_message: messages[<i>].<field>: <code>`. A reason without a code
|
|
1916
|
+
* is a shape the parser refused (as in 0.6.0). Fixed codes, never chain
|
|
1917
|
+
* text: a caller maps them to its own words and treats an unknown code like
|
|
1918
|
+
* none (`parseChainTxRefusal`).
|
|
1919
|
+
*/
|
|
1920
|
+
export const CHAIN_TX_REFUSAL_CODES = Object.freeze([
|
|
1921
|
+
// Staking and rewards.
|
|
1922
|
+
"validator_not_found",
|
|
1923
|
+
"no_stake",
|
|
1924
|
+
"stake_too_small",
|
|
1925
|
+
"same_validator",
|
|
1926
|
+
"invalid_amount",
|
|
1927
|
+
"nothing_to_collect",
|
|
1928
|
+
// Voting.
|
|
1929
|
+
"proposal_not_found",
|
|
1930
|
+
"proposal_not_open",
|
|
1931
|
+
"invalid_vote_option",
|
|
1932
|
+
// Impact tokens.
|
|
1933
|
+
"token_not_found",
|
|
1934
|
+
// Claims and collections.
|
|
1935
|
+
"collection_not_found",
|
|
1936
|
+
"reservations_off",
|
|
1937
|
+
"reservation_exists",
|
|
1938
|
+
"claim_not_found",
|
|
1939
|
+
"claim_in_other_collection",
|
|
1940
|
+
"wrong_admin",
|
|
1941
|
+
"already_in_state",
|
|
1942
|
+
"start_date_missing",
|
|
1943
|
+
"end_before_start",
|
|
1944
|
+
"quota_below_count",
|
|
1945
|
+
"token_payments_present",
|
|
1946
|
+
// Permissions (claims and entity grants).
|
|
1947
|
+
"ends_too_soon",
|
|
1948
|
+
"deadline_too_soon",
|
|
1949
|
+
"current_permission_ends_too_soon",
|
|
1950
|
+
"unlimited_permission_exists",
|
|
1951
|
+
"other_permission_exists",
|
|
1952
|
+
"too_many_constraints",
|
|
1953
|
+
"no_such_permission",
|
|
1954
|
+
"collection_of_other_entity",
|
|
1955
|
+
// Entities and identity documents.
|
|
1956
|
+
"entity_not_found",
|
|
1957
|
+
"account_name_taken",
|
|
1958
|
+
"document_not_found",
|
|
1959
|
+
"nothing_to_remove",
|
|
1960
|
+
"already_exists",
|
|
1961
|
+
"invalid_did",
|
|
1962
|
+
// The card as a whole.
|
|
1963
|
+
"not_available",
|
|
1964
|
+
"chain_data_too_large",
|
|
1965
|
+
// 0.9.0 groups (`proposal_not_found`, `proposal_not_open` and
|
|
1966
|
+
// `chain_data_too_large` above are reused). Nested proposal actions use
|
|
1967
|
+
// their own codes under `actions[<j>]`, e.g.
|
|
1968
|
+
// `messages[0].actions[1].validatorAddress: validator_not_found`.
|
|
1969
|
+
"group_not_supported",
|
|
1970
|
+
"not_in_group",
|
|
1971
|
+
"group_paused",
|
|
1972
|
+
"no_voting_power",
|
|
1973
|
+
"already_voted",
|
|
1974
|
+
"same_vote",
|
|
1975
|
+
"not_a_member",
|
|
1976
|
+
"proposal_not_passed",
|
|
1977
|
+
"proposal_not_rejected",
|
|
1978
|
+
"group_actions_would_fail",
|
|
1979
|
+
"deposit_not_supported",
|
|
1980
|
+
"proposal_too_large",
|
|
1981
|
+
"not_for_groups",
|
|
1982
|
+
"not_group_owned",
|
|
1983
|
+
// 0.9.0 review fixes (appended): a fixable wrong address, a switched-off
|
|
1984
|
+
// proposal list, voting that closes inside the signing window, a group
|
|
1985
|
+
// not taking proposals, a deposit that changed while the card was made.
|
|
1986
|
+
"group_not_found",
|
|
1987
|
+
"list_disabled",
|
|
1988
|
+
"closes_too_soon",
|
|
1989
|
+
"group_inactive",
|
|
1990
|
+
"deposit_changed",
|
|
1991
|
+
]);
|
|
1992
|
+
const CHAIN_TX_REFUSAL =
|
|
1993
|
+
/^invalid_message: messages\[(\d)\]\.([A-Za-z][A-Za-z0-9_.[\]]{0,99})(?:: ([a-z_]{1,64}))?$/;
|
|
1994
|
+
/**
|
|
1995
|
+
* 0.8.0: a chain tx refusal `reason` as `{index, field, code}`: the message
|
|
1996
|
+
* index, its field (a path such as `payments.approval`) and the refusal code
|
|
1997
|
+
* (null when the reason has none, or one this package does not know). Null
|
|
1998
|
+
* when the reason does not name a message field.
|
|
1999
|
+
*/
|
|
2000
|
+
export function parseChainTxRefusal(reason) {
|
|
2001
|
+
if (typeof reason !== "string") return null;
|
|
2002
|
+
const match = CHAIN_TX_REFUSAL.exec(reason);
|
|
2003
|
+
if (!match) return null;
|
|
2004
|
+
const index = Number(match[1]);
|
|
2005
|
+
if (index >= CHAIN_TX_MAX_MESSAGES) return null;
|
|
2006
|
+
const code = match[3] ?? null;
|
|
2007
|
+
return {
|
|
2008
|
+
index,
|
|
2009
|
+
field: match[2],
|
|
2010
|
+
code: code !== null && CHAIN_TX_REFUSAL_CODES.includes(code) ? code : null,
|
|
2011
|
+
};
|
|
2012
|
+
}
|
|
1688
2013
|
// The chain's DID rule (x/iid/types/iid.go didValidationRegexpStr): segments of
|
|
1689
2014
|
// [A-Za-z0-9._-] or a `%` followed by two hex digits, joined by ':', the last
|
|
1690
2015
|
// one non-empty. Anchored at both ends here (the chain's own regex anchors
|
|
@@ -1730,7 +2055,203 @@ const CHAIN_TX_MESSAGE_KEYS = {
|
|
|
1730
2055
|
"verificationProof",
|
|
1731
2056
|
],
|
|
1732
2057
|
"ixo.token.transfer": ["type", "recipient", "tokens"],
|
|
2058
|
+
// 0.8.0 (wire level 6). Signer-derived chain fields (owner, delegator,
|
|
2059
|
+
// voter, signer, ...) are never wire keys: the Hub fills them from `signer`.
|
|
2060
|
+
"ixo.token.retire": ["type", "tokens", "jurisdiction", "reason"],
|
|
2061
|
+
"cosmos.staking.delegate": ["type", "validatorAddress", "amountBaseUnits"],
|
|
2062
|
+
"cosmos.staking.undelegate": ["type", "validatorAddress", "amountBaseUnits"],
|
|
2063
|
+
"cosmos.staking.redelegate": [
|
|
2064
|
+
"type",
|
|
2065
|
+
"fromValidatorAddress",
|
|
2066
|
+
"toValidatorAddress",
|
|
2067
|
+
"amountBaseUnits",
|
|
2068
|
+
],
|
|
2069
|
+
"cosmos.distribution.withdraw-rewards": ["type", "validatorAddress"],
|
|
2070
|
+
"cosmos.gov.vote": ["type", "proposalId", "option"],
|
|
2071
|
+
"ixo.claims.claim-intent": ["type", "collectionId", "agentDid"],
|
|
2072
|
+
"ixo.claims.dispute": [
|
|
2073
|
+
"type",
|
|
2074
|
+
"claimId",
|
|
2075
|
+
"collectionId",
|
|
2076
|
+
"agentDid",
|
|
2077
|
+
"reason",
|
|
2078
|
+
"evidence",
|
|
2079
|
+
],
|
|
2080
|
+
"ixo.claims.update-collection-state": [
|
|
2081
|
+
"type",
|
|
2082
|
+
"collectionId",
|
|
2083
|
+
"adminAddress",
|
|
2084
|
+
"state",
|
|
2085
|
+
],
|
|
2086
|
+
"ixo.claims.update-collection-dates": [
|
|
2087
|
+
"type",
|
|
2088
|
+
"collectionId",
|
|
2089
|
+
"adminAddress",
|
|
2090
|
+
"startDate",
|
|
2091
|
+
"endDate",
|
|
2092
|
+
],
|
|
2093
|
+
"ixo.claims.update-collection-quota": [
|
|
2094
|
+
"type",
|
|
2095
|
+
"collectionId",
|
|
2096
|
+
"adminAddress",
|
|
2097
|
+
"maxClaims",
|
|
2098
|
+
],
|
|
2099
|
+
"ixo.claims.create-claim-authorization": [
|
|
2100
|
+
"type",
|
|
2101
|
+
"collectionId",
|
|
2102
|
+
"adminAddress",
|
|
2103
|
+
"granteeAddress",
|
|
2104
|
+
"role",
|
|
2105
|
+
"maxClaims",
|
|
2106
|
+
"expiresAt",
|
|
2107
|
+
"reservationSeconds",
|
|
2108
|
+
"decideBefore",
|
|
2109
|
+
"creatorDid",
|
|
2110
|
+
],
|
|
2111
|
+
"ixo.claims.update-collection-payments": [
|
|
2112
|
+
"type",
|
|
2113
|
+
"collectionId",
|
|
2114
|
+
"adminAddress",
|
|
2115
|
+
"payments",
|
|
2116
|
+
],
|
|
2117
|
+
"ixo.claims.create-collection": [
|
|
2118
|
+
"type",
|
|
2119
|
+
"entityId",
|
|
2120
|
+
"protocolId",
|
|
2121
|
+
"startDate",
|
|
2122
|
+
"endDate",
|
|
2123
|
+
"maxClaims",
|
|
2124
|
+
"state",
|
|
2125
|
+
"reservations",
|
|
2126
|
+
"payments",
|
|
2127
|
+
],
|
|
2128
|
+
"ixo.entity.create-account": ["type", "entityId", "name"],
|
|
2129
|
+
// D20: no `status` in 0.8.0 (its meaning is client-defined).
|
|
2130
|
+
"ixo.entity.update": [
|
|
2131
|
+
"type",
|
|
2132
|
+
"entityId",
|
|
2133
|
+
"startDate",
|
|
2134
|
+
"endDate",
|
|
2135
|
+
"controllerDid",
|
|
2136
|
+
],
|
|
2137
|
+
"ixo.entity.revoke-account-authz": [
|
|
2138
|
+
"type",
|
|
2139
|
+
"entityId",
|
|
2140
|
+
"granteeAddress",
|
|
2141
|
+
"permission",
|
|
2142
|
+
],
|
|
2143
|
+
"ixo.entity.grant-account-authz": [
|
|
2144
|
+
"type",
|
|
2145
|
+
"entityId",
|
|
2146
|
+
"granteeAddress",
|
|
2147
|
+
"permission",
|
|
2148
|
+
"collectionId",
|
|
2149
|
+
"maxClaims",
|
|
2150
|
+
"reservationSeconds",
|
|
2151
|
+
"decideBefore",
|
|
2152
|
+
"expiresAt",
|
|
2153
|
+
],
|
|
2154
|
+
"ixo.entity.create": [
|
|
2155
|
+
"type",
|
|
2156
|
+
"entityType",
|
|
2157
|
+
"profile",
|
|
2158
|
+
"protocolId",
|
|
2159
|
+
"startDate",
|
|
2160
|
+
"endDate",
|
|
2161
|
+
"ownerDid",
|
|
2162
|
+
],
|
|
2163
|
+
"ixo.iid.add-service": ["type", "did", "service"],
|
|
2164
|
+
"ixo.iid.delete-service": ["type", "did", "serviceId"],
|
|
2165
|
+
"ixo.iid.add-linked-resource": ["type", "did", "resource"],
|
|
2166
|
+
"ixo.iid.delete-linked-resource": ["type", "did", "resourceId"],
|
|
2167
|
+
"ixo.iid.add-linked-claim": ["type", "did", "claim"],
|
|
2168
|
+
"ixo.iid.delete-linked-claim": ["type", "did", "claimId"],
|
|
2169
|
+
"ixo.iid.add-linked-entity": ["type", "did", "entity"],
|
|
2170
|
+
"ixo.iid.delete-linked-entity": ["type", "did", "entityId"],
|
|
2171
|
+
"ixo.iid.add-context": ["type", "did", "context"],
|
|
2172
|
+
"ixo.iid.delete-context": ["type", "did", "key"],
|
|
2173
|
+
// 0.9.0 (wire level 7). The person is the sender; the Hub derives the
|
|
2174
|
+
// proposal intake from `proposalModule` (D17).
|
|
2175
|
+
"daodao.proposal.vote": [
|
|
2176
|
+
"type",
|
|
2177
|
+
"dao",
|
|
2178
|
+
"proposalModule",
|
|
2179
|
+
"proposalId",
|
|
2180
|
+
"vote",
|
|
2181
|
+
"rationale",
|
|
2182
|
+
],
|
|
2183
|
+
"daodao.proposal.execute": ["type", "dao", "proposalModule", "proposalId"],
|
|
2184
|
+
"daodao.proposal.close": ["type", "dao", "proposalModule", "proposalId"],
|
|
2185
|
+
"daodao.proposal.create": [
|
|
2186
|
+
"type",
|
|
2187
|
+
"dao",
|
|
2188
|
+
"proposalModule",
|
|
2189
|
+
"title",
|
|
2190
|
+
"description",
|
|
2191
|
+
"actions",
|
|
2192
|
+
],
|
|
1733
2193
|
};
|
|
2194
|
+
// 0.8.0 wire patterns (the JSON schema in operation-schema.js mirrors them).
|
|
2195
|
+
// A validator operator address: `ixovaloper1` + 38 bech32 characters (20
|
|
2196
|
+
// data bytes); the Hub also checks the checksum.
|
|
2197
|
+
const VALOPER_ADDRESS = /^ixovaloper1[023456789acdefghjklmnpqrstuvwxyz]{38}$/;
|
|
2198
|
+
// x/entity ids: md5 hex of `<nftContract>/<sequence>`.
|
|
2199
|
+
const ENTITY_DID = /^did:ixo:entity:[0-9a-f]{32}$/;
|
|
2200
|
+
// RFC 3339 with `Z` or an offset, at most milliseconds (the schema `instant`).
|
|
2201
|
+
const INSTANT =
|
|
2202
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
2203
|
+
// The latest instant a protobuf Timestamp may hold (9999-12-31T23:59:59.999Z).
|
|
2204
|
+
const INSTANT_MAX_MS = Date.UTC(9999, 11, 31, 23, 59, 59, 999);
|
|
2205
|
+
// `maxClaims` of a claim permission: 1..10^9 (checked numerically below).
|
|
2206
|
+
const CLAIM_PERMISSION_COUNT = /^[1-9][0-9]{0,9}$/;
|
|
2207
|
+
// MsgRetireToken `jurisdiction`: the proto's documented `CC[-SUB[ POSTAL]]`
|
|
2208
|
+
// form, or `Global` (Portal's default). D13.
|
|
2209
|
+
const JURISDICTION =
|
|
2210
|
+
/^(?:Global|[A-Z]{2}(?:-[A-Za-z0-9]{1,3}(?: [A-Za-z0-9]{1,64})?)?)$/;
|
|
2211
|
+
// Dispute evidence `type`, e.g. `eval-engine/v1`.
|
|
2212
|
+
const EVIDENCE_TYPE = /^[A-Za-z0-9][A-Za-z0-9.+/_-]{0,99}$/;
|
|
2213
|
+
// Links on chain. https: a host with no user info (`https://a@b` reads as
|
|
2214
|
+
// `a` but goes to `b`); ipfs: a CID. No whitespace or backslashes anywhere.
|
|
2215
|
+
const HTTPS_URL = /^https:\/\/[^\s/?#@\\]+(?:[/?#][^\s\\]*)?$/;
|
|
2216
|
+
const IPFS_URL = /^ipfs:\/\/[A-Za-z0-9]+(?:\/[^\s\\]*)?$/;
|
|
2217
|
+
const URL_MAX = 300;
|
|
2218
|
+
// MsgCreateEntity `entity_type`: `dao`, `project`, `oracle`, `protocol/claim`.
|
|
2219
|
+
const ENTITY_TYPE = /^[a-z][a-z0-9_-]{0,31}(?:\/[a-z][a-z0-9_-]{0,31})?$/;
|
|
2220
|
+
// Entity account names (`admin` is refused separately, in any case).
|
|
2221
|
+
const ACCOUNT_NAME = /^[A-Za-z0-9](?:[A-Za-z0-9 _-]{0,62}[A-Za-z0-9])?$/;
|
|
2222
|
+
// x/iid item ids for an add: Portal writes `{id}#…` literally (the chain
|
|
2223
|
+
// stores it as is); the name after `#` may hold `:` (Portal's `#app:<id>`).
|
|
2224
|
+
const ITEM_ID = /^(?:\{id\}|did:ixo:[A-Za-z0-9._:%-]{1,160})#[A-Za-z0-9._~:-]{1,64}$/;
|
|
2225
|
+
const ITEM_ID_MAX = 233;
|
|
2226
|
+
// D28: the id of an item to remove is whatever the document holds (Portal
|
|
2227
|
+
// stored `#api` with no prefix, `{id}#ixo1…`, ids without `#`), so any 1..300
|
|
2228
|
+
// characters without whitespace or CONTROL. The Hub's required document read
|
|
2229
|
+
// proves the exact id exists before a card is made.
|
|
2230
|
+
const REMOVE_ID_MAX = 300;
|
|
2231
|
+
// `+`: Portal's `application/vnd.ixo.design-pod+json` resource type.
|
|
2232
|
+
const IID_TYPE = /^[A-Za-z][A-Za-z0-9._:/+-]{0,63}$/;
|
|
2233
|
+
const MEDIA_TYPE = /^[a-z]+\/[A-Za-z0-9.+-]{1,90}$/;
|
|
2234
|
+
const MEDIA_TYPE_MAX = 128;
|
|
2235
|
+
// D28: mixed case (Portal writes `Governs`).
|
|
2236
|
+
const RELATIONSHIP = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
2237
|
+
const CONTEXT_KEY = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
2238
|
+
// A linked entity's `service`: a service name (`svc1`, `matrix`,
|
|
2239
|
+
// `did:cosmos:ixo` as stored on chain) or a DID URL reference to one
|
|
2240
|
+
// (`{id}#matrix`); `""` means none (Portal's default).
|
|
2241
|
+
const SERVICE_REF =
|
|
2242
|
+
/^(?:(?:(?:\{id\}|did:ixo:[A-Za-z0-9._:%-]{1,160})#)?[A-Za-z0-9._~:-]{1,64})?$/;
|
|
2243
|
+
// D28: a linked entity is an IXO DID, an ixo account (20 bytes) or contract
|
|
2244
|
+
// (32 bytes) address, or Portal's `{id}#<address>` (a group's core contract).
|
|
2245
|
+
// The Hub checks the address checksum.
|
|
2246
|
+
const LINKED_ENTITY_ADDRESS =
|
|
2247
|
+
/^(?:\{id\}#)?ixo1(?:[023456789acdefghjklmnpqrstuvwxyz]{38}|[023456789acdefghjklmnpqrstuvwxyz]{58})$/;
|
|
2248
|
+
const CLAIM_PERMISSIONS = ["file-claims", "check-claims"];
|
|
2249
|
+
// 0.9.0: a 32-byte contract address (`ixo1` + 58 bech32 characters); the
|
|
2250
|
+
// Hub also checks the checksum.
|
|
2251
|
+
const CONTRACT_ADDRESS = /^ixo1[023456789acdefghjklmnpqrstuvwxyz]{58}$/;
|
|
2252
|
+
// D16: a group proposal id, decimal without leading zeros, 1..2^53-1 (the
|
|
2253
|
+
// Hub writes it as a JSON number; checked numerically below).
|
|
2254
|
+
const GROUP_PROPOSAL_ID = /^[1-9][0-9]{0,15}$/;
|
|
1734
2255
|
const plain = (value) =>
|
|
1735
2256
|
value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1736
2257
|
const isText = (value, min = 1, max = 200) =>
|
|
@@ -1741,9 +2262,474 @@ const isId = (value, max) => isMatch(value, CHAIN_ID_CHARS, max);
|
|
|
1741
2262
|
const isCollectionId = (value) =>
|
|
1742
2263
|
isMatch(value, COLLECTION_ID, 20) && BigInt(value) <= UINT64_MAX;
|
|
1743
2264
|
const utf8Length = (value) => new TextEncoder().encode(value).length;
|
|
2265
|
+
/** UINT64_TEXT: a uint64 in decimal, no leading zeros. */
|
|
2266
|
+
const isUint64Text = isCollectionId;
|
|
2267
|
+
/** PLAIN_TEXT(max): 1..max characters, not blank, no CONTROL characters. */
|
|
2268
|
+
const isPlainText = (value, max) =>
|
|
2269
|
+
isText(value, 1, max) && !CONTROL.test(value) && /\S/.test(value);
|
|
2270
|
+
/**
|
|
2271
|
+
* 0.8.0 SHOWN_AS_IS: free text published on chain must already be exactly
|
|
2272
|
+
* what the review page shows, i.e. `sanitizePublishedText` leaves it
|
|
2273
|
+
* unchanged: no control, invisible or direction-changing characters (soft
|
|
2274
|
+
* hyphen, word joiners, variation selectors, tag characters, ...), no real
|
|
2275
|
+
* link (a scheme, www., an email address, a host followed by a path; a file
|
|
2276
|
+
* name such as "Annual Report.pdf" is fine), single spaces and no space at
|
|
2277
|
+
* either end.
|
|
2278
|
+
*/
|
|
2279
|
+
const isShownAsIs = (value) =>
|
|
2280
|
+
typeof value === "string" && sanitizePublishedText(value) === value;
|
|
2281
|
+
/** PROPOSAL_ID: a group proposal id (D16). */
|
|
2282
|
+
const isGroupProposalId = (value) =>
|
|
2283
|
+
isMatch(value, GROUP_PROPOSAL_ID, 16) &&
|
|
2284
|
+
BigInt(value) <= BigInt(Number.MAX_SAFE_INTEGER);
|
|
2285
|
+
/**
|
|
2286
|
+
* INSTANT: a real calendar instant whose UTC time a protobuf Timestamp can
|
|
2287
|
+
* hold the way the Hub encodes it (1970-01-01T00:00:00Z .. year 9999).
|
|
2288
|
+
* Returns its epoch milliseconds, or null.
|
|
2289
|
+
*/
|
|
2290
|
+
function instantMs(value) {
|
|
2291
|
+
if (!isMatch(value, INSTANT, 40) || !validCalendarInstant(value)) return null;
|
|
2292
|
+
const ms = Date.parse(value);
|
|
2293
|
+
return Number.isFinite(ms) && ms >= 0 && ms <= INSTANT_MAX_MS ? ms : null;
|
|
2294
|
+
}
|
|
2295
|
+
const isInstant = (value) => instantMs(value) !== null;
|
|
2296
|
+
const isSeconds = (value) =>
|
|
2297
|
+
Number.isSafeInteger(value) && value >= 0 && value <= CHAIN_TX_MAX_SECONDS;
|
|
2298
|
+
const isClaimCount = (value) =>
|
|
2299
|
+
isMatch(value, CLAIM_PERMISSION_COUNT, 10) &&
|
|
2300
|
+
Number(value) <= CHAIN_TX_MAX_CLAIM_PERMISSION;
|
|
2301
|
+
/** An https:// (and, when allowed, ipfs://) link of at most 300 characters. */
|
|
2302
|
+
function isLink(value, { ipfs }) {
|
|
2303
|
+
if (!isText(value, 1, URL_MAX) || CONTROL.test(value)) return false;
|
|
2304
|
+
if (ipfs && IPFS_URL.test(value)) return true;
|
|
2305
|
+
if (!HTTPS_URL.test(value)) return false;
|
|
2306
|
+
try {
|
|
2307
|
+
const url = new URL(value);
|
|
2308
|
+
return (
|
|
2309
|
+
url.protocol === "https:" &&
|
|
2310
|
+
url.hostname !== "" &&
|
|
2311
|
+
url.username === "" &&
|
|
2312
|
+
url.password === ""
|
|
2313
|
+
);
|
|
2314
|
+
} catch {
|
|
2315
|
+
return false;
|
|
2316
|
+
}
|
|
2317
|
+
}
|
|
2318
|
+
/** The first key of `value` not in `keys`, as `${at}.${key}`, or null. */
|
|
2319
|
+
function unknownKey(value, keys, at) {
|
|
2320
|
+
for (const key of Object.keys(value))
|
|
2321
|
+
if (!keys.includes(key)) return `${at}.${key}`;
|
|
2322
|
+
return null;
|
|
2323
|
+
}
|
|
2324
|
+
/** Impact token lines (`ixo.token.transfer`, `ixo.token.retire`). */
|
|
2325
|
+
function tokenLinesField(tokens) {
|
|
2326
|
+
if (
|
|
2327
|
+
!Array.isArray(tokens) ||
|
|
2328
|
+
tokens.length < 1 ||
|
|
2329
|
+
tokens.length > CHAIN_TX_MAX_TOKEN_LINES
|
|
2330
|
+
)
|
|
2331
|
+
return "tokens";
|
|
2332
|
+
for (const [j, token] of tokens.entries()) {
|
|
2333
|
+
if (!plain(token)) return `tokens[${j}]`;
|
|
2334
|
+
for (const key of Object.keys(token))
|
|
2335
|
+
if (key !== "id" && key !== "amount") return `tokens[${j}].${key}`;
|
|
2336
|
+
if (!isId(token.id, 128)) return `tokens[${j}].id`;
|
|
2337
|
+
if (!isMatch(token.amount, BASE_UNITS, 78)) return `tokens[${j}].amount`;
|
|
2338
|
+
}
|
|
2339
|
+
return null;
|
|
2340
|
+
}
|
|
2341
|
+
/**
|
|
2342
|
+
* Collection payments (the 0.8.0 subset): each leg optional; a leg is
|
|
2343
|
+
* `{amount: [{denom, amountBaseUnits}] (0..4, distinct denoms),
|
|
2344
|
+
* payAfterSeconds?}`, and the approval leg may add `networkSplit`.
|
|
2345
|
+
*/
|
|
2346
|
+
function paymentsField(payments) {
|
|
2347
|
+
if (!plain(payments)) return "payments";
|
|
2348
|
+
const extra = unknownKey(payments, CHAIN_TX_PAYMENT_LEGS, "payments");
|
|
2349
|
+
if (extra) return extra;
|
|
2350
|
+
for (const leg of CHAIN_TX_PAYMENT_LEGS) {
|
|
2351
|
+
const value = payments[leg];
|
|
2352
|
+
if (value === undefined) continue;
|
|
2353
|
+
const at = `payments.${leg}`;
|
|
2354
|
+
if (!plain(value)) return at;
|
|
2355
|
+
const keys =
|
|
2356
|
+
leg === "approval"
|
|
2357
|
+
? ["amount", "payAfterSeconds", "networkSplit"]
|
|
2358
|
+
: ["amount", "payAfterSeconds"];
|
|
2359
|
+
const unknown = unknownKey(value, keys, at);
|
|
2360
|
+
if (unknown) return unknown;
|
|
2361
|
+
if (
|
|
2362
|
+
!Array.isArray(value.amount) ||
|
|
2363
|
+
value.amount.length > CHAIN_TX_MAX_PAYMENT_COINS
|
|
2364
|
+
)
|
|
2365
|
+
return `${at}.amount`;
|
|
2366
|
+
const denoms = new Set();
|
|
2367
|
+
for (const [j, coin] of value.amount.entries()) {
|
|
2368
|
+
const c = `${at}.amount[${j}]`;
|
|
2369
|
+
if (!plain(coin)) return c;
|
|
2370
|
+
const bad = unknownKey(coin, ["denom", "amountBaseUnits"], c);
|
|
2371
|
+
if (bad) return bad;
|
|
2372
|
+
if (!isMatch(coin.denom, DENOM, 128) || denoms.has(coin.denom))
|
|
2373
|
+
return `${c}.denom`;
|
|
2374
|
+
denoms.add(coin.denom);
|
|
2375
|
+
if (!isMatch(coin.amountBaseUnits, BASE_UNITS, 78))
|
|
2376
|
+
return `${c}.amountBaseUnits`;
|
|
2377
|
+
}
|
|
2378
|
+
if (value.payAfterSeconds !== undefined && !isSeconds(value.payAfterSeconds))
|
|
2379
|
+
return `${at}.payAfterSeconds`;
|
|
2380
|
+
if (
|
|
2381
|
+
value.networkSplit !== undefined &&
|
|
2382
|
+
typeof value.networkSplit !== "boolean"
|
|
2383
|
+
)
|
|
2384
|
+
return `${at}.networkSplit`;
|
|
2385
|
+
}
|
|
2386
|
+
return null;
|
|
2387
|
+
}
|
|
2388
|
+
/**
|
|
2389
|
+
* Start/end dates. `endNullable`: `null` = no end date. `required`: both keys
|
|
2390
|
+
* must be present (create-collection); otherwise each is optional, and when
|
|
2391
|
+
* `atLeastOne` neither present names `startDate`. A given end at or before a
|
|
2392
|
+
* given start names `endDate`.
|
|
2393
|
+
*/
|
|
2394
|
+
function datesField(m, { endNullable, required = false, atLeastOne = false }) {
|
|
2395
|
+
if (required ? !isInstant(m.startDate) : m.startDate !== undefined && !isInstant(m.startDate))
|
|
2396
|
+
return "startDate";
|
|
2397
|
+
if (
|
|
2398
|
+
(required && m.endDate === undefined) ||
|
|
2399
|
+
(m.endDate !== undefined &&
|
|
2400
|
+
!(endNullable && m.endDate === null) &&
|
|
2401
|
+
!isInstant(m.endDate))
|
|
2402
|
+
)
|
|
2403
|
+
return "endDate";
|
|
2404
|
+
if (atLeastOne && m.startDate === undefined && m.endDate === undefined)
|
|
2405
|
+
return "startDate";
|
|
2406
|
+
if (
|
|
2407
|
+
typeof m.startDate === "string" &&
|
|
2408
|
+
typeof m.endDate === "string" &&
|
|
2409
|
+
instantMs(m.endDate) <= instantMs(m.startDate)
|
|
2410
|
+
)
|
|
2411
|
+
return "endDate";
|
|
2412
|
+
return null;
|
|
2413
|
+
}
|
|
2414
|
+
/** A nested object with exactly `keys` (fields checked by `rules`, in order). */
|
|
2415
|
+
function nestedField(value, at, keys, rules) {
|
|
2416
|
+
if (!plain(value)) return at;
|
|
2417
|
+
const unknown = unknownKey(value, keys, at);
|
|
2418
|
+
if (unknown) return unknown;
|
|
2419
|
+
for (const [key, ok] of rules) if (!ok(value[key])) return `${at}.${key}`;
|
|
2420
|
+
return null;
|
|
2421
|
+
}
|
|
2422
|
+
const optionalRule = (rule) => (value) => value === undefined || rule(value);
|
|
2423
|
+
const linkRule = (value) => isLink(value, { ipfs: true });
|
|
2424
|
+
const itemIdRule = (value) => isMatch(value, ITEM_ID, ITEM_ID_MAX);
|
|
2425
|
+
const removeIdRule = (value) =>
|
|
2426
|
+
isText(value, 1, REMOVE_ID_MAX) && !/\s/.test(value) && !CONTROL.test(value);
|
|
2427
|
+
const iidTypeRule = (value) => isMatch(value, IID_TYPE, 64);
|
|
2428
|
+
const proofRule = (value) => isId(value, 300);
|
|
2429
|
+
// D28: linked resources and claims as Portal stores them: the proof may be
|
|
2430
|
+
// "" (app links), the description "" or up to 300 (form titles), `right` "".
|
|
2431
|
+
const linkProofRule = (value) => value === "" || isId(value, 300);
|
|
2432
|
+
const descriptionRule = (value) =>
|
|
2433
|
+
isText(value, 0, 300) && !CONTROL.test(value) && isShownAsIs(value);
|
|
2434
|
+
const rightRule = optionalRule(
|
|
2435
|
+
(value) => value === "" || (isPlainText(value, 300) && isShownAsIs(value)),
|
|
2436
|
+
);
|
|
2437
|
+
const booleanRule = (value) => typeof value === "boolean";
|
|
2438
|
+
const ixoDidRule = (value) => isMatch(value, IXO_DID);
|
|
2439
|
+
|
|
2440
|
+
/** 0.8.0: the first bad field of each new message type (relative), or null. */
|
|
2441
|
+
const CHAIN_TX_LEVEL6_FIELDS = {
|
|
2442
|
+
"ixo.token.retire": (m) => {
|
|
2443
|
+
const tokens = tokenLinesField(m.tokens);
|
|
2444
|
+
if (tokens) return tokens;
|
|
2445
|
+
if (!isMatch(m.jurisdiction, JURISDICTION, 71)) return "jurisdiction";
|
|
2446
|
+
if (!isPlainText(m.reason, 200) || !isShownAsIs(m.reason)) return "reason";
|
|
2447
|
+
return null;
|
|
2448
|
+
},
|
|
2449
|
+
"cosmos.staking.delegate": (m) => stakeField(m),
|
|
2450
|
+
"cosmos.staking.undelegate": (m) => stakeField(m),
|
|
2451
|
+
"cosmos.staking.redelegate": (m) => {
|
|
2452
|
+
if (!isMatch(m.fromValidatorAddress, VALOPER_ADDRESS, 49))
|
|
2453
|
+
return "fromValidatorAddress";
|
|
2454
|
+
if (
|
|
2455
|
+
!isMatch(m.toValidatorAddress, VALOPER_ADDRESS, 49) ||
|
|
2456
|
+
m.toValidatorAddress === m.fromValidatorAddress
|
|
2457
|
+
)
|
|
2458
|
+
return "toValidatorAddress";
|
|
2459
|
+
if (!isMatch(m.amountBaseUnits, BASE_UNITS, 78)) return "amountBaseUnits";
|
|
2460
|
+
return null;
|
|
2461
|
+
},
|
|
2462
|
+
"cosmos.distribution.withdraw-rewards": (m) =>
|
|
2463
|
+
isMatch(m.validatorAddress, VALOPER_ADDRESS, 49) ? null : "validatorAddress",
|
|
2464
|
+
"cosmos.gov.vote": (m) => {
|
|
2465
|
+
if (!isUint64Text(m.proposalId) || m.proposalId === "0") return "proposalId";
|
|
2466
|
+
if (!CHAIN_TX_VOTE_OPTIONS.includes(m.option)) return "option";
|
|
2467
|
+
return null;
|
|
2468
|
+
},
|
|
2469
|
+
"ixo.claims.claim-intent": (m) => {
|
|
2470
|
+
if (!isCollectionId(m.collectionId)) return "collectionId";
|
|
2471
|
+
if (!isMatch(m.agentDid, IXO_DID)) return "agentDid";
|
|
2472
|
+
return null;
|
|
2473
|
+
},
|
|
2474
|
+
"ixo.claims.dispute": (m) => {
|
|
2475
|
+
if (!isId(m.claimId, 256)) return "claimId";
|
|
2476
|
+
if (!isCollectionId(m.collectionId)) return "collectionId";
|
|
2477
|
+
if (!isMatch(m.agentDid, IXO_DID)) return "agentDid";
|
|
2478
|
+
if (!CHAIN_TX_DISPUTE_REASONS.includes(m.reason)) return "reason";
|
|
2479
|
+
return nestedField(m.evidence, "evidence", ["uri", "type", "proof", "encrypted"], [
|
|
2480
|
+
["uri", linkRule],
|
|
2481
|
+
["type", (v) => isMatch(v, EVIDENCE_TYPE, 100)],
|
|
2482
|
+
["proof", proofRule],
|
|
2483
|
+
["encrypted", optionalRule(booleanRule)],
|
|
2484
|
+
]);
|
|
2485
|
+
},
|
|
2486
|
+
"ixo.claims.update-collection-state": (m) =>
|
|
2487
|
+
collectionAdminField(m) ??
|
|
2488
|
+
(CHAIN_TX_COLLECTION_STATES.includes(m.state) ? null : "state"),
|
|
2489
|
+
"ixo.claims.update-collection-dates": (m) =>
|
|
2490
|
+
collectionAdminField(m) ??
|
|
2491
|
+
datesField(m, { endNullable: true, atLeastOne: true }),
|
|
2492
|
+
"ixo.claims.update-collection-quota": (m) =>
|
|
2493
|
+
collectionAdminField(m) ?? (isUint64Text(m.maxClaims) ? null : "maxClaims"),
|
|
2494
|
+
"ixo.claims.create-claim-authorization": (m) => {
|
|
2495
|
+
const admin = collectionAdminField(m);
|
|
2496
|
+
if (admin) return admin;
|
|
2497
|
+
if (!isMatch(m.granteeAddress, ACCOUNT_ADDRESS)) return "granteeAddress";
|
|
2498
|
+
if (!CHAIN_TX_CLAIM_ROLES.includes(m.role)) return "role";
|
|
2499
|
+
if (!isClaimCount(m.maxClaims)) return "maxClaims";
|
|
2500
|
+
if (m.expiresAt !== undefined && !isInstant(m.expiresAt)) return "expiresAt";
|
|
2501
|
+
return claimConstraintField(m, m.role) ?? (isMatch(m.creatorDid, IXO_DID) ? null : "creatorDid");
|
|
2502
|
+
},
|
|
2503
|
+
"ixo.claims.update-collection-payments": (m) =>
|
|
2504
|
+
collectionAdminField(m) ?? paymentsField(m.payments),
|
|
2505
|
+
"ixo.claims.create-collection": (m) => {
|
|
2506
|
+
if (!isMatch(m.entityId, ENTITY_DID, 47)) return "entityId";
|
|
2507
|
+
if (!isMatch(m.protocolId, IXO_DID)) return "protocolId";
|
|
2508
|
+
const dates = datesField(m, { endNullable: true, required: true });
|
|
2509
|
+
if (dates) return dates;
|
|
2510
|
+
if (!isUint64Text(m.maxClaims)) return "maxClaims";
|
|
2511
|
+
if (!CHAIN_TX_COLLECTION_STATES.includes(m.state)) return "state";
|
|
2512
|
+
if (!CHAIN_TX_RESERVATION_MODES.includes(m.reservations)) return "reservations";
|
|
2513
|
+
return paymentsField(m.payments);
|
|
2514
|
+
},
|
|
2515
|
+
"ixo.entity.create-account": (m) => {
|
|
2516
|
+
if (!isMatch(m.entityId, ENTITY_DID, 47)) return "entityId";
|
|
2517
|
+
if (!isMatch(m.name, ACCOUNT_NAME, 64) || m.name.toLowerCase() === "admin")
|
|
2518
|
+
return "name";
|
|
2519
|
+
return null;
|
|
2520
|
+
},
|
|
2521
|
+
"ixo.entity.update": (m) => {
|
|
2522
|
+
if (!isMatch(m.entityId, ENTITY_DID, 47)) return "entityId";
|
|
2523
|
+
const dates = datesField(m, { endNullable: true, atLeastOne: true });
|
|
2524
|
+
if (dates) return dates;
|
|
2525
|
+
return isMatch(m.controllerDid, IXO_DID) ? null : "controllerDid";
|
|
2526
|
+
},
|
|
2527
|
+
"ixo.entity.revoke-account-authz": (m) => {
|
|
2528
|
+
if (!isMatch(m.entityId, ENTITY_DID, 47)) return "entityId";
|
|
2529
|
+
if (!isMatch(m.granteeAddress, ACCOUNT_ADDRESS)) return "granteeAddress";
|
|
2530
|
+
if (!CHAIN_TX_ENTITY_PERMISSIONS.includes(m.permission)) return "permission";
|
|
2531
|
+
return null;
|
|
2532
|
+
},
|
|
2533
|
+
"ixo.entity.grant-account-authz": (m) => {
|
|
2534
|
+
if (!isMatch(m.entityId, ENTITY_DID, 47)) return "entityId";
|
|
2535
|
+
if (!isMatch(m.granteeAddress, ACCOUNT_ADDRESS)) return "granteeAddress";
|
|
2536
|
+
if (!CHAIN_TX_ENTITY_PERMISSIONS.includes(m.permission)) return "permission";
|
|
2537
|
+
// collectionId and maxClaims: required for the claim permissions, refused otherwise.
|
|
2538
|
+
const claims = CLAIM_PERMISSIONS.includes(m.permission);
|
|
2539
|
+
if (claims ? !isCollectionId(m.collectionId) : m.collectionId !== undefined)
|
|
2540
|
+
return "collectionId";
|
|
2541
|
+
if (claims ? !isClaimCount(m.maxClaims) : m.maxClaims !== undefined)
|
|
2542
|
+
return "maxClaims";
|
|
2543
|
+
const constraint = claimConstraintField(m, m.permission);
|
|
2544
|
+
if (constraint) return constraint;
|
|
2545
|
+
if (m.expiresAt !== undefined && !isInstant(m.expiresAt)) return "expiresAt";
|
|
2546
|
+
return null;
|
|
2547
|
+
},
|
|
2548
|
+
"ixo.entity.create": (m) => {
|
|
2549
|
+
if (!isMatch(m.entityType, ENTITY_TYPE, 65)) return "entityType";
|
|
2550
|
+
if (m.profile !== undefined) {
|
|
2551
|
+
const profile = nestedField(m.profile, "profile", ["serviceEndpoint", "proof"], [
|
|
2552
|
+
["serviceEndpoint", (v) => isLink(v, { ipfs: false })],
|
|
2553
|
+
["proof", proofRule],
|
|
2554
|
+
]);
|
|
2555
|
+
if (profile) return profile;
|
|
2556
|
+
}
|
|
2557
|
+
if (m.protocolId !== undefined && !isMatch(m.protocolId, IXO_DID))
|
|
2558
|
+
return "protocolId";
|
|
2559
|
+
const dates = datesField(m, { endNullable: false });
|
|
2560
|
+
if (dates) return dates;
|
|
2561
|
+
return isMatch(m.ownerDid, IXO_DID) ? null : "ownerDid";
|
|
2562
|
+
},
|
|
2563
|
+
"ixo.iid.add-service": (m) =>
|
|
2564
|
+
iidDidField(m) ??
|
|
2565
|
+
nestedField(m.service, "service", ["id", "type", "serviceEndpoint"], [
|
|
2566
|
+
["id", itemIdRule],
|
|
2567
|
+
["type", iidTypeRule],
|
|
2568
|
+
["serviceEndpoint", linkRule],
|
|
2569
|
+
]),
|
|
2570
|
+
"ixo.iid.delete-service": (m) =>
|
|
2571
|
+
iidDidField(m) ?? (removeIdRule(m.serviceId) ? null : "serviceId"),
|
|
2572
|
+
"ixo.iid.add-linked-resource": (m) =>
|
|
2573
|
+
iidDidField(m) ??
|
|
2574
|
+
nestedField(
|
|
2575
|
+
m.resource,
|
|
2576
|
+
"resource",
|
|
2577
|
+
["id", "type", "description", "mediaType", "serviceEndpoint", "proof", "encrypted", "right"],
|
|
2578
|
+
[
|
|
2579
|
+
["id", itemIdRule],
|
|
2580
|
+
["type", iidTypeRule],
|
|
2581
|
+
["description", descriptionRule],
|
|
2582
|
+
["mediaType", (v) => isMatch(v, MEDIA_TYPE, MEDIA_TYPE_MAX)],
|
|
2583
|
+
["serviceEndpoint", linkRule],
|
|
2584
|
+
["proof", linkProofRule],
|
|
2585
|
+
["encrypted", booleanRule],
|
|
2586
|
+
["right", rightRule],
|
|
2587
|
+
],
|
|
2588
|
+
),
|
|
2589
|
+
"ixo.iid.delete-linked-resource": (m) =>
|
|
2590
|
+
iidDidField(m) ?? (removeIdRule(m.resourceId) ? null : "resourceId"),
|
|
2591
|
+
"ixo.iid.add-linked-claim": (m) =>
|
|
2592
|
+
iidDidField(m) ??
|
|
2593
|
+
nestedField(
|
|
2594
|
+
m.claim,
|
|
2595
|
+
"claim",
|
|
2596
|
+
["id", "type", "description", "serviceEndpoint", "proof", "encrypted", "right"],
|
|
2597
|
+
[
|
|
2598
|
+
["id", (v) => isId(v, 256)],
|
|
2599
|
+
["type", iidTypeRule],
|
|
2600
|
+
["description", descriptionRule],
|
|
2601
|
+
["serviceEndpoint", linkRule],
|
|
2602
|
+
["proof", linkProofRule],
|
|
2603
|
+
["encrypted", booleanRule],
|
|
2604
|
+
["right", rightRule],
|
|
2605
|
+
],
|
|
2606
|
+
),
|
|
2607
|
+
"ixo.iid.delete-linked-claim": (m) =>
|
|
2608
|
+
iidDidField(m) ?? (removeIdRule(m.claimId) ? null : "claimId"),
|
|
2609
|
+
"ixo.iid.add-linked-entity": (m) =>
|
|
2610
|
+
iidDidField(m) ??
|
|
2611
|
+
nestedField(m.entity, "entity", ["id", "type", "relationship", "service"], [
|
|
2612
|
+
["id", (v) => ixoDidRule(v) || isMatch(v, LINKED_ENTITY_ADDRESS, 67)],
|
|
2613
|
+
["type", iidTypeRule],
|
|
2614
|
+
["relationship", (v) => isMatch(v, RELATIONSHIP, 64)],
|
|
2615
|
+
[
|
|
2616
|
+
"service",
|
|
2617
|
+
optionalRule((v) => typeof v === "string" && v.length <= ITEM_ID_MAX && SERVICE_REF.test(v)),
|
|
2618
|
+
],
|
|
2619
|
+
]),
|
|
2620
|
+
"ixo.iid.delete-linked-entity": (m) =>
|
|
2621
|
+
iidDidField(m) ?? (removeIdRule(m.entityId) ? null : "entityId"),
|
|
2622
|
+
"ixo.iid.add-context": (m) =>
|
|
2623
|
+
iidDidField(m) ??
|
|
2624
|
+
nestedField(m.context, "context", ["key", "val"], [
|
|
2625
|
+
["key", (v) => isMatch(v, CONTEXT_KEY, 64)],
|
|
2626
|
+
["val", (v) => isLink(v, { ipfs: false }) || isMatch(v, IXO_DID, URL_MAX)],
|
|
2627
|
+
]),
|
|
2628
|
+
"ixo.iid.delete-context": (m) =>
|
|
2629
|
+
iidDidField(m) ?? (removeIdRule(m.key) ? null : "key"),
|
|
2630
|
+
};
|
|
2631
|
+
/** 0.9.0: the first bad field of each group type (relative), or null. */
|
|
2632
|
+
const CHAIN_TX_GROUP_FIELDS = {
|
|
2633
|
+
"daodao.proposal.vote": (m) => {
|
|
2634
|
+
const target = groupProposalField(m);
|
|
2635
|
+
if (target) return target;
|
|
2636
|
+
if (!CHAIN_TX_GROUP_VOTE_OPTIONS.includes(m.vote)) return "vote";
|
|
2637
|
+
if (m.rationale !== undefined && !isGroupText(m.rationale, 300))
|
|
2638
|
+
return "rationale";
|
|
2639
|
+
return null;
|
|
2640
|
+
},
|
|
2641
|
+
"daodao.proposal.execute": (m) => groupProposalField(m),
|
|
2642
|
+
"daodao.proposal.close": (m) => groupProposalField(m),
|
|
2643
|
+
"daodao.proposal.create": (m) => {
|
|
2644
|
+
const target = groupTargetField(m);
|
|
2645
|
+
if (target) return target;
|
|
2646
|
+
if (!isGroupText(m.title, 100)) return "title";
|
|
2647
|
+
if (!isGroupText(m.description, 300)) return "description";
|
|
2648
|
+
return groupActionsField(m.actions);
|
|
2649
|
+
},
|
|
2650
|
+
};
|
|
2651
|
+
/** D14/D15: proposal text and vote reasons are PLAIN_TEXT and SHOWN_AS_IS. */
|
|
2652
|
+
const isGroupText = (value, max) => isPlainText(value, max) && isShownAsIs(value);
|
|
2653
|
+
/** `dao` (the group's core) and `proposalModule` (D17), two different contracts. */
|
|
2654
|
+
function groupTargetField(m) {
|
|
2655
|
+
if (!isMatch(m.dao, CONTRACT_ADDRESS, 62)) return "dao";
|
|
2656
|
+
if (!isMatch(m.proposalModule, CONTRACT_ADDRESS, 62) || m.proposalModule === m.dao)
|
|
2657
|
+
return "proposalModule";
|
|
2658
|
+
return null;
|
|
2659
|
+
}
|
|
2660
|
+
function groupProposalField(m) {
|
|
2661
|
+
return groupTargetField(m) ?? (isGroupProposalId(m.proposalId) ? null : "proposalId");
|
|
2662
|
+
}
|
|
2663
|
+
/**
|
|
2664
|
+
* D11/D12: 0..4 actions the group's own account does if the proposal
|
|
2665
|
+
* passes, each exactly a top-level message of a `CHAIN_TX_GROUP_ACTION_TYPES`
|
|
2666
|
+
* type (same keys, same field rules; none of them has a field naming the
|
|
2667
|
+
* person, the Hub sets every signer field to `dao`). Paths are
|
|
2668
|
+
* `actions[<j>].<field>`.
|
|
2669
|
+
*/
|
|
2670
|
+
function groupActionsField(actions) {
|
|
2671
|
+
if (!Array.isArray(actions) || actions.length > CHAIN_TX_MAX_GROUP_ACTIONS)
|
|
2672
|
+
return "actions";
|
|
2673
|
+
for (const [j, action] of actions.entries()) {
|
|
2674
|
+
const at = `actions[${j}]`;
|
|
2675
|
+
if (!plain(action)) return at;
|
|
2676
|
+
if (!CHAIN_TX_GROUP_ACTION_TYPES.includes(action.type)) return `${at}.type`;
|
|
2677
|
+
const unknown = unknownKey(action, CHAIN_TX_MESSAGE_KEYS[action.type], at);
|
|
2678
|
+
if (unknown) return unknown;
|
|
2679
|
+
const field = chainTxMessageField(action) ?? groupActionRuleField(action);
|
|
2680
|
+
if (field) return `${at}.${field}`;
|
|
2681
|
+
}
|
|
2682
|
+
return null;
|
|
2683
|
+
}
|
|
2684
|
+
/**
|
|
2685
|
+
* The rule a group action adds to its type's: a collection dates change
|
|
2686
|
+
* names both dates (`endDate` may be null), because a date left out would be
|
|
2687
|
+
* filled from a read that is stale when the group carries it out (D11).
|
|
2688
|
+
*/
|
|
2689
|
+
function groupActionRuleField(action) {
|
|
2690
|
+
if (action.type !== "ixo.claims.update-collection-dates") return null;
|
|
2691
|
+
if (action.startDate === undefined) return "startDate";
|
|
2692
|
+
if (action.endDate === undefined) return "endDate";
|
|
2693
|
+
return null;
|
|
2694
|
+
}
|
|
2695
|
+
function stakeField(m) {
|
|
2696
|
+
if (!isMatch(m.validatorAddress, VALOPER_ADDRESS, 49)) return "validatorAddress";
|
|
2697
|
+
if (!isMatch(m.amountBaseUnits, BASE_UNITS, 78)) return "amountBaseUnits";
|
|
2698
|
+
return null;
|
|
2699
|
+
}
|
|
2700
|
+
/** The collection-admin types: `collectionId`, then `adminAddress` (D3). */
|
|
2701
|
+
function collectionAdminField(m) {
|
|
2702
|
+
if (!isCollectionId(m.collectionId)) return "collectionId";
|
|
2703
|
+
if (!isMatch(m.adminAddress, ACCOUNT_ADDRESS)) return "adminAddress";
|
|
2704
|
+
return null;
|
|
2705
|
+
}
|
|
2706
|
+
/**
|
|
2707
|
+
* Claim-permission constraint options: `reservationSeconds` only for
|
|
2708
|
+
* `file-claims` (intent duration), `decideBefore` only for `check-claims`.
|
|
2709
|
+
*/
|
|
2710
|
+
function claimConstraintField(m, role) {
|
|
2711
|
+
if (
|
|
2712
|
+
m.reservationSeconds !== undefined &&
|
|
2713
|
+
(role !== "file-claims" || !isSeconds(m.reservationSeconds))
|
|
2714
|
+
)
|
|
2715
|
+
return "reservationSeconds";
|
|
2716
|
+
if (
|
|
2717
|
+
m.decideBefore !== undefined &&
|
|
2718
|
+
(role !== "check-claims" || !isInstant(m.decideBefore))
|
|
2719
|
+
)
|
|
2720
|
+
return "decideBefore";
|
|
2721
|
+
return null;
|
|
2722
|
+
}
|
|
2723
|
+
function iidDidField(m) {
|
|
2724
|
+
return isMatch(m.did, IXO_DID) ? null : "did";
|
|
2725
|
+
}
|
|
1744
2726
|
|
|
1745
2727
|
/** The first bad field of one message (relative to the message), or null. */
|
|
1746
2728
|
function chainTxMessageField(m) {
|
|
2729
|
+
if (Object.hasOwn(CHAIN_TX_LEVEL6_FIELDS, m.type))
|
|
2730
|
+
return CHAIN_TX_LEVEL6_FIELDS[m.type](m);
|
|
2731
|
+
if (Object.hasOwn(CHAIN_TX_GROUP_FIELDS, m.type))
|
|
2732
|
+
return CHAIN_TX_GROUP_FIELDS[m.type](m);
|
|
1747
2733
|
if (m.type === "cosmos.bank.send") {
|
|
1748
2734
|
if (!isMatch(m.toAddress, ACCOUNT_ADDRESS)) return "toAddress";
|
|
1749
2735
|
if (!isMatch(m.amountBaseUnits, BASE_UNITS, 78)) return "amountBaseUnits";
|
|
@@ -1779,20 +2765,7 @@ function chainTxMessageField(m) {
|
|
|
1779
2765
|
}
|
|
1780
2766
|
// ixo.token.transfer
|
|
1781
2767
|
if (!isMatch(m.recipient, ACCOUNT_ADDRESS)) return "recipient";
|
|
1782
|
-
|
|
1783
|
-
!Array.isArray(m.tokens) ||
|
|
1784
|
-
m.tokens.length < 1 ||
|
|
1785
|
-
m.tokens.length > CHAIN_TX_MAX_TOKEN_LINES
|
|
1786
|
-
)
|
|
1787
|
-
return "tokens";
|
|
1788
|
-
for (const [j, token] of m.tokens.entries()) {
|
|
1789
|
-
if (!plain(token)) return `tokens[${j}]`;
|
|
1790
|
-
for (const key of Object.keys(token))
|
|
1791
|
-
if (key !== "id" && key !== "amount") return `tokens[${j}].${key}`;
|
|
1792
|
-
if (!isId(token.id, 128)) return `tokens[${j}].id`;
|
|
1793
|
-
if (!isMatch(token.amount, BASE_UNITS, 78)) return `tokens[${j}].amount`;
|
|
1794
|
-
}
|
|
1795
|
-
return null;
|
|
2768
|
+
return tokenLinesField(m.tokens);
|
|
1796
2769
|
}
|
|
1797
2770
|
|
|
1798
2771
|
/** The first bad field of a chain tx operation (`messages[1].toAddress`), or null. */
|
|
@@ -1817,6 +2790,14 @@ function chainTxField(op) {
|
|
|
1817
2790
|
op.messages.length > CHAIN_TX_MAX_MESSAGES
|
|
1818
2791
|
)
|
|
1819
2792
|
return "messages";
|
|
2793
|
+
// 0.9.0 D5: a card with a group step holds exactly that one message.
|
|
2794
|
+
if (
|
|
2795
|
+
op.messages.length > 1 &&
|
|
2796
|
+
op.messages.some(
|
|
2797
|
+
(m) => plain(m) && CHAIN_TX_GROUP_MESSAGE_TYPES.includes(m.type),
|
|
2798
|
+
)
|
|
2799
|
+
)
|
|
2800
|
+
return "messages";
|
|
1820
2801
|
for (const [i, m] of op.messages.entries()) {
|
|
1821
2802
|
const at = `messages[${i}]`;
|
|
1822
2803
|
if (!plain(m)) return at;
|
|
@@ -1850,7 +2831,11 @@ export function chainTxIssue(input) {
|
|
|
1850
2831
|
return null;
|
|
1851
2832
|
}
|
|
1852
2833
|
|
|
1853
|
-
/**
|
|
2834
|
+
/**
|
|
2835
|
+
* `GET /capabilities?version=4` `chainTx`. Accepts every type this package
|
|
2836
|
+
* knows; whether a type is allowed at the caller's level is the Hub's
|
|
2837
|
+
* `chainTxMessageTypesForLevel` rule.
|
|
2838
|
+
*/
|
|
1854
2839
|
export function parseChainTxCapabilities(input) {
|
|
1855
2840
|
const c = object(input, "capabilities.chainTx");
|
|
1856
2841
|
exact(c, ["messageTypes", "maxMessages"], "capabilities.chainTx");
|
|
@@ -1916,6 +2901,8 @@ export function parseChainTxDisplay(input) {
|
|
|
1916
2901
|
"riskLevel",
|
|
1917
2902
|
"cannotUndo",
|
|
1918
2903
|
"transfer",
|
|
2904
|
+
"actions",
|
|
2905
|
+
"proposalPrefix",
|
|
1919
2906
|
],
|
|
1920
2907
|
"view.chainTx.step",
|
|
1921
2908
|
);
|
|
@@ -1925,14 +2912,7 @@ export function parseChainTxDisplay(input) {
|
|
|
1925
2912
|
str(step.whatItDoes, "view.chainTx.step.whatItDoes", 1, 160);
|
|
1926
2913
|
if (!Array.isArray(step.facts) || step.facts.length > 40)
|
|
1927
2914
|
fail("view.chainTx.step.facts");
|
|
1928
|
-
for (const fact of step.facts)
|
|
1929
|
-
object(fact, "view.chainTx.fact");
|
|
1930
|
-
exact(fact, ["label", "value", "technical"], "view.chainTx.fact");
|
|
1931
|
-
str(fact.label, "view.chainTx.fact.label", 1, 60);
|
|
1932
|
-
str(fact.value, "view.chainTx.fact.value", 1, 300);
|
|
1933
|
-
if (fact.technical !== undefined && typeof fact.technical !== "boolean")
|
|
1934
|
-
fail("view.chainTx.fact.technical");
|
|
1935
|
-
}
|
|
2915
|
+
for (const fact of step.facts) parseChainTxFact(fact);
|
|
1936
2916
|
const risk = CHAIN_TX_RISK_LEVELS.indexOf(step.riskLevel);
|
|
1937
2917
|
if (risk < 0) fail("view.chainTx.step.riskLevel");
|
|
1938
2918
|
highest = Math.max(highest, risk);
|
|
@@ -1941,6 +2921,14 @@ export function parseChainTxDisplay(input) {
|
|
|
1941
2921
|
if (step.type !== "cosmos.bank.send") fail("view.chainTx.step.transfer");
|
|
1942
2922
|
parseTransferDisplay(step.transfer);
|
|
1943
2923
|
}
|
|
2924
|
+
parseGroupActionsDisplay(step, risk);
|
|
2925
|
+
// A new proposal's list prefix (1-4 capital letters), so the result can
|
|
2926
|
+
// name it as the cards do ("proposal A4"); absent when the list has none.
|
|
2927
|
+
if (step.proposalPrefix !== undefined) {
|
|
2928
|
+
if (step.type !== "daodao.proposal.create")
|
|
2929
|
+
fail("view.chainTx.step.proposalPrefix");
|
|
2930
|
+
pattern(step.proposalPrefix, /^[A-Z]{1,4}$/, "view.chainTx.step.proposalPrefix");
|
|
2931
|
+
}
|
|
1944
2932
|
}
|
|
1945
2933
|
if (CHAIN_TX_RISK_LEVELS[highest] !== d.riskLevel)
|
|
1946
2934
|
fail("view.chainTx.riskLevel");
|
|
@@ -1956,6 +2944,56 @@ export function parseChainTxDisplay(input) {
|
|
|
1956
2944
|
return d;
|
|
1957
2945
|
}
|
|
1958
2946
|
|
|
2947
|
+
function parseChainTxFact(fact) {
|
|
2948
|
+
object(fact, "view.chainTx.fact");
|
|
2949
|
+
exact(fact, ["label", "value", "technical"], "view.chainTx.fact");
|
|
2950
|
+
str(fact.label, "view.chainTx.fact.label", 1, 60);
|
|
2951
|
+
str(fact.value, "view.chainTx.fact.value", 1, 300);
|
|
2952
|
+
if (fact.technical !== undefined && typeof fact.technical !== "boolean")
|
|
2953
|
+
fail("view.chainTx.fact.technical");
|
|
2954
|
+
}
|
|
2955
|
+
|
|
2956
|
+
/**
|
|
2957
|
+
* 0.9.0 D13: what the group's own account would do, on create (its 0..4
|
|
2958
|
+
* actions, in order) and carry-out (the passed proposal's stored actions,
|
|
2959
|
+
* at most 10 shown; `type: null` for one the page cannot explain). Required
|
|
2960
|
+
* on those two step types (`[]` for none), refused on every other. The
|
|
2961
|
+
* step's risk is at least each action's.
|
|
2962
|
+
*/
|
|
2963
|
+
function parseGroupActionsDisplay(step, stepRisk) {
|
|
2964
|
+
const create = step.type === "daodao.proposal.create";
|
|
2965
|
+
if (!create && step.type !== "daodao.proposal.execute") {
|
|
2966
|
+
if (step.actions !== undefined) fail("view.chainTx.step.actions");
|
|
2967
|
+
return;
|
|
2968
|
+
}
|
|
2969
|
+
const max = create ? CHAIN_TX_MAX_GROUP_ACTIONS : CHAIN_TX_MAX_SHOWN_GROUP_ACTIONS;
|
|
2970
|
+
if (!Array.isArray(step.actions) || step.actions.length > max)
|
|
2971
|
+
fail("view.chainTx.step.actions");
|
|
2972
|
+
for (const action of step.actions) {
|
|
2973
|
+
object(action, "view.chainTx.action");
|
|
2974
|
+
exact(
|
|
2975
|
+
action,
|
|
2976
|
+
["type", "title", "whatItDoes", "facts", "riskLevel", "cannotUndo"],
|
|
2977
|
+
"view.chainTx.action",
|
|
2978
|
+
);
|
|
2979
|
+
if (
|
|
2980
|
+
action.type === null
|
|
2981
|
+
? create
|
|
2982
|
+
: !(create ? CHAIN_TX_GROUP_ACTION_TYPES : CHAIN_TX_LEVEL6_MESSAGE_TYPES).includes(action.type)
|
|
2983
|
+
)
|
|
2984
|
+
fail("view.chainTx.action.type");
|
|
2985
|
+
str(action.title, "view.chainTx.action.title", 1, 60);
|
|
2986
|
+
str(action.whatItDoes, "view.chainTx.action.whatItDoes", 1, 160);
|
|
2987
|
+
if (!Array.isArray(action.facts) || action.facts.length > 12)
|
|
2988
|
+
fail("view.chainTx.action.facts");
|
|
2989
|
+
for (const fact of action.facts) parseChainTxFact(fact);
|
|
2990
|
+
const risk = CHAIN_TX_RISK_LEVELS.indexOf(action.riskLevel);
|
|
2991
|
+
if (risk < 0 || risk > stepRisk) fail("view.chainTx.action.riskLevel");
|
|
2992
|
+
if (typeof action.cannotUndo !== "boolean")
|
|
2993
|
+
fail("view.chainTx.action.cannotUndo");
|
|
2994
|
+
}
|
|
2995
|
+
}
|
|
2996
|
+
|
|
1959
2997
|
function parseChainTxReceipt(r) {
|
|
1960
2998
|
exact(
|
|
1961
2999
|
r,
|
|
@@ -2004,13 +3042,37 @@ function parseChainTxReceipt(r) {
|
|
|
2004
3042
|
r.steps.length > CHAIN_TX_MAX_MESSAGES
|
|
2005
3043
|
)
|
|
2006
3044
|
fail("receipt.steps");
|
|
3045
|
+
// 0.9.0: on a succeeded receipt (included, code 0) a new proposal names its
|
|
3046
|
+
// id (null when the Hub could not read it; never guessed) and a carry-out
|
|
3047
|
+
// says whether the group's actions ran. Absent on every other receipt.
|
|
3048
|
+
const succeeded = r.code === 0 && r.height !== null && !("reason" in r);
|
|
2007
3049
|
for (const step of r.steps) {
|
|
2008
3050
|
object(step, "receipt.step");
|
|
2009
|
-
|
|
3051
|
+
const result =
|
|
3052
|
+
succeeded && Object.hasOwn(CHAIN_TX_RECEIPT_STEP_RESULT, step.type)
|
|
3053
|
+
? CHAIN_TX_RECEIPT_STEP_RESULT[step.type]
|
|
3054
|
+
: undefined;
|
|
3055
|
+
exact(step, result ? ["type", result] : ["type"], "receipt.step");
|
|
2010
3056
|
if (!CHAIN_TX_MESSAGE_TYPES.includes(step.type)) fail("receipt.step.type");
|
|
3057
|
+
if (
|
|
3058
|
+
result === "proposalId" &&
|
|
3059
|
+
(!("proposalId" in step) ||
|
|
3060
|
+
(step.proposalId !== null && !isGroupProposalId(step.proposalId)))
|
|
3061
|
+
)
|
|
3062
|
+
fail("receipt.step.proposalId");
|
|
3063
|
+
if (
|
|
3064
|
+
result === "groupActions" &&
|
|
3065
|
+
!CHAIN_TX_GROUP_ACTION_RESULTS.includes(step.groupActions)
|
|
3066
|
+
)
|
|
3067
|
+
fail("receipt.step.groupActions");
|
|
2011
3068
|
}
|
|
2012
3069
|
return r;
|
|
2013
3070
|
}
|
|
3071
|
+
/** 0.9.0: the one key a succeeded receipt step adds, per type (D8, D9). */
|
|
3072
|
+
const CHAIN_TX_RECEIPT_STEP_RESULT = {
|
|
3073
|
+
"daodao.proposal.create": "proposalId",
|
|
3074
|
+
"daodao.proposal.execute": "groupActions",
|
|
3075
|
+
};
|
|
2014
3076
|
|
|
2015
3077
|
export * from "./outcome-signature.js";
|
|
2016
3078
|
|