@ocap/tx-protocols 1.31.4 → 1.31.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/esm/bridge-release-crypto.mjs +1 -1
  2. package/esm/execute.mjs +24 -5
  3. package/esm/gas-security-guard.d.mts +2 -8
  4. package/esm/gas-security-guard.mjs +16 -1
  5. package/esm/index.d.mts +16 -3
  6. package/esm/index.mjs +0 -2
  7. package/esm/pipes/ensure-cost.mjs +3 -0
  8. package/esm/pipes/ensure-gas.d.mts +2 -1
  9. package/esm/pipes/write-ledger.mjs +2 -1
  10. package/esm/protocols/bridge/release-checks.d.mts +69 -0
  11. package/esm/protocols/bridge/{release.mjs → release-checks.mjs} +101 -100
  12. package/esm/protocols/rollup/bridge-create.mjs +2 -2
  13. package/esm/protocols/rollup/bridge-membership.mjs +10 -12
  14. package/esm/protocols/rollup/bridge-v2.d.mts +15 -5
  15. package/esm/protocols/rollup/bridge-v2.mjs +33 -8
  16. package/esm/protocols/token/deposit-v2.mjs +126 -25
  17. package/esm/protocols/token/withdraw-v2.d.mts +13 -1
  18. package/esm/protocols/token/withdraw-v2.mjs +158 -25
  19. package/esm/protocols/trade/transfer-v2.mjs +3 -8
  20. package/lib/bridge-release-crypto.cjs +1 -1
  21. package/lib/execute.cjs +24 -5
  22. package/lib/gas-security-guard.cjs +16 -1
  23. package/lib/gas-security-guard.d.cts +2 -8
  24. package/lib/index.cjs +0 -2
  25. package/lib/index.d.cts +16 -3
  26. package/lib/pipes/ensure-cost.cjs +3 -0
  27. package/lib/pipes/ensure-gas.d.cts +2 -1
  28. package/lib/pipes/write-ledger.cjs +2 -1
  29. package/lib/protocols/bridge/{release.cjs → release-checks.cjs} +115 -101
  30. package/lib/protocols/bridge/release-checks.d.cts +69 -0
  31. package/lib/protocols/rollup/bridge-create.cjs +1 -1
  32. package/lib/protocols/rollup/bridge-membership.cjs +9 -10
  33. package/lib/protocols/rollup/bridge-v2.cjs +32 -5
  34. package/lib/protocols/rollup/bridge-v2.d.cts +15 -5
  35. package/lib/protocols/token/deposit-v2.cjs +125 -24
  36. package/lib/protocols/token/withdraw-v2.cjs +155 -22
  37. package/lib/protocols/token/withdraw-v2.d.cts +13 -1
  38. package/lib/protocols/trade/transfer-v2.cjs +2 -7
  39. package/package.json +16 -16
  40. package/esm/protocols/bridge/release.d.mts +0 -34
  41. package/lib/protocols/bridge/release.d.cts +0 -34
@@ -6,6 +6,7 @@ const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
6
6
  const require_protocols_rollup_bridge_v2 = require('../rollup/bridge-v2.cjs');
7
7
  const require_protocols_rollup_pipes_verify_signers = require('../rollup/pipes/verify-signers.cjs');
8
8
  const require_protocols_rollup_pipes_verify_status = require('../rollup/pipes/verify-status.cjs');
9
+ const require_protocols_bridge_release_checks = require('../bridge/release-checks.cjs');
9
10
  let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
10
11
  let _ocap_util_lib_error = require("@ocap/util/lib/error");
11
12
  let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
@@ -14,6 +15,7 @@ let _ocap_state = require("@ocap/state");
14
15
  let _ocap_util = require("@ocap/util");
15
16
  let debug = require("debug");
16
17
  debug = require_rolldown_runtime.__toESM(debug);
18
+ let _ocap_util_lib_deposit_v2_bridge_fields = require("@ocap/util/lib/deposit-v2-bridge-fields");
17
19
  let _arcblock_validator = require("@arcblock/validator");
18
20
 
19
21
  //#region src/protocols/token/deposit-v2.ts
@@ -30,20 +32,70 @@ const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
30
32
  actualFee: (0, _arcblock_validator.vBN)((0, _arcblock_validator.vBNMin)(0)),
31
33
  data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
32
34
  }), (0, _arcblock_validator.vStripUnknown)());
