@ocap/tx-pipeline 1.31.1 → 1.31.3
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/esm/bridge-release-keys.d.mts +33 -0
- package/esm/bridge-release-keys.mjs +81 -0
- package/esm/bridge-reserves.d.mts +107 -0
- package/esm/bridge-reserves.mjs +242 -0
- package/esm/helpers/bridge-withdraw.d.mts +86 -0
- package/esm/helpers/bridge-withdraw.mjs +123 -0
- package/esm/helpers/replay-guard.d.mts +19 -1
- package/esm/helpers/replay-guard.mjs +39 -1
- package/esm/index.d.mts +3 -1
- package/esm/index.mjs +8 -6
- package/esm/legacy-protocol-sunset.d.mts +2 -2
- package/esm/legacy-protocol-sunset.mjs +5 -5
- package/esm/legacy-rollup-archive.d.mts +67 -0
- package/esm/legacy-rollup-archive.mjs +199 -0
- package/esm/library.d.mts +4 -4
- package/esm/library.mjs +3 -3
- package/esm/native-abt-exchange.d.mts +3 -0
- package/esm/native-abt-exchange.mjs +93 -46
- package/esm/pipes/bridge-release-gate.d.mts +30 -0
- package/esm/pipes/bridge-release-gate.mjs +143 -0
- package/esm/pipes/bridge-reserve-v2-gate.d.mts +53 -0
- package/esm/pipes/bridge-reserve-v2-gate.mjs +264 -0
- package/esm/pipes/decode-tx.mjs +29 -4
- package/esm/pipes/extract-signer.mjs +1 -1
- package/esm/pipes/extract-state.mjs +2 -2
- package/esm/pipes/post-execution-invariant-gate.d.mts +19 -2
- package/esm/pipes/post-execution-invariant-gate.mjs +121 -6
- package/esm/pipes/pre-flight-waf.d.mts +18 -1
- package/esm/pipes/pre-flight-waf.mjs +48 -13
- package/esm/pipes/update-owner.mjs +1 -1
- package/esm/pipes/verify-account-migration.mjs +1 -1
- package/esm/pipes/verify-delegation.mjs +49 -25
- package/esm/pipes/verify-multisig-v2.mjs +5 -4
- package/esm/pipes/verify-multisig.mjs +5 -4
- package/esm/pipes/verify-signature.d.mts +7 -1
- package/esm/pipes/verify-signature.mjs +119 -16
- package/esm/pipes/verify-signer.mjs +2 -2
- package/esm/pipes/verify-token-access.mjs +1 -1
- package/esm/pipes/verify-token-balance.mjs +1 -1
- package/esm/pipes/verify-tx-input.mjs +1 -1
- package/esm/pipes/verify-tx-size.mjs +1 -1
- package/esm/pipes/verify-tx.mjs +12 -5
- package/esm/super-suites-foundation.mjs +6 -6
- package/esm/transaction-block.d.mts +6 -2
- package/esm/transaction-block.mjs +282 -40
- package/esm/uniswap-router.d.mts +247 -1
- package/esm/uniswap-router.mjs +573 -2
- package/lib/bridge-release-keys.cjs +88 -0
- package/lib/bridge-release-keys.d.cts +33 -0
- package/lib/bridge-reserves.cjs +257 -0
- package/lib/bridge-reserves.d.cts +107 -0
- package/lib/helpers/bridge-withdraw.cjs +136 -0
- package/lib/helpers/bridge-withdraw.d.cts +86 -0
- package/lib/helpers/replay-guard.cjs +39 -0
- package/lib/helpers/replay-guard.d.cts +20 -2
- package/lib/index.cjs +25 -5
- package/lib/index.d.cts +3 -1
- package/lib/legacy-protocol-sunset.cjs +7 -7
- package/lib/legacy-protocol-sunset.d.cts +2 -2
- package/lib/legacy-rollup-archive.cjs +208 -0
- package/lib/legacy-rollup-archive.d.cts +67 -0
- package/lib/library.cjs +14 -68
- package/lib/library.d.cts +4 -4
- package/lib/native-abt-exchange.cjs +93 -46
- package/lib/native-abt-exchange.d.cts +3 -0
- package/lib/pipes/bridge-release-gate.cjs +145 -0
- package/lib/pipes/bridge-release-gate.d.cts +30 -0
- package/lib/pipes/bridge-reserve-v2-gate.cjs +273 -0
- package/lib/pipes/bridge-reserve-v2-gate.d.cts +53 -0
- package/lib/pipes/decode-itx.d.cts +1 -1
- package/lib/pipes/decode-tx.cjs +28 -3
- package/lib/pipes/decode-tx.d.cts +1 -1
- package/lib/pipes/extract-signer.cjs +1 -1
- package/lib/pipes/extract-state.cjs +3 -3
- package/lib/pipes/post-execution-invariant-gate.cjs +121 -4
- package/lib/pipes/post-execution-invariant-gate.d.cts +19 -2
- package/lib/pipes/pre-flight-waf.cjs +51 -13
- package/lib/pipes/pre-flight-waf.d.cts +19 -2
- package/lib/pipes/update-owner.cjs +1 -1
- package/lib/pipes/update-owner.d.cts +1 -1
- package/lib/pipes/verify-account-migration.cjs +1 -1
- package/lib/pipes/verify-delegation.cjs +50 -26
- package/lib/pipes/verify-multisig-v2.cjs +5 -4
- package/lib/pipes/verify-multisig.cjs +5 -4
- package/lib/pipes/verify-service-fee.d.cts +1 -1
- package/lib/pipes/verify-signature.cjs +118 -14
- package/lib/pipes/verify-signature.d.cts +8 -2
- package/lib/pipes/verify-signer.cjs +2 -2
- package/lib/pipes/verify-token-access.cjs +1 -1
- package/lib/pipes/verify-token-balance.cjs +1 -1
- package/lib/pipes/verify-tx-input.cjs +1 -1
- package/lib/pipes/verify-tx-size.cjs +1 -1
- package/lib/pipes/verify-tx-size.d.cts +1 -1
- package/lib/pipes/verify-tx.cjs +11 -5
- package/lib/pipes/verify-tx.d.cts +1 -1
- package/lib/super-suites-foundation.cjs +6 -6
- package/lib/transaction-block.cjs +282 -40
- package/lib/transaction-block.d.cts +6 -2
- package/lib/uniswap-router.cjs +576 -1
- package/lib/uniswap-router.d.cts +247 -1
- package/package.json +10 -10
- package/esm/evm-simplified-bridge.d.mts +0 -1370
- package/esm/evm-simplified-bridge.mjs +0 -3875
- package/lib/evm-simplified-bridge.cjs +0 -3943
- package/lib/evm-simplified-bridge.d.cts +0 -1370
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
3
|
+
|
|
4
|
+
//#region src/bridge-release-keys.ts
|
|
5
|
+
/**
|
|
6
|
+
* Evidence-table keys owned by `release_bridge_token` (#1466, bridge/docs/ocap-protocols.md §3 checks 3 and 7, §v2.4).
|
|
7
|
+
*
|
|
8
|
+
* Both keys carry the bridge address, so several bridges on one chain never share a tombstone or a bucket. Every
|
|
9
|
+
* preimage is fixed-width words followed by the bridge DID as the trailing text (`bridgeEvidenceKey`), so two
|
|
10
|
+
* different tuples can never produce the same bytes:
|
|
11
|
+
*
|
|
12
|
+
* - tombstone: `keccak256("arc-bridge:release:" || uint kind || bytes32 evidence || uint ordinal || bridge)`;
|
|
13
|
+
* - bucket: `keccak256("arc-bridge:release-bucket:" || uint ring || uint slot || bridge)`, `ring` 0 = deposit, 1 = refund.
|
|
14
|
+
*
|
|
15
|
+
* A bucket row stores `"<ring>:<slot>:<bridge>:<hourIndex>:<amountInBaseUnits>"`. The row names its own bridge, ring and
|
|
16
|
+
* slot, so the post-execution gate can tell a bucket row from any other evidence row (and refuse every tx type except
|
|
17
|
+
* `release_bridge_token` writing one) without a global list of bridges: it recomputes the key from the row's own fields.
|
|
18
|
+
*
|
|
19
|
+
* `bridge` is always the canonical bridge address (no `did:abt:` prefix), exactly as `itx.bridge` must be spelled.
|
|
20
|
+
*/
|
|
21
|
+
const RELEASE_RING_SLOTS = 24;
|
|
22
|
+
const RELEASE_RINGS = ["deposit", "refund"];
|
|
23
|
+
const TOMBSTONE_PREFIX = "arc-bridge:release:";
|
|
24
|
+
const BUCKET_PREFIX = "arc-bridge:release-bucket:";
|
|
25
|
+
const ringIndex = (ring) => RELEASE_RINGS.indexOf(ring);
|
|
26
|
+
/** Tombstone key of a release. `evidence` is a lowercase `0x` bytes32 (the itx schema enforces the spelling). */
|
|
27
|
+
function getReleaseTombstoneKey(bridge, kind, evidence, ordinal) {
|
|
28
|
+
return (0, _ocap_mcrypto.bridgeEvidenceKey)(TOMBSTONE_PREFIX, [
|
|
29
|
+
{ uint: kind },
|
|
30
|
+
{ bytes32: String(evidence).toLowerCase() },
|
|
31
|
+
{ uint: ordinal },
|
|
32
|
+
{ text: bridge }
|
|
33
|
+
]);
|
|
34
|
+
}
|
|
35
|
+
/** Key of slot `slot` (0..23) of `ring` of `bridge`. */
|
|
36
|
+
function getReleaseBucketKey(bridge, ring, slot) {
|
|
37
|
+
if (!RELEASE_RINGS.includes(ring) || !Number.isInteger(slot) || slot < 0 || slot >= RELEASE_RING_SLOTS) throw new Error(`Invalid release bucket: ${ring}:${slot}`);
|
|
38
|
+
return (0, _ocap_mcrypto.bridgeEvidenceKey)(BUCKET_PREFIX, [
|
|
39
|
+
{ uint: ringIndex(ring) },
|
|
40
|
+
{ uint: slot },
|
|
41
|
+
{ text: bridge }
|
|
42
|
+
]);
|
|
43
|
+
}
|
|
44
|
+
/** Row data of a bucket: names the bridge, ring and slot it belongs to, then `<hourIndex>:<amount>`. */
|
|
45
|
+
const formatBucketData = (bridge, ring, slot, window) => `${ring}:${slot}:${bridge}:${window}`;
|
|
46
|
+
const BUCKET_DATA = /^(deposit|refund):(\d{1,2}):([^:\s]+):((?:0|[1-9]\d{0,14}):(?:0|[1-9]\d{0,77}))$/;
|
|
47
|
+
/** Parses a bucket row's data; `null` when it is not in the bucket format. */
|
|
48
|
+
function parseBucketData(data) {
|
|
49
|
+
if (typeof data !== "string") return null;
|
|
50
|
+
const m = BUCKET_DATA.exec(data);
|
|
51
|
+
if (!m) return null;
|
|
52
|
+
const slot = Number(m[2]);
|
|
53
|
+
if (slot >= RELEASE_RING_SLOTS) return null;
|
|
54
|
+
return {
|
|
55
|
+
ring: m[1],
|
|
56
|
+
slot,
|
|
57
|
+
bridge: m[3],
|
|
58
|
+
window: m[4]
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The bucket coordinates when `row` is a genuine bucket row: its data is in the bucket format AND its key is the key
|
|
63
|
+
* recomputed from the bridge, ring and slot the data names. `null` for every other evidence row.
|
|
64
|
+
*/
|
|
65
|
+
function identifyBucketRow(row) {
|
|
66
|
+
const parsed = parseBucketData(row?.data);
|
|
67
|
+
if (!parsed || typeof row?.hash !== "string") return null;
|
|
68
|
+
let expected;
|
|
69
|
+
try {
|
|
70
|
+
expected = getReleaseBucketKey(parsed.bridge, parsed.ring, parsed.slot);
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
return expected === row.hash.trim().toLowerCase() ? {
|
|
75
|
+
bridge: parsed.bridge,
|
|
76
|
+
ring: parsed.ring,
|
|
77
|
+
slot: parsed.slot
|
|
78
|
+
} : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
exports.RELEASE_RINGS = RELEASE_RINGS;
|
|
83
|
+
exports.RELEASE_RING_SLOTS = RELEASE_RING_SLOTS;
|
|
84
|
+
exports.formatBucketData = formatBucketData;
|
|
85
|
+
exports.getReleaseBucketKey = getReleaseBucketKey;
|
|
86
|
+
exports.getReleaseTombstoneKey = getReleaseTombstoneKey;
|
|
87
|
+
exports.identifyBucketRow = identifyBucketRow;
|
|
88
|
+
exports.parseBucketData = parseBucketData;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/bridge-release-keys.d.ts
|
|
2
|
+
declare const RELEASE_RING_SLOTS = 24;
|
|
3
|
+
declare const RELEASE_RINGS: readonly ["deposit", "refund"];
|
|
4
|
+
type ReleaseRing = (typeof RELEASE_RINGS)[number];
|
|
5
|
+
/** Tombstone key of a release. `evidence` is a lowercase `0x` bytes32 (the itx schema enforces the spelling). */
|
|
6
|
+
declare function getReleaseTombstoneKey(bridge: string, kind: number, evidence: string, ordinal: number): string;
|
|
7
|
+
/** Key of slot `slot` (0..23) of `ring` of `bridge`. */
|
|
8
|
+
declare function getReleaseBucketKey(bridge: string, ring: ReleaseRing, slot: number): string;
|
|
9
|
+
/** Row data of a bucket: names the bridge, ring and slot it belongs to, then `<hourIndex>:<amount>`. */
|
|
10
|
+
declare const formatBucketData: (bridge: string, ring: ReleaseRing, slot: number, window: string) => string;
|
|
11
|
+
interface ParsedBucketData {
|
|
12
|
+
ring: ReleaseRing;
|
|
13
|
+
slot: number;
|
|
14
|
+
bridge: string;
|
|
15
|
+
/** `<hourIndex>:<amount>` */
|
|
16
|
+
window: string;
|
|
17
|
+
}
|
|
18
|
+
/** Parses a bucket row's data; `null` when it is not in the bucket format. */
|
|
19
|
+
declare function parseBucketData(data: unknown): ParsedBucketData | null;
|
|
20
|
+
/**
|
|
21
|
+
* The bucket coordinates when `row` is a genuine bucket row: its data is in the bucket format AND its key is the key
|
|
22
|
+
* recomputed from the bridge, ring and slot the data names. `null` for every other evidence row.
|
|
23
|
+
*/
|
|
24
|
+
declare function identifyBucketRow(row: {
|
|
25
|
+
hash?: unknown;
|
|
26
|
+
data?: unknown;
|
|
27
|
+
} | null | undefined): {
|
|
28
|
+
bridge: string;
|
|
29
|
+
ring: ReleaseRing;
|
|
30
|
+
slot: number;
|
|
31
|
+
} | null;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { ParsedBucketData, RELEASE_RINGS, RELEASE_RING_SLOTS, ReleaseRing, formatBucketData, getReleaseBucketKey, getReleaseTombstoneKey, identifyBucketRow, parseBucketData };
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
3
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
4
|
+
let _arcblock_did = require("@arcblock/did");
|
|
5
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
6
|
+
let _ocap_state = require("@ocap/state");
|
|
7
|
+
|
|
8
|
+
//#region src/bridge-reserves.ts
|
|
9
|
+
/**
|
|
10
|
+
* Bridge reserve resolution (bridge/docs/ocap-protocols.md §v2.3, §v2.4), the seam shared by the post-execution gate
|
|
11
|
+
* and by the withdraw (#1465) and release (#1466) protocols.
|
|
12
|
+
*
|
|
13
|
+
* - A reserve is recognised SYNTACTICALLY by its DID role (`ROLE_BRIDGE_RESERVE`). The role says nothing about who
|
|
14
|
+
* owns the address: anyone can craft a role-25 address without a derivation preimage.
|
|
15
|
+
* - Which bridge a reserve belongs to is only ever read from the `reserve -> bridge` index
|
|
16
|
+
* (`keccak256("arc-bridge:reserve:" || reserve)` in the evidence table, written once by `create_rollup`), and then
|
|
17
|
+
* verified by recomputing `toBridgeReserveAddress(bridge)`. A `body.reserve` field or an itx field is never trusted.
|
|
18
|
+
* - A release names its bridge (`itx.bridge`); the reserve it may debit is `toBridgeReserveAddress(itx.bridge)`, or,
|
|
19
|
+
* for a bridge that has migrated its reserve, the reserve of the `migratedTo` chain (the liability follows the funds,
|
|
20
|
+
* §v2.2 `migrate_rollup`).
|
|
21
|
+
*
|
|
22
|
+
* Reads go to `context.statedb` and are cached on the context for the lifetime of one transaction.
|
|
23
|
+
*/
|
|
24
|
+
const BRIDGE_RELEASE_TX_TYPE = "release_bridge_token";
|
|
25
|
+
const BRIDGE_WITHDRAW_TX_TYPE = "transfer_v2";
|
|
26
|
+
/**
|
|
27
|
+
* Tx types that may DEBIT a reserve (R1). `release_bridge_token` may only debit the reserve of its own bridge (or of the
|
|
28
|
+
* `migratedTo` chain of a migrated bridge), `migrate_rollup` moves a whole reserve to its successor's.
|
|
29
|
+
* The reconcile transaction of §v2.6 is added to {@link BRIDGE_RECONCILE_TX_TYPES} when it is designed; nothing else
|
|
30
|
+
* in the gate has to change for it to be allowed to debit a mapped reserve.
|
|
31
|
+
*/
|
|
32
|
+
const BRIDGE_RECONCILE_TX_TYPES = [];
|
|
33
|
+
const BRIDGE_RESERVE_DEBIT_TX_TYPES = [
|
|
34
|
+
BRIDGE_RELEASE_TX_TYPE,
|
|
35
|
+
"migrate_rollup",
|
|
36
|
+
...BRIDGE_RECONCILE_TX_TYPES
|
|
37
|
+
];
|
|
38
|
+
/**
|
|
39
|
+
* Tx types that may CREDIT a reserve (R2). `transfer_v2` is the withdraw and only into a live, unpaused, not-closing
|
|
40
|
+
* bridge; `create_rollup` funds the reserve of the bridge it creates; `migrate_rollup` fills the successor's reserve.
|
|
41
|
+
*/
|
|
42
|
+
const BRIDGE_RESERVE_CREDIT_TX_TYPES = [
|
|
43
|
+
BRIDGE_WITHDRAW_TX_TYPE,
|
|
44
|
+
"create_rollup",
|
|
45
|
+
"migrate_rollup"
|
|
46
|
+
];
|
|
47
|
+
/** Longest `migratedTo` chain followed for a refund (a loop or a pathological chain stops the walk, fail closed). */
|
|
48
|
+
const BRIDGE_MAX_MIGRATION_HOPS = 16;
|
|
49
|
+
const stripTxTypePrefix = (txType) => String(txType || "").replace(/^fg:t:/, "");
|
|
50
|
+
/** Strip a `did:abt:` prefix and whitespace: the form used in the evidence index and in rows. */
|
|
51
|
+
const bareAddress = (address) => String(address ?? "").trim().replace(/^did:abt:/, "");
|
|
52
|
+
/** Whether `address` has the DID role of a bridge reserve. Syntactic only: see the file header. */
|
|
53
|
+
function isBridgeReserveRole(address) {
|
|
54
|
+
const bare = bareAddress(address);
|
|
55
|
+
if (!bare) return false;
|
|
56
|
+
try {
|
|
57
|
+
return (0, _arcblock_did.isValid)(bare) && (0, _arcblock_did.toTypeInfo)(bare).role === _ocap_mcrypto.types.RoleType.ROLE_BRIDGE_RESERVE;
|
|
58
|
+
} catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** `keccak256("arc-bridge:reserve:" || reserve)`: the `reserve -> bridge` index (bridge/docs §v2.3). */
|
|
63
|
+
const reserveIndexKey = (reserve) => (0, _ocap_mcrypto.bridgeEvidenceKey)("arc-bridge:reserve:", [{ text: bareAddress(reserve) }]);
|
|
64
|
+
const txSecOf = (context) => {
|
|
65
|
+
try {
|
|
66
|
+
return _ocap_state.rollup.toSeconds(context.txTime);
|
|
67
|
+
} catch {
|
|
68
|
+
throw new _ocap_util_lib_error.CustomError("INVALID_TX", "tx time is required to resolve a bridge");
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const cacheOf = (context) => {
|
|
72
|
+
if (!context._bridgeCache) Object.defineProperty(context, "_bridgeCache", {
|
|
73
|
+
value: {
|
|
74
|
+
bridges: /* @__PURE__ */ new Map(),
|
|
75
|
+
reserves: /* @__PURE__ */ new Map()
|
|
76
|
+
},
|
|
77
|
+
enumerable: false,
|
|
78
|
+
configurable: true,
|
|
79
|
+
writable: true
|
|
80
|
+
});
|
|
81
|
+
return context._bridgeCache;
|
|
82
|
+
};
|
|
83
|
+
/** Describe a v2 row; `null` for anything that is not a v2 bridge. */
|
|
84
|
+
function describeBridge(row, txSec) {
|
|
85
|
+
if (!row || !_ocap_state.rollup.isBridgeV2(row)) return null;
|
|
86
|
+
const body = _ocap_state.rollup.getBridgeBody(row);
|
|
87
|
+
if (!body || typeof row.address !== "string" || !row.address) return null;
|
|
88
|
+
const closing = body.closingAt !== null && body.closingAt !== void 0;
|
|
89
|
+
const final = _ocap_state.rollup.isBridgeFinalAt(body, txSec);
|
|
90
|
+
const paused = _ocap_state.rollup.isBridgePausedAt(row, txSec);
|
|
91
|
+
const migrated = typeof body.migratedTo === "string" && body.migratedTo.length > 0;
|
|
92
|
+
return {
|
|
93
|
+
address: row.address,
|
|
94
|
+
reserve: (0, _arcblock_did_util.toBridgeReserveAddress)(row.address),
|
|
95
|
+
state: row,
|
|
96
|
+
body,
|
|
97
|
+
token: String(row.tokenAddress || ""),
|
|
98
|
+
txSec,
|
|
99
|
+
paused,
|
|
100
|
+
closing,
|
|
101
|
+
final,
|
|
102
|
+
migrated,
|
|
103
|
+
acceptingWithdraws: !paused && !closing && !final && !migrated
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The bridge row as it was BEFORE this transaction. If the write tracker recorded the row (the tx wrote it), its recorded
|
|
108
|
+
* before-state wins over the statedb, which already holds the written row: the flags a gate decision rests on (paused,
|
|
109
|
+
* closing, ...) must never come from a row the transaction itself rewrote. A row created by the tx has no before-state.
|
|
110
|
+
*/
|
|
111
|
+
async function readBridgeRow(context, key) {
|
|
112
|
+
const before = context._stateWriteTracker?.beforeStates?.get("rollup");
|
|
113
|
+
if (before) {
|
|
114
|
+
for (const [k, v] of before.entries()) if (bareAddress(k) === key) return v ?? null;
|
|
115
|
+
}
|
|
116
|
+
return await context.statedb.rollup.get(key, context);
|
|
117
|
+
}
|
|
118
|
+
/** Load a bridge by its own address. `null` when there is no such v2 bridge. */
|
|
119
|
+
function resolveBridge(context, bridgeAddress) {
|
|
120
|
+
const key = bareAddress(bridgeAddress);
|
|
121
|
+
if (!key) return Promise.resolve(null);
|
|
122
|
+
const cache = cacheOf(context);
|
|
123
|
+
let hit = cache.bridges.get(key);
|
|
124
|
+
if (!hit) {
|
|
125
|
+
hit = (async () => {
|
|
126
|
+
const row = await readBridgeRow(context, key);
|
|
127
|
+
if (!row || bareAddress(row.address) !== key) return null;
|
|
128
|
+
return describeBridge(row, txSecOf(context));
|
|
129
|
+
})();
|
|
130
|
+
cache.bridges.set(key, hit);
|
|
131
|
+
hit.catch(() => cache.bridges.delete(key));
|
|
132
|
+
}
|
|
133
|
+
return hit;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The bridge a reserve belongs to, or `null` when the address is not the reserve of an existing v2 bridge.
|
|
137
|
+
*
|
|
138
|
+
* `null` for: an address that does not have the reserve role; a role-25 address with no index entry (a crafted
|
|
139
|
+
* address); an index entry that points at a row that is not a v2 bridge; and an index entry whose bridge does not
|
|
140
|
+
* derive this very reserve (`toBridgeReserveAddress(bridge) !== address`). A caller that gets `null` must treat the
|
|
141
|
+
* address as one that accepts no credit and allows no debit.
|
|
142
|
+
*/
|
|
143
|
+
function resolveBridgeForReserve(context, address) {
|
|
144
|
+
const key = bareAddress(address);
|
|
145
|
+
if (!isBridgeReserveRole(key)) return Promise.resolve(null);
|
|
146
|
+
const cache = cacheOf(context);
|
|
147
|
+
let hit = cache.reserves.get(key);
|
|
148
|
+
if (!hit) {
|
|
149
|
+
hit = (async () => {
|
|
150
|
+
const mapped = (await context.statedb.evidence.get(reserveIndexKey(key), context))?.data?.value?.bridge;
|
|
151
|
+
if (typeof mapped !== "string" || !mapped) return null;
|
|
152
|
+
const bridge = await resolveBridge(context, mapped);
|
|
153
|
+
if (!bridge || bridge.reserve !== key) return null;
|
|
154
|
+
return bridge;
|
|
155
|
+
})();
|
|
156
|
+
cache.reserves.set(key, hit);
|
|
157
|
+
hit.catch(() => cache.reserves.delete(key));
|
|
158
|
+
}
|
|
159
|
+
return hit;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Resolve the bridge of a release and the reserves it may debit. `itx.bridge` is the only input taken from the tx;
|
|
163
|
+
* the reserve is always recomputed. `null` when `itx.bridge` is not an existing v2 bridge.
|
|
164
|
+
*
|
|
165
|
+
* It does not look at pause, close or final: those depend on the release kind (a REFUND stays possible after
|
|
166
|
+
* `close_rollup`) and are checked by the release protocol (#1466) with the flags of the returned bridge.
|
|
167
|
+
*/
|
|
168
|
+
async function resolveBridgeForRelease(context, itx) {
|
|
169
|
+
if (typeof itx?.bridge !== "string" || !itx.bridge) return null;
|
|
170
|
+
const start = await resolveBridge(context, itx.bridge);
|
|
171
|
+
if (!start || start.address !== itx.bridge) return null;
|
|
172
|
+
const debitable = new Map([[start.reserve, start]]);
|
|
173
|
+
let cursor = start;
|
|
174
|
+
let hops = 0;
|
|
175
|
+
while (cursor.migrated) {
|
|
176
|
+
if (hops++ >= BRIDGE_MAX_MIGRATION_HOPS) break;
|
|
177
|
+
const next = await resolveBridge(context, cursor.body.migratedTo);
|
|
178
|
+
if (!next || next.address !== cursor.body.migratedTo || debitable.has(next.reserve)) break;
|
|
179
|
+
debitable.set(next.reserve, next);
|
|
180
|
+
cursor = next;
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
bridge: start,
|
|
184
|
+
reserve: start.reserve,
|
|
185
|
+
debitable,
|
|
186
|
+
fundsReserve: cursor.reserve
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Resolve, once per transaction, everything the synchronous post-execution gate needs about bridge reserves. Run it
|
|
191
|
+
* after the protocol and before `inspectPostExecutionInvariants`. Only reserves that appear in the transaction's write
|
|
192
|
+
* set are looked up, so a transaction that touches no reserve costs one set scan and no read.
|
|
193
|
+
*
|
|
194
|
+
* Never throws: a failed read leaves the reserve unresolved (`null`), which the gate treats as "unmapped".
|
|
195
|
+
*/
|
|
196
|
+
async function prepareBridgeReserveGate(context) {
|
|
197
|
+
const tracker = context._stateWriteTracker;
|
|
198
|
+
Object.defineProperty(context, "_bridgeCache", {
|
|
199
|
+
value: void 0,
|
|
200
|
+
enumerable: false,
|
|
201
|
+
configurable: true,
|
|
202
|
+
writable: true
|
|
203
|
+
});
|
|
204
|
+
const input = {
|
|
205
|
+
reserves: /* @__PURE__ */ new Map(),
|
|
206
|
+
release: null
|
|
207
|
+
};
|
|
208
|
+
Object.defineProperty(context, "_bridgeReserveGate", {
|
|
209
|
+
value: input,
|
|
210
|
+
enumerable: false,
|
|
211
|
+
configurable: true,
|
|
212
|
+
writable: true
|
|
213
|
+
});
|
|
214
|
+
if (!tracker) return;
|
|
215
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
216
|
+
const consider = (raw) => {
|
|
217
|
+
const key = bareAddress(raw);
|
|
218
|
+
if (key && isBridgeReserveRole(key)) candidates.add(key);
|
|
219
|
+
};
|
|
220
|
+
for (const table of ["account", "balance"]) {
|
|
221
|
+
for (const k of tracker.afterStates.get(table)?.keys() ?? []) consider(k);
|
|
222
|
+
for (const k of tracker.beforeStates.get(table)?.keys() ?? []) consider(k);
|
|
223
|
+
for (const k of tracker.writtenKeys.get(table) ?? []) consider(k);
|
|
224
|
+
}
|
|
225
|
+
for (const k of tracker.directBalanceWrites ?? []) consider(k);
|
|
226
|
+
const isRelease = stripTxTypePrefix(context.txType) === BRIDGE_RELEASE_TX_TYPE;
|
|
227
|
+
if (candidates.size === 0 && !isRelease) return;
|
|
228
|
+
await Promise.all([...candidates].map(async (key) => {
|
|
229
|
+
try {
|
|
230
|
+
input.reserves.set(key, await resolveBridgeForReserve(context, key));
|
|
231
|
+
} catch {
|
|
232
|
+
input.reserves.set(key, null);
|
|
233
|
+
}
|
|
234
|
+
}));
|
|
235
|
+
if (isRelease) try {
|
|
236
|
+
input.release = await resolveBridgeForRelease(context, context.itx);
|
|
237
|
+
} catch {
|
|
238
|
+
input.release = null;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
//#endregion
|
|
243
|
+
exports.BRIDGE_MAX_MIGRATION_HOPS = BRIDGE_MAX_MIGRATION_HOPS;
|
|
244
|
+
exports.BRIDGE_RECONCILE_TX_TYPES = BRIDGE_RECONCILE_TX_TYPES;
|
|
245
|
+
exports.BRIDGE_RELEASE_TX_TYPE = BRIDGE_RELEASE_TX_TYPE;
|
|
246
|
+
exports.BRIDGE_RESERVE_CREDIT_TX_TYPES = BRIDGE_RESERVE_CREDIT_TX_TYPES;
|
|
247
|
+
exports.BRIDGE_RESERVE_DEBIT_TX_TYPES = BRIDGE_RESERVE_DEBIT_TX_TYPES;
|
|
248
|
+
exports.BRIDGE_WITHDRAW_TX_TYPE = BRIDGE_WITHDRAW_TX_TYPE;
|
|
249
|
+
exports.bareAddress = bareAddress;
|
|
250
|
+
exports.describeBridge = describeBridge;
|
|
251
|
+
exports.isBridgeReserveRole = isBridgeReserveRole;
|
|
252
|
+
exports.prepareBridgeReserveGate = prepareBridgeReserveGate;
|
|
253
|
+
exports.reserveIndexKey = reserveIndexKey;
|
|
254
|
+
exports.resolveBridge = resolveBridge;
|
|
255
|
+
exports.resolveBridgeForRelease = resolveBridgeForRelease;
|
|
256
|
+
exports.resolveBridgeForReserve = resolveBridgeForReserve;
|
|
257
|
+
exports.stripTxTypePrefix = stripTxTypePrefix;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { rollup } from "@ocap/state";
|
|
2
|
+
|
|
3
|
+
//#region src/bridge-reserves.d.ts
|
|
4
|
+
type Ctx = any;
|
|
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
|
+
/**
|
|
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.
|
|
11
|
+
* The reconcile transaction of §v2.6 is added to {@link BRIDGE_RECONCILE_TX_TYPES} when it is designed; nothing else
|
|
12
|
+
* in the gate has to change for it to be allowed to debit a mapped reserve.
|
|
13
|
+
*/
|
|
14
|
+
declare const BRIDGE_RECONCILE_TX_TYPES: readonly string[];
|
|
15
|
+
declare const BRIDGE_RESERVE_DEBIT_TX_TYPES: readonly string[];
|
|
16
|
+
/**
|
|
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.
|
|
19
|
+
*/
|
|
20
|
+
declare const BRIDGE_RESERVE_CREDIT_TX_TYPES: readonly string[];
|
|
21
|
+
/** Longest `migratedTo` chain followed for a refund (a loop or a pathological chain stops the walk, fail closed). */
|
|
22
|
+
declare const BRIDGE_MAX_MIGRATION_HOPS = 16;
|
|
23
|
+
declare const stripTxTypePrefix: (txType: unknown) => string;
|
|
24
|
+
/** Strip a `did:abt:` prefix and whitespace: the form used in the evidence index and in rows. */
|
|
25
|
+
declare const bareAddress: (address: unknown) => string;
|
|
26
|
+
/** Whether `address` has the DID role of a bridge reserve. Syntactic only: see the file header. */
|
|
27
|
+
declare function isBridgeReserveRole(address: unknown): boolean;
|
|
28
|
+
/** `keccak256("arc-bridge:reserve:" || reserve)`: the `reserve -> bridge` index (bridge/docs §v2.3). */
|
|
29
|
+
declare const reserveIndexKey: (reserve: string) => string;
|
|
30
|
+
/** A bridge as seen at the `txTime` of the transaction being executed. */
|
|
31
|
+
interface ResolvedBridge {
|
|
32
|
+
/** the bridge (RollupState) address */
|
|
33
|
+
address: string;
|
|
34
|
+
/** `toBridgeReserveAddress(address)`, recomputed, never read from the body */
|
|
35
|
+
reserve: string;
|
|
36
|
+
/** the stored row */
|
|
37
|
+
state: AnyRow;
|
|
38
|
+
body: rollup.BridgeBodyV2;
|
|
39
|
+
/** the OCAP token of the bridge (the only token its reserve may hold) */
|
|
40
|
+
token: string;
|
|
41
|
+
/** `txTime` in seconds the flags below were computed at */
|
|
42
|
+
txSec: number;
|
|
43
|
+
/** effective pause (a lone pause expires by itself): `isBridgePausedAt` */
|
|
44
|
+
paused: boolean;
|
|
45
|
+
/** `close_rollup` has run: `closingAt` is set */
|
|
46
|
+
closing: boolean;
|
|
47
|
+
/** closed and `finalAt` has passed */
|
|
48
|
+
final: boolean;
|
|
49
|
+
/** the reserve has been moved to `body.migratedTo` */
|
|
50
|
+
migrated: boolean;
|
|
51
|
+
/** new withdraws are accepted: not paused, not closing, not final, not migrated */
|
|
52
|
+
acceptingWithdraws: boolean;
|
|
53
|
+
}
|
|
54
|
+
/** Describe a v2 row; `null` for anything that is not a v2 bridge. */
|
|
55
|
+
declare function describeBridge(row: AnyRow | null | undefined, txSec: number): ResolvedBridge | null;
|
|
56
|
+
/** Load a bridge by its own address. `null` when there is no such v2 bridge. */
|
|
57
|
+
declare function resolveBridge(context: Ctx, bridgeAddress: unknown): Promise<ResolvedBridge | null>;
|
|
58
|
+
/**
|
|
59
|
+
* The bridge a reserve belongs to, or `null` when the address is not the reserve of an existing v2 bridge.
|
|
60
|
+
*
|
|
61
|
+
* `null` for: an address that does not have the reserve role; a role-25 address with no index entry (a crafted
|
|
62
|
+
* address); an index entry that points at a row that is not a v2 bridge; and an index entry whose bridge does not
|
|
63
|
+
* derive this very reserve (`toBridgeReserveAddress(bridge) !== address`). A caller that gets `null` must treat the
|
|
64
|
+
* address as one that accepts no credit and allows no debit.
|
|
65
|
+
*/
|
|
66
|
+
declare function resolveBridgeForReserve(context: Ctx, address: unknown): Promise<ResolvedBridge | null>;
|
|
67
|
+
/** What a `release_bridge_token` (or a reconcile) is allowed to debit. */
|
|
68
|
+
interface ReleaseResolution {
|
|
69
|
+
/** the bridge named by `itx.bridge` */
|
|
70
|
+
bridge: ResolvedBridge;
|
|
71
|
+
/** `toBridgeReserveAddress(itx.bridge)` */
|
|
72
|
+
reserve: string;
|
|
73
|
+
/**
|
|
74
|
+
* Every reserve this release may debit, keyed by reserve: the bridge's own and, when it has migrated its reserve, the
|
|
75
|
+
* reserves of the `migratedTo` chain (the liability follows the funds). The value is the bridge owning that reserve.
|
|
76
|
+
*/
|
|
77
|
+
debitable: Map<string, ResolvedBridge>;
|
|
78
|
+
/** the reserve that holds the funds today: the end of the `migratedTo` chain (the bridge's own when not migrated) */
|
|
79
|
+
fundsReserve: string;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Resolve the bridge of a release and the reserves it may debit. `itx.bridge` is the only input taken from the tx;
|
|
83
|
+
* the reserve is always recomputed. `null` when `itx.bridge` is not an existing v2 bridge.
|
|
84
|
+
*
|
|
85
|
+
* It does not look at pause, close or final: those depend on the release kind (a REFUND stays possible after
|
|
86
|
+
* `close_rollup`) and are checked by the release protocol (#1466) with the flags of the returned bridge.
|
|
87
|
+
*/
|
|
88
|
+
declare function resolveBridgeForRelease(context: Ctx, itx: {
|
|
89
|
+
bridge?: unknown;
|
|
90
|
+
} | null | undefined): Promise<ReleaseResolution | null>;
|
|
91
|
+
/** What the synchronous gate needs, resolved by {@link prepareBridgeReserveGate} before it runs. */
|
|
92
|
+
interface BridgeReserveResolution {
|
|
93
|
+
/** reserve (bare address) -> its bridge, `null` when unmapped */
|
|
94
|
+
reserves: Map<string, ResolvedBridge | null>;
|
|
95
|
+
/** present for `release_bridge_token` txs whose `itx.bridge` resolves */
|
|
96
|
+
release: ReleaseResolution | null;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Resolve, once per transaction, everything the synchronous post-execution gate needs about bridge reserves. Run it
|
|
100
|
+
* after the protocol and before `inspectPostExecutionInvariants`. Only reserves that appear in the transaction's write
|
|
101
|
+
* set are looked up, so a transaction that touches no reserve costs one set scan and no read.
|
|
102
|
+
*
|
|
103
|
+
* Never throws: a failed read leaves the reserve unresolved (`null`), which the gate treats as "unmapped".
|
|
104
|
+
*/
|
|
105
|
+
declare function prepareBridgeReserveGate(context: Ctx): Promise<void>;
|
|
106
|
+
//#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 };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
3
|
+
let _ocap_util = require("@ocap/util");
|
|
4
|
+
let _ocap_message = require("@ocap/message");
|
|
5
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
6
|
+
|
|
7
|
+
//#region src/helpers/bridge-withdraw.ts
|
|
8
|
+
/**
|
|
9
|
+
* Withdraw rule for a `transfer_v2` into a bridge reserve (#1465, bridge/docs/ocap-protocols.md §2 and §v2.4).
|
|
10
|
+
*
|
|
11
|
+
* Pure function of (tx, itx, resolved bridge, master switch): no state, no clock. The caller resolves the bridge that
|
|
12
|
+
* owns `itx.to` with `resolveBridgeForReserve` (pre-tx flags); this module validates what a withdraw must look like and
|
|
13
|
+
* takes every parameter from that bridge's body. It is used twice on purpose:
|
|
14
|
+
* - by the `transfer_v2` protocol, which rejects with the returned code;
|
|
15
|
+
* - by the post-execution invariant gate (rule R2), as defence in depth against any other path that credits a reserve.
|
|
16
|
+
*/
|
|
17
|
+
const BRIDGE_DISABLED = "BRIDGE_DISABLED";
|
|
18
|
+
const BRIDGE_PAUSED = "BRIDGE_PAUSED";
|
|
19
|
+
const BRIDGE_CLOSED = "BRIDGE_CLOSED";
|
|
20
|
+
const BRIDGE_WITHDRAW_RESERVE = "BRIDGE_WITHDRAW_RESERVE";
|
|
21
|
+
const BRIDGE_WITHDRAW_TOKEN = "BRIDGE_WITHDRAW_TOKEN";
|
|
22
|
+
const BRIDGE_WITHDRAW_AMOUNT = "BRIDGE_WITHDRAW_AMOUNT";
|
|
23
|
+
const BRIDGE_WITHDRAW_RECIPIENT = "BRIDGE_WITHDRAW_RECIPIENT";
|
|
24
|
+
const BRIDGE_WITHDRAW_DELEGATED = "BRIDGE_WITHDRAW_DELEGATED";
|
|
25
|
+
const BRIDGE_WITHDRAW_FEE = "BRIDGE_WITHDRAW_FEE";
|
|
26
|
+
const fail = (code, message) => ({
|
|
27
|
+
code,
|
|
28
|
+
message
|
|
29
|
+
});
|
|
30
|
+
const DECIMAL = /^(0|[1-9][0-9]*)$/;
|
|
31
|
+
/**
|
|
32
|
+
* Total amount of `token` carried by `tokens`.
|
|
33
|
+
* Returns null when a value is not a non-negative integer; `extraTokens` is set when any other token is present.
|
|
34
|
+
*/
|
|
35
|
+
function bridgeTokenAmount(context, itx, token) {
|
|
36
|
+
const tokens = context.tokens ?? ((0, _ocap_util_lib_get_list_field.getListField)(itx, "tokens") || []);
|
|
37
|
+
let amount = new _ocap_util.BN(0);
|
|
38
|
+
let extraTokens = !!itx.value && !context.tokens;
|
|
39
|
+
for (const t of tokens) {
|
|
40
|
+
if (t.address !== token) {
|
|
41
|
+
extraTokens = true;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (typeof t.value !== "string" || !DECIMAL.test(t.value)) return null;
|
|
45
|
+
amount = amount.add(new _ocap_util.BN(t.value));
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
amount,
|
|
49
|
+
extraTokens
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Token inputs of a `transfer_v2` itx the way the protocol sees them after its merge step: a positive primary-token
|
|
54
|
+
* `itx.value` is added to the OFFICIAL token entry, a zero or absent value changes nothing. The envelope expectation
|
|
55
|
+
* and the withdraw rule both read the itx through this function, so they can not disagree about what is being sent.
|
|
56
|
+
*/
|
|
57
|
+
function mergedTransferTokens(itx, officialToken) {
|
|
58
|
+
const tokens = ((0, _ocap_util_lib_get_list_field.getListField)(itx, "tokens") || []).map((t) => ({ ...t }));
|
|
59
|
+
if (itx.value) {
|
|
60
|
+
const amount = new _ocap_util.BN((0, _ocap_message.decodeBigInt)(itx.value));
|
|
61
|
+
if (amount.gt(new _ocap_util.BN(0))) {
|
|
62
|
+
const existing = tokens.find((t) => t.address === officialToken);
|
|
63
|
+
if (existing) existing.value = new _ocap_util.BN(existing.value).add(amount).toString(10);
|
|
64
|
+
else tokens.push({
|
|
65
|
+
address: officialToken,
|
|
66
|
+
value: amount.toString(10)
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return tokens;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The amount and recipient a withdraw into `bridge` carries, decoded exactly like `checkBridgeWithdraw` does (same token
|
|
74
|
+
* rule, same strict parser input), without the policy checks. `null` when the itx is not shaped like a withdraw of the
|
|
75
|
+
* bridge token. `ethRecipient` is the raw value as written (the envelope must show that), `''` when absent.
|
|
76
|
+
*/
|
|
77
|
+
function decodeBridgeWithdrawFields(itx, bridgeToken, officialToken) {
|
|
78
|
+
if (((0, _ocap_util_lib_get_list_field.getListField)(itx, "assets") || []).length > 0) return null;
|
|
79
|
+
const total = bridgeTokenAmount({ tokens: mergedTransferTokens(itx, officialToken) }, itx, bridgeToken);
|
|
80
|
+
if (!total || total.extraTokens) return null;
|
|
81
|
+
const data = itx.data;
|
|
82
|
+
const ethRecipient = data && data.typeUrl === "fg:x:address" && typeof data.value === "string" ? data.value : "";
|
|
83
|
+
return {
|
|
84
|
+
amount: total.amount.toString(10),
|
|
85
|
+
ethRecipient
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Validate a transfer into the reserve of `bridge`. Returns the parsed withdraw, or the first failing check.
|
|
90
|
+
* Order follows the design: 1 accepting, 2 token set and amount range, 3 recipient, 4 delegator, 5 fee.
|
|
91
|
+
*/
|
|
92
|
+
function checkBridgeWithdraw(context, bridge) {
|
|
93
|
+
const { tx } = context;
|
|
94
|
+
const itx = context.itx || {};
|
|
95
|
+
const body = bridge.body;
|
|
96
|
+
if (!context.config?.transaction?.bridge?.enabled) return fail(BRIDGE_DISABLED, "Bridge withdraw is not available: transaction.bridge.enabled is off");
|
|
97
|
+
if (bridge.closing || bridge.final || bridge.migrated) return fail(BRIDGE_CLOSED, `Bridge ${bridge.address} does not accept withdraws (closing, final or migrated)`);
|
|
98
|
+
if (bridge.paused) return fail(BRIDGE_PAUSED, `Bridge ${bridge.address} is paused`);
|
|
99
|
+
const token = bridge.token;
|
|
100
|
+
if (!token) return fail(BRIDGE_WITHDRAW_TOKEN, "Bridge has no token");
|
|
101
|
+
if ((context.assets ?? ((0, _ocap_util_lib_get_list_field.getListField)(itx, "assets") || [])).length > 0) return fail(BRIDGE_WITHDRAW_TOKEN, "Bridge withdraw can not carry assets");
|
|
102
|
+
const total = bridgeTokenAmount(context, itx, token);
|
|
103
|
+
if (!total || total.extraTokens) return fail(BRIDGE_WITHDRAW_TOKEN, `Bridge withdraw must carry the bridge token ${token} only`);
|
|
104
|
+
if (total.amount.lte(new _ocap_util.BN(0))) return fail(BRIDGE_WITHDRAW_AMOUNT, "Bridge withdraw amount must be positive");
|
|
105
|
+
if (!DECIMAL.test(String(body.minWithdraw)) || !DECIMAL.test(String(body.maxWithdraw))) return fail(BRIDGE_WITHDRAW_AMOUNT, "Bridge withdraw limits are not configured");
|
|
106
|
+
if (total.amount.lt(new _ocap_util.BN(String(body.minWithdraw)))) return fail(BRIDGE_WITHDRAW_AMOUNT, `Bridge withdraw amount is below minWithdraw ${body.minWithdraw}`);
|
|
107
|
+
if (total.amount.gt(new _ocap_util.BN(String(body.maxWithdraw)))) return fail(BRIDGE_WITHDRAW_AMOUNT, `Bridge withdraw amount is above maxWithdraw ${body.maxWithdraw}`);
|
|
108
|
+
const data = itx.data;
|
|
109
|
+
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 }");
|
|
110
|
+
const parsed = (0, _ocap_mcrypto.parseStrictEthAddress)(data.value);
|
|
111
|
+
if (!parsed.ok) return fail(BRIDGE_WITHDRAW_RECIPIENT, `Bridge withdraw recipient is not a valid Ethereum address (${parsed.reason})`);
|
|
112
|
+
if (tx?.delegator) return fail(BRIDGE_WITHDRAW_DELEGATED, "Bridge withdraw can not be delegated");
|
|
113
|
+
if (!/^0+$/.test(String(body.withdrawFee ?? "0"))) return fail(BRIDGE_WITHDRAW_FEE, "Bridge withdrawFee must be 0");
|
|
114
|
+
return {
|
|
115
|
+
amount: total.amount.toString(10),
|
|
116
|
+
ethRecipient: parsed.lowercase
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function isBridgeWithdrawFailure(x) {
|
|
120
|
+
return "code" in x;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
exports.BRIDGE_CLOSED = BRIDGE_CLOSED;
|
|
125
|
+
exports.BRIDGE_DISABLED = BRIDGE_DISABLED;
|
|
126
|
+
exports.BRIDGE_PAUSED = BRIDGE_PAUSED;
|
|
127
|
+
exports.BRIDGE_WITHDRAW_AMOUNT = BRIDGE_WITHDRAW_AMOUNT;
|
|
128
|
+
exports.BRIDGE_WITHDRAW_DELEGATED = BRIDGE_WITHDRAW_DELEGATED;
|
|
129
|
+
exports.BRIDGE_WITHDRAW_FEE = BRIDGE_WITHDRAW_FEE;
|
|
130
|
+
exports.BRIDGE_WITHDRAW_RECIPIENT = BRIDGE_WITHDRAW_RECIPIENT;
|
|
131
|
+
exports.BRIDGE_WITHDRAW_RESERVE = BRIDGE_WITHDRAW_RESERVE;
|
|
132
|
+
exports.BRIDGE_WITHDRAW_TOKEN = BRIDGE_WITHDRAW_TOKEN;
|
|
133
|
+
exports.checkBridgeWithdraw = checkBridgeWithdraw;
|
|
134
|
+
exports.decodeBridgeWithdrawFields = decodeBridgeWithdrawFields;
|
|
135
|
+
exports.isBridgeWithdrawFailure = isBridgeWithdrawFailure;
|
|
136
|
+
exports.mergedTransferTokens = mergedTransferTokens;
|