@ocap/tx-protocols 1.30.26 → 1.31.0

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 (118) hide show
  1. package/esm/execute.d.mts +7 -0
  2. package/esm/execute.mjs +196 -71
  3. package/esm/gas-security-guard.d.mts +374 -0
  4. package/esm/gas-security-guard.mjs +1101 -0
  5. package/esm/index.d.mts +35 -5
  6. package/esm/index.mjs +3 -1
  7. package/esm/pipes/ensure-cost.mjs +314 -58
  8. package/esm/protocols/account/delegate.mjs +64 -16
  9. package/esm/protocols/account/migrate.d.mts +3 -0
  10. package/esm/protocols/account/migrate.mjs +235 -35
  11. package/esm/protocols/account/revoke-delegate.d.mts +3 -3
  12. package/esm/protocols/account/revoke-delegate.mjs +70 -38
  13. package/esm/protocols/asset/acquire-v2.mjs +29 -12
  14. package/esm/protocols/asset/acquire-v3.mjs +78 -22
  15. package/esm/protocols/asset/calls/transfer-token.mjs +2 -1
  16. package/esm/protocols/asset/calls/transfer.mjs +1 -0
  17. package/esm/protocols/asset/consume.mjs +12 -4
  18. package/esm/protocols/asset/create.mjs +18 -5
  19. package/esm/protocols/asset/mint.mjs +23 -1
  20. package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +21 -1
  21. package/esm/protocols/asset/pipes/exec-mint-hook.mjs +115 -1
  22. package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +14 -8
  23. package/esm/protocols/asset/pipes/verify-itx-address.mjs +9 -0
  24. package/esm/protocols/asset/pipes/verify-itx-assets.mjs +21 -5
  25. package/esm/protocols/asset/update.d.mts +13 -2
  26. package/esm/protocols/asset/update.mjs +133 -14
  27. package/esm/protocols/factory/create.mjs +14 -6
  28. package/esm/protocols/governance/claim-stake.mjs +92 -10
  29. package/esm/protocols/governance/return-stake.mjs +44 -12
  30. package/esm/protocols/governance/revoke-stake.mjs +17 -5
  31. package/esm/protocols/governance/slash-stake.mjs +64 -25
  32. package/esm/protocols/governance/stake.mjs +63 -13
  33. package/esm/protocols/rollup/claim-reward.mjs +39 -20
  34. package/esm/protocols/rollup/close.mjs +1 -1
  35. package/esm/protocols/rollup/create-block.mjs +8 -7
  36. package/esm/protocols/rollup/create.mjs +17 -6
  37. package/esm/protocols/rollup/join.mjs +3 -3
  38. package/esm/protocols/rollup/leave.mjs +1 -1
  39. package/esm/protocols/rollup/migrate.mjs +7 -4
  40. package/esm/protocols/rollup/pipes/verify-evidence.mjs +12 -4
  41. package/esm/protocols/rollup/update.mjs +6 -1
  42. package/esm/protocols/token/create.mjs +23 -12
  43. package/esm/protocols/token/deposit-v2.d.mts +5 -1
  44. package/esm/protocols/token/deposit-v2.mjs +81 -29
  45. package/esm/protocols/token/withdraw-v2.mjs +33 -16
  46. package/esm/protocols/token-factory/burn.mjs +43 -10
  47. package/esm/protocols/token-factory/create.mjs +23 -14
  48. package/esm/protocols/token-factory/mint.mjs +54 -21
  49. package/esm/protocols/token-factory/pipes/verify-icon.mjs +1 -1
  50. package/esm/protocols/token-factory/update.mjs +16 -6
  51. package/esm/protocols/trade/exchange-v2.d.mts +4 -0
  52. package/esm/protocols/trade/exchange-v2.mjs +78 -22
  53. package/esm/protocols/trade/transfer-v2.mjs +44 -13
  54. package/esm/protocols/trade/transfer-v3.mjs +72 -31
  55. package/esm/util.d.mts +3 -1
  56. package/esm/util.mjs +26 -17
  57. package/lib/execute.cjs +193 -67
  58. package/lib/execute.d.cts +7 -0
  59. package/lib/gas-security-guard.cjs +1140 -0
  60. package/lib/gas-security-guard.d.cts +374 -0
  61. package/lib/index.cjs +122 -1
  62. package/lib/index.d.cts +36 -6
  63. package/lib/pipes/ensure-cost.cjs +313 -57
  64. package/lib/protocols/account/delegate.cjs +64 -16
  65. package/lib/protocols/account/migrate.cjs +235 -35
  66. package/lib/protocols/account/migrate.d.cts +3 -0
  67. package/lib/protocols/account/revoke-delegate.cjs +71 -39
  68. package/lib/protocols/account/revoke-delegate.d.cts +3 -3
  69. package/lib/protocols/asset/acquire-v2.cjs +28 -11
  70. package/lib/protocols/asset/acquire-v3.cjs +77 -20
  71. package/lib/protocols/asset/calls/transfer-token.cjs +3 -2
  72. package/lib/protocols/asset/calls/transfer.cjs +1 -0
  73. package/lib/protocols/asset/consume.cjs +11 -3
  74. package/lib/protocols/asset/create.cjs +19 -6
  75. package/lib/protocols/asset/mint.cjs +23 -1
  76. package/lib/protocols/asset/pipes/exec-mint-hook.cjs +117 -1
  77. package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +21 -1
  78. package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +14 -9
  79. package/lib/protocols/asset/pipes/verify-itx-address.cjs +9 -0
  80. package/lib/protocols/asset/pipes/verify-itx-assets.cjs +21 -5
  81. package/lib/protocols/asset/update.cjs +131 -12
  82. package/lib/protocols/asset/update.d.cts +13 -2
  83. package/lib/protocols/factory/create.cjs +14 -6
  84. package/lib/protocols/governance/claim-stake.cjs +92 -9
  85. package/lib/protocols/governance/claim-stake.d.cts +1 -1
  86. package/lib/protocols/governance/return-stake.cjs +44 -11
  87. package/lib/protocols/governance/return-stake.d.cts +1 -1
  88. package/lib/protocols/governance/revoke-stake.cjs +17 -5
  89. package/lib/protocols/governance/revoke-stake.d.cts +1 -1
  90. package/lib/protocols/governance/slash-stake.cjs +64 -24
  91. package/lib/protocols/governance/slash-stake.d.cts +1 -1
  92. package/lib/protocols/governance/stake.cjs +65 -14
  93. package/lib/protocols/rollup/claim-reward.cjs +39 -20
  94. package/lib/protocols/rollup/close.cjs +1 -1
  95. package/lib/protocols/rollup/create-block.cjs +9 -8
  96. package/lib/protocols/rollup/create-block.d.cts +1 -1
  97. package/lib/protocols/rollup/create.cjs +18 -7
  98. package/lib/protocols/rollup/join.cjs +3 -3
  99. package/lib/protocols/rollup/leave.cjs +1 -1
  100. package/lib/protocols/rollup/migrate.cjs +7 -4
  101. package/lib/protocols/rollup/pipes/verify-evidence.cjs +11 -3
  102. package/lib/protocols/rollup/update.cjs +6 -1
  103. package/lib/protocols/token/create.cjs +24 -13
  104. package/lib/protocols/token/deposit-v2.cjs +80 -28
  105. package/lib/protocols/token/deposit-v2.d.cts +5 -1
  106. package/lib/protocols/token/withdraw-v2.cjs +32 -15
  107. package/lib/protocols/token-factory/burn.cjs +42 -9
  108. package/lib/protocols/token-factory/create.cjs +24 -15
  109. package/lib/protocols/token-factory/mint.cjs +53 -20
  110. package/lib/protocols/token-factory/pipes/verify-icon.cjs +1 -1
  111. package/lib/protocols/token-factory/update.cjs +16 -6
  112. package/lib/protocols/trade/exchange-v2.cjs +76 -20
  113. package/lib/protocols/trade/exchange-v2.d.cts +4 -0
  114. package/lib/protocols/trade/transfer-v2.cjs +45 -14
  115. package/lib/protocols/trade/transfer-v3.cjs +72 -30
  116. package/lib/util.cjs +26 -17
  117. package/lib/util.d.cts +4 -2
  118. package/package.json +16 -16
