@ocap/util 1.31.5 → 1.31.7

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.
@@ -0,0 +1,40 @@
1
+ //#region src/deposit-v2-bridge-fields.d.ts
2
+ /**
3
+ * DepositTokenV2Tx fields 8-13 (#1688 / #1690; bridge/docs/deposit-withdraw-v2-unification.md §2.2, §2.7).
4
+ *
5
+ * The fields carry the bridge branch of `deposit_token_v2`. A legacy (v1 rollup) deposit leaves all of them at their
6
+ * proto3 defaults, which the wire formats omit, so its bytes, hash and signature are unchanged. jspb `toObject()`
7
+ * (and `formatMessage` after it) still emits the defaults, though, so two helpers keep the legacy path unchanged:
8
+ * - `findNonDefaultDepositV2BridgeFields` (G4): the deposit protocol rejects a v1 itx carrying any of them;
9
+ * - `stripDefaultDepositV2BridgeFields` (G6): the itxJson writers drop the defaults of a kind-0 deposit.
10
+ *
11
+ * Field names are the runtime (camelCase) names. The repeated field shows up as `validatorSignaturesList` in the jspb
12
+ * `toObject()` shape and as `validatorSignatures` after `formatMessage`; both are handled.
13
+ */
14
+ declare const DEPOSIT_V2_BRIDGE_FIELDS: readonly ["kind", "ordinal", "epoch", "ethSender", "claimSignature", "validatorSignatures"];
15
+ type DepositV2BridgeField = (typeof DEPOSIT_V2_BRIDGE_FIELDS)[number];
16
+ /** `DepositTokenV2Tx`, `fg:t:deposit_token_v2` or `deposit_token_v2`. */
17
+ declare function isDepositTokenV2Type(type: string | null | undefined): boolean;
18
+ /**
19
+ * G4: the bridge fields an itx carries with a non-default value, in proto order. Empty for a legacy deposit, whether
20
+ * the fields are absent or hold their defaults.
21
+ */
22
+ declare function findNonDefaultDepositV2BridgeFields(itx: Record<string, unknown> | null | undefined): string[];
23
+ /**
24
+ * G6: for a `DepositTokenV2Tx` whose `kind` is 0 or missing, delete the bridge fields that hold a default value, so the
25
+ * itxJson of a legacy deposit stays byte-for-byte what it was before fields 8-13 existed. Non-default values are kept
26
+ * (e.g. a rejected tx with `epoch = 7`), and a deposit with any other `kind` keeps every field. The same rule is then
27
+ * applied to every DepositTokenV2Tx nested in a decoded Any below `itx`, whatever `type` is. Mutates and returns
28
+ * `itx`; uses `delete`, never `= undefined` (CLAUDE.md).
29
+ */
30
+ declare function stripDefaultDepositV2BridgeFields<T extends Record<string, unknown> | null | undefined>(type: string | null | undefined, itx: T): T;
31
+ /**
32
+ * G6 for a whole formatted Transaction (`formatMessage('Transaction', tx)`, as the ledger entry `tx` JSON stores it):
33
+ * strips every DepositTokenV2Tx in a decoded Any, starting with `tx.itx` itself. A protobuf-wire legacy deposit
34
+ * otherwise gains the six default keys, so the entry JSON a node writes would differ from the one it wrote before
35
+ * fields 8-13 existed, and the ledger verifiers (which re-derive that JSON from `txRaw`) would no longer match
36
+ * historical entries. Mutates and returns `tx`.
37
+ */
38
+ declare function stripDefaultDepositV2BridgeFieldsInTx<T extends Record<string, unknown> | null | undefined>(tx: T): T;
39
+ //#endregion
40
+ export { DEPOSIT_V2_BRIDGE_FIELDS, DepositV2BridgeField, findNonDefaultDepositV2BridgeFields, isDepositTokenV2Type, stripDefaultDepositV2BridgeFields, stripDefaultDepositV2BridgeFieldsInTx };
@@ -0,0 +1,103 @@
1
+ //#region src/deposit-v2-bridge-fields.ts
2
+ /**
3
+ * DepositTokenV2Tx fields 8-13 (#1688 / #1690; bridge/docs/deposit-withdraw-v2-unification.md §2.2, §2.7).
4
+ *
5
+ * The fields carry the bridge branch of `deposit_token_v2`. A legacy (v1 rollup) deposit leaves all of them at their
6
+ * proto3 defaults, which the wire formats omit, so its bytes, hash and signature are unchanged. jspb `toObject()`
7
+ * (and `formatMessage` after it) still emits the defaults, though, so two helpers keep the legacy path unchanged:
8
+ * - `findNonDefaultDepositV2BridgeFields` (G4): the deposit protocol rejects a v1 itx carrying any of them;
9
+ * - `stripDefaultDepositV2BridgeFields` (G6): the itxJson writers drop the defaults of a kind-0 deposit.
10
+ *
11
+ * Field names are the runtime (camelCase) names. The repeated field shows up as `validatorSignaturesList` in the jspb
12
+ * `toObject()` shape and as `validatorSignatures` after `formatMessage`; both are handled.
13
+ */
14
+ const DEPOSIT_V2_BRIDGE_FIELDS = [
15
+ "kind",
16
+ "ordinal",
17
+ "epoch",
18
+ "ethSender",
19
+ "claimSignature",
20
+ "validatorSignatures"
21
+ ];
22
+ const REPEATED_FIELDS = new Set(["validatorSignatures"]);
23
+ /** Every runtime key a field can appear under (repeated fields also have the jspb `xxxList` alias). */
24
+ const runtimeKeys = (field) => REPEATED_FIELDS.has(field) ? [field, `${field}List`] : [field];
25
+ /** The proto3 default a writer emits: `0` for the integers, `''` for the strings, `[]` for the repeated field. */
26
+ const isDefaultValue = (value) => value === 0 || value === "" || Array.isArray(value) && value.length === 0;
27
+ /** Absent (`undefined` / `null`) or a proto3 default: encodes exactly like a tx without the field. */
28
+ const isAbsentOrDefault = (value) => value === void 0 || value === null || isDefaultValue(value);
29
+ /** `DepositTokenV2Tx`, `fg:t:deposit_token_v2` or `deposit_token_v2`. */
30
+ function isDepositTokenV2Type(type) {
31
+ if (typeof type !== "string" || !type) return false;
32
+ return type === "DepositTokenV2Tx" || type === "deposit_token_v2" || type === "fg:t:deposit_token_v2";
33
+ }
34
+ /**
35
+ * G4: the bridge fields an itx carries with a non-default value, in proto order. Empty for a legacy deposit, whether
36
+ * the fields are absent or hold their defaults.
37
+ */
38
+ function findNonDefaultDepositV2BridgeFields(itx) {
39
+ if (!itx || typeof itx !== "object") return [];
40
+ return DEPOSIT_V2_BRIDGE_FIELDS.filter((field) => runtimeKeys(field).some((key) => !isAbsentOrDefault(itx[key])));
41
+ }
42
+ /** Delete the default-valued bridge fields of one DepositTokenV2Tx object, when its own `kind` is 0 or missing. */
43
+ function stripOwnDefaults(record) {
44
+ if (!(record.kind === void 0 || record.kind === 0)) return;
45
+ for (const field of DEPOSIT_V2_BRIDGE_FIELDS) for (const key of runtimeKeys(field)) if (key in record && isDefaultValue(record[key])) delete record[key];
46
+ }
47
+ /** `formatMessage` hands `json` / `vc` Any payloads through untouched: user data, never rewritten. */
48
+ const OPAQUE_ANY_TYPES = new Set(["json", "vc"]);
49
+ /**
50
+ * Guard against pathological input, counted in decoded-Any levels only (plain object / array levels are bounded by the
51
+ * schema). Far beyond any nesting a tx within the size limits can carry, so it never decides a real tx.
52
+ */
53
+ const MAX_ANY_DEPTH = 1024;
54
+ const isPlainObject = (value) => !!value && typeof value === "object" && !Array.isArray(value) && !ArrayBuffer.isView(value);
55
+ /**
56
+ * Walk a formatted / `toObject()` tree and strip every DepositTokenV2Tx found inside a decoded Any, i.e. a node
57
+ * `{ type | typeUrl: <DepositTokenV2Tx>, value: { ... } }`. `formatMessage` decodes every google.protobuf.Any at any
58
+ * depth (`TransferV2Tx.data`, `DepositTokenV2Tx.data`, `signatures[].data`, ...), so a protobuf-wire legacy deposit
59
+ * nested in one gains the six default keys too. Each nested deposit follows its own `kind`.
60
+ */
61
+ function walk(node, anyDepth) {
62
+ if (Array.isArray(node)) {
63
+ for (const item of node) walk(item, anyDepth);
64
+ return;
65
+ }
66
+ if (!isPlainObject(node)) return;
67
+ const anyType = typeof node.type === "string" ? node.type : typeof node.typeUrl === "string" ? node.typeUrl : "";
68
+ let depth = anyDepth;
69
+ if (anyType && "value" in node) {
70
+ if (OPAQUE_ANY_TYPES.has(anyType)) return;
71
+ depth += 1;
72
+ if (depth > MAX_ANY_DEPTH) return;
73
+ if (isDepositTokenV2Type(anyType) && isPlainObject(node.value)) stripOwnDefaults(node.value);
74
+ }
75
+ for (const child of Object.values(node)) walk(child, depth);
76
+ }
77
+ /**
78
+ * G6: for a `DepositTokenV2Tx` whose `kind` is 0 or missing, delete the bridge fields that hold a default value, so the
79
+ * itxJson of a legacy deposit stays byte-for-byte what it was before fields 8-13 existed. Non-default values are kept
80
+ * (e.g. a rejected tx with `epoch = 7`), and a deposit with any other `kind` keeps every field. The same rule is then
81
+ * applied to every DepositTokenV2Tx nested in a decoded Any below `itx`, whatever `type` is. Mutates and returns
82
+ * `itx`; uses `delete`, never `= undefined` (CLAUDE.md).
83
+ */
84
+ function stripDefaultDepositV2BridgeFields(type, itx) {
85
+ if (!isPlainObject(itx)) return itx;
86
+ if (isDepositTokenV2Type(type)) stripOwnDefaults(itx);
87
+ walk(itx, 0);
88
+ return itx;
89
+ }
90
+ /**
91
+ * G6 for a whole formatted Transaction (`formatMessage('Transaction', tx)`, as the ledger entry `tx` JSON stores it):
92
+ * strips every DepositTokenV2Tx in a decoded Any, starting with `tx.itx` itself. A protobuf-wire legacy deposit
93
+ * otherwise gains the six default keys, so the entry JSON a node writes would differ from the one it wrote before
94
+ * fields 8-13 existed, and the ledger verifiers (which re-derive that JSON from `txRaw`) would no longer match
95
+ * historical entries. Mutates and returns `tx`.
96
+ */
97
+ function stripDefaultDepositV2BridgeFieldsInTx(tx) {
98
+ walk(tx, 0);
99
+ return tx;
100
+ }
101
+
102
+ //#endregion
103
+ export { DEPOSIT_V2_BRIDGE_FIELDS, findNonDefaultDepositV2BridgeFields, isDepositTokenV2Type, stripDefaultDepositV2BridgeFields, stripDefaultDepositV2BridgeFieldsInTx };
@@ -0,0 +1,108 @@
1
+
2
+ //#region src/deposit-v2-bridge-fields.ts
3
+ /**
4
+ * DepositTokenV2Tx fields 8-13 (#1688 / #1690; bridge/docs/deposit-withdraw-v2-unification.md §2.2, §2.7).
5
+ *
6
+ * The fields carry the bridge branch of `deposit_token_v2`. A legacy (v1 rollup) deposit leaves all of them at their
7
+ * proto3 defaults, which the wire formats omit, so its bytes, hash and signature are unchanged. jspb `toObject()`
8
+ * (and `formatMessage` after it) still emits the defaults, though, so two helpers keep the legacy path unchanged:
9
+ * - `findNonDefaultDepositV2BridgeFields` (G4): the deposit protocol rejects a v1 itx carrying any of them;
10
+ * - `stripDefaultDepositV2BridgeFields` (G6): the itxJson writers drop the defaults of a kind-0 deposit.
11
+ *
12
+ * Field names are the runtime (camelCase) names. The repeated field shows up as `validatorSignaturesList` in the jspb
13
+ * `toObject()` shape and as `validatorSignatures` after `formatMessage`; both are handled.
14
+ */
15
+ const DEPOSIT_V2_BRIDGE_FIELDS = [
16
+ "kind",
17
+ "ordinal",
18
+ "epoch",
19
+ "ethSender",
20
+ "claimSignature",
21
+ "validatorSignatures"
22
+ ];
23
+ const REPEATED_FIELDS = new Set(["validatorSignatures"]);
24
+ /** Every runtime key a field can appear under (repeated fields also have the jspb `xxxList` alias). */
25
+ const runtimeKeys = (field) => REPEATED_FIELDS.has(field) ? [field, `${field}List`] : [field];
26
+ /** The proto3 default a writer emits: `0` for the integers, `''` for the strings, `[]` for the repeated field. */
27
+ const isDefaultValue = (value) => value === 0 || value === "" || Array.isArray(value) && value.length === 0;
28
+ /** Absent (`undefined` / `null`) or a proto3 default: encodes exactly like a tx without the field. */
29
+ const isAbsentOrDefault = (value) => value === void 0 || value === null || isDefaultValue(value);
30
+ /** `DepositTokenV2Tx`, `fg:t:deposit_token_v2` or `deposit_token_v2`. */
31
+ function isDepositTokenV2Type(type) {
32
+ if (typeof type !== "string" || !type) return false;
33
+ return type === "DepositTokenV2Tx" || type === "deposit_token_v2" || type === "fg:t:deposit_token_v2";
34
+ }
35
+ /**
36
+ * G4: the bridge fields an itx carries with a non-default value, in proto order. Empty for a legacy deposit, whether
37
+ * the fields are absent or hold their defaults.
38
+ */
39
+ function findNonDefaultDepositV2BridgeFields(itx) {
40
+ if (!itx || typeof itx !== "object") return [];
41
+ return DEPOSIT_V2_BRIDGE_FIELDS.filter((field) => runtimeKeys(field).some((key) => !isAbsentOrDefault(itx[key])));
42
+ }
43
+ /** Delete the default-valued bridge fields of one DepositTokenV2Tx object, when its own `kind` is 0 or missing. */
44
+ function stripOwnDefaults(record) {
45
+ if (!(record.kind === void 0 || record.kind === 0)) return;
46
+ for (const field of DEPOSIT_V2_BRIDGE_FIELDS) for (const key of runtimeKeys(field)) if (key in record && isDefaultValue(record[key])) delete record[key];
47
+ }
48
+ /** `formatMessage` hands `json` / `vc` Any payloads through untouched: user data, never rewritten. */
49
+ const OPAQUE_ANY_TYPES = new Set(["json", "vc"]);
50
+ /**
51
+ * Guard against pathological input, counted in decoded-Any levels only (plain object / array levels are bounded by the
52
+ * schema). Far beyond any nesting a tx within the size limits can carry, so it never decides a real tx.
53
+ */
54
+ const MAX_ANY_DEPTH = 1024;
55
+ const isPlainObject = (value) => !!value && typeof value === "object" && !Array.isArray(value) && !ArrayBuffer.isView(value);
56
+ /**
57
+ * Walk a formatted / `toObject()` tree and strip every DepositTokenV2Tx found inside a decoded Any, i.e. a node
58
+ * `{ type | typeUrl: <DepositTokenV2Tx>, value: { ... } }`. `formatMessage` decodes every google.protobuf.Any at any
59
+ * depth (`TransferV2Tx.data`, `DepositTokenV2Tx.data`, `signatures[].data`, ...), so a protobuf-wire legacy deposit
60
+ * nested in one gains the six default keys too. Each nested deposit follows its own `kind`.
61
+ */
62
+ function walk(node, anyDepth) {
63
+ if (Array.isArray(node)) {
64
+ for (const item of node) walk(item, anyDepth);
65
+ return;
66
+ }
67
+ if (!isPlainObject(node)) return;
68
+ const anyType = typeof node.type === "string" ? node.type : typeof node.typeUrl === "string" ? node.typeUrl : "";
69
+ let depth = anyDepth;
70
+ if (anyType && "value" in node) {
71
+ if (OPAQUE_ANY_TYPES.has(anyType)) return;
72
+ depth += 1;
73
+ if (depth > MAX_ANY_DEPTH) return;
74
+ if (isDepositTokenV2Type(anyType) && isPlainObject(node.value)) stripOwnDefaults(node.value);
75
+ }
76
+ for (const child of Object.values(node)) walk(child, depth);
77
+ }
78
+ /**
79
+ * G6: for a `DepositTokenV2Tx` whose `kind` is 0 or missing, delete the bridge fields that hold a default value, so the
80
+ * itxJson of a legacy deposit stays byte-for-byte what it was before fields 8-13 existed. Non-default values are kept
81
+ * (e.g. a rejected tx with `epoch = 7`), and a deposit with any other `kind` keeps every field. The same rule is then
82
+ * applied to every DepositTokenV2Tx nested in a decoded Any below `itx`, whatever `type` is. Mutates and returns
83
+ * `itx`; uses `delete`, never `= undefined` (CLAUDE.md).
84
+ */
85
+ function stripDefaultDepositV2BridgeFields(type, itx) {
86
+ if (!isPlainObject(itx)) return itx;
87
+ if (isDepositTokenV2Type(type)) stripOwnDefaults(itx);
88
+ walk(itx, 0);
89
+ return itx;
90
+ }
91
+ /**
92
+ * G6 for a whole formatted Transaction (`formatMessage('Transaction', tx)`, as the ledger entry `tx` JSON stores it):
93
+ * strips every DepositTokenV2Tx in a decoded Any, starting with `tx.itx` itself. A protobuf-wire legacy deposit
94
+ * otherwise gains the six default keys, so the entry JSON a node writes would differ from the one it wrote before
95
+ * fields 8-13 existed, and the ledger verifiers (which re-derive that JSON from `txRaw`) would no longer match
96
+ * historical entries. Mutates and returns `tx`.
97
+ */
98
+ function stripDefaultDepositV2BridgeFieldsInTx(tx) {
99
+ walk(tx, 0);
100
+ return tx;
101
+ }
102
+
103
+ //#endregion
104
+ exports.DEPOSIT_V2_BRIDGE_FIELDS = DEPOSIT_V2_BRIDGE_FIELDS;
105
+ exports.findNonDefaultDepositV2BridgeFields = findNonDefaultDepositV2BridgeFields;
106
+ exports.isDepositTokenV2Type = isDepositTokenV2Type;
107
+ exports.stripDefaultDepositV2BridgeFields = stripDefaultDepositV2BridgeFields;
108
+ exports.stripDefaultDepositV2BridgeFieldsInTx = stripDefaultDepositV2BridgeFieldsInTx;
@@ -0,0 +1,40 @@
1
+ //#region src/deposit-v2-bridge-fields.d.ts
2
+ /**
3
+ * DepositTokenV2Tx fields 8-13 (#1688 / #1690; bridge/docs/deposit-withdraw-v2-unification.md §2.2, §2.7).
4
+ *
5
+ * The fields carry the bridge branch of `deposit_token_v2`. A legacy (v1 rollup) deposit leaves all of them at their
6
+ * proto3 defaults, which the wire formats omit, so its bytes, hash and signature are unchanged. jspb `toObject()`
7
+ * (and `formatMessage` after it) still emits the defaults, though, so two helpers keep the legacy path unchanged:
8
+ * - `findNonDefaultDepositV2BridgeFields` (G4): the deposit protocol rejects a v1 itx carrying any of them;
9
+ * - `stripDefaultDepositV2BridgeFields` (G6): the itxJson writers drop the defaults of a kind-0 deposit.
10
+ *
11
+ * Field names are the runtime (camelCase) names. The repeated field shows up as `validatorSignaturesList` in the jspb
12
+ * `toObject()` shape and as `validatorSignatures` after `formatMessage`; both are handled.
13
+ */
14
+ declare const DEPOSIT_V2_BRIDGE_FIELDS: readonly ["kind", "ordinal", "epoch", "ethSender", "claimSignature", "validatorSignatures"];
15
+ type DepositV2BridgeField = (typeof DEPOSIT_V2_BRIDGE_FIELDS)[number];
16
+ /** `DepositTokenV2Tx`, `fg:t:deposit_token_v2` or `deposit_token_v2`. */
17
+ declare function isDepositTokenV2Type(type: string | null | undefined): boolean;
18
+ /**
19
+ * G4: the bridge fields an itx carries with a non-default value, in proto order. Empty for a legacy deposit, whether
20
+ * the fields are absent or hold their defaults.
21
+ */
22
+ declare function findNonDefaultDepositV2BridgeFields(itx: Record<string, unknown> | null | undefined): string[];
23
+ /**
24
+ * G6: for a `DepositTokenV2Tx` whose `kind` is 0 or missing, delete the bridge fields that hold a default value, so the
25
+ * itxJson of a legacy deposit stays byte-for-byte what it was before fields 8-13 existed. Non-default values are kept
26
+ * (e.g. a rejected tx with `epoch = 7`), and a deposit with any other `kind` keeps every field. The same rule is then
27
+ * applied to every DepositTokenV2Tx nested in a decoded Any below `itx`, whatever `type` is. Mutates and returns
28
+ * `itx`; uses `delete`, never `= undefined` (CLAUDE.md).
29
+ */
30
+ declare function stripDefaultDepositV2BridgeFields<T extends Record<string, unknown> | null | undefined>(type: string | null | undefined, itx: T): T;
31
+ /**
32
+ * G6 for a whole formatted Transaction (`formatMessage('Transaction', tx)`, as the ledger entry `tx` JSON stores it):
33
+ * strips every DepositTokenV2Tx in a decoded Any, starting with `tx.itx` itself. A protobuf-wire legacy deposit
34
+ * otherwise gains the six default keys, so the entry JSON a node writes would differ from the one it wrote before
35
+ * fields 8-13 existed, and the ledger verifiers (which re-derive that JSON from `txRaw`) would no longer match
36
+ * historical entries. Mutates and returns `tx`.
37
+ */
38
+ declare function stripDefaultDepositV2BridgeFieldsInTx<T extends Record<string, unknown> | null | undefined>(tx: T): T;
39
+ //#endregion
40
+ export { DEPOSIT_V2_BRIDGE_FIELDS, DepositV2BridgeField, findNonDefaultDepositV2BridgeFields, isDepositTokenV2Type, stripDefaultDepositV2BridgeFields, stripDefaultDepositV2BridgeFieldsInTx };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/util",
3
- "version": "1.31.5",
3
+ "version": "1.31.7",
4
4
  "type": "module",
5
5
  "description": "utils shared across multiple forge js libs, works in both node.js and browser",
6
6
  "keywords": [
@@ -12,7 +12,7 @@
12
12
  "access": "public"
13
13
  },
14
14
  "dependencies": {
15
- "@ocap/types": "1.31.5",
15
+ "@ocap/types": "1.31.7",
16
16
  "@types/bn.js": "^5.2.0",
17
17
  "base64-url": "^2.3.3",
18
18
  "bn.js": "5.2.3",