@ocap/tx-protocols 1.31.0 → 1.31.2
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-crypto.d.mts +39 -0
- package/esm/bridge-release-crypto.mjs +88 -0
- package/esm/bridge-release-window.d.mts +47 -0
- package/esm/bridge-release-window.mjs +62 -0
- package/esm/bridge-reserve.d.mts +24 -0
- package/esm/bridge-reserve.mjs +41 -0
- package/esm/execute.d.mts +5 -3
- package/esm/execute.mjs +593 -21
- package/esm/gas-security-guard.d.mts +7 -2
- package/esm/gas-security-guard.mjs +25 -16
- package/esm/index.d.mts +38 -19
- package/esm/index.mjs +7 -3
- package/esm/pipes/ensure-cost.mjs +36 -10
- package/esm/pipes/write-ledger.d.mts +2 -2
- package/esm/pipes/write-ledger.mjs +6 -3
- package/esm/protocols/account/delegate.mjs +6 -4
- package/esm/protocols/account/migrate.mjs +5 -3
- package/esm/protocols/account/revoke-delegate.mjs +5 -3
- package/esm/protocols/asset/acquire-v2.mjs +3 -1
- package/esm/protocols/asset/acquire-v3.mjs +4 -2
- package/esm/protocols/asset/consume.mjs +3 -1
- package/esm/protocols/asset/create.mjs +4 -2
- package/esm/protocols/asset/mint.mjs +4 -2
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +1 -1
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +1 -1
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +1 -1
- package/esm/protocols/asset/update.mjs +4 -2
- package/esm/protocols/bridge/release.d.mts +34 -0
- package/esm/protocols/bridge/release.mjs +385 -0
- package/esm/protocols/factory/create.mjs +4 -2
- package/esm/protocols/governance/claim-stake.mjs +35 -11
- package/esm/protocols/governance/return-stake.mjs +4 -2
- package/esm/protocols/governance/revoke-ledger.d.mts +34 -0
- package/esm/protocols/governance/revoke-ledger.mjs +179 -0
- package/esm/protocols/governance/revoke-stake.d.mts +7 -2
- package/esm/protocols/governance/revoke-stake.mjs +58 -29
- package/esm/protocols/governance/slash-stake.mjs +14 -4
- package/esm/protocols/governance/stake.mjs +6 -4
- package/esm/protocols/rollup/bridge-create.d.mts +13 -0
- package/esm/protocols/rollup/bridge-create.mjs +206 -0
- package/esm/protocols/rollup/bridge-membership.d.mts +10 -0
- package/esm/protocols/rollup/bridge-membership.mjs +154 -0
- package/esm/protocols/rollup/bridge-migrate.d.mts +10 -0
- package/esm/protocols/rollup/bridge-migrate.mjs +208 -0
- package/esm/protocols/rollup/bridge-v2.d.mts +113 -0
- package/esm/protocols/rollup/bridge-v2.mjs +219 -0
- package/esm/protocols/rollup/claim-reward.mjs +4 -2
- package/esm/protocols/rollup/close.d.mts +2 -1
- package/esm/protocols/rollup/close.mjs +59 -6
- package/esm/protocols/rollup/create-block.mjs +4 -2
- package/esm/protocols/rollup/create.d.mts +3 -0
- package/esm/protocols/rollup/create.mjs +45 -6
- package/esm/protocols/rollup/join.d.mts +2 -1
- package/esm/protocols/rollup/join.mjs +31 -23
- package/esm/protocols/rollup/leave.d.mts +2 -1
- package/esm/protocols/rollup/leave.mjs +32 -24
- package/esm/protocols/rollup/migrate.d.mts +2 -1
- package/esm/protocols/rollup/migrate.mjs +19 -10
- package/esm/protocols/rollup/pause.d.mts +4 -1
- package/esm/protocols/rollup/pause.mjs +50 -6
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +2 -2
- package/esm/protocols/rollup/resume.d.mts +2 -1
- package/esm/protocols/rollup/resume.mjs +26 -6
- package/esm/protocols/rollup/update.d.mts +5 -1
- package/esm/protocols/rollup/update.mjs +56 -9
- package/esm/protocols/token/create.mjs +4 -2
- package/esm/protocols/token/deposit-v2.mjs +5 -3
- package/esm/protocols/token/withdraw-v2.mjs +5 -3
- package/esm/protocols/token-factory/burn.mjs +4 -2
- package/esm/protocols/token-factory/create.mjs +8 -4
- package/esm/protocols/token-factory/mint.mjs +4 -2
- package/esm/protocols/token-factory/update.mjs +20 -5
- package/esm/protocols/trade/exchange-v2.d.mts +5 -1
- package/esm/protocols/trade/exchange-v2.mjs +89 -4
- package/esm/protocols/trade/transfer-v2.mjs +15 -4
- package/esm/protocols/trade/transfer-v3.mjs +5 -3
- package/esm/stateless-prefix.d.mts +16 -0
- package/esm/stateless-prefix.mjs +82 -0
- package/esm/tx-timer.d.mts +1 -1
- package/esm/tx-timer.mjs +1 -1
- package/esm/util.mjs +2 -2
- package/lib/bridge-release-crypto.cjs +93 -0
- package/lib/bridge-release-crypto.d.cts +39 -0
- package/lib/bridge-release-window.cjs +69 -0
- package/lib/bridge-release-window.d.cts +47 -0
- package/lib/bridge-reserve.cjs +45 -0
- package/lib/bridge-reserve.d.cts +24 -0
- package/lib/execute.cjs +594 -19
- package/lib/execute.d.cts +4 -2
- package/lib/gas-security-guard.cjs +25 -16
- package/lib/gas-security-guard.d.cts +7 -2
- package/lib/index.cjs +10 -1
- package/lib/index.d.cts +37 -18
- package/lib/pipes/ensure-cost.cjs +37 -11
- package/lib/pipes/write-ledger.cjs +6 -3
- package/lib/pipes/write-ledger.d.cts +2 -2
- package/lib/protocols/account/delegate.cjs +6 -4
- package/lib/protocols/account/migrate.cjs +5 -3
- package/lib/protocols/account/revoke-delegate.cjs +5 -3
- package/lib/protocols/asset/acquire-v2.cjs +3 -1
- package/lib/protocols/asset/acquire-v3.cjs +4 -2
- package/lib/protocols/asset/consume.cjs +3 -1
- package/lib/protocols/asset/create.cjs +4 -2
- package/lib/protocols/asset/mint.cjs +4 -2
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +1 -1
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +1 -1
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +1 -1
- package/lib/protocols/asset/update.cjs +4 -2
- package/lib/protocols/bridge/release.cjs +390 -0
- package/lib/protocols/bridge/release.d.cts +34 -0
- package/lib/protocols/factory/create.cjs +4 -2
- package/lib/protocols/governance/claim-stake.cjs +35 -11
- package/lib/protocols/governance/return-stake.cjs +4 -2
- package/lib/protocols/governance/revoke-ledger.cjs +184 -0
- package/lib/protocols/governance/revoke-ledger.d.cts +34 -0
- package/lib/protocols/governance/revoke-stake.cjs +58 -29
- package/lib/protocols/governance/revoke-stake.d.cts +7 -2
- package/lib/protocols/governance/slash-stake.cjs +14 -4
- package/lib/protocols/governance/stake.cjs +6 -4
- package/lib/protocols/rollup/bridge-create.cjs +209 -0
- package/lib/protocols/rollup/bridge-create.d.cts +13 -0
- package/lib/protocols/rollup/bridge-membership.cjs +157 -0
- package/lib/protocols/rollup/bridge-membership.d.cts +10 -0
- package/lib/protocols/rollup/bridge-migrate.cjs +212 -0
- package/lib/protocols/rollup/bridge-migrate.d.cts +10 -0
- package/lib/protocols/rollup/bridge-v2.cjs +244 -0
- package/lib/protocols/rollup/bridge-v2.d.cts +113 -0
- package/lib/protocols/rollup/claim-reward.cjs +4 -2
- package/lib/protocols/rollup/close.cjs +59 -6
- package/lib/protocols/rollup/close.d.cts +2 -1
- package/lib/protocols/rollup/create-block.cjs +4 -2
- package/lib/protocols/rollup/create.cjs +45 -6
- package/lib/protocols/rollup/create.d.cts +3 -0
- package/lib/protocols/rollup/join.cjs +31 -23
- package/lib/protocols/rollup/join.d.cts +2 -1
- package/lib/protocols/rollup/leave.cjs +32 -24
- package/lib/protocols/rollup/leave.d.cts +2 -1
- package/lib/protocols/rollup/migrate.cjs +19 -10
- package/lib/protocols/rollup/migrate.d.cts +2 -1
- package/lib/protocols/rollup/pause.cjs +50 -6
- package/lib/protocols/rollup/pause.d.cts +4 -1
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +2 -2
- package/lib/protocols/rollup/resume.cjs +26 -6
- package/lib/protocols/rollup/resume.d.cts +2 -1
- package/lib/protocols/rollup/update.cjs +56 -9
- package/lib/protocols/rollup/update.d.cts +5 -1
- package/lib/protocols/token/create.cjs +4 -2
- package/lib/protocols/token/deposit-v2.cjs +5 -3
- package/lib/protocols/token/withdraw-v2.cjs +5 -3
- package/lib/protocols/token-factory/burn.cjs +4 -2
- package/lib/protocols/token-factory/create.cjs +8 -4
- package/lib/protocols/token-factory/mint.cjs +4 -2
- package/lib/protocols/token-factory/update.cjs +20 -5
- package/lib/protocols/trade/exchange-v2.cjs +90 -3
- package/lib/protocols/trade/exchange-v2.d.cts +5 -1
- package/lib/protocols/trade/transfer-v2.cjs +15 -4
- package/lib/protocols/trade/transfer-v3.cjs +5 -3
- package/lib/stateless-prefix.cjs +88 -0
- package/lib/stateless-prefix.d.cts +16 -0
- package/lib/tx-timer.cjs +1 -1
- package/lib/tx-timer.d.cts +1 -1
- package/lib/util.cjs +2 -2
- package/package.json +16 -16
- package/tools/fixtures.ts +9 -4
package/esm/tx-timer.d.mts
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* - FAILED txs without persistence stop after the protocol error.
|
|
20
20
|
* - PERSISTED_FAILED txs go through gas-charge runAsLambda and produce a second set.
|
|
21
21
|
*/
|
|
22
|
-
declare const STAGE_LABELS: readonly ["queueWait", "decode", "verify", "lockWait", "
|
|
22
|
+
declare const STAGE_LABELS: readonly ["queueWait", "decode", "verify", "lockWait", "protocolRead", "protocolWrite", "protocolDiff", "ledger", "txstate", "commit", "indexdb"];
|
|
23
23
|
type StageLabel = (typeof STAGE_LABELS)[number];
|
|
24
24
|
declare const COUNTER_NAMES: readonly ["stateReads", "stateWrites", "tokenWrites", "indexEvents"];
|
|
25
25
|
type CounterName = (typeof COUNTER_NAMES)[number];
|
package/esm/tx-timer.mjs
CHANGED
package/esm/util.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { toStakeAddress } from "@arcblock/did-util";
|
|
2
|
-
import { BN, fromTokenToUnit } from "@ocap/util";
|
|
3
1
|
import { CustomError } from "@ocap/util/lib/error";
|
|
4
2
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
3
|
+
import { toStakeAddress } from "@arcblock/did-util";
|
|
4
|
+
import { BN, fromTokenToUnit } from "@ocap/util";
|
|
5
5
|
import cloneDeep from "lodash/cloneDeep.js";
|
|
6
6
|
import { decodeAny } from "@ocap/message";
|
|
7
7
|
import flattenDeep from "lodash/flattenDeep.js";
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
3
|
+
|
|
4
|
+
//#region src/bridge-release-crypto.ts
|
|
5
|
+
/**
|
|
6
|
+
* EIP-712 digests and signature recovery for `release_bridge_token` (#1466, bridge/docs/ocap-protocols.md §3).
|
|
7
|
+
*
|
|
8
|
+
* The structs mirror `@arcblock/bridge-types` (`OcapRelease`, `DepositClaim`; domain `ArcBridge` v1). The chain can
|
|
9
|
+
* not depend on that package (it is private and pulls in viem), so the encoding is re-implemented here and pinned to
|
|
10
|
+
* the shared test vectors (`bridge/types/vectors/eip712.json`) by `tests/bridge-release-crypto.spec.ts`.
|
|
11
|
+
*/
|
|
12
|
+
const DOMAIN_NAME = "ArcBridge";
|
|
13
|
+
const DOMAIN_VERSION = "1";
|
|
14
|
+
const keccak = (data) => Buffer.from(_ocap_mcrypto.Hasher.Keccak.hash256(data, 1, "Uint8Array"));
|
|
15
|
+
const keccakText = (text) => keccak(Buffer.from(text, "utf8"));
|
|
16
|
+
const TYPEHASH_DOMAIN = keccakText("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
|
|
17
|
+
const TYPEHASH_RELEASE = keccakText("OcapRelease(uint8 kind,bytes32 evidence,uint32 ordinal,string ocapChainId,string bridge,string to,uint256 amount,uint64 epoch,address ethSender)");
|
|
18
|
+
const TYPEHASH_CLAIM = keccakText("DepositClaim(bytes32 ethTxHash,string ocapChainId,string recipient,uint256 amount)");
|
|
19
|
+
const UINT256_MAX = (1n << 256n) - 1n;
|
|
20
|
+
const word = (value) => {
|
|
21
|
+
if (value < 0n || value > UINT256_MAX) throw new Error("uint256 out of range");
|
|
22
|
+
return Buffer.from(value.toString(16).padStart(64, "0"), "hex");
|
|
23
|
+
};
|
|
24
|
+
const addressWord = (address) => {
|
|
25
|
+
if (!/^0x[0-9a-fA-F]{40}$/.test(address)) throw new Error("invalid address");
|
|
26
|
+
return Buffer.concat([Buffer.alloc(12), Buffer.from(address.slice(2), "hex")]);
|
|
27
|
+
};
|
|
28
|
+
const bytes32 = (hex) => {
|
|
29
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(hex)) throw new Error("invalid bytes32");
|
|
30
|
+
return Buffer.from(hex.slice(2), "hex");
|
|
31
|
+
};
|
|
32
|
+
const domainSeparator = ({ ethChainId, gateway }) => keccak(Buffer.concat([
|
|
33
|
+
TYPEHASH_DOMAIN,
|
|
34
|
+
keccakText(DOMAIN_NAME),
|
|
35
|
+
keccakText(DOMAIN_VERSION),
|
|
36
|
+
word(BigInt(ethChainId)),
|
|
37
|
+
addressWord(gateway)
|
|
38
|
+
]));
|
|
39
|
+
const toDigest = (domain, structHash) => `0x${keccak(Buffer.concat([
|
|
40
|
+
Buffer.from([25, 1]),
|
|
41
|
+
domainSeparator(domain),
|
|
42
|
+
structHash
|
|
43
|
+
])).toString("hex")}`;
|
|
44
|
+
const ZERO_ETH_ADDRESS = `0x${"0".repeat(40)}`;
|
|
45
|
+
/** EIP-712 digest validators sign for a release. */
|
|
46
|
+
function getOcapReleaseDigest(domain, m) {
|
|
47
|
+
return toDigest(domain, keccak(Buffer.concat([
|
|
48
|
+
TYPEHASH_RELEASE,
|
|
49
|
+
word(BigInt(m.kind)),
|
|
50
|
+
bytes32(m.evidence),
|
|
51
|
+
word(BigInt(m.ordinal)),
|
|
52
|
+
keccakText(m.ocapChainId),
|
|
53
|
+
keccakText(m.bridge),
|
|
54
|
+
keccakText(m.to),
|
|
55
|
+
word(BigInt(m.amount)),
|
|
56
|
+
word(BigInt(m.epoch)),
|
|
57
|
+
addressWord(m.ethSender)
|
|
58
|
+
])));
|
|
59
|
+
}
|
|
60
|
+
/** EIP-712 digest the depositor signs to claim a mode-B deposit. */
|
|
61
|
+
function getDepositClaimDigest(domain, m) {
|
|
62
|
+
return toDigest(domain, keccak(Buffer.concat([
|
|
63
|
+
TYPEHASH_CLAIM,
|
|
64
|
+
bytes32(m.ethTxHash),
|
|
65
|
+
keccakText(m.ocapChainId),
|
|
66
|
+
keccakText(m.recipient),
|
|
67
|
+
word(BigInt(m.amount))
|
|
68
|
+
])));
|
|
69
|
+
}
|
|
70
|
+
const SIGNATURE_PATTERN = /^0x[0-9a-fA-F]{130}$/;
|
|
71
|
+
/**
|
|
72
|
+
* Lowercase signer address of a 65-byte `r || s || v` signature over `digest`, or `null` when it is not a canonical
|
|
73
|
+
* signature: wrong length or encoding, `v` other than 27 / 28, high `s`, or no recoverable key.
|
|
74
|
+
*/
|
|
75
|
+
function recoverEthSigner(digest, signature) {
|
|
76
|
+
if (typeof signature !== "string" || !SIGNATURE_PATTERN.test(signature)) return null;
|
|
77
|
+
const v = Number.parseInt(signature.slice(130, 132), 16);
|
|
78
|
+
if (v !== 27 && v !== 28) return null;
|
|
79
|
+
try {
|
|
80
|
+
return _ocap_mcrypto.Signer.Ethereum.ethRecover(digest, signature).toLowerCase();
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Strict `0x` + 40 hex, any case. */
|
|
86
|
+
const isEthAddress = (value) => typeof value === "string" && /^0x[0-9a-fA-F]{40}$/.test(value);
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
exports.ZERO_ETH_ADDRESS = ZERO_ETH_ADDRESS;
|
|
90
|
+
exports.getDepositClaimDigest = getDepositClaimDigest;
|
|
91
|
+
exports.getOcapReleaseDigest = getOcapReleaseDigest;
|
|
92
|
+
exports.isEthAddress = isEthAddress;
|
|
93
|
+
exports.recoverEthSigner = recoverEthSigner;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//#region src/bridge-release-crypto.d.ts
|
|
2
|
+
interface BridgeDomainInput {
|
|
3
|
+
/** EVM chain id, decimal string */
|
|
4
|
+
ethChainId: string;
|
|
5
|
+
/** BridgeGateway address */
|
|
6
|
+
gateway: string;
|
|
7
|
+
}
|
|
8
|
+
interface OcapReleaseMessage {
|
|
9
|
+
kind: number;
|
|
10
|
+
evidence: string;
|
|
11
|
+
ordinal: number;
|
|
12
|
+
ocapChainId: string;
|
|
13
|
+
/** OCAP bridge (RollupState) address */
|
|
14
|
+
bridge: string;
|
|
15
|
+
to: string;
|
|
16
|
+
amount: string;
|
|
17
|
+
epoch: number;
|
|
18
|
+
ethSender: string;
|
|
19
|
+
}
|
|
20
|
+
declare const ZERO_ETH_ADDRESS: string;
|
|
21
|
+
/** EIP-712 digest validators sign for a release. */
|
|
22
|
+
declare function getOcapReleaseDigest(domain: BridgeDomainInput, m: OcapReleaseMessage): string;
|
|
23
|
+
interface DepositClaimMessage {
|
|
24
|
+
ethTxHash: string;
|
|
25
|
+
ocapChainId: string;
|
|
26
|
+
recipient: string;
|
|
27
|
+
amount: string;
|
|
28
|
+
}
|
|
29
|
+
/** EIP-712 digest the depositor signs to claim a mode-B deposit. */
|
|
30
|
+
declare function getDepositClaimDigest(domain: BridgeDomainInput, m: DepositClaimMessage): string;
|
|
31
|
+
/**
|
|
32
|
+
* Lowercase signer address of a 65-byte `r || s || v` signature over `digest`, or `null` when it is not a canonical
|
|
33
|
+
* signature: wrong length or encoding, `v` other than 27 / 28, high `s`, or no recoverable key.
|
|
34
|
+
*/
|
|
35
|
+
declare function recoverEthSigner(digest: string, signature: string): string | null;
|
|
36
|
+
/** Strict `0x` + 40 hex, any case. */
|
|
37
|
+
declare const isEthAddress: (value: unknown) => value is string;
|
|
38
|
+
//#endregion
|
|
39
|
+
export { BridgeDomainInput, DepositClaimMessage, OcapReleaseMessage, ZERO_ETH_ADDRESS, getDepositClaimDigest, getOcapReleaseDigest, isEthAddress, recoverEthSigner };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/bridge-release-window.ts
|
|
3
|
+
/**
|
|
4
|
+
* Rolling 24 h release window (#1466, bridge/docs/ocap-protocols.md §3 check 7).
|
|
5
|
+
*
|
|
6
|
+
* One ring is 24 hourly slots. Slot `s = hour mod 24` holds `"<hourIndex>:<amountInBaseUnits>"`. The clock is the tx
|
|
7
|
+
* time, never wall time, and it never runs backwards inside a ring: `h = max(hour(txTime), max stored hourIndex)`.
|
|
8
|
+
* Pure functions, no state access, so the arithmetic is testable on its own.
|
|
9
|
+
*/
|
|
10
|
+
const HOUR_MS = 36e5;
|
|
11
|
+
const WINDOW_HOURS = 24;
|
|
12
|
+
/**
|
|
13
|
+
* How far (hours) a stored slot may be ahead of the tx hour before the release is refused. The clock of a ring never
|
|
14
|
+
* moves backwards, so a slot stamped in the far future (a node clock that ran ahead once) would otherwise be adopted
|
|
15
|
+
* for good: every later release would be judged at that hour and the window would stop following real time. Small skews
|
|
16
|
+
* (a few seconds of drift across an hour boundary, two nodes' clocks) are tolerated; beyond this the release fails
|
|
17
|
+
* closed until the tx time catches up.
|
|
18
|
+
*/
|
|
19
|
+
const MAX_RING_SKEW_HOURS = 2;
|
|
20
|
+
const SLOT_DATA_PATTERN = /^(0|[1-9]\d{0,14}):(0|[1-9]\d{0,77})$/;
|
|
21
|
+
/** Parses a stored slot. Anything but the exact canonical form is corruption and throws (the release fails closed). */
|
|
22
|
+
function parseRingSlot(data) {
|
|
23
|
+
if (typeof data !== "string") throw new Error("release window slot is not a string");
|
|
24
|
+
const match = SLOT_DATA_PATTERN.exec(data);
|
|
25
|
+
if (!match) throw new Error(`release window slot is malformed: ${String(data).slice(0, 80)}`);
|
|
26
|
+
return {
|
|
27
|
+
hourIndex: Number(match[1]),
|
|
28
|
+
amount: BigInt(match[2])
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const formatRingSlot = (slot) => `${slot.hourIndex}:${slot.amount.toString(10)}`;
|
|
32
|
+
const hourOf = (timeMs) => Math.floor(timeMs / HOUR_MS);
|
|
33
|
+
/**
|
|
34
|
+
* Evaluates one release against a ring. `slots[i]` is the stored slot `i` (or `null` when it was never written).
|
|
35
|
+
* Does not check `allowed`: the caller rejects when it is false.
|
|
36
|
+
*/
|
|
37
|
+
function evaluateRing(slots, txTimeMs, amount, cap) {
|
|
38
|
+
const txHour = hourOf(txTimeMs);
|
|
39
|
+
let hour = txHour;
|
|
40
|
+
for (const slot of slots) if (slot && slot.hourIndex > hour) hour = slot.hourIndex;
|
|
41
|
+
let used = 0n;
|
|
42
|
+
for (const slot of slots) if (slot && hour - WINDOW_HOURS < slot.hourIndex && slot.hourIndex <= hour) used += slot.amount;
|
|
43
|
+
const index = hour % WINDOW_HOURS;
|
|
44
|
+
const current = slots[index] ?? null;
|
|
45
|
+
const next = current && current.hourIndex === hour ? {
|
|
46
|
+
hourIndex: hour,
|
|
47
|
+
amount: current.amount + amount
|
|
48
|
+
} : {
|
|
49
|
+
hourIndex: hour,
|
|
50
|
+
amount
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
hour,
|
|
54
|
+
slot: index,
|
|
55
|
+
skew: hour - txHour,
|
|
56
|
+
used,
|
|
57
|
+
allowed: used + amount <= cap,
|
|
58
|
+
next
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
exports.HOUR_MS = HOUR_MS;
|
|
64
|
+
exports.MAX_RING_SKEW_HOURS = MAX_RING_SKEW_HOURS;
|
|
65
|
+
exports.WINDOW_HOURS = WINDOW_HOURS;
|
|
66
|
+
exports.evaluateRing = evaluateRing;
|
|
67
|
+
exports.formatRingSlot = formatRingSlot;
|
|
68
|
+
exports.hourOf = hourOf;
|
|
69
|
+
exports.parseRingSlot = parseRingSlot;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/bridge-release-window.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Rolling 24 h release window (#1466, bridge/docs/ocap-protocols.md §3 check 7).
|
|
4
|
+
*
|
|
5
|
+
* One ring is 24 hourly slots. Slot `s = hour mod 24` holds `"<hourIndex>:<amountInBaseUnits>"`. The clock is the tx
|
|
6
|
+
* time, never wall time, and it never runs backwards inside a ring: `h = max(hour(txTime), max stored hourIndex)`.
|
|
7
|
+
* Pure functions, no state access, so the arithmetic is testable on its own.
|
|
8
|
+
*/
|
|
9
|
+
declare const HOUR_MS = 3600000;
|
|
10
|
+
declare const WINDOW_HOURS = 24;
|
|
11
|
+
/**
|
|
12
|
+
* How far (hours) a stored slot may be ahead of the tx hour before the release is refused. The clock of a ring never
|
|
13
|
+
* moves backwards, so a slot stamped in the far future (a node clock that ran ahead once) would otherwise be adopted
|
|
14
|
+
* for good: every later release would be judged at that hour and the window would stop following real time. Small skews
|
|
15
|
+
* (a few seconds of drift across an hour boundary, two nodes' clocks) are tolerated; beyond this the release fails
|
|
16
|
+
* closed until the tx time catches up.
|
|
17
|
+
*/
|
|
18
|
+
declare const MAX_RING_SKEW_HOURS = 2;
|
|
19
|
+
interface RingSlot {
|
|
20
|
+
hourIndex: number;
|
|
21
|
+
amount: bigint;
|
|
22
|
+
}
|
|
23
|
+
/** Parses a stored slot. Anything but the exact canonical form is corruption and throws (the release fails closed). */
|
|
24
|
+
declare function parseRingSlot(data: unknown): RingSlot;
|
|
25
|
+
declare const formatRingSlot: (slot: RingSlot) => string;
|
|
26
|
+
declare const hourOf: (timeMs: number) => number;
|
|
27
|
+
interface RingEvaluation {
|
|
28
|
+
/** the clock after this release: `max(hour(txTime), max stored hourIndex)` */
|
|
29
|
+
hour: number;
|
|
30
|
+
/** slot to write: `hour mod 24` */
|
|
31
|
+
slot: number;
|
|
32
|
+
/** sum of the slots inside the window `hour - 24 < hourIndex <= hour`, before this release */
|
|
33
|
+
used: bigint;
|
|
34
|
+
/** hours the newest stored slot is ahead of the tx hour (0 when it is not) */
|
|
35
|
+
skew: number;
|
|
36
|
+
/** whether `used + amount <= cap` */
|
|
37
|
+
allowed: boolean;
|
|
38
|
+
/** what the slot must hold after this release */
|
|
39
|
+
next: RingSlot;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Evaluates one release against a ring. `slots[i]` is the stored slot `i` (or `null` when it was never written).
|
|
43
|
+
* Does not check `allowed`: the caller rejects when it is false.
|
|
44
|
+
*/
|
|
45
|
+
declare function evaluateRing(slots: ReadonlyArray<RingSlot | null>, txTimeMs: number, amount: bigint, cap: bigint): RingEvaluation;
|
|
46
|
+
//#endregion
|
|
47
|
+
export { HOUR_MS, MAX_RING_SKEW_HOURS, RingEvaluation, RingSlot, WINDOW_HOURS, evaluateRing, formatRingSlot, hourOf, parseRingSlot };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
3
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
4
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
5
|
+
|
|
6
|
+
//#region src/bridge-reserve.ts
|
|
7
|
+
/**
|
|
8
|
+
* Early rejection (R5) of a bridge reserve as an acting party (#1464, #1505, bridge/docs/ocap-protocols.md §1, §v2.3).
|
|
9
|
+
*
|
|
10
|
+
* A reserve is every account with the `ROLE_BRIDGE_RESERVE` DID role (one per bridge, derived, keyless). The role is
|
|
11
|
+
* read from the address itself, so no config and no state lookup is needed, and a crafted role-25 address that maps to
|
|
12
|
+
* no bridge is refused as well. The reserve must never be sender, delegator, multisig signer or gas sponsor.
|
|
13
|
+
* The checks are defence in depth for the paths that spend an account's balance before the post-execution invariant
|
|
14
|
+
* gate is installed (gas, paymaster), and for any future signature scheme.
|
|
15
|
+
* Pure function of the tx: no state, no counters.
|
|
16
|
+
*/
|
|
17
|
+
const BRIDGE_RESERVE_ACTOR_FORBIDDEN = "BRIDGE_RESERVE_ACTOR_FORBIDDEN";
|
|
18
|
+
/** Whether `address` is a bridge reserve address (syntactic: the DID role). */
|
|
19
|
+
const isBridgeReserve = (address) => (0, _ocap_tx_pipeline.isBridgeReserveRole)(address);
|
|
20
|
+
const forbidden = (role) => new _ocap_util_lib_error.CustomError(BRIDGE_RESERVE_ACTOR_FORBIDDEN, `A bridge reserve can not act as ${role}`, { persist: false });
|
|
21
|
+
function assertBridgeReserveNotActor(context) {
|
|
22
|
+
const { tx } = context;
|
|
23
|
+
if (!tx) return;
|
|
24
|
+
if (isBridgeReserve(tx.from)) throw forbidden("transaction sender");
|
|
25
|
+
if (isBridgeReserve(tx.delegator)) throw forbidden("delegator");
|
|
26
|
+
const signatures = (0, _ocap_util_lib_get_list_field.getListField)(tx, "signatures") || [];
|
|
27
|
+
for (const sig of signatures) {
|
|
28
|
+
if (isBridgeReserve(sig?.signer)) throw forbidden("multisig signer");
|
|
29
|
+
if (isBridgeReserve(sig?.delegator)) throw forbidden("multisig delegator");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Paymaster / gas sponsor. The sponsor header is not covered by the tx signature, so the failure strikes nobody. */
|
|
33
|
+
function assertBridgeReserveNotSponsor(sponsorAddress) {
|
|
34
|
+
if (isBridgeReserve(sponsorAddress)) {
|
|
35
|
+
const err = forbidden("gas sponsor");
|
|
36
|
+
err._unauthenticatedExtraFailure = true;
|
|
37
|
+
throw err;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
exports.BRIDGE_RESERVE_ACTOR_FORBIDDEN = BRIDGE_RESERVE_ACTOR_FORBIDDEN;
|
|
43
|
+
exports.assertBridgeReserveNotActor = assertBridgeReserveNotActor;
|
|
44
|
+
exports.assertBridgeReserveNotSponsor = assertBridgeReserveNotSponsor;
|
|
45
|
+
exports.isBridgeReserve = isBridgeReserve;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/bridge-reserve.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Early rejection (R5) of a bridge reserve as an acting party (#1464, #1505, bridge/docs/ocap-protocols.md §1, §v2.3).
|
|
4
|
+
*
|
|
5
|
+
* A reserve is every account with the `ROLE_BRIDGE_RESERVE` DID role (one per bridge, derived, keyless). The role is
|
|
6
|
+
* read from the address itself, so no config and no state lookup is needed, and a crafted role-25 address that maps to
|
|
7
|
+
* no bridge is refused as well. The reserve must never be sender, delegator, multisig signer or gas sponsor.
|
|
8
|
+
* The checks are defence in depth for the paths that spend an account's balance before the post-execution invariant
|
|
9
|
+
* gate is installed (gas, paymaster), and for any future signature scheme.
|
|
10
|
+
* Pure function of the tx: no state, no counters.
|
|
11
|
+
*/
|
|
12
|
+
declare const BRIDGE_RESERVE_ACTOR_FORBIDDEN = "BRIDGE_RESERVE_ACTOR_FORBIDDEN";
|
|
13
|
+
/** Whether `address` is a bridge reserve address (syntactic: the DID role). */
|
|
14
|
+
declare const isBridgeReserve: (address: unknown) => boolean;
|
|
15
|
+
declare function assertBridgeReserveNotActor(context: {
|
|
16
|
+
tx?: {
|
|
17
|
+
from?: string;
|
|
18
|
+
delegator?: string;
|
|
19
|
+
} | null;
|
|
20
|
+
}): void;
|
|
21
|
+
/** Paymaster / gas sponsor. The sponsor header is not covered by the tx signature, so the failure strikes nobody. */
|
|
22
|
+
declare function assertBridgeReserveNotSponsor(sponsorAddress: string): void;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { BRIDGE_RESERVE_ACTOR_FORBIDDEN, assertBridgeReserveNotActor, assertBridgeReserveNotSponsor, isBridgeReserve };
|