@ocap/tx-protocols 1.31.9 → 1.32.1
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/execute.d.mts +6 -1
- package/esm/execute.mjs +151 -9
- package/esm/gas-security-guard.d.mts +19 -1
- package/esm/gas-security-guard.mjs +44 -1
- package/esm/index.d.mts +8 -2
- package/esm/index.mjs +2 -2
- package/esm/pipes/ensure-cost.mjs +39 -2
- package/esm/pipes/write-ledger.mjs +18 -3
- package/esm/protocols/rollup/bridge-create.mjs +10 -4
- package/esm/protocols/rollup/bridge-governance-tx-binding.d.mts +7 -0
- package/esm/protocols/rollup/bridge-governance-tx-binding.mjs +212 -0
- package/esm/protocols/rollup/bridge-governance-vote.d.mts +49 -0
- package/esm/protocols/rollup/bridge-governance-vote.mjs +563 -0
- package/esm/protocols/rollup/bridge-migrate.mjs +9 -9
- package/esm/protocols/rollup/bridge-v2.d.mts +6 -0
- package/esm/protocols/rollup/bridge-v2.mjs +5 -0
- package/esm/protocols/rollup/create.mjs +5 -4
- package/esm/protocols/rollup/token-authority.d.mts +17 -0
- package/esm/protocols/rollup/token-authority.mjs +40 -0
- package/esm/protocols/rollup/update.d.mts +2 -0
- package/esm/protocols/rollup/update.mjs +28 -6
- package/lib/execute.cjs +151 -9
- package/lib/execute.d.cts +6 -1
- package/lib/gas-security-guard.cjs +44 -0
- package/lib/gas-security-guard.d.cts +19 -1
- package/lib/index.cjs +1 -0
- package/lib/index.d.cts +8 -2
- package/lib/pipes/ensure-cost.cjs +39 -2
- package/lib/pipes/write-ledger.cjs +18 -3
- package/lib/protocols/rollup/bridge-create.cjs +10 -4
- package/lib/protocols/rollup/bridge-governance-tx-binding.cjs +213 -0
- package/lib/protocols/rollup/bridge-governance-tx-binding.d.cts +7 -0
- package/lib/protocols/rollup/bridge-governance-vote.cjs +572 -0
- package/lib/protocols/rollup/bridge-governance-vote.d.cts +49 -0
- package/lib/protocols/rollup/bridge-migrate.cjs +9 -9
- package/lib/protocols/rollup/bridge-v2.cjs +5 -0
- package/lib/protocols/rollup/bridge-v2.d.cts +6 -0
- package/lib/protocols/rollup/create.cjs +5 -4
- package/lib/protocols/rollup/token-authority.cjs +42 -0
- package/lib/protocols/rollup/token-authority.d.cts +17 -0
- package/lib/protocols/rollup/update.cjs +28 -6
- package/lib/protocols/rollup/update.d.cts +2 -0
- package/package.json +16 -16
|
@@ -32,12 +32,18 @@ function isMatchingAccountPublicKey(onChainPk, headerPk) {
|
|
|
32
32
|
const { statedb, txType, senderState, gasEstimate, totalGas } = context;
|
|
33
33
|
const { config } = context;
|
|
34
34
|
const { tx, extra = {}, txHash = "", txTime } = context;
|
|
35
|
+
const verifiedLedgerExtraCtx = context;
|
|
35
36
|
const isFailedTxGasCharge = Boolean(context.isFailedTxGasCharge);
|
|
36
37
|
const canChargeSenderDirectly = Boolean(senderState);
|
|
38
|
+
verifiedLedgerExtraCtx._ensureTxCostEvaluated = true;
|
|
39
|
+
if (!isFailedTxGasCharge || !canChargeSenderDirectly) {
|
|
40
|
+
verifiedLedgerExtraCtx._verifiedGasPass = void 0;
|
|
41
|
+
verifiedLedgerExtraCtx._verifiedGasStakeHeaders = void 0;
|
|
42
|
+
}
|
|
37
43
|
const txSize = context._baseTxSize ?? context.txSize;
|
|
38
44
|
const nowMs = txTime ? +new Date(txTime) : Date.now();
|
|
39
45
|
const { token, pk } = extra.gasStakeHeaders || {};
|
|
40
|
-
const rawGasPass = extra.gasPass
|
|
46
|
+
const rawGasPass = extra.gasPass;
|
|
41
47
|
const gasStake = {};
|
|
42
48
|
let isThirdPartySponsor = false;
|
|
43
49
|
let enforceSponsor3DEligibility = false;
|
|
@@ -110,6 +116,15 @@ function isMatchingAccountPublicKey(onChainPk, headerPk) {
|
|
|
110
116
|
gasStake.owner = passOwner;
|
|
111
117
|
gasStake.stakeId = passResult.stakeId;
|
|
112
118
|
gasStake.state = passStakeState;
|
|
119
|
+
verifiedLedgerExtraCtx._verifiedGasPass = {
|
|
120
|
+
stakeId: passResult.stakeId,
|
|
121
|
+
epoch: Number(rawGasPass.epoch),
|
|
122
|
+
nullifier: String(rawGasPass.nullifier).trim(),
|
|
123
|
+
commitment: String(rawGasPass.commitment).trim().toLowerCase(),
|
|
124
|
+
...typeof rawGasPass.passSecret === "string" && rawGasPass.passSecret ? { passSecret: rawGasPass.passSecret } : {},
|
|
125
|
+
...typeof rawGasPass.pk === "string" && rawGasPass.pk ? { pk: rawGasPass.pk } : {},
|
|
126
|
+
...typeof rawGasPass.signature === "string" && rawGasPass.signature ? { signature: rawGasPass.signature } : {}
|
|
127
|
+
};
|
|
113
128
|
}
|
|
114
129
|
} else if ((!isFailedTxGasCharge || !canChargeSenderDirectly) && token && pk) {
|
|
115
130
|
const decoded = _arcblock_jwt.decode(token) || {};
|
|
@@ -219,6 +234,10 @@ function isMatchingAccountPublicKey(onChainPk, headerPk) {
|
|
|
219
234
|
gasStake.owner = sponsorAddress;
|
|
220
235
|
gasStake.stakeId = sponsorStakeId;
|
|
221
236
|
if (sponsorStakeState) gasStake.state = sponsorStakeState;
|
|
237
|
+
if (sponsorIsBacked && typeof token === "string" && token.length > 0 && Buffer.byteLength(token, "utf8") <= 4 * 1024 && typeof pk === "string" && pk.length > 0 && Buffer.byteLength(pk, "utf8") <= 512) verifiedLedgerExtraCtx._verifiedGasStakeHeaders = {
|
|
238
|
+
token,
|
|
239
|
+
pk
|
|
240
|
+
};
|
|
222
241
|
}
|
|
223
242
|
}
|
|
224
243
|
} else console.warn(`Invalid gas payer header for: ${txHash}`);
|
|
@@ -345,7 +364,25 @@ function isMatchingAccountPublicKey(onChainPk, headerPk) {
|
|
|
345
364
|
};
|
|
346
365
|
}
|
|
347
366
|
const nextSponsorState = await statedb.account.update(latestSponsor.address, updatedSponsorPayload, context);
|
|
348
|
-
if (nextSponsorState && typeof nextSponsorState === "object")
|
|
367
|
+
if (nextSponsorState && typeof nextSponsorState === "object") {
|
|
368
|
+
const prevSponsorTick = latestSponsor?._retrievedAt;
|
|
369
|
+
const curSponsorTick = nextSponsorState._retrievedAt;
|
|
370
|
+
if (!(typeof curSponsorTick === "bigint" && (typeof prevSponsorTick !== "bigint" || curSponsorTick > prevSponsorTick))) {
|
|
371
|
+
let nextTick;
|
|
372
|
+
if (typeof prevSponsorTick === "bigint" || typeof curSponsorTick === "bigint") nextTick = [prevSponsorTick, curSponsorTick].filter((v) => typeof v === "bigint").reduce((acc, v) => v > acc ? v : acc, 0n) + 1n;
|
|
373
|
+
else {
|
|
374
|
+
const prevNum = typeof prevSponsorTick === "number" ? prevSponsorTick : 0;
|
|
375
|
+
const curNum = typeof curSponsorTick === "number" ? curSponsorTick : 0;
|
|
376
|
+
nextTick = Math.max(prevNum, curNum) + 1;
|
|
377
|
+
}
|
|
378
|
+
Object.defineProperty(nextSponsorState, "_retrievedAt", {
|
|
379
|
+
value: nextTick,
|
|
380
|
+
enumerable: false,
|
|
381
|
+
writable: true,
|
|
382
|
+
configurable: true
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
349
386
|
context.sponsorState = nextSponsorState;
|
|
350
387
|
}
|
|
351
388
|
let feeVaultState = null;
|
|
@@ -64,11 +64,26 @@ const WriteLedger = async (context, next) => {
|
|
|
64
64
|
return next();
|
|
65
65
|
}
|
|
66
66
|
try {
|
|
67
|
+
const verifiedCtx = context;
|
|
68
|
+
const evaluatedByPipeline = verifiedCtx._ensureTxCostEvaluated === true;
|
|
69
|
+
let canonicalGasHeaders;
|
|
70
|
+
if (evaluatedByPipeline) canonicalGasHeaders = verifiedCtx._verifiedGasStakeHeaders;
|
|
71
|
+
else if (extra?.gasStakeHeaders && typeof extra.gasStakeHeaders === "object" && !Array.isArray(extra.gasStakeHeaders)) {
|
|
72
|
+
const hdr = extra.gasStakeHeaders;
|
|
73
|
+
if (typeof hdr.token === "string" && hdr.token.length > 0 && typeof hdr.pk === "string" && hdr.pk.length > 0) canonicalGasHeaders = {
|
|
74
|
+
token: hdr.token,
|
|
75
|
+
pk: hdr.pk
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const canonicalCoSig = evaluatedByPipeline ? verifiedCtx._verifiedCoSignature : extra?.coSignature && typeof extra.coSignature === "object" && !Array.isArray(extra.coSignature) ? extra.coSignature : void 0;
|
|
79
|
+
const canonicalGasPass = evaluatedByPipeline ? verifiedCtx._verifiedGasPass : extra?.gasPass && typeof extra.gasPass === "object" && !Array.isArray(extra.gasPass) ? extra.gasPass : void 0;
|
|
67
80
|
let txExtra;
|
|
68
|
-
if (extra?.txExtra ||
|
|
81
|
+
if (extra?.txExtra || canonicalGasHeaders || canonicalCoSig || canonicalGasPass) {
|
|
69
82
|
txExtra = {};
|
|
70
|
-
if (extra
|
|
71
|
-
if (
|
|
83
|
+
if (extra?.txExtra) txExtra.txExtra = extra.txExtra;
|
|
84
|
+
if (canonicalGasHeaders) txExtra.gasStakeHeaders = canonicalGasHeaders;
|
|
85
|
+
if (canonicalCoSig) txExtra.coSignature = canonicalCoSig;
|
|
86
|
+
if (canonicalGasPass) txExtra.gasPass = canonicalGasPass;
|
|
72
87
|
}
|
|
73
88
|
const formattedTx = (0, _ocap_util_lib_deposit_v2_bridge_fields.stripDefaultDepositV2BridgeFieldsInTx)((0, _ocap_message.formatMessage)("Transaction", tx));
|
|
74
89
|
const txInput = {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
2
|
const require_protocols_rollup_bridge_v2 = require('./bridge-v2.cjs');
|
|
3
|
+
const require_protocols_rollup_token_authority = require('./token-authority.cjs');
|
|
3
4
|
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
4
5
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
5
6
|
let _arcblock_did = require("@arcblock/did");
|
|
@@ -12,6 +13,7 @@ let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
|
12
13
|
/**
|
|
13
14
|
* `create_rollup` for a v2 bridge (bridge/docs/ocap-protocols.md §v2.2, row `create_rollup`).
|
|
14
15
|
*/
|
|
16
|
+
const ZERO_ETH_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
15
17
|
const isHttpUrl = (s) => {
|
|
16
18
|
try {
|
|
17
19
|
return typeof s === "string" && ["http:", "https:"].includes(new URL(s).protocol);
|
|
@@ -28,17 +30,21 @@ async function verifyBridgeCreate(context, next) {
|
|
|
28
30
|
const { tx, itx, config, tokenState, statedb } = context;
|
|
29
31
|
const input = context.bridgeInput;
|
|
30
32
|
const txSec = require_protocols_rollup_bridge_v2.txSecOf(context);
|
|
31
|
-
const authority =
|
|
33
|
+
const authority = await require_protocols_rollup_token_authority.resolveTokenAuthority(context, itx.tokenAddress, tokenState);
|
|
32
34
|
const owner = [tx.from, tx.delegator].filter(Boolean).find((x) => (0, _ocap_util.isSameDid)(x, authority));
|
|
33
35
|
if (!owner) return next(new _ocap_util_lib_error.CustomError("FORBIDDEN", "Only the token owner can create a bridge for the token"));
|
|
34
36
|
if (!(0, _arcblock_did.isEthereumDid)(owner)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "The bridge owner must use an ethereum compatible address"));
|
|
35
37
|
const seeds = context.seedValidators;
|
|
36
38
|
if (!seeds || seeds.length !== 1 || !_ocap_state.rollup.sameAddress(seeds[0], owner)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "seed_validators of a v2 bridge must be the bridge owner alone"));
|
|
37
|
-
if (!_ocap_state.rollup.isValidRollupContractAddress(itx.contractAddress, "EVM")) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "contractAddress must be an ethereum token address"));
|
|
38
|
-
if (itx.vaultAddress
|
|
39
|
+
if (typeof itx.contractAddress !== "string" || itx.contractAddress !== itx.contractAddress.trim() || !_ocap_state.rollup.isValidRollupContractAddress(itx.contractAddress, "EVM")) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "contractAddress must be an ethereum token address"));
|
|
40
|
+
if (!itx.vaultAddress || typeof itx.vaultAddress !== "string" || itx.vaultAddress !== itx.vaultAddress.trim() || !_ocap_state.rollup.isValidRollupVaultAddress(itx.vaultAddress, "EVM")) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "vaultAddress must be an ethereum address"));
|
|
39
41
|
if (!(itx.minBlockConfirmation >= 1 && itx.minBlockConfirmation <= 100)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "minBlockConfirmation must be between 1 and 100"));
|
|
40
|
-
const ethToken = String(itx.contractAddress).toLowerCase();
|
|
41
42
|
const { body } = input;
|
|
43
|
+
const ethToken = _ocap_state.rollup.bareEthAddress(itx.contractAddress);
|
|
44
|
+
const ethVault = _ocap_state.rollup.bareEthAddress(itx.vaultAddress);
|
|
45
|
+
const ethGateway = _ocap_state.rollup.bareEthAddress(body.gateway);
|
|
46
|
+
if (ethToken === ZERO_ETH_ADDRESS || ethToken === ethGateway) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "contractAddress must be a non-zero ethereum token address distinct from gateway"));
|
|
47
|
+
if (ethVault === ZERO_ETH_ADDRESS || ethVault === ethToken || ethVault === ethGateway) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "vaultAddress must be a non-zero ethereum address distinct from contractAddress and gateway"));
|
|
42
48
|
const epoch1 = body.epochs[0];
|
|
43
49
|
const consents = input.consents;
|
|
44
50
|
if (consents.length !== epoch1.validators.length) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "Every initial validator must sign a BridgeMembership consent"));
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_protocols_rollup_bridge_v2 = require('./bridge-v2.cjs');
|
|
3
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
4
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
5
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
6
|
+
let _arcblock_did = require("@arcblock/did");
|
|
7
|
+
let _ocap_state = require("@ocap/state");
|
|
8
|
+
let _ocap_util = require("@ocap/util");
|
|
9
|
+
let _ocap_message = require("@ocap/message");
|
|
10
|
+
let _ocap_message_cbor = require("@ocap/message/cbor");
|
|
11
|
+
let _ocap_message_protobuf = require("@ocap/message/protobuf");
|
|
12
|
+
|
|
13
|
+
//#region src/protocols/rollup/bridge-governance-tx-binding.ts
|
|
14
|
+
/**
|
|
15
|
+
* On-chain `unsignedTxBytesHex` + `frozenNonce` binding for OCAP `ArcBlockTransaction` governance proposals (#1880).
|
|
16
|
+
*
|
|
17
|
+
* Ensures every `ArcBlockTransaction` co-signed on-chain via `govProposalVote` commits to a canonical unsigned OCAP
|
|
18
|
+
* `Transaction` whose `chainId`, `typeUrl`, `from`, `pk`, `nonce`, `signatures` roster, and decoded `itx` fields
|
|
19
|
+
* match `proposal.action`, and whose `frozenNonce` has not yet been spent on `txFrom`'s on-chain account.
|
|
20
|
+
*/
|
|
21
|
+
function invalidTx(msg) {
|
|
22
|
+
throw new _ocap_util_lib_error.CustomError("INVALID_TX", msg);
|
|
23
|
+
}
|
|
24
|
+
function hasCborSelfDescribeTag(buf) {
|
|
25
|
+
return buf.length >= _ocap_message_cbor.CANONICAL_SELF_DESCRIBE_PREFIX.length && buf[0] === _ocap_message_cbor.CANONICAL_SELF_DESCRIBE_PREFIX[0] && buf[1] === _ocap_message_cbor.CANONICAL_SELF_DESCRIBE_PREFIX[1] && buf[2] === _ocap_message_cbor.CANONICAL_SELF_DESCRIBE_PREFIX[2];
|
|
26
|
+
}
|
|
27
|
+
function normalizeDecodedItx(raw) {
|
|
28
|
+
if (!raw || typeof raw !== "object") invalidTx("unsignedTxBytesHex contains no decodable itx");
|
|
29
|
+
if (typeof raw.typeUrl === "string" && raw.typeUrl) {
|
|
30
|
+
if (typeof raw.value === "string") {
|
|
31
|
+
const decoded = (0, _ocap_message.decodeAny)(raw);
|
|
32
|
+
return {
|
|
33
|
+
typeUrl: raw.typeUrl,
|
|
34
|
+
value: decoded?.value ?? {}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const inner = {};
|
|
38
|
+
for (const [key, val] of Object.entries(raw)) {
|
|
39
|
+
if (key === "typeUrl" || key === "type_url") continue;
|
|
40
|
+
inner[key] = val;
|
|
41
|
+
}
|
|
42
|
+
const msg = (0, _ocap_message.createMessage)((0, _ocap_message.fromTypeUrl)(raw.typeUrl), inner);
|
|
43
|
+
return {
|
|
44
|
+
typeUrl: raw.typeUrl,
|
|
45
|
+
value: msg?.toObject ? msg.toObject() : inner
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (typeof raw.type === "string" && raw.type) {
|
|
49
|
+
const typeUrl = (0, _ocap_message.toTypeUrl)(raw.type);
|
|
50
|
+
const typeName = (0, _ocap_message.fromTypeUrl)(typeUrl);
|
|
51
|
+
const inner = raw.value || {};
|
|
52
|
+
const msg = (0, _ocap_message.createMessage)(typeName, inner);
|
|
53
|
+
return {
|
|
54
|
+
typeUrl,
|
|
55
|
+
value: msg?.toObject ? msg.toObject() : inner
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
invalidTx("unsignedTxBytesHex itx is missing typeUrl");
|
|
59
|
+
}
|
|
60
|
+
function isEmptyBytesOrStr(v) {
|
|
61
|
+
if (v === void 0 || v === null || v === "") return true;
|
|
62
|
+
if (v instanceof Uint8Array) return v.length === 0;
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
function assertUpdatePatchMatches(decodedPatch, expected) {
|
|
66
|
+
const expectedKeys = [];
|
|
67
|
+
for (const k of [
|
|
68
|
+
"minDeposit",
|
|
69
|
+
"minWithdraw",
|
|
70
|
+
"maxWithdraw",
|
|
71
|
+
"releaseDailyCap",
|
|
72
|
+
"refundDailyCap"
|
|
73
|
+
]) if (expected[k] !== void 0) {
|
|
74
|
+
expectedKeys.push(k);
|
|
75
|
+
if (decodedPatch[k] !== expected[k].toString(10)) invalidTx(`unsignedTxBytesHex update_rollup patch.${k} does not match proposal`);
|
|
76
|
+
}
|
|
77
|
+
for (const k of ["releaseGrace", "refundDelay"]) if (expected[k] !== void 0) {
|
|
78
|
+
expectedKeys.push(k);
|
|
79
|
+
if (decodedPatch[k] !== expected[k]) invalidTx(`unsignedTxBytesHex update_rollup patch.${k} does not match proposal`);
|
|
80
|
+
}
|
|
81
|
+
if (expected.pausers !== void 0) {
|
|
82
|
+
expectedKeys.push("pausers");
|
|
83
|
+
if (!Array.isArray(decodedPatch.pausers) || decodedPatch.pausers.length !== expected.pausers.length || decodedPatch.pausers.some((p, i) => {
|
|
84
|
+
try {
|
|
85
|
+
return (0, _ocap_mcrypto.checkedNonZeroAddress)(p, `patch.pausers[${i}]`) !== expected.pausers[i];
|
|
86
|
+
} catch {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
})) invalidTx("unsignedTxBytesHex update_rollup patch.pausers does not match proposal");
|
|
90
|
+
}
|
|
91
|
+
if (expected.cancelPendingEpoch === true) {
|
|
92
|
+
expectedKeys.push("cancelPendingEpoch");
|
|
93
|
+
if (decodedPatch.cancelPendingEpoch !== true) invalidTx("unsignedTxBytesHex update_rollup patch.cancelPendingEpoch does not match proposal");
|
|
94
|
+
}
|
|
95
|
+
if (expected.cancelOwnerProposal === true) {
|
|
96
|
+
expectedKeys.push("cancelOwnerProposal");
|
|
97
|
+
if (decodedPatch.cancelOwnerProposal !== true) invalidTx("unsignedTxBytesHex update_rollup patch.cancelOwnerProposal does not match proposal");
|
|
98
|
+
}
|
|
99
|
+
if (expected.proposeOwner !== void 0) {
|
|
100
|
+
expectedKeys.push("proposeOwner");
|
|
101
|
+
const po = decodedPatch.proposeOwner;
|
|
102
|
+
if (!po || typeof po !== "object" || Array.isArray(po)) invalidTx("unsignedTxBytesHex update_rollup patch.proposeOwner must be an object");
|
|
103
|
+
const poKeys = Object.keys(po);
|
|
104
|
+
if (poKeys.length !== 3 || !poKeys.every((k) => k === "address" || k === "pk" || k === "endpoint")) invalidTx("unsignedTxBytesHex update_rollup patch.proposeOwner has unexpected keys");
|
|
105
|
+
let normAddr = "";
|
|
106
|
+
let normPk = "";
|
|
107
|
+
try {
|
|
108
|
+
normAddr = (0, _ocap_mcrypto.checkedNonZeroAddress)(po.address, "proposeOwner.address");
|
|
109
|
+
normPk = (0, _ocap_mcrypto.normalizeUncompressedSecp256k1Pk)((0, _arcblock_did.normalizePublicKeyHex)(po.pk) || String(po.pk ?? ""));
|
|
110
|
+
} catch (err) {
|
|
111
|
+
invalidTx(err.message);
|
|
112
|
+
}
|
|
113
|
+
if (normAddr !== expected.proposeOwner.newOwner || normPk !== expected.proposeOwner.newOwnerPk || po.endpoint !== expected.proposeOwner.endpoint) invalidTx("unsignedTxBytesHex update_rollup patch.proposeOwner does not match proposal");
|
|
114
|
+
}
|
|
115
|
+
if (expected.evmGovernance !== void 0) {
|
|
116
|
+
expectedKeys.push("evmGovernance");
|
|
117
|
+
if ((0, _ocap_mcrypto.canonicalJson)(_ocap_state.rollup.validateStrictEvmGovernance(decodedPatch.evmGovernance, "INVALID_ROLLUP_UPDATE")) !== (0, _ocap_mcrypto.canonicalJson)(expected.evmGovernance)) invalidTx("unsignedTxBytesHex update_rollup patch.evmGovernance does not match proposal");
|
|
118
|
+
}
|
|
119
|
+
const actualKeys = Object.keys(decodedPatch).filter((k) => decodedPatch[k] !== void 0);
|
|
120
|
+
if (actualKeys.length !== expectedKeys.length || !actualKeys.every((k) => expectedKeys.includes(k))) invalidTx("unsignedTxBytesHex update_rollup patch keys do not match proposal.action.patch");
|
|
121
|
+
}
|
|
122
|
+
async function verifyOcapProposalTxBinding(context, proposal) {
|
|
123
|
+
const { action } = proposal;
|
|
124
|
+
if (action.kind === "join_rollup" || action.kind === "leave_rollup") return;
|
|
125
|
+
if (!action.txFrom || action.frozenNonce === void 0 || !action.unsignedTxBytesHex || !action.challenge) invalidTx(`${action.kind} proposal requires txFrom, frozenNonce, unsignedTxBytesHex, and challenge on-chain`);
|
|
126
|
+
if (action.frozenNonce < 1n) invalidTx(`${action.kind}.frozenNonce must be a positive integer`);
|
|
127
|
+
const originBytes = new Uint8Array((0, _ocap_util.hexToBytes)(action.unsignedTxBytesHex.slice(2)));
|
|
128
|
+
if ((0, _ocap_mcrypto.toHexDigest)(_ocap_mcrypto.Hasher.Keccak.hash256(originBytes, 1, "Uint8Array")) !== action.challenge) invalidTx("action.challenge does not match keccak256(action.unsignedTxBytesHex)");
|
|
129
|
+
const isCbor = hasCborSelfDescribeTag(originBytes);
|
|
130
|
+
let decodedTx;
|
|
131
|
+
try {
|
|
132
|
+
decodedTx = isCbor ? (0, _ocap_message_cbor.decodeTx)(originBytes) : (0, _ocap_message_protobuf.decodeTx)(originBytes);
|
|
133
|
+
const reencoded = isCbor ? (0, _ocap_message_cbor.encodeTx)(decodedTx) : (0, _ocap_message_protobuf.encodeTx)(decodedTx);
|
|
134
|
+
if (reencoded.length !== originBytes.length || !reencoded.every((b, i) => b === originBytes[i])) invalidTx("unsignedTxBytesHex is not canonically encoded");
|
|
135
|
+
const problem = (0, _ocap_message.findFormatProblem)("Transaction", decodedTx);
|
|
136
|
+
if (problem && (!isCbor || problem.kind === "invalid-timestamp")) invalidTx("unsignedTxBytesHex failed Transaction format check");
|
|
137
|
+
} catch (err) {
|
|
138
|
+
const e = err;
|
|
139
|
+
if (e?.code === "INVALID_TX") throw err;
|
|
140
|
+
invalidTx(`Failed to decode unsignedTxBytesHex: ${e?.message || String(err)}`);
|
|
141
|
+
}
|
|
142
|
+
if (!isEmptyBytesOrStr(decodedTx.signature)) invalidTx("unsignedTxBytesHex must be an unsigned Transaction (tx.signature must be empty)");
|
|
143
|
+
if (decodedTx.delegator) invalidTx("unsignedTxBytesHex must not set tx.delegator");
|
|
144
|
+
if (decodedTx.serviceFee && decodedTx.serviceFee !== "0") invalidTx("unsignedTxBytesHex must not set non-zero tx.serviceFee");
|
|
145
|
+
if (decodedTx.chainId !== proposal.ocapChainId) invalidTx("unsignedTxBytesHex tx.chainId does not match proposal.ocapChainId");
|
|
146
|
+
if (decodedTx.from !== action.txFrom) invalidTx("unsignedTxBytesHex tx.from does not match proposal.action.txFrom");
|
|
147
|
+
let txNonce = -1n;
|
|
148
|
+
try {
|
|
149
|
+
txNonce = BigInt(String(decodedTx.nonce ?? ""));
|
|
150
|
+
} catch {
|
|
151
|
+
invalidTx("unsignedTxBytesHex tx.nonce is not a valid integer");
|
|
152
|
+
}
|
|
153
|
+
if (txNonce !== action.frozenNonce) invalidTx("unsignedTxBytesHex tx.nonce does not match proposal.action.frozenNonce");
|
|
154
|
+
const sigList = (0, _ocap_util_lib_get_list_field.getListField)(decodedTx, "signatures");
|
|
155
|
+
for (const s of sigList) if (!isEmptyBytesOrStr(s.signature) || !isEmptyBytesOrStr(s.extra) || !isEmptyBytesOrStr(s.data) || s.delegator) invalidTx("unsignedTxBytesHex tx.signatures entries must omit signature, extra, data, and delegator");
|
|
156
|
+
let fromPk = "";
|
|
157
|
+
try {
|
|
158
|
+
fromPk = (0, _ocap_mcrypto.normalizeUncompressedSecp256k1Pk)((0, _arcblock_did.normalizePublicKeyHex)(decodedTx.pk) || String(decodedTx.pk ?? ""));
|
|
159
|
+
} catch {
|
|
160
|
+
invalidTx("unsignedTxBytesHex tx.pk is not a valid uncompressed secp256k1 public key");
|
|
161
|
+
}
|
|
162
|
+
if (!_ocap_state.rollup.sameAddress((0, _ocap_mcrypto.ethAddressFromUncompressedPk)(fromPk), action.txFrom)) invalidTx("unsignedTxBytesHex tx.pk does not derive to proposal.action.txFrom");
|
|
163
|
+
if ("signerRoster" in action) {
|
|
164
|
+
if (sigList.length !== action.signerRoster.length - 1) invalidTx("unsignedTxBytesHex tx.signatures length does not match proposal signerRoster");
|
|
165
|
+
const rosterByAddr = new Map(action.signerRoster.map((e) => [e.signer, e.pk]));
|
|
166
|
+
if (rosterByAddr.get(action.txFrom) !== fromPk) invalidTx("unsignedTxBytesHex tx.pk does not match txFrom entry in signerRoster");
|
|
167
|
+
const seenSigs = new Set([action.txFrom]);
|
|
168
|
+
for (const s of sigList) {
|
|
169
|
+
let sAddr = "";
|
|
170
|
+
let sPk = "";
|
|
171
|
+
try {
|
|
172
|
+
sAddr = (0, _ocap_mcrypto.checkedNonZeroAddress)(s.signer, "tx.signatures.signer");
|
|
173
|
+
sPk = (0, _ocap_mcrypto.normalizeUncompressedSecp256k1Pk)((0, _arcblock_did.normalizePublicKeyHex)(s.pk) || String(s.pk ?? ""));
|
|
174
|
+
} catch (err) {
|
|
175
|
+
invalidTx(err.message);
|
|
176
|
+
}
|
|
177
|
+
if (s.signer !== sAddr) invalidTx("unsignedTxBytesHex tx.signatures.signer must be EIP-55 checksummed");
|
|
178
|
+
if (seenSigs.has(sAddr) || rosterByAddr.get(sAddr) !== sPk) invalidTx(`unsignedTxBytesHex tx.signatures entry ${sAddr} does not match signerRoster`);
|
|
179
|
+
seenSigs.add(sAddr);
|
|
180
|
+
}
|
|
181
|
+
} else if (sigList.length !== 0) invalidTx("Single-signer OCAP proposal unsignedTxBytesHex must have empty tx.signatures");
|
|
182
|
+
const normItx = normalizeDecodedItx(decodedTx.itx);
|
|
183
|
+
if (normItx.typeUrl !== `fg:t:${action.kind}`) invalidTx(`unsignedTxBytesHex itx.typeUrl (${normItx.typeUrl}) does not match fg:t:${action.kind}`);
|
|
184
|
+
const itx = normItx.value;
|
|
185
|
+
if (itx.rollup !== proposal.ocapBridge) invalidTx("unsignedTxBytesHex itx.rollup does not match proposal.ocapBridge");
|
|
186
|
+
_ocap_state.rollup.assertV1FieldsUnused(itx, "INVALID_ROLLUP_UPDATE");
|
|
187
|
+
if (action.kind === "pause_rollup" || action.kind === "resume_rollup") {
|
|
188
|
+
if (itx.data !== void 0 && itx.data !== null) {
|
|
189
|
+
const d = require_protocols_rollup_bridge_v2.readItxData(itx);
|
|
190
|
+
if (Object.keys(d).some((k) => k !== "reason") || d.reason !== void 0 && d.reason !== action.reason) invalidTx(`unsignedTxBytesHex ${action.kind} itx.data does not match proposal`);
|
|
191
|
+
}
|
|
192
|
+
} else if (action.kind === "close_rollup") {
|
|
193
|
+
if (typeof itx.message !== "string" || itx.message.trim() !== action.reason || itx.token) invalidTx("unsignedTxBytesHex close_rollup message/token does not match proposal");
|
|
194
|
+
const d = require_protocols_rollup_bridge_v2.readItxData(itx);
|
|
195
|
+
if (!Object.keys(d).every((k) => k === "depositCutoffEthBlock" || k === "reason") || String(d.depositCutoffEthBlock ?? "") !== action.depositCutoffEthBlock.toString(10) || d.reason !== void 0 && d.reason !== action.reason) invalidTx("unsignedTxBytesHex close_rollup depositCutoffEthBlock does not match proposal");
|
|
196
|
+
} else if (action.kind === "migrate_rollup") {
|
|
197
|
+
if (itx.type !== action.migrateKind || typeof itx.message !== "string" || itx.message.trim() !== action.reason) invalidTx("unsignedTxBytesHex migrate_rollup type/message does not match proposal");
|
|
198
|
+
if (action.migrateKind === "vault") {
|
|
199
|
+
if (!_ocap_state.rollup.sameAddress(itx.to, action.nextVault) || itx.data !== void 0 && itx.data !== null) invalidTx("unsignedTxBytesHex migrate_rollup(vault) target/data does not match proposal");
|
|
200
|
+
} else {
|
|
201
|
+
if (itx.to !== action.targetBridge) invalidTx("unsignedTxBytesHex migrate_rollup(reserve) to does not match proposal.action.targetBridge");
|
|
202
|
+
const d = require_protocols_rollup_bridge_v2.readItxData(itx);
|
|
203
|
+
if (Object.keys(d).some((k) => k !== "action") || d.action !== action.step) invalidTx("unsignedTxBytesHex migrate_rollup(reserve) step does not match proposal");
|
|
204
|
+
}
|
|
205
|
+
} else if (action.kind === "update_rollup") assertUpdatePatchMatches(require_protocols_rollup_bridge_v2.readItxData(itx), action.patch);
|
|
206
|
+
const fromAcct = await context.statedb.account.get(action.txFrom, context);
|
|
207
|
+
if (!fromAcct) invalidTx(`Proposal txFrom account ${action.txFrom} does not exist on-chain`);
|
|
208
|
+
const currentAcctNonce = BigInt(String(fromAcct.nonce ?? 0));
|
|
209
|
+
if (currentAcctNonce >= action.frozenNonce) invalidTx(`Proposal frozenNonce (${action.frozenNonce.toString(10)}) is already spent on ${action.txFrom} (account.nonce=${currentAcctNonce.toString(10)})`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
//#endregion
|
|
213
|
+
exports.verifyOcapProposalTxBinding = verifyOcapProposalTxBinding;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { OcapGovernanceProposal } from "@ocap/mcrypto";
|
|
2
|
+
|
|
3
|
+
//#region src/protocols/rollup/bridge-governance-tx-binding.d.ts
|
|
4
|
+
type Ctx = any;
|
|
5
|
+
declare function verifyOcapProposalTxBinding(context: Ctx, proposal: OcapGovernanceProposal): Promise<void>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { verifyOcapProposalTxBinding };
|