@ocap/tx-pipeline 1.31.4 → 1.31.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/esm/bridge-release-keys.d.mts +17 -1
  2. package/esm/bridge-release-keys.mjs +35 -20
  3. package/esm/bridge-reserves.d.mts +48 -9
  4. package/esm/bridge-reserves.mjs +66 -16
  5. package/esm/helpers/bridge-withdraw.d.mts +22 -38
  6. package/esm/helpers/bridge-withdraw.mjs +57 -77
  7. package/esm/helpers/replay-guard.d.mts +29 -12
  8. package/esm/helpers/replay-guard.mjs +68 -22
  9. package/esm/index.d.mts +3 -3
  10. package/esm/index.mjs +3 -3
  11. package/esm/legacy-protocol-sunset.d.mts +35 -21
  12. package/esm/legacy-protocol-sunset.mjs +124 -70
  13. package/esm/legacy-rollup-archive.d.mts +44 -7
  14. package/esm/legacy-rollup-archive.mjs +78 -13
  15. package/esm/library.d.mts +3 -3
  16. package/esm/library.mjs +3 -3
  17. package/esm/pipes/bridge-release-gate.d.mts +3 -2
  18. package/esm/pipes/bridge-release-gate.mjs +90 -35
  19. package/esm/pipes/bridge-reserve-v2-gate.d.mts +2 -0
  20. package/esm/pipes/bridge-reserve-v2-gate.mjs +31 -13
  21. package/esm/pipes/decode-tx.mjs +1 -1
  22. package/esm/pipes/post-execution-invariant-gate.mjs +5 -4
  23. package/esm/pipes/pre-flight-waf.mjs +6 -3
  24. package/esm/pipes/verify-multisig-v2.mjs +1 -1
  25. package/esm/pipes/verify-multisig.mjs +1 -1
  26. package/esm/pipes/verify-tx-size.mjs +8 -2
  27. package/esm/pipes/verify-tx.mjs +1 -1
  28. package/esm/transaction-block.mjs +11 -0
  29. package/lib/bridge-release-keys.cjs +35 -18
  30. package/lib/bridge-release-keys.d.cts +17 -1
  31. package/lib/bridge-reserves.cjs +73 -18
  32. package/lib/bridge-reserves.d.cts +48 -9
  33. package/lib/helpers/bridge-withdraw.cjs +60 -80
  34. package/lib/helpers/bridge-withdraw.d.cts +22 -38
  35. package/lib/helpers/replay-guard.cjs +64 -17
  36. package/lib/helpers/replay-guard.d.cts +29 -12
  37. package/lib/index.cjs +2 -2
  38. package/lib/index.d.cts +3 -3
  39. package/lib/legacy-protocol-sunset.cjs +122 -69
  40. package/lib/legacy-protocol-sunset.d.cts +35 -21
  41. package/lib/legacy-rollup-archive.cjs +82 -14
  42. package/lib/legacy-rollup-archive.d.cts +44 -7
  43. package/lib/library.cjs +1 -2
  44. package/lib/library.d.cts +3 -3
  45. package/lib/pipes/bridge-release-gate.cjs +89 -33
  46. package/lib/pipes/bridge-release-gate.d.cts +3 -2
  47. package/lib/pipes/bridge-reserve-v2-gate.cjs +30 -12
  48. package/lib/pipes/bridge-reserve-v2-gate.d.cts +2 -0
  49. package/lib/pipes/decode-tx.cjs +1 -1
  50. package/lib/pipes/post-execution-invariant-gate.cjs +3 -2
  51. package/lib/pipes/pre-flight-waf.cjs +6 -3
  52. package/lib/pipes/verify-multisig-v2.cjs +1 -1
  53. package/lib/pipes/verify-multisig.cjs +1 -1
  54. package/lib/pipes/verify-tx-size.cjs +8 -2
  55. package/lib/pipes/verify-tx.cjs +1 -1
  56. package/lib/transaction-block.cjs +11 -0
  57. package/package.json +10 -10
@@ -4,6 +4,22 @@ declare const RELEASE_RINGS: readonly ["deposit", "refund"];
4
4
  type ReleaseRing = (typeof RELEASE_RINGS)[number];
5
5
  /** Tombstone key of a release. `evidence` is a lowercase `0x` bytes32 (the itx schema enforces the spelling). */
6
6
  declare function getReleaseTombstoneKey(bridge: string, kind: number, evidence: string, ordinal: number): string;
7
+ /**
8
+ * `data` of the rows the legacy rollup v1 protocols write at a CALLER-CHOSEN key (`itx.evidence.hash`, any
9
+ * `patterns.txHash`): `join_rollup`, `leave_rollup`, `deposit_token_v2` (v1) and `claim_block_reward`. Those are the only
10
+ * writers that can name a keccak `0x` bridge key (#1680); every other evidence key is derived (unsigned-tx, abuse-slash)
11
+ * or must be an existing tx (`claim_stake`).
12
+ */
13
+ declare const V1_EVIDENCE_DATA: ReadonlySet<string>;
14
+ /**
15
+ * Whether `row` is a row a legacy v1 tx planted (its `data` is exactly one of `V1_EVIDENCE_DATA`). Such a row at a
16
+ * keccak `0x` bridge key (membership tombstone, gateway / token / reserve index) blocks nothing and is replaced by the
17
+ * bridge record. Any OTHER existing row at a bridge key blocks (fail closed): a bridge tx never overwrites a row it can
18
+ * not identify.
19
+ */
20
+ declare const isV1EvidenceRow: (row: {
21
+ data?: unknown;
22
+ } | null | undefined) => boolean;
7
23
  /** Key of slot `slot` (0..23) of `ring` of `bridge`. */
8
24
  declare function getReleaseBucketKey(bridge: string, ring: ReleaseRing, slot: number): string;
9
25
  /** Row data of a bucket: names the bridge, ring and slot it belongs to, then `<hourIndex>:<amount>`. */
