@ocap/state 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/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { account_d_exports } from "./states/account.mjs";
1
+ import { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, account_d_exports, isExplicitlyPreActivation, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight } from "./states/account.mjs";
2
2
  import { asset_d_exports } from "./states/asset.mjs";
3
3
  import { ChainState, chain_d_exports } from "./states/chain.mjs";
4
4
  import { delegation_d_exports } from "./states/delegation.mjs";
@@ -17,4 +17,4 @@ import Blacklist, { BlacklistOptions, TrustedAccountConfig } from "./states/blac
17
17
  declare const indexes: readonly ["account", "asset", "delegation", "tx", "factory", "token", "stake", "rollup", "rollupBlock", "rollupValidator", "tokenDistribution", "tokenFactory"];
18
18
  declare const tables: readonly ["chain", "account", "asset", "delegation", "tx", "factory", "token", "stake", "rollup", "rollupBlock", "evidence", "balance", "tokenFactory"];
19
19
  //#endregion
20
- export { Blacklist, type BlacklistOptions, type ChainState, type ItxJson, type Receipt, type ReceiptChange, type TokenInput, type TrustedAccountConfig, type TxContext, type TxData, type TxInput, type TxOutput, type TxResult, account_d_exports as account, asset_d_exports as asset, chain_d_exports as chain, delegation_d_exports as delegation, evidence_d_exports as evidence, factory_d_exports as factory, indexes, rollup_d_exports as rollup, rollup_block_d_exports as rollupBlock, stake_d_exports as stake, tables, token_d_exports as token, token_factory_d_exports as tokenFactory, tx_d_exports as tx };
20
+ export { Blacklist, type BlacklistOptions, CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, type ChainState, EXPLICIT_HEIGHT_PATHS, type ItxJson, PENDING_DEDUP_ACTIVATION_HEIGHT, type Receipt, type ReceiptChange, type TokenInput, type TrustedAccountConfig, type TxContext, type TxData, type TxInput, type TxOutput, type TxResult, account_d_exports as account, asset_d_exports as asset, chain_d_exports as chain, delegation_d_exports as delegation, evidence_d_exports as evidence, factory_d_exports as factory, indexes, isExplicitlyPreActivation, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, rollup_d_exports as rollup, rollup_block_d_exports as rollupBlock, stake_d_exports as stake, tables, token_d_exports as token, token_factory_d_exports as tokenFactory, tx_d_exports as tx };
package/esm/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { account_exports } from "./states/account.mjs";
1
+ import { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, account_exports, isExplicitlyPreActivation, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight } from "./states/account.mjs";
2
2
  import { asset_exports } from "./states/asset.mjs";
3
3
  import { chain_exports } from "./states/chain.mjs";
4
4
  import { delegation_exports } from "./states/delegation.mjs";
@@ -44,4 +44,4 @@ const tables = [
44
44
  ];
45
45
 
46
46
  //#endregion
47
- export { blacklist_default as Blacklist, account_exports as account, asset_exports as asset, chain_exports as chain, delegation_exports as delegation, evidence_exports as evidence, factory_exports as factory, indexes, rollup_exports as rollup, rollup_block_exports as rollupBlock, stake_exports as stake, tables, token_exports as token, token_factory_exports as tokenFactory, tx_exports as tx };
47
+ export { blacklist_default as Blacklist, CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, account_exports as account, asset_exports as asset, chain_exports as chain, delegation_exports as delegation, evidence_exports as evidence, factory_exports as factory, indexes, isExplicitlyPreActivation, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, rollup_exports as rollup, rollup_block_exports as rollupBlock, stake_exports as stake, tables, token_exports as token, token_factory_exports as tokenFactory, tx_exports as tx };
@@ -1,17 +1,18 @@
1
1
  import { StateContextInput } from "../contexts/state.mjs";
2
+ import { BN } from "@ocap/util";
2
3
  import { v } from "@arcblock/validator";
3
4
  import { IAccountState } from "@ocap/types";
4
5
 
5
6
  //#region src/states/account.d.ts
6
7
  declare namespace account_d_exports {
7
- export { create, ensureChecksumAddress, isMigrated, schema, update, updateOrCreate, validate };
8
+ export { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, create, ensureChecksumAddress, isExplicitlyPreActivation, isMigrated, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, schema, update, updateOrCreate, validate };
8
9
  }
9
10
  declare const schema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
10
11
  readonly address: any;
11
12
  readonly pk: v.OptionalSchema<v.NullableSchema<v.StringSchema<undefined>, undefined>, undefined>;
12
13
  readonly issuer: v.OptionalSchema<v.NullableSchema<v.UnionSchema<[v.LiteralSchema<"", undefined>, any], undefined>, undefined>, undefined>;
13
14
  readonly moniker: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.CheckAction<string, "Invalid moniker format">]>, undefined>;
14
- readonly nonce: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>]>, 0>;
15
+ readonly nonce: v.OptionalSchema<v.UnionSchema<[v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>]>, v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.CheckAction<string, "Invalid nonce format">]>], undefined>, 0>;
15
16
  readonly tokens: v.OptionalSchema<any, {}>;
16
17
  readonly migratedTo: v.OptionalSchema<v.ArraySchema<any, undefined>, readonly []>;
17
18
  readonly migratedFrom: v.OptionalSchema<v.ArraySchema<any, undefined>, readonly []>;
@@ -24,6 +25,13 @@ declare const schema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
24
25
  readonly data: v.OptionalSchema<v.NullableSchema<v.AnySchema, undefined>, undefined>;
25
26
  }, undefined>, any]>;
26
27
  declare const create: (attrs: Partial<IAccountState>, context: StateContextInput) => IAccountState;
28
+ declare const PENDING_DEDUP_ACTIVATION_HEIGHT: number;
29
+ declare const CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS: Readonly<Record<string, number>>;
30
+ declare const EXPLICIT_HEIGHT_PATHS: readonly ["height", "blockHeight", "sequence", "ledgerSequence", "expectedLedgerSequence", "extra.height", "extra.blockHeight", "extra.sequence", "tx.height"];
31
+ declare const readExplicitHeight: (context: unknown) => number | undefined;
32
+ declare const resolveDedupActivationHeight: (context: unknown) => number;
33
+ declare const isExplicitlyPreActivation: (context: unknown) => boolean;
34
+ declare const parseNonceBn: (rawNonce: unknown) => InstanceType<typeof BN> | null;
27
35
  declare const update: (state: IAccountState, attrs: Partial<IAccountState>, context: StateContextInput) => IAccountState;
28
36
  declare const updateOrCreate: (state: IAccountState | null, attrs: Partial<IAccountState>, context: StateContextInput) => IAccountState;
29
37
  declare const validate: (state: IAccountState) => IAccountState;
@@ -32,4 +40,4 @@ declare const isMigrated: (state: {
32
40
  }) => boolean;
33
41
  declare const ensureChecksumAddress: (address: string) => string;
34
42
  //#endregion
35
- export { account_d_exports, create, ensureChecksumAddress, isMigrated, schema, update, updateOrCreate, validate };
43
+ export { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, account_d_exports, create, ensureChecksumAddress, isExplicitlyPreActivation, isMigrated, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, schema, update, updateOrCreate, validate };
@@ -5,15 +5,23 @@ import { isEthereumDid, toChecksumAddress } from "@arcblock/did/lib/type";
5
5
  import { BN, toAddress, toBase58 } from "@ocap/util";
6
6
  import { CustomError } from "@ocap/util/lib/error";
7
7
  import flatten from "lodash/flatten.js";
8
+ import get from "lodash/get.js";
8
9
  import pick from "lodash/pick.js";
9
10
  import uniq from "lodash/uniq.js";
