@ocap/tx-protocols 1.30.24 → 1.30.25

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 (101) hide show
  1. package/esm/execute.d.mts +6 -2
  2. package/esm/execute.mjs +214 -25
  3. package/esm/index.d.mts +25 -2
  4. package/esm/index.mjs +2 -2
  5. package/esm/pipes/ensure-cost.mjs +2 -2
  6. package/esm/pipes/write-ledger.mjs +7 -0
  7. package/esm/protocols/account/delegate.mjs +7 -6
  8. package/esm/protocols/account/migrate.mjs +2 -2
  9. package/esm/protocols/account/revoke-delegate.mjs +1 -1
  10. package/esm/protocols/asset/acquire-v2.mjs +25 -6
  11. package/esm/protocols/asset/acquire-v3.d.mts +11 -1
  12. package/esm/protocols/asset/acquire-v3.mjs +78 -9
  13. package/esm/protocols/asset/consume.mjs +1 -1
  14. package/esm/protocols/asset/create.mjs +1 -1
  15. package/esm/protocols/asset/mint.mjs +8 -2
  16. package/esm/protocols/asset/pipes/verify-itx-address.mjs +1 -1
  17. package/esm/protocols/asset/update.mjs +1 -1
  18. package/esm/protocols/factory/create.mjs +4 -3
  19. package/esm/protocols/governance/claim-stake.mjs +2 -2
  20. package/esm/protocols/governance/return-stake.mjs +2 -2
  21. package/esm/protocols/governance/revoke-stake.mjs +2 -2
  22. package/esm/protocols/governance/slash-stake.mjs +2 -2
  23. package/esm/protocols/governance/stake.mjs +2 -2
  24. package/esm/protocols/rollup/claim-reward.mjs +5 -4
  25. package/esm/protocols/rollup/close.mjs +1 -1
  26. package/esm/protocols/rollup/create-block.mjs +1 -1
  27. package/esm/protocols/rollup/create.mjs +1 -1
  28. package/esm/protocols/rollup/join.mjs +2 -2
  29. package/esm/protocols/rollup/leave.mjs +1 -1
  30. package/esm/protocols/rollup/migrate.mjs +1 -1
  31. package/esm/protocols/rollup/pause.mjs +1 -1
  32. package/esm/protocols/rollup/pipes/verify-evidence.mjs +5 -2
  33. package/esm/protocols/rollup/pipes/verify-signers.mjs +5 -3
  34. package/esm/protocols/rollup/resume.mjs +1 -1
  35. package/esm/protocols/rollup/update.mjs +4 -4
  36. package/esm/protocols/token/create.mjs +2 -2
  37. package/esm/protocols/token/deposit-v2.mjs +2 -2
  38. package/esm/protocols/token/withdraw-v2.mjs +13 -9
  39. package/esm/protocols/token-factory/burn.mjs +4 -4
  40. package/esm/protocols/token-factory/create.mjs +2 -2
  41. package/esm/protocols/token-factory/mint.mjs +2 -2
  42. package/esm/protocols/token-factory/update.mjs +7 -5
  43. package/esm/protocols/trade/exchange-v2.d.mts +3 -1
  44. package/esm/protocols/trade/exchange-v2.mjs +69 -11
  45. package/esm/protocols/trade/transfer-v2.mjs +39 -23
  46. package/esm/protocols/trade/transfer-v3.d.mts +11 -1
  47. package/esm/protocols/trade/transfer-v3.mjs +102 -11
  48. package/esm/tx-timer.d.mts +5 -0
  49. package/esm/tx-timer.mjs +13 -0
  50. package/esm/util.mjs +2 -1
  51. package/lib/execute.cjs +211 -20
  52. package/lib/execute.d.cts +6 -2
  53. package/lib/index.cjs +3 -1
  54. package/lib/index.d.cts +25 -2
  55. package/lib/pipes/ensure-cost.cjs +2 -2
  56. package/lib/pipes/write-ledger.cjs +7 -0
  57. package/lib/protocols/account/delegate.cjs +7 -6
  58. package/lib/protocols/account/migrate.cjs +2 -2
  59. package/lib/protocols/account/revoke-delegate.cjs +1 -1
  60. package/lib/protocols/asset/acquire-v2.cjs +24 -5
  61. package/lib/protocols/asset/acquire-v3.cjs +77 -8
  62. package/lib/protocols/asset/acquire-v3.d.cts +11 -1
  63. package/lib/protocols/asset/consume.cjs +1 -1
  64. package/lib/protocols/asset/create.cjs +1 -1
  65. package/lib/protocols/asset/mint.cjs +7 -1
  66. package/lib/protocols/asset/pipes/verify-itx-address.cjs +1 -1
  67. package/lib/protocols/asset/update.cjs +1 -1
  68. package/lib/protocols/factory/create.cjs +4 -3
  69. package/lib/protocols/governance/claim-stake.cjs +2 -2
  70. package/lib/protocols/governance/return-stake.cjs +2 -2
  71. package/lib/protocols/governance/revoke-stake.cjs +2 -2
  72. package/lib/protocols/governance/slash-stake.cjs +2 -2
  73. package/lib/protocols/governance/stake.cjs +2 -2
  74. package/lib/protocols/rollup/claim-reward.cjs +5 -4
  75. package/lib/protocols/rollup/close.cjs +1 -1
  76. package/lib/protocols/rollup/create-block.cjs +1 -1
  77. package/lib/protocols/rollup/create.cjs +1 -1
  78. package/lib/protocols/rollup/join.cjs +2 -2
  79. package/lib/protocols/rollup/leave.cjs +1 -1
  80. package/lib/protocols/rollup/migrate.cjs +1 -1
  81. package/lib/protocols/rollup/pause.cjs +1 -1
  82. package/lib/protocols/rollup/pipes/verify-evidence.cjs +5 -2
  83. package/lib/protocols/rollup/pipes/verify-signers.cjs +5 -4
  84. package/lib/protocols/rollup/resume.cjs +1 -1
  85. package/lib/protocols/rollup/update.cjs +4 -4
  86. package/lib/protocols/token/create.cjs +2 -2
  87. package/lib/protocols/token/deposit-v2.cjs +2 -2
  88. package/lib/protocols/token/withdraw-v2.cjs +13 -9
  89. package/lib/protocols/token-factory/burn.cjs +4 -4
  90. package/lib/protocols/token-factory/create.cjs +2 -2
  91. package/lib/protocols/token-factory/mint.cjs +2 -2
  92. package/lib/protocols/token-factory/update.cjs +7 -5
  93. package/lib/protocols/trade/exchange-v2.cjs +68 -10
  94. package/lib/protocols/trade/exchange-v2.d.cts +3 -1
  95. package/lib/protocols/trade/transfer-v2.cjs +39 -23
  96. package/lib/protocols/trade/transfer-v3.cjs +101 -10
  97. package/lib/protocols/trade/transfer-v3.d.cts +11 -1
  98. package/lib/tx-timer.cjs +13 -0
  99. package/lib/tx-timer.d.cts +5 -0
  100. package/lib/util.cjs +2 -1
  101. package/package.json +16 -16
