@ocap/tx-protocols 1.30.25 → 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.
- package/esm/execute.d.mts +7 -0
- package/esm/execute.mjs +196 -71
- package/esm/gas-security-guard.d.mts +374 -0
- package/esm/gas-security-guard.mjs +1101 -0
- package/esm/index.d.mts +35 -5
- package/esm/index.mjs +3 -1
- package/esm/pipes/ensure-cost.mjs +314 -58
- package/esm/protocols/account/delegate.mjs +64 -16
- package/esm/protocols/account/migrate.d.mts +3 -0
- package/esm/protocols/account/migrate.mjs +235 -35
- package/esm/protocols/account/revoke-delegate.d.mts +3 -3
- package/esm/protocols/account/revoke-delegate.mjs +70 -38
- package/esm/protocols/asset/acquire-v2.mjs +29 -12
- package/esm/protocols/asset/acquire-v3.mjs +78 -22
- package/esm/protocols/asset/calls/transfer-token.mjs +2 -1
- package/esm/protocols/asset/calls/transfer.mjs +1 -0
- package/esm/protocols/asset/consume.mjs +12 -4
- package/esm/protocols/asset/create.mjs +18 -5
- package/esm/protocols/asset/mint.mjs +23 -1
- package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +21 -1
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +115 -1
- package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +14 -8
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +9 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +21 -5
- package/esm/protocols/asset/update.d.mts +13 -2
- package/esm/protocols/asset/update.mjs +133 -14
- package/esm/protocols/factory/create.mjs +14 -6
- package/esm/protocols/governance/claim-stake.mjs +92 -10
- package/esm/protocols/governance/return-stake.mjs +44 -12
- package/esm/protocols/governance/revoke-stake.mjs +17 -5
- package/esm/protocols/governance/slash-stake.mjs +64 -25
- package/esm/protocols/governance/stake.mjs +63 -13
- package/esm/protocols/rollup/claim-reward.mjs +39 -20
- package/esm/protocols/rollup/close.mjs +1 -1
- package/esm/protocols/rollup/create-block.mjs +8 -7
- package/esm/protocols/rollup/create.mjs +17 -6
- package/esm/protocols/rollup/join.mjs +3 -3
- package/esm/protocols/rollup/leave.mjs +1 -1
- package/esm/protocols/rollup/migrate.mjs +7 -4
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +12 -4
- package/esm/protocols/rollup/update.mjs +6 -1
- package/esm/protocols/token/create.mjs +23 -12
- package/esm/protocols/token/deposit-v2.d.mts +5 -1
- package/esm/protocols/token/deposit-v2.mjs +81 -29
- package/esm/protocols/token/withdraw-v2.mjs +33 -16
- package/esm/protocols/token-factory/burn.mjs +43 -10
- package/esm/protocols/token-factory/create.mjs +23 -14
- package/esm/protocols/token-factory/mint.mjs +54 -21
- package/esm/protocols/token-factory/pipes/verify-icon.mjs +1 -1
- package/esm/protocols/token-factory/update.mjs +16 -6
- package/esm/protocols/trade/exchange-v2.d.mts +4 -0
- package/esm/protocols/trade/exchange-v2.mjs +78 -22
- package/esm/protocols/trade/transfer-v2.mjs +44 -13
- package/esm/protocols/trade/transfer-v3.mjs +72 -31
- package/esm/util.d.mts +3 -1
- package/esm/util.mjs +26 -17
- package/lib/execute.cjs +193 -67
- package/lib/execute.d.cts +7 -0
- package/lib/gas-security-guard.cjs +1140 -0
- package/lib/gas-security-guard.d.cts +374 -0
- package/lib/index.cjs +122 -1
- package/lib/index.d.cts +36 -6
- package/lib/pipes/ensure-cost.cjs +313 -57
- package/lib/protocols/account/delegate.cjs +64 -16
- package/lib/protocols/account/migrate.cjs +235 -35
- package/lib/protocols/account/migrate.d.cts +3 -0
- package/lib/protocols/account/revoke-delegate.cjs +71 -39
- package/lib/protocols/account/revoke-delegate.d.cts +3 -3
- package/lib/protocols/asset/acquire-v2.cjs +28 -11
- package/lib/protocols/asset/acquire-v3.cjs +77 -20
- package/lib/protocols/asset/calls/transfer-token.cjs +3 -2
- package/lib/protocols/asset/calls/transfer.cjs +1 -0
- package/lib/protocols/asset/consume.cjs +11 -3
- package/lib/protocols/asset/create.cjs +19 -6
- package/lib/protocols/asset/mint.cjs +23 -1
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +117 -1
- package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +21 -1
- package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +14 -9
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +9 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +21 -5
- package/lib/protocols/asset/update.cjs +131 -12
- package/lib/protocols/asset/update.d.cts +13 -2
- package/lib/protocols/factory/create.cjs +14 -6
- package/lib/protocols/governance/claim-stake.cjs +92 -9
- package/lib/protocols/governance/claim-stake.d.cts +1 -1
- package/lib/protocols/governance/return-stake.cjs +44 -11
- package/lib/protocols/governance/return-stake.d.cts +1 -1
- package/lib/protocols/governance/revoke-stake.cjs +17 -5
- package/lib/protocols/governance/revoke-stake.d.cts +1 -1
- package/lib/protocols/governance/slash-stake.cjs +64 -24
- package/lib/protocols/governance/slash-stake.d.cts +1 -1
- package/lib/protocols/governance/stake.cjs +65 -14
- package/lib/protocols/rollup/claim-reward.cjs +39 -20
- package/lib/protocols/rollup/close.cjs +1 -1
- package/lib/protocols/rollup/create-block.cjs +9 -8
- package/lib/protocols/rollup/create-block.d.cts +1 -1
- package/lib/protocols/rollup/create.cjs +18 -7
- package/lib/protocols/rollup/join.cjs +3 -3
- package/lib/protocols/rollup/leave.cjs +1 -1
- package/lib/protocols/rollup/migrate.cjs +7 -4
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +11 -3
- package/lib/protocols/rollup/update.cjs +6 -1
- package/lib/protocols/token/create.cjs +24 -13
- package/lib/protocols/token/deposit-v2.cjs +80 -28
- package/lib/protocols/token/deposit-v2.d.cts +5 -1
- package/lib/protocols/token/withdraw-v2.cjs +32 -15
- package/lib/protocols/token-factory/burn.cjs +42 -9
- package/lib/protocols/token-factory/create.cjs +24 -15
- package/lib/protocols/token-factory/mint.cjs +53 -20
- package/lib/protocols/token-factory/pipes/verify-icon.cjs +1 -1
- package/lib/protocols/token-factory/update.cjs +16 -6
- package/lib/protocols/trade/exchange-v2.cjs +76 -20
- package/lib/protocols/trade/exchange-v2.d.cts +4 -0
- package/lib/protocols/trade/transfer-v2.cjs +45 -14
- package/lib/protocols/trade/transfer-v3.cjs +72 -30
- package/lib/util.cjs +26 -17
- package/lib/util.d.cts +4 -2
- package/package.json +16 -16
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import transfer_default from "../calls/transfer.mjs";
|
|
2
2
|
import transfer_token_default from "../calls/transfer-token.mjs";
|
|
3
|
+
import { BN } from "@ocap/util";
|
|
4
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
3
5
|
import cloneDeep from "lodash/cloneDeep.js";
|
|
4
6
|
|
|
5
7
|
//#region src/protocols/asset/pipes/exec-mint-hook.ts
|
|
@@ -7,6 +9,97 @@ const contractCalls = {
|
|
|
7
9
|
transfer: transfer_default,
|
|
8
10
|
transferToken: transfer_token_default
|
|
9
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Break the tokens paid into a factory into payee legs, for delegation requirements.
|
|
14
|
+
*
|
|
15
|
+
* - non-instant settlement: all tokens are credited to the factory itself
|
|
16
|
+
* - instant settlement with a contract mint hook: each hook call credits its own `to` account
|
|
17
|
+
* (one leg per call, exact amount); whatever is not forwarded by the hook is attributed to the factory
|
|
18
|
+
*
|
|
19
|
+
* One leg per payee (instead of one requirement with a union payee set) keeps delegation `limit.to`
|
|
20
|
+
* pinning strict: a delegation pinned to payee X can not be used to also pay Y.
|
|
21
|
+
*/
|
|
22
|
+
function getMintTokenPayLegs(factory, factoryTokens, officialTokenAddress) {
|
|
23
|
+
const factoryTo = factory?.address ? [factory.address] : [];
|
|
24
|
+
const hook = factory?.settlement === "instant" ? (factory.hooks || []).find((x) => x.name === "mint" && x.type === "contract") : void 0;
|
|
25
|
+
const legs = [];
|
|
26
|
+
const forwarded = /* @__PURE__ */ new Map();
|
|
27
|
+
for (const x of hook?.compiled || []) {
|
|
28
|
+
const tokenAddr = x.call === "transfer" ? officialTokenAddress : x.args?.tokenAddress;
|
|
29
|
+
const to = String(x.args?.to || "");
|
|
30
|
+
let amount = new BN(0);
|
|
31
|
+
try {
|
|
32
|
+
amount = new BN(String(x.args?.amount || "0"));
|
|
33
|
+
} catch {}
|
|
34
|
+
if (!tokenAddr || !to || amount.lte(new BN(0))) continue;
|
|
35
|
+
legs.push({
|
|
36
|
+
address: tokenAddr,
|
|
37
|
+
value: amount.toString(10),
|
|
38
|
+
to: [to]
|
|
39
|
+
});
|
|
40
|
+
forwarded.set(tokenAddr, (forwarded.get(tokenAddr) || new BN(0)).add(amount));
|
|
41
|
+
}
|
|
42
|
+
for (const ft of factoryTokens) {
|
|
43
|
+
const rest = new BN(String(ft.value || "0")).sub(forwarded.get(ft.address) || new BN(0));
|
|
44
|
+
if (rest.gt(new BN(0))) legs.push({
|
|
45
|
+
address: ft.address,
|
|
46
|
+
value: rest.toString(10),
|
|
47
|
+
to: factoryTo
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return legs;
|
|
51
|
+
}
|
|
52
|
+
const verifyMintHook = async (context, next) => {
|
|
53
|
+
const { statedb, factoryState, config } = context;
|
|
54
|
+
const factory = factoryState;
|
|
55
|
+
if (!factory || factory.settlement !== "instant") return next();
|
|
56
|
+
const mintHook = (factory.hooks || []).find((x) => x.name === "mint");
|
|
57
|
+
if (!mintHook || mintHook.type !== "contract") return next();
|
|
58
|
+
const clone = cloneDeep(mintHook.compiled || []);
|
|
59
|
+
const factoryTokens = context.factoryTokens;
|
|
60
|
+
const hookTotals = /* @__PURE__ */ new Map();
|
|
61
|
+
for (const x of clone) {
|
|
62
|
+
const tokenAddr = x.call === "transfer" ? config.token.address : x.args.tokenAddress;
|
|
63
|
+
if (tokenAddr) {
|
|
64
|
+
const prev = hookTotals.get(tokenAddr) || new BN(0);
|
|
65
|
+
hookTotals.set(tokenAddr, prev.add(new BN(String(x.args.amount || "0"))));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (Array.isArray(factoryTokens)) {
|
|
69
|
+
const paidTotals = /* @__PURE__ */ new Map();
|
|
70
|
+
for (const ft of factoryTokens) {
|
|
71
|
+
const prev = paidTotals.get(ft.address) || new BN(0);
|
|
72
|
+
paidTotals.set(ft.address, prev.add(new BN(String(ft.value || "0"))));
|
|
73
|
+
}
|
|
74
|
+
for (const [tokenAddr, totalOut] of hookTotals.entries()) {
|
|
75
|
+
const paid = paidTotals.get(tokenAddr) || new BN(0);
|
|
76
|
+
if (totalOut.gt(paid)) return next(new CustomError("INVALID_FACTORY_HOOK", `Mint hook outflow for ${tokenAddr} exceeds paid factory token input`, { persist: true }));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const filter = context.filter;
|
|
80
|
+
const ownerAddress = context.ownerAddress;
|
|
81
|
+
for (const x of clone) {
|
|
82
|
+
const toAddr = String(x.args.to || "");
|
|
83
|
+
if (filter && typeof filter.isBlocked === "function" && toAddr && await filter.isBlocked(toAddr)) return next(new CustomError("FORBIDDEN", `Account blocked: ${toAddr}`, { persist: true }));
|
|
84
|
+
if (toAddr && toAddr !== ownerAddress) {
|
|
85
|
+
if (x.call === "transfer") {
|
|
86
|
+
x.call = "transferToken";
|
|
87
|
+
x.args.tokenAddress = config.token.address;
|
|
88
|
+
}
|
|
89
|
+
await contractCalls[x.call].runAsync({
|
|
90
|
+
states: context.states,
|
|
91
|
+
statedb,
|
|
92
|
+
txn: context.txn,
|
|
93
|
+
txTime: context.txTime,
|
|
94
|
+
txHash: context.txHash,
|
|
95
|
+
logger: context.logger,
|
|
96
|
+
filter,
|
|
97
|
+
args: x.args
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return next();
|
|
102
|
+
};
|
|
10
103
|
const execMintHook = async (context, next) => {
|
|
11
104
|
const { statedb, factoryState, tokenUpdates, config } = context;
|
|
12
105
|
const factory = factoryState;
|
|
@@ -15,6 +108,26 @@ const execMintHook = async (context, next) => {
|
|
|
15
108
|
if (mintHook && mintHook.type === "contract") {
|
|
16
109
|
context.updatedAccounts = context.updatedAccounts || [];
|
|
17
110
|
const clone = cloneDeep(mintHook.compiled);
|
|
111
|
+
const factoryTokens = context.factoryTokens;
|
|
112
|
+
const hookTotals = /* @__PURE__ */ new Map();
|
|
113
|
+
for (const x of clone) {
|
|
114
|
+
const tokenAddr = x.call === "transfer" ? config.token.address : x.args.tokenAddress;
|
|
115
|
+
if (tokenAddr) {
|
|
116
|
+
const prev = hookTotals.get(tokenAddr) || new BN(0);
|
|
117
|
+
hookTotals.set(tokenAddr, prev.add(new BN(String(x.args.amount || "0"))));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (Array.isArray(factoryTokens)) {
|
|
121
|
+
const paidTotals = /* @__PURE__ */ new Map();
|
|
122
|
+
for (const ft of factoryTokens) {
|
|
123
|
+
const prev = paidTotals.get(ft.address) || new BN(0);
|
|
124
|
+
paidTotals.set(ft.address, prev.add(new BN(String(ft.value || "0"))));
|
|
125
|
+
}
|
|
126
|
+
for (const [tokenAddr, totalOut] of hookTotals.entries()) {
|
|
127
|
+
const paid = paidTotals.get(tokenAddr) || new BN(0);
|
|
128
|
+
if (totalOut.gt(paid)) return next(new CustomError("INVALID_FACTORY_HOOK", `Mint hook outflow for ${tokenAddr} exceeds paid factory token input`, { persist: true }));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
18
131
|
const mintHookStates = {};
|
|
19
132
|
for (const x of clone) {
|
|
20
133
|
if (x.call === "transfer") {
|
|
@@ -28,6 +141,7 @@ const execMintHook = async (context, next) => {
|
|
|
28
141
|
txTime: context.txTime,
|
|
29
142
|
txHash: context.txHash,
|
|
30
143
|
logger: context.logger,
|
|
144
|
+
filter: context.filter,
|
|
31
145
|
args: x.args
|
|
32
146
|
});
|
|
33
147
|
mintHookStates[updateSet.address] = await statedb.account.update(updateSet.address, updateSet.updates, context);
|
|
@@ -51,4 +165,4 @@ const execMintHook = async (context, next) => {
|
|
|
51
165
|
var exec_mint_hook_default = execMintHook;
|
|
52
166
|
|
|
53
167
|
//#endregion
|
|
54
|
-
export { exec_mint_hook_default as default };
|
|
168
|
+
export { exec_mint_hook_default as default, getMintTokenPayLegs, verifyMintHook };
|
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import { BN } from "@ocap/util";
|
|
2
2
|
import { createSortedList } from "@ocap/util/lib/create-sorted-list";
|
|
3
|
-
import uniqBy from "lodash/uniqBy.js";
|
|
4
3
|
|
|
5
4
|
//#region src/protocols/asset/pipes/extract-factory-tokens.ts
|
|
6
5
|
const extract_factory_tokens = (context, next) => {
|
|
7
6
|
const { factoryState, config } = context;
|
|
8
7
|
const input = factoryState.input;
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
address
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
const merged = /* @__PURE__ */ new Map();
|
|
9
|
+
for (const item of input.tokens || []) {
|
|
10
|
+
const prev = merged.get(item.address) || new BN(0);
|
|
11
|
+
merged.set(item.address, prev.add(new BN(item.value || "0")));
|
|
12
|
+
}
|
|
13
|
+
if (new BN(input.value || "0").gt(new BN(0))) {
|
|
14
|
+
const primaryAddr = config.token.address;
|
|
15
|
+
const prev = merged.get(primaryAddr) || new BN(0);
|
|
16
|
+
merged.set(primaryAddr, prev.add(new BN(input.value)));
|
|
17
|
+
}
|
|
18
|
+
context.factoryTokens = Array.from(merged.entries()).map(([address, val]) => ({
|
|
19
|
+
address,
|
|
20
|
+
value: val.toString()
|
|
21
|
+
}));
|
|
16
22
|
context.tokenAddress = createSortedList((context.factoryTokens || []).map((x) => x.address));
|
|
17
23
|
next();
|
|
18
24
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isFromPublicKey, normalizePublicKeyHex } from "@arcblock/did";
|
|
1
2
|
import { CustomError } from "@ocap/util/lib/error";
|
|
2
3
|
import Debug from "debug";
|
|
3
4
|
import { mintFromFactory } from "@ocap/asset";
|
|
@@ -26,6 +27,14 @@ const verify_itx_address = (mode) => (context, next) => {
|
|
|
26
27
|
name: sender.moniker
|
|
27
28
|
};
|
|
28
29
|
}
|
|
30
|
+
if (mode === "acquire-v2" || mode === "acquire-v3") {
|
|
31
|
+
const issuerState = context.issuerState;
|
|
32
|
+
const rawIssuer = itx.issuer;
|
|
33
|
+
const rawPk = typeof rawIssuer?.pk === "string" ? rawIssuer.pk.trim() : rawIssuer?.pk;
|
|
34
|
+
if (issuerState && !(rawPk === void 0 || rawPk === null || rawPk === "" || rawPk === "z" || rawPk === "0x")) {
|
|
35
|
+
if (!(issuerState.pk ? normalizePublicKeyHex(rawIssuer.pk) === normalizePublicKeyHex(issuerState.pk) : isFromPublicKey(issuerState.address, rawIssuer.pk))) return next(new CustomError("INVALID_ASSET", "Invalid itx.address / itx.issuer.pk: does not match with minted asset address (check that itx.issuer.id/itx.issuer.pk match the trusted issuer's actual on-chain public key — a stale or wrong issuer.pk is the most common cause)"));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
29
38
|
const minted = mintFromFactory({
|
|
30
39
|
factory,
|
|
31
40
|
inputs,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { token } from "@ocap/state";
|
|
1
2
|
import { CustomError } from "@ocap/util/lib/error";
|
|
2
3
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
3
4
|
|
|
@@ -6,19 +7,34 @@ const verify_itx_assets = (context, next) => {
|
|
|
6
7
|
const { factoryState, expectedFactoryStates = [], assetStates: actualAssetStates = [], itx } = context;
|
|
7
8
|
const expectedAssets = factoryState.input.assets;
|
|
8
9
|
const assetStates = actualAssetStates;
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
const rawInputAssets = getListField(itx, "assets") || [];
|
|
11
|
+
const inputAssets = rawInputAssets.length > 0 ? rawInputAssets : context.assets || [];
|
|
12
|
+
if (new Set(inputAssets).size !== inputAssets.length) return next(new CustomError("INVALID_ASSET", "Duplicate input assets are not allowed"));
|
|
13
|
+
if (inputAssets.length !== assetStates.length) return next(new CustomError("INVALID_ASSET", "Input asset does not exist on chain (asset not found)"));
|
|
14
|
+
if (assetStates.some((x) => token.isTokenCharterAsset(x))) return next(new CustomError("FORBIDDEN", "Token charter asset cannot be consumed as factory input"));
|
|
15
|
+
if (assetStates.some((x) => Boolean(x.consumedTime))) return next(new CustomError("INVALID_ASSET", "Some input asset already consumed"));
|
|
16
|
+
if (expectedAssets.length === 0) {
|
|
17
|
+
if (inputAssets.length > 0 || assetStates.length > 0) return next(new CustomError("INVALID_ASSET", "Factory does not accept input assets"));
|
|
18
|
+
return next();
|
|
19
|
+
}
|
|
11
20
|
if (expectedFactoryStates.length === expectedAssets.length) {
|
|
21
|
+
if (assetStates.length === 0) return next(new CustomError("INVALID_ASSET", "Input asset is required by factory.input.assets"));
|
|
12
22
|
if (assetStates.every((x) => x.parent && expectedAssets.includes(x.parent)) === false) return next(new CustomError("INVALID_ASSET", "Input asset not minted from any of factory.input.assets"));
|
|
13
|
-
if (assetStates.every((x) => !x.consumedTime) === false) return next(new CustomError("INVALID_ASSET", "Some input asset already consumed"));
|
|
14
23
|
return next();
|
|
15
24
|
}
|
|
25
|
+
const usedAssetAddresses = /* @__PURE__ */ new Set();
|
|
16
26
|
for (const address of expectedAssets) {
|
|
17
|
-
const plainAsset = assetStates.find((x) => x.address === address);
|
|
27
|
+
const plainAsset = assetStates.find((x) => x.address === address && !usedAssetAddresses.has(x.address));
|
|
18
28
|
if (plainAsset) {
|
|
19
29
|
if (plainAsset.consumedTime) return next(new CustomError("INVALID_ASSET", `Input asset ${address} already consumed`));
|
|
20
|
-
|
|
30
|
+
usedAssetAddresses.add(plainAsset.address);
|
|
31
|
+
} else {
|
|
32
|
+
const childAsset = assetStates.find((x) => x.parent === address && !x.consumedTime && !usedAssetAddresses.has(x.address));
|
|
33
|
+
if (!childAsset) return next(new CustomError("INVALID_ASSET", `Input asset not found ${address}`));
|
|
34
|
+
usedAssetAddresses.add(childAsset.address);
|
|
35
|
+
}
|
|
21
36
|
}
|
|
37
|
+
if (assetStates.length !== expectedAssets.length) return next(new CustomError("INVALID_ASSET", "Input assets count does not match factory.input.assets"));
|
|
22
38
|
return next();
|
|
23
39
|
};
|
|
24
40
|
var verify_itx_assets_default = verify_itx_assets;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Runner } from "@ocap/tx-pipeline";
|
|
2
2
|
import { AnyData } from "@ocap/message";
|
|
3
|
-
import { IReadyContext, IWithAsset, IWithIssuer, IWithSender, TUpdateAssetTx } from "@ocap/types";
|
|
3
|
+
import { IReadyContext, IWithAsset, IWithDelegation, IWithDelegator, IWithIssuer, IWithSender, TUpdateAssetTx } from "@ocap/types";
|
|
4
4
|
|
|
5
5
|
//#region src/protocols/asset/update.d.ts
|
|
6
6
|
|
|
@@ -9,9 +9,20 @@ import { IReadyContext, IWithAsset, IWithIssuer, IWithSender, TUpdateAssetTx } f
|
|
|
9
9
|
* Phase: IReadyContext (after TakeStateSnapshot)
|
|
10
10
|
* Mixins: IWithAsset (required), IWithSender (required), IWithIssuer (optional)
|
|
11
11
|
*/
|
|
12
|
-
type Context = IReadyContext<TUpdateAssetTx> & IWithAsset & IWithSender & Partial<IWithIssuer> & {
|
|
12
|
+
type Context = IReadyContext<TUpdateAssetTx> & IWithAsset & IWithSender & Partial<IWithDelegator> & Partial<IWithDelegation> & Partial<IWithIssuer> & {
|
|
13
13
|
/** Decoded new data for asset update, null means no update */
|
|
14
14
|
newData: AnyData | null;
|
|
15
|
+
/** Whether delegation was changed */
|
|
16
|
+
isDelegationChanged?: boolean;
|
|
17
|
+
/** Token info for website ownership verification */
|
|
18
|
+
tokenInfoForVerification?: {
|
|
19
|
+
symbol: string;
|
|
20
|
+
address: string;
|
|
21
|
+
website?: string | null;
|
|
22
|
+
};
|
|
23
|
+
/** Incoming website/communityUrl for URL verification */
|
|
24
|
+
charterWebsite?: string;
|
|
25
|
+
charterCommunityUrl?: string;
|
|
15
26
|
};
|
|
16
27
|
declare const runner: Runner<Context>;
|
|
17
28
|
//#endregion
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { decodeAnySafe } from "../../util.mjs";
|
|
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
|
|
4
|
+
import verify_ownership_default from "../token-factory/pipes/verify-ownership.mjs";
|
|
5
|
+
import verify_url_default from "../token-factory/pipes/verify-url.mjs";
|
|
6
|
+
import { account, asset, delegation, token } from "@ocap/state";
|
|
5
7
|
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
8
|
+
import { isSameDid } from "@ocap/util";
|
|
6
9
|
import { CustomError } from "@ocap/util/lib/error";
|
|
10
|
+
import { getRelatedAddresses } from "@ocap/util/lib/get-related-addr";
|
|
7
11
|
import Debug from "debug";
|
|
8
12
|
import { v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
|
|
13
|
+
import { isSvgFile, sanitizeSvg } from "@blocklet/xss";
|
|
9
14
|
import { diff } from "deep-diff";
|
|
10
15
|
|
|
11
16
|
//#region src/protocols/asset/update.ts
|
|
@@ -51,11 +56,47 @@ runner.use(pipes.VerifyAccountMigration({
|
|
|
51
56
|
stateKey: "senderState",
|
|
52
57
|
addressKey: "tx.from"
|
|
53
58
|
}));
|
|
54
|
-
runner.use(pipes.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
runner.use(pipes.ExtractState({
|
|
60
|
+
from: "tx.delegator",
|
|
61
|
+
to: "delegatorState",
|
|
62
|
+
status: "OK",
|
|
63
|
+
table: "account"
|
|
58
64
|
}));
|
|
65
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
66
|
+
stateKey: "delegatorState",
|
|
67
|
+
addressKey: "tx.delegator"
|
|
68
|
+
}));
|
|
69
|
+
runner.use(pipes.VerifyDelegation({
|
|
70
|
+
type: "signature",
|
|
71
|
+
signerKey: "senderState",
|
|
72
|
+
delegatorKey: "delegatorState",
|
|
73
|
+
getRequirements: (ctx) => [...getDelegationRequirements(ctx), ...ctx.itx?.address ? [{
|
|
74
|
+
type: "asset",
|
|
75
|
+
address: ctx.itx.address,
|
|
76
|
+
to: []
|
|
77
|
+
}] : []]
|
|
78
|
+
}));
|
|
79
|
+
runner.use(pipes.VerifyBlocked({ stateKeys: [
|
|
80
|
+
"senderState",
|
|
81
|
+
"delegatorState",
|
|
82
|
+
"issuerState"
|
|
83
|
+
] }));
|
|
84
|
+
runner.use((context, next) => {
|
|
85
|
+
if (Boolean(context.isImplicitDelegation) && Boolean(context.assetState?.address) && (isSameDid(context.tx.from, context.assetState.address) || isSameDid(context.tx.from, context.assetState.owner) || Boolean(context.assetState.issuer) && isSameDid(context.tx.from, context.assetState.issuer))) return next();
|
|
86
|
+
const senderRelated = context.senderState ? getRelatedAddresses(context.senderState) : [];
|
|
87
|
+
const isSenderOwnerOrIssuer = senderRelated.some((a) => isSameDid(a, context.assetState.owner)) || Boolean(context.assetState.issuer) && senderRelated.some((a) => isSameDid(a, context.assetState.issuer));
|
|
88
|
+
if (context.tx?.delegator && !context.delegatorState) {
|
|
89
|
+
const isUndeclaredDelegatorOwnerOrIssuer = Boolean(context.assetState) && (context.assetState.owner && isSameDid(context.tx.delegator, context.assetState.owner) || context.assetState.issuer && isSameDid(context.tx.delegator, context.assetState.issuer));
|
|
90
|
+
if (isSenderOwnerOrIssuer || isUndeclaredDelegatorOwnerOrIssuer) return next();
|
|
91
|
+
return next(new CustomError("INVALID_OWNER", "Asset is not owned by the given owner", { persist: true }));
|
|
92
|
+
}
|
|
93
|
+
const ownerKey = isSenderOwnerOrIssuer ? "senderState" : context.delegatorState ? "delegatorState" : "senderState";
|
|
94
|
+
return pipes.VerifyUpdater({
|
|
95
|
+
assetKey: "assetState",
|
|
96
|
+
ownerKey,
|
|
97
|
+
updaterKey: ["owner", "issuer"]
|
|
98
|
+
})(context, next);
|
|
99
|
+
});
|
|
59
100
|
runner.use(pipes.VerifyInfo([{
|
|
60
101
|
error: "READONLY_ASSET",
|
|
61
102
|
message: "Can not update a readonly asset",
|
|
@@ -70,15 +111,47 @@ runner.use(pipes.VerifyInfo([{
|
|
|
70
111
|
},
|
|
71
112
|
persist: true
|
|
72
113
|
}]));
|
|
73
|
-
runner.use((context, next) => {
|
|
74
|
-
const { itx, newData, senderState, assetState,
|
|
75
|
-
|
|
114
|
+
runner.use(async (context, next) => {
|
|
115
|
+
const { tx: tx$1, itx, newData, senderState, delegatorState, assetState, statedb } = context;
|
|
116
|
+
const senderRelated = senderState ? getRelatedAddresses(senderState) : [];
|
|
117
|
+
const actorState = senderRelated.some((a) => isSameDid(a, assetState.owner)) || Boolean(assetState.issuer) && senderRelated.some((a) => isSameDid(a, assetState.issuer)) ? senderState : delegatorState;
|
|
118
|
+
const isOwnerActor = (actorState ? getRelatedAddresses(actorState) : [tx$1?.delegator || senderState.address]).some((a) => isSameDid(a, assetState.owner)) || Boolean(context.isImplicitDelegation) && (isSameDid(tx$1.from, assetState.address) || isSameDid(tx$1.from, assetState.owner));
|
|
119
|
+
if (token.isTokenCharterAsset(assetState)) {
|
|
120
|
+
if (!isOwnerActor) return next(new CustomError("FORBIDDEN", "Token charter asset can only be updated by owner"));
|
|
121
|
+
if (itx.consumed) return next(new CustomError("FORBIDDEN", "Token charter asset cannot be consumed"));
|
|
122
|
+
const incomingValue = newData && newData.type === "json" && newData.value && typeof newData.value === "object" ? newData.value : {};
|
|
123
|
+
if (typeof incomingValue.icon === "string" && incomingValue.icon.trim()) {
|
|
124
|
+
const rawIcon = incomingValue.icon.trim();
|
|
125
|
+
if (rawIcon.startsWith("<svg") || isSvgFile(rawIcon)) {
|
|
126
|
+
if (!isSvgFile(rawIcon)) return next(new CustomError("INVALID_ICON", "Token Icon is not a valid SVG file"));
|
|
127
|
+
if (Buffer.byteLength(rawIcon, "utf8") > 16 * 1024) return next(new CustomError("INVALID_ICON", "Token Icon must be less than 16k"));
|
|
128
|
+
incomingValue.icon = sanitizeSvg(rawIcon, { preserveCase: true });
|
|
129
|
+
} else if (!token.iconToNftDisplay(rawIcon)) return next(new CustomError("INVALID_ICON", "Token Icon must be a valid SVG, HTTPS URL, or safe image data URI"));
|
|
130
|
+
}
|
|
131
|
+
if (typeof incomingValue.website === "string" && incomingValue.website.trim()) context.charterWebsite = incomingValue.website.trim();
|
|
132
|
+
const incomingMeta = incomingValue.metadata;
|
|
133
|
+
if (incomingMeta && typeof incomingMeta === "object" && typeof incomingMeta.communityUrl === "string" && incomingMeta.communityUrl.trim()) context.charterCommunityUrl = incomingMeta.communityUrl.trim();
|
|
134
|
+
const targetTokenAddress = assetState.data?.value?.tokenAddress;
|
|
135
|
+
if (targetTokenAddress && statedb?.token?.get) {
|
|
136
|
+
const existingTokenState = await statedb.token.get(targetTokenAddress, context);
|
|
137
|
+
if (existingTokenState && context.charterWebsite && context.charterWebsite !== (existingTokenState.website || "")) context.tokenInfoForVerification = {
|
|
138
|
+
symbol: existingTokenState.symbol,
|
|
139
|
+
address: existingTokenState.address,
|
|
140
|
+
website: context.charterWebsite
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return next();
|
|
144
|
+
}
|
|
145
|
+
if (!isOwnerActor) {
|
|
146
|
+
if (itx.consumed) return next(new CustomError("FORBIDDEN", "Asset can only be consumed by owner in update_asset"));
|
|
76
147
|
if (itx.moniker !== assetState.moniker) return next(new CustomError("FORBIDDEN", "Asset moniker can only be updated by owner"));
|
|
77
148
|
if (newData?.type !== assetState.data?.type) return next(new CustomError("FORBIDDEN", "Asset data type can only be updated by owner"));
|
|
78
149
|
if ((diff(assetState.data?.value, newData?.value) || []).every((x) => x.kind === "N" || x.kind === "A" && x.item.kind === "N") === false) return next(new CustomError("APPEND_ONLY", "Asset data value can only be updated in append only mode"));
|
|
79
150
|
}
|
|
80
151
|
return next();
|
|
81
152
|
});
|
|
153
|
+
runner.use(verify_url_default({ urlKeys: ["charterWebsite", "charterCommunityUrl"] }));
|
|
154
|
+
runner.use(verify_ownership_default({ tokenKey: "tokenInfoForVerification" }));
|
|
82
155
|
runner.use(ensure_gas_default(() => ({
|
|
83
156
|
create: 0,
|
|
84
157
|
update: 2,
|
|
@@ -93,14 +166,60 @@ runner.use(async (context, next) => {
|
|
|
93
166
|
data: newData || assetState.data
|
|
94
167
|
};
|
|
95
168
|
if (itx.consumed && !assetState.consumedTime) updates.consumedTime = txTime;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
169
|
+
let pendingTokenUpdate = null;
|
|
170
|
+
if (token.isTokenCharterAsset(assetState) && statedb.token?.get) {
|
|
171
|
+
const targetTokenAddress = assetState.data?.value?.tokenAddress;
|
|
172
|
+
if (targetTokenAddress) {
|
|
173
|
+
const existingTokenState = await statedb.token.get(targetTokenAddress, context);
|
|
174
|
+
if (existingTokenState) {
|
|
175
|
+
const incomingValue = newData && newData.type === "json" && newData.value && typeof newData.value === "object" ? newData.value : {};
|
|
176
|
+
const mergedCandidate = {
|
|
177
|
+
...assetState,
|
|
178
|
+
moniker: itx.moniker || assetState.moniker,
|
|
179
|
+
data: {
|
|
180
|
+
type: "json",
|
|
181
|
+
value: {
|
|
182
|
+
...assetState.data?.value || {},
|
|
183
|
+
...incomingValue,
|
|
184
|
+
kind: token.TOKEN_CHARTER_KIND,
|
|
185
|
+
tokenAddress: existingTokenState.address,
|
|
186
|
+
symbol: existingTokenState.symbol,
|
|
187
|
+
unit: existingTokenState.unit,
|
|
188
|
+
decimal: existingTokenState.decimal,
|
|
189
|
+
tokenFactoryAddress: existingTokenState.tokenFactoryAddress || ""
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const tokenUpdates = token.extractTokenUpdatesFromCharterAsset(existingTokenState, mergedCandidate);
|
|
194
|
+
const safeTokenBase = token.normalizeLegacyTokenStateForUpdate(existingTokenState);
|
|
195
|
+
const nextTokenState = token.update(safeTokenBase, tokenUpdates);
|
|
196
|
+
pendingTokenUpdate = {
|
|
197
|
+
address: existingTokenState.address,
|
|
198
|
+
state: nextTokenState
|
|
199
|
+
};
|
|
200
|
+
updates.moniker = token.resolveCharterMoniker(nextTokenState);
|
|
201
|
+
updates.data = token.buildTokenCharterAssetData(nextTokenState);
|
|
202
|
+
const display = token.iconToNftDisplay(nextTokenState.icon);
|
|
203
|
+
const endpoint = token.websiteToNftEndpoint(nextTokenState.website);
|
|
204
|
+
updates.display = display ?? void 0;
|
|
205
|
+
updates.endpoint = endpoint ?? void 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const [newSenderState, newAssetState, , newDelegationState] = await Promise.all([
|
|
210
|
+
statedb.account.update(senderState.address, account.update(senderState, {
|
|
211
|
+
nonce: tx$1.nonce,
|
|
212
|
+
pk: tx$1.delegator ? senderState.pk : tx$1.pk,
|
|
213
|
+
...senderUpdates
|
|
214
|
+
}, context), context),
|
|
215
|
+
statedb.asset.update(itx.address, asset.update(assetState, updates, context), context),
|
|
216
|
+
pendingTokenUpdate && statedb.token?.update ? statedb.token.update(pendingTokenUpdate.address, pendingTokenUpdate.state, context) : null,
|
|
217
|
+
context.isDelegationChanged && context.delegationState ? statedb.delegation.update(context.delegationState.address, delegation.update(context.delegationState, {}, context), context) : context.delegationState
|
|
218
|
+
]);
|
|
101
219
|
if (updateVaults) await updateVaults();
|
|
102
220
|
context.senderState = newSenderState;
|
|
103
221
|
context.assetState = newAssetState;
|
|
222
|
+
if (newDelegationState) context.delegationState = newDelegationState;
|
|
104
223
|
debug("update", newAssetState);
|
|
105
224
|
next();
|
|
106
225
|
}, { persistError: true });
|
|
@@ -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 {
|
|
4
|
+
import { toFactoryAddress } from "@arcblock/did-util";
|
|
5
|
+
import { account, delegation, factory, token } from "@ocap/state";
|
|
5
6
|
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
6
7
|
import { BN } from "@ocap/util";
|
|
7
8
|
import { CustomError } from "@ocap/util/lib/error";
|
|
9
|
+
import cloneDeep from "lodash/cloneDeep.js";
|
|
8
10
|
import get from "lodash/get.js";
|
|
9
|
-
import { toFactoryAddress } from "@arcblock/did-util";
|
|
10
11
|
import Debug from "debug";
|
|
11
12
|
import { formatMessage } from "@ocap/message";
|
|
12
|
-
import cloneDeep from "lodash/cloneDeep.js";
|
|
13
13
|
import { isValidFactory } from "@ocap/asset";
|
|
14
14
|
import isEmpty from "lodash/isEmpty.js";
|
|
15
15
|
|
|
@@ -34,9 +34,11 @@ runner.use((context, next) => {
|
|
|
34
34
|
if (factoryTokenAddresses.length > maxListSize) return next(new CustomError("INVALID_FACTORY_INPUT", `input.tokens exceeded max allowed length: ${maxListSize}`));
|
|
35
35
|
if (factoryProps.input.assets.length > maxListSize) return next(new CustomError("INVALID_FACTORY_INPUT", `input.assets exceeded max allowed length: ${maxListSize}`));
|
|
36
36
|
if (new BN(factoryProps.input.value || 0).gt(new BN(0))) {
|
|
37
|
-
|
|
37
|
+
const primaryToken = context.config.token.address;
|
|
38
|
+
if (factoryTokenAddresses.includes(primaryToken)) return next(new CustomError("INVALID_FACTORY_PROPS", "Factory token input should not contain primary token when input.value is set"));
|
|
39
|
+
factoryTokenAddresses.push(primaryToken);
|
|
38
40
|
factoryProps.input.tokens.push({
|
|
39
|
-
address:
|
|
41
|
+
address: primaryToken,
|
|
40
42
|
value: factoryProps.input.value
|
|
41
43
|
});
|
|
42
44
|
factoryProps.input.value = "0";
|
|
@@ -104,6 +106,7 @@ runner.use(pipes.ExtractState({
|
|
|
104
106
|
runner.use((context, next) => {
|
|
105
107
|
const { inputAssetStates = [], inputFactoryStates = [], factoryProps } = context;
|
|
106
108
|
if (inputAssetStates.some((x) => !!x.consumedTime)) return next(new CustomError("INVALID_FACTORY_INPUT", "Some of input.assets already consumed"));
|
|
109
|
+
if (inputAssetStates.some((x) => token.isTokenCharterAsset(x))) return next(new CustomError("INVALID_FACTORY_INPUT", "Token charter asset cannot be used as factory input"));
|
|
107
110
|
if (inputAssetStates.length + inputFactoryStates.length === factoryProps.input.assets.length) return next();
|
|
108
111
|
return next(new CustomError("INVALID_FACTORY_INPUT", "Not all input.assets exist on chain"));
|
|
109
112
|
});
|
|
@@ -118,6 +121,11 @@ runner.use(pipes.VerifyAccountMigration({
|
|
|
118
121
|
stateKey: "issuerStates",
|
|
119
122
|
addressKey: "factoryProps.trustedIssuers"
|
|
120
123
|
}));
|
|
124
|
+
runner.use(pipes.VerifyBlocked({ stateKeys: [
|
|
125
|
+
"senderState",
|
|
126
|
+
"delegatorState",
|
|
127
|
+
"issuerStates"
|
|
128
|
+
] }));
|
|
121
129
|
runner.use(ensure_gas_default(() => ({
|
|
122
130
|
create: 1,
|
|
123
131
|
update: 2,
|
|
@@ -132,7 +140,7 @@ runner.use(async (context, next) => {
|
|
|
132
140
|
const [newSenderState, factoryState, newDelegationState] = await Promise.all([
|
|
133
141
|
statedb.account.update(senderState.address, account.update(senderState, {
|
|
134
142
|
nonce: tx$1.nonce,
|
|
135
|
-
pk: tx$1.pk,
|
|
143
|
+
pk: tx$1.delegator ? senderState.pk : tx$1.pk,
|
|
136
144
|
...senderUpdates
|
|
137
145
|
}, context), context),
|
|
138
146
|
statedb.factory.create(itx.address, factory.create({
|