@@ -4,13 +4,13 @@ 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
+ import { toStakeAddress } from "@arcblock/did-util";
8
+ import { account, evidence, rollup, rollupBlock, stake } from "@ocap/state";
8
9
  import { Runner, pipes } from "@ocap/tx-pipeline";
9
10
  import { BN } from "@ocap/util";
10
11
  import { CustomError } from "@ocap/util/lib/error";
11
- import { toStakeAddress } from "@arcblock/did-util";
12
- import Debug from "debug";
13
12
  import cloneDeep from "lodash/cloneDeep.js";
13
+ import Debug from "debug";
14
14
  import groupBy from "lodash/groupBy.js";
15
15
  import { patterns, v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
16
16
  import uniqBy from "lodash/uniqBy.js";
@@ -26,10 +26,7 @@ const schema = v.pipe(v.object({
26
26
  blockHeight: v.pipe(v.number(), v.integer(), v.check((n) => n > 0, "Must be > 0")),
27
27
  blockHash: v.pipe(v.string(), v.regex(patterns.txHash)),
28
28
  evidence: v.object({ hash: v.pipe(v.string(), v.regex(patterns.txHash)) }),
29
- publisher: vDID({
30
- prefix: true,
31
- wallet: "ethereum"
32
- }),
29
+ publisher: vDID({ prefix: true }),
33
30
  data: v.optional(v.nullable(v.any()))
34
31
  }), vStripUnknown());
35
32
  runner.use((context, next) => {
@@ -63,6 +60,14 @@ runner.use(pipes.ExtractState({
63
60
  status: "INVALID_ROLLUP",
64
61
  table: "rollup"
65
62
  }));
63
+ runner.use((context, next) => {
64
+ const { itx, rollupState } = context;
65
+ if (rollup.getRollupTargetType(rollupState) === "EVM" && vValidate(vDID({
66
+ prefix: true,
67
+ wallet: "ethereum"
68
+ }), itx.publisher).error) return next(new CustomError("INVALID_TX", "\"publisher\" Expected wallet type to be \"ethereum\""));
69
+ return next();
70
+ });
66
71
  runner.use(ensure_validator_default(false));
67
72
  runner.use(verify_status_default({ closed: false }));
68
73
  runner.use(pipes.ExtractState({
@@ -146,23 +151,27 @@ runner.use((context, next) => {
146
151
  const slashShare = slashAmount.div(new BN(2));
147
152
  changes.stake.push({
148
153
  address: stakeState.address,
154
+ token: tokenState.address,
149
155
  delta: `-${slashAmount.toString(10)}`,
150
156
  action: "slash"
151
157
  });
152
158
  changes.stake.push({
153
159
  address: lockerState.address,
160
+ token: tokenState.address,
154
161
  delta: slashShare.toString(10),
155
162
  action: "slash"
156
163
  });
157
164
  changes.account.push({
158
165
  address: itx.publisher,
166
+ token: tokenState.address,
159
167
  delta: slashShare.toString(10),
160
168
  action: "reward"
161
169
  });
162
170
  }
163
171
  (txs || []).forEach((x) => {
164
172
  const txData = x.tx;
165
- const { actualFee } = txData.itxJson;
173
+ const { actualFee, token: txTokenObj } = txData.itxJson;
174
+ const txToken = txTokenObj?.address || tokenState.address;
166
175
  const { publisher, proposer, validator } = splitTxFee({
167
176
  total: actualFee,
168
177
  shares,
@@ -170,11 +179,13 @@ runner.use((context, next) => {
170
179
  });
171
180
  changes.stake.push({
172
181
  address: lockerState.address,
182
+ token: txToken,
173
183
  delta: `-${actualFee}`,
174
184
  action: "claim"
175
185
  });
176
186
  changes.account.push({
177
187
  address: itx.publisher,
188
+ token: txToken,
178
189
  delta: publisher.toString(10),
179
190
  action: "fee"
180
191
  });
@@ -182,6 +193,7 @@ runner.use((context, next) => {
182
193
  const txItxJson = txData.itxJson;
183
194
  changes.account.push({
184
195
  address: txItxJson.proposer,
196
+ token: txToken,
185
197
  delta: proposer.toString(10),
186
198
  action: "fee"
187
199
  });
@@ -191,12 +203,14 @@ runner.use((context, next) => {
191
203
  const validatorShare = validator.div(new BN(validators.length));
192
204
  validators.forEach((v$1) => changes.account.push({
193
205
  address: v$1,
206
+ token: txToken,
194
207
  delta: validatorShare.toString(10),
195
208
  action: "fee"
196
209
  }));
197
210
  } else if (x.type === "withdraw_token_v2") {
198
211
  changes.account.push({
199
212
  address: blockState.proposer,
213
+ token: txToken,
200
214
  delta: proposer.toString(10),
201
215
  action: "fee"
202
216
  });
@@ -212,6 +226,7 @@ runner.use((context, next) => {
212
226
  });
213
227
  Object.keys(validatorShares).forEach((v$1) => changes.account.push({
214
228
  address: v$1,
229
+ token: txToken,
215
230
  delta: validatorShares[v$1],
216
231
  action: "fee"
217
232
  }));
@@ -221,23 +236,27 @@ runner.use((context, next) => {
221
236
  account: groupBy(changes.account, "address"),
222
237
  stake: groupBy(changes.stake, "address")
223
238
  };
239
+ const aggregateChanges = (items, addr) => {
240
+ const byToken = groupBy(items, (c) => c.token || tokenState.address);
241
+ const perToken = Object.keys(byToken).map((tok) => ({
242
+ address: addr,
243
+ token: tok,
244
+ delta: getBNSum(...byToken[tok].map((c) => c.delta)),
245
+ action: byToken[tok][byToken[tok].length - 1].action
246
+ }));
247
+ if (perToken.length === 1) return perToken[0];
248
+ return {
249
+ ...perToken[perToken.length - 1],
250
+ changes: perToken
251
+ };
252
+ };
224
253
  const updates = {
225
254
  stake: Object.keys(grouped.stake).reduce((acc, x) => {
226
- acc[x] = {
227
- address: x,
228
- token: tokenState.address,
229
- delta: getBNSum(...grouped.stake[x].map((c) => c.delta)),
230
- action: grouped.stake[x][grouped.stake[x].length - 1].action
231
- };
255
+ acc[x] = aggregateChanges(grouped.stake[x], x);
232
256
  return acc;
233
257
  }, {}),
234
258
  account: Object.keys(grouped.account).reduce((acc, x) => {
235
- acc[x] = {
236
- address: x,
237
- token: tokenState.address,
238
- delta: getBNSum(...grouped.account[x].map((c) => c.delta)),
239
- action: grouped.account[x][grouped.account[x].length - 1].action
240
- };
259
+ acc[x] = aggregateChanges(grouped.account[x], x);
241
260
  return acc;
242
261
  }, {})
243
262
  };
@@ -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 { toStakeAddress } from "@arcblock/did-util";
6
7
  import { account, rollup, stake } from "@ocap/state";
7
8
  import { Runner, pipes } from "@ocap/tx-pipeline";
8
9
  import { CustomError } from "@ocap/util/lib/error";
9
- import { toStakeAddress } from "@arcblock/did-util";
10
10
  import { v, vBN, vBNPositive, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
11
11
 
12
12
  //#region src/protocols/rollup/close.ts
@@ -5,15 +5,15 @@ 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 { toStakeAddress } from "@arcblock/did-util";
8
9
  import { account, rollup, rollupBlock, stake, tx } from "@ocap/state";
9
10
  import { Runner, pipes } from "@ocap/tx-pipeline";
10
11
  import { CustomError } from "@ocap/util/lib/error";
12
+ import { getListField } from "@ocap/util/lib/get-list-field";
13
+ import cloneDeep from "lodash/cloneDeep.js";
11
14
  import pick from "lodash/pick.js";
12
- import { toStakeAddress } from "@arcblock/did-util";
13
15
  import Debug from "debug";
14
16
  import { formatMessage } from "@ocap/message";
15
- import { getListField } from "@ocap/util/lib/get-list-field";
16
- import cloneDeep from "lodash/cloneDeep.js";
17
17
  import { patterns, schemas, v, vBN, vBNMin, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
18
18
  import MerkleTree from "@ocap/merkle-tree";
19
19
 
@@ -28,10 +28,7 @@ const schema = v.pipe(v.object({
28
28
  previousHash: v.optional(v.nullable(v.string())),
29
29
  txsHash: txHashString,
30
30
  txsList: v.pipe(v.array(txHashString), v.minLength(1)),
31
- proposer: vDID({
32
- prefix: true,
33
- wallet: "ethereum"
34
- }),
31
+ proposer: vDID({ prefix: true }),
35
32
  signaturesList: v.pipe(schemas.multiSig, v.minLength(1)),
36
33
  rollup: vDID({
37
34
  prefix: true,
@@ -73,6 +70,10 @@ runner.use(ensure_validator_default(false));
73
70
  runner.use(verify_status_default({ closed: false }));
74
71
  runner.use((context, next) => {
75
72
  const { itx, rollupState } = context;
73
+ if (rollup.getRollupTargetType(rollupState) === "EVM" && vValidate(vDID({
74
+ prefix: true,
75
+ wallet: "ethereum"
76
+ }), itx.proposer).error) return next(new CustomError("INVALID_TX", "\"proposer\" Expected wallet type to be \"ethereum\""));
76
77
  if (itx.height !== (rollupState?.blockHeight || 0) + 1) return next(new CustomError("INVALID_TX", `Expect new block height to be ${(rollupState?.blockHeight || 0) + 1}, got ${itx.height}`));
77
78
  const itxTxsList = getListField(itx, "txs");
78
79
  if (itx.governance) {
@@ -1,15 +1,15 @@
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 { isEthereumDid, isFromPublicKey } from "@arcblock/did";
5
+ import { toRollupAddress } from "@arcblock/did-util";
4
6
  import { account, delegation, rollup } from "@ocap/state";
5
7
  import { Runner, pipes } from "@ocap/tx-pipeline";
6
8
  import { CustomError } from "@ocap/util/lib/error";
7
- import { isEthereumDid, isFromPublicKey } from "@arcblock/did";
8
- import { toRollupAddress } from "@arcblock/did-util";
9
- import Debug from "debug";
10
- import { formatMessage } from "@ocap/message";
11
9
  import { getListField } from "@ocap/util/lib/get-list-field";
12
10
  import cloneDeep from "lodash/cloneDeep.js";
11
+ import Debug from "debug";
12
+ import { formatMessage } from "@ocap/message";
13
13
  import isEmpty from "lodash/isEmpty.js";
14
14
 
15
15
  //#region src/protocols/rollup/create.ts
@@ -19,8 +19,13 @@ runner.use(pipes.VerifyMultiSig(0));
19
19
  runner.use((context, next) => {
20
20
  const { itx } = context;
21
21
  context.formattedItx = formatMessage("CreateRollupTx", itx);
22
+ const decodedData = decodeAnySafe(itx.data ?? null);
23
+ context.rollupData = decodedData;
22
24
  try {
23
- rollup.create(context.formattedItx, context);
25
+ rollup.create({
26
+ ...context.formattedItx,
27
+ data: decodedData
28
+ }, context);
24
29
  } catch (err) {
25
30
  return next(new CustomError("INVALID_TX", err.message));
26
31
  }
@@ -46,7 +51,13 @@ runner.use(pipes.VerifyInfo([
46
51
  error: "INVALID_TX",
47
52
  message: "Seed validator must use ethereum compatible address",
48
53
  fn: (ctx) => {
49
- return (ctx.formattedItx?.seedValidators || []).every(({ address }) => isEthereumDid(address));
54
+ const context = ctx;
55
+ const targetType = rollup.getRollupTargetType({
56
+ ...context.formattedItx,
57
+ data: context.rollupData
58
+ });
59
+ const seedValidators = context.formattedItx?.seedValidators || [];
60
+ return targetType === "EVM" ? seedValidators.every(({ address }) => isEthereumDid(address)) : true;
50
61
  }
51
62
  },
52
63
  {
@@ -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 { isEthereumDid } from "@arcblock/did";
9
+ import { toStakeAddress } from "@arcblock/did-util";
8
10
  import { account, evidence, rollup, stake } from "@ocap/state";
9
11
  import { Runner, pipes } from "@ocap/tx-pipeline";
10
12
  import { BN, toHex } from "@ocap/util";
11
13
  import { CustomError } from "@ocap/util/lib/error";
12
- import { isEthereumDid } from "@arcblock/did";
13
- import { toStakeAddress } from "@arcblock/did-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";
@@ -47,7 +47,7 @@ runner.use((context, next) => {
47
47
  address: tx$1.from,
48
48
  endpoint: itx.endpoint
49
49
  };
50
- if (isEthereumDid(tx$1.from) === false) return next(new CustomError("INVALID_TX", "Invalid tx.from: only ethereum compatible address can join rollup"));
50
+ if (rollup.getRollupTargetType(rollupState) === "EVM" && isEthereumDid(tx$1.from) === false) return next(new CustomError("INVALID_TX", "Invalid tx.from: only ethereum compatible address can join rollup"));
51
51
  if (rollupState?.validators.some((x) => x.address === validator.address)) return next(new CustomError("INVALID_TX", `Address ${validator.address} already exist in validators`));
52
52
  if (rollupState?.validators.some((x) => joinUrl(x.endpoint, "/") === joinUrl(validator.endpoint, "/"))) return next(new CustomError("INVALID_TX", `Endpoint ${validator.endpoint} already exist in validators`));
53
53
  context.newValidator = validator;
@@ -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 { toStakeAddress } from "@arcblock/did-util";
8
9
  import { account, evidence, rollup, stake } from "@ocap/state";
9
10
  import { Runner, pipes } from "@ocap/tx-pipeline";
10
11
  import { CustomError } from "@ocap/util/lib/error";
11
- import { toStakeAddress } from "@arcblock/did-util";
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";
@@ -15,10 +15,7 @@ const schema = v.pipe(v.object({
15
15
  prefix: true,
16
16
  role: "ROLE_ROLLUP"
17
17
  }),
18
- to: vDID({
19
- prefix: true,
20
- wallet: "ethereum"
21
- }),
18
+ to: v.pipe(v.string(), v.minLength(1)),
22
19
  type: v.pipe(v.string(), v.trim(), v.check((s) => s === "vault" || s === "contract", "Must be vault or contract")),
23
20
  message: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(512)),
24
21
  data: v.optional(v.nullable(v.any()))
@@ -33,6 +30,12 @@ runner.use(pipes.ExtractState({
33
30
  status: "INVALID_ROLLUP",
34
31
  table: "rollup"
35
32
  }));
33
+ runner.use((context, next) => {
34
+ const { itx, rollupState } = context;
35
+ const targetType = rollup.getRollupTargetType(rollupState);
36
+ if (!(itx.type === "vault" ? Boolean(itx.to) && rollup.isValidRollupVaultAddress(itx.to, targetType) : rollup.isValidRollupContractAddress(itx.to, targetType))) return next(new CustomError("INVALID_TX", "Invalid itx: \"to\" Expected wallet type to be \"ethereum\""));
37
+ return next();
38
+ });
36
39
  runner.use(ensure_validator_default(true));
37
40
  runner.use(verify_status_default({
38
41
  paused: true,
@@ -1,24 +1,32 @@
1
+ import { isEthereumDid, isFromPublicKey, toTypeInfo } from "@arcblock/did";
2
+ import { rollup } from "@ocap/state";
1
3
  import { toHex } from "@ocap/util";
2
4
  import { CustomError } from "@ocap/util/lib/error";
3
- import get from "lodash/get.js";
4
- import { toTypeInfo } from "@arcblock/did";
5
5
  import { getListField } from "@ocap/util/lib/get-list-field";
6
+ import get from "lodash/get.js";
6
7
  import { fromPublicKey as fromPublicKey$1 } from "@ocap/wallet";
7
8
 
8
9
  //#region src/protocols/rollup/pipes/verify-evidence.ts
9
10
  function CreateVerifyEvidencePipe({ evidenceKey, signaturesKey, verifyMethod = "verify" }) {
10
11
  if (["verify", "ethVerify"].includes(verifyMethod) === false) throw new CustomError("INVALID_ARGUMENT", `Invalid verify method: ${verifyMethod}, supported methods are: verify, ethVerify`);
11
12
  return async (context, next) => {
12
- const evidence = get(context, evidenceKey);
13
+ const evidence$1 = get(context, evidenceKey);
13
14
  const signatures = getListField(context, signaturesKey);
14
15
  const signers = signatures.map((s) => s.signer);
15
16
  if (new Set(signers).size !== signers.length) return next(new CustomError("INVALID_SIGNATURE", `Duplicate signers in ${signaturesKey} are not allowed`));
16
17
  for (const sig of signatures) {
17
18
  const { signer, pk, delegator, signature } = sig;
18
19
  const address = delegator || signer;
20
+ if (isFromPublicKey(address, pk) === false) return next(new CustomError("INVALID_SIGNATURE", `Signature for evidence signer ${address} and pk do not match`));
19
21
  const wallet = fromPublicKey$1(pk, toTypeInfo(address));
20
22
  try {
21
- if (await wallet[verifyMethod](toHex(evidence), toHex(signature)) === false) return next(new CustomError("INVALID_SIGNATURE", `Signature for evidence from ${address} is not valid`));
23
+ const targetType = rollup.getRollupTargetType(context.rollupState);
24
+ const effectiveMethod = verifyMethod === "ethVerify" && targetType !== "EVM" && !isEthereumDid(address) ? "verify" : verifyMethod;
25
+ const hexEvidence = toHex(evidence$1);
26
+ const hexSig = toHex(signature);
27
+ let verified = await wallet[effectiveMethod](hexEvidence, hexSig);
28
+ if (!verified && verifyMethod === "ethVerify" && targetType !== "EVM" && isEthereumDid(address)) verified = await wallet.verify(hexEvidence, hexSig);
29
+ if (verified === false) return next(new CustomError("INVALID_SIGNATURE", `Signature for evidence from ${address} is not valid`));
22
30
  } catch (err) {
23
31
  return next(new CustomError("INVALID_SIGNATURE", `Signature for evidence from ${address} verify failed: ${err.message}`));
24
32
  }
@@ -92,11 +92,16 @@ runner.use(pipes.TakeStateSnapshot());
92
92
  runner.use(async (context, next) => {
93
93
  const { tx: tx$1, itx, rollupState, statedb, senderState, senderUpdates, updateVaults } = context;
94
94
  const data = decodeAnySafe(itx.data ?? null);
95
+ const sanitizedItx = { ...itx };
96
+ delete sanitizedItx.reserves;
97
+ delete sanitizedItx.targetType;
98
+ delete sanitizedItx.targetChainId;
99
+ delete sanitizedItx.enforceReserveWatermark;
95
100
  const [newSenderState, newRollupState] = await Promise.all([statedb.account.update(senderState.address, account.update(senderState, {
96
101
  nonce: tx$1.nonce,
97
102
  ...senderUpdates
98
103
  }, context), context), statedb.rollup.update(itx.rollup, rollup.update(rollupState, {
99
- ...itx,
104
+ ...sanitizedItx,
100
105
  data
101
106
  }, context), context)]);
102
107
  if (updateVaults) await updateVaults();
@@ -1,13 +1,13 @@
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 { toTokenAddress } from "@arcblock/did-util";
4
5
  import { account, delegation, token } from "@ocap/state";
5
6
  import { Runner, pipes } from "@ocap/tx-pipeline";
6
7
  import { BN, fromTokenToUnit } from "@ocap/util";
7
8
  import { CustomError } from "@ocap/util/lib/error";
8
- import { toTokenAddress } from "@arcblock/did-util";
9
- import Debug from "debug";
10
9
  import cloneDeep from "lodash/cloneDeep.js";
10
+ import Debug from "debug";
11
11
  import { schemas, v, vBN, vBNGreater, vBNMax, vDID, vForbidden, vStripUnknown, vValidate } from "@arcblock/validator";
12
12
  import uniq from "lodash/uniq.js";
13
13
  import isEmpty from "lodash/isEmpty.js";
@@ -27,11 +27,11 @@ const schema = v.pipe(v.object({
27
27
  symbol: v.pipe(v.string(), v.minLength(2), v.maxLength(6), v.toUpperCase()),
28
28
  unit: v.pipe(v.string(), v.minLength(1), v.maxLength(6), v.toLowerCase()),
29
29
  decimal: v.pipe(v.number(), v.minValue(2), v.maxValue(18)),
30
- icon: v.optional(v.nullable(v.literal(""))),
30
+ icon: v.optional(v.nullable(v.string())),
31
31
  totalSupply: vBN(vBNGreater(0), vBNMax(MAX_TOTAL_SUPPLY)),
32
32
  initialSupply: vBN(vBNGreater(0)),
33
33
  maxTotalSupply: v.optional(v.nullable(v.union([vBN(vBNGreater(0)), v.literal("")]))),
34
- foreignToken: v.optional(v.nullable(schemas.foreignToken)),
34
+ foreignToken: v.optional(v.nullable(token.multiChainForeignTokenSchema ?? schemas.foreignToken)),
35
35
  spendersList: v.optional(v.nullable(v.pipe(v.array(vDID({ prefix: true })), v.maxLength(30)))),
36
36
  tokenFactoryAddress: v.optional(vForbidden("tokenFactoryAddress is forbidden")),
37
37
  data: v.optional(v.nullable(v.any()))
@@ -45,6 +45,13 @@ runner.use(({ itx }, next) => {
45
45
  const spendersList = value.spendersList;
46
46
  itx.spenders = spendersList?.length ? uniq(spendersList) : null;
47
47
  delete itx.spendersList;
48
+ if (typeof itx.icon === "string" && itx.icon.trim()) {
49
+ const trimmedIcon = itx.icon.trim();
50
+ if (trimmedIcon.startsWith("<svg")) {
51
+ if (Buffer.byteLength(trimmedIcon, "utf8") > 16 * 1024) return next(new CustomError("INVALID_ICON", "Token Icon must be less than 16k"));
52
+ if (token.sanitizeTokenSvg(trimmedIcon) !== trimmedIcon) return next(new CustomError("INVALID_ICON", "Token Icon contains unsafe SVG elements or attributes"));
53
+ } else if (/^data:/i.test(trimmedIcon) && !token.iconToNftDisplay(trimmedIcon)) return next(new CustomError("INVALID_ICON", "Token Icon data URI must be a safe image format"));
54
+ }
48
55
  return next();
49
56
  });
50
57
  runner.use(pipes.VerifyInfo([{
@@ -78,6 +85,7 @@ runner.use(pipes.VerifyAccountMigration({
78
85
  stateKey: "delegatorState",
79
86
  addressKey: "tx.delegator"
80
87
  }));
88
+ runner.use(pipes.VerifyBlocked({ stateKeys: ["senderState", "delegatorState"] }));
81
89
  runner.use(pipes.VerifyDelegation({
82
90
  type: "signature",
83
91
  signerKey: "senderState",
@@ -123,20 +131,23 @@ runner.use(async (context, next) => {
123
131
  delegatorUpdates.tokens = delegatorState.tokens || {};
124
132
  delegatorUpdates.tokens[itx.address] = itx.initialSupply;
125
133
  } else updates.tokens[itx.address] = itx.initialSupply;
134
+ const initialTokenState = token.create({
135
+ ...cloneDeep(itx),
136
+ data,
137
+ issuer: owner,
138
+ type: "Token"
139
+ }, context);
140
+ const charterAsset = token.buildTokenCharterAssetState(initialTokenState, context, owner);
126
141
  const [newSenderState, tokenState, newDelegatorState, newDelegationState] = await Promise.all([
127
142
  statedb.account.update(senderState.address, account.update(senderState, {
128
143
  nonce: tx$1.nonce,
129
- pk: tx$1.pk,
144
+ pk: tx$1.delegator ? senderState.pk : tx$1.pk,
130
145
  ...senderUpdates
131
146
  }, context), context),
132
- statedb.token.create(itx.address, token.create({
133
- ...cloneDeep(itx),
134
- data,
135
- issuer: owner,
136
- type: "Token"
137
- }, context), context),
147
+ statedb.token.create(itx.address, initialTokenState, context),
138
148
  delegatorState ? statedb.account.update(delegatorState.address, account.update(delegatorState, delegatorUpdates, context), context) : null,
139
- context.isDelegationChanged ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context) : delegationState
149
+ context.isDelegationChanged ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context) : delegationState,
150
+ statedb.asset?.create ? Promise.resolve(statedb.asset.get?.(charterAsset.address, context)).then((existing) => existing && statedb.asset.update ? statedb.asset.update(charterAsset.address, charterAsset, context) : statedb.asset.create(charterAsset.address, charterAsset, context)) : null
140
151
  ]);
141
152
  if (updateVaults) await updateVaults();
142
153
  context.senderState = newSenderState;
@@ -1,5 +1,5 @@
1
1
  import { Runner } from "@ocap/tx-pipeline";
2
- import { IReadyContext, IStakeState, IWithEvidence, IWithRollup, IWithSender, IWithSigners, IWithStake, IWithToken, TDepositTokenV2Tx } from "@ocap/types";
2
+ import { IAccountState, IReadyContext, IStakeState, IWithEvidence, IWithRollup, IWithSender, IWithSigners, IWithStake, IWithToken, TDepositTokenV2Tx } from "@ocap/types";
3
3
 
4
4
  //#region src/protocols/token/deposit-v2.d.ts
5
5
  /** Token change for updates */
@@ -21,6 +21,10 @@ type Context = IReadyContext<TDepositTokenV2Tx> & Partial<IWithSender> & Partial
21
21
  lockerAddress?: string;
22
22
  /** Locker stake state */
23
23
  lockerState?: IStakeState;
24
+ /** Recipient account state when proposer-sponsored (itx.to !== tx.from) */
25
+ receiverState?: IAccountState | null;
26
+ /** Whether deposit is submitted by proposer on behalf of itx.to */
27
+ isProposerSponsor?: boolean;
24
28
  /** Signer addresses */
25
29
  signers?: string[];
26
30
  /** All updated stake states */