@ocap/state 1.28.9 → 1.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/esm/_virtual/rolldown_runtime.mjs +18 -0
  2. package/esm/contexts/state.d.mts +15 -0
  3. package/esm/contexts/state.mjs +17 -0
  4. package/esm/index.d.mts +20 -0
  5. package/esm/index.mjs +47 -0
  6. package/esm/states/account.d.mts +18 -0
  7. package/esm/states/account.mjs +91 -0
  8. package/esm/states/asset.d.mts +14 -0
  9. package/esm/states/asset.mjs +80 -0
  10. package/esm/states/blacklist.d.mts +36 -0
  11. package/esm/states/blacklist.mjs +71 -0
  12. package/esm/states/chain.d.mts +30 -0
  13. package/esm/states/chain.mjs +52 -0
  14. package/esm/states/delegation.d.mts +11 -0
  15. package/esm/states/delegation.mjs +42 -0
  16. package/esm/states/evidence.d.mts +12 -0
  17. package/esm/states/evidence.mjs +35 -0
  18. package/esm/states/factory.d.mts +12 -0
  19. package/esm/states/factory.mjs +76 -0
  20. package/esm/states/rollup-block.d.mts +13 -0
  21. package/esm/states/rollup-block.mjs +75 -0
  22. package/esm/states/rollup.d.mts +18 -0
  23. package/esm/states/rollup.mjs +215 -0
  24. package/esm/states/stake.d.mts +13 -0
  25. package/esm/states/stake.mjs +89 -0
  26. package/esm/states/token-factory.d.mts +13 -0
  27. package/esm/states/token-factory.mjs +76 -0
  28. package/esm/states/token.d.mts +14 -0
  29. package/esm/states/token.mjs +109 -0
  30. package/esm/states/tx.d.mts +233 -0
  31. package/esm/states/tx.mjs +867 -0
  32. package/esm/util.d.mts +6 -0
  33. package/esm/util.mjs +18 -0
  34. package/lib/_virtual/rolldown_runtime.cjs +43 -0
  35. package/lib/contexts/state.cjs +19 -0
  36. package/lib/contexts/state.d.cts +15 -0
  37. package/lib/index.cjs +121 -0
  38. package/lib/index.d.cts +20 -0
  39. package/lib/states/account.cjs +106 -0
  40. package/lib/states/account.d.cts +18 -0
  41. package/lib/states/asset.cjs +91 -0
  42. package/lib/states/asset.d.cts +14 -0
  43. package/lib/states/blacklist.cjs +74 -0
  44. package/lib/states/blacklist.d.cts +36 -0
  45. package/lib/states/chain.cjs +62 -0
  46. package/lib/states/chain.d.cts +30 -0
  47. package/lib/states/delegation.cjs +50 -0
  48. package/lib/states/delegation.d.cts +11 -0
  49. package/lib/states/evidence.cjs +44 -0
  50. package/lib/states/evidence.d.cts +12 -0
  51. package/lib/states/factory.cjs +85 -0
  52. package/lib/states/factory.d.cts +12 -0
  53. package/lib/states/rollup-block.cjs +85 -0
  54. package/lib/states/rollup-block.d.cts +13 -0
  55. package/lib/states/rollup.cjs +230 -0
  56. package/lib/states/rollup.d.cts +18 -0
  57. package/lib/states/stake.cjs +99 -0
  58. package/lib/states/stake.d.cts +13 -0
  59. package/lib/states/token-factory.cjs +86 -0
  60. package/lib/states/token-factory.d.cts +13 -0
  61. package/lib/states/token.cjs +121 -0
  62. package/lib/states/token.d.cts +14 -0
  63. package/lib/states/tx.cjs +889 -0
  64. package/lib/states/tx.d.cts +233 -0
  65. package/lib/util.cjs +19 -0
  66. package/lib/util.d.cts +6 -0
  67. package/package.json +46 -14
  68. package/lib/contexts/state.js +0 -19
  69. package/lib/index.js +0 -63
  70. package/lib/states/account.js +0 -95
  71. package/lib/states/asset.js +0 -91
  72. package/lib/states/blacklist.js +0 -103
  73. package/lib/states/chain.js +0 -49
  74. package/lib/states/delegation.js +0 -46
  75. package/lib/states/evidence.js +0 -35
  76. package/lib/states/factory.js +0 -92
  77. package/lib/states/rollup-block.js +0 -84
  78. package/lib/states/rollup.js +0 -297
  79. package/lib/states/stake.js +0 -83
  80. package/lib/states/token-factory.js +0 -74
  81. package/lib/states/token.js +0 -124
  82. package/lib/states/tx.js +0 -896
  83. package/lib/util.js +0 -28
