@ocap/state 1.28.9 → 1.29.0

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,233 @@
1
+ declare namespace tx_d_exports {
2
+ export { FORGE_TOKEN_HOLDER, ItxJson, Receipt, ReceiptChange, TokenInput, TxContext, TxData, TxInput, TxOutput, TxResult, attachPaidTxGas, create, eachReceipts, getTxReceipts, getTxReceiver, getTxSender, groupReceiptTokenChanges, mergeTxReceipts, update, verifyTxReceipts };
3
+ }
4
+ declare const FORGE_TOKEN_HOLDER = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
5
+ interface ReceiptChange {
6
+ target: string;
7
+ action: string;
8
+ value: string;
9
+ }
10
+ interface Receipt {
11
+ address: string;
12
+ changes: ReceiptChange[];
13
+ }
14
+ interface TokenInput {
15
+ address: string;
16
+ value: string;
17
+ }
18
+ interface TxInput {
19
+ owner: string;
20
+ tokens?: TokenInput[];
21
+ assets?: string[];
22
+ }
23
+ interface TxOutput {
24
+ owner: string;
25
+ tokens?: TokenInput[];
26
+ assets?: string[];
27
+ }
28
+ interface ItxJson {
29
+ _type?: string;
30
+ to?: string;
31
+ owner?: string;
32
+ address?: string;
33
+ receiver?: string;
34
+ value?: string;
35
+ tokens?: TokenInput[];
36
+ assets?: string[];
37
+ sender?: {
38
+ value?: string;
39
+ tokens?: TokenInput[];
40
+ assets?: string[];
41
+ };
42
+ inputs?: TxInput[];
43
+ outputs?: TxOutput[];
44
+ initialSupply?: string;
45
+ amount?: string;
46
+ issuer?: string;
47
+ withdraw_tx_hash?: string;
48
+ [key: string]: unknown;
49
+ }
50
+ interface TxData {
51
+ from: string;
52
+ delegator?: string;
53
+ itxJson: ItxJson;
54
+ chainId?: string;
55
+ nonce?: number;
56
+ serviceFee?: string;
57
+ pk?: Uint8Array;
58
+ signature?: Uint8Array;
59
+ signatures?: unknown[];
60
+ itx?: {
61
+ typeUrl: string;
62
+ value?: Uint8Array;
63
+ };
64
+ gasPaid?: string;
65
+ gasFee?: string;
66
+ }
67
+ interface TxContext {
68
+ txHash?: string;
69
+ txTime?: string;
70
+ tx?: TxData;
71
+ totalGas?: unknown;
72
+ itxExtras?: Record<string, unknown>;
73
+ extra?: {
74
+ txExtra?: unknown;
75
+ };
76
+ senderState?: {
77
+ address?: string;
78
+ tokens?: Record<string, string>;
79
+ };
80
+ receiverState?: {
81
+ address?: string;
82
+ };
83
+ fromState?: {
84
+ address?: string;
85
+ tokens?: Record<string, string>;
86
+ };
87
+ factoryState?: {
88
+ input?: {
89
+ value?: string;
90
+ tokens?: TokenInput[];
91
+ };
92
+ };
93
+ ownerAddress?: string;
94
+ config?: {
95
+ token?: {
96
+ address?: string;
97
+ decimal?: number;
98
+ };
99
+ vaults?: {
100
+ txGas?: string[];
101
+ };
102
+ };
103
+ time?: string;
104
+ receipts?: Receipt[];
105
+ gasPaid?: boolean;
106
+ outputs?: TxOutput[];
107
+ withdrawTx?: {
108
+ hash?: string;
109
+ tx?: TxData;
110
+ receipts?: Receipt[];
111
+ };
112
+ updatedAccounts?: Array<{
113
+ address?: string;
114
+ delta?: string;
115
+ token?: string;
116
+ action?: string;
117
+ }>;
118
+ inputChanges?: Array<{
119
+ address: string;
120
+ delta: string;
121
+ }>;
122
+ tokenFactoryState?: {
123
+ reserveAddress: string;
124
+ tokenAddress: string;
125
+ owner: string;
126
+ };
127
+ reserveFee?: string;
128
+ reserveAmount?: string;
129
+ [key: string]: unknown;
130
+ }
131
+ interface TxResult {
132
+ code: string;
133
+ hash?: string;
134
+ height: number;
135
+ index: number;
136
+ time?: string;
137
+ sender: string;
138
+ receiver: string;
139
+ type: string;
140
+ tx: {
141
+ chainId?: string;
142
+ delegator?: string;
143
+ from?: string;
144
+ nonce?: number;
145
+ serviceFee: string;
146
+ gasFee: string;
147
+ gasPaid?: string;
148
+ pk?: string;
149
+ signature?: string;
150
+ signatures?: unknown[];
151
+ itx?: {
152
+ __typename: string;
153
+ };
154
+ itxJson?: Record<string, unknown>;
155
+ extra?: unknown;
156
+ };
157
+ receipts?: Receipt[];
158
+ receiptsVerified?: boolean;
159
+ finalized?: boolean;
160
+ }
161
+ type EachReceiptsCallback = (address: string, change: ReceiptChange) => unknown;
162
+ declare function eachReceipts(receipts: Receipt[] | undefined, callback: EachReceiptsCallback): unknown[];
163
+ declare function groupReceiptTokenChanges(receipts: Receipt[] | undefined): Record<string, Record<string, string>>;
164
+ interface GetTxReceiverArgs {
165
+ tx?: TxData;
166
+ itx?: ItxJson;
167
+ typeUrl: string;
168
+ }
169
+ declare const getTxReceiver: ({
170
+ tx,
171
+ itx,
172
+ typeUrl
173
+ }: GetTxReceiverArgs) => string;
174
+ interface GetTxSenderArgs {
175
+ tx: TxData;
176
+ itx?: ItxJson;
177
+ typeUrl: string;
178
+ }
179
+ declare const getTxSender: ({
180
+ tx,
181
+ typeUrl
182
+ }: GetTxSenderArgs) => string;
183
+ declare const mergeTxReceipts: (receipts: Receipt[]) => Receipt[];
184
+ /**
185
+ * Verify transaction receipts
186
+ */
187
+ declare const verifyTxReceipts: (receipts: Receipt[], typeUrl: string, ctx?: TxContext) => boolean;
188
+ interface GetTxReceiptsInput {
189
+ tx: TxData;
190
+ code: string;
191
+ }
192
+ /**
193
+ * Create transaction receipts, each receipt has following properties:
194
+ *
195
+ * - address: the entity did that changed
196
+ * - changes:
197
+ * - target: the target address that was changed, can be token address, asset address
198
+ * - action: why the target has changed, such as consume, burn and transfer
199
+ * - value: the amount that has changed
200
+ */
201
+ declare const getTxReceipts: ({
202
+ tx,
203
+ code
204
+ }: GetTxReceiptsInput, ctx?: TxContext) => Receipt[];
205
+ interface CreateContext extends TxContext {
206
+ txHash: string;
207
+ txTime: string;
208
+ tx: {
209
+ chainId?: string;
210
+ delegator?: string;
211
+ from: string;
212
+ nonce?: number;
213
+ serviceFee?: string;
214
+ pk?: Uint8Array;
215
+ signature?: Uint8Array;
216
+ signatures?: unknown[];
217
+ itx: {
218
+ typeUrl: string;
219
+ value?: Uint8Array;
220
+ };
221
+ itxJson: ItxJson;
222
+ };
223
+ totalGas?: unknown;
224
+ itxExtras?: Record<string, unknown>;
225
+ extra?: {
226
+ txExtra?: unknown;
227
+ };
228
+ }
229
+ declare const create: (context: CreateContext, code?: string, verifyReceipts?: boolean) => TxResult;
230
+ declare const attachPaidTxGas: (tx: TxResult) => TxResult;
231
+ declare const update: (state: TxResult, updates: Partial<TxResult>) => TxResult;
232
+ //#endregion
233
+ export { FORGE_TOKEN_HOLDER, ItxJson, Receipt, ReceiptChange, TokenInput, TxContext, TxData, TxInput, TxOutput, TxResult, attachPaidTxGas, create, eachReceipts, getTxReceipts, getTxReceiver, getTxSender, groupReceiptTokenChanges, mergeTxReceipts, tx_d_exports, update, verifyTxReceipts };
package/lib/util.cjs ADDED
@@ -0,0 +1,19 @@
1
+
2
+ //#region src/util.ts
3
+ function nullify(obj) {
4
+ if (typeof obj === "undefined") return null;
5
+ if (typeof obj === "object" && obj !== null) if (Array.isArray(obj)) obj.forEach((item, index) => {
6
+ obj[index] = nullify(item);
7
+ });
8
+ else {
9
+ const record = obj;
10
+ for (const key in record) if (Object.prototype.hasOwnProperty.call(record, key)) {
11
+ if (typeof record[key] === "undefined") record[key] = null;
12
+ else if (typeof record[key] === "object") record[key] = nullify(record[key]);
13
+ }
14
+ }
15
+ return obj;
16
+ }
17
+
18
+ //#endregion
19
+ exports.nullify = nullify;
package/lib/util.d.cts ADDED
@@ -0,0 +1,6 @@
1
+ //#region src/util.d.ts
2
+ type NullifyInput = unknown;
3
+ type NullifyOutput<T> = T extends undefined ? null : T;
4
+ declare function nullify<T extends NullifyInput>(obj: T): NullifyOutput<T>;
5
+ //#endregion
6
+ export { nullify };
package/package.json CHANGED
@@ -3,31 +3,63 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.28.9",
7
- "description": "",
8
- "main": "lib/index.js",
6
+ "version": "1.29.0",
7
+ "description": "State management utilities for OCAP blockchain",
8
+ "type": "module",
9
+ "main": "./lib/index.cjs",
10
+ "module": "./esm/index.mjs",
11
+ "types": "./esm/index.d.mts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./esm/index.d.mts",
15
+ "import": "./esm/index.mjs",
16
+ "default": "./lib/index.cjs"
17
+ },
18
+ "./lib/*.js": {
19
+ "types": "./esm/*.d.mts",
20
+ "import": "./esm/*.mjs",
21
+ "default": "./lib/*.cjs"
22
+ },
23
+ "./lib/*": {
24
+ "types": "./esm/*.d.mts",
25
+ "import": "./esm/*.mjs",
26
+ "default": "./lib/*.cjs"
27
+ }
28
+ },
9
29
  "files": [
10
- "lib"
30
+ "lib",
31
+ "esm"
11
32
  ],
