@ocap/mcrypto 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.
Files changed (49) hide show
  1. package/esm/bridge-governance/evm-abi.d.mts +179 -0
  2. package/esm/bridge-governance/evm-abi.mjs +344 -0
  3. package/esm/bridge-governance/evm-proposals.d.mts +39 -0
  4. package/esm/bridge-governance/evm-proposals.mjs +345 -0
  5. package/esm/bridge-governance/evm-sync.d.mts +45 -0
  6. package/esm/bridge-governance/evm-sync.mjs +120 -0
  7. package/esm/bridge-governance/index.d.mts +8 -0
  8. package/esm/bridge-governance/index.mjs +9 -0
  9. package/esm/bridge-governance/keys.d.mts +60 -0
  10. package/esm/bridge-governance/keys.mjs +224 -0
  11. package/esm/bridge-governance/membership.d.mts +116 -0
  12. package/esm/bridge-governance/membership.mjs +175 -0
  13. package/esm/bridge-governance/ocap-proposals.d.mts +132 -0
  14. package/esm/bridge-governance/ocap-proposals.mjs +481 -0
  15. package/esm/bridge-governance/strict-json.d.mts +8 -0
  16. package/esm/bridge-governance/strict-json.mjs +102 -0
  17. package/esm/bridge-governance.d.mts +9 -116
  18. package/esm/bridge-governance.mjs +8 -174
  19. package/esm/envelope.mjs +28 -1
  20. package/esm/index.d.mts +10 -2
  21. package/esm/index.mjs +8 -2
  22. package/esm/signer/ethereum.mjs +2 -2
  23. package/esm/signer/secp256k1.d.mts +5 -0
  24. package/esm/signer/secp256k1.mjs +18 -0
  25. package/lib/_vendor/@noble/hashes/utils.cjs +19 -1
  26. package/lib/bridge-governance/evm-abi.cjs +362 -0
  27. package/lib/bridge-governance/evm-abi.d.cts +179 -0
  28. package/lib/bridge-governance/evm-proposals.cjs +353 -0
  29. package/lib/bridge-governance/evm-proposals.d.cts +39 -0
  30. package/lib/bridge-governance/evm-sync.cjs +130 -0
  31. package/lib/bridge-governance/evm-sync.d.cts +45 -0
  32. package/lib/bridge-governance/index.cjs +129 -0
  33. package/lib/bridge-governance/index.d.cts +8 -0
  34. package/lib/bridge-governance/keys.cjs +273 -0
  35. package/lib/bridge-governance/keys.d.cts +60 -0
  36. package/lib/bridge-governance/membership.cjs +195 -0
  37. package/lib/bridge-governance/membership.d.cts +116 -0
  38. package/lib/bridge-governance/ocap-proposals.cjs +491 -0
  39. package/lib/bridge-governance/ocap-proposals.d.cts +132 -0
  40. package/lib/bridge-governance/strict-json.cjs +103 -0
  41. package/lib/bridge-governance/strict-json.d.cts +8 -0
  42. package/lib/bridge-governance.cjs +128 -194
  43. package/lib/bridge-governance.d.cts +8 -116
  44. package/lib/envelope.cjs +28 -1
  45. package/lib/index.cjs +128 -21
  46. package/lib/index.d.cts +9 -2
  47. package/lib/signer/secp256k1.cjs +18 -0
  48. package/lib/signer/secp256k1.d.cts +5 -0
  49. package/package.json +2 -2