10
11
  import { schemas, v, vBN, vBNMin, vDID, vFilteredRecord, vStripUnknown, vValidate } from "@arcblock/validator";
11
12
 
12
13
  //#region src/states/account.ts
13
14
  var account_exports = /* @__PURE__ */ __exportAll({
15
+ CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS: () => CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS,
16
+ EXPLICIT_HEIGHT_PATHS: () => EXPLICIT_HEIGHT_PATHS,
17
+ PENDING_DEDUP_ACTIVATION_HEIGHT: () => PENDING_DEDUP_ACTIVATION_HEIGHT,
14
18
  create: () => create,
15
19
  ensureChecksumAddress: () => ensureChecksumAddress,
20
+ isExplicitlyPreActivation: () => isExplicitlyPreActivation,
16
21
  isMigrated: () => isMigrated,
22
+ parseNonceBn: () => parseNonceBn,
23
+ readExplicitHeight: () => readExplicitHeight,
24
+ resolveDedupActivationHeight: () => resolveDedupActivationHeight,
17
25
  schema: () => schema,
18
26
  update: () => update,
19
27
  updateOrCreate: () => updateOrCreate,
@@ -24,7 +32,7 @@ const schema = v.pipe(v.object({
24
32
  pk: v.optional(v.nullable(v.string())),
25
33
  issuer: v.optional(v.nullable(v.union([v.literal(""), vDID({ prefix: true })]))),
26
34
  moniker: v.optional(v.pipe(v.string(), v.check((s) => s === "" || /^[a-zA-Z0-9][-a-zA-Z0-9_]{2,128}$/.test(s), "Invalid moniker format"))),
27
- nonce: v.optional(v.pipe(v.number(), v.minValue(0)), 0),
35
+ nonce: v.optional(v.union([v.pipe(v.number(), v.minValue(0)), v.pipe(v.string(), v.check((s) => /^\d+$/.test(s), "Invalid nonce format"))]), 0),
28
36
  tokens: v.optional(vFilteredRecord(vDID({
29
37
  prefix: true,
30
38
  role: "ROLE_TOKEN"
@@ -35,13 +43,16 @@ const schema = v.pipe(v.object({
35
43
  data: v.optional(v.nullable(v.any()))
36
44
  }), vStripUnknown());
37
45
  const create = (attrs, context) => {
46
+ const normalizedAttrs = { ...attrs };
47
+ const targetAddress = toAddress(ensureChecksumAddress(normalizedAttrs.address || ""));
48
+ if (normalizedAttrs.pk && isFromPublicKey(targetAddress, normalizedAttrs.pk) === false) delete normalizedAttrs.pk;
38
49
  const account = {
39
50
  nonce: 0,
40
- migratedTo: uniq([].concat(attrs.migratedTo || [])),
51
+ migratedTo: uniq([].concat(normalizedAttrs.migratedTo || [])),
41
52
  migratedFrom: [],
42
53
  tokens: {},
43
54
  context: create$1(context),
44
- ...pick(attrs, [
55
+ ...pick(normalizedAttrs, [
45
56
  "address",
46
57
  "pk",
47
58
  "issuer",
@@ -52,23 +63,107 @@ const create = (attrs, context) => {
52
63
  "tokens"
53
64
  ])
54
65
  };
55
- account.address = toAddress(ensureChecksumAddress(account.address));
66
+ account.address = targetAddress;
56
67
  if (!account.moniker) account.moniker = [account.address.slice(0, 6), account.address.slice(-5)].join("-");
57
68
  return validate(account);
58
69
  };
70
+ const PENDING_DEDUP_ACTIVATION_HEIGHT = Number.MAX_SAFE_INTEGER;
71
+ const CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS = Object.freeze({
72
+ main: PENDING_DEDUP_ACTIVATION_HEIGHT,
73
+ "xenon-2020-01-15": PENDING_DEDUP_ACTIVATION_HEIGHT,
74
+ xenon: PENDING_DEDUP_ACTIVATION_HEIGHT,
75
+ beta: 1,
76
+ "test-chain": 1,
77
+ test: 1
78
+ });
79
+ const EXPLICIT_HEIGHT_PATHS = [
80
+ "height",
81
+ "blockHeight",
82
+ "sequence",
83
+ "ledgerSequence",
84
+ "expectedLedgerSequence",
85
+ "extra.height",
86
+ "extra.blockHeight",
87
+ "extra.sequence",
88
+ "tx.height"
89
+ ];
90
+ const readExplicitHeight = (context) => {
91
+ for (const path of EXPLICIT_HEIGHT_PATHS) {
92
+ const val = get(context, path);
93
+ if (val !== void 0 && val !== null && val !== "") {
94
+ const parsed = Number(val);
95
+ if (Number.isFinite(parsed) && parsed >= 0) return Math.floor(parsed);
96
+ }
97
+ }
98
+ };
99
+ const resolveDedupActivationHeight = (context) => {
100
+ const rawChainId = get(context, "config.chainId") ?? get(context, "tx.chainId") ?? get(context, "chainId");
101
+ const chainId = typeof rawChainId === "string" ? rawChainId.trim() : "";
102
+ const registryHeight = chainId && Object.prototype.hasOwnProperty.call(CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, chainId) ? CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS[chainId] : void 0;
103
+ if (typeof registryHeight === "number" && Number.isFinite(registryHeight) && registryHeight > 1 && registryHeight < PENDING_DEDUP_ACTIVATION_HEIGHT) return Math.floor(registryHeight);
104
+ const rawConfig = get(context, "config.transaction.unsignedTxHashDedupHeight", 0);
105
+ const parsedConfig = Number(rawConfig);
106
+ if (Number.isFinite(parsedConfig) && parsedConfig > 0) return Math.floor(parsedConfig);
107
+ if (typeof registryHeight === "number" && Number.isFinite(registryHeight) && registryHeight > 0) return Math.floor(registryHeight);
108
+ if (chainId) return PENDING_DEDUP_ACTIVATION_HEIGHT;
109
+ return 0;
110
+ };
111
+ const isExplicitlyPreActivation = (context) => {
112
+ const explicitFlag = get(context, "dedupActive");
113
+ if (typeof explicitFlag === "boolean") return !explicitFlag;
114
+ const dedupHeight = resolveDedupActivationHeight(context);
115
+ if (!Number.isFinite(dedupHeight) || dedupHeight <= 0) return false;
116
+ const explicitHeight = readExplicitHeight(context);
117
+ if (explicitHeight !== void 0) return explicitHeight < dedupHeight;
118
+ if (dedupHeight >= PENDING_DEDUP_ACTIVATION_HEIGHT) return true;
119
+ return false;
120
+ };
121
+ const parseNonceBn = (rawNonce) => {
122
+ if (typeof rawNonce === "number") {
123
+ if (!Number.isFinite(rawNonce) || !Number.isInteger(rawNonce)) return null;
124
+ return new BN(String(rawNonce), 10);
125
+ }
126
+ if (typeof rawNonce === "string") {
127
+ const trimmed = rawNonce.trim();
128
+ if (!/^-?\d+$/.test(trimmed)) return null;
129
+ return new BN(trimmed, 10);
130
+ }
131
+ return null;
132
+ };
59
133
  const update = (state, attrs, context) => {
60
- if (attrs.nonce && new BN(attrs.nonce).eq(new BN(state.nonce))) throw new CustomError("INVALID_NONCE", "nonce must be greater in newer transactions");
134
+ const hasExplicitNonce = attrs.nonce !== void 0 && attrs.nonce !== null;
135
+ let shouldUpdateNonce = false;
136
+ let normalizedNonce;
137
+ if (hasExplicitNonce) if (isExplicitlyPreActivation(context)) {
138
+ if (attrs.nonce && Number(attrs.nonce) <= Number(state.nonce)) throw new CustomError("INVALID_NONCE", "nonce must be greater in newer transactions");
139
+ shouldUpdateNonce = true;
140
+ normalizedNonce = attrs.nonce;
141
+ } else {
142
+ const incomingNonceBn = parseNonceBn(attrs.nonce);
143
+ const currentNonceBn = parseNonceBn(state.nonce) ?? new BN(0);
144
+ if (!incomingNonceBn || incomingNonceBn.lte(currentNonceBn) || incomingNonceBn.lte(new BN(0))) throw new CustomError("INVALID_NONCE", "nonce must be greater in newer transactions");
145
+ shouldUpdateNonce = true;
146
+ normalizedNonce = incomingNonceBn.lte(new BN(Number.MAX_SAFE_INTEGER)) ? incomingNonceBn.toNumber() : incomingNonceBn.toString(10);
147
+ }
61
148
  if (attrs.pk && isFromPublicKey(state.address, attrs.pk) === false) delete attrs.pk;
149
+ const pickFields = shouldUpdateNonce ? [
150
+ "moniker",
151
+ "data",
152
+ "migratedTo",
153
+ "nonce",
154
+ "tokens",
155
+ "pk"
156
+ ] : [
157
+ "moniker",
158
+ "data",
159
+ "migratedTo",
160
+ "tokens",
161
+ "pk"
162
+ ];
62
163
  return validate({
63
164
  ...state,
64
- ...pick(attrs, [
65
- "moniker",
66
- "data",
67
- "migratedTo",
68
- "nonce",
69
- "tokens",
70
- "pk"
71
- ]),
165
+ ...pick(attrs, pickFields),
166
+ ...shouldUpdateNonce && normalizedNonce !== void 0 ? { nonce: normalizedNonce } : {},
72
167
  migratedTo: uniq(flatten(attrs.migratedTo ? [attrs.migratedTo].concat(state.migratedTo) : state.migratedTo)),
73
168
  context: update$1(state.context, context)
74
169
  });
@@ -88,4 +183,4 @@ const isMigrated = (state) => (state.migratedTo || []).length > 0;
88
183
  const ensureChecksumAddress = (address) => isEthereumDid(address) ? toChecksumAddress(address) : address;
89
184
 
90
185
  //#endregion
91
- export { account_exports, create, ensureChecksumAddress, isMigrated, schema, update, updateOrCreate, validate };
186
+ export { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, account_exports, create, ensureChecksumAddress, isExplicitlyPreActivation, isMigrated, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, schema, update, updateOrCreate, validate };
@@ -1,8 +1,8 @@
1
1
  import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
2
2
  import { create as create$1, update as update$1 } from "../contexts/state.mjs";
3
3
  import { CustomError } from "@ocap/util/lib/error";
4
- import pick from "lodash/pick.js";
5
4
  import get from "lodash/get.js";
5
+ import pick from "lodash/pick.js";
6
6
  import isEqual from "lodash/isEqual.js";
7
7
 
8
8
  //#region src/states/chain.ts
@@ -34,6 +34,8 @@ const update = (state, updates) => {
34
34
  ];
35
35
  if (!process.env.CHAIN_TOKEN_ADDRESS) immutableAttrs.push("token.name", "token.description", "token.unit", "token.totalSupply", "token.initialSupply", "token.foreignToken");
36
36
  for (const attr of immutableAttrs) if (get(updates, attr) && !isEqual(get(updates, attr), get(state, attr))) throw new CustomError("FORBIDDEN", `Cannot update ${attr} field on chain state`);
37
+ const prevDedupHeight = Number(get(state, "transaction.unsignedTxHashDedupHeight", 0));
38
+ if (prevDedupHeight > 0 && updates.transaction !== void 0 && Number(get(updates, "transaction.unsignedTxHashDedupHeight", 0)) !== prevDedupHeight) throw new CustomError("FORBIDDEN", "Cannot update transaction.unsignedTxHashDedupHeight field on chain state");
37
39
  const context = { txTime: (/* @__PURE__ */ new Date()).toISOString() };
38
40
  return {
39
41
  ...state,
@@ -1,11 +1,23 @@
1
1
  import { StateContextInput } from "../contexts/state.mjs";
2
+ import { v } from "@arcblock/validator";
2
3
  import { IDelegateState } from "@ocap/types";
3
4
 
4
5
  //#region src/states/delegation.d.ts
5
6
  declare namespace delegation_d_exports {
6
- export { create, update };
7
+ export { create, schema, update, validate };
7
8
  }
9
+ declare const schema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
10
+ readonly address: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
11
+ readonly to: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
12
+ readonly from: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
13
+ readonly ops: v.OptionalSchema<v.NullableSchema<v.UnionSchema<[v.ArraySchema<v.CustomSchema<unknown, "Invalid delegation op">, undefined>, v.CustomSchema<unknown, "Invalid delegation ops map">], undefined>, undefined>, undefined>;
14
+ readonly deny: v.ArraySchema<v.StringSchema<undefined>, undefined>;
15
+ readonly validUntil: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
16
+ readonly context: v.OptionalSchema<v.AnySchema, undefined>;
17
+ readonly data: v.OptionalSchema<v.NullableSchema<v.AnySchema, undefined>, undefined>;
18
+ }, undefined>, any]>;
19
+ declare const validate: (state: IDelegateState) => IDelegateState;
8
20
  declare const create: (attrs: Partial<IDelegateState>, context: StateContextInput) => IDelegateState;
9
21
  declare const update: (state: IDelegateState, attrs: Partial<IDelegateState>, context: StateContextInput) => IDelegateState;
10
22
  //#endregion
11
- export { create, delegation_d_exports, update };
23
+ export { create, delegation_d_exports, schema, update, validate };
@@ -2,13 +2,33 @@ import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
2
2
  import { create as create$1, update as update$1 } from "../contexts/state.mjs";
3
3
  import { nullify } from "../util.mjs";
4
4
  import { toAddress } from "@ocap/util";
5
+ import { CustomError } from "@ocap/util/lib/error";
5
6
  import pick from "lodash/pick.js";
7
+ import { v, vStripUnknown, vValidate } from "@arcblock/validator";
6
8
 
7
9
  //#region src/states/delegation.ts
8
10
  var delegation_exports = /* @__PURE__ */ __exportAll({
9
11
  create: () => create,
10
- update: () => update
12
+ schema: () => schema,
13
+ update: () => update,
14
+ validate: () => validate
11
15
  });
16
+ const schema = v.pipe(v.object({
17
+ address: v.pipe(v.string(), v.minLength(1)),
18
+ to: v.optional(v.string()),
19
+ from: v.optional(v.string()),
20
+ ops: v.optional(v.nullable(v.union([v.array(v.custom((x) => Boolean(x && typeof x === "object" && !Array.isArray(x)), "Invalid delegation op")), v.custom((x) => Boolean(x && typeof x === "object" && !Array.isArray(x)), "Invalid delegation ops map")]))),
21
+ deny: v.array(v.string()),
22
+ validUntil: v.pipe(v.number(), v.integer(), v.minValue(0)),
23
+ context: v.optional(v.any()),
24
+ data: v.optional(v.nullable(v.any()))
25
+ }), vStripUnknown());
26
+ const validate = (state) => {
27
+ const { value, error } = vValidate(schema, state);
28
+ if (error) throw new CustomError("INVALID_DELEGATION", `Invalid delegation: ${error.details.map((x) => x.message).join(", ")}`);
29
+ if (!value.data) value.data = null;
30
+ return value;
31
+ };
12
32
  const create = (attrs, context) => {
13
33
  const delegation = {
14
34
  context: create$1(context),
@@ -23,11 +43,11 @@ const create = (attrs, context) => {
23
43
  ])
24
44
  };
25
45
  if (!delegation.data) delegation.data = null;
26
- if (!delegation.deny) delegation.deny = [];
27
- if (!delegation.validUntil) delegation.validUntil = 0;
28
- delegation.address = toAddress(delegation.address);
29
- delegation.ops = nullify(delegation.ops);
30
- return delegation;
46
+ if (delegation.deny === void 0 || delegation.deny === null) delegation.deny = [];
47
+ if (delegation.validUntil === void 0 || delegation.validUntil === null) delegation.validUntil = 0;
48
+ if (typeof delegation.address === "string") delegation.address = toAddress(delegation.address);
49
+ if (Array.isArray(delegation.ops)) delegation.ops = nullify(delegation.ops);
50
+ return validate(delegation);
31
51
  };
32
52
  const update = (state, attrs, context) => {
33
53
  const delegation = {
@@ -41,11 +61,13 @@ const update = (state, attrs, context) => {
41
61
  context: update$1(state.context, context)
42
62
  };
43
63
  if (!delegation.data) delegation.data = null;
44
- if (!state.to && attrs.to) delegation.to = toAddress(attrs.to);
45
- if (!state.from && attrs.from) delegation.from = toAddress(attrs.from);
46
- delegation.ops = nullify(delegation.ops);
47
- return delegation;
64
+ if (delegation.deny === void 0 || delegation.deny === null) delegation.deny = [];
65
+ if (delegation.validUntil === void 0 || delegation.validUntil === null) delegation.validUntil = 0;
66
+ if (!state.to && attrs.to !== void 0) delegation.to = typeof attrs.to === "string" ? toAddress(attrs.to) : attrs.to;
67
+ if (!state.from && attrs.from !== void 0) delegation.from = typeof attrs.from === "string" ? toAddress(attrs.from) : attrs.from;
68
+ if (Array.isArray(delegation.ops)) delegation.ops = nullify(delegation.ops);
69
+ return validate(delegation);
48
70
  };
49
71
 
50
72
  //#endregion
51
- export { create, delegation_exports, update };
73
+ export { create, delegation_exports, schema, update, validate };
@@ -36,9 +36,9 @@ declare const schema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
36
36
  readonly minStakeAmount: any;
37
37
  readonly maxStakeAmount: any;
38
38
  readonly minSignerCount: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>]>;
39
- readonly maxSignerCount: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 8, undefined>]>;
39
+ readonly maxSignerCount: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 16, undefined>]>;
40
40
  readonly minBlockSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>]>;
41
- readonly maxBlockSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 32, undefined>]>;
41
+ readonly maxBlockSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 256, undefined>]>;
42
42
  readonly minBlockInterval: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 3600, undefined>]>;
43
43
  readonly minBlockConfirmation: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 100, undefined>]>;
44
44
  readonly minDepositAmount: any;
@@ -58,9 +58,9 @@ const schema = v.pipe(v.object({
58
58
  minStakeAmount: vBN(vBNPositive()),
59
59
  maxStakeAmount: vBN(),
60
60
  minSignerCount: v.pipe(v.number(), v.integer()),
61
- maxSignerCount: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(8)),
61
+ maxSignerCount: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(16)),
62
62
  minBlockSize: v.pipe(v.number(), v.integer(), v.minValue(1)),
63
- maxBlockSize: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(32)),
63
+ maxBlockSize: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(256)),
64
64
  minBlockInterval: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(3600)),
65
65
  minBlockConfirmation: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(100)),
66
66
  minDepositAmount: vBN(vBNPositive()),
@@ -226,7 +226,7 @@ interface CreateContext extends TxContext {
226
226
  txExtra?: unknown;
227
227
  };
228
228
  }
