@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.
- package/esm/execute.d.mts +53 -0
- package/esm/execute.mjs +225 -0
- package/esm/index.d.mts +95 -0
- package/esm/index.mjs +100 -0
- package/esm/pipes/ensure-cost.d.mts +18 -0
- package/esm/pipes/ensure-cost.mjs +136 -0
- package/esm/pipes/ensure-gas.d.mts +14 -0
- package/esm/pipes/ensure-gas.mjs +38 -0
- package/esm/protocols/account/delegate.d.mts +4 -0
- package/esm/protocols/account/delegate.mjs +195 -0
- package/esm/protocols/account/migrate.d.mts +4 -0
- package/esm/protocols/account/migrate.mjs +130 -0
- package/esm/protocols/account/revoke-delegate.d.mts +4 -0
- package/esm/protocols/account/revoke-delegate.mjs +102 -0
- package/esm/protocols/asset/acquire-v2.d.mts +9 -0
- package/esm/protocols/asset/acquire-v2.mjs +213 -0
- package/esm/protocols/asset/acquire-v3.d.mts +17 -0
- package/esm/protocols/asset/acquire-v3.mjs +265 -0
- package/esm/protocols/asset/calls/transfer-token.d.mts +4 -0
- package/esm/protocols/asset/calls/transfer-token.mjs +36 -0
- package/esm/protocols/asset/calls/transfer.d.mts +4 -0
- package/esm/protocols/asset/calls/transfer.mjs +32 -0
- package/esm/protocols/asset/consume.d.mts +4 -0
- package/esm/protocols/asset/consume.mjs +92 -0
- package/esm/protocols/asset/create.d.mts +4 -0
- package/esm/protocols/asset/create.mjs +136 -0
- package/esm/protocols/asset/mint.d.mts +4 -0
- package/esm/protocols/asset/mint.mjs +156 -0
- package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +24 -0
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +54 -0
- package/esm/protocols/asset/pipes/extract-factory-tokens.d.mts +21 -0
- package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +22 -0
- package/esm/protocols/asset/pipes/verify-itx-address.d.mts +32 -0
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +56 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.d.mts +18 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +27 -0
- package/esm/protocols/asset/pipes/verify-itx-variables.d.mts +17 -0
- package/esm/protocols/asset/pipes/verify-itx-variables.mjs +20 -0
- package/esm/protocols/asset/pipes/verify-mint-limit.d.mts +15 -0
- package/esm/protocols/asset/pipes/verify-mint-limit.mjs +14 -0
- package/esm/protocols/asset/update.d.mts +4 -0
- package/esm/protocols/asset/update.mjs +111 -0
- package/esm/protocols/factory/create.d.mts +29 -0
- package/esm/protocols/factory/create.mjs +155 -0
- package/esm/protocols/governance/claim-stake.d.mts +27 -0
- package/esm/protocols/governance/claim-stake.mjs +220 -0
- package/esm/protocols/governance/return-stake.d.mts +27 -0
- package/esm/protocols/governance/return-stake.mjs +211 -0
- package/esm/protocols/governance/revoke-stake.d.mts +27 -0
- package/esm/protocols/governance/revoke-stake.mjs +178 -0
- package/esm/protocols/governance/slash-stake.d.mts +17 -0
- package/esm/protocols/governance/slash-stake.mjs +213 -0
- package/esm/protocols/governance/stake.d.mts +15 -0
- package/esm/protocols/governance/stake.mjs +270 -0
- package/esm/protocols/rollup/claim-reward.d.mts +11 -0
- package/esm/protocols/rollup/claim-reward.mjs +322 -0
- package/esm/protocols/rollup/close.d.mts +4 -0
- package/esm/protocols/rollup/close.mjs +105 -0
- package/esm/protocols/rollup/create-block.d.mts +11 -0
- package/esm/protocols/rollup/create-block.mjs +303 -0
- package/esm/protocols/rollup/create.d.mts +4 -0
- package/esm/protocols/rollup/create.mjs +164 -0
- package/esm/protocols/rollup/join.d.mts +4 -0
- package/esm/protocols/rollup/join.mjs +152 -0
- package/esm/protocols/rollup/leave.d.mts +4 -0
- package/esm/protocols/rollup/leave.mjs +137 -0
- package/esm/protocols/rollup/migrate.d.mts +4 -0
- package/esm/protocols/rollup/migrate.mjs +85 -0
- package/esm/protocols/rollup/pause.d.mts +4 -0
- package/esm/protocols/rollup/pause.mjs +76 -0
- package/esm/protocols/rollup/pipes/ensure-validator.d.mts +6 -0
- package/esm/protocols/rollup/pipes/ensure-validator.mjs +12 -0
- package/esm/protocols/rollup/pipes/verify-evidence.d.mts +15 -0
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +29 -0
- package/esm/protocols/rollup/pipes/verify-signers.d.mts +15 -0
- package/esm/protocols/rollup/pipes/verify-signers.mjs +36 -0
- package/esm/protocols/rollup/pipes/verify-status.d.mts +13 -0
- package/esm/protocols/rollup/pipes/verify-status.mjs +26 -0
- package/esm/protocols/rollup/resume.d.mts +4 -0
- package/esm/protocols/rollup/resume.mjs +79 -0
- package/esm/protocols/rollup/update.d.mts +4 -0
- package/esm/protocols/rollup/update.mjs +111 -0
- package/esm/protocols/token/create.d.mts +4 -0
- package/esm/protocols/token/create.mjs +150 -0
- package/esm/protocols/token/deposit-v2.d.mts +11 -0
- package/esm/protocols/token/deposit-v2.mjs +216 -0
- package/esm/protocols/token/withdraw-v2.d.mts +9 -0
- package/esm/protocols/token/withdraw-v2.mjs +222 -0
- package/esm/protocols/token-factory/burn.d.mts +15 -0
- package/esm/protocols/token-factory/burn.mjs +233 -0
- package/esm/protocols/token-factory/create.d.mts +4 -0
- package/esm/protocols/token-factory/create.mjs +254 -0
- package/esm/protocols/token-factory/mint.d.mts +15 -0
- package/esm/protocols/token-factory/mint.mjs +234 -0
- package/esm/protocols/token-factory/pipes/calc-reserve.d.mts +21 -0
- package/esm/protocols/token-factory/pipes/calc-reserve.mjs +34 -0
- package/esm/protocols/token-factory/pipes/verify-icon.d.mts +14 -0
- package/esm/protocols/token-factory/pipes/verify-icon.mjs +18 -0
- package/esm/protocols/token-factory/pipes/verify-ownership.d.mts +12 -0
- package/esm/protocols/token-factory/pipes/verify-ownership.mjs +63 -0
- package/esm/protocols/token-factory/pipes/verify-url.d.mts +12 -0
- package/esm/protocols/token-factory/pipes/verify-url.mjs +26 -0
- package/esm/protocols/token-factory/update.d.mts +10 -0
- package/esm/protocols/token-factory/update.mjs +152 -0
- package/esm/protocols/trade/exchange-v2.d.mts +9 -0
- package/esm/protocols/trade/exchange-v2.mjs +239 -0
- package/esm/protocols/trade/transfer-v2.d.mts +9 -0
- package/esm/protocols/trade/transfer-v2.mjs +226 -0
- package/esm/protocols/trade/transfer-v3.d.mts +17 -0
- package/esm/protocols/trade/transfer-v3.mjs +270 -0
- package/esm/util.d.mts +141 -0
- package/esm/util.mjs +278 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/execute.cjs +231 -0
- package/lib/execute.d.cts +53 -0
- package/lib/index.cjs +105 -0
- package/lib/index.d.cts +95 -0
- package/lib/pipes/ensure-cost.cjs +141 -0
- package/lib/pipes/ensure-cost.d.cts +18 -0
- package/lib/pipes/ensure-gas.cjs +41 -0
- package/lib/pipes/ensure-gas.d.cts +14 -0
- package/lib/protocols/account/delegate.cjs +201 -0
- package/lib/protocols/account/delegate.d.cts +4 -0
- package/lib/protocols/account/migrate.cjs +135 -0
- package/lib/protocols/account/migrate.d.cts +4 -0
- package/lib/protocols/account/revoke-delegate.cjs +107 -0
- package/lib/protocols/account/revoke-delegate.d.cts +4 -0
- package/lib/protocols/asset/acquire-v2.cjs +216 -0
- package/lib/protocols/asset/acquire-v2.d.cts +9 -0
- package/lib/protocols/asset/acquire-v3.cjs +269 -0
- package/lib/protocols/asset/acquire-v3.d.cts +17 -0
- package/lib/protocols/asset/calls/transfer-token.cjs +40 -0
- package/lib/protocols/asset/calls/transfer-token.d.cts +4 -0
- package/lib/protocols/asset/calls/transfer.cjs +35 -0
- package/lib/protocols/asset/calls/transfer.d.cts +4 -0
- package/lib/protocols/asset/consume.cjs +95 -0
- package/lib/protocols/asset/consume.d.cts +4 -0
- package/lib/protocols/asset/create.cjs +140 -0
- package/lib/protocols/asset/create.d.cts +4 -0
- package/lib/protocols/asset/mint.cjs +159 -0
- package/lib/protocols/asset/mint.d.cts +4 -0
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +57 -0
- package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +24 -0
- package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +25 -0
- package/lib/protocols/asset/pipes/extract-factory-tokens.d.cts +21 -0
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +59 -0
- package/lib/protocols/asset/pipes/verify-itx-address.d.cts +32 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +29 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.d.cts +18 -0
- package/lib/protocols/asset/pipes/verify-itx-variables.cjs +22 -0
- package/lib/protocols/asset/pipes/verify-itx-variables.d.cts +17 -0
- package/lib/protocols/asset/pipes/verify-mint-limit.cjs +16 -0
- package/lib/protocols/asset/pipes/verify-mint-limit.d.cts +15 -0
- package/lib/protocols/asset/update.cjs +114 -0
- package/lib/protocols/asset/update.d.cts +4 -0
- package/lib/protocols/factory/create.cjs +161 -0
- package/lib/protocols/factory/create.d.cts +29 -0
- package/lib/protocols/governance/claim-stake.cjs +223 -0
- package/lib/protocols/governance/claim-stake.d.cts +27 -0
- package/lib/protocols/governance/return-stake.cjs +215 -0
- package/lib/protocols/governance/return-stake.d.cts +27 -0
- package/lib/protocols/governance/revoke-stake.cjs +182 -0
- package/lib/protocols/governance/revoke-stake.d.cts +27 -0
- package/lib/protocols/governance/slash-stake.cjs +217 -0
- package/lib/protocols/governance/slash-stake.d.cts +17 -0
- package/lib/protocols/governance/stake.cjs +275 -0
- package/lib/protocols/governance/stake.d.cts +15 -0
- package/lib/protocols/rollup/claim-reward.cjs +328 -0
- package/lib/protocols/rollup/claim-reward.d.cts +11 -0
- package/lib/protocols/rollup/close.cjs +107 -0
- package/lib/protocols/rollup/close.d.cts +4 -0
- package/lib/protocols/rollup/create-block.cjs +309 -0
- package/lib/protocols/rollup/create-block.d.cts +11 -0
- package/lib/protocols/rollup/create.cjs +169 -0
- package/lib/protocols/rollup/create.d.cts +4 -0
- package/lib/protocols/rollup/join.cjs +157 -0
- package/lib/protocols/rollup/join.d.cts +4 -0
- package/lib/protocols/rollup/leave.cjs +141 -0
- package/lib/protocols/rollup/leave.d.cts +4 -0
- package/lib/protocols/rollup/migrate.cjs +87 -0
- package/lib/protocols/rollup/migrate.d.cts +4 -0
- package/lib/protocols/rollup/pause.cjs +78 -0
- package/lib/protocols/rollup/pause.d.cts +4 -0
- package/lib/protocols/rollup/pipes/ensure-validator.cjs +14 -0
- package/lib/protocols/rollup/pipes/ensure-validator.d.cts +6 -0
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +32 -0
- package/lib/protocols/rollup/pipes/verify-evidence.d.cts +15 -0
- package/lib/protocols/rollup/pipes/verify-signers.cjs +39 -0
- package/lib/protocols/rollup/pipes/verify-signers.d.cts +15 -0
- package/lib/protocols/rollup/pipes/verify-status.cjs +28 -0
- package/lib/protocols/rollup/pipes/verify-status.d.cts +13 -0
- package/lib/protocols/rollup/resume.cjs +81 -0
- package/lib/protocols/rollup/resume.d.cts +4 -0
- package/lib/protocols/rollup/update.cjs +114 -0
- package/lib/protocols/rollup/update.d.cts +4 -0
- package/lib/protocols/token/create.cjs +156 -0
- package/lib/protocols/token/create.d.cts +4 -0
- package/lib/protocols/token/deposit-v2.cjs +219 -0
- package/lib/protocols/token/deposit-v2.d.cts +11 -0
- package/lib/protocols/token/withdraw-v2.cjs +225 -0
- package/lib/protocols/token/withdraw-v2.d.cts +9 -0
- package/lib/protocols/token-factory/burn.cjs +236 -0
- package/lib/protocols/token-factory/burn.d.cts +15 -0
- package/lib/protocols/token-factory/create.cjs +260 -0
- package/lib/protocols/token-factory/create.d.cts +4 -0
- package/lib/protocols/token-factory/mint.cjs +237 -0
- package/lib/protocols/token-factory/mint.d.cts +15 -0
- package/lib/protocols/token-factory/pipes/calc-reserve.cjs +38 -0
- package/lib/protocols/token-factory/pipes/calc-reserve.d.cts +21 -0
- package/lib/protocols/token-factory/pipes/verify-icon.cjs +22 -0
- package/lib/protocols/token-factory/pipes/verify-icon.d.cts +14 -0
- package/lib/protocols/token-factory/pipes/verify-ownership.cjs +66 -0
- package/lib/protocols/token-factory/pipes/verify-ownership.d.cts +12 -0
- package/lib/protocols/token-factory/pipes/verify-url.cjs +29 -0
- package/lib/protocols/token-factory/pipes/verify-url.d.cts +12 -0
- package/lib/protocols/token-factory/update.cjs +155 -0
- package/lib/protocols/token-factory/update.d.cts +10 -0
- package/lib/protocols/trade/exchange-v2.cjs +243 -0
- package/lib/protocols/trade/exchange-v2.d.cts +9 -0
- package/lib/protocols/trade/transfer-v2.cjs +229 -0
- package/lib/protocols/trade/transfer-v2.d.cts +9 -0
- package/lib/protocols/trade/transfer-v3.cjs +274 -0
- package/lib/protocols/trade/transfer-v3.d.cts +17 -0
- package/lib/util.cjs +296 -0
- package/lib/util.d.cts +141 -0
- package/package.json +49 -22
- package/tools/fixtures.ts +564 -0
- package/lib/execute.js +0 -254
- package/lib/index.js +0 -117
- package/lib/pipes/ensure-cost.js +0 -193
- package/lib/pipes/ensure-gas.js +0 -48
- package/lib/protocols/account/delegate.js +0 -223
- package/lib/protocols/account/migrate.js +0 -153
- package/lib/protocols/account/revoke-delegate.js +0 -110
- package/lib/protocols/asset/acquire-v2.js +0 -262
- package/lib/protocols/asset/acquire-v3.js +0 -330
- package/lib/protocols/asset/calls/README.md +0 -5
- package/lib/protocols/asset/calls/transfer-token.js +0 -36
- package/lib/protocols/asset/calls/transfer.js +0 -28
- package/lib/protocols/asset/consume.js +0 -105
- package/lib/protocols/asset/create.js +0 -151
- package/lib/protocols/asset/mint.js +0 -199
- package/lib/protocols/asset/pipes/exec-mint-hook.js +0 -62
- package/lib/protocols/asset/pipes/extract-factory-tokens.js +0 -18
- package/lib/protocols/asset/pipes/verify-itx-address.js +0 -54
- package/lib/protocols/asset/pipes/verify-itx-assets.js +0 -51
- package/lib/protocols/asset/pipes/verify-itx-variables.js +0 -26
- package/lib/protocols/asset/pipes/verify-mint-limit.js +0 -13
- package/lib/protocols/asset/update.js +0 -131
- package/lib/protocols/factory/create.js +0 -191
- package/lib/protocols/governance/claim-stake.js +0 -266
- package/lib/protocols/governance/return-stake.js +0 -248
- package/lib/protocols/governance/revoke-stake.js +0 -172
- package/lib/protocols/governance/slash-stake.js +0 -271
- package/lib/protocols/governance/stake.js +0 -303
- package/lib/protocols/rollup/claim-reward.js +0 -342
- package/lib/protocols/rollup/close.js +0 -104
- package/lib/protocols/rollup/create-block.js +0 -413
- package/lib/protocols/rollup/create.js +0 -197
- package/lib/protocols/rollup/join.js +0 -182
- package/lib/protocols/rollup/leave.js +0 -145
- package/lib/protocols/rollup/migrate.js +0 -85
- package/lib/protocols/rollup/pause.js +0 -75
- package/lib/protocols/rollup/pipes/ensure-validator.js +0 -12
- package/lib/protocols/rollup/pipes/verify-evidence.js +0 -37
- package/lib/protocols/rollup/pipes/verify-signers.js +0 -87
- package/lib/protocols/rollup/pipes/verify-status.js +0 -30
- package/lib/protocols/rollup/resume.js +0 -75
- package/lib/protocols/rollup/update.js +0 -122
- package/lib/protocols/token/create.js +0 -199
- package/lib/protocols/token/deposit-v2.js +0 -290
- package/lib/protocols/token/withdraw-v2.js +0 -305
- package/lib/protocols/token-factory/burn.js +0 -371
- package/lib/protocols/token-factory/create.js +0 -342
- package/lib/protocols/token-factory/mint.js +0 -385
- package/lib/protocols/token-factory/pipes/calc-reserve.js +0 -37
- package/lib/protocols/token-factory/pipes/verify-icon.js +0 -27
- package/lib/protocols/token-factory/pipes/verify-ownership.js +0 -93
- package/lib/protocols/token-factory/pipes/verify-url.js +0 -32
- package/lib/protocols/token-factory/update.js +0 -208
- package/lib/protocols/trade/exchange-v2.js +0 -239
- package/lib/protocols/trade/transfer-v2.js +0 -233
- package/lib/protocols/trade/transfer-v3.js +0 -333
- 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;
|