12
33
  "scripts": {
34
+ "build": "tsdown",
35
+ "prebuild": "rm -rf lib esm",
13
36
  "lint": "biome check",
14
37
  "lint:fix": "biome check --write",
15
38
  "test": "bun test",
16
39
  "coverage": "bun test --coverage"
17
40
  },
18
41
  "dependencies": {
19
- "@arcblock/did": "1.28.9",
20
- "@arcblock/validator": "1.28.9",
21
- "@ocap/contract": "1.28.9",
22
- "@ocap/mcrypto": "1.28.9",
23
- "@ocap/message": "1.28.9",
24
- "@ocap/util": "1.28.9",
25
- "@ocap/wallet": "1.28.9",
42
+ "@arcblock/did": "1.29.0",
43
+ "@arcblock/validator": "1.29.0",
44
+ "@ocap/contract": "1.29.0",
45
+ "@ocap/mcrypto": "1.29.0",
46
+ "@ocap/message": "1.29.0",
47
+ "@ocap/types": "1.29.0",
48
+ "@ocap/util": "1.29.0",
49
+ "@ocap/wallet": "1.29.0",
26
50
  "bloom-filters": "^1.3.9",
27
- "lodash": "^4.17.21"
51
+ "lodash": "^4.17.23"
52
+ },
53
+ "devDependencies": {
54
+ "@types/lodash": "^4.17.16"
28
55
  },