229
- declare const create: (context: CreateContext, code?: string, verifyReceipts?: boolean) => TxResult;
229
+ declare const create: (context: CreateContext, code?: string, verifyReceipts?: boolean | "strict") => TxResult;
230
230
  declare const attachPaidTxGas: (tx: TxResult) => TxResult;
231
231
  declare const update: (state: TxResult, updates: Partial<TxResult>) => TxResult;
232
232
  //#endregion
package/esm/states/tx.mjs CHANGED
@@ -2,8 +2,8 @@ import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
2
2
  import { toTypeInfo, types } from "@arcblock/did";
3
3
  import { BN, fromTokenToUnit, isBN, toBase64 } from "@ocap/util";
4
4
  import { CustomError } from "@ocap/util/lib/error";
5
- import pick from "lodash/pick.js";
6
5
  import get from "lodash/get.js";
6
+ import pick from "lodash/pick.js";
7
7
  import { createMessage, decodeAny, formatMessage, fromTypeUrl, toTypeUrl } from "@ocap/message";
8
8
  import { getListField } from "@ocap/util/lib/get-list-field";
9
9
  import { getRelatedAddresses } from "@ocap/util/lib/get-related-addr";
@@ -860,9 +860,10 @@ const create = (context, code = "OK", verifyReceipts = true) => {
860
860
  verifyTxReceipts(result.receipts, typeUrl, context);
861
861
  result.receiptsVerified = true;
862
862
  } catch (err) {
863
+ result.receiptsVerified = false;
864
+ if (verifyReceipts === "strict") throw err;
863
865
  console.warn(JSON.stringify(result, null));
864
866
  console.error("verifyTxReceipts error", err);
865
- result.receiptsVerified = false;
866
867
  }
