@ocap/config 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/default.mjs CHANGED
@@ -2,8 +2,8 @@
2
2
  const defaultConfig = {
3
3
  transaction: {
4
4
  maxAssetSize: 65536,
5
- maxListSize: 8,
6
- maxMultisig: 8,
5
+ maxListSize: 64,
6
+ maxMultisig: 32,
7
7
  maxTxSize: {
8
8
  default: 65535,
9
9
  "fg:t:account_migrate": 1691,
@@ -12,7 +12,7 @@ const defaultConfig = {
12
12
  "fg:t:exchange_v2": 1691,
13
13
  "fg:t:transfer": 664,
14
14
  "fg:t:transfer_v2": 664,
15
- "fg:t:transfer_v3": 4096,
15
+ "fg:t:transfer_v3": 16384,
16
16
  "fg:t:update_asset": 65881,
17
17
  "fg:t:consume_asset": 1691,
18
18
  "fg:t:acquire_asset_v2": 4096,
@@ -34,7 +34,7 @@ const defaultConfig = {
34
34
  "fg:t:join_rollup": 1691,
35
35
  "fg:t:leave_rollup": 1691,
36
36
  "fg:t:migrate_rollup": 1691,
37
- "fg:t:create_rollup_block": 8192,
37
+ "fg:t:create_rollup_block": 32768,
38
38
  "fg:t:claim_block_reward": 1691,
39
39
  "fg:t:mint_token": 4096,
40
40
  "fg:t:burn_token": 4096,
package/esm/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
- import { IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl } from "./types.mjs";
1
+ import { ChainMode, IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl } from "./types.mjs";
2
2
 
3
3
  //#region src/index.d.ts
4
4
  declare const validate: (config: Partial<IChainConfig>) => IChainConfig;
5
5
  declare const create: (props: IChainConfigInput) => IChainConfig;
6
6
  declare const genTokenItx: (config: IChainConfig) => ITokenItx;
7
7
  //#endregion
8
- export { IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl, create, genTokenItx, validate };
8
+ export { ChainMode, IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl, create, genTokenItx, validate };
package/esm/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import default_default from "./default.mjs";
2
- import schema_default from "./schema.mjs";
3
- import { isValid, toTypeInfoStr } from "@arcblock/did";
2
+ import schema_default, { decodePublicKeyCandidates } from "./schema.mjs";
3
+ import { isFromPublicKey, isValid, toTypeInfoStr } from "@arcblock/did";
4
4
  import { toTokenAddress } from "@arcblock/did-util";
5
5
  import { vValidate } from "@arcblock/validator";
6
6
  import { fromTokenToUnit } from "@ocap/util";
@@ -12,16 +12,27 @@ import pick from "lodash/pick.js";
12
12
  const validate = (config) => {
13
13
  const { value, error } = vValidate(schema_default, config);
14
14
  if (error) throw new Error(`Invalid config: ${error.details.map((x) => x.message).join(", ")}`);
15
- const { vaults, transaction, accounts } = value;
16
- if (flatten(Object.values(vaults)).some((a) => !accounts.find((x) => x.address === a))) throw new Error("Invalid config: vault address does not exist in accounts");
15
+ const { mode, vaults, transaction, accounts, moderator } = value;
16
+ if (mode !== "ledger" && !vaults.slashedStake) throw new Error("Invalid config: vaults.slashedStake must be a valid DID");
17
+ if (flatten(Object.values(vaults)).filter(Boolean).some((a) => !accounts.find((x) => x.address === a))) throw new Error("Invalid config: vault address does not exist in accounts");
17
18
  if (transaction.txGas.price > 0 && (!vaults.txGas || !vaults.txGas.length)) throw new Error("Invalid config: vaults.txGas can not be empty when gas enabled");
18
19
  if (Object.keys(transaction.txFee).some((x) => transaction.txFee[x] > 0) && !vaults.txFee) throw new Error("Invalid config: vaults.txFee can not be empty when there are paid transactions");
19
20
  if (vaults.txFee && vaults.txGas && vaults.txGas.includes(vaults.txFee)) throw new Error("Invalid config: vaults.txFee can not contain same address with vaults.txGas");
20
- if (!vaults.slashedStake) throw new Error("Invalid config: vaults.slashedStake must be a valid DID");
21
+ if (!decodePublicKeyCandidates(moderator.pk).some((pk) => isFromPublicKey(moderator.address, pk))) throw new Error(`Invalid config: moderator public key does not match address ${moderator.address}`);
22
+ const gasVaultSet = new Set(vaults.txGas || []);
23
+ for (const account of accounts) {
24
+ const accountPkCandidates = decodePublicKeyCandidates(account.pk);
25
+ if (accountPkCandidates.length === 0 || !gasVaultSet.has(account.address) && !accountPkCandidates.some((pk) => isFromPublicKey(account.address, pk))) throw new Error(`Invalid config: account public key does not match address ${account.address}`);
26
+ }
21
27
  return value;
22
28
  };
23
29
  const create = (props) => {
24
- return validate(merge({}, default_default, props));
30
+ const baseDefaults = merge({}, default_default);
31
+ if (props.mode === "ledger") {
32
+ baseDefaults.transaction.txGas.price = 0;
33
+ baseDefaults.transaction.txFee = { default: 0 };
34
+ }
35
+ return validate(merge({}, baseDefaults, props));
25
36
  };
26
37
  const genTokenItx = (config) => {
27
38
  const { token, moderator } = config;
package/esm/schema.d.mts CHANGED
@@ -1,20 +1,24 @@
1
1
  import { v } from "@arcblock/validator";
2
2
 
3
3
  //#region src/schema.d.ts
4
+ declare const decodePublicKeyCandidates: (pk: string) => Uint8Array[];
5
+ declare const decodePublicKey: (pk: string) => Uint8Array | null;
4
6
  declare const configSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
7
+ readonly mode: v.OptionalSchema<v.UnionSchema<[v.LiteralSchema<"chain", undefined>, v.LiteralSchema<"ledger", undefined>], undefined>, undefined>;
5
8
  readonly chainId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
6
9
  readonly version: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
7
10
  readonly transaction: v.OptionalSchema<v.ObjectSchema<{
8
- readonly maxAssetSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
9
- readonly maxListSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
10
- readonly maxMultisig: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
11
- readonly maxTxSize: v.RecordSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, undefined>]>, v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be > 0">]>, undefined>;
11
+ readonly maxAssetSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 1 and 16777216">]>;
12
+ readonly maxListSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 1 and 10000">]>;
13
+ readonly maxMultisig: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 1 and 1024">]>;
14
+ readonly maxTxSize: v.RecordSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, undefined>]>, v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 1 and 16777216">]>, undefined>;
12
15
  readonly delegate: v.ObjectSchema<{
13
16
  readonly deltaInterval: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>, undefined>;
14
17
  readonly typeUrls: v.OptionalSchema<v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>;
15
18
  }, undefined>;
16
19
  readonly supportedTxs: v.SchemaWithPipe<readonly [v.ArraySchema<v.StringSchema<undefined>, undefined>, v.MinLengthAction<string[], 1, undefined>]>;
17
20
  readonly multiSignV2Txs: v.SchemaWithPipe<readonly [v.ArraySchema<v.StringSchema<undefined>, undefined>, v.MinLengthAction<string[], 1, undefined>]>;
21
+ readonly unsignedTxHashDedupHeight: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>, undefined>;
18
22
  readonly txFee: v.RecordSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, undefined>]>, v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>]>, undefined>;
19
23
  readonly txGas: v.OptionalSchema<v.ObjectSchema<{
20
24
  readonly price: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
@@ -33,18 +37,18 @@ declare const configSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
33
37
  }, undefined>, any>;
34
38
  readonly moderator: v.ObjectSchema<{
35
39
  address: any;
36
- pk: v.StringSchema<undefined>;
40
+ pk: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>, v.CheckAction<string, "Must be a valid public key">]>;
37
41
  balance: v.OptionalSchema<v.NumberSchema<undefined>, 0>;
38
42
  moniker: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
39
43
  }, undefined>;
40
44
  readonly accounts: v.ArraySchema<v.ObjectSchema<{
41
45
  address: any;
42
- pk: v.StringSchema<undefined>;
46
+ pk: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>, v.CheckAction<string, "Must be a valid public key">]>;
43
47
  balance: v.OptionalSchema<v.NumberSchema<undefined>, 0>;
44
48
  moniker: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
45
49
  }, undefined>, undefined>;
46
50
  readonly vaults: v.OptionalSchema<v.ObjectSchema<{
47
- readonly slashedStake: any;
51
+ readonly slashedStake: v.OptionalSchema<any, undefined>;
48
52
  readonly txFee: v.OptionalSchema<any, undefined>;
49
53
  readonly txFees: v.OptionalSchema<v.ArraySchema<any, undefined>, undefined>;
50
54
  readonly txGas: v.OptionalSchema<v.ArraySchema<any, undefined>, undefined>;
@@ -54,7 +58,7 @@ declare const configSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
54
58
  readonly description: v.StringSchema<undefined>;
55
59
  readonly symbol: v.StringSchema<undefined>;
56
60
  readonly unit: v.StringSchema<undefined>;
57
- readonly decimal: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
61
+ readonly decimal: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 0 and 36">]>;
58
62
  readonly icon: v.StringSchema<undefined>;
59
63
  readonly initialSupply: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
60
64
  readonly totalSupply: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
@@ -72,4 +76,4 @@ declare const configSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
72
76
  readonly reservedSymbols: v.OptionalSchema<v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>;
73
77
  }, undefined>, any]>;