29
- "devDependencies": {},
30
- "keywords": [],
56
+ "keywords": [
57
+ "blockchain",
58
+ "arcblock",
59
+ "sdk",
60
+ "nodejs",
61
+ "state"
62
+ ],
31
63
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
32
64
  "license": "MIT",
33
65
  "gitHead": "e5764f753181ed6a7c615cd4fc6682aacf0cb7cd"
@@ -1,19 +0,0 @@
1
- // StateContext utils for account/asset/delegate etc.
2
-
3
- const create = ({ txHash, txTime }) => ({
4
- genesisTime: txTime || '',
5
- genesisTx: txHash || '',
6
- renaissanceTime: txTime || '',
7
- renaissanceTx: txHash || '',
8
- });
9
-
10
- const update = (context, { txHash, txTime }) => ({
11
- ...context,
12
- // NOTE: for historical reasons, some account state does not have genesisTime
13
- genesisTime: context.genesisTime || '2020-01-15T00:00:00.000Z',
14
- genesisTx: context.genesisTx || '',
15
- renaissanceTime: txTime || '',
16
- renaissanceTx: txHash || '',
17
- });
18
-
19
- module.exports = { create, update };
package/lib/index.js DELETED
@@ -1,63 +0,0 @@
1
- const chain = require('./states/chain');
2
- const account = require('./states/account');
3
- const asset = require('./states/asset');
4
- const delegation = require('./states/delegation');
5
- const tx = require('./states/tx');
6
- const factory = require('./states/factory');
7
- const token = require('./states/token');
8
- const stake = require('./states/stake');
9
- const rollup = require('./states/rollup');
10
- const rollupBlock = require('./states/rollup-block');
11
- const evidence = require('./states/evidence');
12
- const tokenFactory = require('./states/token-factory');
13
-
14
- const Blacklist = require('./states/blacklist');
15
-
16
- module.exports = {
17
- chain,
18
- account,
19
- asset,
20
- factory,
21
- delegation,
22
- tx,
23
- token,
24
- stake,
25
- rollup,
26
- rollupBlock,
27
- evidence,
28
- tokenFactory,
29
-
30
- // indexes of the statedb to speed up reading
31
- indexes: [
32
- 'account',
33
- 'asset',
34
- 'delegation',
35
- 'tx',
36
- 'factory',
37
- 'token',
38
- 'stake',
39
- 'rollup',
40
- 'rollupBlock',
41
- 'rollupValidator',
42
- 'tokenDistribution',
43
- 'tokenFactory',
44
- ],
45
-
46
- tables: [
47
- 'chain',
48
- 'account',
49
- 'asset',
50
- 'delegation',
51
- 'tx',
52
- 'factory',
53
- 'token',
54
- 'stake',
55
- 'rollup',
56
- 'rollupBlock',
57
- 'evidence', // a simple key-value store
58
- 'balance',
59
- 'tokenFactory',
60
- ],
61
-
62
- Blacklist,
63
- };
@@ -1,95 +0,0 @@
1
- const pick = require('lodash/pick');
2
- const uniq = require('lodash/uniq');
3
- const flatten = require('lodash/flatten');
4
- const { CustomError: Error } = require('@ocap/util/lib/error');
5
- const { isFromPublicKey } = require('@arcblock/did');
6
- const { isEthereumDid, toChecksumAddress } = require('@arcblock/did/lib/type');
7
- const { toBase58, toAddress, BN } = require('@ocap/util');
8
-
9
- const { Joi, schemas } = require('@arcblock/validator');
10
- const { create: createStateContext, update: updateStateContext } = require('../contexts/state');
11
-
12
- const schema = Joi.object({
13
- address: Joi.DID().prefix().required(),
14
- pk: Joi.string().allow(null).allow(''),
15
- issuer: Joi.DID().prefix().allow(null).allow(''),
16
- moniker: Joi.string()
17
- .regex(/^[a-zA-Z0-9][-a-zA-Z0-9_]{2,128}$/)
18
- .allow(''),
19
- nonce: Joi.number().min(0).default(0),
20
- tokens: Joi.object().pattern(Joi.DID().prefix().role('ROLE_TOKEN'), Joi.BN().min(0)).default({}),
21
- migratedTo: Joi.array().items(Joi.DID().prefix()).default([]),
22
- migratedFrom: Joi.array().items(Joi.DID().prefix()).default([]),
23
- context: schemas.context,
24
- data: Joi.any().optional().allow(null),
25
- }).options({ stripUnknown: true, noDefaults: false });
26
-
27
- const create = (attrs, context) => {
28
- const account = {
29
- nonce: 0,
30
- migratedTo: uniq([].concat(attrs.migratedTo || [])),
31
- migratedFrom: [],
32
- tokens: {},
33
- context: createStateContext(context),
34
- ...pick(attrs, ['address', 'pk', 'issuer', 'moniker', 'data', 'nonce', 'migratedFrom', 'tokens']),
35
- };
36
-
37
- account.address = toAddress(ensureChecksumAddress(account.address));
38
-
39
- if (!account.moniker) {
40
- account.moniker = [account.address.slice(0, 6), account.address.slice(-5)].join('-');
41
- }
42
-
43
- return validate(account);
44
- };
45
-
46
- const update = (state, attrs, context) => {
47
- if (attrs.nonce && new BN(attrs.nonce).eq(new BN(state.nonce))) {
48
- throw new Error('INVALID_NONCE', 'nonce must be greater in newer transactions');
49
- }
50
-
51
- // ensure we are updating the correct pk
52
- if (attrs.pk && isFromPublicKey(state.address, attrs.pk) === false) {
53
- delete attrs.pk;
54
- }
55
-
56
- const account = {
57
- ...state,
58
- ...pick(attrs, ['moniker', 'data', 'migratedTo', 'nonce', 'tokens', 'pk']),
59
- migratedTo: uniq(flatten(attrs.migratedTo ? [attrs.migratedTo].concat(state.migratedTo) : state.migratedTo)),
60
- context: updateStateContext(state.context, context),
61
- };
62
-
63
- return validate(account);
64
- };
65
-
66
- const updateOrCreate = (state, attrs, context) => {
67
- if (state) {
68
- return update(state, attrs, context);
69
- }
70
-
71
- return create(attrs, context);
72
- };
73
-
74
- const validate = (state) => {
75
- // ensure we have correct pk
76
- if (state.pk && typeof state.pk !== 'string') {
77
- state.pk = toBase58(state.pk);
78
- }
79
-
80
- const { value, error } = schema.validate(state);
81
- if (error) {
82
- throw new Error('INVALID_ACCOUNT', `Invalid account: ${error.details.map((x) => x.message).join(', ')}`);
83
- }
84
-
85
- if (!value.data) {
86
- value.data = null;
87
- }
88
-
89
- return value;
90
- };
91
-
92
- const isMigrated = (state) => (state.migratedTo || []).length > 0;
93
- const ensureChecksumAddress = (address) => (isEthereumDid(address) ? toChecksumAddress(address) : address);
94
-
95
- module.exports = { create, update, updateOrCreate, validate, isMigrated, ensureChecksumAddress, schema };
@@ -1,91 +0,0 @@
1
- const pick = require('lodash/pick');
2
- const { Joi, schemas } = require('@arcblock/validator');
3
- const { CustomError: Error } = require('@ocap/util/lib/error');
4
- const { toAddress } = require('@ocap/util');
5
-
6
- const { create: createStateContext, update: updateStateContext } = require('../contexts/state');
7
-
8
- const schema = Joi.object({
9
- ...schemas.assetProps,
10
- owner: Joi.DID().prefix().required(),
11
- consumedTime: Joi.date().iso().raw().allow(''),
12
- context: schemas.context,
13
- }).options({
14
- stripUnknown: true,
15
- noDefaults: false,
16
- });
17
-
18
- const create = (attrs, context) => {
19
- const asset = {
20
- consumedTime: '',
21
- issuer: '',
22
- parent: '',
23
- readonly: false,
24
- transferrable: true,
25
- ttl: 0, // means unlimited
26
- tags: [],
27
- context: createStateContext(context),
28
- ...pick(attrs, [
29
- 'address',
30
- 'owner',
31
- 'issuer',
32
- 'moniker',
33
- 'parent',
34
- 'data',
35
- 'readonly',
36
- 'transferrable',
37
- 'ttl',
38
- 'endpoint',
39
- 'display',
40
- 'tags',
41
- ]),
42
- };
43
-
44
- if (asset.tags === null) {
45
- asset.tags = [];
46
- }
47
-
48
- asset.address = toAddress(asset.address);
49
-
50
- return validate(asset);
51
- };
52
-
53
- const update = (state, attrs, context) => {
54
- const asset = {
55
- ...state,
56
- ...pick(attrs, ['moniker', 'data', 'owner', 'consumedTime']),
57
- context: updateStateContext(state.context, context),
58
- };
59
-
60
- if (asset.tags === null) {
61
- asset.tags = [];
62
- }
63
-
64
- if (!asset.consumedTime) {
65
- asset.consumedTime = '';
66
- }
67
-
68
- return validate(asset);
69
- };
70
-
71
- const validate = (state) => {
72
- const { value, error } = schema.validate(state);
73
- if (error) {
74
- throw new Error('INVALID_ASSET', `Invalid asset: ${error.details.map((x) => x.message).join(', ')}`);
75
- }
76
-
77
- ['endpoint', 'display'].forEach((key) => {
78
- if (!value[key]) {
79
- delete value[key];
80
- }
81
- });
82
-
83
- return value;
84
- };
85
-
86
- module.exports = {
87
- create,
88
- update,
89
- validate,
90
- schema: schemas.assetSchema,
91
- };