867
868
  attachPaidTxGas(result);
868
869
  return result;
package/lib/index.cjs CHANGED
@@ -45,6 +45,9 @@ const tables = [
45
45
 
46
46
  //#endregion
47
47
  exports.Blacklist = require_states_blacklist.default;
48
+ exports.CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS = require_states_account.CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS;
49
+ exports.EXPLICIT_HEIGHT_PATHS = require_states_account.EXPLICIT_HEIGHT_PATHS;
50
+ exports.PENDING_DEDUP_ACTIVATION_HEIGHT = require_states_account.PENDING_DEDUP_ACTIVATION_HEIGHT;
48
51
  Object.defineProperty(exports, 'account', {
49
52
  enumerable: true,
50
53
  get: function () {
@@ -82,6 +85,10 @@ Object.defineProperty(exports, 'factory', {
82
85
  }
83
86
  });
84
87
  exports.indexes = indexes;
88
+ exports.isExplicitlyPreActivation = require_states_account.isExplicitlyPreActivation;
89
+ exports.parseNonceBn = require_states_account.parseNonceBn;
90
+ exports.readExplicitHeight = require_states_account.readExplicitHeight;
91
+ exports.resolveDedupActivationHeight = require_states_account.resolveDedupActivationHeight;
85
92
  Object.defineProperty(exports, 'rollup', {
86
93
  enumerable: true,
87
94
  get: function () {
package/lib/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { account_d_exports } from "./states/account.cjs";
1
+ import { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, account_d_exports, isExplicitlyPreActivation, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight } from "./states/account.cjs";
2
2
  import { asset_d_exports } from "./states/asset.cjs";
3
3
  import { ChainState, chain_d_exports } from "./states/chain.cjs";
4
4
  import { delegation_d_exports } from "./states/delegation.cjs";
@@ -17,4 +17,4 @@ import Blacklist, { BlacklistOptions, TrustedAccountConfig } from "./states/blac
17
17
  declare const indexes: readonly ["account", "asset", "delegation", "tx", "factory", "token", "stake", "rollup", "rollupBlock", "rollupValidator", "tokenDistribution", "tokenFactory"];
18
18
  declare const tables: readonly ["chain", "account", "asset", "delegation", "tx", "factory", "token", "stake", "rollup", "rollupBlock", "evidence", "balance", "tokenFactory"];
19
19
  //#endregion
20
- export { Blacklist, type BlacklistOptions, type ChainState, type ItxJson, type Receipt, type ReceiptChange, type TokenInput, type TrustedAccountConfig, type TxContext, type TxData, type TxInput, type TxOutput, type TxResult, account_d_exports as account, asset_d_exports as asset, chain_d_exports as chain, delegation_d_exports as delegation, evidence_d_exports as evidence, factory_d_exports as factory, indexes, rollup_d_exports as rollup, rollup_block_d_exports as rollupBlock, stake_d_exports as stake, tables, token_d_exports as token, token_factory_d_exports as tokenFactory, tx_d_exports as tx };
20
+ export { Blacklist, type BlacklistOptions, CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, type ChainState, EXPLICIT_HEIGHT_PATHS, type ItxJson, PENDING_DEDUP_ACTIVATION_HEIGHT, type Receipt, type ReceiptChange, type TokenInput, type TrustedAccountConfig, type TxContext, type TxData, type TxInput, type TxOutput, type TxResult, account_d_exports as account, asset_d_exports as asset, chain_d_exports as chain, delegation_d_exports as delegation, evidence_d_exports as evidence, factory_d_exports as factory, indexes, isExplicitlyPreActivation, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, rollup_d_exports as rollup, rollup_block_d_exports as rollupBlock, stake_d_exports as stake, tables, token_d_exports as token, token_factory_d_exports as tokenFactory, tx_d_exports as tx };
@@ -6,6 +6,8 @@ let _ocap_util = require("@ocap/util");
6
6
  let _ocap_util_lib_error = require("@ocap/util/lib/error");
7
7
  let lodash_flatten = require("lodash/flatten");
8
8
  lodash_flatten = require_rolldown_runtime.__toESM(lodash_flatten);
9
+ let lodash_get = require("lodash/get");
10
+ lodash_get = require_rolldown_runtime.__toESM(lodash_get);
9
11
  let lodash_pick = require("lodash/pick");
10
12
  lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
11
13
  let lodash_uniq = require("lodash/uniq");
@@ -14,9 +16,16 @@ let _arcblock_validator = require("@arcblock/validator");
14
16
 
15
17
  //#region src/states/account.ts
16
18
  var account_exports = /* @__PURE__ */ require_rolldown_runtime.__exportAll({
19
+ CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS: () => CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS,
20
+ EXPLICIT_HEIGHT_PATHS: () => EXPLICIT_HEIGHT_PATHS,
21
+ PENDING_DEDUP_ACTIVATION_HEIGHT: () => PENDING_DEDUP_ACTIVATION_HEIGHT,
17
22
  create: () => create,
18
23
  ensureChecksumAddress: () => ensureChecksumAddress,
24
+ isExplicitlyPreActivation: () => isExplicitlyPreActivation,
19
25
  isMigrated: () => isMigrated,
26
+ parseNonceBn: () => parseNonceBn,
27
+ readExplicitHeight: () => readExplicitHeight,
28
+ resolveDedupActivationHeight: () => resolveDedupActivationHeight,
20
29
  schema: () => schema,
21
30
  update: () => update,
22
31
  updateOrCreate: () => updateOrCreate,
@@ -27,7 +36,7 @@ const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
27
36
  pk: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.string())),
28
37
  issuer: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.union([_arcblock_validator.v.literal(""), (0, _arcblock_validator.vDID)({ prefix: true })]))),
29
38
  moniker: _arcblock_validator.v.optional(_arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.check((s) => s === "" || /^[a-zA-Z0-9][-a-zA-Z0-9_]{2,128}$/.test(s), "Invalid moniker format"))),
30
- nonce: _arcblock_validator.v.optional(_arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.minValue(0)), 0),
39
+ nonce: _arcblock_validator.v.optional(_arcblock_validator.v.union([_arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.minValue(0)), _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.check((s) => /^\d+$/.test(s), "Invalid nonce format"))]), 0),
31
40
  tokens: _arcblock_validator.v.optional((0, _arcblock_validator.vFilteredRecord)((0, _arcblock_validator.vDID)({
32
41
  prefix: true,
33
42
  role: "ROLE_TOKEN"
@@ -38,13 +47,16 @@ const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
38
47
  data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
39
48
  }), (0, _arcblock_validator.vStripUnknown)());
40
49
  const create = (attrs, context) => {
50
+ const normalizedAttrs = { ...attrs };
51
+ const targetAddress = (0, _ocap_util.toAddress)(ensureChecksumAddress(normalizedAttrs.address || ""));
52
+ if (normalizedAttrs.pk && (0, _arcblock_did.isFromPublicKey)(targetAddress, normalizedAttrs.pk) === false) delete normalizedAttrs.pk;
41
53
  const account = {
42
54
  nonce: 0,
43
- migratedTo: (0, lodash_uniq.default)([].concat(attrs.migratedTo || [])),
55
+ migratedTo: (0, lodash_uniq.default)([].concat(normalizedAttrs.migratedTo || [])),
44
56
  migratedFrom: [],
45
57
  tokens: {},
46
58
  context: require_contexts_state.create(context),
47
- ...(0, lodash_pick.default)(attrs, [
59
+ ...(0, lodash_pick.default)(normalizedAttrs, [
48
60
  "address",
49
61
  "pk",
50
62
  "issuer",
@@ -55,23 +67,107 @@ const create = (attrs, context) => {
55
67
  "tokens"
56
68
  ])
57
69
  };
58
- account.address = (0, _ocap_util.toAddress)(ensureChecksumAddress(account.address));
70
+ account.address = targetAddress;
59
71
  if (!account.moniker) account.moniker = [account.address.slice(0, 6), account.address.slice(-5)].join("-");
60
72
  return validate(account);
61
73
  };
74
+ const PENDING_DEDUP_ACTIVATION_HEIGHT = Number.MAX_SAFE_INTEGER;
75
+ const CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS = Object.freeze({
76
+ main: PENDING_DEDUP_ACTIVATION_HEIGHT,
77
+ "xenon-2020-01-15": PENDING_DEDUP_ACTIVATION_HEIGHT,
78
+ xenon: PENDING_DEDUP_ACTIVATION_HEIGHT,
79
+ beta: 1,
80
+ "test-chain": 1,
81
+ test: 1
82
+ });
83
+ const EXPLICIT_HEIGHT_PATHS = [
84
+ "height",
85
+ "blockHeight",
86
+ "sequence",
87
+ "ledgerSequence",
88
+ "expectedLedgerSequence",
89
+ "extra.height",
90
+ "extra.blockHeight",
91
+ "extra.sequence",
92
+ "tx.height"
93
+ ];
94
+ const readExplicitHeight = (context) => {
95
+ for (const path of EXPLICIT_HEIGHT_PATHS) {
96
+ const val = (0, lodash_get.default)(context, path);
97
+ if (val !== void 0 && val !== null && val !== "") {
98
+ const parsed = Number(val);
99
+ if (Number.isFinite(parsed) && parsed >= 0) return Math.floor(parsed);
100
+ }
101
+ }
102
+ };
103
+ const resolveDedupActivationHeight = (context) => {
104
+ const rawChainId = (0, lodash_get.default)(context, "config.chainId") ?? (0, lodash_get.default)(context, "tx.chainId") ?? (0, lodash_get.default)(context, "chainId");
105
+ const chainId = typeof rawChainId === "string" ? rawChainId.trim() : "";
106
+ const registryHeight = chainId && Object.prototype.hasOwnProperty.call(CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, chainId) ? CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS[chainId] : void 0;
107
+ if (typeof registryHeight === "number" && Number.isFinite(registryHeight) && registryHeight > 1 && registryHeight < PENDING_DEDUP_ACTIVATION_HEIGHT) return Math.floor(registryHeight);
108
+ const rawConfig = (0, lodash_get.default)(context, "config.transaction.unsignedTxHashDedupHeight", 0);
109
+ const parsedConfig = Number(rawConfig);
110
+ if (Number.isFinite(parsedConfig) && parsedConfig > 0) return Math.floor(parsedConfig);
111
+ if (typeof registryHeight === "number" && Number.isFinite(registryHeight) && registryHeight > 0) return Math.floor(registryHeight);
112
+ if (chainId) return PENDING_DEDUP_ACTIVATION_HEIGHT;
113
+ return 0;
114
+ };
115
+ const isExplicitlyPreActivation = (context) => {
116
+ const explicitFlag = (0, lodash_get.default)(context, "dedupActive");
117
+ if (typeof explicitFlag === "boolean") return !explicitFlag;
118
+ const dedupHeight = resolveDedupActivationHeight(context);
119
+ if (!Number.isFinite(dedupHeight) || dedupHeight <= 0) return false;
120
+ const explicitHeight = readExplicitHeight(context);
121
+ if (explicitHeight !== void 0) return explicitHeight < dedupHeight;
122
+ if (dedupHeight >= PENDING_DEDUP_ACTIVATION_HEIGHT) return true;
123
+ return false;
124
+ };
125
+ const parseNonceBn = (rawNonce) => {
126
+ if (typeof rawNonce === "number") {
127
+ if (!Number.isFinite(rawNonce) || !Number.isInteger(rawNonce)) return null;
128
+ return new _ocap_util.BN(String(rawNonce), 10);
129
+ }
130
+ if (typeof rawNonce === "string") {
131
+ const trimmed = rawNonce.trim();
132
+ if (!/^-?\d+$/.test(trimmed)) return null;
133
+ return new _ocap_util.BN(trimmed, 10);
134
+ }
135
+ return null;
136
+ };
62
137
  const update = (state, attrs, context) => {
63
- if (attrs.nonce && new _ocap_util.BN(attrs.nonce).eq(new _ocap_util.BN(state.nonce))) throw new _ocap_util_lib_error.CustomError("INVALID_NONCE", "nonce must be greater in newer transactions");
138
+ const hasExplicitNonce = attrs.nonce !== void 0 && attrs.nonce !== null;
139
+ let shouldUpdateNonce = false;
140
+ let normalizedNonce;
141
+ if (hasExplicitNonce) if (isExplicitlyPreActivation(context)) {
142
+ if (attrs.nonce && Number(attrs.nonce) <= Number(state.nonce)) throw new _ocap_util_lib_error.CustomError("INVALID_NONCE", "nonce must be greater in newer transactions");
143
+ shouldUpdateNonce = true;
144
+ normalizedNonce = attrs.nonce;
145
+ } else {
146
+ const incomingNonceBn = parseNonceBn(attrs.nonce);
147
+ const currentNonceBn = parseNonceBn(state.nonce) ?? new _ocap_util.BN(0);
148
+ if (!incomingNonceBn || incomingNonceBn.lte(currentNonceBn) || incomingNonceBn.lte(new _ocap_util.BN(0))) throw new _ocap_util_lib_error.CustomError("INVALID_NONCE", "nonce must be greater in newer transactions");
149
+ shouldUpdateNonce = true;
150
+ normalizedNonce = incomingNonceBn.lte(new _ocap_util.BN(Number.MAX_SAFE_INTEGER)) ? incomingNonceBn.toNumber() : incomingNonceBn.toString(10);
151
+ }
64
152
  if (attrs.pk && (0, _arcblock_did.isFromPublicKey)(state.address, attrs.pk) === false) delete attrs.pk;
153
+ const pickFields = shouldUpdateNonce ? [
154
+ "moniker",
155
+ "data",
156
+ "migratedTo",
157
+ "nonce",
158
+ "tokens",
159
+ "pk"
160
+ ] : [
161
+ "moniker",
162
+ "data",
163
+ "migratedTo",
164
+ "tokens",
165
+ "pk"
166
+ ];
65
167
  return validate({
66
168
  ...state,
67
- ...(0, lodash_pick.default)(attrs, [
68
- "moniker",
69
- "data",
70
- "migratedTo",
71
- "nonce",
72
- "tokens",
73
- "pk"
74
- ]),
169
+ ...(0, lodash_pick.default)(attrs, pickFields),
170
+ ...shouldUpdateNonce && normalizedNonce !== void 0 ? { nonce: normalizedNonce } : {},
75
171
  migratedTo: (0, lodash_uniq.default)((0, lodash_flatten.default)(attrs.migratedTo ? [attrs.migratedTo].concat(state.migratedTo) : state.migratedTo)),
76
172
  context: require_contexts_state.update(state.context, context)
77
173
  });
@@ -91,6 +187,9 @@ const isMigrated = (state) => (state.migratedTo || []).length > 0;
91
187
  const ensureChecksumAddress = (address) => (0, _arcblock_did_lib_type.isEthereumDid)(address) ? (0, _arcblock_did_lib_type.toChecksumAddress)(address) : address;
92
188
 
93
189
  //#endregion
190
+ exports.CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS = CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS;
191
+ exports.EXPLICIT_HEIGHT_PATHS = EXPLICIT_HEIGHT_PATHS;
192
+ exports.PENDING_DEDUP_ACTIVATION_HEIGHT = PENDING_DEDUP_ACTIVATION_HEIGHT;
94
193
  Object.defineProperty(exports, 'account_exports', {
95
194
  enumerable: true,
96
195
  get: function () {
@@ -99,7 +198,11 @@ Object.defineProperty(exports, 'account_exports', {
99
198
  });
100
199
  exports.create = create;
101
200
  exports.ensureChecksumAddress = ensureChecksumAddress;
201
+ exports.isExplicitlyPreActivation = isExplicitlyPreActivation;
102
202
  exports.isMigrated = isMigrated;
203
+ exports.parseNonceBn = parseNonceBn;
204
+ exports.readExplicitHeight = readExplicitHeight;
205
+ exports.resolveDedupActivationHeight = resolveDedupActivationHeight;
103
206
  exports.schema = schema;
104
207
  exports.update = update;
105
208
  exports.updateOrCreate = updateOrCreate;
@@ -1,17 +1,18 @@
1
1
  import { StateContextInput } from "../contexts/state.cjs";
2
2
  import { IAccountState } from "@ocap/types";
3
+ import { BN } from "@ocap/util";
3
4
  import { v } from "@arcblock/validator";
4
5
 
5
6
  //#region src/states/account.d.ts
6
7
  declare namespace account_d_exports {
7
- export { create, ensureChecksumAddress, isMigrated, schema, update, updateOrCreate, validate };
8
+ export { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, create, ensureChecksumAddress, isExplicitlyPreActivation, isMigrated, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, schema, update, updateOrCreate, validate };
8
9
  }
9
10
  declare const schema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
10
11
  readonly address: any;
11
12
  readonly pk: v.OptionalSchema<v.NullableSchema<v.StringSchema<undefined>, undefined>, undefined>;
12
13
  readonly issuer: v.OptionalSchema<v.NullableSchema<v.UnionSchema<[v.LiteralSchema<"", undefined>, any], undefined>, undefined>, undefined>;
13
14
  readonly moniker: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.CheckAction<string, "Invalid moniker format">]>, undefined>;
14
- readonly nonce: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>]>, 0>;
15
+ readonly nonce: v.OptionalSchema<v.UnionSchema<[v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>]>, v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.CheckAction<string, "Invalid nonce format">]>], undefined>, 0>;
15
16
  readonly tokens: v.OptionalSchema<any, {}>;
16
17
  readonly migratedTo: v.OptionalSchema<v.ArraySchema<any, undefined>, readonly []>;
17
18
  readonly migratedFrom: v.OptionalSchema<v.ArraySchema<any, undefined>, readonly []>;
@@ -24,6 +25,13 @@ declare const schema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
24
25
  readonly data: v.OptionalSchema<v.NullableSchema<v.AnySchema, undefined>, undefined>;
25
26
  }, undefined>, any]>;
26
27
  declare const create: (attrs: Partial<IAccountState>, context: StateContextInput) => IAccountState;
28
+ declare const PENDING_DEDUP_ACTIVATION_HEIGHT: number;
29
+ declare const CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS: Readonly<Record<string, number>>;
30
+ declare const EXPLICIT_HEIGHT_PATHS: readonly ["height", "blockHeight", "sequence", "ledgerSequence", "expectedLedgerSequence", "extra.height", "extra.blockHeight", "extra.sequence", "tx.height"];
31
+ declare const readExplicitHeight: (context: unknown) => number | undefined;
32
+ declare const resolveDedupActivationHeight: (context: unknown) => number;
33
+ declare const isExplicitlyPreActivation: (context: unknown) => boolean;
34
+ declare const parseNonceBn: (rawNonce: unknown) => InstanceType<typeof BN> | null;
27
35
  declare const update: (state: IAccountState, attrs: Partial<IAccountState>, context: StateContextInput) => IAccountState;
28
36
  declare const updateOrCreate: (state: IAccountState | null, attrs: Partial<IAccountState>, context: StateContextInput) => IAccountState;
29
37
  declare const validate: (state: IAccountState) => IAccountState;
@@ -32,4 +40,4 @@ declare const isMigrated: (state: {
32
40
  }) => boolean;
33
41
  declare const ensureChecksumAddress: (address: string) => string;
34
42
  //#endregion
35
- export { account_d_exports, create, ensureChecksumAddress, isMigrated, schema, update, updateOrCreate, validate };
43
+ export { CHAIN_UNSIGNED_TX_DEDUP_HEIGHTS, EXPLICIT_HEIGHT_PATHS, PENDING_DEDUP_ACTIVATION_HEIGHT, account_d_exports, create, ensureChecksumAddress, isExplicitlyPreActivation, isMigrated, parseNonceBn, readExplicitHeight, resolveDedupActivationHeight, schema, update, updateOrCreate, validate };
@@ -1,10 +1,10 @@
1
1
  const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
2
  const require_contexts_state = require('../contexts/state.cjs');
3
3
  let _ocap_util_lib_error = require("@ocap/util/lib/error");
4
- let lodash_pick = require("lodash/pick");
5
- lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
6
4
  let lodash_get = require("lodash/get");
7
5
  lodash_get = require_rolldown_runtime.__toESM(lodash_get);
6
+ let lodash_pick = require("lodash/pick");
7
+ lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
8
8
  let lodash_isEqual = require("lodash/isEqual");
9
9
  lodash_isEqual = require_rolldown_runtime.__toESM(lodash_isEqual);
10
10
 
@@ -37,6 +37,8 @@ const update = (state, updates) => {
37
37
  ];
38
38
  if (!process.env.CHAIN_TOKEN_ADDRESS) immutableAttrs.push("token.name", "token.description", "token.unit", "token.totalSupply", "token.initialSupply", "token.foreignToken");
39
39
  for (const attr of immutableAttrs) if ((0, lodash_get.default)(updates, attr) && !(0, lodash_isEqual.default)((0, lodash_get.default)(updates, attr), (0, lodash_get.default)(state, attr))) throw new _ocap_util_lib_error.CustomError("FORBIDDEN", `Cannot update ${attr} field on chain state`);
40
+ const prevDedupHeight = Number((0, lodash_get.default)(state, "transaction.unsignedTxHashDedupHeight", 0));
41
+ if (prevDedupHeight > 0 && updates.transaction !== void 0 && Number((0, lodash_get.default)(updates, "transaction.unsignedTxHashDedupHeight", 0)) !== prevDedupHeight) throw new _ocap_util_lib_error.CustomError("FORBIDDEN", "Cannot update transaction.unsignedTxHashDedupHeight field on chain state");
40
42
  const context = { txTime: (/* @__PURE__ */ new Date()).toISOString() };
41
43
  return {
42
44
  ...state,
@@ -2,14 +2,34 @@ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
2
  const require_contexts_state = require('../contexts/state.cjs');
3
3
  const require_util = require('../util.cjs');
4
4
  let _ocap_util = require("@ocap/util");
5
+ let _ocap_util_lib_error = require("@ocap/util/lib/error");
5
6
  let lodash_pick = require("lodash/pick");
6
7
  lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
8
+ let _arcblock_validator = require("@arcblock/validator");
7
9
 
8
10
  //#region src/states/delegation.ts
9
11
  var delegation_exports = /* @__PURE__ */ require_rolldown_runtime.__exportAll({
10
12
  create: () => create,
11
- update: () => update
13
+ schema: () => schema,
14
+ update: () => update,
15
+ validate: () => validate
12
16
  });
17
+ const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
18
+ address: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(1)),
19
+ to: _arcblock_validator.v.optional(_arcblock_validator.v.string()),
20
+ from: _arcblock_validator.v.optional(_arcblock_validator.v.string()),
21
+ ops: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.union([_arcblock_validator.v.array(_arcblock_validator.v.custom((x) => Boolean(x && typeof x === "object" && !Array.isArray(x)), "Invalid delegation op")), _arcblock_validator.v.custom((x) => Boolean(x && typeof x === "object" && !Array.isArray(x)), "Invalid delegation ops map")]))),
22
+ deny: _arcblock_validator.v.array(_arcblock_validator.v.string()),
23
+ validUntil: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(0)),
24
+ context: _arcblock_validator.v.optional(_arcblock_validator.v.any()),
25
+ data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
26
+ }), (0, _arcblock_validator.vStripUnknown)());
27
+ const validate = (state) => {
28
+ const { value, error } = (0, _arcblock_validator.vValidate)(schema, state);
29
+ if (error) throw new _ocap_util_lib_error.CustomError("INVALID_DELEGATION", `Invalid delegation: ${error.details.map((x) => x.message).join(", ")}`);
30
+ if (!value.data) value.data = null;
31
+ return value;
32
+ };
13
33
  const create = (attrs, context) => {
14
34
  const delegation = {
15
35
  context: require_contexts_state.create(context),
@@ -24,11 +44,11 @@ const create = (attrs, context) => {
24
44
  ])
25
45
  };
