@ocap/tx-pipeline 1.30.24 → 1.30.25
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/helpers/dedup-height.d.mts +16 -0
- package/esm/helpers/dedup-height.mjs +61 -0
- package/esm/index.d.mts +7 -4
- package/esm/index.mjs +6 -4
- package/esm/pipes/anti-land-attack.mjs +1 -1
- package/esm/pipes/decode-tx.d.mts +3 -2
- package/esm/pipes/decode-tx.mjs +175 -29
- package/esm/pipes/extract-signer.mjs +11 -2
- package/esm/pipes/extract-state.d.mts +4 -1
- package/esm/pipes/extract-state.mjs +14 -7
- package/esm/pipes/take-state-snapshot.mjs +8 -0
- package/esm/pipes/update-owner.d.mts +3 -1
- package/esm/pipes/update-owner.mjs +28 -2
- package/esm/pipes/verify-account-migration.mjs +2 -2
- package/esm/pipes/verify-blocked.mjs +1 -1
- package/esm/pipes/verify-delegation.d.mts +6 -3
- package/esm/pipes/verify-delegation.mjs +121 -47
- package/esm/pipes/verify-list-size.mjs +1 -1
- package/esm/pipes/verify-multisig-v2.mjs +39 -9
- package/esm/pipes/verify-multisig.mjs +20 -5
- package/esm/pipes/verify-sender.mjs +1 -1
- package/esm/pipes/verify-signature.mjs +14 -5
- package/esm/pipes/verify-signer.mjs +5 -3
- package/esm/pipes/verify-token-access.mjs +31 -2
- package/esm/pipes/verify-token-balance.mjs +4 -2
- package/esm/pipes/verify-transferrable.mjs +1 -1
- package/esm/pipes/verify-tx-input.mjs +3 -3
- package/esm/pipes/verify-tx-size.mjs +1 -1
- package/esm/pipes/verify-tx.d.mts +1 -1
- package/esm/pipes/verify-tx.mjs +12 -5
- package/esm/pipes/verify-updater.mjs +1 -1
- package/lib/helpers/dedup-height.cjs +72 -0
- package/lib/helpers/dedup-height.d.cts +16 -0
- package/lib/index.cjs +15 -0
- package/lib/index.d.cts +7 -4
- package/lib/pipes/anti-land-attack.cjs +2 -2
- package/lib/pipes/decode-tx.cjs +176 -26
- package/lib/pipes/decode-tx.d.cts +3 -2
- package/lib/pipes/extract-signer.cjs +11 -2
- package/lib/pipes/extract-state.cjs +14 -7
- package/lib/pipes/extract-state.d.cts +4 -1
- package/lib/pipes/take-state-snapshot.cjs +8 -0
- package/lib/pipes/update-owner.cjs +29 -2
- package/lib/pipes/update-owner.d.cts +3 -1
- package/lib/pipes/verify-account-migration.cjs +2 -2
- package/lib/pipes/verify-blocked.cjs +1 -1
- package/lib/pipes/verify-delegation.cjs +123 -48
- package/lib/pipes/verify-delegation.d.cts +6 -3
- package/lib/pipes/verify-list-size.cjs +1 -1
- package/lib/pipes/verify-multisig-v2.cjs +40 -9
- package/lib/pipes/verify-multisig.cjs +21 -5
- package/lib/pipes/verify-sender.cjs +1 -1
- package/lib/pipes/verify-signature.cjs +13 -4
- package/lib/pipes/verify-signer.cjs +5 -3
- package/lib/pipes/verify-token-access.cjs +32 -3
- package/lib/pipes/verify-token-balance.cjs +5 -3
- package/lib/pipes/verify-transferrable.cjs +1 -1
- package/lib/pipes/verify-tx-input.cjs +4 -4
- package/lib/pipes/verify-tx-size.cjs +1 -1
- package/lib/pipes/verify-tx.cjs +12 -4
- package/lib/pipes/verify-tx.d.cts +1 -1
- package/lib/pipes/verify-updater.cjs +1 -1
- package/package.json +10 -10
package/esm/pipes/verify-tx.mjs
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isDedupHeightActive, parseNonceBn } from "../helpers/dedup-height.mjs";
|
|
2
2
|
import get from "lodash/get.js";
|
|
3
|
+
import { BN } from "@ocap/util";
|
|
4
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
3
5
|
import { toTypeUrl } from "@ocap/message";
|
|
6
|
+
import { types } from "@ocap/mcrypto";
|
|
4
7
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
5
|
-
import { isFromPublicKey } from "@arcblock/did";
|
|
8
|
+
import { isFromPublicKey, toTypeInfo } from "@arcblock/did";
|
|
6
9
|
import isEmpty from "lodash/isEmpty.js";
|
|
7
10
|
|
|
8
11
|
//#region src/pipes/verify-tx.ts
|
|
9
|
-
function VerifyTx(context, next) {
|
|
12
|
+
async function VerifyTx(context, next) {
|
|
10
13
|
for (const field of [
|
|
11
14
|
"from",
|
|
12
15
|
"chainId",
|
|
@@ -17,13 +20,17 @@ function VerifyTx(context, next) {
|
|
|
17
20
|
const { itx, chainId, nonce, pk, from, delegator } = context.tx;
|
|
18
21
|
const expected = get(context, "config.chainId");
|
|
19
22
|
if (chainId !== expected) return next(new CustomError("INVALID_CHAIN_ID", `Invalid chain id: expected ${expected}, got ${chainId}`));
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
if (await isDedupHeightActive(context)) {
|
|
24
|
+
const nonceBn = parseNonceBn(nonce);
|
|
25
|
+
if (!nonceBn || nonceBn.lte(new BN(0))) return next(new CustomError("INVALID_NONCE", "tx.nonce should be a positive integer"));
|
|
26
|
+
} else if (Number.isNaN(Number(nonce)) || Number(nonce) < 0) return next(new CustomError("INVALID_NONCE", "tx.nonce should be a positive integer"));
|
|
22
27
|
const { supportedTxs, maxMultisig } = get(context, "config.transaction");
|
|
23
28
|
const rawItx = itx;
|
|
24
29
|
const itxTypeUrl = rawItx.typeUrl || (rawItx.type ? toTypeUrl(rawItx.type) : void 0);
|
|
25
30
|
if (!itxTypeUrl || supportedTxs.includes(itxTypeUrl) === false) return next(new CustomError("UNSUPPORTED_TX", `Transaction ${itxTypeUrl} is not supported`));
|
|
26
31
|
if (getListField(context, "tx.signatures").length > maxMultisig) return next(new CustomError("INVALID_TX", "Too many multisig signatures"));
|
|
32
|
+
if (toTypeInfo(from).role === types.RoleType.ROLE_ASSET && isEmpty(delegator)) return next(new CustomError("INVALID_TX", "Asset sender requires a delegator"));
|
|
33
|
+
if (!isEmpty(delegator) && toTypeInfo(delegator).role === types.RoleType.ROLE_ASSET) return next(new CustomError("INVALID_TX", "Asset cannot act as a delegator signer"));
|
|
27
34
|
if (isFromPublicKey(delegator || from, pk) === false) return next(new CustomError("INVALID_TX", "Sender or delegator address does not match pk"));
|
|
28
35
|
next();
|
|
29
36
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { name } from "../package.mjs";
|
|
2
|
-
import { CustomError } from "@ocap/util/lib/error";
|
|
3
2
|
import get from "lodash/get.js";
|
|
3
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
4
4
|
import Debug from "debug";
|
|
5
5
|
import { getRelatedAddresses } from "@ocap/util/lib/get-related-addr";
|
|
6
6
|
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let _ocap_state = require("@ocap/state");
|
|
3
|
+
let lodash_get = require("lodash/get");
|
|
4
|
+
lodash_get = require_rolldown_runtime.__toESM(lodash_get);
|
|
5
|
+
|
|
6
|
+
//#region src/helpers/dedup-height.ts
|
|
7
|
+
const hasLedgerSequenceProvider = (context) => {
|
|
8
|
+
const ledger = context?.ledger;
|
|
9
|
+
return typeof ledger?.getLatestSequence === "function" || typeof ledger?.getSequence === "function";
|
|
10
|
+
};
|
|
11
|
+
async function resolveEffectiveHeight(context) {
|
|
12
|
+
const explicit = (0, _ocap_state.readExplicitHeight)(context);
|
|
13
|
+
if (explicit !== void 0) return explicit;
|
|
14
|
+
const ledger = context?.ledger;
|
|
15
|
+
const getSeqFn = typeof ledger?.getLatestSequence === "function" ? () => ledger.getLatestSequence() : typeof ledger?.getSequence === "function" ? () => ledger.getSequence() : null;
|
|
16
|
+
if (getSeqFn) try {
|
|
17
|
+
const seq = await getSeqFn();
|
|
18
|
+
const num = Number(seq);
|
|
19
|
+
if (Number.isFinite(num) && num >= 0) {
|
|
20
|
+
const nextSeq = num + 1;
|
|
21
|
+
if (context && typeof context === "object") context.expectedLedgerSequence = nextSeq;
|
|
22
|
+
return nextSeq;
|
|
23
|
+
}
|
|
24
|
+
} catch (err) {
|
|
25
|
+
if (!(err instanceof Error ? err.message : String(err)).includes("No read ledger configured on the sequencer-driven ledger facade")) throw err;
|
|
26
|
+
}
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
function tryResolveDedupHeightActiveSync(context) {
|
|
30
|
+
const explicitFlag = (0, lodash_get.default)(context, "dedupActive");
|
|
31
|
+
if (typeof explicitFlag === "boolean") return explicitFlag;
|
|
32
|
+
const dedupHeight = (0, _ocap_state.resolveDedupActivationHeight)(context);
|
|
33
|
+
if (dedupHeight <= 0) return true;
|
|
34
|
+
const explicitHeight = (0, _ocap_state.readExplicitHeight)(context);
|
|
35
|
+
if (explicitHeight !== void 0) return explicitHeight >= dedupHeight;
|
|
36
|
+
if (!hasLedgerSequenceProvider(context) || dedupHeight >= _ocap_state.PENDING_DEDUP_ACTIVATION_HEIGHT) return dedupHeight < _ocap_state.PENDING_DEDUP_ACTIVATION_HEIGHT;
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
async function isDedupHeightActive(context) {
|
|
40
|
+
const syncResult = tryResolveDedupHeightActiveSync(context);
|
|
41
|
+
if (syncResult !== null) return syncResult;
|
|
42
|
+
const dedupHeight = (0, _ocap_state.resolveDedupActivationHeight)(context);
|
|
43
|
+
return await resolveEffectiveHeight(context) >= dedupHeight;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Validate that a passkey Multisig.extra payload is a JSON object containing
|
|
47
|
+
* non-empty string `authenticatorData` and `clientDataJSON`.
|
|
48
|
+
* Does not restrict key ordering or additional client fields (such as iOS
|
|
49
|
+
* DIDConnect `signature` and `userHandle`), relying on `unsignedTxHash`
|
|
50
|
+
* deduplication and `nonce > 0` for replay protection.
|
|
51
|
+
*/
|
|
52
|
+
function isCanonicalPasskeyExtra(extra) {
|
|
53
|
+
try {
|
|
54
|
+
const parsed = JSON.parse(extra);
|
|
55
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return false;
|
|
56
|
+
return typeof parsed.authenticatorData === "string" && parsed.authenticatorData.length > 0 && typeof parsed.clientDataJSON === "string" && parsed.clientDataJSON.length > 0;
|
|
57
|
+
} catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
exports.CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS = _ocap_state.CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS;
|
|
64
|
+
exports.EXPLICIT_HEIGHT_PATHS = _ocap_state.EXPLICIT_HEIGHT_PATHS;
|
|
65
|
+
exports.PENDING_DEDUP_ACTIVATION_HEIGHT = _ocap_state.PENDING_DEDUP_ACTIVATION_HEIGHT;
|
|
66
|
+
exports.isCanonicalPasskeyExtra = isCanonicalPasskeyExtra;
|
|
67
|
+
exports.isDedupHeightActive = isDedupHeightActive;
|
|
68
|
+
exports.parseNonceBn = _ocap_state.parseNonceBn;
|
|
69
|
+
exports.readExplicitHeight = _ocap_state.readExplicitHeight;
|
|
70
|
+
exports.resolveDedupActivationHeight = _ocap_state.resolveDedupActivationHeight;
|
|
71
|
+
exports.resolveEffectiveHeight = resolveEffectiveHeight;
|
|
72
|
+
exports.tryResolveDedupHeightActiveSync = tryResolveDedupHeightActiveSync;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight } from "@ocap/state";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers/dedup-height.d.ts
|
|
4
|
+
declare function resolveEffectiveHeight(context: unknown): Promise<number>;
|
|
5
|
+
declare function tryResolveDedupHeightActiveSync(context: unknown): boolean | null;
|
|
6
|
+
declare function isDedupHeightActive(context: unknown): Promise<boolean>;
|
|
7
|
+
/**
|
|
8
|
+
* Validate that a passkey Multisig.extra payload is a JSON object containing
|
|
9
|
+
* non-empty string `authenticatorData` and `clientDataJSON`.
|
|
10
|
+
* Does not restrict key ordering or additional client fields (such as iOS
|
|
11
|
+
* DIDConnect `signature` and `userHandle`), relying on `unsignedTxHash`
|
|
12
|
+
* deduplication and `nonce > 0` for replay protection.
|
|
13
|
+
*/
|
|
14
|
+
declare function isCanonicalPasskeyExtra(extra: string): boolean;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, isCanonicalPasskeyExtra, isDedupHeightActive, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, resolveEffectiveHeight, tryResolveDedupHeightActiveSync };
|
package/lib/index.cjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
3
|
+
const require_helpers_dedup_height = require('./helpers/dedup-height.cjs');
|
|
2
4
|
const require_pipes_anti_land_attack = require('./pipes/anti-land-attack.cjs');
|
|
3
5
|
const require_pipes_decode_itx = require('./pipes/decode-itx.cjs');
|
|
4
6
|
const require_pipes_decode_tx = require('./pipes/decode-tx.cjs');
|
|
@@ -27,12 +29,14 @@ const require_pipes_verify_tx_input = require('./pipes/verify-tx-input.cjs');
|
|
|
27
29
|
const require_pipes_verify_tx_size = require('./pipes/verify-tx-size.cjs');
|
|
28
30
|
const require_pipes_verify_updater = require('./pipes/verify-updater.cjs');
|
|
29
31
|
const require_runner = require('./runner.cjs');
|
|
32
|
+
let _ocap_state = require("@ocap/state");
|
|
30
33
|
|
|
31
34
|
//#region src/index.ts
|
|
32
35
|
const pipes = {
|
|
33
36
|
AntiLandAttack: require_pipes_anti_land_attack.default,
|
|
34
37
|
DecodeItx: require_pipes_decode_itx.default,
|
|
35
38
|
DecodeTx: require_pipes_decode_tx.default,
|
|
39
|
+
assertCanonicalActiveTx: require_pipes_decode_tx.assertCanonicalActiveTx,
|
|
36
40
|
ExtractReceiver: require_pipes_extract_receiver.default,
|
|
37
41
|
ExtractSigner: require_pipes_extract_signer.default,
|
|
38
42
|
ExtractState: require_pipes_extract_state.default,
|
|
@@ -65,11 +69,13 @@ var src_default = {
|
|
|
65
69
|
|
|
66
70
|
//#endregion
|
|
67
71
|
exports.AntiLandAttack = require_pipes_anti_land_attack.default;
|
|
72
|
+
exports.CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS = _ocap_state.CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS;
|
|
68
73
|
exports.DecodeItx = require_pipes_decode_itx.default;
|
|
69
74
|
exports.DecodeTx = require_pipes_decode_tx.default;
|
|
70
75
|
exports.ExtractReceiver = require_pipes_extract_receiver.default;
|
|
71
76
|
exports.ExtractSigner = require_pipes_extract_signer.default;
|
|
72
77
|
exports.ExtractState = require_pipes_extract_state.default;
|
|
78
|
+
exports.PENDING_DEDUP_ACTIVATION_HEIGHT = _ocap_state.PENDING_DEDUP_ACTIVATION_HEIGHT;
|
|
73
79
|
exports.Runner = require_runner.default;
|
|
74
80
|
exports.TakeStateSnapshot = require_pipes_take_state_snapshot.default;
|
|
75
81
|
exports.UpdateOwner = require_pipes_update_owner.default;
|
|
@@ -91,8 +97,17 @@ exports.VerifyTx = require_pipes_verify_tx.default;
|
|
|
91
97
|
exports.VerifyTxInput = require_pipes_verify_tx_input.default;
|
|
92
98
|
exports.VerifyTxSize = require_pipes_verify_tx_size.default;
|
|
93
99
|
exports.VerifyUpdater = require_pipes_verify_updater.default;
|
|
100
|
+
exports.assertCanonicalActiveTx = require_pipes_decode_tx.assertCanonicalActiveTx;
|
|
94
101
|
exports.default = src_default;
|
|
95
102
|
exports.groupStatesByAddress = require_pipes_verify_state_diff.groupStatesByAddress;
|
|
103
|
+
exports.isCanonicalPasskeyExtra = require_helpers_dedup_height.isCanonicalPasskeyExtra;
|
|
104
|
+
exports.isDedupHeightActive = require_helpers_dedup_height.isDedupHeightActive;
|
|
96
105
|
exports.isDelegationSufficient = require_pipes_verify_delegation.isDelegationSufficient;
|
|
106
|
+
exports.parseNonceBn = _ocap_state.parseNonceBn;
|
|
97
107
|
exports.pipes = pipes;
|
|
108
|
+
exports.readExplicitHeight = _ocap_state.readExplicitHeight;
|
|
109
|
+
exports.resolveDedupActivationHeight = _ocap_state.resolveDedupActivationHeight;
|
|
110
|
+
exports.resolveEffectiveHeight = require_helpers_dedup_height.resolveEffectiveHeight;
|
|
111
|
+
exports.verifyAssetHierarchy = require_pipes_update_owner.verifyAssetHierarchy;
|
|
112
|
+
exports.verifyImplicitAssetOwnership = require_pipes_verify_delegation.verifyImplicitAssetOwnership;
|
|
98
113
|
exports.verifyTokenAccess = require_pipes_verify_token_access.default;
|
package/lib/index.d.cts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
+
import { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, PENDING_DEDUP_ACTIVATION_HEIGHT, isCanonicalPasskeyExtra, isDedupHeightActive, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, resolveEffectiveHeight } from "./helpers/dedup-height.cjs";
|
|
1
2
|
import PipelineRunner, { ErrorPipeFunction, PipeFunction, PipeNext, PipeOptions } from "./runner.cjs";
|
|
2
3
|
import CreateAntiLandAttackPipe from "./pipes/anti-land-attack.cjs";
|
|
3
4
|
import DecodeItx from "./pipes/decode-itx.cjs";
|
|
4
|
-
import DecodeTx from "./pipes/decode-tx.cjs";
|
|
5
|
+
import DecodeTx, { assertCanonicalActiveTx } from "./pipes/decode-tx.cjs";
|
|
5
6
|
import CreateExtractReceiverPipe from "./pipes/extract-receiver.cjs";
|
|
6
7
|
import CreateExtractSignerPipe from "./pipes/extract-signer.cjs";
|
|
7
8
|
import CreateExtractStatePipe from "./pipes/extract-state.cjs";
|
|
8
9
|
import CreateTakeStateSnapshotPipe from "./pipes/take-state-snapshot.cjs";
|
|
9
|
-
import CreateUpdateOwnerPipe from "./pipes/update-owner.cjs";
|
|
10
|
+
import CreateUpdateOwnerPipe, { verifyAssetHierarchy } from "./pipes/update-owner.cjs";
|
|
10
11
|
import CreateVerifyAccountMigrationPipe from "./pipes/verify-account-migration.cjs";
|
|
11
12
|
import CreatedVerifyBlockedPipe from "./pipes/verify-blocked.cjs";
|
|
12
|
-
import CreateVerifyDelegationPipe, { isDelegationSufficient } from "./pipes/verify-delegation.cjs";
|
|
13
|
+
import CreateVerifyDelegationPipe, { isDelegationSufficient, verifyImplicitAssetOwnership } from "./pipes/verify-delegation.cjs";
|
|
13
14
|
import CreateVerifyInfoPipe from "./pipes/verify-info.cjs";
|
|
14
15
|
import CreateVerifyListSizePipe from "./pipes/verify-list-size.cjs";
|
|
15
16
|
import CreateVerifyMultiSigPipe from "./pipes/verify-multisig.cjs";
|
|
@@ -33,6 +34,7 @@ declare const pipes: {
|
|
|
33
34
|
AntiLandAttack: typeof CreateAntiLandAttackPipe;
|
|
34
35
|
DecodeItx: typeof DecodeItx;
|
|
35
36
|
DecodeTx: typeof DecodeTx;
|
|
37
|
+
assertCanonicalActiveTx: typeof assertCanonicalActiveTx;
|
|
36
38
|
ExtractReceiver: typeof CreateExtractReceiverPipe;
|
|
37
39
|
ExtractSigner: typeof CreateExtractSignerPipe;
|
|
38
40
|
ExtractState: typeof CreateExtractStatePipe;
|
|
@@ -64,6 +66,7 @@ declare const _default: {
|
|
|
64
66
|
AntiLandAttack: typeof CreateAntiLandAttackPipe;
|
|
65
67
|
DecodeItx: typeof DecodeItx;
|
|
66
68
|
DecodeTx: typeof DecodeTx;
|
|
69
|
+
assertCanonicalActiveTx: typeof assertCanonicalActiveTx;
|
|
67
70
|
ExtractReceiver: typeof CreateExtractReceiverPipe;
|
|
68
71
|
ExtractSigner: typeof CreateExtractSignerPipe;
|
|
69
72
|
ExtractState: typeof CreateExtractStatePipe;
|
|
@@ -91,4 +94,4 @@ declare const _default: {
|
|
|
91
94
|
};
|
|
92
95
|
};
|
|
93
96
|
//#endregion
|
|
94
|
-
export { CreateAntiLandAttackPipe as AntiLandAttack, DecodeItx, DecodeTx, type ErrorPipeFunction, CreateExtractReceiverPipe as ExtractReceiver, CreateExtractSignerPipe as ExtractSigner, CreateExtractStatePipe as ExtractState, type IAccountUpdate, type IBaseContext, type IDecodedContext, type IDecodedTransaction, type IExecutedContext, type IGasContext, type IReadyContext, type ITxOnlyContext, type IWithAsset, type IWithAssets, type IWithDelegation, type IWithDelegator, type IWithEvidence, type IWithFactory, type IWithLocked, type IWithOwner, type IWithReceiver, type IWithRollup, type IWithRollupBlock, type IWithSender, type IWithStake, type IWithStakes, type IWithToken, type IWithTokenFactory, type IWithTokens, type PipeFunction, type PipeNext, type PipeOptions, PipelineRunner as Runner, type TTransactionReceipt, CreateTakeStateSnapshotPipe as TakeStateSnapshot, CreateUpdateOwnerPipe as UpdateOwner, CreateVerifyAccountMigrationPipe as VerifyAccountMigration, CreatedVerifyBlockedPipe as VerifyBlocked, CreateVerifyDelegationPipe as VerifyDelegation, CreateVerifyInfoPipe as VerifyInfo, CreateVerifyListSizePipe as VerifyListSize, CreateVerifyMultiSigPipe as VerifyMultiSig, CreateVerifyMultiSigV2Pipe as VerifyMultiSigV2, CreateVerifySenderPipe as VerifySender, VerifyServiceFee, VerifySignature, CreateVerifySignerPipe as VerifySigner, CreateVerifyStateDiffPipe as VerifyStateDiff, CreateVerifyTokenBalancePipe as VerifyTokenBalance, CreateVerifyTransferrablePipe as VerifyTransferrable, VerifyTx, CreateVerifyTxInputPipe as VerifyTxInput, VerifyTxSize, CreateVerifyUpdaterPipe as VerifyUpdater, _default as default, groupStatesByAddress, isDelegationSufficient, pipes, CreateVerifyTokenAccessPipe as verifyTokenAccess };
|
|
97
|
+
export { CreateAntiLandAttackPipe as AntiLandAttack, CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, DecodeItx, DecodeTx, type ErrorPipeFunction, CreateExtractReceiverPipe as ExtractReceiver, CreateExtractSignerPipe as ExtractSigner, CreateExtractStatePipe as ExtractState, type IAccountUpdate, type IBaseContext, type IDecodedContext, type IDecodedTransaction, type IExecutedContext, type IGasContext, type IReadyContext, type ITxOnlyContext, type IWithAsset, type IWithAssets, type IWithDelegation, type IWithDelegator, type IWithEvidence, type IWithFactory, type IWithLocked, type IWithOwner, type IWithReceiver, type IWithRollup, type IWithRollupBlock, type IWithSender, type IWithStake, type IWithStakes, type IWithToken, type IWithTokenFactory, type IWithTokens, PENDING_DEDUP_ACTIVATION_HEIGHT, type PipeFunction, type PipeNext, type PipeOptions, PipelineRunner as Runner, type TTransactionReceipt, CreateTakeStateSnapshotPipe as TakeStateSnapshot, CreateUpdateOwnerPipe as UpdateOwner, CreateVerifyAccountMigrationPipe as VerifyAccountMigration, CreatedVerifyBlockedPipe as VerifyBlocked, CreateVerifyDelegationPipe as VerifyDelegation, CreateVerifyInfoPipe as VerifyInfo, CreateVerifyListSizePipe as VerifyListSize, CreateVerifyMultiSigPipe as VerifyMultiSig, CreateVerifyMultiSigV2Pipe as VerifyMultiSigV2, CreateVerifySenderPipe as VerifySender, VerifyServiceFee, VerifySignature, CreateVerifySignerPipe as VerifySigner, CreateVerifyStateDiffPipe as VerifyStateDiff, CreateVerifyTokenBalancePipe as VerifyTokenBalance, CreateVerifyTransferrablePipe as VerifyTransferrable, VerifyTx, CreateVerifyTxInputPipe as VerifyTxInput, VerifyTxSize, CreateVerifyUpdaterPipe as VerifyUpdater, assertCanonicalActiveTx, _default as default, groupStatesByAddress, isCanonicalPasskeyExtra, isDedupHeightActive, isDelegationSufficient, parseNonceBn, pipes, readExplicitHeight, resolveDedupActivationHeight, resolveEffectiveHeight, verifyAssetHierarchy, verifyImplicitAssetOwnership, CreateVerifyTokenAccessPipe as verifyTokenAccess };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
|
-
let _ocap_util = require("@ocap/util");
|
|
4
|
-
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
5
3
|
let lodash_get = require("lodash/get");
|
|
6
4
|
lodash_get = require_rolldown_runtime.__toESM(lodash_get);
|
|
5
|
+
let _ocap_util = require("@ocap/util");
|
|
6
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
7
7
|
|
|
8
8
|
//#region src/pipes/anti-land-attack.ts
|
|
9
9
|
function CreateAntiLandAttackPipe({ senderState = "senderState", receiverState = "receiverState" } = {}) {
|
package/lib/pipes/decode-tx.cjs
CHANGED
|
@@ -1,16 +1,68 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
|
+
const require_helpers_dedup_height = require('../helpers/dedup-height.cjs');
|
|
3
4
|
const require_package = require('../package.cjs');
|
|
5
|
+
const require_helpers_unwrap_opaque_any = require('../helpers/unwrap-opaque-any.cjs');
|
|
6
|
+
let lodash_get = require("lodash/get");
|
|
7
|
+
lodash_get = require_rolldown_runtime.__toESM(lodash_get);
|
|
4
8
|
let _ocap_util = require("@ocap/util");
|
|
5
9
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
10
|
+
let _ocap_message = require("@ocap/message");
|
|
6
11
|
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
7
12
|
let _ocap_message_cbor = require("@ocap/message/cbor");
|
|
8
13
|
let _ocap_message_protobuf = require("@ocap/message/protobuf");
|
|
14
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
15
|
+
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
16
|
+
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
17
|
+
let lodash_omit = require("lodash/omit");
|
|
18
|
+
lodash_omit = require_rolldown_runtime.__toESM(lodash_omit);
|
|
9
19
|
let debug = require("debug");
|
|
10
20
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
11
21
|
|
|
12
22
|
//#region src/pipes/decode-tx.ts
|
|
13
23
|
const debug$1 = (0, debug.default)(`${require_package.name}:pipe:decode-tx`);
|
|
24
|
+
function isCanonicalSigData(data, isCbor) {
|
|
25
|
+
if (!data || typeof data !== "object") return true;
|
|
26
|
+
const anyData = data;
|
|
27
|
+
const { typeUrl, value } = anyData;
|
|
28
|
+
if (!typeUrl && !value && !anyData.type) return true;
|
|
29
|
+
if (typeUrl && ["json", "vc"].includes(typeUrl)) {
|
|
30
|
+
if (!isCbor && typeof value === "string") {
|
|
31
|
+
const rawBytes = Buffer.from(value, "base64");
|
|
32
|
+
if (rawBytes.toString("base64") !== value) return false;
|
|
33
|
+
const rawStr = rawBytes.toString("utf8");
|
|
34
|
+
let parsed;
|
|
35
|
+
try {
|
|
36
|
+
parsed = JSON.parse(rawStr);
|
|
37
|
+
} catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (JSON.stringify(parsed) !== rawStr) return false;
|
|
41
|
+
if (typeof parsed === "string") try {
|
|
42
|
+
const innerBytes = Buffer.from(parsed, "base64");
|
|
43
|
+
const innerStr = innerBytes.toString("utf8");
|
|
44
|
+
const innerParsed = JSON.parse(innerStr);
|
|
45
|
+
if (innerBytes.toString("base64") !== parsed || JSON.stringify(innerParsed) !== innerStr) return false;
|
|
46
|
+
} catch {}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
if (isCbor && typeof value === "string") try {
|
|
50
|
+
const innerBytes = Buffer.from(value, "base64");
|
|
51
|
+
const innerStr = innerBytes.toString("utf8");
|
|
52
|
+
const innerParsed = JSON.parse(innerStr);
|
|
53
|
+
if (innerBytes.toString("base64") !== value || JSON.stringify(innerParsed) !== innerStr) return false;
|
|
54
|
+
} catch {}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
if (!isCbor && typeUrl && typeUrl !== "fg:x:address" && typeof value === "string" && value.length > 0) {
|
|
58
|
+
const decoded = (0, _ocap_message.decodeAny)(anyData);
|
|
59
|
+
if (decoded?.type && decoded.type !== "Unknown") {
|
|
60
|
+
const reencodedAny = (0, _ocap_message.encodeAny)(decoded)?.toObject();
|
|
61
|
+
if (!reencodedAny || reencodedAny.value !== value) return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
14
66
|
/**
|
|
15
67
|
* Detect the CBOR self-describe tag 55799 that our canonical encoder always
|
|
16
68
|
* prepends to a Transaction. The first three bytes are unambiguously distinct
|
|
@@ -21,45 +73,143 @@ const debug$1 = (0, debug.default)(`${require_package.name}:pipe:decode-tx`);
|
|
|
21
73
|
function hasCborSelfDescribeTag(buf) {
|
|
22
74
|
return buf.length >= _ocap_message_cbor.CANONICAL_SELF_DESCRIBE_PREFIX.length && buf[0] === _ocap_message_cbor.CANONICAL_SELF_DESCRIBE_PREFIX[0] && buf[1] === _ocap_message_cbor.CANONICAL_SELF_DESCRIBE_PREFIX[1] && buf[2] === _ocap_message_cbor.CANONICAL_SELF_DESCRIBE_PREFIX[2];
|
|
23
75
|
}
|
|
76
|
+
const NON_CANONICAL_MESSAGE = "Transaction encoding is not canonical";
|
|
77
|
+
const V1_MULTISIG_TX_TYPES = new Set(["fg:t:exchange", "fg:t:exchange_v2"]);
|
|
78
|
+
function assertCanonicalActiveTx(context) {
|
|
79
|
+
if (!context?.txBase64) return;
|
|
80
|
+
const txBuffer = (0, _ocap_util.toUint8Array)((0, _ocap_util.fromBase64)(context.txBase64));
|
|
81
|
+
const isCbor = hasCborSelfDescribeTag(txBuffer);
|
|
82
|
+
const unsignedTx = (0, lodash_cloneDeep.default)(isCbor ? (0, _ocap_message_cbor.decodeTx)(txBuffer) : (0, _ocap_message_protobuf.decodeTx)(txBuffer));
|
|
83
|
+
unsignedTx.signature = void 0;
|
|
84
|
+
const sigs = (0, _ocap_util_lib_get_list_field.getListField)(unsignedTx, "signatures");
|
|
85
|
+
if (sigs.length > 0) {
|
|
86
|
+
for (const sig of sigs) if (!isCanonicalSigData(sig.data, isCbor)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", NON_CANONICAL_MESSAGE);
|
|
87
|
+
try {
|
|
88
|
+
(0, _ocap_util_lib_get_list_field.setListField)(unsignedTx, "signatures", sigs.map((x) => {
|
|
89
|
+
const stripped = (0, lodash_omit.default)(x, ["signature", "extra"]);
|
|
90
|
+
if (stripped.data) stripped.data = require_helpers_unwrap_opaque_any.unwrapOpaqueAnyData((0, _ocap_message.decodeAny)(stripped.data));
|
|
91
|
+
return stripped;
|
|
92
|
+
}));
|
|
93
|
+
} catch {
|
|
94
|
+
throw new _ocap_util_lib_error.CustomError("INVALID_TX", "Can not decode base64 encoded transaction");
|
|
95
|
+
}
|
|
96
|
+
const unsignedBytes = isCbor ? (0, _ocap_message_cbor.encodeTx)(unsignedTx) : (0, _ocap_message_protobuf.encodeTx)(unsignedTx);
|
|
97
|
+
Object.defineProperty(context, "unsignedTxHash", {
|
|
98
|
+
value: (0, _ocap_mcrypto.toTxHash)(unsignedBytes),
|
|
99
|
+
enumerable: true,
|
|
100
|
+
configurable: true
|
|
101
|
+
});
|
|
102
|
+
const multiSignV2Txs = (0, lodash_get.default)(context, "config.transaction.multiSignV2Txs", []);
|
|
103
|
+
const txType = context.txType;
|
|
104
|
+
if (txType && !multiSignV2Txs.includes(txType)) {
|
|
105
|
+
if (!V1_MULTISIG_TX_TYPES.has(txType)) throw new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Expect 0 multi signatures but found ${sigs.length}`);
|
|
106
|
+
if (sigs.length > 1) throw new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Expect 1 multi signatures but found ${sigs.length}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
24
110
|
function DecodeTx(context, next) {
|
|
111
|
+
let nonCanonical = false;
|
|
25
112
|
try {
|
|
26
113
|
const txBuffer = (0, _ocap_util.toUint8Array)((0, _ocap_util.fromBase64)(context.txBase64));
|
|
27
114
|
const isCbor = hasCborSelfDescribeTag(txBuffer);
|
|
28
115
|
const tx = isCbor ? (0, _ocap_message_cbor.decodeTx)(txBuffer) : (0, _ocap_message_protobuf.decodeTx)(txBuffer);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
116
|
+
const reencoded = isCbor ? (0, _ocap_message_cbor.encodeTx)(tx) : (0, _ocap_message_protobuf.encodeTx)(tx);
|
|
117
|
+
if (reencoded.length !== txBuffer.length || !reencoded.every((b, i) => b === txBuffer[i])) {
|
|
118
|
+
nonCanonical = true;
|
|
119
|
+
throw new _ocap_util_lib_error.CustomError("INVALID_TX", NON_CANONICAL_MESSAGE);
|
|
120
|
+
}
|
|
121
|
+
const unsignedTx = (0, lodash_cloneDeep.default)(tx);
|
|
122
|
+
unsignedTx.signature = void 0;
|
|
123
|
+
const sigs = (0, _ocap_util_lib_get_list_field.getListField)(unsignedTx, "signatures");
|
|
124
|
+
const finalizeDecode = (dedupActive) => {
|
|
125
|
+
if (sigs.length > 0) {
|
|
126
|
+
if (dedupActive) {
|
|
127
|
+
for (const sig of sigs) if (!isCanonicalSigData(sig.data, isCbor)) {
|
|
128
|
+
nonCanonical = true;
|
|
129
|
+
throw new _ocap_util_lib_error.CustomError("INVALID_TX", NON_CANONICAL_MESSAGE);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
(0, _ocap_util_lib_get_list_field.setListField)(unsignedTx, "signatures", sigs.map((x) => {
|
|
133
|
+
const stripped = (0, lodash_omit.default)(x, ["signature", "extra"]);
|
|
134
|
+
if (stripped.data) if (dedupActive) stripped.data = require_helpers_unwrap_opaque_any.unwrapOpaqueAnyData((0, _ocap_message.decodeAny)(stripped.data));
|
|
135
|
+
else try {
|
|
136
|
+
stripped.data = require_helpers_unwrap_opaque_any.unwrapOpaqueAnyData((0, _ocap_message.decodeAny)(stripped.data));
|
|
137
|
+
} catch {}
|
|
138
|
+
return stripped;
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
const unsignedBytes = isCbor ? (0, _ocap_message_cbor.encodeTx)(unsignedTx) : (0, _ocap_message_protobuf.encodeTx)(unsignedTx);
|
|
142
|
+
Object.defineProperty(context, "tx", {
|
|
143
|
+
value: tx,
|
|
144
|
+
enumerable: true,
|
|
145
|
+
configurable: true
|
|
146
|
+
});
|
|
147
|
+
Object.defineProperty(context, "txEncoding", {
|
|
148
|
+
value: isCbor ? "cbor" : "protobuf",
|
|
149
|
+
enumerable: true,
|
|
150
|
+
configurable: true
|
|
151
|
+
});
|
|
152
|
+
Object.defineProperty(context, "txSize", {
|
|
153
|
+
value: Buffer.byteLength(txBuffer),
|
|
154
|
+
enumerable: true,
|
|
155
|
+
configurable: true
|
|
156
|
+
});
|
|
157
|
+
Object.defineProperty(context, "txHash", {
|
|
158
|
+
value: (0, _ocap_mcrypto.toTxHash)(txBuffer),
|
|
159
|
+
enumerable: true,
|
|
160
|
+
configurable: true
|
|
161
|
+
});
|
|
162
|
+
Object.defineProperty(context, "unsignedTxHash", {
|
|
163
|
+
value: (0, _ocap_mcrypto.toTxHash)(unsignedBytes),
|
|
164
|
+
enumerable: true,
|
|
165
|
+
configurable: true
|
|
166
|
+
});
|
|
167
|
+
const pinned = context.extra?.txTime;
|
|
168
|
+
const pinnedMs = pinned === void 0 || pinned === null || pinned === "" ? NaN : new Date(pinned).getTime();
|
|
169
|
+
Object.defineProperty(context, "txTime", {
|
|
170
|
+
value: Number.isFinite(pinnedMs) ? new Date(pinnedMs).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
|
|
171
|
+
enumerable: true,
|
|
172
|
+
configurable: true
|
|
173
|
+
});
|
|
174
|
+
Object.defineProperty(context, "txBaseGas", {
|
|
175
|
+
value: true,
|
|
176
|
+
enumerable: true,
|
|
177
|
+
writable: true,
|
|
178
|
+
configurable: true
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
if (sigs.length > 0) {
|
|
182
|
+
const syncActive = require_helpers_dedup_height.tryResolveDedupHeightActiveSync(context);
|
|
183
|
+
if (syncActive === null) return require_helpers_dedup_height.isDedupHeightActive(context).then((dedupActive) => {
|
|
184
|
+
try {
|
|
185
|
+
finalizeDecode(dedupActive);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
debug$1("failed to decode transaction", {
|
|
188
|
+
txBase64: context.txBase64,
|
|
189
|
+
error: err
|
|
190
|
+
});
|
|
191
|
+
return next(new _ocap_util_lib_error.CustomError("INVALID_TX", nonCanonical ? NON_CANONICAL_MESSAGE : "Can not decode base64 encoded transaction"));
|
|
192
|
+
}
|
|
193
|
+
next();
|
|
194
|
+
}, (err) => {
|
|
195
|
+
debug$1("failed to resolve dedup height for transaction", {
|
|
196
|
+
txBase64: context.txBase64,
|
|
197
|
+
error: err
|
|
198
|
+
});
|
|
199
|
+
return next(err);
|
|
200
|
+
});
|
|
201
|
+
finalizeDecode(syncActive);
|
|
202
|
+
} else finalizeDecode(true);
|
|
54
203
|
} catch (err) {
|
|
55
204
|
debug$1("failed to decode transaction", {
|
|
56
205
|
txBase64: context.txBase64,
|
|
57
206
|
error: err
|
|
58
207
|
});
|
|
59
|
-
return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Can not decode base64 encoded transaction"));
|
|
208
|
+
return next(new _ocap_util_lib_error.CustomError("INVALID_TX", nonCanonical ? NON_CANONICAL_MESSAGE : "Can not decode base64 encoded transaction"));
|
|
60
209
|
}
|
|
61
210
|
next();
|
|
62
211
|
}
|
|
63
212
|
|
|
64
213
|
//#endregion
|
|
214
|
+
exports.assertCanonicalActiveTx = assertCanonicalActiveTx;
|
|
65
215
|
exports.default = DecodeTx;
|
|
@@ -2,6 +2,7 @@ import { IBaseContext } from "@ocap/types";
|
|
|
2
2
|
import { CustomError } from "@ocap/util/lib/error";
|
|
3
3
|
|
|
4
4
|
//#region src/pipes/decode-tx.d.ts
|
|
5
|
-
declare function
|
|
5
|
+
declare function assertCanonicalActiveTx(context: IBaseContext): void;
|
|
6
|
+
declare function DecodeTx(context: IBaseContext, next: (err?: CustomError) => void): void | Promise<void>;
|
|
6
7
|
//#endregion
|
|
7
|
-
export { DecodeTx as default };
|
|
8
|
+
export { assertCanonicalActiveTx, DecodeTx as default };
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
|
+
let _ocap_state = require("@ocap/state");
|
|
3
4
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
5
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
6
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
4
7
|
let lodash_set = require("lodash/set");
|
|
5
8
|
lodash_set = require_rolldown_runtime.__toESM(lodash_set);
|
|
6
|
-
let
|
|
9
|
+
let _arcblock_did = require("@arcblock/did");
|
|
7
10
|
|
|
8
11
|
//#region src/pipes/extract-signer.ts
|
|
9
12
|
function CreateExtractSignerPipe({ signerKey = "signerStates", delegatorKey = "delegatorStates" } = {}) {
|
|
@@ -17,7 +20,13 @@ function CreateExtractSignerPipe({ signerKey = "signerStates", delegatorKey = "d
|
|
|
17
20
|
}
|
|
18
21
|
const signers = signatures.map((x) => x.signer).filter(Boolean);
|
|
19
22
|
const delegators = signatures.map((x) => x.delegator).filter(Boolean);
|
|
20
|
-
const signerStates = (await Promise.all(signers.map((x) =>
|
|
23
|
+
const signerStates = (await Promise.all(signers.map(async (x) => {
|
|
24
|
+
const state = await statedb.account.get(x, context);
|
|
25
|
+
if (!state && (0, _arcblock_did.toTypeInfo)(x).role === _ocap_mcrypto.types.RoleType.ROLE_ASSET) {
|
|
26
|
+
if (await statedb.asset?.get?.(x, context)) return _ocap_state.account.create({ address: x }, context);
|
|
27
|
+
}
|
|
28
|
+
return state;
|
|
29
|
+
}))).filter(Boolean);
|
|
21
30
|
const delegatorStates = (await Promise.all(delegators.map((x) => statedb.account.get(x, context)))).filter(Boolean);
|
|
22
31
|
if (signerStates.length !== signers.length) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNER_STATE", "Signer states does not match"));
|
|
23
32
|
(0, lodash_set.default)(context, signerKey, signerStates);
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
const require_package = require('../package.cjs');
|
|
4
|
-
let
|
|
4
|
+
let _ocap_state = require("@ocap/state");
|
|
5
5
|
let lodash_get = require("lodash/get");
|
|
6
6
|
lodash_get = require_rolldown_runtime.__toESM(lodash_get);
|
|
7
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
7
8
|
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
8
9
|
let debug = require("debug");
|
|
9
10
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
10
11
|
let lodash_set = require("lodash/set");
|
|
11
12
|
lodash_set = require_rolldown_runtime.__toESM(lodash_set);
|
|
12
13
|
let _arcblock_did = require("@arcblock/did");
|
|
13
|
-
let _ocap_state = require("@ocap/state");
|
|
14
14
|
let _ocap_util_lib_get_related_addr = require("@ocap/util/lib/get-related-addr");
|
|
15
15
|
let lodash_isEmpty = require("lodash/isEmpty");
|
|
16
16
|
lodash_isEmpty = require_rolldown_runtime.__toESM(lodash_isEmpty);
|
|
@@ -27,17 +27,17 @@ const getRelatedAddrList = (x) => x.hash ? [x.hash] : (0, _ocap_util_lib_get_rel
|
|
|
27
27
|
* @return {string} params.table which statedb to query, default is auto detect, can be [account,asset,delegation,factory,token]
|
|
28
28
|
* @return {string} params.status error code when the extracted state is empty
|
|
29
29
|
*/
|
|
30
|
-
function CreateExtractStatePipe({ from, to, table, status = "OK" }) {
|
|
30
|
+
function CreateExtractStatePipe({ from, to, table, status = "OK", message }) {
|
|
31
31
|
if (table && !_ocap_state.tables.includes(table)) throw new _ocap_util_lib_error.CustomError("INTERNAL", "Invalid table param for ExtractState pipe");
|
|
32
32
|
return async function ExtractState(context, next) {
|
|
33
33
|
if (typeof (0, lodash_get.default)(context, to) !== "undefined") return next();
|
|
34
|
-
const address = (0, lodash_get.default)(context, from);
|
|
34
|
+
const address = (0, lodash_get.default)(context, from) ?? (from.endsWith("List") ? (0, lodash_get.default)(context, from.slice(0, -4)) : void 0);
|
|
35
35
|
if ((0, lodash_isEmpty.default)(address)) {
|
|
36
36
|
(0, lodash_set.default)(context, to, void 0);
|
|
37
37
|
return next();
|
|
38
38
|
}
|
|
39
39
|
const list = Array.isArray(address) ? address : [address];
|
|
40
|
-
const result = (await Promise.all(list.map((x) => {
|
|
40
|
+
const result = (await Promise.all(list.map(async (x) => {
|
|
41
41
|
if (typeof table === "undefined") {
|
|
42
42
|
const type = (0, _arcblock_did.toTypeInfo)(x);
|
|
43
43
|
if (type.role === _ocap_mcrypto.types.RoleType.ROLE_ASSET) return context.statedb.asset.get(x, context);
|
|
@@ -49,7 +49,11 @@ function CreateExtractStatePipe({ from, to, table, status = "OK" }) {
|
|
|
49
49
|
if (type.role === _ocap_mcrypto.types.RoleType.ROLE_TOKEN_FACTORY) return context.statedb.tokenFactory.get(x, context);
|
|
50
50
|
return context.statedb.account.get(x, context);
|
|
51
51
|
}
|
|
52
|
-
|
|
52
|
+
const state = await context.statedb[table].get(x, context);
|
|
53
|
+
if (!state && status !== "OK" && table === "account" && (0, _arcblock_did.toTypeInfo)(x).role === _ocap_mcrypto.types.RoleType.ROLE_ASSET) {
|
|
54
|
+
if (await context.statedb.asset?.get?.(x, context)) return _ocap_state.account.create({ address: x }, context);
|
|
55
|
+
}
|
|
56
|
+
return state;
|
|
53
57
|
}))).filter(Boolean);
|
|
54
58
|
if (status !== "OK") {
|
|
55
59
|
const missing = list.find((x) => !result.find((r) => getRelatedAddrList(r).includes(x)));
|
|
@@ -58,7 +62,10 @@ function CreateExtractStatePipe({ from, to, table, status = "OK" }) {
|
|
|
58
62
|
result,
|
|
59
63
|
missing
|
|
60
64
|
});
|
|
61
|
-
if (missing)
|
|
65
|
+
if (missing) {
|
|
66
|
+
const role = ((0, _arcblock_did.toTypeInfoStr)(missing).role || "").replace("ROLE_", "");
|
|
67
|
+
return next(new _ocap_util_lib_error.CustomError(status, message ? message(missing, role || table || "") : `Invalid state extracted for ${role || table}: ${missing}`, { persist: true }));
|
|
68
|
+
}
|
|
62
69
|
}
|
|
63
70
|
(0, lodash_set.default)(context, to, Array.isArray(address) ? result : result.shift());
|
|
64
71
|
next();
|
|
@@ -7,6 +7,8 @@ interface ExtractStateOptions {
|
|
|
7
7
|
to: string;
|
|
8
8
|
table?: string;
|
|
9
9
|
status?: string;
|
|
10
|
+
/** Override the default "Invalid state extracted for X: Y" message with a clearer, caller-specific one. */
|
|
11
|
+
message?: (address: string, role: string) => string;
|
|
10
12
|
}
|
|
11
13
|
/**
|
|
12
14
|
* Extract state from state db
|
|
@@ -21,7 +23,8 @@ declare function CreateExtractStatePipe<TContext extends IBaseContext = IBaseCon
|
|
|
21
23
|
from,
|
|
22
24
|
to,
|
|
23
25
|
table,
|
|
24
|
-
status
|
|
26
|
+
status,
|
|
27
|
+
message
|
|
25
28
|
}: ExtractStateOptions): PipeFunction<TContext>;
|
|
26
29
|
//#endregion
|
|
27
30
|
export { CreateExtractStatePipe as default };
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
const require_pipes_verify_state_diff = require('./verify-state-diff.cjs');
|
|
4
|
+
let _ocap_util = require("@ocap/util");
|
|
5
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
6
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
4
7
|
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
5
8
|
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
6
9
|
|
|
@@ -13,6 +16,11 @@ function CreateTakeStateSnapshotPipe({ key = "stateSnapshot" } = {}) {
|
|
|
13
16
|
return async function TakeStateSnapshot(context, next) {
|
|
14
17
|
const { statedb } = context;
|
|
15
18
|
const ctx = context;
|
|
19
|
+
if (ctx.tx?.from && ctx.senderState?.address && ctx.senderState.address !== ctx.tx.from && !(0, _ocap_util.isSameDid)(ctx.senderState.address, ctx.tx.from)) return next(new _ocap_util_lib_error.CustomError("ACCOUNT_MIGRATED", "Cannot send tx from migrated account"));
|
|
20
|
+
if (ctx.tx && !context.txType?.startsWith("fg:x:")) {
|
|
21
|
+
if (ctx.tx.delegator && !ctx.delegationState && !ctx.isImplicitDelegation) return next(new _ocap_util_lib_error.CustomError("INVALID_DELEGATION", "Delegation is not supported for this transaction"));
|
|
22
|
+
if ((0, _ocap_util_lib_get_list_field.getListField)(ctx.tx, "signatures").some((s) => Boolean(s?.delegator)) && !ctx.delegationStates && !Array.isArray(ctx.delegationState)) return next(new _ocap_util_lib_error.CustomError("INVALID_DELEGATION", "Multisig delegation is not supported for this transaction"));
|
|
23
|
+
}
|
|
16
24
|
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]);
|
|
17
25
|
if (feeVaultState) ctx.feeVaultState = feeVaultState;
|
|
18
26
|
if (gasVaultState) ctx.gasVaultState = gasVaultState;
|