@@ -0,0 +1,481 @@
1
+ import { BRIDGE_MEMBERSHIP_ACTION, computeBridgeEndpointsHash, computeBridgeMembershipDigest, recoverBridgeSigner } from "./membership.mjs";
2
+ import { MAX_OCAP_DELAY_SEC, MAX_OCAP_PAUSERS, MAX_OCAP_VALIDATORS, MAX_PROPOSAL_JSON_BYTES, MAX_PROPOSAL_SUMMARY_BYTES, MAX_SAFE_BIGINT, MAX_U256, MAX_U64, MIN_OCAP_VALIDATORS, OCAP_PROPOSAL_HASH_PREFIX, assertAllowedKeys, assertBareBridgeDid, assertBigIntRange, assertBoundedPlainText, assertHttpUrl, assertPlainObject, assertSafeChainId, assertSafeUint, assertTrimmedSingleLine, assertWellFormedString, canonicalJson, checkedBytes32, checkedNonEmptyHex, checkedNonZeroAddress, ethAddressFromUncompressedPk, normalizeUncompressedSecp256k1Pk, parseDecimalBigInt, toJsonSafeValue, utf8ByteLength } from "./keys.mjs";
3
+ import { parseStrictJson } from "./strict-json.mjs";
4
+ import { normalizeEcdsaSignature } from "./evm-abi.mjs";
5
+ import { validateEvmGovernanceRosterConfig } from "./evm-sync.mjs";
6
+ import { keccak_256 } from "@noble/hashes/sha3.js";
7
+ import { bytesToHex, concatBytes, hexToBytes, utf8ToBytes } from "@noble/hashes/utils.js";
8
+
9
+ //#region src/bridge-governance/ocap-proposals.ts
10
+ const BARE_DID_RE = /^z[1-9A-HJ-NP-Za-km-z]{20,60}$/;
11
+ const LOCKSTEP_KEYS = [
12
+ "txFrom",
13
+ "frozenNonce",
14
+ "signerRoster",
15
+ "unsignedTxBytesHex",
16
+ "challenge"
17
+ ];
18
+ const SINGLE_META_KEYS = [
19
+ "txFrom",
20
+ "frozenNonce",
21
+ "unsignedTxBytesHex",
22
+ "challenge"
23
+ ];
24
+ const SIGNER_ROSTER_ENTRY_KEYS = new Set(["signer", "pk"]);
25
+ const OCAP_PROPOSAL_KEYS = new Set([
26
+ "version",
27
+ "plane",
28
+ "ocapChainId",
29
+ "ocapBridge",
30
+ "createdAt",
31
+ "expiresAt",
32
+ "summary",
33
+ "action"
34
+ ]);
35
+ const OCAP_PROPOSE_OWNER_KEYS = new Set([
36
+ "newOwner",
37
+ "newOwnerPk",
38
+ "endpoint"
39
+ ]);
40
+ const OCAP_PATCH_BIGINT_KEYS = [
41
+ "minDeposit",
42
+ "minWithdraw",
43
+ "maxWithdraw",
44
+ "releaseDailyCap",
45
+ "refundDailyCap"
46
+ ];
47
+ const OCAP_PATCH_ALLOWED_KEYS = new Set([
48
+ ...OCAP_PATCH_BIGINT_KEYS,
49
+ "releaseGrace",
50
+ "refundDelay",
51
+ "pausers",
52
+ "cancelPendingEpoch",
53
+ "proposeOwner",
54
+ "cancelOwnerProposal",
55
+ "evmGovernance"
56
+ ]);
57
+ const OCAP_ACTION_ALLOWED_KEYS = {
58
+ join_rollup: new Set([
59
+ "kind",
60
+ "validator",
61
+ "endpoint",
62
+ "epoch",
63
+ "validators",
64
+ "endpoints",
65
+ "threshold",
66
+ "activeFrom",
67
+ "voucherValidity"
68
+ ]),
69
+ leave_rollup: new Set([
70
+ "kind",
71
+ "validator",
72
+ "epoch",
73
+ "validators",
74
+ "endpoints",
75
+ "threshold",
76
+ "activeFrom",
77
+ "voucherValidity"
78
+ ]),
79
+ pause_rollup: new Set([
80
+ "kind",
81
+ "mode",
82
+ "reason",
83
+ ...LOCKSTEP_KEYS
84
+ ]),
85
+ resume_rollup: new Set([
86
+ "kind",
87
+ "reason",
88
+ ...LOCKSTEP_KEYS
89
+ ]),
90
+ close_rollup: new Set([
91
+ "kind",
92
+ "depositCutoffEthBlock",
93
+ "reason",
94
+ ...LOCKSTEP_KEYS
95
+ ]),
96
+ update_rollup: new Set([
97
+ "kind",
98
+ "patch",
99
+ ...LOCKSTEP_KEYS
100
+ ]),
101
+ migrate_rollup: new Set([
102
+ "kind",
103
+ "migrateKind",
104
+ "nextVault",
105
+ "step",
106
+ "targetBridge",
107
+ "reason",
108
+ ...LOCKSTEP_KEYS
109
+ ])
110
+ };
111
+ const OCAP_PAUSE_LONE_KEYS = new Set([
112
+ "kind",
113
+ "mode",
114
+ "reason",
115
+ ...SINGLE_META_KEYS
116
+ ]);
117
+ const OCAP_PAUSE_QUORUM_KEYS = new Set([
118
+ "kind",
119
+ "mode",
120
+ "reason",
121
+ ...LOCKSTEP_KEYS
122
+ ]);
123
+ const OCAP_MIGRATE_VAULT_KEYS = new Set([
124
+ "kind",
125
+ "migrateKind",
126
+ "nextVault",
127
+ "reason",
128
+ ...LOCKSTEP_KEYS
129
+ ]);
130
+ const OCAP_MIGRATE_RESERVE_QUORUM_KEYS = new Set([
131
+ "kind",
132
+ "migrateKind",
133
+ "step",
134
+ "targetBridge",
135
+ "reason",
136
+ ...LOCKSTEP_KEYS
137
+ ]);
138
+ const OCAP_MIGRATE_RESERVE_CANCEL_KEYS = new Set([
139
+ "kind",
140
+ "migrateKind",
141
+ "step",
142
+ "targetBridge",
143
+ "reason",
144
+ ...SINGLE_META_KEYS
145
+ ]);
146
+ function normalizeBareActorAddress(val, name) {
147
+ const raw = assertTrimmedSingleLine(val, name, 128, true);
148
+ if (/^did:abt:/i.test(raw)) throw new Error(`${name} must not carry a did:abt: prefix`);
149
+ if (/^0x[0-9a-fA-F]{40}$/.test(raw)) return checkedNonZeroAddress(raw, name);
150
+ if (BARE_DID_RE.test(raw)) return raw;
151
+ throw new Error(`${name} must be a non-zero Ethereum address or bare base58btc DID`);
152
+ }
153
+ function normalizeSignerRoster(val, name) {
154
+ if (!Array.isArray(val) || val.length === 0 || val.length > MAX_OCAP_VALIDATORS + 1) throw new Error(`${name} must contain 1..${MAX_OCAP_VALIDATORS + 1} signer entries`);
155
+ const seen = /* @__PURE__ */ new Set();
156
+ return val.map((item, i) => {
157
+ const rec = assertPlainObject(item, `${name}[${i}]`);
158
+ assertAllowedKeys(rec, SIGNER_ROSTER_ENTRY_KEYS, `${name}[${i}]`);
159
+ const signer = checkedNonZeroAddress(rec.signer, `${name}[${i}].signer`);
160
+ const pk = normalizeUncompressedSecp256k1Pk(assertWellFormedString(rec.pk, `${name}[${i}].pk`));
161
+ if (ethAddressFromUncompressedPk(pk) !== signer) throw new Error(`${name}[${i}].pk does not match Ethereum signer ${signer}`);
162
+ const dedup = signer.toLowerCase();
163
+ if (seen.has(dedup)) throw new Error(`duplicate signer in ${name}`);
164
+ seen.add(dedup);
165
+ return {
166
+ signer,
167
+ pk
168
+ };
169
+ });
170
+ }
171
+ function normalizeLockstep(rec, prefix, parseDec) {
172
+ const numFn = parseDec ? parseDecimalBigInt : assertBigIntRange;
173
+ const txFrom = checkedNonZeroAddress(rec.txFrom, `${prefix}.txFrom`);
174
+ const frozenNonce = numFn(rec.frozenNonce, `${prefix}.frozenNonce`, 0n, MAX_U256);
175
+ const signerRoster = normalizeSignerRoster(rec.signerRoster, `${prefix}.signerRoster`);
176
+ if (!signerRoster.some((e) => e.signer === txFrom)) throw new Error(`${prefix}.txFrom (${txFrom}) must be included in ${prefix}.signerRoster`);
177
+ const out = {
178
+ txFrom,
179
+ frozenNonce,
180
+ signerRoster
181
+ };
182
+ if (rec.unsignedTxBytesHex !== void 0) out.unsignedTxBytesHex = checkedNonEmptyHex(rec.unsignedTxBytesHex, `${prefix}.unsignedTxBytesHex`, 16384);
183
+ if (rec.challenge !== void 0) out.challenge = checkedBytes32(rec.challenge, `${prefix}.challenge`);
184
+ if (out.unsignedTxBytesHex !== void 0 && out.challenge !== void 0) {
185
+ if (`0x${bytesToHex(keccak_256(hexToBytes(out.unsignedTxBytesHex.slice(2))))}` !== out.challenge) throw new Error(`${prefix}.challenge does not match keccak256(${prefix}.unsignedTxBytesHex)`);
186
+ }
187
+ return out;
188
+ }
189
+ function normalizeSingleMeta(rec, prefix, parseDec) {
190
+ const numFn = parseDec ? parseDecimalBigInt : assertBigIntRange;
191
+ const out = {};
192
+ if (rec.txFrom !== void 0) out.txFrom = normalizeBareActorAddress(rec.txFrom, `${prefix}.txFrom`);
193
+ if (rec.frozenNonce !== void 0) out.frozenNonce = numFn(rec.frozenNonce, `${prefix}.frozenNonce`, 0n, MAX_U256);
194
+ if (rec.unsignedTxBytesHex !== void 0) out.unsignedTxBytesHex = checkedNonEmptyHex(rec.unsignedTxBytesHex, `${prefix}.unsignedTxBytesHex`, 16384);
195
+ if (rec.challenge !== void 0) out.challenge = checkedBytes32(rec.challenge, `${prefix}.challenge`);
196
+ if (out.unsignedTxBytesHex !== void 0 && out.challenge !== void 0 && out.txFrom !== void 0 && out.txFrom.startsWith("0x")) {
197
+ if (`0x${bytesToHex(keccak_256(hexToBytes(out.unsignedTxBytesHex.slice(2))))}` !== out.challenge) throw new Error(`${prefix}.challenge does not match keccak256(${prefix}.unsignedTxBytesHex)`);
198
+ }
199
+ return out;
200
+ }
201
+ function normalizeEpochFields(kind, validator, rec, parseDec, joinEndpoint) {
202
+ const numFn = parseDec ? parseDecimalBigInt : assertBigIntRange;
203
+ const epoch = numFn(rec.epoch, `${kind}.epoch`, 2n, MAX_SAFE_BIGINT);
204
+ const activeFrom = numFn(rec.activeFrom, `${kind}.activeFrom`, 1n, MAX_SAFE_BIGINT);
205
+ const voucherValidity = numFn(rec.voucherValidity, `${kind}.voucherValidity`, 1n, BigInt(MAX_OCAP_DELAY_SEC));
206
+ if (!Array.isArray(rec.validators) || rec.validators.length < MIN_OCAP_VALIDATORS || rec.validators.length > MAX_OCAP_VALIDATORS) throw new Error(`${kind}.validators must contain ${MIN_OCAP_VALIDATORS}..${MAX_OCAP_VALIDATORS} addresses`);
207
+ const validators = rec.validators.map((v, i) => checkedNonZeroAddress(v, `${kind}.validators[${i}]`));
208
+ for (let i = 1; i < validators.length; i++) if (BigInt(validators[i]) <= BigInt(validators[i - 1])) throw new Error(`${kind}.validators must be strictly ascending without duplicates`);
209
+ if (!Array.isArray(rec.endpoints) || rec.endpoints.length !== validators.length) throw new Error(`${kind}.endpoints must align 1:1 with validators`);
210
+ const endpoints = rec.endpoints.map((e, i) => assertHttpUrl(e, `${kind}.endpoints[${i}]`));
211
+ const idx = validators.indexOf(validator);
212
+ if (kind === "join_rollup") {
213
+ if (idx === -1) throw new Error("join_rollup.validators must include join_rollup.validator");
214
+ if (endpoints[idx] !== joinEndpoint) throw new Error("join_rollup.endpoints mismatch at validator index");
215
+ } else if (idx !== -1) throw new Error("leave_rollup.validators must not include leave_rollup.validator");
216
+ const threshold = assertSafeUint(rec.threshold, `${kind}.threshold`, 1, validators.length);
217
+ if (2 * threshold <= validators.length) throw new Error(`${kind}.threshold must represent a strict majority`);
218
+ return {
219
+ epoch,
220
+ validators,
221
+ endpoints,
222
+ threshold,
223
+ activeFrom,
224
+ voucherValidity
225
+ };
226
+ }
227
+ function normalizeOcapPatch(rawPatch, parseDec) {
228
+ const rec = assertPlainObject(rawPatch, "update_rollup.patch");
229
+ assertAllowedKeys(rec, OCAP_PATCH_ALLOWED_KEYS, "update_rollup.patch");
230
+ const numFn = parseDec ? parseDecimalBigInt : assertBigIntRange;
231
+ const out = {};
232
+ for (const k of OCAP_PATCH_BIGINT_KEYS) if (rec[k] !== void 0) out[k] = numFn(rec[k], `patch.${k}`, 1n, MAX_U256);
233
+ if (out.minWithdraw !== void 0 && out.maxWithdraw !== void 0 && out.maxWithdraw < out.minWithdraw) throw new Error("patch.maxWithdraw must be >= patch.minWithdraw");
234
+ if (rec.releaseGrace !== void 0) out.releaseGrace = assertSafeUint(rec.releaseGrace, "patch.releaseGrace", 0, MAX_OCAP_DELAY_SEC);
235
+ if (rec.refundDelay !== void 0) out.refundDelay = assertSafeUint(rec.refundDelay, "patch.refundDelay", 0, MAX_OCAP_DELAY_SEC);
236
+ if (rec.pausers !== void 0) {
237
+ if (!Array.isArray(rec.pausers) || rec.pausers.length > MAX_OCAP_PAUSERS) throw new Error(`patch.pausers must be an array of at most ${MAX_OCAP_PAUSERS} addresses`);
238
+ const seen = /* @__PURE__ */ new Set();
239
+ out.pausers = rec.pausers.map((p, i) => {
240
+ const addr = normalizeBareActorAddress(p, `patch.pausers[${i}]`);
241
+ const lower = addr.toLowerCase();
242
+ if (seen.has(lower)) throw new Error("duplicate signer in patch.pausers");
243
+ seen.add(lower);
244
+ return addr;
245
+ });
246
+ }
247
+ if (rec.cancelPendingEpoch !== void 0) {
248
+ if (rec.cancelPendingEpoch !== true) throw new Error("patch.cancelPendingEpoch must be true");
249
+ out.cancelPendingEpoch = true;
250
+ }
251
+ if (rec.proposeOwner !== void 0) {
252
+ const poRec = assertPlainObject(rec.proposeOwner, "patch.proposeOwner");
253
+ assertAllowedKeys(poRec, OCAP_PROPOSE_OWNER_KEYS, "patch.proposeOwner");
254
+ const newOwner = checkedNonZeroAddress(poRec.newOwner, "patch.proposeOwner.newOwner");
255
+ const newOwnerPk = normalizeUncompressedSecp256k1Pk(assertWellFormedString(poRec.newOwnerPk, "patch.proposeOwner.newOwnerPk"));
256
+ if (ethAddressFromUncompressedPk(newOwnerPk) !== newOwner) throw new Error("patch.proposeOwner.newOwnerPk does not match patch.proposeOwner.newOwner");
257
+ out.proposeOwner = {
258
+ newOwner,
259
+ newOwnerPk,
260
+ endpoint: assertHttpUrl(poRec.endpoint, "patch.proposeOwner.endpoint")
261
+ };
262
+ }
263
+ if (rec.cancelOwnerProposal !== void 0) {
264
+ if (rec.cancelOwnerProposal !== true) throw new Error("patch.cancelOwnerProposal must be true");
265
+ out.cancelOwnerProposal = true;
266
+ }
267
+ if (rec.evmGovernance !== void 0) out.evmGovernance = validateEvmGovernanceRosterConfig(rec.evmGovernance);
268
+ const keys = Object.keys(out);
269
+ if (keys.length === 0) throw new Error("update_rollup.patch must specify at least one field");
270
+ if ((out.proposeOwner !== void 0 || out.cancelOwnerProposal !== void 0) && keys.length !== 1) throw new Error("proposeOwner and cancelOwnerProposal cannot be combined with other patch keys");
271
+ return out;
272
+ }
273
+ function normalizeOcapAction(raw, parseDec) {
274
+ const rec = assertPlainObject(raw, "action");
275
+ if (typeof rec.kind !== "string" || !Object.prototype.hasOwnProperty.call(OCAP_ACTION_ALLOWED_KEYS, rec.kind)) throw new Error(`unknown OCAP governed action ${String(rec.kind)}`);
276
+ const kind = rec.kind;
277
+ assertAllowedKeys(rec, OCAP_ACTION_ALLOWED_KEYS[kind], `action(${kind})`);
278
+ const numFn = parseDec ? parseDecimalBigInt : assertBigIntRange;
279
+ switch (kind) {
280
+ case "join_rollup": {
281
+ const validator = checkedNonZeroAddress(rec.validator, "join_rollup.validator");
282
+ const endpoint = assertHttpUrl(rec.endpoint, "join_rollup.endpoint");
283
+ return {
284
+ kind,
285
+ validator,
286
+ endpoint,
287
+ ...normalizeEpochFields(kind, validator, rec, parseDec, endpoint)
288
+ };
289
+ }
290
+ case "leave_rollup": {
291
+ const validator = checkedNonZeroAddress(rec.validator, "leave_rollup.validator");
292
+ return {
293
+ kind,
294
+ validator,
295
+ ...normalizeEpochFields(kind, validator, rec, parseDec)
296
+ };
297
+ }
298
+ case "pause_rollup": {
299
+ const reason = assertBoundedPlainText(rec.reason, "pause_rollup.reason", MAX_PROPOSAL_SUMMARY_BYTES);
300
+ if (rec.mode === "lone") {
301
+ assertAllowedKeys(rec, OCAP_PAUSE_LONE_KEYS, "action(pause_rollup:lone)");
302
+ return {
303
+ kind,
304
+ mode: "lone",
305
+ reason,
306
+ ...normalizeSingleMeta(rec, "pause_rollup", parseDec)
307
+ };
308
+ }
309
+ if (rec.mode === "quorum") {
310
+ assertAllowedKeys(rec, OCAP_PAUSE_QUORUM_KEYS, "action(pause_rollup:quorum)");
311
+ return {
312
+ kind,
313
+ mode: "quorum",
314
+ reason,
315
+ ...normalizeLockstep(rec, "pause_rollup", parseDec)
316
+ };
317
+ }
318
+ throw new Error("pause_rollup.mode must be 'lone' or 'quorum'");
319
+ }
320
+ case "resume_rollup": return {
321
+ kind,
322
+ reason: assertBoundedPlainText(rec.reason, "resume_rollup.reason", MAX_PROPOSAL_SUMMARY_BYTES),
323
+ ...normalizeLockstep(rec, "resume_rollup", parseDec)
324
+ };
325
+ case "close_rollup": return {
326
+ kind,
327
+ depositCutoffEthBlock: numFn(rec.depositCutoffEthBlock, "close_rollup.depositCutoffEthBlock", 0n, MAX_U256),
328
+ reason: assertBoundedPlainText(rec.reason, "close_rollup.reason", MAX_PROPOSAL_SUMMARY_BYTES),
329
+ ...normalizeLockstep(rec, "close_rollup", parseDec)
330
+ };
331
+ case "update_rollup": {
332
+ const patch = normalizeOcapPatch(rec.patch, parseDec);
333
+ const lockstep = normalizeLockstep(rec, "update_rollup", parseDec);
334
+ if (patch.cancelOwnerProposal === true && lockstep.signerRoster.length !== 1) throw new Error("update_rollup(cancelOwnerProposal) requires a single-owner signerRoster");
335
+ return {
336
+ kind,
337
+ patch,
338
+ ...lockstep
339
+ };
340
+ }
341
+ case "migrate_rollup": {
342
+ const reason = assertBoundedPlainText(rec.reason, "migrate_rollup.reason", MAX_PROPOSAL_SUMMARY_BYTES);
343
+ if (rec.migrateKind === "vault") {
344
+ assertAllowedKeys(rec, OCAP_MIGRATE_VAULT_KEYS, "action(migrate_rollup:vault)");
345
+ return {
346
+ kind,
347
+ migrateKind: "vault",
348
+ nextVault: checkedNonZeroAddress(rec.nextVault, "migrate_rollup.nextVault"),
349
+ reason,
350
+ ...normalizeLockstep(rec, "migrate_rollup", parseDec)
351
+ };
352
+ }
353
+ if (rec.migrateKind === "reserve") {
354
+ const targetBridge = assertBareBridgeDid(rec.targetBridge, "migrate_rollup.targetBridge");
355
+ if (rec.step === "cancel") {
356
+ assertAllowedKeys(rec, OCAP_MIGRATE_RESERVE_CANCEL_KEYS, "action(migrate_rollup:reserve:cancel)");
357
+ return {
358
+ kind,
359
+ migrateKind: "reserve",
360
+ step: "cancel",
361
+ targetBridge,
362
+ reason,
363
+ ...normalizeSingleMeta(rec, "migrate_rollup", parseDec)
364
+ };
365
+ }
366
+ if (rec.step === "announce" || rec.step === "execute") {
367
+ assertAllowedKeys(rec, OCAP_MIGRATE_RESERVE_QUORUM_KEYS, `action(migrate_rollup:reserve:${rec.step})`);
368
+ return {
369
+ kind,
370
+ migrateKind: "reserve",
371
+ step: rec.step,
372
+ targetBridge,
373
+ reason,
374
+ ...normalizeLockstep(rec, "migrate_rollup", parseDec)
375
+ };
376
+ }
377
+ throw new Error("migrate_rollup.step must be 'announce', 'execute', or 'cancel'");
378
+ }
379
+ throw new Error("migrate_rollup.migrateKind must be 'vault' or 'reserve'");
380
+ }
381
+ }
382
+ }
383
+ function validateOcapProposalInternal(raw, parseDec) {
384
+ const rec = assertPlainObject(raw, "OcapGovernanceProposal");
385
+ assertAllowedKeys(rec, OCAP_PROPOSAL_KEYS, "OcapGovernanceProposal");
386
+ if (rec.version !== 1) throw new Error("OcapGovernanceProposal.version must be 1");
387
+ if (rec.plane !== "ocap") throw new Error("OcapGovernanceProposal.plane must be 'ocap'");
388
+ const numFn = parseDec ? parseDecimalBigInt : assertBigIntRange;
389
+ const ocapChainId = assertSafeChainId(rec.ocapChainId, "ocapChainId");
390
+ const ocapBridge = assertBareBridgeDid(rec.ocapBridge, "ocapBridge");
391
+ const createdAt = numFn(rec.createdAt, "createdAt", 0n, MAX_U64);
392
+ const expiresAt = numFn(rec.expiresAt, "expiresAt", 1n, MAX_U64);
393
+ if (expiresAt <= createdAt) throw new Error("expiresAt must be strictly greater than createdAt");
394
+ const summary = assertBoundedPlainText(rec.summary, "summary", MAX_PROPOSAL_SUMMARY_BYTES);
395
+ const action = normalizeOcapAction(rec.action, parseDec);
396
+ if (action.kind === "migrate_rollup" && action.migrateKind === "reserve" && action.targetBridge === ocapBridge) throw new Error("migrate_rollup.targetBridge cannot equal source ocapBridge");
397
+ return {
398
+ version: 1,
399
+ plane: "ocap",
400
+ ocapChainId,
401
+ ocapBridge,
402
+ createdAt,
403
+ expiresAt,
404
+ summary,
405
+ action
406
+ };
407
+ }
408
+ function validateOcapProposal(proposal) {
409
+ return validateOcapProposalInternal(proposal, false);
410
+ }
411
+ function coerceOcapProposal(raw) {
412
+ return validateOcapProposalInternal(raw, true);
413
+ }
414
+ function serializeOcapProposal(proposal) {
415
+ const json = canonicalJson(toJsonSafeValue(validateOcapProposal(proposal)));
416
+ if (utf8ByteLength(json) > MAX_PROPOSAL_JSON_BYTES) throw new Error(`serialized OcapGovernanceProposal exceeds ${MAX_PROPOSAL_JSON_BYTES} bytes`);
417
+ return json;
418
+ }
419
+ function parseOcapProposal(json) {
420
+ const s = assertWellFormedString(json, "proposal JSON");
421
+ if (utf8ByteLength(s) > MAX_PROPOSAL_JSON_BYTES) throw new Error(`proposal JSON exceeds ${MAX_PROPOSAL_JSON_BYTES} bytes`);
422
+ return validateOcapProposalInternal(parseStrictJson(s), true);
423
+ }
424
+ function computeOcapProposalId(proposal) {
425
+ const canonical = serializeOcapProposal(proposal);
426
+ return `0x${bytesToHex(keccak_256(concatBytes(utf8ToBytes(OCAP_PROPOSAL_HASH_PREFIX), utf8ToBytes(canonical))))}`;
427
+ }
428
+ function membershipFromOcapProposal(proposal) {
429
+ const canonical = validateOcapProposal(proposal);
430
+ const { action } = canonical;
431
+ if (action.kind !== "join_rollup" && action.kind !== "leave_rollup") throw new Error(`membershipFromOcapProposal requires join_rollup or leave_rollup, got ${action.kind}`);
432
+ return {
433
+ ocapChainId: canonical.ocapChainId,
434
+ bridge: canonical.ocapBridge,
435
+ epoch: action.epoch,
436
+ validators: action.validators,
437
+ threshold: action.threshold,
438
+ activeFrom: action.activeFrom,
439
+ voucherValidity: action.voucherValidity,
440
+ endpointsHash: computeBridgeEndpointsHash(action.endpoints),
441
+ action: action.kind === "join_rollup" ? BRIDGE_MEMBERSHIP_ACTION.JOIN : BRIDGE_MEMBERSHIP_ACTION.LEAVE
442
+ };
443
+ }
444
+ function recoverOcapMembershipProposalSigner(proposal, sig) {
445
+ const signature = normalizeEcdsaSignature(sig);
446
+ const recovered = recoverBridgeSigner(computeBridgeMembershipDigest(membershipFromOcapProposal(proposal)), signature);
447
+ if (!recovered) throw new Error("failed to recover BridgeMembership signer");
448
+ return {
449
+ signer: checkedNonZeroAddress(recovered.address, "recovered signer"),
450
+ signature
451
+ };
452
+ }
453
+ const ARCBLOCK_TX_DOMAIN_SEP = keccak_256(concatBytes(keccak_256(utf8ToBytes("EIP712Domain(string name,string version)")), keccak_256(utf8ToBytes("ArcBlock")), keccak_256(utf8ToBytes("1"))));
454
+ const ARCBLOCK_TX_TYPE_HASH = keccak_256(utf8ToBytes("ArcBlockTransaction(string chainId,string action,string from,string nonce,string challenge)"));
455
+ function computeArcBlockTxEip712Digest(fields) {
456
+ const challenge = checkedBytes32(fields.challenge, "challenge");
457
+ const structHash = keccak_256(concatBytes(ARCBLOCK_TX_TYPE_HASH, keccak_256(utf8ToBytes(fields.chainId)), keccak_256(utf8ToBytes(fields.action)), keccak_256(utf8ToBytes(fields.from)), keccak_256(utf8ToBytes(fields.nonce)), keccak_256(utf8ToBytes(challenge))));
458
+ return keccak_256(concatBytes(new Uint8Array([25, 1]), ARCBLOCK_TX_DOMAIN_SEP, structHash));
459
+ }
460
+ function formatArcBlockTxEip191Text(fields) {
461
+ const challenge = checkedBytes32(fields.challenge, "challenge");
462
+ return `ArcBlock Transaction v1\nChain: ${fields.chainId}\nAction: ${fields.action}\nSigner: ${fields.from}\nNonce: ${fields.nonce}\nChallenge: ${challenge}`;
463
+ }
464
+ function computeArcBlockTxEip191Digest(fields) {
465
+ const body = utf8ToBytes(formatArcBlockTxEip191Text(fields));
466
+ return keccak_256(concatBytes(utf8ToBytes(`\x19Ethereum Signed Message:\n${body.byteLength}`), body));
467
+ }
468
+ function recoverArcBlockTxEnvelopeSigner(fields, sig, envelope = "eip712") {
469
+ const signature = normalizeEcdsaSignature(sig);
470
+ const recovered = recoverBridgeSigner(envelope === "eip191" ? computeArcBlockTxEip191Digest(fields) : computeArcBlockTxEip712Digest(fields), signature);
471
+ if (!recovered) throw new Error("failed to recover ArcBlockTransaction signer");
472
+ const publicKeyHex = `0x${bytesToHex(recovered.publicKey.subarray(1))}`;
473
+ return {
474
+ signer: checkedNonZeroAddress(recovered.address, "recovered signer"),
475
+ publicKeyHex,
476
+ signature
477
+ };
478
+ }
479
+
480
+ //#endregion
481
+ export { coerceOcapProposal, computeArcBlockTxEip191Digest, computeArcBlockTxEip712Digest, computeOcapProposalId, formatArcBlockTxEip191Text, membershipFromOcapProposal, parseOcapProposal, recoverArcBlockTxEnvelopeSigner, recoverOcapMembershipProposalSigner, serializeOcapProposal, validateOcapProposal };
@@ -0,0 +1,8 @@
1
+ //#region src/bridge-governance/strict-json.d.ts
2
+ /**
3
+ * Strict JSON parser that rejects duplicate object keys at any nesting depth before delegating to `JSON.parse`.
4
+ * Prevents canonical-JSON digest/payload divergence caused by ECMA-262 last-key-wins silent overwrites.
5
+ */
6
+ declare function parseStrictJson(raw: string): unknown;
7
+ //#endregion
8
+ export { parseStrictJson };
@@ -0,0 +1,102 @@
1
+ //#region src/bridge-governance/strict-json.ts
2
+ /**
3
+ * Strict JSON parser that rejects duplicate object keys at any nesting depth before delegating to `JSON.parse`.
4
+ * Prevents canonical-JSON digest/payload divergence caused by ECMA-262 last-key-wins silent overwrites.
5
+ */
6
+ function parseStrictJson(raw) {
7
+ if (typeof raw !== "string") throw new SyntaxError("JSON input must be a string");
8
+ let i = 0;
9
+ const n = raw.length;
10
+ const skipWs = () => {
11
+ while (i < n) {
12
+ const c = raw.charCodeAt(i);
13
+ if (c === 32 || c === 9 || c === 10 || c === 13) i++;
14
+ else break;
15
+ }
16
+ };
17
+ const readString = () => {
18
+ const start = i;
19
+ i++;
20
+ while (i < n) {
21
+ const c = raw.charCodeAt(i);
22
+ if (c === 34) {
23
+ i++;
24
+ return JSON.parse(raw.slice(start, i));
25
+ }
26
+ if (c === 92) i += 2;
27
+ else i++;
28
+ }
29
+ throw new SyntaxError("Unterminated JSON string");
30
+ };
31
+ const scanValue = (depth) => {
32
+ if (depth > 64) throw new SyntaxError("JSON nesting depth exceeds limit");
33
+ skipWs();
34
+ if (i >= n) return;
35
+ const ch = raw[i];
36
+ if (ch === "{") {
37
+ i++;
38
+ skipWs();
39
+ if (raw[i] === "}") {
40
+ i++;
41
+ return;
42
+ }
43
+ const seen = /* @__PURE__ */ new Set();
44
+ while (i < n) {
45
+ skipWs();
46
+ if (raw[i] !== "\"") break;
47
+ const key = readString();
48
+ if (seen.has(key)) throw new SyntaxError(`Duplicate JSON object key: ${JSON.stringify(key)}`);
49
+ seen.add(key);
50
+ skipWs();
51
+ if (raw[i] !== ":") break;
52
+ i++;
53
+ scanValue(depth + 1);
54
+ skipWs();
55
+ if (raw[i] === ",") {
56
+ i++;
57
+ skipWs();
58
+ if (raw[i] === "}") break;
59
+ } else if (raw[i] === "}") {
60
+ i++;
61
+ return;
62
+ } else break;
63
+ }
64
+ return;
65
+ }
66
+ if (ch === "[") {
67
+ i++;
68
+ skipWs();
69
+ if (raw[i] === "]") {
70
+ i++;
71
+ return;
72
+ }
73
+ while (i < n) {
74
+ scanValue(depth + 1);
75
+ skipWs();
76
+ if (raw[i] === ",") {
77
+ i++;
78
+ skipWs();
79
+ if (raw[i] === "]") break;
80
+ } else if (raw[i] === "]") {
81
+ i++;
82
+ return;
83
+ } else break;
84
+ }
85
+ return;
86
+ }
87
+ if (ch === "\"") {
88
+ readString();
89
+ return;
90
+ }
91
+ while (i < n) {
92
+ const c = raw.charCodeAt(i);
93
+ if (c === 44 || c === 125 || c === 93 || c === 32 || c === 9 || c === 10 || c === 13) break;
94
+ i++;
95
+ }
96
+ };
97
+ scanValue(0);
98
+ return JSON.parse(raw);
99
+ }
100
+
101
+ //#endregion
102
+ export { parseStrictJson };