35
+ /**
36
+ * itx shape of the bridge branch (#1692 §2.2, §2.5). The release fields map onto the deposit fields: `rollup` is the bridge
37
+ * (canonical spelling, checked against the stored row), `token.value` the amount, `evidence.hash` the evidence (lowercase
38
+ * `0x` only: the tombstone key is derived from the exact spelling), `validatorSignatures` the OcapRelease signatures.
39
+ * `proposer` must be empty, `actualFee` empty or `'0'`, `data` empty (checked next to the schema).
40
+ */
41
+ const bridgeSchema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
42
+ kind: _arcblock_validator.v.picklist([
43
+ 1,
44
+ 2,
45
+ 3
46
+ ]),
47
+ rollup: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(1), _arcblock_validator.v.maxLength(128)),
48
+ evidence: _arcblock_validator.v.object({ hash: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.regex(/^0x[0-9a-f]{64}$/)) }),
49
+ ordinal: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(0)),
50
+ to: _arcblock_validator.schemas.tokenHolder,
51
+ token: _arcblock_validator.v.object({
52
+ address: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(1), _arcblock_validator.v.maxLength(128)),
53
+ value: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.regex(/^[1-9]\d{0,77}$/))
54
+ }),
55
+ epoch: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(0), _arcblock_validator.v.maxValue(Number.MAX_SAFE_INTEGER)),
56
+ ethSender: _arcblock_validator.v.optional(_arcblock_validator.v.string(), ""),
57
+ claimSignature: _arcblock_validator.v.optional(_arcblock_validator.v.string(), ""),
58
+ proposer: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.literal("")), ""),
59
+ actualFee: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.union([_arcblock_validator.v.literal(""), _arcblock_validator.v.literal("0")])), ""),
60
+ data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
61
+ }), (0, _arcblock_validator.vStripUnknown)());
62
+ /** An absent or empty `google.protobuf.Any` (jspb decodes an unset `data` as undefined, CBOR may leave it out). */
63
+ const isEmptyAny = (value) => {
64
+ if (value === void 0 || value === null || value === "") return true;
65
+ if (typeof value !== "object") return false;
66
+ const { typeUrl, type, value: inner, ...rest } = value;
67
+ const empty = (x) => x === void 0 || x === null || x === "" || (x instanceof Uint8Array || Array.isArray(x)) && x.length === 0;
68
+ return empty(typeUrl) && empty(type) && empty(inner) && Object.keys(rest).length === 0;
69
+ };
33
70
  const runner = new _ocap_tx_pipeline.Runner("deposit_token_v2");
34
- runner.use(({ itx }, next) => {
71
+ runner.use(require_protocols_rollup_bridge_v2.peekBridgeFailClosed("deposit_token_v2", "PeekDepositBridge"));
72
+ /** The guarded pipe keeps the name of the pipe it wraps (structural tests and debugging find pipes by name). */
73
+ const named = (guarded, pipe) => Object.defineProperty(guarded, "name", {
74
+ value: pipe.name,
75
+ configurable: true
76
+ });
77
+ /** Add a pipe of the v1 branch (skipped for a v2 bridge). */
78
+ const useV1 = (pipe, opts) => runner.use(named(require_protocols_rollup_bridge_v2.ifV1(pipe), pipe), opts);
79
+ /** Add a pipe of the bridge branch (skipped for anything else). */
80
+ const useV2 = (pipe, opts) => runner.use(named(require_protocols_rollup_bridge_v2.ifV2(pipe), pipe), opts);
81
+ useV1(({ itx }, next) => {
82
+ const fields = (0, _ocap_util_lib_deposit_v2_bridge_fields.findNonDefaultDepositV2BridgeFields)(itx);
83
+ if (fields.length) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Invalid itx: a legacy rollup deposit must not carry bridge fields: ${fields.join(", ")}`));
84
+ return next();
85
+ });
86
+ useV1(({ itx }, next) => {
35
87
  const { error } = (0, _arcblock_validator.vValidate)(schema, itx);
36
88
  if (error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Invalid itx: ${error.message}`));
37
89
  return next();
38
90
  });
39
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
91
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
40
92
  from: "itx.rollup",
41
93
  to: "rollupState",
42
94
  status: "INVALID_ROLLUP",
43
95
  table: "rollup"
44
96
  }));