@@ -30,4 +46,4 @@ declare function identifyBucketRow(row: {
30
46
  slot: number;
31
47
  } | null;
32
48
  //#endregion
33
- export { ParsedBucketData, RELEASE_RINGS, RELEASE_RING_SLOTS, ReleaseRing, formatBucketData, getReleaseBucketKey, getReleaseTombstoneKey, identifyBucketRow, parseBucketData };
49
+ export { ParsedBucketData, RELEASE_RINGS, RELEASE_RING_SLOTS, ReleaseRing, V1_EVIDENCE_DATA, formatBucketData, getReleaseBucketKey, getReleaseTombstoneKey, identifyBucketRow, isV1EvidenceRow, parseBucketData };
@@ -1,44 +1,59 @@
1
- import { bridgeEvidenceKey } from "@ocap/mcrypto";
1
+ import { bridgeReleaseBucketKey, bridgeReleaseTombstoneKey } from "@ocap/mcrypto";
2
2
 
3
3
  //#region src/bridge-release-keys.ts
4
4
  /**
5
- * Evidence-table keys owned by `release_bridge_token` (#1466, bridge/docs/ocap-protocols.md §3 checks 3 and 7, §v2.4).
5
+ * Evidence-table keys owned by the bridge release, `deposit_token_v2` kind 1/2/3 (#1466,
6
+ * bridge/docs/ocap-protocols.md §3 checks 3 and 7, §v2.4).
6
7
  *
7
8
  * Both keys carry the bridge address, so several bridges on one chain never share a tombstone or a bucket. Every
8
9
  * preimage is fixed-width words followed by the bridge DID as the trailing text (`bridgeEvidenceKey`), so two
9
10
  * different tuples can never produce the same bytes:
10
11
  *
11
- * - tombstone: `keccak256("arc-bridge:release:" || uint kind || bytes32 evidence || uint ordinal || bridge)`;
12
- * - bucket: `keccak256("arc-bridge:release-bucket:" || uint ring || uint slot || bridge)`, `ring` 0 = deposit, 1 = refund.
12
+ * - tombstone: `"rt:" + keccak256("arc-bridge:release:" || uint kind || bytes32 evidence || uint ordinal || bridge)[0..63)`;
13
+ * - bucket: `"rb:" + keccak256("arc-bridge:release-bucket:" || uint ring || uint slot || bridge)[0..63)`, `ring` 0 =
14
+ * deposit, 1 = refund.
15
+ *
16
+ * #1680: the keys are SELF-IDENTIFYING (`@ocap/mcrypto` `bridgeReleaseTombstoneKey` / `bridgeReleaseBucketKey`, the one
17
+ * derivation the bridge runtime uses too). Legacy rollup v1 protocols write evidence rows at a caller-chosen
18
+ * `patterns.txHash` key; no such key is ever in the `rt:` / `rb:` space, so a v1 tx can not pre-write a tombstone (a
19
+ * faked replay) or a bucket (a bricked ring). The gate refuses every tx type but a bridge release any write in that
20
+ * space, by key.
13
21
  *
14
22
  * A bucket row stores `"<ring>:<slot>:<bridge>:<hourIndex>:<amountInBaseUnits>"`. The row names its own bridge, ring and
15
- * slot, so the post-execution gate can tell a bucket row from any other evidence row (and refuse every tx type except
16
- * `release_bridge_token` writing one) without a global list of bridges: it recomputes the key from the row's own fields.
23
+ * slot, so the post-execution gate can tell which bucket a release wrote: it recomputes the key from the row's own fields.
17
24
  *
18
25
  * `bridge` is always the canonical bridge address (no `did:abt:` prefix), exactly as `itx.bridge` must be spelled.
19
26
  */
20
27
  const RELEASE_RING_SLOTS = 24;
21
28
  const RELEASE_RINGS = ["deposit", "refund"];
22
- const TOMBSTONE_PREFIX = "arc-bridge:release:";
23
- const BUCKET_PREFIX = "arc-bridge:release-bucket:";
24
29
  const ringIndex = (ring) => RELEASE_RINGS.indexOf(ring);
25
30
  /** Tombstone key of a release. `evidence` is a lowercase `0x` bytes32 (the itx schema enforces the spelling). */
26
31
  function getReleaseTombstoneKey(bridge, kind, evidence, ordinal) {
27
- return bridgeEvidenceKey(TOMBSTONE_PREFIX, [
28
- { uint: kind },
29
- { bytes32: String(evidence).toLowerCase() },
30
- { uint: ordinal },
31
- { text: bridge }
32
- ]);
32
+ return bridgeReleaseTombstoneKey(bridge, kind, String(evidence).toLowerCase(), ordinal);
33
33
  }
34
+ /**
35
+ * `data` of the rows the legacy rollup v1 protocols write at a CALLER-CHOSEN key (`itx.evidence.hash`, any
36
+ * `patterns.txHash`): `join_rollup`, `leave_rollup`, `deposit_token_v2` (v1) and `claim_block_reward`. Those are the only
37
+ * writers that can name a keccak `0x` bridge key (#1680); every other evidence key is derived (unsigned-tx, abuse-slash)
38
+ * or must be an existing tx (`claim_stake`).
39
+ */
40
+ const V1_EVIDENCE_DATA = new Set([
41
+ "rollup-join",
42
+ "rollup-leave",
43
+ "rollup-deposit",
44
+ "rollup-claim-block-reward"
45
+ ]);
46
+ /**
47
+ * Whether `row` is a row a legacy v1 tx planted (its `data` is exactly one of `V1_EVIDENCE_DATA`). Such a row at a
48
+ * keccak `0x` bridge key (membership tombstone, gateway / token / reserve index) blocks nothing and is replaced by the
49
+ * bridge record. Any OTHER existing row at a bridge key blocks (fail closed): a bridge tx never overwrites a row it can
50
+ * not identify.
51
+ */
52
+ const isV1EvidenceRow = (row) => !!row && typeof row.data === "string" && V1_EVIDENCE_DATA.has(row.data);
34
53
  /** Key of slot `slot` (0..23) of `ring` of `bridge`. */
35
54
  function getReleaseBucketKey(bridge, ring, slot) {
36
55
  if (!RELEASE_RINGS.includes(ring) || !Number.isInteger(slot) || slot < 0 || slot >= RELEASE_RING_SLOTS) throw new Error(`Invalid release bucket: ${ring}:${slot}`);
37
- return bridgeEvidenceKey(BUCKET_PREFIX, [
38
- { uint: ringIndex(ring) },
39
- { uint: slot },
40
- { text: bridge }
41
- ]);
56
+ return bridgeReleaseBucketKey(bridge, ringIndex(ring), slot);
42
57
  }
43
58
  /** Row data of a bucket: names the bridge, ring and slot it belongs to, then `<hourIndex>:<amount>`. */
44
59
  const formatBucketData = (bridge, ring, slot, window) => `${ring}:${slot}:${bridge}:${window}`;
@@ -78,4 +93,4 @@ function identifyBucketRow(row) {
78
93
  }
79
94
 
80
95
  //#endregion
81
- export { RELEASE_RINGS, RELEASE_RING_SLOTS, formatBucketData, getReleaseBucketKey, getReleaseTombstoneKey, identifyBucketRow, parseBucketData };
96
+ export { RELEASE_RINGS, RELEASE_RING_SLOTS, V1_EVIDENCE_DATA, formatBucketData, getReleaseBucketKey, getReleaseTombstoneKey, identifyBucketRow, isV1EvidenceRow, parseBucketData };
@@ -3,24 +3,58 @@ import { rollup } from "@ocap/state";
3
3
  //#region src/bridge-reserves.d.ts
4
4
  type Ctx = any;
5
5
  type AnyRow = Record<string, any>;
6
- declare const BRIDGE_RELEASE_TX_TYPE = "release_bridge_token";
7
- declare const BRIDGE_WITHDRAW_TX_TYPE = "transfer_v2";
8
6
  /**
9
- * Tx types that may DEBIT a reserve (R1). `release_bridge_token` may only debit the reserve of its own bridge (or of the
10
- * `migratedTo` chain of a migrated bridge), `migrate_rollup` moves a whole reserve to its successor's.
7
+ * The withdraw (#1691, bridge/docs/deposit-withdraw-v2-unification.md §2.4): a `withdraw_token_v2` whose
8
+ * `itx.rollup` is a v2 bridge.
9
+ */
10
+ declare const BRIDGE_WITHDRAW_V2_TX_TYPE = "withdraw_token_v2";
11
+ /**
12
+ * The release (#1692, bridge/docs/deposit-withdraw-v2-unification.md §2.3, §2.5): a `deposit_token_v2` with
13
+ * `kind ∈ {1,2,3}` (DEPOSIT, REFUND, DEPOSIT_B), the only tx that debits a bridge reserve for a user.
14
+ */
15
+ declare const BRIDGE_RELEASE_V2_TX_TYPE = "deposit_token_v2";
16
+ /** The `kind` values of a bridge release (`OcapRelease.kind`); 0 is a legacy (v1 rollup) deposit. */
17
+ declare const BRIDGE_RELEASE_KINDS: readonly number[];
18
+ /**
19
+ * maxTxSize / maxGasOps key of the release (§2.7 item 3). Not a tx type: it never appears in `supportedTxs`.
20
+ * It passes the config key pattern `fg:t:[_a-z0-9]+`.
21
+ */
22
+ declare const BRIDGE_RELEASE_V2_RESOURCE_KEY = "fg:t:deposit_token_v2_bridge";
23
+ /**
24
+ * Tx types that may DEBIT a reserve (R1), besides a bridge release (`isNamedBridgeRelease`, which may only debit the
25
+ * reserve of its own bridge, or of the `migratedTo` chain of a migrated bridge): `migrate_rollup` moves a whole reserve
26
+ * to its successor's.
11
27
  * The reconcile transaction of §v2.6 is added to {@link BRIDGE_RECONCILE_TX_TYPES} when it is designed; nothing else
12
28
  * in the gate has to change for it to be allowed to debit a mapped reserve.
13
29
  */
14
30
  declare const BRIDGE_RECONCILE_TX_TYPES: readonly string[];
15
31
  declare const BRIDGE_RESERVE_DEBIT_TX_TYPES: readonly string[];
16
32
  /**
17
- * Tx types that may CREDIT a reserve (R2). `transfer_v2` is the withdraw and only into a live, unpaused, not-closing
18
- * bridge; `create_rollup` funds the reserve of the bridge it creates; `migrate_rollup` fills the successor's reserve.
33
+ * Tx types that may CREDIT a reserve (R2). `withdraw_token_v2` (bridge branch) is the withdraw and
34
+ * only into a live, unpaused, not-closing bridge; `create_rollup` funds the reserve of the bridge it creates;
35
+ * `migrate_rollup` fills the successor's reserve.
19
36
  */
20
37
  declare const BRIDGE_RESERVE_CREDIT_TX_TYPES: readonly string[];
21
38
  /** Longest `migratedTo` chain followed for a refund (a loop or a pathological chain stops the walk, fail closed). */
22
39
  declare const BRIDGE_MAX_MIGRATION_HOPS = 16;
23
40
  declare const stripTxTypePrefix: (txType: unknown) => string;
41
+ /**
42
+ * `isBridgeRelease(txType, itx)` (§2.3, one definition for the whole design): `txType` is `deposit_token_v2` and
43
+ * `itx.kind ∈ {1,2,3}`. Stateless (the kind is in the itx); the protocol also requires the row `itx.rollup` names to be a
44
+ * v2 bridge (both keys must match). Strict: a kind that is not one of the three numbers is not a bridge release.
45
+ */
46
+ declare function isBridgeRelease(txType: unknown, itx: unknown): boolean;
47
+ /**
48
+ * For the gate: an `isBridgeRelease` that also NAMES its bridge (`itx.rollup` a non-empty string). Narrower, so fail
49
+ * closed: a bridge kind without a bridge is no release at all (the protocol refuses it before any write), and it owns
50
+ * nothing in the release key space and may debit no reserve.
51
+ */
52
+ declare function isNamedBridgeRelease(txType: unknown, itx: unknown): boolean;
53
+ /**
54
+ * The `transaction.maxTxSize` / `maxGasOps` key a tx is measured by: `fg:t:deposit_token_v2_bridge` for a bridge release,
55
+ * the tx type itself otherwise (so every other tx, and every v1 deposit, keeps its key and limit).
56
+ */
57
+ declare function txResourceKey(txType: string, itx: unknown): string;
24
58
  /** Strip a `did:abt:` prefix and whitespace: the form used in the evidence index and in rows. */
25
59
  declare const bareAddress: (address: unknown) => string;
26
60
  /** Whether `address` has the DID role of a bridge reserve. Syntactic only: see the file header. */
@@ -64,7 +98,7 @@ declare function resolveBridge(context: Ctx, bridgeAddress: unknown): Promise<Re
64
98
  * address as one that accepts no credit and allows no debit.
65
99
  */
66
100
  declare function resolveBridgeForReserve(context: Ctx, address: unknown): Promise<ResolvedBridge | null>;
67
- /** What a `release_bridge_token` (or a reconcile) is allowed to debit. */
101
+ /** What a bridge release (or a reconcile) is allowed to debit. */
68
102
  interface ReleaseResolution {
69
103
  /** the bridge named by `itx.bridge` */
70
104
  bridge: ResolvedBridge;
@@ -92,8 +126,13 @@ declare function resolveBridgeForRelease(context: Ctx, itx: {
92
126
  interface BridgeReserveResolution {
93
127
  /** reserve (bare address) -> its bridge, `null` when unmapped */
94
128
  reserves: Map<string, ResolvedBridge | null>;
95
- /** present for `release_bridge_token` txs whose `itx.bridge` resolves */
129
+ /** present for a release whose bridge (`itx.rollup`) resolves */
96
130
  release: ReleaseResolution | null;
131
+ /**
132
+ * `withdraw_token_v2` that touched a reserve: the v2 bridge `itx.rollup` names (pre-tx row), `null` when it names no
133
+ * v2 bridge (a v1 rollup, nothing, a failed read). Only the reserve of this bridge may be credited.
134
+ */
135
+ withdraw?: ResolvedBridge | null;
97
136
  }
98
137
  /**
99
138
  * Resolve, once per transaction, everything the synchronous post-execution gate needs about bridge reserves. Run it
@@ -104,4 +143,4 @@ interface BridgeReserveResolution {
104
143
  */
105
144
  declare function prepareBridgeReserveGate(context: Ctx): Promise<void>;
106
145
  //#endregion
107
- export { BRIDGE_MAX_MIGRATION_HOPS, BRIDGE_RECONCILE_TX_TYPES, BRIDGE_RELEASE_TX_TYPE, BRIDGE_RESERVE_CREDIT_TX_TYPES, BRIDGE_RESERVE_DEBIT_TX_TYPES, BRIDGE_WITHDRAW_TX_TYPE, BridgeReserveResolution, ReleaseResolution, ResolvedBridge, bareAddress, describeBridge, isBridgeReserveRole, prepareBridgeReserveGate, reserveIndexKey, resolveBridge, resolveBridgeForRelease, resolveBridgeForReserve, stripTxTypePrefix };
146
+ export { BRIDGE_MAX_MIGRATION_HOPS, BRIDGE_RECONCILE_TX_TYPES, BRIDGE_RELEASE_KINDS, BRIDGE_RELEASE_V2_RESOURCE_KEY, BRIDGE_RELEASE_V2_TX_TYPE, BRIDGE_RESERVE_CREDIT_TX_TYPES, BRIDGE_RESERVE_DEBIT_TX_TYPES, BRIDGE_WITHDRAW_V2_TX_TYPE, BridgeReserveResolution, ReleaseResolution, ResolvedBridge, bareAddress, describeBridge, isBridgeRelease, isBridgeReserveRole, isNamedBridgeRelease, prepareBridgeReserveGate, reserveIndexKey, resolveBridge, resolveBridgeForRelease, resolveBridgeForReserve, stripTxTypePrefix, txResourceKey };
@@ -20,32 +20,75 @@ import { rollup } from "@ocap/state";
20
20
  *
21
21
  * Reads go to `context.statedb` and are cached on the context for the lifetime of one transaction.
22
22
  */
23
- const BRIDGE_RELEASE_TX_TYPE = "release_bridge_token";
24
- const BRIDGE_WITHDRAW_TX_TYPE = "transfer_v2";
25
23
  /**
26
- * Tx types that may DEBIT a reserve (R1). `release_bridge_token` may only debit the reserve of its own bridge (or of the
27
- * `migratedTo` chain of a migrated bridge), `migrate_rollup` moves a whole reserve to its successor's.
24
+ * The withdraw (#1691, bridge/docs/deposit-withdraw-v2-unification.md §2.4): a `withdraw_token_v2` whose
25
+ * `itx.rollup` is a v2 bridge.
26
+ */
27
+ const BRIDGE_WITHDRAW_V2_TX_TYPE = "withdraw_token_v2";
28
+ /**
29
+ * The release (#1692, bridge/docs/deposit-withdraw-v2-unification.md §2.3, §2.5): a `deposit_token_v2` with
30
+ * `kind ∈ {1,2,3}` (DEPOSIT, REFUND, DEPOSIT_B), the only tx that debits a bridge reserve for a user.
31
+ */
32
+ const BRIDGE_RELEASE_V2_TX_TYPE = "deposit_token_v2";
33
+ /** The `kind` values of a bridge release (`OcapRelease.kind`); 0 is a legacy (v1 rollup) deposit. */
34
+ const BRIDGE_RELEASE_KINDS = [
35
+ 1,
36
+ 2,
37
+ 3
38
+ ];
39
+ /**
40
+ * maxTxSize / maxGasOps key of the release (§2.7 item 3). Not a tx type: it never appears in `supportedTxs`.
41
+ * It passes the config key pattern `fg:t:[_a-z0-9]+`.
42
+ */
43
+ const BRIDGE_RELEASE_V2_RESOURCE_KEY = "fg:t:deposit_token_v2_bridge";
44
+ /**
45
+ * Tx types that may DEBIT a reserve (R1), besides a bridge release (`isNamedBridgeRelease`, which may only debit the
46
+ * reserve of its own bridge, or of the `migratedTo` chain of a migrated bridge): `migrate_rollup` moves a whole reserve
47
+ * to its successor's.
28
48
  * The reconcile transaction of §v2.6 is added to {@link BRIDGE_RECONCILE_TX_TYPES} when it is designed; nothing else
29
49
  * in the gate has to change for it to be allowed to debit a mapped reserve.
30
50
  */
31
51
  const BRIDGE_RECONCILE_TX_TYPES = [];
32
- const BRIDGE_RESERVE_DEBIT_TX_TYPES = [
33
- BRIDGE_RELEASE_TX_TYPE,
34
- "migrate_rollup",
35
- ...BRIDGE_RECONCILE_TX_TYPES
36
- ];
52
+ const BRIDGE_RESERVE_DEBIT_TX_TYPES = ["migrate_rollup", ...BRIDGE_RECONCILE_TX_TYPES];
37
53
  /**
38
- * Tx types that may CREDIT a reserve (R2). `transfer_v2` is the withdraw and only into a live, unpaused, not-closing
39
- * bridge; `create_rollup` funds the reserve of the bridge it creates; `migrate_rollup` fills the successor's reserve.
54
+ * Tx types that may CREDIT a reserve (R2). `withdraw_token_v2` (bridge branch) is the withdraw and
55
+ * only into a live, unpaused, not-closing bridge; `create_rollup` funds the reserve of the bridge it creates;
56
+ * `migrate_rollup` fills the successor's reserve.
40
57
  */
41
58
  const BRIDGE_RESERVE_CREDIT_TX_TYPES = [
42
- BRIDGE_WITHDRAW_TX_TYPE,
59
+ BRIDGE_WITHDRAW_V2_TX_TYPE,
43
60
  "create_rollup",
44
61
  "migrate_rollup"
45
62
  ];
46
63
  /** Longest `migratedTo` chain followed for a refund (a loop or a pathological chain stops the walk, fail closed). */
47
64
  const BRIDGE_MAX_MIGRATION_HOPS = 16;
48
65
  const stripTxTypePrefix = (txType) => String(txType || "").replace(/^fg:t:/, "");
66
+ /**
67
+ * `isBridgeRelease(txType, itx)` (§2.3, one definition for the whole design): `txType` is `deposit_token_v2` and
68
+ * `itx.kind ∈ {1,2,3}`. Stateless (the kind is in the itx); the protocol also requires the row `itx.rollup` names to be a
69
+ * v2 bridge (both keys must match). Strict: a kind that is not one of the three numbers is not a bridge release.
70
+ */
71
+ function isBridgeRelease(txType, itx) {
72
+ if (stripTxTypePrefix(txType) !== BRIDGE_RELEASE_V2_TX_TYPE) return false;
73
+ const kind = itx?.kind;
74
+ return typeof kind === "number" && BRIDGE_RELEASE_KINDS.includes(kind);
75
+ }
76
+ /**
77
+ * For the gate: an `isBridgeRelease` that also NAMES its bridge (`itx.rollup` a non-empty string). Narrower, so fail
78
+ * closed: a bridge kind without a bridge is no release at all (the protocol refuses it before any write), and it owns
79
+ * nothing in the release key space and may debit no reserve.
80
+ */
81
+ function isNamedBridgeRelease(txType, itx) {
82
+ const rollupField = itx?.rollup;
83
+ return isBridgeRelease(txType, itx) && typeof rollupField === "string" && rollupField.length > 0;
84
+ }
85
+ /**
86
+ * The `transaction.maxTxSize` / `maxGasOps` key a tx is measured by: `fg:t:deposit_token_v2_bridge` for a bridge release,
87
+ * the tx type itself otherwise (so every other tx, and every v1 deposit, keeps its key and limit).
88
+ */
89
+ function txResourceKey(txType, itx) {
90
+ return isBridgeRelease(txType, itx) ? BRIDGE_RELEASE_V2_RESOURCE_KEY : txType;
91
+ }
49
92
  /** Strip a `did:abt:` prefix and whitespace: the form used in the evidence index and in rows. */
50
93
  const bareAddress = (address) => String(address ?? "").trim().replace(/^did:abt:/, "");
51
94
  /** Whether `address` has the DID role of a bridge reserve. Syntactic only: see the file header. */
@@ -202,7 +245,8 @@ async function prepareBridgeReserveGate(context) {
202
245
  });
203
246
  const input = {
204
247
  reserves: /* @__PURE__ */ new Map(),
205
- release: null
248
+ release: null,
249
+ withdraw: null
206
250
  };
207
251
  Object.defineProperty(context, "_bridgeReserveGate", {
208
252
  value: input,
@@ -222,7 +266,8 @@ async function prepareBridgeReserveGate(context) {
222
266
  for (const k of tracker.writtenKeys.get(table) ?? []) consider(k);
223
267
  }
224
268
  for (const k of tracker.directBalanceWrites ?? []) consider(k);
225
- const isRelease = stripTxTypePrefix(context.txType) === BRIDGE_RELEASE_TX_TYPE;
269
+ const txType = stripTxTypePrefix(context.txType);
270
+ const isRelease = isNamedBridgeRelease(txType, context.itx);
226
271
  if (candidates.size === 0 && !isRelease) return;
227
272
  await Promise.all([...candidates].map(async (key) => {
228
273
  try {
@@ -232,11 +277,16 @@ async function prepareBridgeReserveGate(context) {
232
277
  }
233
278
  }));
234
279
  if (isRelease) try {
235
- input.release = await resolveBridgeForRelease(context, context.itx);
280
+ input.release = await resolveBridgeForRelease(context, { bridge: context.itx.rollup });
236
281
  } catch {
237
282
  input.release = null;
238
283
  }
284
+ if (txType === BRIDGE_WITHDRAW_V2_TX_TYPE && candidates.size > 0) try {
285
+ input.withdraw = await resolveBridge(context, context.itx?.rollup);
286
+ } catch {
287
+ input.withdraw = null;
288
+ }
239
289
  }
240
290
 
241
291
  //#endregion
242
- export { BRIDGE_MAX_MIGRATION_HOPS, BRIDGE_RECONCILE_TX_TYPES, BRIDGE_RELEASE_TX_TYPE, BRIDGE_RESERVE_CREDIT_TX_TYPES, BRIDGE_RESERVE_DEBIT_TX_TYPES, BRIDGE_WITHDRAW_TX_TYPE, bareAddress, describeBridge, isBridgeReserveRole, prepareBridgeReserveGate, reserveIndexKey, resolveBridge, resolveBridgeForRelease, resolveBridgeForReserve, stripTxTypePrefix };
292
+ export { BRIDGE_MAX_MIGRATION_HOPS, BRIDGE_RECONCILE_TX_TYPES, BRIDGE_RELEASE_KINDS, BRIDGE_RELEASE_V2_RESOURCE_KEY, BRIDGE_RELEASE_V2_TX_TYPE, BRIDGE_RESERVE_CREDIT_TX_TYPES, BRIDGE_RESERVE_DEBIT_TX_TYPES, BRIDGE_WITHDRAW_V2_TX_TYPE, bareAddress, describeBridge, isBridgeRelease, isBridgeReserveRole, isNamedBridgeRelease, prepareBridgeReserveGate, reserveIndexKey, resolveBridge, resolveBridgeForRelease, resolveBridgeForReserve, stripTxTypePrefix, txResourceKey };
@@ -3,12 +3,13 @@ import { ResolvedBridge } from "../bridge-reserves.mjs";
3
3
  //#region src/helpers/bridge-withdraw.d.ts
4
4
 
5
5
  /**
6
- * Withdraw rule for a `transfer_v2` into a bridge reserve (#1465, bridge/docs/ocap-protocols.md §2 and §v2.4).
6
+ * Withdraw rule for the bridge branch of `withdraw_token_v2` (#1691, bridge/docs/deposit-withdraw-v2-unification.md §2.4),
7
+ * the only way to move funds into a bridge reserve.
7
8
  *
8
- * Pure function of (tx, itx, resolved bridge, master switch): no state, no clock. The caller resolves the bridge that
9
- * owns `itx.to` with `resolveBridgeForReserve` (pre-tx flags); this module validates what a withdraw must look like and
10
- * takes every parameter from that bridge's body. It is used twice on purpose:
11
- * - by the `transfer_v2` protocol, which rejects with the returned code;
9
+ * Pure function of (tx, itx, resolved bridge, master switch): no state, no clock. The caller resolves the bridge `itx.rollup`
10
+ * names (pre-tx flags); this module validates what a withdraw must look like and takes every parameter from that bridge's
11
+ * body. It is used twice on purpose:
12
+ * - by the `withdraw_token_v2` protocol, which rejects with the returned code;
12
13
  * - by the post-execution invariant gate (rule R2), as defence in depth against any other path that credits a reserve.
13
14
  */
14
15
  declare const BRIDGE_DISABLED = "BRIDGE_DISABLED";
@@ -20,7 +21,9 @@ declare const BRIDGE_WITHDRAW_AMOUNT = "BRIDGE_WITHDRAW_AMOUNT";
20
21
  declare const BRIDGE_WITHDRAW_RECIPIENT = "BRIDGE_WITHDRAW_RECIPIENT";
21
22
  declare const BRIDGE_WITHDRAW_DELEGATED = "BRIDGE_WITHDRAW_DELEGATED";
22
23
  declare const BRIDGE_WITHDRAW_FEE = "BRIDGE_WITHDRAW_FEE";
23
- type BridgeWithdrawErrorCode = typeof BRIDGE_DISABLED | typeof BRIDGE_PAUSED | typeof BRIDGE_CLOSED | typeof BRIDGE_WITHDRAW_RESERVE | typeof BRIDGE_WITHDRAW_TOKEN | typeof BRIDGE_WITHDRAW_AMOUNT | typeof BRIDGE_WITHDRAW_RECIPIENT | typeof BRIDGE_WITHDRAW_DELEGATED | typeof BRIDGE_WITHDRAW_FEE;
24
+ /** `withdraw_token_v2` into a v2 bridge carrying `tx.signatures` (§2.4 step 5, D3: same as `VerifyMultiSig(0)`). */
25
+ declare const BRIDGE_WITHDRAW_MULTISIG = "BRIDGE_WITHDRAW_MULTISIG";
26
+ type BridgeWithdrawErrorCode = typeof BRIDGE_DISABLED | typeof BRIDGE_PAUSED | typeof BRIDGE_CLOSED | typeof BRIDGE_WITHDRAW_RESERVE | typeof BRIDGE_WITHDRAW_TOKEN | typeof BRIDGE_WITHDRAW_AMOUNT | typeof BRIDGE_WITHDRAW_RECIPIENT | typeof BRIDGE_WITHDRAW_DELEGATED | typeof BRIDGE_WITHDRAW_FEE | typeof BRIDGE_WITHDRAW_MULTISIG;
24
27
  interface BridgeWithdrawFailure {
25
28
  code: BridgeWithdrawErrorCode;
26
29
  message: string;
@@ -41,46 +44,27 @@ interface WithdrawContext {
41
44
  } | null;
42
45
  tx?: {
43
46
  delegator?: string;
47
+ signatures?: unknown;
48
+ signaturesList?: unknown;
44
49
  } | null;
45
50
  itx?: unknown;
46
- /** Token inputs after `transfer_v2` merged `itx.value` into them. Falls back to `itx` when absent. */
47
- tokens?: Array<{
48
- address: string;
49
- value: string;
50
- }>;
51
- assets?: string[];
52
51
  }
52
+ declare function isBridgeWithdrawFailure(x: BridgeWithdrawOk | BridgeWithdrawFailure): x is BridgeWithdrawFailure;
53
53
  /**
54
- * Token inputs of a `transfer_v2` itx the way the protocol sees them after its merge step: a positive primary-token
55
- * `itx.value` is added to the OFFICIAL token entry, a zero or absent value changes nothing. The envelope expectation
56
- * and the withdraw rule both read the itx through this function, so they can not disagree about what is being sent.
57
- */
58
- declare function mergedTransferTokens(itx: {
59
- value?: unknown;
60
- }, officialToken: string | undefined): Array<{
61
- address: string;
62
- value: string;
63
- }>;
64
- /**
65
- * The amount and recipient a withdraw into `bridge` carries, decoded exactly like `checkBridgeWithdraw` does (same token
66
- * rule, same strict parser input), without the policy checks. `null` when the itx is not shaped like a withdraw of the
67
- * bridge token. `ethRecipient` is the raw value as written (the envelope must show that), `''` when absent.
54
+ * The amount and recipient a `withdraw_token_v2` into a bridge of token `bridgeToken` carries (§4): `amount` is
55
+ * `itx.token.value`, `ethRecipient` is `itx.to` exactly as written (the envelope must show that). `null` when
56
+ * `itx.token.address` is not the bridge token or the amount is not a decimal string. The clear-signing expectation and
57
+ * the in-branch envelope re-check both read the itx through this function.
68
58
  */
69
- declare function decodeBridgeWithdrawFields(itx: {
70
- value?: unknown;
71
- data?: {
72
- typeUrl?: string;
73
- value?: unknown;
74
- } | null;
75
- }, bridgeToken: string, officialToken: string | undefined): {
59
+ declare function decodeBridgeWithdrawV2Fields(itx: unknown, bridgeToken: string): {
76
60
  amount: string;
77
61
  ethRecipient: string;
78
62
  } | null;
79
63
  /**
80
- * Validate a transfer into the reserve of `bridge`. Returns the parsed withdraw, or the first failing check.
81
- * Order follows the design: 1 accepting, 2 token set and amount range, 3 recipient, 4 delegator, 5 fee.
64
+ * The bridge branch of `withdraw_token_v2` (§2.4 steps 1-5): pure function of (tx, itx, resolved bridge, config).
65
+ * `bridge` is the v2 row `itx.rollup` resolves to (pre-tx flags); the reserve is never read from the itx, it is
66
+ * `toBridgeReserveAddress(itx.rollup)`. Used by the protocol and by gate R2.
82
67
  */
83
- declare function checkBridgeWithdraw(context: WithdrawContext, bridge: ResolvedBridge): BridgeWithdrawOk | BridgeWithdrawFailure;
84
- declare function isBridgeWithdrawFailure(x: BridgeWithdrawOk | BridgeWithdrawFailure): x is BridgeWithdrawFailure;
68
+ declare function checkBridgeWithdrawV2(context: WithdrawContext, bridge: ResolvedBridge): BridgeWithdrawOk | BridgeWithdrawFailure;
85
69
  //#endregion
86
- export { BRIDGE_CLOSED, BRIDGE_DISABLED, BRIDGE_PAUSED, BRIDGE_WITHDRAW_AMOUNT, BRIDGE_WITHDRAW_DELEGATED, BRIDGE_WITHDRAW_FEE, BRIDGE_WITHDRAW_RECIPIENT, BRIDGE_WITHDRAW_RESERVE, BRIDGE_WITHDRAW_TOKEN, BridgeWithdrawErrorCode, BridgeWithdrawFailure, BridgeWithdrawOk, checkBridgeWithdraw, decodeBridgeWithdrawFields, isBridgeWithdrawFailure, mergedTransferTokens };
70
+ export { BRIDGE_CLOSED, BRIDGE_DISABLED, BRIDGE_PAUSED, BRIDGE_WITHDRAW_AMOUNT, BRIDGE_WITHDRAW_DELEGATED, BRIDGE_WITHDRAW_FEE, BRIDGE_WITHDRAW_MULTISIG, BRIDGE_WITHDRAW_RECIPIENT, BRIDGE_WITHDRAW_RESERVE, BRIDGE_WITHDRAW_TOKEN, BridgeWithdrawErrorCode, BridgeWithdrawFailure, BridgeWithdrawOk, checkBridgeWithdrawV2, decodeBridgeWithdrawV2Fields, isBridgeWithdrawFailure };
@@ -1,16 +1,16 @@
1
1
  import { parseStrictEthAddress } from "@ocap/mcrypto";
2
2
  import { BN } from "@ocap/util";
3
- import { decodeBigInt } from "@ocap/message";
4
3
  import { getListField } from "@ocap/util/lib/get-list-field";
5
4
 
6
5
  //#region src/helpers/bridge-withdraw.ts
7
6
  /**
8
- * Withdraw rule for a `transfer_v2` into a bridge reserve (#1465, bridge/docs/ocap-protocols.md §2 and §v2.4).
7
+ * Withdraw rule for the bridge branch of `withdraw_token_v2` (#1691, bridge/docs/deposit-withdraw-v2-unification.md §2.4),
8
+ * the only way to move funds into a bridge reserve.
9
9
  *
10
- * Pure function of (tx, itx, resolved bridge, master switch): no state, no clock. The caller resolves the bridge that
11
- * owns `itx.to` with `resolveBridgeForReserve` (pre-tx flags); this module validates what a withdraw must look like and
12
- * takes every parameter from that bridge's body. It is used twice on purpose:
13
- * - by the `transfer_v2` protocol, which rejects with the returned code;
10
+ * Pure function of (tx, itx, resolved bridge, master switch): no state, no clock. The caller resolves the bridge `itx.rollup`
11
+ * names (pre-tx flags); this module validates what a withdraw must look like and takes every parameter from that bridge's
12
+ * body. It is used twice on purpose:
13
+ * - by the `withdraw_token_v2` protocol, which rejects with the returned code;
14
14
  * - by the post-execution invariant gate (rule R2), as defence in depth against any other path that credits a reserve.
15
15
  */
16
16
  const BRIDGE_DISABLED = "BRIDGE_DISABLED";
@@ -22,102 +22,82 @@ const BRIDGE_WITHDRAW_AMOUNT = "BRIDGE_WITHDRAW_AMOUNT";
22
22
  const BRIDGE_WITHDRAW_RECIPIENT = "BRIDGE_WITHDRAW_RECIPIENT";
23
23
  const BRIDGE_WITHDRAW_DELEGATED = "BRIDGE_WITHDRAW_DELEGATED";
24
24
  const BRIDGE_WITHDRAW_FEE = "BRIDGE_WITHDRAW_FEE";
25
+ /** `withdraw_token_v2` into a v2 bridge carrying `tx.signatures` (§2.4 step 5, D3: same as `VerifyMultiSig(0)`). */
26
+ const BRIDGE_WITHDRAW_MULTISIG = "BRIDGE_WITHDRAW_MULTISIG";
25
27
  const fail = (code, message) => ({
26
28
  code,
27
29
  message
28
30
  });
29
31
  const DECIMAL = /^(0|[1-9][0-9]*)$/;
30
- /**
31
- * Total amount of `token` carried by `tokens`.
32
- * Returns null when a value is not a non-negative integer; `extraTokens` is set when any other token is present.
33
- */
34
- function bridgeTokenAmount(context, itx, token) {
35
- const tokens = context.tokens ?? (getListField(itx, "tokens") || []);
36
- let amount = new BN(0);
37
- let extraTokens = !!itx.value && !context.tokens;
38
- for (const t of tokens) {
39
- if (t.address !== token) {
40
- extraTokens = true;
41
- continue;
42
- }
43
- if (typeof t.value !== "string" || !DECIMAL.test(t.value)) return null;
44
- amount = amount.add(new BN(t.value));
45
- }
46
- return {
47
- amount,
48
- extraTokens
49
- };
32
+ function isBridgeWithdrawFailure(x) {
33
+ return "code" in x;
50
34
  }
51
- /**
52
- * Token inputs of a `transfer_v2` itx the way the protocol sees them after its merge step: a positive primary-token
53
- * `itx.value` is added to the OFFICIAL token entry, a zero or absent value changes nothing. The envelope expectation
54
- * and the withdraw rule both read the itx through this function, so they can not disagree about what is being sent.
55
- */
56
- function mergedTransferTokens(itx, officialToken) {
57
- const tokens = (getListField(itx, "tokens") || []).map((t) => ({ ...t }));
58
- if (itx.value) {
59
- const amount = new BN(decodeBigInt(itx.value));
60
- if (amount.gt(new BN(0))) {
61
- const existing = tokens.find((t) => t.address === officialToken);
62
- if (existing) existing.value = new BN(existing.value).add(amount).toString(10);
63
- else tokens.push({
64
- address: officialToken,
65
- value: amount.toString(10)
66
- });
67
- }
68
- }
69
- return tokens;
35
+ /** A fee field of the bridge branch: empty or exactly `'0'`. */
36
+ const isNoFee = (x) => x === void 0 || x === null || x === "" || x === "0";
37
+ /** `itx.data` of the bridge branch must be empty: absent, or an `Any` with no type and no value. */
38
+ function isEmptyData(data) {
39
+ if (data === void 0 || data === null) return true;
40
+ if (typeof data !== "object") return false;
41
+ const d = data;
42
+ for (const k of Object.keys(d)) if (k !== "typeUrl" && k !== "type" && k !== "value") return false;
43
+ if (d.typeUrl || d.type) return false;
44
+ const v = d.value;
45
+ if (v === void 0 || v === null || v === "") return true;
46
+ return typeof v.length === "number" && v.length === 0;
70
47
  }
71
48
  /**
72
- * The amount and recipient a withdraw into `bridge` carries, decoded exactly like `checkBridgeWithdraw` does (same token
73
- * rule, same strict parser input), without the policy checks. `null` when the itx is not shaped like a withdraw of the
74
- * bridge token. `ethRecipient` is the raw value as written (the envelope must show that), `''` when absent.
49
+ * The amount and recipient a `withdraw_token_v2` into a bridge of token `bridgeToken` carries (§4): `amount` is
50
+ * `itx.token.value`, `ethRecipient` is `itx.to` exactly as written (the envelope must show that). `null` when
51
+ * `itx.token.address` is not the bridge token or the amount is not a decimal string. The clear-signing expectation and
52
+ * the in-branch envelope re-check both read the itx through this function.
75
53
  */
76
- function decodeBridgeWithdrawFields(itx, bridgeToken, officialToken) {
77
- if ((getListField(itx, "assets") || []).length > 0) return null;
78
- const total = bridgeTokenAmount({ tokens: mergedTransferTokens(itx, officialToken) }, itx, bridgeToken);
79
- if (!total || total.extraTokens) return null;
80
- const data = itx.data;
81
- const ethRecipient = data && data.typeUrl === "fg:x:address" && typeof data.value === "string" ? data.value : "";
54
+ function decodeBridgeWithdrawV2Fields(itx, bridgeToken) {
55
+ const x = itx || {};
56
+ const token = x.token;
57
+ if (!token || typeof token !== "object") return null;
58
+ if (!bridgeToken || token.address !== bridgeToken) return null;
59
+ if (typeof token.value !== "string" || !DECIMAL.test(token.value)) return null;
82
60
  return {
83
- amount: total.amount.toString(10),
84
- ethRecipient
61
+ amount: token.value,
62
+ ethRecipient: typeof x.to === "string" ? x.to : ""
85
63
  };
86
64
  }
87
65
  /**
88
- * Validate a transfer into the reserve of `bridge`. Returns the parsed withdraw, or the first failing check.
89
- * Order follows the design: 1 accepting, 2 token set and amount range, 3 recipient, 4 delegator, 5 fee.
66
+ * The bridge branch of `withdraw_token_v2` (§2.4 steps 1-5): pure function of (tx, itx, resolved bridge, config).
67
+ * `bridge` is the v2 row `itx.rollup` resolves to (pre-tx flags); the reserve is never read from the itx, it is
68
+ * `toBridgeReserveAddress(itx.rollup)`. Used by the protocol and by gate R2.
90
69
  */
91
- function checkBridgeWithdraw(context, bridge) {
70
+ function checkBridgeWithdrawV2(context, bridge) {
92
71
  const { tx } = context;
93
72
  const itx = context.itx || {};
94
73
  const body = bridge.body;
74
+ if (typeof itx.rollup !== "string" || itx.rollup !== bridge.address) return fail(BRIDGE_WITHDRAW_RESERVE, `itx.rollup must be the canonical address of the bridge ${bridge.address}`);
75
+ const token = itx.token;
76
+ if (!token || typeof token !== "object" || typeof token.address !== "string" || !token.address) return fail(BRIDGE_WITHDRAW_TOKEN, "Bridge withdraw requires itx.token = { address, value }");
77
+ if (typeof token.value !== "string" || !DECIMAL.test(token.value)) return fail(BRIDGE_WITHDRAW_AMOUNT, "Bridge withdraw amount must be a decimal string");
78
+ const parsed = parseStrictEthAddress(itx.to);
79
+ if (!parsed.ok) return fail(BRIDGE_WITHDRAW_RECIPIENT, `Bridge withdraw recipient is not a valid Ethereum address (${parsed.reason})`);
80
+ if (itx.proposer !== void 0 && itx.proposer !== null && itx.proposer !== "") return fail(BRIDGE_WITHDRAW_FEE, "Bridge withdraw has no proposer");
81
+ if (!isNoFee(itx.maxFee) || !isNoFee(itx.actualFee)) return fail(BRIDGE_WITHDRAW_FEE, "Bridge withdraw carries no maxFee or actualFee");
82
+ if (!isEmptyData(itx.data)) return fail(BRIDGE_WITHDRAW_RECIPIENT, "Bridge withdraw carries no itx.data: the recipient is itx.to");
95
83
  if (!context.config?.transaction?.bridge?.enabled) return fail(BRIDGE_DISABLED, "Bridge withdraw is not available: transaction.bridge.enabled is off");
96
84
  if (bridge.closing || bridge.final || bridge.migrated) return fail(BRIDGE_CLOSED, `Bridge ${bridge.address} does not accept withdraws (closing, final or migrated)`);
97
85
  if (bridge.paused) return fail(BRIDGE_PAUSED, `Bridge ${bridge.address} is paused`);
98
- const token = bridge.token;
99
- if (!token) return fail(BRIDGE_WITHDRAW_TOKEN, "Bridge has no token");
100
- if ((context.assets ?? (getListField(itx, "assets") || [])).length > 0) return fail(BRIDGE_WITHDRAW_TOKEN, "Bridge withdraw can not carry assets");
101
- const total = bridgeTokenAmount(context, itx, token);
102
- if (!total || total.extraTokens) return fail(BRIDGE_WITHDRAW_TOKEN, `Bridge withdraw must carry the bridge token ${token} only`);
103
- if (total.amount.lte(new BN(0))) return fail(BRIDGE_WITHDRAW_AMOUNT, "Bridge withdraw amount must be positive");
86
+ if (!bridge.token) return fail(BRIDGE_WITHDRAW_TOKEN, "Bridge has no token");
87
+ if (token.address !== bridge.token) return fail(BRIDGE_WITHDRAW_TOKEN, `Bridge withdraw must carry the bridge token ${bridge.token}`);
88
+ const amount = new BN(token.value);
89
+ if (amount.lte(new BN(0))) return fail(BRIDGE_WITHDRAW_AMOUNT, "Bridge withdraw amount must be positive");
104
90
  if (!DECIMAL.test(String(body.minWithdraw)) || !DECIMAL.test(String(body.maxWithdraw))) return fail(BRIDGE_WITHDRAW_AMOUNT, "Bridge withdraw limits are not configured");
105
- if (total.amount.lt(new BN(String(body.minWithdraw)))) return fail(BRIDGE_WITHDRAW_AMOUNT, `Bridge withdraw amount is below minWithdraw ${body.minWithdraw}`);
106
- if (total.amount.gt(new BN(String(body.maxWithdraw)))) return fail(BRIDGE_WITHDRAW_AMOUNT, `Bridge withdraw amount is above maxWithdraw ${body.maxWithdraw}`);
107
- const data = itx.data;
108
- if (!data || data.typeUrl !== "fg:x:address" || typeof data.value !== "string") return fail(BRIDGE_WITHDRAW_RECIPIENT, "Bridge withdraw requires itx.data = { typeUrl: \"fg:x:address\", value }");
109
- const parsed = parseStrictEthAddress(data.value);
110
- if (!parsed.ok) return fail(BRIDGE_WITHDRAW_RECIPIENT, `Bridge withdraw recipient is not a valid Ethereum address (${parsed.reason})`);
111
- if (tx?.delegator) return fail(BRIDGE_WITHDRAW_DELEGATED, "Bridge withdraw can not be delegated");
91
+ if (amount.lt(new BN(String(body.minWithdraw)))) return fail(BRIDGE_WITHDRAW_AMOUNT, `Bridge withdraw amount is below minWithdraw ${body.minWithdraw}`);
92
+ if (amount.gt(new BN(String(body.maxWithdraw)))) return fail(BRIDGE_WITHDRAW_AMOUNT, `Bridge withdraw amount is above maxWithdraw ${body.maxWithdraw}`);
112
93
  if (!/^0+$/.test(String(body.withdrawFee ?? "0"))) return fail(BRIDGE_WITHDRAW_FEE, "Bridge withdrawFee must be 0");
94
+ if (tx?.delegator) return fail(BRIDGE_WITHDRAW_DELEGATED, "Bridge withdraw can not be delegated");
95
+ if ((getListField(tx || {}, "signatures") || []).length > 0) return fail(BRIDGE_WITHDRAW_MULTISIG, "Bridge withdraw can not carry tx.signatures");
113
96
  return {
114
- amount: total.amount.toString(10),
97
+ amount: amount.toString(10),
115
98
  ethRecipient: parsed.lowercase
116
99
  };
117
100
  }
118
- function isBridgeWithdrawFailure(x) {
119
- return "code" in x;
120
- }
121
101
 
122
102
  //#endregion
123
- export { BRIDGE_CLOSED, BRIDGE_DISABLED, BRIDGE_PAUSED, BRIDGE_WITHDRAW_AMOUNT, BRIDGE_WITHDRAW_DELEGATED, BRIDGE_WITHDRAW_FEE, BRIDGE_WITHDRAW_RECIPIENT, BRIDGE_WITHDRAW_RESERVE, BRIDGE_WITHDRAW_TOKEN, checkBridgeWithdraw, decodeBridgeWithdrawFields, isBridgeWithdrawFailure, mergedTransferTokens };
103
+ export { BRIDGE_CLOSED, BRIDGE_DISABLED, BRIDGE_PAUSED, BRIDGE_WITHDRAW_AMOUNT, BRIDGE_WITHDRAW_DELEGATED, BRIDGE_WITHDRAW_FEE, BRIDGE_WITHDRAW_MULTISIG, BRIDGE_WITHDRAW_RECIPIENT, BRIDGE_WITHDRAW_RESERVE, BRIDGE_WITHDRAW_TOKEN, checkBridgeWithdrawV2, decodeBridgeWithdrawV2Fields, isBridgeWithdrawFailure };