26
46
  if (!delegation.data) delegation.data = null;
27
- if (!delegation.deny) delegation.deny = [];
28
- if (!delegation.validUntil) delegation.validUntil = 0;
29
- delegation.address = (0, _ocap_util.toAddress)(delegation.address);
30
- delegation.ops = require_util.nullify(delegation.ops);
31
- return delegation;
47
+ if (delegation.deny === void 0 || delegation.deny === null) delegation.deny = [];
48
+ if (delegation.validUntil === void 0 || delegation.validUntil === null) delegation.validUntil = 0;
49
+ if (typeof delegation.address === "string") delegation.address = (0, _ocap_util.toAddress)(delegation.address);
50
+ if (Array.isArray(delegation.ops)) delegation.ops = require_util.nullify(delegation.ops);
51
+ return validate(delegation);
32
52
  };
33
53
  const update = (state, attrs, context) => {
34
54
  const delegation = {
@@ -42,10 +62,12 @@ const update = (state, attrs, context) => {
42
62
  context: require_contexts_state.update(state.context, context)
43
63
  };
44
64
  if (!delegation.data) delegation.data = null;
45
- if (!state.to && attrs.to) delegation.to = (0, _ocap_util.toAddress)(attrs.to);
46
- if (!state.from && attrs.from) delegation.from = (0, _ocap_util.toAddress)(attrs.from);
47
- delegation.ops = require_util.nullify(delegation.ops);
48
- return delegation;
65
+ if (delegation.deny === void 0 || delegation.deny === null) delegation.deny = [];
66
+ if (delegation.validUntil === void 0 || delegation.validUntil === null) delegation.validUntil = 0;
67
+ if (!state.to && attrs.to !== void 0) delegation.to = typeof attrs.to === "string" ? (0, _ocap_util.toAddress)(attrs.to) : attrs.to;
68
+ if (!state.from && attrs.from !== void 0) delegation.from = typeof attrs.from === "string" ? (0, _ocap_util.toAddress)(attrs.from) : attrs.from;
69
+ if (Array.isArray(delegation.ops)) delegation.ops = require_util.nullify(delegation.ops);
70
+ return validate(delegation);
49
71
  };
50
72
 
51
73
  //#endregion
@@ -56,4 +78,6 @@ Object.defineProperty(exports, 'delegation_exports', {
56
78
  return delegation_exports;
57
79
  }
58
80
  });
