@ocap/tx-pipeline 1.30.26 → 1.31.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/appchain-sovereign-hub.d.mts +160 -0
- package/esm/appchain-sovereign-hub.mjs +348 -0
- package/esm/arcvault-external-adapter.d.mts +166 -0
- package/esm/arcvault-external-adapter.mjs +376 -0
- package/esm/encrypted-nft-envelope.d.mts +172 -0
- package/esm/encrypted-nft-envelope.mjs +458 -0
- package/esm/evm-simplified-bridge.d.mts +1370 -0
- package/esm/evm-simplified-bridge.mjs +3875 -0
- package/esm/give-to-mint-karma.d.mts +472 -0
- package/esm/give-to-mint-karma.mjs +1057 -0
- package/esm/helpers/replay-guard.d.mts +15 -0
- package/esm/helpers/replay-guard.mjs +23 -0
- package/esm/index.d.mts +10 -5
- package/esm/index.mjs +14 -10
- package/esm/legacy-protocol-sunset.d.mts +251 -0
- package/esm/legacy-protocol-sunset.mjs +1519 -0
- package/esm/library.d.mts +16 -0
- package/esm/library.mjs +17 -0
- package/esm/native-abt-exchange.d.mts +2055 -0
- package/esm/native-abt-exchange.mjs +7250 -0
- package/esm/ofac-screening.d.mts +17 -0
- package/esm/ofac-screening.mjs +39 -0
- package/esm/pipes/anti-land-attack.mjs +1 -1
- package/esm/pipes/decode-itx.mjs +46 -4
- package/esm/pipes/decode-tx.d.mts +2 -3
- package/esm/pipes/decode-tx.mjs +56 -115
- package/esm/pipes/extract-signer.mjs +2 -2
- package/esm/pipes/extract-state.mjs +14 -3
- package/esm/pipes/post-execution-invariant-gate.d.mts +61 -0
- package/esm/pipes/post-execution-invariant-gate.mjs +789 -0
- package/esm/pipes/pre-flight-waf.d.mts +73 -0
- package/esm/pipes/pre-flight-waf.mjs +756 -0
- package/esm/pipes/take-state-snapshot.mjs +3 -2
- package/esm/pipes/update-owner.mjs +39 -4
- package/esm/pipes/verify-account-migration.mjs +14 -4
- package/esm/pipes/verify-blocked.mjs +1 -1
- package/esm/pipes/verify-delegation.d.mts +22 -3
- package/esm/pipes/verify-delegation.mjs +259 -36
- package/esm/pipes/verify-list-size.mjs +1 -1
- package/esm/pipes/verify-multisig-v2.mjs +15 -7
- package/esm/pipes/verify-multisig.mjs +15 -7
- package/esm/pipes/verify-sender.mjs +1 -1
- package/esm/pipes/verify-signature.mjs +35 -6
- package/esm/pipes/verify-signer.mjs +2 -2
- package/esm/pipes/verify-state-diff.mjs +3 -3
- package/esm/pipes/verify-token-access.mjs +1 -1
- package/esm/pipes/verify-token-balance.mjs +13 -5
- package/esm/pipes/verify-transferrable.d.mts +8 -1
- package/esm/pipes/verify-transferrable.mjs +3 -3
- package/esm/pipes/verify-tx-input.mjs +2 -2
- package/esm/pipes/verify-tx-size.mjs +46 -2
- package/esm/pipes/verify-tx.mjs +5 -7
- package/esm/pipes/verify-updater.mjs +1 -1
- package/esm/social-economy-factories.d.mts +346 -0
- package/esm/social-economy-factories.mjs +845 -0
- package/esm/super-suites-foundation.d.mts +513 -0
- package/esm/super-suites-foundation.mjs +1241 -0
- package/esm/transaction-block.d.mts +471 -0
- package/esm/transaction-block.mjs +1906 -0
- package/esm/uniswap-erc7683-solver.d.mts +201 -0
- package/esm/uniswap-erc7683-solver.mjs +321 -0
- package/esm/uniswap-lp-copilot.d.mts +113 -0
- package/esm/uniswap-lp-copilot.mjs +320 -0
- package/esm/uniswap-router.d.mts +335 -0
- package/esm/uniswap-router.mjs +578 -0
- package/esm/uniswap-token-factory-graduation.d.mts +187 -0
- package/esm/uniswap-token-factory-graduation.mjs +426 -0
- package/esm/uniswap-v4-hook.d.mts +173 -0
- package/esm/uniswap-v4-hook.mjs +517 -0
- package/lib/appchain-sovereign-hub.cjs +352 -0
- package/lib/appchain-sovereign-hub.d.cts +160 -0
- package/lib/arcvault-external-adapter.cjs +383 -0
- package/lib/arcvault-external-adapter.d.cts +166 -0
- package/lib/encrypted-nft-envelope.cjs +477 -0
- package/lib/encrypted-nft-envelope.d.cts +172 -0
- package/lib/evm-simplified-bridge.cjs +3943 -0
- package/lib/evm-simplified-bridge.d.cts +1370 -0
- package/lib/give-to-mint-karma.cjs +1084 -0
- package/lib/give-to-mint-karma.d.cts +472 -0
- package/lib/helpers/replay-guard.cjs +27 -0
- package/lib/helpers/replay-guard.d.cts +15 -0
- package/lib/index.cjs +49 -16
- package/lib/index.d.cts +10 -5
- package/lib/legacy-protocol-sunset.cjs +1543 -0
- package/lib/legacy-protocol-sunset.d.cts +251 -0
- package/lib/library.cjs +303 -0
- package/lib/library.d.cts +16 -0
- package/lib/native-abt-exchange.cjs +7322 -0
- package/lib/native-abt-exchange.d.cts +2055 -0
- package/lib/ofac-screening.cjs +42 -0
- package/lib/ofac-screening.d.cts +17 -0
- package/lib/pipes/anti-land-attack.cjs +2 -2
- package/lib/pipes/decode-itx.cjs +46 -4
- package/lib/pipes/decode-itx.d.cts +1 -1
- package/lib/pipes/decode-tx.cjs +55 -116
- package/lib/pipes/decode-tx.d.cts +3 -4
- package/lib/pipes/extract-signer.cjs +2 -2
- package/lib/pipes/extract-state.cjs +14 -3
- package/lib/pipes/post-execution-invariant-gate.cjs +800 -0
- package/lib/pipes/post-execution-invariant-gate.d.cts +61 -0
- package/lib/pipes/pre-flight-waf.cjs +772 -0
- package/lib/pipes/pre-flight-waf.d.cts +73 -0
- package/lib/pipes/take-state-snapshot.cjs +3 -2
- package/lib/pipes/update-owner.cjs +40 -5
- package/lib/pipes/update-owner.d.cts +1 -1
- package/lib/pipes/verify-account-migration.cjs +14 -4
- package/lib/pipes/verify-blocked.cjs +1 -1
- package/lib/pipes/verify-delegation.cjs +259 -36
- package/lib/pipes/verify-delegation.d.cts +22 -3
- package/lib/pipes/verify-list-size.cjs +1 -1
- package/lib/pipes/verify-multisig-v2.cjs +15 -7
- package/lib/pipes/verify-multisig.cjs +15 -7
- package/lib/pipes/verify-sender.cjs +1 -1
- package/lib/pipes/verify-service-fee.d.cts +1 -1
- package/lib/pipes/verify-signature.cjs +35 -6
- package/lib/pipes/verify-signature.d.cts +1 -1
- package/lib/pipes/verify-signer.cjs +2 -2
- package/lib/pipes/verify-state-diff.cjs +4 -4
- package/lib/pipes/verify-token-access.cjs +2 -2
- package/lib/pipes/verify-token-balance.cjs +13 -5
- package/lib/pipes/verify-transferrable.cjs +3 -3
- package/lib/pipes/verify-transferrable.d.cts +8 -1
- package/lib/pipes/verify-tx-input.cjs +3 -3
- package/lib/pipes/verify-tx-size.cjs +46 -2
- package/lib/pipes/verify-tx-size.d.cts +1 -1
- package/lib/pipes/verify-tx.cjs +6 -8
- package/lib/pipes/verify-tx.d.cts +1 -1
- package/lib/pipes/verify-updater.cjs +1 -1
- package/lib/social-economy-factories.cjs +852 -0
- package/lib/social-economy-factories.d.cts +346 -0
- package/lib/super-suites-foundation.cjs +1257 -0
- package/lib/super-suites-foundation.d.cts +513 -0
- package/lib/transaction-block.cjs +1941 -0
- package/lib/transaction-block.d.cts +471 -0
- package/lib/uniswap-erc7683-solver.cjs +327 -0
- package/lib/uniswap-erc7683-solver.d.cts +201 -0
- package/lib/uniswap-lp-copilot.cjs +328 -0
- package/lib/uniswap-lp-copilot.d.cts +113 -0
- package/lib/uniswap-router.cjs +593 -0
- package/lib/uniswap-router.d.cts +335 -0
- package/lib/uniswap-token-factory-graduation.cjs +431 -0
- package/lib/uniswap-token-factory-graduation.d.cts +187 -0
- package/lib/uniswap-v4-hook.cjs +527 -0
- package/lib/uniswap-v4-hook.d.cts +173 -0
- package/package.json +11 -10
- package/esm/helpers/dedup-height.d.mts +0 -16
- package/esm/helpers/dedup-height.mjs +0 -61
- package/lib/helpers/dedup-height.cjs +0 -72
- package/lib/helpers/dedup-height.d.cts +0 -16
|
@@ -15,8 +15,9 @@ function CreateTakeStateSnapshotPipe({ key = "stateSnapshot" } = {}) {
|
|
|
15
15
|
const ctx = context;
|
|
16
16
|
if (ctx.tx?.from && ctx.senderState?.address && ctx.senderState.address !== ctx.tx.from && !isSameDid(ctx.senderState.address, ctx.tx.from)) return next(new CustomError("ACCOUNT_MIGRATED", "Cannot send tx from migrated account"));
|
|
17
17
|
if (ctx.tx && !context.txType?.startsWith("fg:x:")) {
|
|
18
|
-
|
|
19
|
-
if (
|
|
18
|
+
const isSelfDelegationProtocol = context.txType === "fg:t:delegate" || context.txType === "fg:t:revoke_delegate";
|
|
19
|
+
if (ctx.tx.delegator && (isSelfDelegationProtocol || !ctx.isDelegationVerified && !ctx.delegationState && !ctx.isImplicitDelegation)) return next(new CustomError("INVALID_DELEGATION", "Delegation is not supported for this transaction"));
|
|
20
|
+
if (getListField(ctx.tx, "signatures").some((s) => Boolean(s?.delegator)) && (isSelfDelegationProtocol || !ctx.isMultiSigDelegationVerified && !ctx.delegationStates && !Array.isArray(ctx.delegationState))) return next(new CustomError("INVALID_DELEGATION", "Multisig delegation is not supported for this transaction"));
|
|
20
21
|
}
|
|
21
22
|
const [feeVaultState, gasVaultState] = await Promise.all([context.feeVault && !ctx.feeVaultState ? statedb.account.get(context.feeVault, context) : null, context.gasVault && !ctx.gasVaultState ? statedb.account.get(context.gasVault, context) : null]);
|
|
22
23
|
if (feeVaultState) ctx.feeVaultState = feeVaultState;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { name } from "../package.mjs";
|
|
2
|
-
import
|
|
2
|
+
import { types } from "@ocap/mcrypto";
|
|
3
3
|
import { isSameDid } from "@ocap/util";
|
|
4
4
|
import { CustomError } from "@ocap/util/lib/error";
|
|
5
|
-
import
|
|
5
|
+
import get from "lodash/get.js";
|
|
6
6
|
import Debug from "debug";
|
|
7
7
|
import set from "lodash/set.js";
|
|
8
8
|
import { toTypeInfo } from "@arcblock/did";
|
|
@@ -46,14 +46,49 @@ function CreateUpdateOwnerPipe({ assets, owner }) {
|
|
|
46
46
|
});
|
|
47
47
|
const hierarchyError = await verifyAssetHierarchy(itemList.map((item) => item.address), ownerAddress, context);
|
|
48
48
|
if (hierarchyError) return next(hierarchyError);
|
|
49
|
+
const isTargetAsset = Boolean(ownerAddress && toTypeInfo(ownerAddress).role === types.RoleType.ROLE_ASSET);
|
|
50
|
+
if (isTargetAsset && itemList.some((item) => states.token?.isTokenCharterAsset?.(item))) return next(new CustomError("INVALID_OWNER", "Token Charter NFT cannot be owned by an asset", { persist: true }));
|
|
49
51
|
try {
|
|
50
|
-
const
|
|
52
|
+
const prepared = await Promise.all(itemList.map(async (item) => {
|
|
51
53
|
const old = await statedb.asset.get(item.address, context);
|
|
54
|
+
if (isTargetAsset && states.token?.isTokenCharterAsset?.(old)) throw new CustomError("INVALID_OWNER", "Token Charter NFT cannot be owned by an asset", { persist: true });
|
|
52
55
|
const updates = states.asset.update(old, { owner: ownerAddress }, context);
|
|
53
|
-
|
|
56
|
+
let tokenAddress;
|
|
57
|
+
let updatedToken = null;
|
|
58
|
+
let existingFactory = null;
|
|
59
|
+
let updatedFactory = null;
|
|
60
|
+
if (ownerAddress && states.token?.isTokenCharterAsset?.(old)) {
|
|
61
|
+
tokenAddress = (old?.data)?.value?.tokenAddress;
|
|
62
|
+
if (tokenAddress && statedb.token?.get && statedb.token?.update && states.token?.update) {
|
|
63
|
+
const existingToken = await statedb.token.get(tokenAddress, context);
|
|
64
|
+
if (existingToken) {
|
|
65
|
+
const safeToken = states.token.normalizeLegacyTokenStateForUpdate ? states.token.normalizeLegacyTokenStateForUpdate(existingToken) : existingToken;
|
|
66
|
+
updatedToken = states.token.update(safeToken, { issuer: ownerAddress });
|
|
67
|
+
if (existingToken.tokenFactoryAddress && statedb.tokenFactory?.get && statedb.tokenFactory?.update && states.tokenFactory?.update) {
|
|
68
|
+
existingFactory = await statedb.tokenFactory.get(existingToken.tokenFactoryAddress, context);
|
|
69
|
+
if (existingFactory) updatedFactory = states.tokenFactory.update(existingFactory, { owner: ownerAddress }, context);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
item,
|
|
76
|
+
updates,
|
|
77
|
+
tokenAddress,
|
|
78
|
+
updatedToken,
|
|
79
|
+
existingFactory,
|
|
80
|
+
updatedFactory
|
|
81
|
+
};
|
|
82
|
+
}));
|
|
83
|
+
const updated = await Promise.all(prepared.map(async ({ item, updates, tokenAddress, updatedToken, existingFactory, updatedFactory }) => {
|
|
84
|
+
const updatedAsset = await statedb.asset.update(item.address, updates, context);
|
|
85
|
+
if (tokenAddress && updatedToken && statedb.token?.update) await statedb.token.update(tokenAddress, updatedToken, context);
|
|
86
|
+
if (existingFactory && updatedFactory && statedb.tokenFactory?.update) await statedb.tokenFactory.update(existingFactory.address, updatedFactory, context);
|
|
87
|
+
return updatedAsset;
|
|
54
88
|
}));
|
|
55
89
|
set(context, assets, isList ? updated : updated.shift());
|
|
56
90
|
} catch (err) {
|
|
91
|
+
if (err?.code === "INVALID_OWNER") return next(err);
|
|
57
92
|
return next(new CustomError("INTERNAL", err.message, { persist: true }));
|
|
58
93
|
}
|
|
59
94
|
next();
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import get from "lodash/get.js";
|
|
2
1
|
import { CustomError } from "@ocap/util/lib/error";
|
|
2
|
+
import get from "lodash/get.js";
|
|
3
3
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
4
|
+
import { getRelatedAddresses } from "@ocap/util/lib/get-related-addr";
|
|
4
5
|
|
|
5
6
|
//#region src/pipes/verify-account-migration.ts
|
|
6
7
|
function CreateVerifyAccountMigrationPipe({ stateKey = "", addressKey = "", signerKey = "" }) {
|
|
@@ -11,11 +12,15 @@ function CreateVerifyAccountMigrationPipe({ stateKey = "", addressKey = "", sign
|
|
|
11
12
|
if (rawAddressList && rawStateList) {
|
|
12
13
|
const stateList = (Array.isArray(rawStateList) ? rawStateList : [rawStateList]).filter(Boolean);
|
|
13
14
|
const addressList = (Array.isArray(rawAddressList) ? rawAddressList : [rawAddressList]).filter(Boolean);
|
|
14
|
-
if (stateList.length !== addressList.length)
|
|
15
|
+
if (stateList.length !== addressList.length) {
|
|
16
|
+
const migratedAddr = addressList.find((addr) => !stateList.some((s) => s.address === addr) && stateList.some((s) => getRelatedAddresses(s).includes(addr)));
|
|
17
|
+
if (migratedAddr) return next(new CustomError("ACCOUNT_MIGRATED", `account ${migratedAddr} is migrated`, { persist: true }));
|
|
18
|
+
return next(new CustomError("INVALID_TX", "state and address list size mismatch for account migration verify", { persist: false }));
|
|
19
|
+
}
|
|
15
20
|
for (let i = 0; i < stateList.length; i += 1) {
|
|
16
21
|
const state = stateList[i];
|
|
17
22
|
const address = addressList[i];
|
|
18
|
-
if (state.address !== address) return next(new CustomError("ACCOUNT_MIGRATED", `account ${address} is migrated`, { persist: true }));
|
|
23
|
+
if (state.address !== address || Array.isArray(state.migratedTo) && state.migratedTo.length > 0) return next(new CustomError("ACCOUNT_MIGRATED", `account ${address} is migrated`, { persist: true }));
|
|
19
24
|
}
|
|
20
25
|
}
|
|
21
26
|
}
|
|
@@ -25,8 +30,13 @@ function CreateVerifyAccountMigrationPipe({ stateKey = "", addressKey = "", sign
|
|
|
25
30
|
const signatures = getListField(context.tx, "signatures");
|
|
26
31
|
for (const signature of signatures) {
|
|
27
32
|
const state = signers.find((x) => x.address === signature.signer);
|
|
28
|
-
if (!state || state.address !== signature.signer) return next(new CustomError("ACCOUNT_MIGRATED", `Signer account ${signature.signer} is migrated`, { persist: true }));
|
|
33
|
+
if (!state || state.address !== signature.signer || Array.isArray(state.migratedTo) && state.migratedTo.length > 0) return next(new CustomError("ACCOUNT_MIGRATED", `Signer account ${signature.signer} is migrated`, { persist: true }));
|
|
29
34
|
}
|
|
35
|
+
} else if (signers && typeof signers === "object" && !Array.isArray(signers)) {
|
|
36
|
+
const singleState = signers;
|
|
37
|
+
const signatures = getListField(context.tx, "signatures");
|
|
38
|
+
const expectedAddress = (addressKey ? get(context, addressKey) : void 0) || signatures[0]?.signer;
|
|
39
|
+
if (expectedAddress && singleState.address !== expectedAddress || Array.isArray(singleState.migratedTo) && singleState.migratedTo.length > 0) return next(new CustomError("ACCOUNT_MIGRATED", `Signer account ${expectedAddress || singleState.address} is migrated`, { persist: true }));
|
|
30
40
|
}
|
|
31
41
|
}
|
|
32
42
|
next();
|
|
@@ -13,8 +13,21 @@ interface TokenLimit {
|
|
|
13
13
|
};
|
|
14
14
|
lastTx?: number;
|
|
15
15
|
txAllowance?: string;
|
|
16
|
+
maxPerTx?: string;
|
|
16
17
|
totalAllowance?: string;
|
|
17
18
|
spentAllowance?: string;
|
|
19
|
+
/** Tier 1 (Subscription): require non-empty pinned payee `to` and optional pre-debit notification metadata */
|
|
20
|
+
requirePinnedPayee?: boolean;
|
|
21
|
+
notifyBeforeDebit?: boolean;
|
|
22
|
+
notifyLeadSeconds?: number;
|
|
23
|
+
/** Tier 2 (Autonomous Micro-Spend): rolling/tumbling time-window allowance */
|
|
24
|
+
windowLimit?: string;
|
|
25
|
+
windowSeconds?: number;
|
|
26
|
+
windowSpent?: string;
|
|
27
|
+
windowStart?: number;
|
|
28
|
+
/** Tier 3 (Over-Budget Step-Up): human owner co-signature allowance & requirement */
|
|
29
|
+
coSignAllowance?: string;
|
|
30
|
+
coSignRequired?: boolean;
|
|
18
31
|
}
|
|
19
32
|
interface AssetLimit {
|
|
20
33
|
address: string[];
|
|
@@ -30,6 +43,8 @@ interface AssetLimit {
|
|
|
30
43
|
interface Limitation {
|
|
31
44
|
tokens: TokenLimit[];
|
|
32
45
|
assets: AssetLimit[];
|
|
46
|
+
/** Explicit opt-in for unrestricted delegation when `tokens` and `assets` are both empty (#882) */
|
|
47
|
+
unlimited?: boolean;
|
|
33
48
|
}
|
|
34
49
|
interface Requirement {
|
|
35
50
|
type: 'token' | 'asset';
|
|
@@ -37,7 +52,11 @@ interface Requirement {
|
|
|
37
52
|
to?: string | string[];
|
|
38
53
|
value?: string;
|
|
39
54
|
}
|
|
40
|
-
|
|
55
|
+
interface DelegationCheckOptions {
|
|
56
|
+
now?: number;
|
|
57
|
+
coSignedByOwner?: boolean;
|
|
58
|
+
}
|
|
59
|
+
declare const isDelegationSufficient: (requirements: Requirement[], limitation: Limitation, options?: DelegationCheckOptions) => {
|
|
41
60
|
sufficient: boolean;
|
|
42
61
|
reason?: string;
|
|
43
62
|
};
|
|
@@ -63,10 +82,10 @@ declare function CreateVerifyDelegationPipe({
|
|
|
63
82
|
getRequirements
|
|
64
83
|
}: VerifyDelegationOptions): PipeFunction<IDecodedContext>;
|
|
65
84
|
declare namespace CreateVerifyDelegationPipe {
|
|
66
|
-
var isDelegationSufficient: (requirements: Requirement[], limitation: Limitation) => {
|
|
85
|
+
var isDelegationSufficient: (requirements: Requirement[], limitation: Limitation, options?: DelegationCheckOptions) => {
|
|
67
86
|
sufficient: boolean;
|
|
68
87
|
reason?: string;
|
|
69
88
|
};
|
|
70
89
|
}
|
|
71
90
|
//#endregion
|
|
72
|
-
export { CreateVerifyDelegationPipe as default, isDelegationSufficient, verifyImplicitAssetOwnership };
|
|
91
|
+
export { AssetLimit, DelegationCheckOptions, Limitation, Requirement, TokenLimit, CreateVerifyDelegationPipe as default, isDelegationSufficient, verifyImplicitAssetOwnership };
|
|
@@ -1,31 +1,108 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { types } from "@ocap/mcrypto";
|
|
2
2
|
import { isSameDid, scopeMatch, scopeMatchAny, toBN } from "@ocap/util";
|
|
3
3
|
import { CustomError } from "@ocap/util/lib/error";
|
|
4
|
-
import
|
|
4
|
+
import get from "lodash/get.js";
|
|
5
5
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
6
6
|
import set from "lodash/set.js";
|
|
7
|
-
import { toTypeInfo } from "@arcblock/did";
|
|
7
|
+
import { isFromPublicKey, normalizePublicKeyHex, toTypeInfo } from "@arcblock/did";
|
|
8
8
|
import { getRelatedAddresses } from "@ocap/util/lib/get-related-addr";
|
|
9
9
|
import isEmpty from "lodash/isEmpty.js";
|
|
10
|
+
import { fromPublicKey } from "@ocap/wallet";
|
|
10
11
|
import { toDelegateAddress } from "@arcblock/did-util";
|
|
11
12
|
|
|
12
13
|
//#region src/pipes/verify-delegation.ts
|
|
13
14
|
const ZERO = toBN(0);
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
const DECIMAL_UINT_RE = /^\d+$/;
|
|
16
|
+
const parseNonNegativeBN = (raw, defaultZero = true) => {
|
|
17
|
+
if (raw === void 0 || raw === null || raw === "") return defaultZero ? ZERO : null;
|
|
18
|
+
const str = String(raw).trim();
|
|
19
|
+
if (!DECIMAL_UINT_RE.test(str)) return null;
|
|
20
|
+
try {
|
|
21
|
+
const val = toBN(str);
|
|
22
|
+
return val.gte(ZERO) ? val : null;
|
|
23
|
+
} catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const isDelegationSufficient = (requirements, limitation, options = {}) => {
|
|
28
|
+
const now = options.now !== void 0 && Number.isFinite(options.now) ? Math.floor(options.now) : Math.floor(Date.now() / 1e3);
|
|
29
|
+
const coSignedByOwner = Boolean(options.coSignedByOwner);
|
|
30
|
+
if (!requirements || requirements.length === 0) {
|
|
31
|
+
if (!limitation || !limitation.tokens?.length && !limitation.assets?.length) return { sufficient: true };
|
|
32
|
+
const zeroReqCountedLimits = /* @__PURE__ */ new Set();
|
|
33
|
+
for (const limit of limitation.tokens || []) {
|
|
34
|
+
if (limit.coSignRequired && !coSignedByOwner) return {
|
|
35
|
+
sufficient: false,
|
|
36
|
+
reason: "human co-signature required for tier-3 delegation"
|
|
37
|
+
};
|
|
38
|
+
if (limit.txCount > 0 && limit.txSent >= limit.txCount) return {
|
|
39
|
+
sufficient: false,
|
|
40
|
+
reason: "tx count exceeds threshold"
|
|
41
|
+
};
|
|
42
|
+
if (limit.validUntil > 0 && limit.validUntil < now) return {
|
|
43
|
+
sufficient: false,
|
|
44
|
+
reason: "delegation expired"
|
|
45
|
+
};
|
|
46
|
+
if (limit.rate && limit.lastTx && limit.rate.interval > 0) {
|
|
47
|
+
if (limit.lastTx + limit.rate.interval > now) return {
|
|
48
|
+
sufficient: false,
|
|
49
|
+
reason: "delegation rate limit exceeded"
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
zeroReqCountedLimits.add(limit);
|
|
53
|
+
}
|
|
54
|
+
for (const limit of limitation.assets || []) {
|
|
55
|
+
if (limit.txCount > 0 && limit.txSent >= limit.txCount) return {
|
|
56
|
+
sufficient: false,
|
|
57
|
+
reason: "tx count exceeds threshold"
|
|
58
|
+
};
|
|
59
|
+
if (limit.validUntil > 0 && limit.validUntil < now) return {
|
|
60
|
+
sufficient: false,
|
|
61
|
+
reason: "delegation expired"
|
|
62
|
+
};
|
|
63
|
+
if (limit.rate && limit.lastTx && limit.rate.interval > 0) {
|
|
64
|
+
if (limit.lastTx + limit.rate.interval > now) return {
|
|
65
|
+
sufficient: false,
|
|
66
|
+
reason: "delegation rate limit exceeded"
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
zeroReqCountedLimits.add(limit);
|
|
70
|
+
}
|
|
71
|
+
for (const limit of zeroReqCountedLimits) {
|
|
72
|
+
limit.txSent = (limit.txSent || 0) + 1;
|
|
73
|
+
limit.lastTx = now;
|
|
74
|
+
}
|
|
75
|
+
return { sufficient: true };
|
|
76
|
+
}
|
|
77
|
+
if (!limitation || !limitation.tokens?.length && !limitation.assets?.length) {
|
|
78
|
+
if (limitation?.unlimited === true) return { sufficient: true };
|
|
79
|
+
return {
|
|
80
|
+
sufficient: false,
|
|
81
|
+
reason: "delegation limit is empty"
|
|
82
|
+
};
|
|
83
|
+
}
|
|
17
84
|
const countedLimits = /* @__PURE__ */ new Set();
|
|
18
85
|
const txSpentByLimit = /* @__PURE__ */ new Map();
|
|
86
|
+
const baseSpentByLimit = /* @__PURE__ */ new Map();
|
|
87
|
+
const windowStateByLimit = /* @__PURE__ */ new Map();
|
|
19
88
|
for (const requirement of requirements) {
|
|
20
89
|
if (requirement.type === "token") {
|
|
21
90
|
const limit = limitation.tokens.find((token) => isSameDid(token.address, requirement.address));
|
|
22
91
|
if (limit) {
|
|
23
92
|
const requirementTo = [requirement.to].flat().filter(Boolean);
|
|
24
93
|
const limitTo = [limit.to].flat().filter(Boolean);
|
|
94
|
+
if (limit.requirePinnedPayee && (!limitTo.length || !requirementTo.length)) return {
|
|
95
|
+
sufficient: false,
|
|
96
|
+
reason: "pinned payee address is required for subscription delegation"
|
|
97
|
+
};
|
|
25
98
|
if (!(!requirementTo.length || !limitTo.length || requirementTo.some((x) => limitTo.some((y) => isSameDid(x, y))))) return {
|
|
26
99
|
sufficient: false,
|
|
27
100
|
reason: "token receiver is not allowed"
|
|
28
101
|
};
|
|
102
|
+
if (limit.coSignRequired && !coSignedByOwner) return {
|
|
103
|
+
sufficient: false,
|
|
104
|
+
reason: "human co-signature required for tier-3 delegation"
|
|
105
|
+
};
|
|
29
106
|
const isFirstMatchInTx = !countedLimits.has(limit);
|
|
30
107
|
if (isFirstMatchInTx) {
|
|
31
108
|
if (limit.txCount > 0 && limit.txSent >= limit.txCount) return {
|
|
@@ -45,26 +122,94 @@ const isDelegationSufficient = (requirements, limitation) => {
|
|
|
45
122
|
};
|
|
46
123
|
}
|
|
47
124
|
}
|
|
48
|
-
const requirementValue =
|
|
125
|
+
const requirementValue = parseNonNegativeBN(requirement.value);
|
|
126
|
+
if (!requirementValue) return {
|
|
127
|
+
sufficient: false,
|
|
128
|
+
reason: "invalid token requirement value"
|
|
129
|
+
};
|
|
49
130
|
const currentTxSpent = (txSpentByLimit.get(limit) || ZERO).add(requirementValue);
|
|
50
131
|
txSpentByLimit.set(limit, currentTxSpent);
|
|
51
|
-
const
|
|
52
|
-
|
|
132
|
+
const rawTxAllowance = parseNonNegativeBN(limit.txAllowance);
|
|
133
|
+
const rawMaxPerTx = parseNonNegativeBN(limit.maxPerTx);
|
|
134
|
+
const coSignAllowance = parseNonNegativeBN(limit.coSignAllowance);
|
|
135
|
+
if (!rawTxAllowance || !rawMaxPerTx || !coSignAllowance) return {
|
|
136
|
+
sufficient: false,
|
|
137
|
+
reason: "invalid delegation allowance configuration"
|
|
138
|
+
};
|
|
139
|
+
let txAllowance = ZERO;
|
|
140
|
+
if (rawTxAllowance.gt(ZERO) && rawMaxPerTx.gt(ZERO)) txAllowance = rawTxAllowance.lt(rawMaxPerTx) ? rawTxAllowance : rawMaxPerTx;
|
|
141
|
+
else if (rawTxAllowance.gt(ZERO)) txAllowance = rawTxAllowance;
|
|
142
|
+
else if (rawMaxPerTx.gt(ZERO)) txAllowance = rawMaxPerTx;
|
|
143
|
+
const hasCoSignStepUp = coSignAllowance.gt(ZERO);
|
|
144
|
+
const windowLimit = parseNonNegativeBN(limit.windowLimit);
|
|
145
|
+
if (!windowLimit) return {
|
|
146
|
+
sufficient: false,
|
|
147
|
+
reason: "invalid window allowance configuration"
|
|
148
|
+
};
|
|
149
|
+
if (hasCoSignStepUp && currentTxSpent.gt(coSignAllowance)) return {
|
|
150
|
+
sufficient: false,
|
|
151
|
+
reason: "token amount exceeds co-sign allowance"
|
|
152
|
+
};
|
|
153
|
+
if (hasCoSignStepUp && txAllowance.isZero() && windowLimit.isZero() && !coSignedByOwner && currentTxSpent.gt(ZERO)) return {
|
|
154
|
+
sufficient: false,
|
|
155
|
+
reason: "human co-signature required for over-budget spend"
|
|
156
|
+
};
|
|
157
|
+
if (txAllowance.gt(ZERO) && currentTxSpent.gt(txAllowance)) if (coSignedByOwner && hasCoSignStepUp && currentTxSpent.lte(coSignAllowance)) {} else if (!coSignedByOwner && hasCoSignStepUp && currentTxSpent.lte(coSignAllowance)) return {
|
|
158
|
+
sufficient: false,
|
|
159
|
+
reason: "human co-signature required for over-budget spend"
|
|
160
|
+
};
|
|
161
|
+
else return {
|
|
53
162
|
sufficient: false,
|
|
54
163
|
reason: "token amount exceeds tx allowance"
|
|
55
164
|
};
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
if (
|
|
165
|
+
const hasWindowSeconds = limit.windowSeconds !== void 0 && limit.windowSeconds !== null;
|
|
166
|
+
const windowSeconds = hasWindowSeconds ? Number(limit.windowSeconds) : 0;
|
|
167
|
+
if (windowLimit.gt(ZERO) && (!Number.isFinite(windowSeconds) || windowSeconds <= 0) || hasWindowSeconds && (!Number.isFinite(windowSeconds) || windowSeconds < 0 || windowSeconds > 0 && windowLimit.isZero())) return {
|
|
59
168
|
sufficient: false,
|
|
60
|
-
reason: "
|
|
169
|
+
reason: "invalid window allowance configuration"
|
|
61
170
|
};
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
171
|
+
if (windowLimit.gt(ZERO) && windowSeconds > 0) {
|
|
172
|
+
let winState = windowStateByLimit.get(limit);
|
|
173
|
+
if (!winState) {
|
|
174
|
+
const prevStart = Number(limit.windowStart || 0);
|
|
175
|
+
const isExpiredWindow = !prevStart || now >= prevStart + windowSeconds;
|
|
176
|
+
const prevWindowSpent = parseNonNegativeBN(limit.windowSpent);
|
|
177
|
+
if (!prevWindowSpent) return {
|
|
178
|
+
sufficient: false,
|
|
179
|
+
reason: "invalid window allowance configuration"
|
|
180
|
+
};
|
|
181
|
+
winState = {
|
|
182
|
+
spent: isExpiredWindow ? ZERO : prevWindowSpent,
|
|
183
|
+
start: isExpiredWindow ? now : prevStart
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
const nextWindowSpent = winState.spent.add(requirementValue);
|
|
187
|
+
if (nextWindowSpent.gt(windowLimit)) if (coSignedByOwner && hasCoSignStepUp && currentTxSpent.lte(coSignAllowance)) {} else if (!coSignedByOwner && hasCoSignStepUp && currentTxSpent.lte(coSignAllowance)) return {
|
|
188
|
+
sufficient: false,
|
|
189
|
+
reason: "human co-signature required for over-budget spend"
|
|
190
|
+
};
|
|
191
|
+
else return {
|
|
192
|
+
sufficient: false,
|
|
193
|
+
reason: "token amount exceeds window allowance"
|
|
194
|
+
};
|
|
195
|
+
winState.spent = nextWindowSpent;
|
|
196
|
+
windowStateByLimit.set(limit, winState);
|
|
66
197
|
}
|
|
67
|
-
|
|
198
|
+
const totalAllowance = parseNonNegativeBN(limit.totalAllowance);
|
|
199
|
+
let baseSpent = baseSpentByLimit.get(limit);
|
|
200
|
+
if (!baseSpent) {
|
|
201
|
+
baseSpent = parseNonNegativeBN(limit.spentAllowance) ?? void 0;
|
|
202
|
+
if (baseSpent) baseSpentByLimit.set(limit, baseSpent);
|
|
203
|
+
}
|
|
204
|
+
if (!totalAllowance || !baseSpent) return {
|
|
205
|
+
sufficient: false,
|
|
206
|
+
reason: "invalid total allowance configuration"
|
|
207
|
+
};
|
|
208
|
+
if (totalAllowance.gt(ZERO) && currentTxSpent.add(baseSpent).gt(totalAllowance)) return {
|
|
209
|
+
sufficient: false,
|
|
210
|
+
reason: "token amount exceeds total allowance"
|
|
211
|
+
};
|
|
212
|
+
if (isFirstMatchInTx) countedLimits.add(limit);
|
|
68
213
|
continue;
|
|
69
214
|
}
|
|
70
215
|
return {
|
|
@@ -99,8 +244,6 @@ const isDelegationSufficient = (requirements, limitation) => {
|
|
|
99
244
|
};
|
|
100
245
|
}
|
|
101
246
|
countedLimits.add(limit);
|
|
102
|
-
limit.txSent = (limit.txSent || 0) + 1;
|
|
103
|
-
limit.lastTx = now;
|
|
104
247
|
}
|
|
105
248
|
continue;
|
|
106
249
|
}
|
|
@@ -114,6 +257,18 @@ const isDelegationSufficient = (requirements, limitation) => {
|
|
|
114
257
|
reason: "requirement type invalid"
|
|
115
258
|
};
|
|
116
259
|
}
|
|
260
|
+
for (const limit of countedLimits) {
|
|
261
|
+
limit.txSent = (limit.txSent || 0) + 1;
|
|
262
|
+
limit.lastTx = now;
|
|
263
|
+
}
|
|
264
|
+
for (const [limit, txSpent] of txSpentByLimit.entries()) {
|
|
265
|
+
limit.spentAllowance = (baseSpentByLimit.get(limit) || ZERO).add(txSpent).toString();
|
|
266
|
+
const updatedWin = windowStateByLimit.get(limit);
|
|
267
|
+
if (updatedWin) {
|
|
268
|
+
limit.windowSpent = updatedWin.spent.toString();
|
|
269
|
+
limit.windowStart = updatedWin.start;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
117
272
|
return { sufficient: true };
|
|
118
273
|
};
|
|
119
274
|
const verifyImplicitAssetOwnership = async (signerAddress, delegatorState, context, delegatorAddress) => {
|
|
@@ -148,19 +303,71 @@ const verifyImplicitAssetOwnership = async (signerAddress, delegatorState, conte
|
|
|
148
303
|
function CreateVerifyDelegationPipe({ type = "signature", delegationKey = "delegationState", requirementsKey = "delegationRequirements", signerKey = "signerState", delegatorKey = "delegatorState", getRequirements }) {
|
|
149
304
|
return async function VerifyDelegation(context, next) {
|
|
150
305
|
const { tx, txType, statedb } = context;
|
|
151
|
-
const getDelegationState = async (signerState, delegatorState) => {
|
|
306
|
+
const getDelegationState = async (signerState, delegatorState, existingPool = []) => {
|
|
152
307
|
if (delegatorState) {
|
|
153
308
|
const related = getRelatedAddresses(delegatorState);
|
|
309
|
+
const topLevelDelegation = context.delegationState;
|
|
154
310
|
return (await Promise.all(related.map(async (x) => {
|
|
155
|
-
const
|
|
311
|
+
const targetAddress = toDelegateAddress(signerState.address, x);
|
|
312
|
+
const cached = existingPool.find((d) => d && isSameDid(d.address, targetAddress)) || (topLevelDelegation && isSameDid(topLevelDelegation.address, targetAddress) ? topLevelDelegation : void 0);
|
|
313
|
+
if (cached) {
|
|
314
|
+
cached.to = x;
|
|
315
|
+
return cached;
|
|
316
|
+
}
|
|
317
|
+
const delegation = await statedb.delegation.get(targetAddress, context);
|
|
156
318
|
if (delegation) delegation.to = x;
|
|
157
319
|
return delegation;
|
|
158
320
|
}))).find((x) => x);
|
|
159
321
|
}
|
|
160
322
|
return null;
|
|
161
323
|
};
|
|
162
|
-
const
|
|
163
|
-
|
|
324
|
+
const verifyOwnerCoSignature = async (signerState, delegateePk) => {
|
|
325
|
+
if (!signerState?.address) return false;
|
|
326
|
+
if (Array.isArray(signerState.migratedTo) && signerState.migratedTo.length > 0) return false;
|
|
327
|
+
let rawCoSig = get(context, "extra.coSignature");
|
|
328
|
+
if (!rawCoSig) {
|
|
329
|
+
const rawTxExtra = get(context, "extra.txExtra");
|
|
330
|
+
if (typeof rawTxExtra === "string" && rawTxExtra.trim().startsWith("{")) try {
|
|
331
|
+
const parsed = JSON.parse(rawTxExtra);
|
|
332
|
+
if (parsed?.coSignature && typeof parsed.coSignature === "object") rawCoSig = parsed.coSignature;
|
|
333
|
+
} catch {}
|
|
334
|
+
else if (rawTxExtra && typeof rawTxExtra === "object") {
|
|
335
|
+
const maybeCoSig = rawTxExtra.coSignature;
|
|
336
|
+
if (maybeCoSig && typeof maybeCoSig === "object") rawCoSig = maybeCoSig;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (!rawCoSig || typeof rawCoSig.pk !== "string" || typeof rawCoSig.signature !== "string") return false;
|
|
340
|
+
const candidatePayloads = [context.txHash, context.unsignedTxHash].filter((x) => typeof x === "string" && x.length > 0);
|
|
341
|
+
if (candidatePayloads.length === 0) return false;
|
|
342
|
+
const ownerAddr = signerState.address;
|
|
343
|
+
const forbiddenPk = delegateePk || tx.pk;
|
|
344
|
+
const coSigPkHex = normalizePublicKeyHex(rawCoSig.pk);
|
|
345
|
+
const forbiddenPkHex = forbiddenPk ? normalizePublicKeyHex(forbiddenPk) : "";
|
|
346
|
+
if (!coSigPkHex || forbiddenPkHex && coSigPkHex === forbiddenPkHex) return false;
|
|
347
|
+
if (!isFromPublicKey(ownerAddr, rawCoSig.pk)) return false;
|
|
348
|
+
try {
|
|
349
|
+
const wallet = fromPublicKey(rawCoSig.pk, toTypeInfo(ownerAddr));
|
|
350
|
+
for (const payload of candidatePayloads) {
|
|
351
|
+
if (await wallet.verify(payload, rawCoSig.signature, false)) return true;
|
|
352
|
+
if (await wallet.verify(payload, rawCoSig.signature, true)) return true;
|
|
353
|
+
}
|
|
354
|
+
} catch {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
return false;
|
|
358
|
+
};
|
|
359
|
+
const getTxTimestampSeconds = () => {
|
|
360
|
+
const txTime = context.txTime;
|
|
361
|
+
if (txTime !== void 0 && txTime !== null && txTime !== "") if (typeof txTime === "number") {
|
|
362
|
+
if (Number.isFinite(txTime) && txTime > 0) return txTime > 1e11 ? Math.floor(txTime / 1e3) : Math.floor(txTime);
|
|
363
|
+
} else {
|
|
364
|
+
const parsedMs = new Date(txTime).getTime();
|
|
365
|
+
if (Number.isFinite(parsedMs) && parsedMs > 0) return Math.floor(parsedMs / 1e3);
|
|
366
|
+
}
|
|
367
|
+
return Math.floor(Date.now() / 1e3);
|
|
368
|
+
};
|
|
369
|
+
const checkDelegation = (delegationState, requirements, coSignedByOwner = false) => {
|
|
370
|
+
const now = getTxTimestampSeconds();
|
|
164
371
|
const validUntil = delegationState.validUntil || 0;
|
|
165
372
|
if (validUntil > 0 && validUntil < now) return {
|
|
166
373
|
valid: false,
|
|
@@ -195,7 +402,11 @@ function CreateVerifyDelegationPipe({ type = "signature", delegationKey = "deleg
|
|
|
195
402
|
};
|
|
196
403
|
const result = isDelegationSufficient(requirements, {
|
|
197
404
|
tokens: rawLimit.tokens || [],
|
|
198
|
-
assets: rawLimit.assets || []
|
|
405
|
+
assets: rawLimit.assets || [],
|
|
406
|
+
...rawLimit.unlimited === true ? { unlimited: true } : {}
|
|
407
|
+
}, {
|
|
408
|
+
now,
|
|
409
|
+
coSignedByOwner
|
|
199
410
|
});
|
|
200
411
|
if (result.sufficient === false) return {
|
|
201
412
|
valid: false,
|
|
@@ -211,16 +422,18 @@ function CreateVerifyDelegationPipe({ type = "signature", delegationKey = "deleg
|
|
|
211
422
|
const signerState = get(context, signerKey);
|
|
212
423
|
const delegatorState = get(context, delegatorKey);
|
|
213
424
|
const signerAddress = signerState?.address || tx?.from;
|
|
425
|
+
if (delegatorState && (tx?.delegator && !isSameDid(delegatorState.address, tx.delegator) || Array.isArray(delegatorState.migratedTo) && delegatorState.migratedTo.length > 0)) return next(new CustomError("ACCOUNT_MIGRATED", `Delegator account ${tx?.delegator} is migrated`, { persist: false }));
|
|
214
426
|
if (signerAddress && toTypeInfo(signerAddress).role === types.RoleType.ROLE_ASSET) {
|
|
215
427
|
try {
|
|
216
428
|
if (await verifyImplicitAssetOwnership(signerAddress, delegatorState || (tx?.delegator ? { address: tx.delegator } : null), context, tx?.delegator)) {
|
|
217
429
|
set(context, "isImplicitDelegation", true);
|
|
430
|
+
set(context, "isDelegationVerified", true);
|
|
218
431
|
return next();
|
|
219
432
|
}
|
|
220
433
|
} catch (err) {
|
|
221
434
|
return next(err);
|
|
222
435
|
}
|
|
223
|
-
return next(new CustomError("INVALID_DELEGATION", `Account ${delegatorState?.address || tx?.delegator} is not the owner of asset ${signerAddress}`, { persist:
|
|
436
|
+
return next(new CustomError("INVALID_DELEGATION", `Account ${delegatorState?.address || tx?.delegator} is not the owner of asset ${signerAddress}`, { persist: false }));
|
|
224
437
|
}
|
|
225
438
|
const delegationState = await getDelegationState(signerState, delegatorState ?? null);
|
|
226
439
|
if (!delegationState) {
|
|
@@ -228,12 +441,15 @@ function CreateVerifyDelegationPipe({ type = "signature", delegationKey = "deleg
|
|
|
228
441
|
signerAddress: signerState?.address,
|
|
229
442
|
delegatorAddress: delegatorState?.address
|
|
230
443
|
});
|
|
231
|
-
return next(new CustomError("INVALID_DELEGATION", "No delegation found between sender and delegator", { persist:
|
|
444
|
+
return next(new CustomError("INVALID_DELEGATION", "No delegation found between sender and delegator", { persist: false }));
|
|
232
445
|
}
|
|
233
|
-
const
|
|
234
|
-
|
|
446
|
+
const coSignedByOwner = await verifyOwnerCoSignature(signerState);
|
|
447
|
+
const result = checkDelegation(delegationState, requirements, coSignedByOwner);
|
|
448
|
+
if (result.code) return next(new CustomError(result.code, `Delegation check failed: ${result.reason}`, { persist: false }));
|
|
449
|
+
if (coSignedByOwner) set(context, "coSignedByOwner", true);
|
|
235
450
|
set(context, delegationKey, delegationState);
|
|
236
451
|
set(context, "isDelegationChanged", true);
|
|
452
|
+
set(context, "isDelegationVerified", true);
|
|
237
453
|
return next();
|
|
238
454
|
}
|
|
239
455
|
if (type === "multisig") {
|
|
@@ -254,7 +470,7 @@ function CreateVerifyDelegationPipe({ type = "signature", delegationKey = "deleg
|
|
|
254
470
|
allRequirements.push(...sigRequirements);
|
|
255
471
|
const signerState = signerStates?.find((x) => signer && isSameDid(x.address, signer));
|
|
256
472
|
const delegatorState = delegatorStates?.find((x) => delegator && getRelatedAddresses(x).some((addr) => isSameDid(addr, delegator)));
|
|
257
|
-
if (delegatorState && (delegator && !isSameDid(delegatorState.address, delegator) || Array.isArray(delegatorState.migratedTo) && delegatorState.migratedTo.length > 0)) return next(new CustomError("ACCOUNT_MIGRATED", `Delegator account ${delegator} is migrated`, { persist:
|
|
473
|
+
if (delegatorState && (delegator && !isSameDid(delegatorState.address, delegator) || Array.isArray(delegatorState.migratedTo) && delegatorState.migratedTo.length > 0)) return next(new CustomError("ACCOUNT_MIGRATED", `Delegator account ${delegator} is migrated`, { persist: false }));
|
|
258
474
|
const signerAddress = signerState?.address || signer;
|
|
259
475
|
if (signerAddress && toTypeInfo(signerAddress).role === types.RoleType.ROLE_ASSET) {
|
|
260
476
|
try {
|
|
@@ -262,22 +478,29 @@ function CreateVerifyDelegationPipe({ type = "signature", delegationKey = "deleg
|
|
|
262
478
|
} catch (err) {
|
|
263
479
|
return next(err);
|
|
264
480
|
}
|
|
265
|
-
return next(new CustomError("INVALID_DELEGATION", `Account ${delegatorState?.address || delegator} is not the owner of asset ${signerAddress}`, { persist:
|
|
481
|
+
return next(new CustomError("INVALID_DELEGATION", `Account ${delegatorState?.address || delegator} is not the owner of asset ${signerAddress}`, { persist: false }));
|
|
266
482
|
}
|
|
267
|
-
const delegationState = await getDelegationState(signerState, delegatorState ?? null);
|
|
483
|
+
const delegationState = await getDelegationState(signerState, delegatorState ?? null, delegationStates);
|
|
268
484
|
if (!delegationState) {
|
|
269
485
|
console.error("invalid_delegation", {
|
|
270
486
|
signerAddress: signerState?.address,
|
|
271
487
|
delegatorAddress: delegatorState?.address
|
|
272
488
|
});
|
|
273
|
-
return next(new CustomError("INVALID_DELEGATION", "No delegation found between signer and delegator", { persist:
|
|
489
|
+
return next(new CustomError("INVALID_DELEGATION", "No delegation found between signer and delegator", { persist: false }));
|
|
490
|
+
}
|
|
491
|
+
const topLevelDelegation = context.delegationState;
|
|
492
|
+
const alreadyVerifiedInTx = delegationStates.some((d) => isSameDid(d.address, delegationState.address)) || Boolean(topLevelDelegation && isSameDid(topLevelDelegation.address, delegationState.address));
|
|
493
|
+
const coSignedByOwner = await verifyOwnerCoSignature(signerState, signature.pk);
|
|
494
|
+
if (!alreadyVerifiedInTx || sigRequirements.length > 0) {
|
|
495
|
+
const result = checkDelegation(delegationState, sigRequirements, coSignedByOwner);
|
|
496
|
+
if (result.code) return next(new CustomError(result.code, `Delegation check failed: ${result.reason}`, { persist: false }));
|
|
274
497
|
}
|
|
275
|
-
|
|
276
|
-
if (
|
|
277
|
-
delegationStates.push(delegationState);
|
|
498
|
+
if (coSignedByOwner) set(context, "coSignedByOwner", true);
|
|
499
|
+
if (!delegationStates.some((d) => isSameDid(d.address, delegationState.address))) delegationStates.push(delegationState);
|
|
278
500
|
}
|
|
279
501
|
set(context, requirementsKey, allRequirements);
|
|
280
502
|
set(context, delegationKey, delegationStates);
|
|
503
|
+
set(context, "isMultiSigDelegationVerified", true);
|
|
281
504
|
if (delegationStates.length > 0) set(context, "isDelegationChanged", true);
|
|
282
505
|
return next();
|
|
283
506
|
}
|