@ocap/tx-protocols 1.28.9 → 1.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (284) hide show
  1. package/esm/execute.d.mts +53 -0
  2. package/esm/execute.mjs +225 -0
  3. package/esm/index.d.mts +95 -0
  4. package/esm/index.mjs +100 -0
  5. package/esm/pipes/ensure-cost.d.mts +18 -0
  6. package/esm/pipes/ensure-cost.mjs +136 -0
  7. package/esm/pipes/ensure-gas.d.mts +14 -0
  8. package/esm/pipes/ensure-gas.mjs +38 -0
  9. package/esm/protocols/account/delegate.d.mts +4 -0
  10. package/esm/protocols/account/delegate.mjs +195 -0
  11. package/esm/protocols/account/migrate.d.mts +4 -0
  12. package/esm/protocols/account/migrate.mjs +130 -0
  13. package/esm/protocols/account/revoke-delegate.d.mts +4 -0
  14. package/esm/protocols/account/revoke-delegate.mjs +102 -0
  15. package/esm/protocols/asset/acquire-v2.d.mts +9 -0
  16. package/esm/protocols/asset/acquire-v2.mjs +213 -0
  17. package/esm/protocols/asset/acquire-v3.d.mts +17 -0
  18. package/esm/protocols/asset/acquire-v3.mjs +265 -0
  19. package/esm/protocols/asset/calls/transfer-token.d.mts +4 -0
  20. package/esm/protocols/asset/calls/transfer-token.mjs +36 -0
  21. package/esm/protocols/asset/calls/transfer.d.mts +4 -0
  22. package/esm/protocols/asset/calls/transfer.mjs +32 -0
  23. package/esm/protocols/asset/consume.d.mts +4 -0
  24. package/esm/protocols/asset/consume.mjs +92 -0
  25. package/esm/protocols/asset/create.d.mts +4 -0
  26. package/esm/protocols/asset/create.mjs +136 -0
  27. package/esm/protocols/asset/mint.d.mts +4 -0
  28. package/esm/protocols/asset/mint.mjs +156 -0
  29. package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +24 -0
  30. package/esm/protocols/asset/pipes/exec-mint-hook.mjs +54 -0
  31. package/esm/protocols/asset/pipes/extract-factory-tokens.d.mts +21 -0
  32. package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +22 -0
  33. package/esm/protocols/asset/pipes/verify-itx-address.d.mts +32 -0
  34. package/esm/protocols/asset/pipes/verify-itx-address.mjs +56 -0
  35. package/esm/protocols/asset/pipes/verify-itx-assets.d.mts +18 -0
  36. package/esm/protocols/asset/pipes/verify-itx-assets.mjs +27 -0
  37. package/esm/protocols/asset/pipes/verify-itx-variables.d.mts +17 -0
  38. package/esm/protocols/asset/pipes/verify-itx-variables.mjs +20 -0
  39. package/esm/protocols/asset/pipes/verify-mint-limit.d.mts +15 -0
  40. package/esm/protocols/asset/pipes/verify-mint-limit.mjs +14 -0
  41. package/esm/protocols/asset/update.d.mts +4 -0
  42. package/esm/protocols/asset/update.mjs +111 -0
  43. package/esm/protocols/factory/create.d.mts +29 -0
  44. package/esm/protocols/factory/create.mjs +155 -0
  45. package/esm/protocols/governance/claim-stake.d.mts +27 -0
  46. package/esm/protocols/governance/claim-stake.mjs +220 -0
  47. package/esm/protocols/governance/return-stake.d.mts +27 -0
  48. package/esm/protocols/governance/return-stake.mjs +211 -0
  49. package/esm/protocols/governance/revoke-stake.d.mts +27 -0
  50. package/esm/protocols/governance/revoke-stake.mjs +178 -0
  51. package/esm/protocols/governance/slash-stake.d.mts +17 -0
  52. package/esm/protocols/governance/slash-stake.mjs +213 -0
  53. package/esm/protocols/governance/stake.d.mts +15 -0
  54. package/esm/protocols/governance/stake.mjs +270 -0
  55. package/esm/protocols/rollup/claim-reward.d.mts +11 -0
  56. package/esm/protocols/rollup/claim-reward.mjs +322 -0
  57. package/esm/protocols/rollup/close.d.mts +4 -0
  58. package/esm/protocols/rollup/close.mjs +105 -0
  59. package/esm/protocols/rollup/create-block.d.mts +11 -0
  60. package/esm/protocols/rollup/create-block.mjs +303 -0
  61. package/esm/protocols/rollup/create.d.mts +4 -0
  62. package/esm/protocols/rollup/create.mjs +164 -0
  63. package/esm/protocols/rollup/join.d.mts +4 -0
  64. package/esm/protocols/rollup/join.mjs +152 -0
  65. package/esm/protocols/rollup/leave.d.mts +4 -0
  66. package/esm/protocols/rollup/leave.mjs +137 -0
  67. package/esm/protocols/rollup/migrate.d.mts +4 -0
  68. package/esm/protocols/rollup/migrate.mjs +85 -0
  69. package/esm/protocols/rollup/pause.d.mts +4 -0
  70. package/esm/protocols/rollup/pause.mjs +76 -0
  71. package/esm/protocols/rollup/pipes/ensure-validator.d.mts +6 -0
  72. package/esm/protocols/rollup/pipes/ensure-validator.mjs +12 -0
  73. package/esm/protocols/rollup/pipes/verify-evidence.d.mts +15 -0
  74. package/esm/protocols/rollup/pipes/verify-evidence.mjs +29 -0
  75. package/esm/protocols/rollup/pipes/verify-signers.d.mts +15 -0
  76. package/esm/protocols/rollup/pipes/verify-signers.mjs +36 -0
  77. package/esm/protocols/rollup/pipes/verify-status.d.mts +13 -0
  78. package/esm/protocols/rollup/pipes/verify-status.mjs +26 -0
  79. package/esm/protocols/rollup/resume.d.mts +4 -0
  80. package/esm/protocols/rollup/resume.mjs +79 -0
  81. package/esm/protocols/rollup/update.d.mts +4 -0
  82. package/esm/protocols/rollup/update.mjs +111 -0
  83. package/esm/protocols/token/create.d.mts +4 -0
  84. package/esm/protocols/token/create.mjs +150 -0
  85. package/esm/protocols/token/deposit-v2.d.mts +11 -0
  86. package/esm/protocols/token/deposit-v2.mjs +216 -0
  87. package/esm/protocols/token/withdraw-v2.d.mts +9 -0
  88. package/esm/protocols/token/withdraw-v2.mjs +222 -0
  89. package/esm/protocols/token-factory/burn.d.mts +15 -0
  90. package/esm/protocols/token-factory/burn.mjs +233 -0
  91. package/esm/protocols/token-factory/create.d.mts +4 -0
  92. package/esm/protocols/token-factory/create.mjs +254 -0
  93. package/esm/protocols/token-factory/mint.d.mts +15 -0
  94. package/esm/protocols/token-factory/mint.mjs +234 -0
  95. package/esm/protocols/token-factory/pipes/calc-reserve.d.mts +21 -0
  96. package/esm/protocols/token-factory/pipes/calc-reserve.mjs +34 -0
  97. package/esm/protocols/token-factory/pipes/verify-icon.d.mts +14 -0
  98. package/esm/protocols/token-factory/pipes/verify-icon.mjs +18 -0
  99. package/esm/protocols/token-factory/pipes/verify-ownership.d.mts +12 -0
  100. package/esm/protocols/token-factory/pipes/verify-ownership.mjs +63 -0
  101. package/esm/protocols/token-factory/pipes/verify-url.d.mts +12 -0
  102. package/esm/protocols/token-factory/pipes/verify-url.mjs +26 -0
  103. package/esm/protocols/token-factory/update.d.mts +10 -0
  104. package/esm/protocols/token-factory/update.mjs +152 -0
  105. package/esm/protocols/trade/exchange-v2.d.mts +9 -0
  106. package/esm/protocols/trade/exchange-v2.mjs +239 -0
  107. package/esm/protocols/trade/transfer-v2.d.mts +9 -0
  108. package/esm/protocols/trade/transfer-v2.mjs +226 -0
  109. package/esm/protocols/trade/transfer-v3.d.mts +17 -0
  110. package/esm/protocols/trade/transfer-v3.mjs +270 -0
  111. package/esm/util.d.mts +141 -0
  112. package/esm/util.mjs +278 -0
  113. package/lib/_virtual/rolldown_runtime.cjs +29 -0
  114. package/lib/execute.cjs +231 -0
  115. package/lib/execute.d.cts +53 -0
  116. package/lib/index.cjs +105 -0
  117. package/lib/index.d.cts +95 -0
  118. package/lib/pipes/ensure-cost.cjs +141 -0
  119. package/lib/pipes/ensure-cost.d.cts +18 -0
  120. package/lib/pipes/ensure-gas.cjs +41 -0
  121. package/lib/pipes/ensure-gas.d.cts +14 -0
  122. package/lib/protocols/account/delegate.cjs +201 -0
  123. package/lib/protocols/account/delegate.d.cts +4 -0
  124. package/lib/protocols/account/migrate.cjs +135 -0
  125. package/lib/protocols/account/migrate.d.cts +4 -0
  126. package/lib/protocols/account/revoke-delegate.cjs +107 -0
  127. package/lib/protocols/account/revoke-delegate.d.cts +4 -0
  128. package/lib/protocols/asset/acquire-v2.cjs +216 -0
  129. package/lib/protocols/asset/acquire-v2.d.cts +9 -0
  130. package/lib/protocols/asset/acquire-v3.cjs +269 -0
  131. package/lib/protocols/asset/acquire-v3.d.cts +17 -0
  132. package/lib/protocols/asset/calls/transfer-token.cjs +40 -0
  133. package/lib/protocols/asset/calls/transfer-token.d.cts +4 -0
  134. package/lib/protocols/asset/calls/transfer.cjs +35 -0
  135. package/lib/protocols/asset/calls/transfer.d.cts +4 -0
  136. package/lib/protocols/asset/consume.cjs +95 -0
  137. package/lib/protocols/asset/consume.d.cts +4 -0
  138. package/lib/protocols/asset/create.cjs +140 -0
  139. package/lib/protocols/asset/create.d.cts +4 -0
  140. package/lib/protocols/asset/mint.cjs +159 -0
  141. package/lib/protocols/asset/mint.d.cts +4 -0
  142. package/lib/protocols/asset/pipes/exec-mint-hook.cjs +57 -0
  143. package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +24 -0
  144. package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +25 -0
  145. package/lib/protocols/asset/pipes/extract-factory-tokens.d.cts +21 -0
  146. package/lib/protocols/asset/pipes/verify-itx-address.cjs +59 -0
  147. package/lib/protocols/asset/pipes/verify-itx-address.d.cts +32 -0
  148. package/lib/protocols/asset/pipes/verify-itx-assets.cjs +29 -0
  149. package/lib/protocols/asset/pipes/verify-itx-assets.d.cts +18 -0
  150. package/lib/protocols/asset/pipes/verify-itx-variables.cjs +22 -0
  151. package/lib/protocols/asset/pipes/verify-itx-variables.d.cts +17 -0
  152. package/lib/protocols/asset/pipes/verify-mint-limit.cjs +16 -0
  153. package/lib/protocols/asset/pipes/verify-mint-limit.d.cts +15 -0
  154. package/lib/protocols/asset/update.cjs +114 -0
  155. package/lib/protocols/asset/update.d.cts +4 -0
  156. package/lib/protocols/factory/create.cjs +161 -0
  157. package/lib/protocols/factory/create.d.cts +29 -0
  158. package/lib/protocols/governance/claim-stake.cjs +223 -0
  159. package/lib/protocols/governance/claim-stake.d.cts +27 -0
  160. package/lib/protocols/governance/return-stake.cjs +215 -0
  161. package/lib/protocols/governance/return-stake.d.cts +27 -0
  162. package/lib/protocols/governance/revoke-stake.cjs +182 -0
  163. package/lib/protocols/governance/revoke-stake.d.cts +27 -0
  164. package/lib/protocols/governance/slash-stake.cjs +217 -0
  165. package/lib/protocols/governance/slash-stake.d.cts +17 -0
  166. package/lib/protocols/governance/stake.cjs +275 -0
  167. package/lib/protocols/governance/stake.d.cts +15 -0
  168. package/lib/protocols/rollup/claim-reward.cjs +328 -0
  169. package/lib/protocols/rollup/claim-reward.d.cts +11 -0
  170. package/lib/protocols/rollup/close.cjs +107 -0
  171. package/lib/protocols/rollup/close.d.cts +4 -0
  172. package/lib/protocols/rollup/create-block.cjs +309 -0
  173. package/lib/protocols/rollup/create-block.d.cts +11 -0
  174. package/lib/protocols/rollup/create.cjs +169 -0
  175. package/lib/protocols/rollup/create.d.cts +4 -0
  176. package/lib/protocols/rollup/join.cjs +157 -0
  177. package/lib/protocols/rollup/join.d.cts +4 -0
  178. package/lib/protocols/rollup/leave.cjs +141 -0
  179. package/lib/protocols/rollup/leave.d.cts +4 -0
  180. package/lib/protocols/rollup/migrate.cjs +87 -0
  181. package/lib/protocols/rollup/migrate.d.cts +4 -0
  182. package/lib/protocols/rollup/pause.cjs +78 -0
  183. package/lib/protocols/rollup/pause.d.cts +4 -0
  184. package/lib/protocols/rollup/pipes/ensure-validator.cjs +14 -0
  185. package/lib/protocols/rollup/pipes/ensure-validator.d.cts +6 -0
  186. package/lib/protocols/rollup/pipes/verify-evidence.cjs +32 -0
  187. package/lib/protocols/rollup/pipes/verify-evidence.d.cts +15 -0
  188. package/lib/protocols/rollup/pipes/verify-signers.cjs +39 -0
  189. package/lib/protocols/rollup/pipes/verify-signers.d.cts +15 -0
  190. package/lib/protocols/rollup/pipes/verify-status.cjs +28 -0
  191. package/lib/protocols/rollup/pipes/verify-status.d.cts +13 -0
  192. package/lib/protocols/rollup/resume.cjs +81 -0
  193. package/lib/protocols/rollup/resume.d.cts +4 -0
  194. package/lib/protocols/rollup/update.cjs +114 -0
  195. package/lib/protocols/rollup/update.d.cts +4 -0
  196. package/lib/protocols/token/create.cjs +156 -0
  197. package/lib/protocols/token/create.d.cts +4 -0
  198. package/lib/protocols/token/deposit-v2.cjs +219 -0
  199. package/lib/protocols/token/deposit-v2.d.cts +11 -0
  200. package/lib/protocols/token/withdraw-v2.cjs +225 -0
  201. package/lib/protocols/token/withdraw-v2.d.cts +9 -0
  202. package/lib/protocols/token-factory/burn.cjs +236 -0
  203. package/lib/protocols/token-factory/burn.d.cts +15 -0
  204. package/lib/protocols/token-factory/create.cjs +260 -0
  205. package/lib/protocols/token-factory/create.d.cts +4 -0
  206. package/lib/protocols/token-factory/mint.cjs +237 -0
  207. package/lib/protocols/token-factory/mint.d.cts +15 -0
  208. package/lib/protocols/token-factory/pipes/calc-reserve.cjs +38 -0
  209. package/lib/protocols/token-factory/pipes/calc-reserve.d.cts +21 -0
  210. package/lib/protocols/token-factory/pipes/verify-icon.cjs +22 -0
  211. package/lib/protocols/token-factory/pipes/verify-icon.d.cts +14 -0
  212. package/lib/protocols/token-factory/pipes/verify-ownership.cjs +66 -0
  213. package/lib/protocols/token-factory/pipes/verify-ownership.d.cts +12 -0
  214. package/lib/protocols/token-factory/pipes/verify-url.cjs +29 -0
  215. package/lib/protocols/token-factory/pipes/verify-url.d.cts +12 -0
  216. package/lib/protocols/token-factory/update.cjs +155 -0
  217. package/lib/protocols/token-factory/update.d.cts +10 -0
  218. package/lib/protocols/trade/exchange-v2.cjs +243 -0
  219. package/lib/protocols/trade/exchange-v2.d.cts +9 -0
  220. package/lib/protocols/trade/transfer-v2.cjs +229 -0
  221. package/lib/protocols/trade/transfer-v2.d.cts +9 -0
  222. package/lib/protocols/trade/transfer-v3.cjs +274 -0
  223. package/lib/protocols/trade/transfer-v3.d.cts +17 -0
  224. package/lib/util.cjs +296 -0
  225. package/lib/util.d.cts +141 -0
  226. package/package.json +49 -22
  227. package/tools/fixtures.ts +564 -0
  228. package/lib/execute.js +0 -254
  229. package/lib/index.js +0 -117
  230. package/lib/pipes/ensure-cost.js +0 -193
  231. package/lib/pipes/ensure-gas.js +0 -48
  232. package/lib/protocols/account/delegate.js +0 -223
  233. package/lib/protocols/account/migrate.js +0 -153
  234. package/lib/protocols/account/revoke-delegate.js +0 -110
  235. package/lib/protocols/asset/acquire-v2.js +0 -262
  236. package/lib/protocols/asset/acquire-v3.js +0 -330
  237. package/lib/protocols/asset/calls/README.md +0 -5
  238. package/lib/protocols/asset/calls/transfer-token.js +0 -36
  239. package/lib/protocols/asset/calls/transfer.js +0 -28
  240. package/lib/protocols/asset/consume.js +0 -105
  241. package/lib/protocols/asset/create.js +0 -151
  242. package/lib/protocols/asset/mint.js +0 -199
  243. package/lib/protocols/asset/pipes/exec-mint-hook.js +0 -62
  244. package/lib/protocols/asset/pipes/extract-factory-tokens.js +0 -18
  245. package/lib/protocols/asset/pipes/verify-itx-address.js +0 -54
  246. package/lib/protocols/asset/pipes/verify-itx-assets.js +0 -51
  247. package/lib/protocols/asset/pipes/verify-itx-variables.js +0 -26
  248. package/lib/protocols/asset/pipes/verify-mint-limit.js +0 -13
  249. package/lib/protocols/asset/update.js +0 -131
  250. package/lib/protocols/factory/create.js +0 -191
  251. package/lib/protocols/governance/claim-stake.js +0 -266
  252. package/lib/protocols/governance/return-stake.js +0 -248
  253. package/lib/protocols/governance/revoke-stake.js +0 -172
  254. package/lib/protocols/governance/slash-stake.js +0 -271
  255. package/lib/protocols/governance/stake.js +0 -303
  256. package/lib/protocols/rollup/claim-reward.js +0 -342
  257. package/lib/protocols/rollup/close.js +0 -104
  258. package/lib/protocols/rollup/create-block.js +0 -413
  259. package/lib/protocols/rollup/create.js +0 -197
  260. package/lib/protocols/rollup/join.js +0 -182
  261. package/lib/protocols/rollup/leave.js +0 -145
  262. package/lib/protocols/rollup/migrate.js +0 -85
  263. package/lib/protocols/rollup/pause.js +0 -75
  264. package/lib/protocols/rollup/pipes/ensure-validator.js +0 -12
  265. package/lib/protocols/rollup/pipes/verify-evidence.js +0 -37
  266. package/lib/protocols/rollup/pipes/verify-signers.js +0 -87
  267. package/lib/protocols/rollup/pipes/verify-status.js +0 -30
  268. package/lib/protocols/rollup/resume.js +0 -75
  269. package/lib/protocols/rollup/update.js +0 -122
  270. package/lib/protocols/token/create.js +0 -199
  271. package/lib/protocols/token/deposit-v2.js +0 -290
  272. package/lib/protocols/token/withdraw-v2.js +0 -305
  273. package/lib/protocols/token-factory/burn.js +0 -371
  274. package/lib/protocols/token-factory/create.js +0 -342
  275. package/lib/protocols/token-factory/mint.js +0 -385
  276. package/lib/protocols/token-factory/pipes/calc-reserve.js +0 -37
  277. package/lib/protocols/token-factory/pipes/verify-icon.js +0 -27
  278. package/lib/protocols/token-factory/pipes/verify-ownership.js +0 -93
  279. package/lib/protocols/token-factory/pipes/verify-url.js +0 -32
  280. package/lib/protocols/token-factory/update.js +0 -208
  281. package/lib/protocols/trade/exchange-v2.js +0 -239
  282. package/lib/protocols/trade/transfer-v2.js +0 -233
  283. package/lib/protocols/trade/transfer-v3.js +0 -333
  284. package/lib/util.js +0 -442