74
78
  //#endregion
75
- export { configSchema as default };
79
+ export { decodePublicKey, decodePublicKeyCandidates, configSchema as default };
package/esm/schema.mjs CHANGED
@@ -1,7 +1,36 @@
1
1
  import default_default from "./default.mjs";
2
2
  import { v, vDID, vStripUnknown } from "@arcblock/validator";
3
+ import { fromBase58, fromBase64, isBase58btc, isHexStrict, toUint8Array } from "@ocap/util";
3
4
 
4
5
  //#region src/schema.ts
6
+ const MAX_ASSET_OR_TX_SIZE = 16 * 1024 * 1024;
7
+ const MAX_LIST_SIZE = 1e4;
8
+ const MAX_MULTISIG_SIZE = 1024;
9
+ const MAX_TOKEN_DECIMAL = 36;
10
+ const VALID_PK_BYTE_LENGTHS = [
11
+ 32,
12
+ 33,
13
+ 64,
14
+ 65
15
+ ];
16
+ const decodePublicKeyCandidates = (pk) => {
17
+ if (typeof pk !== "string" || !pk.trim()) return [];
18
+ const candidates = [];
19
+ if (isHexStrict(pk)) try {
20
+ const bytes = toUint8Array(pk);
21
+ if (VALID_PK_BYTE_LENGTHS.includes(bytes.byteLength)) candidates.push(bytes);
22
+ } catch {}
23
+ if (isBase58btc(pk)) try {
24
+ const bytes = fromBase58(pk);
25
+ if (VALID_PK_BYTE_LENGTHS.includes(bytes.byteLength)) candidates.push(new Uint8Array(bytes));
26
+ } catch {}
27
+ if (/^[A-Za-z0-9+/=_-]+$/.test(pk)) try {
28
+ const bytes = fromBase64(pk);
29
+ if (VALID_PK_BYTE_LENGTHS.includes(bytes.byteLength)) candidates.push(new Uint8Array(bytes));
30
+ } catch {}
31
+ return candidates;
32
+ };
33
+ const decodePublicKey = (pk) => decodePublicKeyCandidates(pk)[0] ?? null;
5
34
  const isHttpsUrl = (s) => {
6
35
  try {
7
36
  return new URL(s).protocol === "https:";
@@ -12,24 +41,26 @@ const isHttpsUrl = (s) => {
12
41
  const txKeyPattern = v.pipe(v.string(), v.regex(/^(default|fg:t:[_a-z0-9]+)$/));
13
42
  const accountSchemaEntries = {
14
43
  address: vDID({ prefix: true }),
15
- pk: v.string(),
44
+ pk: v.pipe(v.string(), v.minLength(1), v.check((s) => decodePublicKey(s) !== null, "Must be a valid public key")),
16
45
  balance: v.optional(v.number(), 0),
17
46
  moniker: v.optional(v.string())
18
47
  };
19
48
  const configSchema = v.pipe(v.object({
49
+ mode: v.optional(v.union([v.literal("chain"), v.literal("ledger")])),
20
50
  chainId: v.pipe(v.string(), v.minLength(1)),
21
51
  version: v.pipe(v.string(), v.minLength(1)),
22
52
  transaction: v.optional(v.object({
23
- maxAssetSize: v.pipe(v.number(), v.check((n) => n > 0, "Must be positive")),
24
- maxListSize: v.pipe(v.number(), v.check((n) => n > 0, "Must be positive")),
25
- maxMultisig: v.pipe(v.number(), v.check((n) => n > 0, "Must be positive")),
26
- maxTxSize: v.record(txKeyPattern, v.pipe(v.number(), v.integer(), v.check((n) => n > 0, "Must be > 0"))),
53
+ maxAssetSize: v.pipe(v.number(), v.integer(), v.check((n) => n > 0 && n <= MAX_ASSET_OR_TX_SIZE, "Must be between 1 and 16777216")),
54
+ maxListSize: v.pipe(v.number(), v.integer(), v.check((n) => n > 0 && n <= MAX_LIST_SIZE, "Must be between 1 and 10000")),
55
+ maxMultisig: v.pipe(v.number(), v.integer(), v.check((n) => n > 0 && n <= MAX_MULTISIG_SIZE, "Must be between 1 and 1024")),
56
+ maxTxSize: v.record(txKeyPattern, v.pipe(v.number(), v.integer(), v.check((n) => n > 0 && n <= MAX_ASSET_OR_TX_SIZE, "Must be between 1 and 16777216"))),
27
57
  delegate: v.object({
28
58
  deltaInterval: v.optional(v.pipe(v.number(), v.check((n) => n > 0, "Must be positive"))),
29
59
  typeUrls: v.optional(v.array(v.string()))
30
60
  }),
31
61
  supportedTxs: v.pipe(v.array(v.string()), v.minLength(1)),
32
62
  multiSignV2Txs: v.pipe(v.array(v.string()), v.minLength(1)),
63
+ unsignedTxHashDedupHeight: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0))),
33
64
  txFee: v.record(txKeyPattern, v.pipe(v.number(), v.minValue(0))),
34
65
  txGas: v.optional(v.object({
35
66
  price: v.pipe(v.number(), v.integer(), v.minValue(0)),
@@ -49,7 +80,7 @@ const configSchema = v.pipe(v.object({
49
80
  moderator: v.object(accountSchemaEntries),
50
81
  accounts: v.array(v.object(accountSchemaEntries)),
51
82
  vaults: v.optional(v.object({
52
- slashedStake: vDID({ prefix: true }),
83
+ slashedStake: v.optional(vDID({ prefix: true })),
53
84
  txFee: v.optional(vDID({ prefix: true })),
54
85
  txFees: v.optional(v.array(vDID({ prefix: true }))),
55
86
  txGas: v.optional(v.array(vDID({ prefix: true })))
@@ -59,7 +90,7 @@ const configSchema = v.pipe(v.object({
59
90
  description: v.string(),
60
91
  symbol: v.string(),
61
92
  unit: v.string(),
62
- decimal: v.pipe(v.number(), v.check((n) => n > 0, "Must be positive")),
93
+ decimal: v.pipe(v.number(), v.integer(), v.check((n) => n >= 0 && n <= MAX_TOKEN_DECIMAL, "Must be between 0 and 36")),
63
94
  icon: v.string(),
64
95
  initialSupply: v.pipe(v.number(), v.check((n) => n > 0, "Must be positive")),
65
96
  totalSupply: v.pipe(v.number(), v.check((n) => n > 0, "Must be positive")),
@@ -79,4 +110,4 @@ const configSchema = v.pipe(v.object({
79
110
  var schema_default = configSchema;
80
111
 
81
112
  //#endregion
82
- export { schema_default as default };
113
+ export { decodePublicKey, decodePublicKeyCandidates, schema_default as default };
package/esm/types.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl } from "@ocap/types";
2
- export { type IAccountConfig, type IChainConfig, type IChainConfigInput, type IDefaultConfig, type ITokenConfig, type ITokenItx, type ITransactionConfig, type ITxGasConfig, type ITxStakeConfig, type IVaultsConfig, type TDelegateConfig, type TForeignToken, type TxTypeMap, type TxTypeUrl };
1
+ import { ChainMode, IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl } from "@ocap/types";
2
+ export { type ChainMode, type IAccountConfig, type IChainConfig, type IChainConfigInput, type IDefaultConfig, type ITokenConfig, type ITokenItx, type ITransactionConfig, type ITxGasConfig, type ITxStakeConfig, type IVaultsConfig, type TDelegateConfig, type TForeignToken, type TxTypeMap, type TxTypeUrl };
package/lib/default.cjs CHANGED
@@ -4,8 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
  const defaultConfig = {
5
5
  transaction: {
6
6
  maxAssetSize: 65536,
7
- maxListSize: 8,
8
- maxMultisig: 8,
7
+ maxListSize: 64,
8
+ maxMultisig: 32,
9
9
  maxTxSize: {
10
10
  default: 65535,
11
11
  "fg:t:account_migrate": 1691,
@@ -14,7 +14,7 @@ const defaultConfig = {
14
14
  "fg:t:exchange_v2": 1691,
15
15
  "fg:t:transfer": 664,
16
16
  "fg:t:transfer_v2": 664,
17
- "fg:t:transfer_v3": 4096,
17
+ "fg:t:transfer_v3": 16384,
18
18
  "fg:t:update_asset": 65881,
19
19
  "fg:t:consume_asset": 1691,
20
20
  "fg:t:acquire_asset_v2": 4096,
@@ -36,7 +36,7 @@ const defaultConfig = {
36
36
  "fg:t:join_rollup": 1691,
37
37
  "fg:t:leave_rollup": 1691,
38
38
  "fg:t:migrate_rollup": 1691,
39
- "fg:t:create_rollup_block": 8192,
39
+ "fg:t:create_rollup_block": 32768,
40
40
  "fg:t:claim_block_reward": 1691,
41
41
  "fg:t:mint_token": 4096,
42
42
  "fg:t:burn_token": 4096,
package/lib/index.cjs CHANGED
@@ -16,16 +16,27 @@ lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
16
16
  const validate = (config) => {
17
17
  const { value, error } = (0, _arcblock_validator.vValidate)(require_schema.default, config);
18
18
  if (error) throw new Error(`Invalid config: ${error.details.map((x) => x.message).join(", ")}`);
19
- const { vaults, transaction, accounts } = value;
20
- if ((0, lodash_flatten.default)(Object.values(vaults)).some((a) => !accounts.find((x) => x.address === a))) throw new Error("Invalid config: vault address does not exist in accounts");
19
+ const { mode, vaults, transaction, accounts, moderator } = value;
20
+ if (mode !== "ledger" && !vaults.slashedStake) throw new Error("Invalid config: vaults.slashedStake must be a valid DID");
21
+ if ((0, lodash_flatten.default)(Object.values(vaults)).filter(Boolean).some((a) => !accounts.find((x) => x.address === a))) throw new Error("Invalid config: vault address does not exist in accounts");
21
22
  if (transaction.txGas.price > 0 && (!vaults.txGas || !vaults.txGas.length)) throw new Error("Invalid config: vaults.txGas can not be empty when gas enabled");
22
23
  if (Object.keys(transaction.txFee).some((x) => transaction.txFee[x] > 0) && !vaults.txFee) throw new Error("Invalid config: vaults.txFee can not be empty when there are paid transactions");
23
24
  if (vaults.txFee && vaults.txGas && vaults.txGas.includes(vaults.txFee)) throw new Error("Invalid config: vaults.txFee can not contain same address with vaults.txGas");
24
- if (!vaults.slashedStake) throw new Error("Invalid config: vaults.slashedStake must be a valid DID");
25
+ if (!require_schema.decodePublicKeyCandidates(moderator.pk).some((pk) => (0, _arcblock_did.isFromPublicKey)(moderator.address, pk))) throw new Error(`Invalid config: moderator public key does not match address ${moderator.address}`);
26
+ const gasVaultSet = new Set(vaults.txGas || []);
27
+ for (const account of accounts) {
28
+ const accountPkCandidates = require_schema.decodePublicKeyCandidates(account.pk);
29
+ if (accountPkCandidates.length === 0 || !gasVaultSet.has(account.address) && !accountPkCandidates.some((pk) => (0, _arcblock_did.isFromPublicKey)(account.address, pk))) throw new Error(`Invalid config: account public key does not match address ${account.address}`);
30
+ }
25
31
  return value;
26
32
  };
27
33
  const create = (props) => {
28
- return validate((0, lodash_merge.default)({}, require_default.default, props));
34
+ const baseDefaults = (0, lodash_merge.default)({}, require_default.default);
35
+ if (props.mode === "ledger") {
36
+ baseDefaults.transaction.txGas.price = 0;
37
+ baseDefaults.transaction.txFee = { default: 0 };
38
+ }
39
+ return validate((0, lodash_merge.default)({}, baseDefaults, props));
29
40
  };
30
41
  const genTokenItx = (config) => {
31
42
  const { token, moderator } = config;
package/lib/index.d.cts CHANGED
@@ -1,8 +1,8 @@
1
- import { IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl } from "./types.cjs";
1
+ import { ChainMode, IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl } from "./types.cjs";
2
2
 
3
3
  //#region src/index.d.ts
4
4
  declare const validate: (config: Partial<IChainConfig>) => IChainConfig;
5
5
  declare const create: (props: IChainConfigInput) => IChainConfig;
6
6
  declare const genTokenItx: (config: IChainConfig) => ITokenItx;
7
7
  //#endregion
8
- export { IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl, create, genTokenItx, validate };
8
+ export { ChainMode, IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl, create, genTokenItx, validate };
package/lib/schema.cjs CHANGED
@@ -2,8 +2,37 @@ Object.defineProperty(exports, '__esModule', { value: true });
2
2
  const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
3
3
  const require_default = require('./default.cjs');
4
4
  let _arcblock_validator = require("@arcblock/validator");
5
+ let _ocap_util = require("@ocap/util");
5
6
 
6
7
  //#region src/schema.ts
8
+ const MAX_ASSET_OR_TX_SIZE = 16 * 1024 * 1024;
9
+ const MAX_LIST_SIZE = 1e4;
10
+ const MAX_MULTISIG_SIZE = 1024;
11
+ const MAX_TOKEN_DECIMAL = 36;
12
+ const VALID_PK_BYTE_LENGTHS = [
13
+ 32,
14
+ 33,
15
+ 64,
16
+ 65
17
+ ];
18
+ const decodePublicKeyCandidates = (pk) => {
19
+ if (typeof pk !== "string" || !pk.trim()) return [];
20
+ const candidates = [];
21
+ if ((0, _ocap_util.isHexStrict)(pk)) try {
22
+ const bytes = (0, _ocap_util.toUint8Array)(pk);
23
+ if (VALID_PK_BYTE_LENGTHS.includes(bytes.byteLength)) candidates.push(bytes);
24
+ } catch {}
25
+ if ((0, _ocap_util.isBase58btc)(pk)) try {
26
+ const bytes = (0, _ocap_util.fromBase58)(pk);
27
+ if (VALID_PK_BYTE_LENGTHS.includes(bytes.byteLength)) candidates.push(new Uint8Array(bytes));
28
+ } catch {}
29
+ if (/^[A-Za-z0-9+/=_-]+$/.test(pk)) try {
30
+ const bytes = (0, _ocap_util.fromBase64)(pk);
31
+ if (VALID_PK_BYTE_LENGTHS.includes(bytes.byteLength)) candidates.push(new Uint8Array(bytes));
32
+ } catch {}
33
+ return candidates;
34
+ };
35
+ const decodePublicKey = (pk) => decodePublicKeyCandidates(pk)[0] ?? null;
7
36
  const isHttpsUrl = (s) => {
8
37
  try {
9
38
  return new URL(s).protocol === "https:";
@@ -14,24 +43,26 @@ const isHttpsUrl = (s) => {
14
43
  const txKeyPattern = _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.regex(/^(default|fg:t:[_a-z0-9]+)$/));
15
44
  const accountSchemaEntries = {
16
45
  address: (0, _arcblock_validator.vDID)({ prefix: true }),
17
- pk: _arcblock_validator.v.string(),
46
+ pk: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(1), _arcblock_validator.v.check((s) => decodePublicKey(s) !== null, "Must be a valid public key")),
18
47
  balance: _arcblock_validator.v.optional(_arcblock_validator.v.number(), 0),
19
48
  moniker: _arcblock_validator.v.optional(_arcblock_validator.v.string())
20
49
  };
21
50
  const configSchema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
51
+ mode: _arcblock_validator.v.optional(_arcblock_validator.v.union([_arcblock_validator.v.literal("chain"), _arcblock_validator.v.literal("ledger")])),
22
52
  chainId: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(1)),
23
53
  version: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(1)),
24
54
  transaction: _arcblock_validator.v.optional(_arcblock_validator.v.object({
25
- maxAssetSize: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.check((n) => n > 0, "Must be positive")),
26
- maxListSize: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.check((n) => n > 0, "Must be positive")),
27
- maxMultisig: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.check((n) => n > 0, "Must be positive")),
28
- maxTxSize: _arcblock_validator.v.record(txKeyPattern, _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.check((n) => n > 0, "Must be > 0"))),
55
+ maxAssetSize: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.check((n) => n > 0 && n <= MAX_ASSET_OR_TX_SIZE, "Must be between 1 and 16777216")),
56
+ maxListSize: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.check((n) => n > 0 && n <= MAX_LIST_SIZE, "Must be between 1 and 10000")),
57
+ maxMultisig: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.check((n) => n > 0 && n <= MAX_MULTISIG_SIZE, "Must be between 1 and 1024")),
58
+ maxTxSize: _arcblock_validator.v.record(txKeyPattern, _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.check((n) => n > 0 && n <= MAX_ASSET_OR_TX_SIZE, "Must be between 1 and 16777216"))),
29
59
  delegate: _arcblock_validator.v.object({
30
60
  deltaInterval: _arcblock_validator.v.optional(_arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.check((n) => n > 0, "Must be positive"))),
31
61
  typeUrls: _arcblock_validator.v.optional(_arcblock_validator.v.array(_arcblock_validator.v.string()))
32
62
  }),
33
63
  supportedTxs: _arcblock_validator.v.pipe(_arcblock_validator.v.array(_arcblock_validator.v.string()), _arcblock_validator.v.minLength(1)),
34
64
  multiSignV2Txs: _arcblock_validator.v.pipe(_arcblock_validator.v.array(_arcblock_validator.v.string()), _arcblock_validator.v.minLength(1)),
65
+ unsignedTxHashDedupHeight: _arcblock_validator.v.optional(_arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(0))),
35
66
  txFee: _arcblock_validator.v.record(txKeyPattern, _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.minValue(0))),
36
67
  txGas: _arcblock_validator.v.optional(_arcblock_validator.v.object({
37
68
  price: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(0)),
@@ -51,7 +82,7 @@ const configSchema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
51
82
  moderator: _arcblock_validator.v.object(accountSchemaEntries),
52
83
  accounts: _arcblock_validator.v.array(_arcblock_validator.v.object(accountSchemaEntries)),
53
84
  vaults: _arcblock_validator.v.optional(_arcblock_validator.v.object({
54
- slashedStake: (0, _arcblock_validator.vDID)({ prefix: true }),
85
+ slashedStake: _arcblock_validator.v.optional((0, _arcblock_validator.vDID)({ prefix: true })),
55
86
  txFee: _arcblock_validator.v.optional((0, _arcblock_validator.vDID)({ prefix: true })),
56
87
  txFees: _arcblock_validator.v.optional(_arcblock_validator.v.array((0, _arcblock_validator.vDID)({ prefix: true }))),
57
88
  txGas: _arcblock_validator.v.optional(_arcblock_validator.v.array((0, _arcblock_validator.vDID)({ prefix: true })))
@@ -61,7 +92,7 @@ const configSchema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
61
92
  description: _arcblock_validator.v.string(),
62
93
  symbol: _arcblock_validator.v.string(),
63
94
  unit: _arcblock_validator.v.string(),
64
- decimal: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.check((n) => n > 0, "Must be positive")),
95
+ decimal: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.check((n) => n >= 0 && n <= MAX_TOKEN_DECIMAL, "Must be between 0 and 36")),
65
96
  icon: _arcblock_validator.v.string(),
66
97
  initialSupply: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.check((n) => n > 0, "Must be positive")),
67
98
  totalSupply: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.check((n) => n > 0, "Must be positive")),
@@ -81,4 +112,6 @@ const configSchema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
81
112
  var schema_default = configSchema;
82
113
 
83
114
  //#endregion
115
+ exports.decodePublicKey = decodePublicKey;
116
+ exports.decodePublicKeyCandidates = decodePublicKeyCandidates;
84
117
  exports.default = schema_default;
package/lib/schema.d.cts CHANGED
@@ -1,20 +1,24 @@
1
1
  import { v } from "@arcblock/validator";
2
2
 
3
3
  //#region src/schema.d.ts
4
+ declare const decodePublicKeyCandidates: (pk: string) => Uint8Array[];
5
+ declare const decodePublicKey: (pk: string) => Uint8Array | null;
4
6
  declare const configSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
7
+ readonly mode: v.OptionalSchema<v.UnionSchema<[v.LiteralSchema<"chain", undefined>, v.LiteralSchema<"ledger", undefined>], undefined>, undefined>;
5
8
  readonly chainId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
6
9
  readonly version: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
7
10
  readonly transaction: v.OptionalSchema<v.ObjectSchema<{
8
- readonly maxAssetSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
9
- readonly maxListSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
10
- readonly maxMultisig: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
11
- readonly maxTxSize: v.RecordSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, undefined>]>, v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be > 0">]>, undefined>;
11
+ readonly maxAssetSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 1 and 16777216">]>;
12
+ readonly maxListSize: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 1 and 10000">]>;
13
+ readonly maxMultisig: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 1 and 1024">]>;
14
+ readonly maxTxSize: v.RecordSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, undefined>]>, v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 1 and 16777216">]>, undefined>;
12
15
  readonly delegate: v.ObjectSchema<{
13
16
  readonly deltaInterval: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>, undefined>;
14
17
  readonly typeUrls: v.OptionalSchema<v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>;
15
18
  }, undefined>;
16
19
  readonly supportedTxs: v.SchemaWithPipe<readonly [v.ArraySchema<v.StringSchema<undefined>, undefined>, v.MinLengthAction<string[], 1, undefined>]>;
17
20
  readonly multiSignV2Txs: v.SchemaWithPipe<readonly [v.ArraySchema<v.StringSchema<undefined>, undefined>, v.MinLengthAction<string[], 1, undefined>]>;
21
+ readonly unsignedTxHashDedupHeight: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>, undefined>;
18
22
  readonly txFee: v.RecordSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, undefined>]>, v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.MinValueAction<number, 0, undefined>]>, undefined>;
19
23
  readonly txGas: v.OptionalSchema<v.ObjectSchema<{
20
24
  readonly price: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
@@ -33,18 +37,18 @@ declare const configSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
33
37
  }, undefined>, any>;
34
38
  readonly moderator: v.ObjectSchema<{
35
39
  address: any;
36
- pk: v.StringSchema<undefined>;
40
+ pk: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>, v.CheckAction<string, "Must be a valid public key">]>;
37
41
  balance: v.OptionalSchema<v.NumberSchema<undefined>, 0>;
38
42
  moniker: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
39
43
  }, undefined>;
40
44
  readonly accounts: v.ArraySchema<v.ObjectSchema<{
41
45
  address: any;
42
- pk: v.StringSchema<undefined>;
46
+ pk: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>, v.CheckAction<string, "Must be a valid public key">]>;
43
47
  balance: v.OptionalSchema<v.NumberSchema<undefined>, 0>;
44
48
  moniker: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
45
49
  }, undefined>, undefined>;
46
50
  readonly vaults: v.OptionalSchema<v.ObjectSchema<{
47
- readonly slashedStake: any;
51
+ readonly slashedStake: v.OptionalSchema<any, undefined>;
48
52
  readonly txFee: v.OptionalSchema<any, undefined>;
49
53
  readonly txFees: v.OptionalSchema<v.ArraySchema<any, undefined>, undefined>;
50
54
  readonly txGas: v.OptionalSchema<v.ArraySchema<any, undefined>, undefined>;
@@ -54,7 +58,7 @@ declare const configSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
54
58
  readonly description: v.StringSchema<undefined>;
55
59
  readonly symbol: v.StringSchema<undefined>;
56
60
  readonly unit: v.StringSchema<undefined>;
57
- readonly decimal: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
61
+ readonly decimal: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.IntegerAction<number, undefined>, v.CheckAction<number, "Must be between 0 and 36">]>;
58
62
  readonly icon: v.StringSchema<undefined>;
59
63
  readonly initialSupply: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
60
64
  readonly totalSupply: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.CheckAction<number, "Must be positive">]>;
@@ -72,4 +76,4 @@ declare const configSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
72
76
  readonly reservedSymbols: v.OptionalSchema<v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>;
73
77
  }, undefined>, any]>;
74
78
  //#endregion
75
- export { configSchema as default };
79
+ export { decodePublicKey, decodePublicKeyCandidates, configSchema as default };
package/lib/types.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- import { IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl } from "@ocap/types";
2
- export { type IAccountConfig, type IChainConfig, type IChainConfigInput, type IDefaultConfig, type ITokenConfig, type ITokenItx, type ITransactionConfig, type ITxGasConfig, type ITxStakeConfig, type IVaultsConfig, type TDelegateConfig, type TForeignToken, type TxTypeMap, type TxTypeUrl };
1
+ import { ChainMode, IAccountConfig, IChainConfig, IChainConfigInput, IDefaultConfig, ITokenConfig, ITokenItx, ITransactionConfig, ITxGasConfig, ITxStakeConfig, IVaultsConfig, TDelegateConfig, TForeignToken, TxTypeMap, TxTypeUrl } from "@ocap/types";
2
+ export { type ChainMode, type IAccountConfig, type IChainConfig, type IChainConfigInput, type IDefaultConfig, type ITokenConfig, type ITokenItx, type ITransactionConfig, type ITxGasConfig, type ITxStakeConfig, type IVaultsConfig, type TDelegateConfig, type TForeignToken, type TxTypeMap, type TxTypeUrl };
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": "OCAP config parsing/validation and default",
8
8
  "type": "module",
9
9
  "main": "./lib/index.cjs",
@@ -47,11 +47,11 @@
47
47
  "license": "MIT",
48
48
  "devDependencies": {},
49
49
  "dependencies": {
50
- "@arcblock/did": "1.30.24",
51
- "@arcblock/did-util": "1.30.24",
52
- "@arcblock/validator": "1.30.24",
53
- "@ocap/types": "1.30.24",
54
- "@ocap/util": "1.30.24",
50
+ "@arcblock/did": "1.30.25",
51
+ "@arcblock/did-util": "1.30.25",
52
+ "@arcblock/validator": "1.30.25",
53
+ "@ocap/types": "1.30.25",
54
+ "@ocap/util": "1.30.25",
55
55
  "lodash": "^4.17.23"
56
56
  }
57
57
  }