59
- exports.update = update;
81
+ exports.schema = schema;
82
+ exports.update = update;
83
+ exports.validate = validate;
@@ -1,11 +1,23 @@
1
1
  import { StateContextInput } from "../contexts/state.cjs";
2
2
  import { IDelegateState } from "@ocap/types";
3
+ import { v } from "@arcblock/validator";
3
4
 
4
5
  //#region src/states/delegation.d.ts
5
6
  declare namespace delegation_d_exports {
6
- export { create, update };
7
+ export { create, schema, update, validate };
7
8
  }
9
+ declare const schema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
10
+ readonly address: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
11
+ readonly to: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
12
+ readonly from: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
13
+ readonly ops: v.OptionalSchema<v.NullableSchema<v.UnionSchema<[v.ArraySchema<v.CustomSchema<unknown, "Invalid delegation op">, undefined>, v.CustomSchema<unknown, "Invalid delegation ops map">], undefined>, undefined>, undefined>;
14
+ readonly deny: v.ArraySchema<v.StringSchema<undefined>, undefined>;
15
+ readonly validUntil: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
16
+ readonly context: v.OptionalSchema<v.AnySchema, undefined>;
17
+ readonly data: v.OptionalSchema<v.NullableSchema<v.AnySchema, undefined>, undefined>;
18
+ }, undefined>, any]>;
19
+ declare const validate: (state: IDelegateState) => IDelegateState;
8
20
  declare const create: (attrs: Partial<IDelegateState>, context: StateContextInput) => IDelegateState;