@@ -7,10 +7,10 @@ import verify_itx_address_default from "./pipes/verify-itx-address.mjs";
7
7
  import verify_itx_assets_default from "./pipes/verify-itx-assets.mjs";
8
8
  import verify_itx_variables_default from "./pipes/verify-itx-variables.mjs";
9
9
  import verify_mint_limit_default from "./pipes/verify-mint-limit.mjs";
10
- import { Runner, pipes } from "@ocap/tx-pipeline";
10
+ import { account, asset, delegation, factory } from "@ocap/state";
11
+ import { Runner, pipes, verifyAssetHierarchy } from "@ocap/tx-pipeline";
12
+ import { BN, isSameDid } from "@ocap/util";
11
13
  import { CustomError } from "@ocap/util/lib/error";
12
- import { account, asset, factory } from "@ocap/state";
13
- import { BN } from "@ocap/util";
14
14
  import Debug from "debug";
15
15
  import { getListField } from "@ocap/util/lib/get-list-field";
16
16
  import { schemas, v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
@@ -164,6 +164,53 @@ runner.use(pipes.VerifyAccountMigration({
164
164
  addressKey: "tx.from"
165
165
  }));
166
166
  runner.use(pipes.VerifyBlocked({ stateKeys: ["signerStates", "ownerState"] }));
167
+ const getAcquireAssetV3Requirements = (ctx, index, signature) => {
168
+ const { tx: tx$1, itx, inputs = [] } = ctx;
169
+ const signatures = getListField(tx$1, "signatures");
170
+ const targetOwner = signature?.signer || (typeof index === "number" ? signatures?.[index]?.signer : tx$1?.from);
171
+ if (!targetOwner) return [];
172
+ const input = inputs.find((x) => isSameDid(x.owner, targetOwner));
173
+ if (!input) return [];
174
+ const tokensList = getListField(input, "tokens");
175
+ const assetsList = getListField(input, "assets");
176
+ return [...tokensList.map((t) => ({
177
+ type: "token",
178
+ address: t.address,
179
+ value: t.value,
180
+ to: itx?.factory ? [itx.factory] : []
181
+ })), ...assetsList.map((a) => ({
182
+ type: "asset",
183
+ address: a,
184
+ to: itx?.factory ? [itx.factory] : []
185
+ }))];
186
+ };
187
+ runner.use(pipes.ExtractState({
188
+ from: "tx.delegator",
189
+ to: "delegatorState",
190
+ status: "OK",
191
+ table: "account"
192
+ }));
193
+ runner.use(pipes.VerifyAccountMigration({
194
+ stateKey: "delegatorState",
195
+ addressKey: "tx.delegator"
196
+ }));
197
+ runner.use(pipes.VerifyDelegation({
198
+ type: "signature",
199
+ signerKey: "senderState",
200
+ delegatorKey: "delegatorState",
201
+ getRequirements: (ctx) => getAcquireAssetV3Requirements(ctx)
202
+ }));
203
+ runner.use(pipes.ExtractSigner({
204
+ signerKey: "msSignerStates",
205
+ delegatorKey: "delegatorStates"
206
+ }));
207
+ runner.use(pipes.VerifyDelegation({
208
+ type: "multisig",
209
+ signerKey: "msSignerStates",
210
+ delegatorKey: "delegatorStates",
211
+ delegationKey: "delegationStates",
212
+ getRequirements: (ctx, index, sig) => getAcquireAssetV3Requirements(ctx, index, sig)
213
+ }));
167
214
  runner.use(pipes.ExtractState({
168
215
  from: "tokens",
169
216
  to: "tokenStates",
@@ -174,6 +221,12 @@ runner.use(pipes.VerifyTokenBalance({
174
221
  ownerKey: "signerStates",
175
222
  conditionKey: "inputs"
176
223
  }));
224
+ runner.use(pipes.verifyTokenAccess({
225
+ statesKey: "tokenStates",
226
+ listFieldKey: "spenders",
227
+ accountKeys: ["signerStates"],
228
+ errorMessage: "Account {address} is not allowed to use token {tokenAddress}"
229
+ }));
177
230
  runner.use(pipes.ExtractState({
178
231
  from: "assets",
179
232
  to: "assetStates",
@@ -206,6 +259,11 @@ runner.use(pipes.ExtractState({
206
259
  }));
207
260
  runner.use(verify_itx_address_default("acquire-v3"));
208
261
  runner.use(verify_itx_assets_default);
262
+ runner.use(async (context, next) => {
263
+ const { itx, ownerState, assets = [] } = context;
264
+ const owner = ownerState ? ownerState.address : itx.owner;
265
+ return next(await verifyAssetHierarchy([itx.address], owner, context, void 0, assets) || void 0);
266
+ });
209
267
  runner.use(ensure_gas_default((context) => {
210
268
  const result = {
211
269
  create: 1,
@@ -220,7 +278,7 @@ runner.use(ensure_gas_default((context) => {
220
278
  runner.use(ensure_cost_default({ attachSenderChanges: true }));
221
279
  runner.use(pipes.TakeStateSnapshot());
222
280
  runner.use(async (context, next) => {
223
- const { tx: tx$1, itx, txTime, inputs = [], statedb, senderState, ownerState, factoryState, factoryTokens = [], mintedAsset, mintedAddress, signerStates = [], senderChange, updateVaults, assetStates = [] } = context;
281
+ const { tx: tx$1, itx, txTime, inputs = [], statedb, senderState, ownerState, factoryState, factoryTokens = [], mintedAsset, mintedAddress, signerStates = [], senderChange, updateVaults, assetStates = [], delegationState, delegationStates = [] } = context;
224
282
  const signerUpdates = {};
225
283
  inputs.forEach((x) => {
226
284
  const { owner: owner$1 } = x;
@@ -234,11 +292,12 @@ runner.use(async (context, next) => {
234
292
  Object.assign(factoryUpdates, applyTokenUpdates(factoryTokens, factoryState, "add"));
235
293
  context.tokenUpdates = applyTokenUpdates(factoryTokens, { tokens: {} }, "add");
236
294
  }
237
- const [newSenderState, newReceiverState, assetState, newSignerStates, newFactoryState, newAssetStates] = await Promise.all([
238
- statedb.account.updateOrCreate(senderState ?? null, account.updateOrCreate(senderState ?? null, Object.assign({
295
+ const existingSenderState = senderState ? await statedb.account.get(senderState.address, context) : null;
296
+ const [newSenderState, newReceiverState, assetState, newSignerStates, newFactoryState, newAssetStates, newDelegationState, newDelegationStates] = await Promise.all([
297
+ statedb.account.updateOrCreate(existingSenderState ?? null, account.updateOrCreate(existingSenderState ?? null, Object.assign({
239
298
  address: tx$1.from,
240
299
  nonce: tx$1.nonce,
241
- pk: tx$1.pk
300
+ pk: tx$1.delegator ? senderState?.pk : tx$1.pk
242
301
  }, signerUpdates[tx$1.from] || (senderChange ? applyTokenChange(senderState, senderChange) : {})), context), context),
243
302
  ownerState || owner === tx$1.from ? Promise.resolve(ownerState) : statedb.account.create(owner, account.create({ address: owner }, context), context),
244
303
  statedb.asset.create(itx.address, asset.create({
@@ -246,9 +305,17 @@ runner.use(async (context, next) => {
246
305
  owner,
247
306
  address: mintedAddress
248
307
  }, context), context),
249
- Promise.all(signerStates.filter((x) => x.address !== tx$1.from).map((x) => statedb.account.update(x.address, account.update(x, signerUpdates[x.address], context), context))),
308
+ Promise.all(signerStates.filter((x) => x.address !== tx$1.from).map(async (x) => {
309
+ const existing = await statedb.account.get(x.address, context);
310
+ return statedb.account.updateOrCreate(existing ?? null, account.updateOrCreate(existing ?? null, {
311
+ address: x.address,
312
+ ...signerUpdates[x.address]
313
+ }, context), context);
314
+ })),
250
315
  statedb.factory.update(factoryState.address, factory.update(factoryState, factoryUpdates, context), context),
251
- Promise.all(assetStates.map((x) => statedb.asset.update(x.address, asset.update(x, { consumedTime: txTime }, context), context)))
316
+ Promise.all(assetStates.map((x) => statedb.asset.update(x.address, asset.update(x, { consumedTime: txTime }, context), context))),
317
+ context.isDelegationChanged && delegationState ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context) : delegationState,
318
+ context.isDelegationChanged && delegationStates.length ? Promise.all(delegationStates.map((x) => statedb.delegation.update(x.address, delegation.update(x, {}, context), context))) : delegationStates
252
319
  ]);
253
320
  if (updateVaults) await updateVaults();
254
321
  context.senderState = newSenderState;
@@ -257,6 +324,8 @@ runner.use(async (context, next) => {
257
324
  context.assetState = assetState;
258
325
  context.factoryState = newFactoryState;
259
326
  context.assetStates = newAssetStates;
327
+ context.delegationState = newDelegationState ?? void 0;
328
+ context.delegationStates = newDelegationStates;
260
329
  debug("acquire-v3", assetState);
261
330
  next();
262
331
  }, { persistError: true });
@@ -1,8 +1,8 @@
1
1
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
2
2
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
3
+ import { account, asset } from "@ocap/state";
3
4
  import { Runner, pipes } from "@ocap/tx-pipeline";
4
5
  import { CustomError } from "@ocap/util/lib/error";
5
- import { account, asset } from "@ocap/state";
6
6
  import Debug from "debug";
7
7
  import { v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
8
8
 
@@ -1,10 +1,10 @@
1
1
  import { decodeAnySafe, getDelegationRequirements } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
+ import { account, asset, delegation } from "@ocap/state";
4
5
  import { Runner, pipes } from "@ocap/tx-pipeline";
5
6
  import { CustomError } from "@ocap/util/lib/error";
6
7
  import { toAssetAddress } from "@arcblock/did-util";
7
- import { account, asset, delegation } from "@ocap/state";
8
8
  import Debug from "debug";
9
9
  import { formatMessage } from "@ocap/message";
10
10
  import { getListField } from "@ocap/util/lib/get-list-field";
@@ -4,9 +4,9 @@ import verify_itx_address_default from "./pipes/verify-itx-address.mjs";
4
4
  import verify_itx_assets_default from "./pipes/verify-itx-assets.mjs";
5
5
  import verify_itx_variables_default from "./pipes/verify-itx-variables.mjs";
6
6
  import verify_mint_limit_default from "./pipes/verify-mint-limit.mjs";
7
- import { Runner, pipes } from "@ocap/tx-pipeline";
8
- import { CustomError } from "@ocap/util/lib/error";
9
7
  import { account, asset, factory } from "@ocap/state";
8
+ import { Runner, pipes, verifyAssetHierarchy } from "@ocap/tx-pipeline";
9
+ import { CustomError } from "@ocap/util/lib/error";
10
10
  import Debug from "debug";
11
11
  import { schemas, v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
12
12
 
@@ -116,6 +116,12 @@ runner.use(pipes.VerifyUpdater({
116
116
  ownerKey: "ownerState"
117
117
  }));
118
118
  runner.use(verify_itx_assets_default);
119
+ runner.use(async (context, next) => {
120
+ const { itx, ownerAddress, assetStates = [] } = context;
121
+ const owner = ownerAddress || itx.owner;
122
+ const consumedAssets = assetStates.map((x) => x.address);
123
+ return next(await verifyAssetHierarchy([itx.address], owner, context, void 0, consumedAssets) || void 0);
124
+ });
119
125
  runner.use(ensure_gas_default((context) => {
120
126
  const result = {
121
127
  create: 1,
@@ -44,7 +44,7 @@ const verify_itx_address = (mode) => (context, next) => {
44
44
  senderState,
45
45
  delegatorState
46
46
  });
47
- return next(new CustomError("INVALID_ASSET", "Invalid itx.address: does not match with minted asset address"));
47
+ return next(new CustomError("INVALID_ASSET", `Invalid itx.address: does not match with minted asset address${mode === "acquire-v2" || mode === "acquire-v3" ? " (check that itx.issuer.id/itx.issuer.pk match the trusted issuer's actual on-chain public key — a stale or wrong issuer.pk is the most common cause)" : ""}`));
48
48
  }
49
49
  context.ownerAddress = owner.address;
50
50
  context.mintedAsset = minted.asset;
@@ -1,9 +1,9 @@
1
1
  import { decodeAnySafe } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
+ import { account, asset } from "@ocap/state";
4
5
  import { Runner, pipes } from "@ocap/tx-pipeline";
5
6
  import { CustomError } from "@ocap/util/lib/error";
6
- import { account, asset } from "@ocap/state";
7
7
  import Debug from "debug";
8
8
  import { v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
9
9
  import { diff } from "deep-diff";
@@ -1,12 +1,12 @@
1
1
  import { decodeAnySafe, getDelegationRequirements } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
+ import { account, delegation, factory } from "@ocap/state";
4
5
  import { Runner, pipes } from "@ocap/tx-pipeline";
6
+ import { BN } from "@ocap/util";
5
7
  import { CustomError } from "@ocap/util/lib/error";
6
8
  import get from "lodash/get.js";
7
9
  import { toFactoryAddress } from "@arcblock/did-util";
8
- import { account, delegation, factory } from "@ocap/state";
9
- import { BN } from "@ocap/util";
10
10
  import Debug from "debug";
11
11
  import { formatMessage } from "@ocap/message";
12
12
  import cloneDeep from "lodash/cloneDeep.js";
@@ -111,7 +111,8 @@ runner.use(pipes.ExtractState({
111
111
  from: "factoryProps.trustedIssuers",
112
112
  to: "issuerStates",
113
113
  table: "account",
114
- status: "INVALID_ISSUER_STATE"
114
+ status: "INVALID_ISSUER_STATE",
115
+ message: (address) => `Trusted issuer account has no on-chain record yet: ${address}. The wallet must appear on chain (e.g. send or receive a transaction) before it can be used as a trusted issuer.`
115
116
  }));
116
117
  runner.use(pipes.VerifyAccountMigration({
117
118
  stateKey: "issuerStates",
@@ -1,10 +1,10 @@
1
1
  import { applyTokenChange, applyTokenUpdates } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
- import { Runner, pipes } from "@ocap/tx-pipeline";
5
- import { CustomError } from "@ocap/util/lib/error";
6
4
  import { account, asset, evidence, stake } from "@ocap/state";
5
+ import { Runner, pipes } from "@ocap/tx-pipeline";
7
6
  import { BN } from "@ocap/util";
7
+ import { CustomError } from "@ocap/util/lib/error";
8
8
  import Debug from "debug";
9
9
  import { patterns, v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
10
10
  import { getRelatedAddresses } from "@ocap/util/lib/get-related-addr";
@@ -1,10 +1,10 @@
1
1
  import { applyTokenChange, applyTokenUpdates } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
- import { Runner, pipes } from "@ocap/tx-pipeline";
5
- import { CustomError } from "@ocap/util/lib/error";
6
4
  import { account, asset, stake } from "@ocap/state";
5
+ import { Runner, pipes } from "@ocap/tx-pipeline";
7
6
  import { BN } from "@ocap/util";
7
+ import { CustomError } from "@ocap/util/lib/error";
8
8
  import Debug from "debug";
9
9
  import { getListField } from "@ocap/util/lib/get-list-field";
10
10
  import { schemas, v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
@@ -1,10 +1,10 @@
1
1
  import { applyTokenUpdates } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
- import { Runner, pipes } from "@ocap/tx-pipeline";
5
- import { CustomError } from "@ocap/util/lib/error";
6
4
  import { account, stake } from "@ocap/state";
5
+ import { Runner, pipes } from "@ocap/tx-pipeline";
7
6
  import { BN } from "@ocap/util";
7
+ import { CustomError } from "@ocap/util/lib/error";
8
8
  import Debug from "debug";
9
9
  import { getListField } from "@ocap/util/lib/get-list-field";
10
10
  import { schemas, v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
@@ -1,10 +1,10 @@
1
1
  import { applyTokenChange, applyTokenUpdates } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
- import { Runner, pipes } from "@ocap/tx-pipeline";
5
- import { CustomError } from "@ocap/util/lib/error";
6
4
  import { account, asset, stake } from "@ocap/state";
5
+ import { Runner, pipes } from "@ocap/tx-pipeline";
7
6
  import { BN } from "@ocap/util";
7
+ import { CustomError } from "@ocap/util/lib/error";
8
8
  import Debug from "debug";
9
9
  import { getListField } from "@ocap/util/lib/get-list-field";
10
10
  import { schemas, v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
@@ -1,12 +1,12 @@
1
1
  import { applyTokenChange, applyTokenUpdates, isGasStakeAddress, isGasStakeInput } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
+ import { account, asset, stake } from "@ocap/state";
4
5
  import { Runner, pipes } from "@ocap/tx-pipeline";
6
+ import { BN, fromTokenToUnit } from "@ocap/util";
5
7
  import { CustomError } from "@ocap/util/lib/error";
6
8
  import pick from "lodash/pick.js";
7
9
  import { toStakeAddress } from "@arcblock/did-util";
8
- import { account, asset, stake } from "@ocap/state";
9
- import { BN, fromTokenToUnit } from "@ocap/util";
10
10
  import Debug from "debug";
11
11
  import { getListField } from "@ocap/util/lib/get-list-field";
12
12
  import { schemas, v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
@@ -4,11 +4,11 @@ import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
4
  import ensure_validator_default from "./pipes/ensure-validator.mjs";
5
5
  import verify_signers_default from "./pipes/verify-signers.mjs";
6
6
  import verify_status_default from "./pipes/verify-status.mjs";
7
+ import { account, evidence, rollupBlock, stake } from "@ocap/state";
7
8
  import { Runner, pipes } from "@ocap/tx-pipeline";
9
+ import { BN } from "@ocap/util";
8
10
  import { CustomError } from "@ocap/util/lib/error";
9
11
  import { toStakeAddress } from "@arcblock/did-util";
10
- import { account, evidence, rollupBlock, stake } from "@ocap/state";
11
- import { BN } from "@ocap/util";
12
12
  import Debug from "debug";
13
13
  import cloneDeep from "lodash/cloneDeep.js";
14
14
  import groupBy from "lodash/groupBy.js";
@@ -187,7 +187,7 @@ runner.use((context, next) => {
187
187
  });
188
188
  const txSignatures = txData.signatures;
189
189
  const blockSignatures = blockState.signatures;
190
- const validators = txSignatures.map((s) => s.signer).concat(blockSignatures.map((s) => s.signer));
190
+ const validators = Array.from(new Set(txSignatures.map((s) => s.signer).concat(blockSignatures.map((s) => s.signer))));
191
191
  const validatorShare = validator.div(new BN(validators.length));
192
192
  validators.forEach((v$1) => changes.account.push({
193
193
  address: v$1,
@@ -200,7 +200,8 @@ runner.use((context, next) => {
200
200
  delta: proposer.toString(10),
201
201
  action: "fee"
202
202
  });
203
- const validators = blockState.signatures.map((s) => s.signer);
203
+ const blockSignatures = blockState.signatures;
204
+ const validators = Array.from(new Set(blockSignatures.map((s) => s.signer)));
204
205
  const validatorShares = splitTxFee({
205
206
  total: validator.toString(10),
206
207
  shares: validators.reduce((acc, signer) => {
@@ -3,10 +3,10 @@ import ensure_gas_default from "../../pipes/ensure-gas.mjs";
3
3
  import ensure_validator_default from "./pipes/ensure-validator.mjs";
4
4
  import verify_signers_default from "./pipes/verify-signers.mjs";
5
5
  import verify_status_default from "./pipes/verify-status.mjs";
6
+ import { account, rollup, stake } from "@ocap/state";
6
7
  import { Runner, pipes } from "@ocap/tx-pipeline";
7
8
  import { CustomError } from "@ocap/util/lib/error";
8
9
  import { toStakeAddress } from "@arcblock/did-util";
9
- import { account, rollup, stake } from "@ocap/state";
10
10
  import { v, vBN, vBNPositive, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
11
11
 
12
12
  //#region src/protocols/rollup/close.ts
@@ -5,11 +5,11 @@ import ensure_validator_default from "./pipes/ensure-validator.mjs";
5
5
  import verify_signers_default from "./pipes/verify-signers.mjs";
6
6
  import verify_status_default from "./pipes/verify-status.mjs";
7
7
  import verify_evidence_default from "./pipes/verify-evidence.mjs";
8
+ import { account, rollup, rollupBlock, stake, tx } from "@ocap/state";
8
9
  import { Runner, pipes } from "@ocap/tx-pipeline";
9
10
  import { CustomError } from "@ocap/util/lib/error";
10
11
  import pick from "lodash/pick.js";
11
12
  import { toStakeAddress } from "@arcblock/did-util";
12
- import { account, rollup, rollupBlock, stake, tx } from "@ocap/state";
13
13
  import Debug from "debug";
14
14
  import { formatMessage } from "@ocap/message";
15
15
  import { getListField } from "@ocap/util/lib/get-list-field";
@@ -1,11 +1,11 @@
1
1
  import { decodeAnySafe, getDelegationRequirements } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
+ import { account, delegation, rollup } from "@ocap/state";
4
5
  import { Runner, pipes } from "@ocap/tx-pipeline";
5
6
  import { CustomError } from "@ocap/util/lib/error";
6
7
  import { isEthereumDid, isFromPublicKey } from "@arcblock/did";
7
8
  import { toRollupAddress } from "@arcblock/did-util";
8
- import { account, delegation, rollup } from "@ocap/state";
9
9
  import Debug from "debug";
10
10
  import { formatMessage } from "@ocap/message";
11
11
  import { getListField } from "@ocap/util/lib/get-list-field";
@@ -5,12 +5,12 @@ import verify_signers_default from "./pipes/verify-signers.mjs";
5
5
  import verify_status_default from "./pipes/verify-status.mjs";
6
6
  import verify_evidence_default from "./pipes/verify-evidence.mjs";
7
7
  import { any, check, minLength, nullable, object, optional, pipe, regex, string } from "../../node_modules/valibot/dist/index.mjs";
8
+ import { account, evidence, rollup, stake } from "@ocap/state";
8
9
  import { Runner, pipes } from "@ocap/tx-pipeline";
10
+ import { BN, toHex } from "@ocap/util";
9
11
  import { CustomError } from "@ocap/util/lib/error";
10
12
  import { isEthereumDid } from "@arcblock/did";
11
13
  import { toStakeAddress } from "@arcblock/did-util";
12
- import { account, evidence, rollup, stake } from "@ocap/state";
13
- import { BN, toHex } from "@ocap/util";
14
14
  import Debug from "debug";
15
15
  import { patterns, schemas, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
16
16
  import MerkleTree from "@ocap/merkle-tree";
@@ -5,10 +5,10 @@ import verify_signers_default from "./pipes/verify-signers.mjs";
5
5
  import verify_status_default from "./pipes/verify-status.mjs";
6
6
  import verify_evidence_default from "./pipes/verify-evidence.mjs";
7
7
  import { any, minLength, nullable, object, optional, pipe, regex, string } from "../../node_modules/valibot/dist/index.mjs";
8
+ import { account, evidence, rollup, stake } from "@ocap/state";
8
9
  import { Runner, pipes } from "@ocap/tx-pipeline";
9
10
  import { CustomError } from "@ocap/util/lib/error";
10
11
  import { toStakeAddress } from "@arcblock/did-util";
11
- import { account, evidence, rollup, stake } from "@ocap/state";
12
12
  import Debug from "debug";
13
13
  import { patterns, schemas, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
14
14
  import MerkleTree from "@ocap/merkle-tree";
@@ -3,9 +3,9 @@ import ensure_gas_default from "../../pipes/ensure-gas.mjs";
3
3
  import ensure_validator_default from "./pipes/ensure-validator.mjs";
4
4
  import verify_signers_default from "./pipes/verify-signers.mjs";
5
5
  import verify_status_default from "./pipes/verify-status.mjs";
6
+ import { account, rollup } from "@ocap/state";
6
7
  import { Runner, pipes } from "@ocap/tx-pipeline";
7
8
  import { CustomError } from "@ocap/util/lib/error";
8
- import { account, rollup } from "@ocap/state";
9
9
  import { v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
10
10
 
11
11
  //#region src/protocols/rollup/migrate.ts
@@ -3,9 +3,9 @@ import ensure_gas_default from "../../pipes/ensure-gas.mjs";
3
3
  import ensure_validator_default from "./pipes/ensure-validator.mjs";
4
4
  import verify_signers_default from "./pipes/verify-signers.mjs";
5
5
  import verify_status_default from "./pipes/verify-status.mjs";
6
+ import { account, rollup } from "@ocap/state";
6
7
  import { Runner, pipes } from "@ocap/tx-pipeline";
7
8
  import { CustomError } from "@ocap/util/lib/error";
8
- import { account, rollup } from "@ocap/state";
9
9
  import { v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
10
10
 
11
11
  //#region src/protocols/rollup/pause.ts
@@ -1,7 +1,8 @@
1
+ import { toHex } from "@ocap/util";
1
2
  import { CustomError } from "@ocap/util/lib/error";
2
3
  import get from "lodash/get.js";
3
4
  import { toTypeInfo } from "@arcblock/did";
4
- import { toHex } from "@ocap/util";
5
+ import { getListField } from "@ocap/util/lib/get-list-field";
5
6
  import { fromPublicKey as fromPublicKey$1 } from "@ocap/wallet";
6
7
 
7
8
  //#region src/protocols/rollup/pipes/verify-evidence.ts
@@ -9,7 +10,9 @@ function CreateVerifyEvidencePipe({ evidenceKey, signaturesKey, verifyMethod = "
9
10
  if (["verify", "ethVerify"].includes(verifyMethod) === false) throw new CustomError("INVALID_ARGUMENT", `Invalid verify method: ${verifyMethod}, supported methods are: verify, ethVerify`);
10
11
  return async (context, next) => {
11
12
  const evidence = get(context, evidenceKey);
12
- const signatures = get(context, signaturesKey);
13
+ const signatures = getListField(context, signaturesKey);
14
+ const signers = signatures.map((s) => s.signer);
15
+ if (new Set(signers).size !== signers.length) return next(new CustomError("INVALID_SIGNATURE", `Duplicate signers in ${signaturesKey} are not allowed`));
13
16
  for (const sig of signatures) {
14
17
  const { signer, pk, delegator, signature } = sig;
15
18
  const address = delegator || signer;
@@ -1,5 +1,5 @@
1
1
  import { CustomError } from "@ocap/util/lib/error";
2
- import get from "lodash/get.js";
2
+ import { getListField } from "@ocap/util/lib/get-list-field";
3
3
 
4
4
  //#region src/protocols/rollup/pipes/verify-signers.ts
5
5
  function CreateVerifySignersPipe({ signersKey, allowSender = true, allowShrink = false }) {
@@ -7,14 +7,15 @@ function CreateVerifySignersPipe({ signersKey, allowSender = true, allowShrink =
7
7
  const { tx, rollupState } = context;
8
8
  const rollup = rollupState;
9
9
  const { minSignerCount, maxSignerCount } = rollup;
10
- const signatures = get(context, signersKey) ?? [];
10
+ const signatures = getListField(context, signersKey);
11
11
  const signerCount = signatures.length;
12
+ if (signatures.some((x) => Boolean(x.delegator))) return next(new CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, delegation is not allowed`));
12
13
  const seedValidators = (rollup.seedValidators ?? []).map((x) => x.address);
13
14
  const validators = (rollup.validators ?? []).map((x) => x.address);
14
15
  if (!allowSender && signatures.some((x) => x.signer === tx.from)) return next(new CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, sender not allowed to sign`));
16
+ if (signerCount > maxSignerCount) return next(new CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, Expect at most ${maxSignerCount} signatures, got ${signerCount}`));
15
17
  if (validators.length > minSignerCount) {
16
18
  if (signerCount < minSignerCount) return next(new CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, expect at least ${minSignerCount} signatures, got ${signerCount}`));
17
- if (signerCount > maxSignerCount) return next(new CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, Expect at most ${maxSignerCount} signatures, got ${signerCount}`));
18
19
  } else {
19
20
  let actualValidatorCount = validators.length;
20
21
  if (!allowSender && allowShrink) actualValidatorCount -= 1;
@@ -22,6 +23,7 @@ function CreateVerifySignersPipe({ signersKey, allowSender = true, allowShrink =
22
23
  if (signerCount < expectedSignerCount) return next(new CustomError("INVALID_SIGNATURE", `Invalid ${signersKey}, all validators are expected to sign because we have less than ${expectedSignerCount} validators`));
23
24
  }
24
25
  const signers = signatures.map((x) => x.signer);
26
+ if (new Set(signers).size !== signers.length) return next(new CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, duplicate signers are not allowed`));
25
27
  const missingSigner = seedValidators.find((x) => signers.includes(x) === false);
26
28
  if (missingSigner) return next(new CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, missing signature from seed validator: ${missingSigner}`));
27
29
  const invalidSigner = signers.find((x) => validators.includes(x) === false);
@@ -3,9 +3,9 @@ import ensure_gas_default from "../../pipes/ensure-gas.mjs";
3
3
  import ensure_validator_default from "./pipes/ensure-validator.mjs";
4
4
  import verify_signers_default from "./pipes/verify-signers.mjs";
5
5
  import verify_status_default from "./pipes/verify-status.mjs";
6
+ import { account, rollup } from "@ocap/state";
6
7
  import { Runner, pipes } from "@ocap/tx-pipeline";
7
8
  import { CustomError } from "@ocap/util/lib/error";
8
- import { account, rollup } from "@ocap/state";
9
9
  import { v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
10
10
 
11
11
  //#region src/protocols/rollup/resume.ts
@@ -4,10 +4,10 @@ import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
4
  import ensure_validator_default from "./pipes/ensure-validator.mjs";
5
5
  import verify_signers_default from "./pipes/verify-signers.mjs";
6
6
  import verify_status_default from "./pipes/verify-status.mjs";
7
- import { Runner, pipes } from "@ocap/tx-pipeline";
8
- import { CustomError } from "@ocap/util/lib/error";
9
7
  import { account, rollup } from "@ocap/state";
8
+ import { Runner, pipes } from "@ocap/tx-pipeline";
10
9
  import { BN } from "@ocap/util";
10
+ import { CustomError } from "@ocap/util/lib/error";
11
11
  import Debug from "debug";
12
12
  import { v, vBN, vBNGreater, vBNPositive, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
13
13
 
@@ -22,9 +22,9 @@ const schema = v.pipe(v.object({
22
22
  minStakeAmount: vBN(vBNGreater(0)),
23
23
  maxStakeAmount: vBN(vBNGreater(0)),
24
24
  minSignerCount: v.pipe(v.number(), v.integer()),
25
- maxSignerCount: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(8)),
25
+ maxSignerCount: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(16)),
26
26
  minBlockSize: v.pipe(v.number(), v.integer(), v.minValue(1)),
27
- maxBlockSize: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(32)),
27
+ maxBlockSize: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(256)),
28
28
  minBlockInterval: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(3600)),
29
29
  minBlockConfirmation: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(100)),
30
30
  minDepositAmount: vBN(vBNPositive()),
@@ -1,11 +1,11 @@
1
1
  import { decodeAnySafe, getDelegationRequirements } from "../../util.mjs";
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
+ import { account, delegation, token } from "@ocap/state";
4
5
  import { Runner, pipes } from "@ocap/tx-pipeline";
6
+ import { BN, fromTokenToUnit } from "@ocap/util";
5
7
  import { CustomError } from "@ocap/util/lib/error";
6
8
  import { toTokenAddress } from "@arcblock/did-util";
7
- import { account, delegation, token } from "@ocap/state";
8
- import { BN, fromTokenToUnit } from "@ocap/util";
9
9
  import Debug from "debug";
10
10
  import cloneDeep from "lodash/cloneDeep.js";
11
11
  import { schemas, v, vBN, vBNGreater, vBNMax, vDID, vForbidden, vStripUnknown, vValidate } from "@arcblock/validator";
@@ -3,11 +3,11 @@ import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
4
  import verify_signers_default from "../rollup/pipes/verify-signers.mjs";
5
5
  import verify_status_default from "../rollup/pipes/verify-status.mjs";
6
+ import { account, evidence, stake } from "@ocap/state";
6
7
  import { Runner, pipes } from "@ocap/tx-pipeline";
8
+ import { BN, fromUnitToToken } from "@ocap/util";
7
9
  import { CustomError } from "@ocap/util/lib/error";
8
10
  import { toStakeAddress } from "@arcblock/did-util";
9
- import { account, evidence, stake } from "@ocap/state";
10
- import { BN, fromUnitToToken } from "@ocap/util";
11
11
  import Debug from "debug";
12
12
  import { getListField } from "@ocap/util/lib/get-list-field";
13
13
  import { patterns, schemas, v, vBN, vBNMin, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
@@ -2,11 +2,11 @@ import { applyTokenChange, applyTokenUpdates, getBNSum, getRewardLocker, getTxFe
2
2
  import ensure_cost_default from "../../pipes/ensure-cost.mjs";
3
3
  import ensure_gas_default from "../../pipes/ensure-gas.mjs";
4
4
  import verify_status_default from "../rollup/pipes/verify-status.mjs";
5
+ import { account, stake } from "@ocap/state";
5
6
  import { Runner, pipes } from "@ocap/tx-pipeline";
7
+ import { BN, fromUnitToToken } from "@ocap/util";
6
8
  import { CustomError } from "@ocap/util/lib/error";
7
9
  import { toStakeAddress } from "@arcblock/did-util";
8
- import { account, stake } from "@ocap/state";
9
- import { BN, fromUnitToToken } from "@ocap/util";
10
10
  import Debug from "debug";
11
11
  import { getListField } from "@ocap/util/lib/get-list-field";
12
12
  import { schemas, v, vBN, vBNMin, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
@@ -87,20 +87,24 @@ runner.use(pipes.VerifyTokenBalance({
87
87
  runner.use((context, next) => {
88
88
  const { itx, rollupState, tokenState } = context;
89
89
  const { withdrawFeeRate, maxWithdrawFee, minWithdrawFee } = rollupState;
90
+ const { reward } = getTxFee({
91
+ amount: itx.token.value,
92
+ feeRate: withdrawFeeRate,
93
+ maxFee: maxWithdrawFee,
94
+ minFee: minWithdrawFee
95
+ });
90
96
  if (new BN(itx.maxFee).isZero()) {
91
- const { reward } = getTxFee({
92
- amount: itx.token.value,
93
- feeRate: withdrawFeeRate,
94
- maxFee: maxWithdrawFee,
95
- minFee: minWithdrawFee
96
- });
97
97
  if (new BN(itx.actualFee).lt(new BN(reward))) return next(new CustomError("INVALID_TX", `itx.actualFee too low, expect at least ${fromUnitToToken(reward, tokenState.decimal)}, got ${fromUnitToToken(itx.actualFee, tokenState.decimal)}`));
98
+ } else {
99
+ if (!new BN(itx.actualFee || 0).isZero()) return next(new CustomError("INVALID_TX", "itx.actualFee must be 0 when itx.maxFee is specified"));
100
+ if (new BN(itx.maxFee).lt(new BN(reward))) return next(new CustomError("INVALID_TX", `itx.maxFee too low, expect at least ${fromUnitToToken(reward, tokenState.decimal)}, got ${fromUnitToToken(itx.maxFee, tokenState.decimal)}`));
98
101
  }
99
102
  return next();
100
103
  });
101
104
  runner.use((context, next) => {
102
- const { tx: tx$1, itx } = context;
105
+ const { tx: tx$1, itx, rollupState } = context;
103
106
  if (new BN(itx.maxFee).isZero()) return next();
107
+ if ((rollupState.validators ?? []).map((x) => x.address).includes(itx.proposer) === false) return next(new CustomError("INVALID_TX", `itx.proposer ${itx.proposer} does not exist in validator whitelist`));
104
108
  const signatures = getListField(tx$1, "signatures");
105
109
  if (signatures.length !== 1) return next(new CustomError("INVALID_TX", "Withdraw with maxFee should have one multi-sig"));
106
110
  if (signatures.some((x) => x.signer === itx.proposer) === false) return next(new CustomError("INVALID_TX", "itx.proposer must exist in tx.signatures"));