@@ -1,223 +0,0 @@
1
- const get = require('lodash/get');
2
- const cloneDeep = require('lodash/cloneDeep');
3
- const pick = require('lodash/pick');
4
- const { Joi } = require('@arcblock/validator');
5
- const { CustomError: Error } = require('@ocap/util/lib/error');
6
- const { delegation, account } = require('@ocap/state');
7
- const { Runner, pipes } = require('@ocap/tx-pipeline');
8
- const { toDelegateAddress } = require('@arcblock/did-util');
9
- const { formatMessage } = require('@ocap/message');
10
-
11
- const debug = require('debug')(`${require('../../../package.json').name}:delegate`);
12
-
13
- const EnsureTxGas = require('../../pipes/ensure-gas');
14
- const EnsureTxCost = require('../../pipes/ensure-cost');
15
-
16
- const runner = new Runner();
17
-
18
- runner.use(pipes.VerifyMultiSig(0));
19
-
20
- const rateLimit = Joi.object({
21
- interval: Joi.number().integer().greater(0).required(),
22
- anchor: Joi.number().integer().min(0).optional().allow(null).default(0),
23
- });
24
-
25
- const tokenLimit = Joi.object({
26
- address: Joi.DID().prefix().role('ROLE_TOKEN').required(),
27
- toList: Joi.array().items(Joi.DID().prefix()).max(32).unique().optional().allow(null).default([]),
28
- txCount: Joi.number().integer().min(0).empty('').optional().allow(null).default(0),
29
- txAllowance: Joi.BN().optional().allow(null).default('0'),
30
- totalAllowance: Joi.BN().optional().allow(null).default('0'),
31
- validUntil: Joi.number().integer().min(0).empty('').optional().allow(null).default(0),
32
- rate: rateLimit.optional().allow(null),
33
- });
34
-
35
- const assetLimit = Joi.object({
36
- address: Joi.array()
37
- .items(Joi.DID().prefix().role('ROLE_ASSET'))
38
- .unique()
39
- .max(1024)
40
- .optional()
41
- .allow(null)
42
- .default([]),
43
- toList: Joi.array().items(Joi.DID().prefix()).unique().max(32).optional().allow(null).default([]),
44
- txCount: Joi.number().integer().min(0).empty('').optional().allow(null).default(0),
45
- validUntil: Joi.number().integer().min(0).empty('').optional().allow(null).default(0),
46
- rate: rateLimit.optional().allow(null),
47
- });
48
-
49
- // verify itx
50
- const schema = Joi.object({
51
- address: Joi.DID().prefix().role('ROLE_DELEGATION').required(),
52
- to: Joi.DID().prefix().required(),
53
- opsList: Joi.array()
54
- .items(
55
- Joi.object({
56
- typeUrl: Joi.string().required(),
57
- limit: Joi.object({
58
- tokensList: Joi.array()
59
- .items(tokenLimit)
60
- .unique((a, b) => a.address === b.address)
61
- .max(32),
62
- assetsList: Joi.array().items(assetLimit).max(32),
63
- })
64
- .optional()
65
- .allow(null)
66
- .default({ tokensList: [], assetsList: [] }),
67
- })
68
- )
69
- .unique((a, b) => a.typeUrl === b.typeUrl)
70
- .min(1)
71
- .required(),
72
- data: Joi.any().optional().allow(null),
73
- }).options({ stripUnknown: true, noDefaults: false });
74
- runner.use((context, next) => {
75
- const { error, value } = schema.validate(context.itx);
76
- if (error) {
77
- return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
78
- }
79
- context.formattedItx = formatMessage('DelegateTx', context.itx);
80
- context.validatedItx = value;
81
- return next();
82
- });
83
- runner.use(
84
- pipes.VerifyInfo([
85
- {
86
- error: 'INVALID_TX',
87
- message: 'Delegation address does not match',
88
- fn: ({ itx, tx }) => toDelegateAddress(tx.from, itx.to) === itx.address,
89
- },
90
- {
91
- error: 'INVALID_DELEGATION_TYPE_URL',
92
- message: 'Delegation type url is not allowed',
93
- fn: ({ formattedItx, config }) => {
94
- const allowed = get(config, 'transaction.delegate.typeUrls', []);
95
- return formattedItx.ops.every((op) => allowed.includes(op.typeUrl));
96
- },
97
- },
98
- ])
99
- );
100
-
101
- // Ensure sender exist and not migrated
102
- runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'OK', table: 'account' }));
103
- runner.use(pipes.VerifyAccountMigration({ stateKey: 'senderState', addressKey: 'tx.from' }));
104
-
105
- // Ensure receiver exist and not migrated
106
- runner.use(pipes.ExtractState({ from: 'itx.to', to: 'receiverState', status: 'OK', table: 'account' }));
107
- runner.use(pipes.VerifyBlocked({ stateKeys: ['receiverState'] }));
108
- runner.use(pipes.VerifyAccountMigration({ stateKey: 'receiverState', addressKey: 'itx.to' }));
109
-
110
- // Ensure sender and receiver are not the same
111
- runner.use(pipes.AntiLandAttack({ senderState: 'senderState', receiverState: 'receiverState' }));
112
-
113
- // Extract delegation
114
- runner.use(pipes.ExtractState({ from: 'itx.address', to: 'delegationState', table: 'delegation', status: 'OK' }));
115
-
116
- // Ensure tx fee and gas
117
- runner.use(
118
- EnsureTxGas((context) => {
119
- const result = { create: 0, update: 1, payment: 0 };
120
- if (context.receiverState) {
121
- result.update += 1;
122
- } else {
123
- result.create += 1;
124
- }
125
- if (context.delegationState) {
126
- result.update += 1;
127
- } else {
128
- result.create += 1;
129
- }
130
-
131
- return result;
132
- })
133
- );
134
- runner.use(EnsureTxCost({ attachSenderChanges: true }));
135
-
136
- // Save context snapshot before updating states
137
- runner.use(pipes.TakeStateSnapshot());
138
-
139
- // Create delegation state
140
- runner.use(
141
- async (context, next) => {
142
- const {
143
- tx,
144
- itx,
145
- formattedItx,
146
- validatedItx,
147
- statedb,
148
- senderState,
149
- receiverState,
150
- delegationState,
151
- senderUpdates,
152
- updateVaults,
153
- } = context;
154
-
155
- const merged = cloneDeep(formattedItx.ops).reduce(
156
- (acc, x, i) => {
157
- acc[x.typeUrl] = {
158
- // This performs a reset of the limit
159
- limit: {
160
- tokens: (x.limit?.tokens || []).map((y, j) => {
161
- const input = validatedItx.opsList[i].limit.tokensList[j];
162
- return Object.assign(y, pick(input, ['txCount', 'txAllowance', 'totalAllowance', 'validUntil', 'rate']), {
163
- txSent: 0,
164
- lastTx: y.rate ? y.rate.anchor : 0,
165
- spentAllowance: '0',
166
- });
167
- }),
168
- assets: (x.limit?.assets || []).map((y, j) => {
169
- const input = validatedItx.opsList[i].limit.assetsList[j];
170
- return Object.assign(y, pick(input, ['txCount', 'validUntil', 'rate']), {
171
- txSent: 0,
172
- lastTx: y.rate ? y.rate.anchor : 0,
173
- });
174
- }),
175
- },
176
- };
177
- return acc;
178
- },
179
- delegationState ? delegationState.ops : {}
180
- );
181
-
182
- const sender = senderState ? senderState.address : tx.from;
183
- const receiver = receiverState ? receiverState.address : itx.to;
184
-
185
- const [newSenderState, newReceiverState, newDelegationState] = await Promise.all([
186
- // update sender
187
- statedb.account.updateOrCreate(
188
- senderState,
189
- account.updateOrCreate(senderState, { ...senderUpdates, address: sender, nonce: tx.nonce, pk: tx.pk }, context),
190
- context
191
- ),
192
-
193
- // update receiver
194
- receiverState
195
- ? Promise.resolve(receiverState)
196
- : statedb.account.create(
197
- receiver,
198
- account.create({ address: receiver, tokens: { [context.config.token.address]: '0' } }, context),
199
- context
200
- ),
201
-
202
- delegationState
203
- ? statedb.delegation.update(itx.address, delegation.update(delegationState, { ...itx, from: sender, ops: merged }, context), context)
204
- : statedb.delegation.create(itx.address, delegation.create({ ...itx, from: sender, ops: merged }, context), context),
205
- ]);
206
-
207
- await updateVaults();
208
-
209
- debug(delegationState ? 'update' : 'create', newDelegationState);
210
-
211
- // Update context
212
- context.senderState = newSenderState;
213
- context.receiverState = newReceiverState;
214
- context.delegationState = newDelegationState;
215
-
216
- return next();
217
- },
218
- { persistError: true }
219
- );
220
-
221
- runner.use(pipes.VerifyStateDiff());
222
-
223
- module.exports = runner;
@@ -1,153 +0,0 @@
1
- const uniq = require('lodash/uniq');
2
- const flatten = require('lodash/flatten');
3
- const { CustomError: Error } = require('@ocap/util/lib/error');
4
- const { Joi } = require('@arcblock/validator');
5
- const { getRelatedAddresses } = require('@ocap/util/lib/get-related-addr');
6
- const { Runner, pipes } = require('@ocap/tx-pipeline');
7
- const { fromPublicKey, toTypeInfo } = require('@arcblock/did');
8
- const { account } = require('@ocap/state');
9
- const {
10
- types: { RoleType },
11
- } = require('@ocap/mcrypto');
12
-
13
- const debug = require('debug')(`${require('../../../package.json').name}:migrate`);
14
-
15
- const EnsureTxGas = require('../../pipes/ensure-gas');
16
- const EnsureTxCost = require('../../pipes/ensure-cost');
17
- const { decodeAnySafe } = require('../../util');
18
-
19
- const runner = new Runner();
20
-
21
- runner.use(pipes.VerifyMultiSig(0));
22
-
23
- // verify itx
24
- const schema = Joi.object({
25
- address: Joi.DID().prefix().required(),
26
- pk: Joi.any().required(),
27
- data: Joi.any().optional().allow(null),
28
- }).options({ stripUnknown: true, noDefaults: false });
29
- runner.use(({ itx }, next) => {
30
- const { error } = schema.validate(itx);
31
- if (error) {
32
- return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
33
- }
34
- return next();
35
- });
36
- runner.use(
37
- pipes.VerifyInfo([
38
- {
39
- error: 'INVALID_RECEIVER_STATE',
40
- message: 'Receiver pk and address does not match',
41
- fn: ({ itx, tx }) => fromPublicKey(itx.pk, toTypeInfo(itx.address)) === itx.address && itx.address !== tx.from,
42
- },
43
- ])
44
- );
45
-
46
- // Ensure sender exist
47
- runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'OK', table: 'account' }));
48
- runner.use(pipes.VerifyAccountMigration({ stateKey: 'senderState', addressKey: 'tx.from' }));
49
-
50
- // Ensure tx fee and gas
51
- runner.use(
52
- EnsureTxGas((context) => {
53
- const { itx, senderState, tx } = context;
54
- const isApplicationMigration = [senderState?.address || tx.from, itx.address].every(
55
- (address) => toTypeInfo(address).role === RoleType.ROLE_APPLICATION
56
- );
57
-
58
- // Do not charge gas for migration between applications
59
- if (isApplicationMigration) {
60
- Object.defineProperty(context, 'txBaseGas', { value: false });
61
- return { create: 0, update: 0, payment: 0 };
62
- }
63
-
64
- // Do not charge gas for connecting wallet from application
65
- if (itx.data) {
66
- const decoded = decodeAnySafe(itx.data);
67
- if (decoded?.value && decoded.value.purpose === 'connect-wallet') {
68
- Object.defineProperty(context, 'txBaseGas', { value: false });
69
- return { create: 0, update: 0, payment: 0 };
70
- }
71
- }
72
-
73
- return {
74
- create: context.senderState ? 1 : 2,
75
- update: context.senderState ? getRelatedAddresses(context.senderState).length : 0,
76
- payment: 0,
77
- };
78
- })
79
- );
80
- runner.use(EnsureTxCost({ attachSenderChanges: true, throwOnInsufficientFund: false }));
81
-
82
- // Save context snapshot before updating states
83
- runner.use(pipes.TakeStateSnapshot());
84
-
85
- // Create account state, and update old accounts
86
- runner.use(
87
- async (context, next) => {
88
- const { tx, itx, statedb, senderUpdates, updateVaults } = context;
89
-
90
- const sender = context.senderState;
91
- const receiver = await statedb.account.get(itx.address, context);
92
- const senderTokens = sender?.tokens || { [context.config.token.address]: '0' };
93
- const senderAddress = sender?.address || tx.from;
94
-
95
- // Ensure receiver does not exist
96
- if (receiver) {
97
- return next(new Error('INVALID_RECEIVER_STATE', 'Can not migrate to an existing account'));
98
- }
99
-
100
- // Create new account
101
- const newAccount = await statedb.account.create(
102
- itx.address,
103
- account.create(
104
- {
105
- ...(sender || { tokens: senderTokens }), // copy all old account data to new account
106
- ...senderUpdates,
107
- address: itx.address,
108
- pk: itx.pk,
109
- migratedFrom: sender ? uniq(flatten([sender.address, ...sender.migratedFrom])) : [senderAddress],
110
- },
111
- context
112
- ),
113
- context
114
- );
115
- debug('new account', newAccount);
116
-
117
- // Update old accounts
118
- const addresses = sender ? getRelatedAddresses(sender) : [senderAddress];
119
- const states = await Promise.all(
120
- addresses.map(async (address) => {
121
- let state = null;
122
- if (address === senderAddress) {
123
- state = account.updateOrCreate(
124
- sender,
125
- { address, tokens: senderTokens, migratedTo: itx.address, nonce: tx.nonce },
126
- context
127
- );
128
- await statedb.account.updateOrCreate(sender, state, context);
129
- } else {
130
- const old = await statedb.account.get(address, { traceMigration: false, txn: context.txn });
131
- state = account.update(old, { migratedTo: itx.address }, context);
132
- await statedb.account.update(address, state, context);
133
- }
134
-
135
- return state;
136
- })
137
- );
138
- debug('old accounts', states);
139
-
140
- await updateVaults();
141
-
142
- // Update context
143
- context.receiverState = newAccount;
144
- context.senderState = states.find((x) => x.address === senderAddress);
145
-
146
- return next();
147
- },
148
- { persistError: true }
149
- );
150
-
151
- runner.use(pipes.VerifyStateDiff());
152
-
153
- module.exports = runner;
@@ -1,110 +0,0 @@
1
- const get = require('lodash/get');
2
- const cloneDeep = require('lodash/cloneDeep');
3
- const { Joi } = require('@arcblock/validator');
4
- const { CustomError: Error } = require('@ocap/util/lib/error');
5
- const { getListField } = require('@ocap/util/lib/get-list-field');
6
- const { Runner, pipes } = require('@ocap/tx-pipeline');
7
- const { delegation, account } = require('@ocap/state');
8
- const { toDelegateAddress } = require('@arcblock/did-util');
9
-
10
- const debug = require('debug')(`${require('../../../package.json').name}:delegate`);
11
-
12
- const EnsureTxGas = require('../../pipes/ensure-gas');
13
- const EnsureTxCost = require('../../pipes/ensure-cost');
14
-
15
- const runner = new Runner();
16
-
17
- runner.use(pipes.VerifyMultiSig(0));
18
-
19
- // Verify itx
20
- const schema = Joi.object({
21
- address: Joi.DID().prefix().role('ROLE_DELEGATION').required(),
22
- to: Joi.DID().prefix().required(),
23
- typeUrlsList: Joi.array().items(Joi.string().required()).min(1).required(),
24
- data: Joi.any().optional().allow(null),
25
- }).options({ stripUnknown: true, noDefaults: false });
26
- runner.use(({ itx }, next) => {
27
- const { error } = schema.validate(itx);
28
- if (error) {
29
- return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
30
- }
31
- return next();
32
- });
33
- runner.use(
34
- pipes.VerifyInfo([
35
- {
36
- error: 'OK',
37
- fn: (context) => {
38
- context.typeUrls = getListField(context, 'itx.typeUrls');
39
- return true;
40
- },
41
- },
42
- {
43
- error: 'INVALID_TX',
44
- message: 'Delegation address does not match',
45
- fn: ({ itx, tx }) => toDelegateAddress(tx.from, itx.to) === itx.address,
46
- },
47
- {
48
- error: 'INVALID_DELEGATION_TYPE_URL',
49
- message: 'Delegation type url is not allowed',
50
- fn: ({ typeUrls, config }) => {
51
- const allowed = get(config, 'transaction.delegate.typeUrls', []);
52
- return typeUrls.every((x) => allowed.includes(x));
53
- },
54
- },
55
- ])
56
- );
57
-
58
- // Ensure sender/receiver/delegation exist
59
- runner.use(
60
- pipes.ExtractState({ from: 'itx.address', to: 'delegationState', status: 'INVALID_DELEGATION', table: 'delegation' })
61
- );
62
- runner.use(
63
- pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE', table: 'account' })
64
- );
65
- runner.use(pipes.VerifyAccountMigration({ stateKey: 'senderState', addressKey: 'tx.from' }));
66
-
67
- // Ensure tx fee and gas
68
- runner.use(EnsureTxGas(() => ({ create: 0, update: 2, payment: 0 })));
69
- runner.use(EnsureTxCost({ attachSenderChanges: true }));
70
-
71
- // Save context snapshot before updating states
72
- runner.use(pipes.TakeStateSnapshot());
73
-
74
- // Update delegation state
75
- runner.use(
76
- async (context, next) => {
77
- const { tx, itx, typeUrls, statedb, senderState, senderUpdates, delegationState, updateVaults } = context;
78
-
79
- debug('update', { address: itx.address, typeUrls });
80
-
81
- const newOps = cloneDeep(delegationState.ops);
82
- typeUrls.forEach((x) => delete newOps[x]);
83
-
84
- const [newSenderState, newDelegationState] = await Promise.all([
85
- statedb.account.update(
86
- senderState.address,
87
- account.update(senderState, { ...senderUpdates, nonce: tx.nonce }, context),
88
- context
89
- ),
90
- statedb.delegation.update(
91
- itx.address,
92
- delegation.update(context.delegationState, { ops: newOps }, context),
93
- context
94
- ),
95
- ]);
96
-
97
- await updateVaults();
98
-
99
- // Update context
100
- context.senderState = newSenderState;
101
- context.delegationState = newDelegationState;
102
-
103
- return next();
104
- },
105
- { persistError: true }
106
- );
107
-
108
- runner.use(pipes.VerifyStateDiff());
109
-
110
- module.exports = runner;