9
21
  declare const update: (state: IDelegateState, attrs: Partial<IDelegateState>, context: StateContextInput) => IDelegateState;
10
22
  //#endregion
11
- export { create, delegation_d_exports, update };
23
+ export { create, delegation_d_exports, schema, update, validate };
@@ -59,9 +59,9 @@ const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
59
59
  minStakeAmount: (0, _arcblock_validator.vBN)((0, _arcblock_validator.vBNPositive)()),
60
60
  maxStakeAmount: (0, _arcblock_validator.vBN)(),
61
61
  minSignerCount: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer()),
62
- maxSignerCount: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(1), _arcblock_validator.v.maxValue(8)),
62
+ maxSignerCount: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(1), _arcblock_validator.v.maxValue(16)),
63
63
  minBlockSize: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(1)),
64
- maxBlockSize: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(1), _arcblock_validator.v.maxValue(32)),
64
+ maxBlockSize: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(1), _arcblock_validator.v.maxValue(256)),
65
65
  minBlockInterval: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(1), _arcblock_validator.v.maxValue(3600)),
66
66
  minBlockConfirmation: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(1), _arcblock_validator.v.maxValue(100)),
67
67
  minDepositAmount: (0, _arcblock_validator.vBN)((0, _arcblock_validator.vBNPositive)()),