45
- runner.use(require_protocols_rollup_bridge_v2.rejectBridgeV2("deposit_token_v2"));
46
- runner.use((context, next) => {
97
+ useV1(require_protocols_rollup_bridge_v2.rejectBridgeV2("deposit_token_v2"));
98
+ useV1((context, next) => {
47
99
  const { tx, itx, rollupState } = context;
48
100
  if (_ocap_state.rollup.getRollupTargetType(rollupState) === "EVM") {
49
101
  if ((0, _arcblock_validator.vValidate)((0, _arcblock_validator.vDID)({
@@ -64,53 +116,53 @@ runner.use((context, next) => {
64
116
  if (isProposerSponsor && signatures.some((x) => x.signer === itx.to)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "itx.to should not exist in signatures"));
65
117
  return next();
66
118
  });
67
- runner.use(require_protocols_rollup_pipes_verify_status.default({ paused: false }));
68
- runner.use((context, next) => {
119
+ useV1(require_protocols_rollup_pipes_verify_status.default({ paused: false }));
120
+ useV1((context, next) => {
69
121
  const { itx, rollupState } = context;
70
122
  if (!_ocap_state.rollup.isTokenSupportedByRollup(rollupState, itx.token.address)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Deposit token address does not match with rollup state"));
71
123
  if (rollupState.validators.some((x) => x.address === itx.proposer) === false) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "itx.proposer does not exist in validators"));
72
124
  return next();
73
125
  });
74
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
126
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
75
127
  from: "itx.evidence.hash",
76
128
  to: "evidenceState",
77
129
  status: "OK",
78
130
  table: "evidence"
79
131
  }));
80
- runner.use(({ evidenceState }, next) => {
132
+ useV1(({ evidenceState }, next) => {
81
133
  if (evidenceState) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Deposit evidence already seen on this chain"));
82
134
  return next();
83
135
  });
84
- runner.use(require_protocols_rollup_pipes_verify_signers.default({
136
+ useV1(require_protocols_rollup_pipes_verify_signers.default({
85
137
  signersKey: "tx.signaturesList",
86
138
  allowSender: true
87
139
  }));
88
- runner.use(_ocap_tx_pipeline.pipes.VerifyMultiSigV2({ signersKey: "signers" }));
89
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
140
+ useV1(_ocap_tx_pipeline.pipes.VerifyMultiSigV2({ signersKey: "signers" }));
141
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
90
142
  from: "itx.token.address",
91
143
  to: "tokenState",
92
144
  status: "INVALID_TOKEN",
93
145
  table: "token"
94
146
  }));
95
- runner.use((context, next) => {
147
+ useV1((context, next) => {
96
148
  const { itx } = context;
97
149
  context.stakeAddress = (0, _arcblock_did_util.toStakeAddress)(itx.proposer, itx.rollup);
98
150
  context.lockerAddress = require_util.getRewardLocker(itx.rollup);
99
151
  return next();
100
152
  });
101
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
153
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
102
154
  from: "lockerAddress",
103
155
  to: "lockerState",
104
156
  status: "OK",
105
157
  table: "stake"
106
158
  }));
107
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
159
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
108
160
  from: "stakeAddress",
109
161
  to: "stakeState",
110
162
  status: "INVALID_STAKE_STATE",
111
163
  table: "stake"
112
164
  }));
113
- runner.use((context, next) => {
165
+ useV1((context, next) => {
114
166
  const { itx, stakeState, tokenState, rollupState } = context;
115
167
  if (stakeState.revocable === true) return next(new _ocap_util_lib_error.CustomError("INVALID_STAKE_STATE", `Staking not locked for deposit: ${stakeState.address}`));
116
168
  const depositAmount = new _ocap_util.BN(itx.token.value).add(new _ocap_util.BN(itx.actualFee));
@@ -122,7 +174,7 @@ runner.use((context, next) => {
122
174
  if (depositAmount.gt(stakedAmount)) return next(new _ocap_util_lib_error.CustomError("INSUFFICIENT_STAKE", `Deposit amount must be less than available stake: ${(0, _ocap_util.fromUnitToToken)(stakedAmount, tokenState.decimal)}`));
123
175
  return next();
124
176
  });
125
- runner.use((context, next) => {
177
+ useV1((context, next) => {
126
178
  const { itx, rollupState, tokenState } = context;
127
179
  const { depositFeeRate, maxDepositFee, minDepositFee } = rollupState;
128
180
  const { reward } = require_util.getTxFee({
@@ -134,36 +186,36 @@ runner.use((context, next) => {
134
186
  if (new _ocap_util.BN(itx.actualFee).lt(new _ocap_util.BN(reward))) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `itx.actualFee too low, expect at least ${(0, _ocap_util.fromUnitToToken)(reward, tokenState.decimal)}, got ${(0, _ocap_util.fromUnitToToken)(itx.actualFee, tokenState.decimal)}`));
135
187
  return next();
136
188
  });
137
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
189
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
138
190
  from: "tx.from",
139
191
  to: "senderState",
140
192
  status: "OK",
141
193
  table: "account"
142
194
  }));
143
- runner.use(async (context, next) => {
195
+ useV1(async (context, next) => {
144
196
  if (context.isProposerSponsor) {
145
197
  context.receiverState = await context.statedb.account.get(context.itx.to, context);
146
198
  if (context.receiverState && (context.receiverState.address !== context.itx.to || _ocap_state.account.isMigrated(context.receiverState))) return next(new _ocap_util_lib_error.CustomError("ACCOUNT_MIGRATED", `Recipient account is already migrated: ${context.itx.to}`));
147
199
  }
148
200
  return next();
149
201
  });
150
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
202
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
151
203
  from: "signers",
152
204
  to: "signerStates",
153
205
  status: "INVALID_SIGNER_STATE",
154
206
  table: "account"
155
207
  }));
156
- runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
208
+ useV1(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
157
209
  signerKey: "signerStates",
158
210
  stateKey: "senderState",
159
211
  addressKey: "tx.from"
160
212
  }));
161
- runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
213
+ useV1(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
162
214
  "senderState",
163
215
  "receiverState",
164
216
  "signerStates"
165
217
  ] }));
166
- runner.use(require_pipes_ensure_gas.default((context) => {
218
+ useV1(require_pipes_ensure_gas.default((context) => {
167
219
  const result = {
168
220
  create: 1,
169
221
  update: 1,
@@ -177,9 +229,57 @@ runner.use(require_pipes_ensure_gas.default((context) => {
177
229
  else result.create += 1;
178
230
  return result;
179
231
  }));
180
- runner.use(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
232
+ useV1(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
233
+ useV2(_ocap_tx_pipeline.pipes.VerifyMultiSig(0));
234
+ useV2(function BridgeDepositSchema(context, next) {
235
+ const { error } = (0, _arcblock_validator.vValidate)(bridgeSchema, context.itx);
236
+ if (error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Invalid itx: ${error.message}`));
237
+ const itx = context.itx;
238
+ if (!isEmptyAny(itx.data)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Invalid itx: a bridge deposit carries no data"));
239
+ const signatures = (0, _ocap_util_lib_get_list_field.getListField)(itx, "validatorSignatures");
240
+ if (!Array.isArray(signatures) || signatures.length > 16 || signatures.some((x) => typeof x !== "string")) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Invalid itx: validatorSignatures must be at most 16 strings"));
241
+ context.releaseItx = {
242
+ kind: itx.kind,
243
+ bridge: itx.rollup,
244
+ evidence: itx.evidence.hash,
245
+ ordinal: itx.ordinal,
246
+ to: itx.to,
247
+ amount: itx.token.value,
248
+ epoch: itx.epoch,
249
+ ethSender: itx.ethSender ?? "",
250
+ claimSignature: itx.claimSignature ?? "",
251
+ signaturesList: signatures
252
+ };
253
+ return next();
254
+ });
255
+ useV2(require_protocols_bridge_release_checks.checkReleaseFields);
256
+ useV2(require_protocols_bridge_release_checks.rejectReleaseDelegator);
257
+ useV2(require_protocols_bridge_release_checks.resolveReleaseTime);
258
+ useV2(require_protocols_bridge_release_checks.resolveReleaseBridge({ token: (context) => context.itx.token?.address }));
259
+ useV2(require_protocols_bridge_release_checks.checkReleaseMinimum);
260
+ useV2(require_protocols_bridge_release_checks.checkReleaseRecipient);
261
+ useV2(require_protocols_bridge_release_checks.checkReleaseTombstone);
262
+ useV2(require_protocols_bridge_release_checks.verifyReleaseSignatures);
263
+ useV2(require_protocols_bridge_release_checks.verifyReleaseClaim);
264
+ useV2(require_protocols_bridge_release_checks.checkReleaseRefund);
265
+ useV2(require_protocols_bridge_release_checks.checkReleaseRecipientState);
266
+ useV2(require_protocols_bridge_release_checks.checkReleaseCap);
267
+ useV2(require_protocols_bridge_release_checks.checkReleaseReserve);
268
+ useV2(require_pipes_ensure_gas.default(require_protocols_bridge_release_checks.releaseGasEstimate));
269
+ useV2(_ocap_tx_pipeline.pipes.ExtractState({
270
+ from: "tx.from",
271
+ to: "senderState",
272
+ status: "INVALID_SENDER_STATE",
273
+ table: "account"
274
+ }));
275
+ useV2(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
276
+ stateKey: "senderState",
277
+ addressKey: "tx.from"
278
+ }));
279
+ useV2(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: ["senderState", "receiverState"] }));
280
+ useV2(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
181
281
  runner.use(_ocap_tx_pipeline.pipes.TakeStateSnapshot());
182
- runner.use(async (context, next) => {
282
+ useV1(async (context, next) => {
183
283
  const { tx, itx, statedb, rollupState, senderState, receiverState, isProposerSponsor, stakeState, stakeAddress, lockerState, lockerAddress, senderChange, updateVaults } = context;
184
284
  const user = itx.token.value;
185
285
  const fee = itx.actualFee;
@@ -272,6 +372,7 @@ runner.use(async (context, next) => {
272
372
  debug$1("deposit-token-v2", itx);
273
373
  next();
274
374
  }, { persistError: true });
375
+ useV2(require_protocols_bridge_release_checks.writeRelease, { persistError: true });
275
376
  runner.use(_ocap_tx_pipeline.pipes.VerifyStateDiff());
276
377
  var deposit_v2_default = runner;
277
378
 
@@ -14,6 +14,8 @@ let _ocap_util = require("@ocap/util");
14
14
  let debug = require("debug");
15
15
  debug = require_rolldown_runtime.__toESM(debug);
16
16
  let _arcblock_validator = require("@arcblock/validator");
17
+ let _ocap_tx_pipeline_lib_helpers_bridge_withdraw = require("@ocap/tx-pipeline/lib/helpers/bridge-withdraw");
18
+ let _ocap_tx_pipeline_lib_helpers_replay_guard = require("@ocap/tx-pipeline/lib/helpers/replay-guard");
17
19
 
18
20
  //#region src/protocols/token/withdraw-v2.ts
19
21
  const debug$1 = (0, debug.default)("@ocap/tx-protocols:withdraw-token");
@@ -31,19 +33,30 @@ const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
31
33
  data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
32
34
  }), (0, _arcblock_validator.vStripUnknown)());
33
35
  const runner = new _ocap_tx_pipeline.Runner("withdraw_token_v2");
34
- runner.use(({ itx }, next) => {
36
+ const PeekWithdrawBridge = require_protocols_rollup_bridge_v2.peekBridgeFailClosed("withdraw_token_v2", "PeekWithdrawBridge");
37
+ runner.use(PeekWithdrawBridge);
38
+ /** The guarded pipe keeps the name of the pipe it wraps (structural tests and debugging find pipes by name). */
39
+ const named = (guarded, pipe) => Object.defineProperty(guarded, "name", {
40
+ value: pipe.name,
41
+ configurable: true
42
+ });
43
+ /** Add a pipe of the v1 branch (skipped for a v2 bridge). */
44
+ const useV1 = (pipe, opts) => runner.use(named(require_protocols_rollup_bridge_v2.ifV1(pipe), pipe), opts);
45
+ /** Add a pipe of the bridge branch (skipped for anything else). */
46
+ const useV2 = (pipe, opts) => runner.use(named(require_protocols_rollup_bridge_v2.ifV2(pipe), pipe), opts);
47
+ useV1(({ itx }, next) => {
35
48
  const { error } = (0, _arcblock_validator.vValidate)(schema, itx);
36
49
  if (error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Invalid itx: ${error.message}`));
37
50
  return next();
38
51
  });
39
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
52
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
40
53
  from: "itx.rollup",
41
54
  to: "rollupState",
42
55
  status: "INVALID_ROLLUP",
43
56
  table: "rollup"
44
57
  }));
45
- runner.use(require_protocols_rollup_bridge_v2.rejectBridgeV2("withdraw_token_v2"));
46
- runner.use((context, next) => {
58
+ useV1(require_protocols_rollup_bridge_v2.rejectBridgeV2("withdraw_token_v2"));
59
+ useV1((context, next) => {
47
60
  const { tx, itx, rollupState } = context;
48
61
  if (_ocap_state.rollup.getRollupTargetType(rollupState) === "EVM") {
49
62
  if ((0, _arcblock_validator.vValidate)((0, _arcblock_validator.vDID)({
@@ -58,8 +71,8 @@ runner.use((context, next) => {
58
71
  if (itx.to !== tx.from) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "You can only withdraw to tx sender account"));
59
72
  return next();
60
73
  });
61
- runner.use(require_protocols_rollup_pipes_verify_status.default({ paused: false }));
62
- runner.use((context, next) => {
74
+ useV1(require_protocols_rollup_pipes_verify_status.default({ paused: false }));
75
+ useV1((context, next) => {
63
76
  const { itx, rollupState } = context;
64
77
  if (!_ocap_state.rollup.isTokenSupportedByRollup(rollupState, itx.token.address)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Withdraw token address does not match with rollup"));
65
78
  if (_ocap_state.rollup.hasRollupReserveTracking(rollupState)) {
@@ -70,19 +83,19 @@ runner.use((context, next) => {
70
83
  }
71
84
  return next();
72
85
  });
73
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
86
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
74
87
  from: "itx.token.address",
75
88
  to: "tokenState",
76
89
  status: "INVALID_TOKEN",
77
90
  table: "token"
78
91
  }));
79
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
92
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
80
93
  from: "tx.from",
81
94
  to: "senderState",
82
95
  status: "INVALID_SENDER_STATE",
83
96
  table: "account"
84
97
  }));
85
- runner.use((context, next) => {
98
+ useV1((context, next) => {
86
99
  const { tx, itx, tokenState, rollupState } = context;
87
100
  const withdrawAmount = new _ocap_util.BN(itx.token.value);
88
101
  const minWithdrawAmount = new _ocap_util.BN(rollupState.minWithdrawAmount);
@@ -99,11 +112,11 @@ runner.use((context, next) => {
99
112
  };
100
113
  return next();
101
114
  });
102
- runner.use(_ocap_tx_pipeline.pipes.VerifyTokenBalance({
115
+ useV1(_ocap_tx_pipeline.pipes.VerifyTokenBalance({
103
116
  ownerKey: "senderState",
104
117
  conditionKey: "tokenConditions"
105
118
  }));
106
- runner.use((context, next) => {
119
+ useV1((context, next) => {
107
120
  const { itx, rollupState, tokenState } = context;
108
121
  const { withdrawFeeRate, maxWithdrawFee, minWithdrawFee } = rollupState;
109
122
  const { reward } = require_util.getTxFee({
@@ -120,7 +133,7 @@ runner.use((context, next) => {
120
133
  }
121
134
  return next();
122
135
  });
123
- runner.use((context, next) => {
136
+ useV1((context, next) => {
124
137
  const { tx, itx, rollupState } = context;
125
138
  if (new _ocap_util.BN(itx.maxFee).isZero()) return next();
126
139
  if ((rollupState.validators ?? []).map((x) => x.address).includes(itx.proposer) === false) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `itx.proposer ${itx.proposer} does not exist in validator whitelist`));
@@ -130,42 +143,42 @@ runner.use((context, next) => {
130
143
  context.signers = signatures.map((x) => x.signer);
131
144
  verifyMultiSigV2(context, next);
132
145
  });
133
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
146
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
134
147
  from: "signers",
135
148
  to: "signerStates",
136
149
  status: "INVALID_SIGNER_STATE",
137
150
  table: "account"
138
151
  }));
139
- runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
152
+ useV1(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
140
153
  signerKey: "signerStates",
141
154
  stateKey: "senderState",
142
155
  addressKey: "tx.from"
143
156
  }));
144
- runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: ["senderState", "signerStates"] }));
145
- runner.use((context, next) => {
157
+ useV1(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: ["senderState", "signerStates"] }));
158
+ useV1((context, next) => {
146
159
  const { itx, senderState } = context;
147
160
  context.stakeAddress = (0, _arcblock_did_util.toStakeAddress)(senderState.address, itx.rollup);
148
161
  context.lockerAddress = require_util.getRewardLocker(itx.rollup);
149
162
  return next();
150
163
  });
151
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
164
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
152
165
  from: "lockerAddress",
153
166
  to: "lockerState",
154
167
  status: "OK",
155
168
  table: "stake"
156
169
  }));
157
- runner.use(_ocap_tx_pipeline.pipes.ExtractState({
170
+ useV1(_ocap_tx_pipeline.pipes.ExtractState({
158
171
  from: "stakeAddress",
159
172
  to: "stakeState",
160
173
  status: "OK",
161
174
  table: "stake"
162
175
  }));
163
- runner.use((context, next) => {
176
+ useV1((context, next) => {
164
177
  const { stakeState } = context;
165
178
  if (stakeState && stakeState.revocable === true) return next(new _ocap_util_lib_error.CustomError("INVALID_STAKE_STATE", `Staking not locked for withdraw: ${stakeState.address}`));
166
179
  return next();
167
180
  });
168
- runner.use(require_pipes_ensure_gas.default((context) => {
181
+ useV1(require_pipes_ensure_gas.default((context) => {
169
182
  const result = {
170
183
  create: 0,
171
184
  update: 1,
@@ -177,9 +190,94 @@ runner.use(require_pipes_ensure_gas.default((context) => {
177
190
  else result.create += 1;
178
191
  return result;
179
192
  }));
180
- runner.use(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
193
+ useV1(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
194
+ useV2(async (context, next) => {
195
+ let bridge;
196
+ try {
197
+ bridge = await (0, _ocap_tx_pipeline.resolveBridge)(context, context.itx.rollup);
198
+ } catch (err) {
199
+ return next(err);
200
+ }
201
+ if (!bridge) return next(new _ocap_util_lib_error.CustomError("BRIDGE_WITHDRAW_RESERVE", `${context.itx.rollup} is not a v2 bridge that can be withdrawn from`));
202
+ const result = (0, _ocap_tx_pipeline_lib_helpers_bridge_withdraw.checkBridgeWithdrawV2)({
203
+ ...context,
204
+ txType: "withdraw_token_v2"
205
+ }, bridge);
206
+ if ((0, _ocap_tx_pipeline_lib_helpers_bridge_withdraw.isBridgeWithdrawFailure)(result)) return next(new _ocap_util_lib_error.CustomError(result.code, result.message));
207
+ context.bridge = bridge;
208
+ context.bridgeWithdraw = result;
209
+ context.reserveAddress = (0, _arcblock_did_util.toBridgeReserveAddress)(bridge.address);
210
+ return next();
211
+ });
212
+ useV2((context, next) => {
213
+ const bridge = context.bridge;
214
+ const fields = (0, _ocap_tx_pipeline_lib_helpers_bridge_withdraw.decodeBridgeWithdrawV2Fields)(context.itx, bridge.token);
215
+ if (!fields) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "Bridge withdraw envelope can not be matched to the transaction"));
216
+ const problem = (0, _ocap_tx_pipeline_lib_helpers_replay_guard.assertBridgeWithdrawEnvelopeInBranch)(context, {
217
+ bridge: bridge.address,
218
+ amount: fields.amount,
219
+ token: bridge.token,
220
+ ethRecipient: fields.ethRecipient
221
+ });
222
+ if (problem) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Bridge withdraw envelope does not match: ${problem}`));
223
+ return next();
224
+ });
225
+ useV2(_ocap_tx_pipeline.pipes.ExtractState({
226
+ from: "tx.from",
227
+ to: "senderState",
228
+ status: "INVALID_SENDER_STATE",
229
+ table: "account"
230
+ }));
231
+ useV2((context, next) => {
232
+ context.tokenConditions = {
233
+ owner: context.tx.from,
234
+ tokens: [{
235
+ address: context.bridge.token,
236
+ value: context.bridgeWithdraw.amount
237
+ }]
238
+ };
239
+ return next();
240
+ });
241
+ useV2(_ocap_tx_pipeline.pipes.VerifyTokenBalance({
242
+ ownerKey: "senderState",
243
+ conditionKey: "tokenConditions"
244
+ }));
245
+ useV2(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
246
+ signerKey: "signerStates",
247
+ stateKey: "senderState",
248
+ addressKey: "tx.from"
249
+ }));
250
+ useV2(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: ["senderState"] }));
251
+ useV2((context, next) => {
252
+ context.bridgeTokens = [context.bridge.token];
253
+ return next();
254
+ });
255
+ useV2(_ocap_tx_pipeline.pipes.ExtractState({
256
+ from: "bridgeTokens",
257
+ to: "bridgeTokenStates",
258
+ status: "INVALID_TOKEN",
259
+ table: "token"
260
+ }));
261
+ useV2(_ocap_tx_pipeline.pipes.verifyTokenAccess({
262
+ statesKey: "bridgeTokenStates",
263
+ listFieldKey: "spenders",
264
+ accountKeys: ["senderState"],
265
+ errorMessage: "Account {address} is not allowed to transfer token {tokenAddress}"
266
+ }));
267
+ useV2(_ocap_tx_pipeline.pipes.ExtractState({
268
+ from: "reserveAddress",
269
+ to: "reserveState",
270
+ status: "BRIDGE_WITHDRAW_RESERVE",
271
+ table: "account"
272
+ }));
273
+ useV2(require_pipes_ensure_gas.default((context) => ({
274
+ create: 0,
275
+ update: 2,
276
+ payment: context.bridge.token === context.config.token.address ? context.bridgeWithdraw.amount : 0
277
+ })));
278
+ useV2(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
181
279
  runner.use(_ocap_tx_pipeline.pipes.TakeStateSnapshot());
182
- runner.use(async (context, next) => {
280
+ useV1(async (context, next) => {
183
281
  const { tx, itx, statedb, stakeState, senderState, lockerState, lockerAddress, stakeAddress, senderChange, updateVaults } = context;
184
282
  const total = require_util.getBNSum(itx.token.value, itx.actualFee, itx.maxFee);
185
283
  const fee = require_util.getBNSum(itx.actualFee, itx.maxFee);
@@ -247,6 +345,41 @@ runner.use(async (context, next) => {
247
345
  debug$1("withdraw-token-v2", itx);
248
346
  next();
249
347
  }, { persistError: true });
348
+ useV2(async (context, next) => {
349
+ const { tx, statedb, senderState, reserveState, senderChange, updateVaults } = context;
350
+ const token = context.bridge.token;
351
+ const { amount } = context.bridgeWithdraw;
352
+ let senderUpdates = require_util.applyTokenUpdates([{
353
+ address: token,
354
+ value: amount
355
+ }], senderState, "sub");
356
+ if (senderChange) senderUpdates = require_util.applyTokenChange(senderUpdates, senderChange);
357
+ const reserveUpdates = require_util.applyTokenUpdates([{
358
+ address: token,
359
+ value: amount
360
+ }], reserveState, "add");
361
+ const [newSenderState, newReserveState] = await Promise.all([statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
362
+ nonce: tx.nonce,
363
+ ...senderUpdates
364
+ }, context), context), statedb.account.update(reserveState.address, _ocap_state.account.update(reserveState, reserveUpdates, context), context)]);
365
+ if (updateVaults) await updateVaults();
366
+ context.senderState = newSenderState;
367
+ context.reserveState = newReserveState;
368
+ context.updatedAccounts = context.updatedAccounts || [];
369
+ context.updatedAccounts.push({
370
+ address: senderState.address,
371
+ token,
372
+ delta: `-${amount}`,
373
+ action: "transfer"
374
+ }, {
375
+ address: reserveState.address,
376
+ token,
377
+ delta: amount,
378
+ action: "transfer"
379
+ });
380
+ debug$1("withdraw-token-v2 (bridge)", context.itx);
381
+ next();
382
+ }, { persistError: true });
250
383
  runner.use(_ocap_tx_pipeline.pipes.VerifyStateDiff());
251
384
  var withdraw_v2_default = runner;
252
385
 
@@ -1,5 +1,5 @@
1
1
  import { IReadyContext, IStakeState, IWithRollup, IWithSender, IWithSigners, IWithStake, IWithToken, TWithdrawTokenV2Tx } from "@ocap/types";
2
- import { Runner } from "@ocap/tx-pipeline";
2
+ import { ResolvedBridge, Runner } from "@ocap/tx-pipeline";
3
3
 
4
4
  //#region src/protocols/token/withdraw-v2.d.ts
5
5
  /** Token input */
@@ -35,6 +35,18 @@ type Context = IReadyContext<TWithdrawTokenV2Tx> & IWithSender & Partial<IWithSi
35
35
  delta: string;
36
36
  action: string;
37
37
  }>;
38
+ /** set by the first pipe: `itx.rollup` is a v2 bridge, i.e. the bridge branch runs */
39
+ isBridgeV2?: boolean;
40
+ /** bridge branch: the bridge `itx.rollup` resolves to (pre-tx row, flags at txTime) */
41
+ bridge?: ResolvedBridge;
42
+ /** bridge branch: the validated withdraw */
43
+ bridgeWithdraw?: {
44
+ amount: string;
45
+ ethRecipient: string;
46
+ };
47
+ /** bridge branch: `toBridgeReserveAddress(itx.rollup)`, recomputed, and its account */
48
+ reserveAddress?: string;
49
+ reserveState?: any;
38
50
  };
39
51
  declare const runner: Runner<Context>;
40
52
  //#endregion
@@ -17,7 +17,6 @@ let _ocap_message = require("@ocap/message");
17
17
  let _arcblock_validator = require("@arcblock/validator");
18
18
  let lodash_isEmpty = require("lodash/isEmpty");
19
19
  lodash_isEmpty = require_rolldown_runtime.__toESM(lodash_isEmpty);
20
- let _ocap_tx_pipeline_lib_helpers_bridge_withdraw = require("@ocap/tx-pipeline/lib/helpers/bridge-withdraw");
21
20
 
22
21
  //#region src/protocols/trade/transfer-v2.ts
23
22
  const runner = new _ocap_tx_pipeline.Runner("transfer_v2");
@@ -110,12 +109,8 @@ runner.use((context, next) => {
110
109
  }
111
110
  next();
112
111
  });
113
- runner.use(async (context, next) => {
114
- if (!(0, _ocap_tx_pipeline.isBridgeReserveRole)(context.itx.to)) return next();
115
- const bridge = await (0, _ocap_tx_pipeline.resolveBridgeForReserve)(context, context.itx.to);
116
- if (!bridge) return next(new _ocap_util_lib_error.CustomError("BRIDGE_WITHDRAW_RESERVE", `${context.itx.to} has the bridge reserve role but is not the reserve of any bridge`));
117
- const result = (0, _ocap_tx_pipeline_lib_helpers_bridge_withdraw.checkBridgeWithdraw)(context, bridge);
118
- if ((0, _ocap_tx_pipeline_lib_helpers_bridge_withdraw.isBridgeWithdrawFailure)(result)) return next(new _ocap_util_lib_error.CustomError(result.code, result.message));
112
+ runner.use((context, next) => {
113
+ if ((0, _ocap_tx_pipeline.isBridgeReserveRole)(context.itx.to)) return next(new _ocap_util_lib_error.CustomError("BRIDGE_WITHDRAW_RESERVE", `${context.itx.to} is a bridge reserve: transfer_v2 can not credit it, withdraw with withdraw_token_v2`));
119
114
  return next();
120
115
  });
121
116
  runner.use(require_pipes_ensure_gas.default((context) => {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.31.4",
6
+ "version": "1.31.6",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
8
  "type": "module",
9
9
  "main": "./lib/index.cjs",
@@ -46,22 +46,22 @@
46
46
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
47
47
  "license": "MIT",
48
48
  "dependencies": {
49
- "@arcblock/did": "1.31.4",
50
- "@arcblock/did-util": "1.31.4",
51
- "@arcblock/jwt": "1.31.4",
52
- "@arcblock/validator": "1.31.4",
53
- "@arcblock/vc": "1.31.4",
49
+ "@arcblock/did": "1.31.6",
50
+ "@arcblock/did-util": "1.31.6",
51
+ "@arcblock/jwt": "1.31.6",
52
+ "@arcblock/validator": "1.31.6",
53
+ "@arcblock/vc": "1.31.6",
54
54
  "@blocklet/xss": "^0.3.7",
55
- "@ocap/asset": "1.31.4",
56
- "@ocap/client": "1.31.4",
57
- "@ocap/mcrypto": "1.31.4",
58
- "@ocap/merkle-tree": "1.31.4",
59
- "@ocap/message": "1.31.4",
60
- "@ocap/state": "1.31.4",
61
- "@ocap/tx-pipeline": "1.31.4",
62
- "@ocap/types": "1.31.4",
63
- "@ocap/util": "1.31.4",
64
- "@ocap/wallet": "1.31.4",
55
+ "@ocap/asset": "1.31.6",
56
+ "@ocap/client": "1.31.6",
57
+ "@ocap/mcrypto": "1.31.6",
58
+ "@ocap/merkle-tree": "1.31.6",
59
+ "@ocap/message": "1.31.6",
60
+ "@ocap/state": "1.31.6",
61
+ "@ocap/tx-pipeline": "1.31.6",
62
+ "@ocap/types": "1.31.6",
63
+ "@ocap/util": "1.31.6",
64
+ "@ocap/wallet": "1.31.6",
65
65
  "debug": "^4.4.3",
66
66
  "deep-diff": "^1.0.2",
67
67
  "lodash": "^4.17.23",