@@ -0,0 +1,18 @@
1
+ //#region rolldown:runtime
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, symbols) => {
4
+ let target = {};
5
+ for (var name in all) {
6
+ __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: true
9
+ });
10
+ }
11
+ if (symbols) {
12
+ __defProp(target, Symbol.toStringTag, { value: "Module" });
13
+ }
14
+ return target;
15
+ };
16
+
17
+ //#endregion
18
+ export { __exportAll };
@@ -0,0 +1,15 @@
1
+ import { IStateContext, IStateContextInput } from "@ocap/types";
2
+
3
+ //#region src/contexts/state.d.ts
4
+ type StateContextInput = IStateContextInput;
5
+ type StateContext = IStateContext;
6
+ declare const create: ({
7
+ txHash,
8
+ txTime
9
+ }: StateContextInput) => StateContext;
10
+ declare const update: (context: StateContext, {
11
+ txHash,
12
+ txTime
13
+ }: StateContextInput) => StateContext;
14
+ //#endregion
15
+ export { StateContext, StateContextInput, create, update };
@@ -0,0 +1,17 @@
1
+ //#region src/contexts/state.ts
2
+ const create = ({ txHash, txTime }) => ({
3
+ genesisTime: txTime || "",
4
+ genesisTx: txHash || "",
5
+ renaissanceTime: txTime || "",
6
+ renaissanceTx: txHash || ""
7
+ });
8
+ const update = (context, { txHash, txTime }) => ({
9
+ ...context,
10
+ genesisTime: context.genesisTime || "2020-01-15T00:00:00.000Z",
11
+ genesisTx: context.genesisTx || "",
12
+ renaissanceTime: txTime || "",
13
+ renaissanceTx: txHash || ""
14
+ });
15
+
16
+ //#endregion
17
+ export { create, update };
@@ -0,0 +1,20 @@
1
+ import { account_d_exports } from "./states/account.mjs";
2
+ import { asset_d_exports } from "./states/asset.mjs";
3
+ import { ChainState, chain_d_exports } from "./states/chain.mjs";
4
+ import { delegation_d_exports } from "./states/delegation.mjs";
5
+ import { evidence_d_exports } from "./states/evidence.mjs";
6
+ import { factory_d_exports } from "./states/factory.mjs";
7
+ import { rollup_d_exports } from "./states/rollup.mjs";
8
+ import { rollup_block_d_exports } from "./states/rollup-block.mjs";
9
+ import { stake_d_exports } from "./states/stake.mjs";
10
+ import { token_d_exports } from "./states/token.mjs";
11
+ import { token_factory_d_exports } from "./states/token-factory.mjs";
12
+ import { ItxJson, Receipt, ReceiptChange, TokenInput, TxContext, TxData, TxInput, TxOutput, TxResult, tx_d_exports } from "./states/tx.mjs";
13
+ import Blacklist, { BlacklistOptions, TrustedAccountConfig } from "./states/blacklist.mjs";
14
+
15
+ //#region src/index.d.ts
16
+
17
+ declare const indexes: readonly ["account", "asset", "delegation", "tx", "factory", "token", "stake", "rollup", "rollupBlock", "rollupValidator", "tokenDistribution", "tokenFactory"];
18
+ declare const tables: readonly ["chain", "account", "asset", "delegation", "tx", "factory", "token", "stake", "rollup", "rollupBlock", "evidence", "balance", "tokenFactory"];
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 };
package/esm/index.mjs ADDED
@@ -0,0 +1,47 @@
1
+ import { account_exports } from "./states/account.mjs";
2
+ import { asset_exports } from "./states/asset.mjs";
3
+ import { chain_exports } from "./states/chain.mjs";
4
+ import { delegation_exports } from "./states/delegation.mjs";
5
+ import { evidence_exports } from "./states/evidence.mjs";
6
+ import { factory_exports } from "./states/factory.mjs";
7
+ import { rollup_exports } from "./states/rollup.mjs";
8
+ import { rollup_block_exports } from "./states/rollup-block.mjs";
9
+ import { stake_exports } from "./states/stake.mjs";
10
+ import { token_exports } from "./states/token.mjs";
11
+ import { token_factory_exports } from "./states/token-factory.mjs";
12
+ import { tx_exports } from "./states/tx.mjs";
13
+ import blacklist_default from "./states/blacklist.mjs";
14
+
15
+ //#region src/index.ts
16
+ const indexes = [
17
+ "account",
18
+ "asset",
19
+ "delegation",
20
+ "tx",
21
+ "factory",
22
+ "token",
23
+ "stake",
24
+ "rollup",
25
+ "rollupBlock",
26
+ "rollupValidator",
27
+ "tokenDistribution",
28
+ "tokenFactory"
29
+ ];
30
+ const tables = [
31
+ "chain",
32
+ "account",
33
+ "asset",
34
+ "delegation",
35
+ "tx",
36
+ "factory",
37
+ "token",
38
+ "stake",
39
+ "rollup",
40
+ "rollupBlock",
41
+ "evidence",
42
+ "balance",
43
+ "tokenFactory"
44
+ ];
45
+
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 };
@@ -0,0 +1,18 @@
1
+ import { StateContextInput } from "../contexts/state.mjs";
2
+ import { IAccountState } from "@ocap/types";
3
+
4
+ //#region src/states/account.d.ts
5
+ declare namespace account_d_exports {
6
+ export { create, ensureChecksumAddress, isMigrated, schema, update, updateOrCreate, validate };
7
+ }
8
+ declare const schema: any;
9
+ declare const create: (attrs: Partial<IAccountState>, context: StateContextInput) => IAccountState;
10
+ declare const update: (state: IAccountState, attrs: Partial<IAccountState>, context: StateContextInput) => IAccountState;
11
+ declare const updateOrCreate: (state: IAccountState | null, attrs: Partial<IAccountState>, context: StateContextInput) => IAccountState;
12
+ declare const validate: (state: IAccountState) => IAccountState;
13
+ declare const isMigrated: (state: {
14
+ migratedTo?: string[];
15
+ }) => boolean;
16
+ declare const ensureChecksumAddress: (address: string) => string;
17
+ //#endregion
18
+ export { account_d_exports, create, ensureChecksumAddress, isMigrated, schema, update, updateOrCreate, validate };
@@ -0,0 +1,91 @@
1
+ import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
2
+ import { create as create$1, update as update$1 } from "../contexts/state.mjs";
3
+ import { isFromPublicKey } from "@arcblock/did";
4
+ import { isEthereumDid, toChecksumAddress } from "@arcblock/did/lib/type";
5
+ import { BN, toAddress, toBase58 } from "@ocap/util";
6
+ import { CustomError } from "@ocap/util/lib/error";
7
+ import flatten from "lodash/flatten.js";
8
+ import pick from "lodash/pick.js";
9
+ import uniq from "lodash/uniq.js";
10
+ import { Joi, schemas } from "@arcblock/validator";
11
+
12
+ //#region src/states/account.ts
13
+ var account_exports = /* @__PURE__ */ __exportAll({
14
+ create: () => create,
15
+ ensureChecksumAddress: () => ensureChecksumAddress,
16
+ isMigrated: () => isMigrated,
17
+ schema: () => schema,
18
+ update: () => update,
19
+ updateOrCreate: () => updateOrCreate,
20
+ validate: () => validate
21
+ });
22
+ const schema = Joi.object({
23
+ address: Joi.DID().prefix().required(),
24
+ pk: Joi.string().allow(null).allow(""),
25
+ issuer: Joi.DID().prefix().allow(null).allow(""),
26
+ moniker: Joi.string().regex(/^[a-zA-Z0-9][-a-zA-Z0-9_]{2,128}$/).allow(""),
27
+ nonce: Joi.number().min(0).default(0),
28
+ tokens: Joi.object().pattern(Joi.DID().prefix().role("ROLE_TOKEN"), Joi.BN().min(0)).default({}),
29
+ migratedTo: Joi.array().items(Joi.DID().prefix()).default([]),
30
+ migratedFrom: Joi.array().items(Joi.DID().prefix()).default([]),
31
+ context: schemas.context,
32
+ data: Joi.any().optional().allow(null)
33
+ }).options({
34
+ stripUnknown: true,
35
+ noDefaults: false
36
+ });
37
+ const create = (attrs, context) => {
38
+ const account = {
39
+ nonce: 0,
40
+ migratedTo: uniq([].concat(attrs.migratedTo || [])),
41
+ migratedFrom: [],
42
+ tokens: {},
43
+ context: create$1(context),
44
+ ...pick(attrs, [
45
+ "address",
46
+ "pk",
47
+ "issuer",
48
+ "moniker",
49
+ "data",
50
+ "nonce",
51
+ "migratedFrom",
52
+ "tokens"
53
+ ])
54
+ };
55
+ account.address = toAddress(ensureChecksumAddress(account.address));
56
+ if (!account.moniker) account.moniker = [account.address.slice(0, 6), account.address.slice(-5)].join("-");
57
+ return validate(account);
58
+ };
59
+ 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");
61
+ if (attrs.pk && isFromPublicKey(state.address, attrs.pk) === false) delete attrs.pk;
62
+ return validate({
63
+ ...state,
64
+ ...pick(attrs, [
65
+ "moniker",
66
+ "data",
67
+ "migratedTo",
68
+ "nonce",
69
+ "tokens",
70
+ "pk"
71
+ ]),
72
+ migratedTo: uniq(flatten(attrs.migratedTo ? [attrs.migratedTo].concat(state.migratedTo) : state.migratedTo)),
73
+ context: update$1(state.context, context)
74
+ });
75
+ };
76
+ const updateOrCreate = (state, attrs, context) => {
77
+ if (state) return update(state, attrs, context);
78
+ return create(attrs, context);
79
+ };
80
+ const validate = (state) => {
81
+ if (state.pk && typeof state.pk !== "string") state.pk = toBase58(state.pk);
82
+ const { value, error } = schema.validate(state);
83
+ if (error) throw new CustomError("INVALID_ACCOUNT", `Invalid account: ${error.details.map((x) => x.message).join(", ")}`);
84
+ if (!value.data) value.data = null;
85
+ return value;
86
+ };
87
+ const isMigrated = (state) => (state.migratedTo || []).length > 0;
88
+ const ensureChecksumAddress = (address) => isEthereumDid(address) ? toChecksumAddress(address) : address;
89
+
90
+ //#endregion
91
+ export { account_exports, create, ensureChecksumAddress, isMigrated, schema, update, updateOrCreate, validate };
@@ -0,0 +1,14 @@
1
+ import { StateContextInput } from "../contexts/state.mjs";
2
+ import { schemas } from "@arcblock/validator";
3
+ import { IAssetState } from "@ocap/types";
4
+
5
+ //#region src/states/asset.d.ts
6
+ declare namespace asset_d_exports {
7
+ export { create, schema, schemas, update, validate };
8
+ }
9
+ declare const schema: any;
10
+ declare const create: (attrs: Partial<IAssetState>, context: StateContextInput) => IAssetState;
11
+ declare const update: (state: IAssetState, attrs: Partial<IAssetState>, context: StateContextInput) => IAssetState;
12
+ declare const validate: (state: IAssetState) => IAssetState;
13
+ //#endregion
14
+ export { asset_d_exports, create, schema, schemas, update, validate };
@@ -0,0 +1,80 @@
1
+ import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
2
+ import { create as create$1, update as update$1 } from "../contexts/state.mjs";
3
+ import { toAddress } from "@ocap/util";
4
+ import { CustomError } from "@ocap/util/lib/error";
5
+ import pick from "lodash/pick.js";
6
+ import { Joi, schemas } from "@arcblock/validator";
7
+
8
+ //#region src/states/asset.ts
9
+ var asset_exports = /* @__PURE__ */ __exportAll({
10
+ create: () => create,
11
+ schema: () => schema,
12
+ schemas: () => schemas,
13
+ update: () => update,
14
+ validate: () => validate
15
+ });
16
+ const stateSchema = Joi.object({
17
+ ...schemas.assetProps,
18
+ owner: Joi.DID().prefix().required(),
19
+ consumedTime: Joi.date().iso().raw().allow(""),
20
+ context: schemas.context
21
+ }).options({
22
+ stripUnknown: true,
23
+ noDefaults: false
24
+ });
25
+ const schema = schemas.assetSchema;
26
+ const create = (attrs, context) => {
27
+ const asset = {
28
+ consumedTime: "",
29
+ issuer: "",
30
+ parent: "",
31
+ readonly: false,
32
+ transferrable: true,
33
+ ttl: 0,
34
+ tags: [],
35
+ context: create$1(context),
36
+ ...pick(attrs, [
37
+ "address",
38
+ "owner",
39
+ "issuer",
40
+ "moniker",
41
+ "parent",
42
+ "data",
43
+ "readonly",
44
+ "transferrable",
45
+ "ttl",
46
+ "endpoint",
47
+ "display",
48
+ "tags"
49
+ ])
50
+ };
51
+ if (asset.tags === null) asset.tags = [];
52
+ asset.address = toAddress(asset.address);
53
+ return validate(asset);
54
+ };
55
+ const update = (state, attrs, context) => {
56
+ const asset = {
57
+ ...state,
58
+ ...pick(attrs, [
59
+ "moniker",
60
+ "data",
61
+ "owner",
62
+ "consumedTime"
63
+ ]),
64
+ context: update$1(state.context, context)
65
+ };
66
+ if (asset.tags === null) asset.tags = [];
67
+ if (!asset.consumedTime) asset.consumedTime = "";
68
+ return validate(asset);
69
+ };
70
+ const validate = (state) => {
71
+ const { value, error } = stateSchema.validate(state);
72
+ if (error) throw new CustomError("INVALID_ASSET", `Invalid asset: ${error.details.map((x) => x.message).join(", ")}`);
73
+ ["endpoint", "display"].forEach((key) => {
74
+ if (!value[key]) delete value[key];
75
+ });
76
+ return value;
77
+ };
78
+
79
+ //#endregion
80
+ export { asset_exports, create, schema, schemas, update, validate };
@@ -0,0 +1,36 @@
1
+ //#region src/states/blacklist.d.ts
2
+ interface TrustedAccountConfig {
3
+ address: string;
4
+ [key: string]: unknown;
5
+ }
6
+ interface BlacklistOptions {
7
+ size?: number;
8
+ numHashFns?: number;
9
+ dumpPath?: string;
10
+ dumpInterval?: number;
11
+ getBlockedAccounts?: (() => Promise<string[]>) | null;
12
+ getTrustedAccounts?: (() => Promise<TrustedAccountConfig[]>) | null;
13
+ }
14
+ declare class Blacklist {
15
+ private dumpPath;
16
+ private filter;
17
+ private getBlockedAccounts;
18
+ private getTrustedAccounts;
19
+ private timer?;
20
+ constructor({
21
+ size,
22
+ numHashFns,
23
+ dumpPath,
24
+ dumpInterval,
25
+ getBlockedAccounts,
26
+ getTrustedAccounts
27
+ }?: BlacklistOptions);
28
+ has(key: string): boolean;
29
+ add(key: string): void;
30
+ isBlocked(address: string): Promise<boolean>;
31
+ isTrusted(address: string): Promise<boolean>;
32
+ getTrustedAccountConfig(address: string): Promise<TrustedAccountConfig | null | undefined>;
33
+ dump(): void;
34
+ }
35
+ //#endregion
36
+ export { BlacklistOptions, TrustedAccountConfig, Blacklist as default };
@@ -0,0 +1,71 @@
1
+ import { isSameDid } from "@ocap/util";
2
+ import { CustomError } from "@ocap/util/lib/error";
3
+ import fs from "node:fs";
4
+ import { BloomFilter } from "bloom-filters";
5
+
6
+ //#region src/states/blacklist.ts
7
+ var Blacklist = class {
8
+ constructor({ size = 256, numHashFns = 4, dumpPath = "", dumpInterval = 0, getBlockedAccounts = null, getTrustedAccounts = null } = {}) {
9
+ let filter;
10
+ if (dumpPath && fs.existsSync(dumpPath)) try {
11
+ const json = JSON.parse(fs.readFileSync(dumpPath, "utf-8"));
12
+ filter = BloomFilter.fromJSON(json);
13
+ } catch (_err) {
14
+ throw new CustomError("INTERNAL", `Cat not read serialized blacklist from json${dumpPath}`);
15
+ }
16
+ if (!filter) filter = new BloomFilter(size, numHashFns);
17
+ this.dumpPath = dumpPath;
18
+ this.filter = filter;
19
+ this.getBlockedAccounts = getBlockedAccounts;
20
+ this.getTrustedAccounts = getTrustedAccounts;
21
+ if (dumpPath && dumpInterval > 0) {
22
+ this.timer = setTimeout(() => {
23
+ this.dump();
24
+ this.timer = setTimeout(() => {
25
+ this.dump();
26
+ }, dumpInterval);
27
+ this.timer.unref();
28
+ }, dumpInterval);
29
+ this.timer.unref();
30
+ process.on("beforeExit", () => {
31
+ this.dump();
32
+ });
33
+ }
34
+ }
35
+ has(key) {
36
+ return this.filter.has(key);
37
+ }
38
+ add(key) {
39
+ this.filter.add(key);
40
+ }
41
+ async isBlocked(address) {
42
+ if (typeof this.getBlockedAccounts === "function") {
43
+ const blocked = await this.getBlockedAccounts();
44
+ if (blocked.length) return blocked.some((b) => isSameDid(b, address));
45
+ }
46
+ return false;
47
+ }
48
+ async isTrusted(address) {
49
+ if (typeof this.getTrustedAccounts === "function") {
50
+ const trustedAccounts = await this.getTrustedAccounts();
51
+ if (trustedAccounts.length) return trustedAccounts.some((b) => isSameDid(b.address, address));
52
+ }
53
+ return false;
54
+ }
55
+ async getTrustedAccountConfig(address) {
56
+ if (typeof this.getTrustedAccounts === "function") return (await this.getTrustedAccounts()).find((x) => isSameDid(x.address, address));
57
+ return null;
58
+ }
59
+ dump() {
60
+ try {
61
+ const exported = this.filter.saveAsJSON();
62
+ const tmpFile = `${this.dumpPath}~`;
63
+ fs.writeFileSync(tmpFile, JSON.stringify(exported));
64
+ fs.renameSync(tmpFile, this.dumpPath);
65
+ } catch (_err) {}
66
+ }
67
+ };
68
+ var blacklist_default = Blacklist;
69
+
70
+ //#endregion
71
+ export { blacklist_default as default };
@@ -0,0 +1,30 @@
1
+ import { StateContext } from "../contexts/state.mjs";
2
+
3
+ //#region src/states/chain.d.ts
4
+ declare namespace chain_d_exports {
5
+ export { ChainState, create, update };
6
+ }
7
+ interface ChainState {
8
+ address?: string;
9
+ chainId?: string;
10
+ version?: string;
11
+ transaction?: unknown;
12
+ moderator?: unknown;
13
+ accounts?: unknown;
14
+ token?: {
15
+ symbol?: string;
16
+ decimal?: number;
17
+ name?: string;
18
+ description?: string;
19
+ unit?: string;
20
+ totalSupply?: string;
21
+ initialSupply?: string;
22
+ foreignToken?: unknown;
23
+ };
24
+ vaults?: unknown;
25
+ context: StateContext;
26
+ }
27
+ declare const create: (attrs: Partial<ChainState>) => ChainState;
28
+ declare const update: (state: ChainState, updates: Partial<ChainState>) => ChainState;
29
+ //#endregion
30
+ export { ChainState, chain_d_exports, create, update };
@@ -0,0 +1,52 @@
1
+ import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
2
+ import { create as create$1, update as update$1 } from "../contexts/state.mjs";
3
+ import { CustomError } from "@ocap/util/lib/error";
4
+ import pick from "lodash/pick.js";
5
+ import get from "lodash/get.js";
6
+ import isEqual from "lodash/isEqual.js";
7
+
8
+ //#region src/states/chain.ts
9
+ var chain_exports = /* @__PURE__ */ __exportAll({
10
+ create: () => create,
11
+ update: () => update
12
+ });
13
+ const create = (attrs) => {
14
+ return {
15
+ context: create$1({ txTime: (/* @__PURE__ */ new Date()).toISOString() }),
16
+ ...pick(attrs, [
17
+ "address",
18
+ "chainId",
19
+ "version",
20
+ "transaction",
21
+ "moderator",
22
+ "accounts",
23
+ "token",
24
+ "vaults"
25
+ ])
26
+ };
27
+ };
28
+ const update = (state, updates) => {
29
+ const immutableAttrs = [
30
+ "chainId",
31
+ "moderator",
32
+ "token.symbol",
33
+ "token.decimal"
34
+ ];
35
+ if (!process.env.CHAIN_TOKEN_ADDRESS) immutableAttrs.push("token.name", "token.description", "token.unit", "token.totalSupply", "token.initialSupply", "token.foreignToken");
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 context = { txTime: (/* @__PURE__ */ new Date()).toISOString() };
38
+ return {
39
+ ...state,
40
+ ...pick(updates, [
41
+ "version",
42
+ "transaction",
43
+ "accounts",
44
+ "vaults",
45
+ "token"
46
+ ]),
47
+ context: update$1(state.context, context)
48
+ };
49
+ };
50
+
51
+ //#endregion
52
+ export { chain_exports, create, update };
@@ -0,0 +1,11 @@
1
+ import { StateContextInput } from "../contexts/state.mjs";
2
+ import { IDelegateState } from "@ocap/types";
3
+
4
+ //#region src/states/delegation.d.ts
5
+ declare namespace delegation_d_exports {
6
+ export { create, update };
7
+ }
8
+ declare const create: (attrs: Partial<IDelegateState>, context: StateContextInput) => IDelegateState;
9
+ declare const update: (state: IDelegateState, attrs: Partial<IDelegateState>, context: StateContextInput) => IDelegateState;
10
+ //#endregion
11
+ export { create, delegation_d_exports, update };
@@ -0,0 +1,42 @@
1
+ import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
2
+ import { create as create$1, update as update$1 } from "../contexts/state.mjs";
3
+ import { nullify } from "../util.mjs";
4
+ import { toAddress } from "@ocap/util";
5
+ import pick from "lodash/pick.js";
6
+
7
+ //#region src/states/delegation.ts
8
+ var delegation_exports = /* @__PURE__ */ __exportAll({
9
+ create: () => create,
10
+ update: () => update
11
+ });
12
+ const create = (attrs, context) => {
13
+ const delegation = {
14
+ context: create$1(context),
15
+ ...pick(attrs, [
16
+ "address",
17
+ "to",
18
+ "from",
19
+ "ops",
20
+ "data"
21
+ ])
22
+ };
23
+ if (!delegation.data) delegation.data = null;
24
+ delegation.address = toAddress(delegation.address);
25
+ delegation.ops = nullify(delegation.ops);
26
+ return delegation;
27
+ };
28
+ const update = (state, attrs, context) => {
29
+ const delegation = {
30
+ ...state,
31
+ ...pick(attrs, ["ops", "data"]),
32
+ context: update$1(state.context, context)
33
+ };
34
+ if (!delegation.data) delegation.data = null;
35
+ if (!state.to && attrs.to) delegation.to = toAddress(attrs.to);
36
+ if (!state.from && attrs.from) delegation.from = toAddress(attrs.from);
37
+ delegation.ops = nullify(delegation.ops);
38
+ return delegation;
39
+ };
40
+
41
+ //#endregion
42
+ export { create, delegation_exports, update };
@@ -0,0 +1,12 @@
1
+ import { StateContextInput } from "../contexts/state.mjs";
2
+ import { IEvidenceState } from "@ocap/types";
3
+
4
+ //#region src/states/evidence.d.ts
5
+ declare namespace evidence_d_exports {
6
+ export { create, schema, validate };
7
+ }
8
+ declare const schema: any;
9
+ declare const create: (attrs: Partial<IEvidenceState>, context: StateContextInput) => IEvidenceState;
10
+ declare const validate: (state: IEvidenceState) => IEvidenceState;
11
+ //#endregion
12
+ export { create, evidence_d_exports, schema, validate };
@@ -0,0 +1,35 @@
1
+ import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
2
+ import { create as create$1 } from "../contexts/state.mjs";
3
+ import { CustomError } from "@ocap/util/lib/error";
4
+ import pick from "lodash/pick.js";
5
+ import { Joi, patterns, schemas } from "@arcblock/validator";
6
+
7
+ //#region src/states/evidence.ts
8
+ var evidence_exports = /* @__PURE__ */ __exportAll({
9
+ create: () => create,
10
+ schema: () => schema,
11
+ validate: () => validate
12
+ });
13
+ const schema = Joi.object({
14
+ hash: Joi.string().regex(patterns.txHash).required(),
15
+ context: schemas.context,
16
+ data: Joi.any().optional().allow(null)
17
+ }).options({
18
+ stripUnknown: true,
19
+ noDefaults: false
20
+ });
21
+ const create = (attrs, context) => {
22
+ return validate({
23
+ context: create$1(context),
24
+ ...pick(attrs, ["hash", "data"])
25
+ });
26
+ };
27
+ const validate = (state) => {
28
+ const { value, error } = schema.validate(state);
29
+ if (error) throw new CustomError("INVALID_EVIDENCE", `Invalid evidence state: ${error.details.map((x) => x.message).join(", ")}`);
30
+ if (!value.data) value.data = null;
31
+ return value;
32
+ };
33
+
34
+ //#endregion
35
+ export { create, evidence_exports, schema, validate };
@@ -0,0 +1,12 @@
1
+ import { StateContextInput } from "../contexts/state.mjs";
2
+ import { IAssetFactoryState } from "@ocap/types";
3
+
4
+ //#region src/states/factory.d.ts
5
+ declare namespace factory_d_exports {
6
+ export { create, update, validate };
7
+ }
8
+ declare const create: (attrs: Partial<IAssetFactoryState>, context: StateContextInput) => IAssetFactoryState;
9
+ declare const update: (state: IAssetFactoryState, attrs: Partial<IAssetFactoryState>, context: StateContextInput) => IAssetFactoryState;
10
+ declare const validate: (state: IAssetFactoryState) => IAssetFactoryState;
11
+ //#endregion
12
+ export { create, factory_d_exports, update, validate };