@@ -36,9 +36,9 @@ declare const schema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
36
36
  readonly minStakeAmount: any;
37
37
  readonly maxStakeAmount: any;
38
38
  readonly minSignerCount: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>]>;
39
- readonly maxSignerCount: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 8, undefined>]>;
39
+ readonly maxSignerCount: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 16, undefined>]>;
40
40
  readonly minBlockSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>]>;
41
- readonly maxBlockSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 32, undefined>]>;
41
+ readonly maxBlockSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 256, undefined>]>;
42
42
  readonly minBlockInterval: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 3600, undefined>]>;
43
43
  readonly minBlockConfirmation: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, 100, undefined>]>;
44
44
  readonly minDepositAmount: any;
package/lib/states/tx.cjs CHANGED
@@ -2,10 +2,10 @@ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
2
  let _arcblock_did = require("@arcblock/did");
3
3
  let _ocap_util = require("@ocap/util");
4
4
  let _ocap_util_lib_error = require("@ocap/util/lib/error");
5
- let lodash_pick = require("lodash/pick");
6
- lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
7
5
  let lodash_get = require("lodash/get");
8
6
  lodash_get = require_rolldown_runtime.__toESM(lodash_get);
7
+ let lodash_pick = require("lodash/pick");
8
+ lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
9
9
  let _ocap_message = require("@ocap/message");
10
10
  let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
11
11
  let _ocap_util_lib_get_related_addr = require("@ocap/util/lib/get-related-addr");
@@ -866,9 +866,10 @@ const create = (context, code = "OK", verifyReceipts = true) => {
866
866
  verifyTxReceipts(result.receipts, typeUrl, context);
867
867
  result.receiptsVerified = true;
868
868
  } catch (err) {
869
+ result.receiptsVerified = false;
870
+ if (verifyReceipts === "strict") throw err;
869
871
  console.warn(JSON.stringify(result, null));
870
872
  console.error("verifyTxReceipts error", err);
871
- result.receiptsVerified = false;
872
873
  }
873
874
  attachPaidTxGas(result);
874
875
  return result;
@@ -226,7 +226,7 @@ interface CreateContext extends TxContext {
226
226
  txExtra?: unknown;
227
227
  };
228
228
  }
229
- declare const create: (context: CreateContext, code?: string, verifyReceipts?: boolean) => TxResult;
229
+ declare const create: (context: CreateContext, code?: string, verifyReceipts?: boolean | "strict") => TxResult;
230
230
  declare const attachPaidTxGas: (tx: TxResult) => TxResult;
231
231
  declare const update: (state: TxResult, updates: Partial<TxResult>) => TxResult;
232
232
  //#endregion
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.30.24",
6
+ "version": "1.30.25",
7
7
  "description": "State management utilities for OCAP blockchain",
8
8
  "type": "module",
9
9
  "main": "./lib/index.cjs",
@@ -40,14 +40,14 @@
40
40
  "clean": "rm -rf lib esm"
41
41
  },
42
42
  "dependencies": {
43
- "@arcblock/did": "1.30.24",
44
- "@arcblock/validator": "1.30.24",
45
- "@ocap/contract": "1.30.24",
46
- "@ocap/mcrypto": "1.30.24",
47
- "@ocap/message": "1.30.24",
48
- "@ocap/types": "1.30.24",
49
- "@ocap/util": "1.30.24",
50
- "@ocap/wallet": "1.30.24",
43
+ "@arcblock/did": "1.30.25",
44
+ "@arcblock/validator": "1.30.25",
45
+ "@ocap/contract": "1.30.25",
46
+ "@ocap/mcrypto": "1.30.25",
47
+ "@ocap/message": "1.30.25",
48
+ "@ocap/types": "1.30.25",
49
+ "@ocap/util": "1.30.25",
50
+ "@ocap/wallet": "1.30.25",
51
51
  "bloom-filters": "^1.3.9",
52
52
  "lodash": "^4.17.23"
53
53
  },