@ocap/asset 1.30.23 → 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.
@@ -1,7 +1,18 @@
1
- import { TInputMap, TIssuer, TPickedFactoryState, TPreMintResult } from "../types.mjs";
1
+ import { TInputMap, TIssuer, TIssuerInput, TMintResult, TPickedFactoryState, TPreMintResult } from "../types.mjs";
2
2
  import { formatFactoryState } from "./shared.mjs";
3
3
 
4
4
  //#region src/mint/client.d.ts
5
+ declare const mintFromFactory: ({
6
+ factory,
7
+ inputs,
8
+ owner,
9
+ issuer
10
+ }: {
11
+ factory: TPickedFactoryState;
12
+ inputs: TInputMap;
13
+ owner: string;
14
+ issuer: TIssuerInput;
15
+ }) => TMintResult;
5
16
  declare const preMintFromFactory: ({
6
17
  factory,
7
18
  inputs,
@@ -14,4 +25,4 @@ declare const preMintFromFactory: ({
14
25
  issuer?: TIssuer;
15
26
  }) => Promise<TPreMintResult>;
16
27
  //#endregion
17
- export { formatFactoryState, preMintFromFactory };
28
+ export { formatFactoryState, mintFromFactory, preMintFromFactory };
@@ -1,4 +1,4 @@
1
- import { formatFactoryState, preparePreMintAsset } from "./shared.mjs";
1
+ import { formatFactoryState, preparePreMintAsset, renderMintAsset } from "./shared.mjs";
2
2
  import { toAssetAddress } from "@arcblock/did-util/cbor";
3
3
 
4
4
  //#region src/mint/client.ts
@@ -13,6 +13,18 @@ import { toAssetAddress } from "@arcblock/did-util/cbor";
13
13
  * parameter — the subpath IS the encoding selector. Chain-side callers that
14
14
  * need dispatch keep using `@ocap/asset/mint/server` or `@ocap/asset`.
15
15
  */
16
+ const mintFromFactory = ({ factory, inputs, owner, issuer }) => {
17
+ const asset = renderMintAsset({
18
+ factory,
19
+ inputs,
20
+ owner,
21
+ issuer
22
+ });
23
+ return {
24
+ asset,
25
+ address: toAssetAddress(asset)
26
+ };
27
+ };
16
28
  const preMintFromFactory = async ({ factory, inputs, owner, issuer }) => {
17
29
  const { asset, issuer: issuerObject, variables } = await preparePreMintAsset({
18
30
  factory,
@@ -29,4 +41,4 @@ const preMintFromFactory = async ({ factory, inputs, owner, issuer }) => {
29
41
  };
30
42
 
31
43
  //#endregion
32
- export { formatFactoryState, preMintFromFactory };
44
+ export { formatFactoryState, mintFromFactory, preMintFromFactory };
@@ -16,6 +16,16 @@ import mustache from "mustache";
16
16
  * browser/Worker bundles that stick to CBOR never drag the protobuf runtime
17
17
  * in via the aggregate did-util entry.
18
18
  */
19
+ const enforceProtectedAssetFields = (rendered, output, factoryAddress) => {
20
+ if (!rendered || typeof rendered !== "object") return rendered;
21
+ if (output && typeof output === "object") {
22
+ if (output.readonly !== void 0 && rendered.readonly !== output.readonly) throw new Error("Failed to mint asset from factory: protected field \"readonly\" cannot be overridden");
23
+ if (output.transferrable !== void 0 && rendered.transferrable !== output.transferrable) throw new Error("Failed to mint asset from factory: protected field \"transferrable\" cannot be overridden");
24
+ if (!("owner" in output) && "owner" in rendered) throw new Error("Failed to mint asset from factory: protected field \"owner\" cannot be injected");
25
+ if (output.parent === "{{ctx.factory}}" && factoryAddress && rendered.parent !== factoryAddress) throw new Error("Failed to mint asset from factory: protected field \"parent\" cannot be overridden");
26
+ }
27
+ return rendered;
28
+ };
19
29
  const getPath = (obj, path) => {
20
30
  if (obj == null) return void 0;
21
31
  return path.split(".").reduce((acc, k) => acc == null ? acc : acc[k], obj);
@@ -34,7 +44,7 @@ const setPath = (obj, path, value) => {
34
44
  };
35
45
  const debug = Debug("@ocap/asset");
36
46
  const formatFactoryState = (state) => {
37
- const { address, output, data, numMinted } = state;
47
+ const { address, output, data, numMinted, limit } = state;
38
48
  const outputX = structuredClone(output);
39
49
  const outputData = outputX.data;
40
50
  outputData.value = JSON.parse(outputData.value);
@@ -44,7 +54,8 @@ const formatFactoryState = (state) => {
44
54
  address,
45
55
  output: outputX,
46
56
  data: stateData?.value ? JSON.parse(stateData.value) : {},
47
- numMinted
57
+ numMinted,
58
+ ...limit !== void 0 ? { limit } : {}
48
59
  };
49
60
  };
50
61
  /**
@@ -63,7 +74,7 @@ const renderMintAsset = ({ factory, inputs, owner, issuer }) => {
63
74
  issuer,
64
75
  data
65
76
  }, null, 2));
66
- return JSON.parse(mustache.render(JSON.stringify(output), {
77
+ return enforceProtectedAssetFields(JSON.parse(mustache.render(JSON.stringify(output), {
67
78
  input: inputs,
68
79
  data: data?.value || data,
69
80
  ctx: {
@@ -72,7 +83,7 @@ const renderMintAsset = ({ factory, inputs, owner, issuer }) => {
72
83
  owner,
73
84
  issuer
74
85
  }
75
- }));
86
+ })), output, factoryAddress);
76
87
  };
77
88
  /**
78
89
  * Build the rendered asset + resolved issuer + resolved variables for a
@@ -150,7 +161,7 @@ const preparePreMintAsset = async ({ factory, inputs, owner, issuer }) => {
150
161
  };
151
162
  extra.signature = signature;
152
163
  try {
153
- asset = render(structuredClone(output));
164
+ asset = enforceProtectedAssetFields(render(structuredClone(output)), output, factoryAddress);
154
165
  vcObj = getPath(asset, vcRootPath);
155
166
  debug("preparePreMintAsset.result", JSON.stringify(asset, null, 2));
156
167
  await verify({
@@ -165,7 +176,7 @@ const preparePreMintAsset = async ({ factory, inputs, owner, issuer }) => {
165
176
  }
166
177
  }
167
178
  } else {
168
- asset = render(template);
179
+ asset = enforceProtectedAssetFields(render(template), output, factoryAddress);
169
180
  debug("preparePreMintAsset.result", JSON.stringify(asset, null, 2));
170
181
  }
171
182
  return {
package/esm/validate.mjs CHANGED
@@ -37,7 +37,8 @@ const isValidFactory = (props) => {
37
37
  if (uniq(value.input.assets).length !== value.input.assets.length) throw new Error("Factory asset input should not contains duplicate address");
38
38
  try {
39
39
  const template = JSON.stringify(value.output, null, 2);
40
- if (mustache.parse(template).filter(([type]) => type === "name").some(([, notation]) => isValidNotation(notation) === false)) throw new Error("Invalid tags found in the output template");
40
+ const result = mustache.parse(template);
41
+ if (result.some(([type]) => type !== "text" && type !== "name") || result.filter(([type]) => type === "name").some(([, notation]) => isValidNotation(notation) === false)) throw new Error("Invalid tags found in the output template");
41
42
  } catch (_err) {
42
43
  throw new Error("Factory output should be a valid mustache template when serialized as json");
43
44
  }
@@ -47,7 +48,7 @@ const isValidFactory = (props) => {
47
48
  if (invalidHook) throw new Error(`Factory hook ${invalidHook.name} is invalid`);
48
49
  }
49
50
  if (value.settlement === "instant") {
50
- if (quota.value <= 0 && Object.keys(quota.tokens).every((x) => quota[x] <= 0)) return true;
51
+ if (quota.value <= 0 && Object.keys(quota.tokens ?? {}).every((x) => Number(quota.tokens[x]) <= 0)) return true;
51
52
  if (isEmpty(value.hooks)) throw new Error("Factory hooks should not be empty for instant settlement");
52
53
  const mintHook = value.hooks.find((x) => x.name === "mint");
53
54
  if (!mintHook) throw new Error("Factory hook mint should not be empty for instant settlement that consumes token");
@@ -14,6 +14,18 @@ let _arcblock_did_util_cbor = require("@arcblock/did-util/cbor");
14
14
  * parameter — the subpath IS the encoding selector. Chain-side callers that
15
15
  * need dispatch keep using `@ocap/asset/mint/server` or `@ocap/asset`.
16
16
  */
17
+ const mintFromFactory = ({ factory, inputs, owner, issuer }) => {
18
+ const asset = require_mint_shared.renderMintAsset({
19
+ factory,
20
+ inputs,
21
+ owner,
22
+ issuer
23
+ });
24
+ return {
25
+ asset,
26
+ address: (0, _arcblock_did_util_cbor.toAssetAddress)(asset)
27
+ };
28
+ };
17
29
  const preMintFromFactory = async ({ factory, inputs, owner, issuer }) => {
18
30
  const { asset, issuer: issuerObject, variables } = await require_mint_shared.preparePreMintAsset({
19
31
  factory,
@@ -31,4 +43,5 @@ const preMintFromFactory = async ({ factory, inputs, owner, issuer }) => {
31
43
 
32
44
  //#endregion
33
45
  exports.formatFactoryState = require_mint_shared.formatFactoryState;
46
+ exports.mintFromFactory = mintFromFactory;
34
47
  exports.preMintFromFactory = preMintFromFactory;
@@ -1,7 +1,18 @@
1
- import { TInputMap, TIssuer, TPickedFactoryState, TPreMintResult } from "../types.cjs";
1
+ import { TInputMap, TIssuer, TIssuerInput, TMintResult, TPickedFactoryState, TPreMintResult } from "../types.cjs";
2
2
  import { formatFactoryState } from "./shared.cjs";
3
3
 
4
4
  //#region src/mint/client.d.ts
5
+ declare const mintFromFactory: ({
6
+ factory,
7
+ inputs,
8
+ owner,
9
+ issuer
10
+ }: {
11
+ factory: TPickedFactoryState;
12
+ inputs: TInputMap;
13
+ owner: string;
14
+ issuer: TIssuerInput;
15
+ }) => TMintResult;
5
16
  declare const preMintFromFactory: ({
6
17
  factory,
7
18
  inputs,
@@ -14,4 +25,4 @@ declare const preMintFromFactory: ({
14
25
  issuer?: TIssuer;
15
26
  }) => Promise<TPreMintResult>;
16
27
  //#endregion
17
- export { formatFactoryState, preMintFromFactory };
28
+ export { formatFactoryState, mintFromFactory, preMintFromFactory };
@@ -19,6 +19,16 @@ mustache = require_rolldown_runtime.__toESM(mustache);
19
19
  * browser/Worker bundles that stick to CBOR never drag the protobuf runtime
20
20
  * in via the aggregate did-util entry.
21
21
  */
22
+ const enforceProtectedAssetFields = (rendered, output, factoryAddress) => {
23
+ if (!rendered || typeof rendered !== "object") return rendered;
24
+ if (output && typeof output === "object") {
25
+ if (output.readonly !== void 0 && rendered.readonly !== output.readonly) throw new Error("Failed to mint asset from factory: protected field \"readonly\" cannot be overridden");
26
+ if (output.transferrable !== void 0 && rendered.transferrable !== output.transferrable) throw new Error("Failed to mint asset from factory: protected field \"transferrable\" cannot be overridden");
27
+ if (!("owner" in output) && "owner" in rendered) throw new Error("Failed to mint asset from factory: protected field \"owner\" cannot be injected");
28
+ if (output.parent === "{{ctx.factory}}" && factoryAddress && rendered.parent !== factoryAddress) throw new Error("Failed to mint asset from factory: protected field \"parent\" cannot be overridden");
29
+ }
30
+ return rendered;
31
+ };
22
32
  const getPath = (obj, path) => {
23
33
  if (obj == null) return void 0;
24
34
  return path.split(".").reduce((acc, k) => acc == null ? acc : acc[k], obj);
@@ -37,7 +47,7 @@ const setPath = (obj, path, value) => {
37
47
  };
38
48
  const debug$1 = (0, debug.default)("@ocap/asset");
39
49
  const formatFactoryState = (state) => {
40
- const { address, output, data, numMinted } = state;
50
+ const { address, output, data, numMinted, limit } = state;
41
51
  const outputX = structuredClone(output);
42
52
  const outputData = outputX.data;
43
53
  outputData.value = JSON.parse(outputData.value);
@@ -47,7 +57,8 @@ const formatFactoryState = (state) => {
47
57
  address,
48
58
  output: outputX,
49
59
  data: stateData?.value ? JSON.parse(stateData.value) : {},
50
- numMinted
60
+ numMinted,
61
+ ...limit !== void 0 ? { limit } : {}
51
62
  };
52
63
  };
53
64
  /**
@@ -66,7 +77,7 @@ const renderMintAsset = ({ factory, inputs, owner, issuer }) => {
66
77
  issuer,
67
78
  data
68
79
  }, null, 2));
69
- return JSON.parse(mustache.default.render(JSON.stringify(output), {
80
+ return enforceProtectedAssetFields(JSON.parse(mustache.default.render(JSON.stringify(output), {
70
81
  input: inputs,
71
82
  data: data?.value || data,
72
83
  ctx: {
@@ -75,7 +86,7 @@ const renderMintAsset = ({ factory, inputs, owner, issuer }) => {
75
86
  owner,
76
87
  issuer
77
88
  }
78
- }));
89
+ })), output, factoryAddress);
79
90
  };
80
91
  /**
81
92
  * Build the rendered asset + resolved issuer + resolved variables for a
@@ -153,7 +164,7 @@ const preparePreMintAsset = async ({ factory, inputs, owner, issuer }) => {
153
164
  };
154
165
  extra.signature = signature;
155
166
  try {
156
- asset = render(structuredClone(output));
167
+ asset = enforceProtectedAssetFields(render(structuredClone(output)), output, factoryAddress);
157
168
  vcObj = getPath(asset, vcRootPath);
158
169
  debug$1("preparePreMintAsset.result", JSON.stringify(asset, null, 2));
159
170
  await (0, _arcblock_vc.verify)({
@@ -168,7 +179,7 @@ const preparePreMintAsset = async ({ factory, inputs, owner, issuer }) => {
168
179
  }
169
180
  }
170
181
  } else {
171
- asset = render(template);
182
+ asset = enforceProtectedAssetFields(render(template), output, factoryAddress);
172
183
  debug$1("preparePreMintAsset.result", JSON.stringify(asset, null, 2));
173
184
  }
174
185
  return {
package/lib/validate.cjs CHANGED
@@ -43,7 +43,8 @@ const isValidFactory = (props) => {
43
43
  if ((0, lodash_uniq.default)(value.input.assets).length !== value.input.assets.length) throw new Error("Factory asset input should not contains duplicate address");
44
44
  try {
45
45
  const template = JSON.stringify(value.output, null, 2);
46
- if (mustache.default.parse(template).filter(([type]) => type === "name").some(([, notation]) => require_utils.isValidNotation(notation) === false)) throw new Error("Invalid tags found in the output template");
46
+ const result = mustache.default.parse(template);
47
+ if (result.some(([type]) => type !== "text" && type !== "name") || result.filter(([type]) => type === "name").some(([, notation]) => require_utils.isValidNotation(notation) === false)) throw new Error("Invalid tags found in the output template");
47
48
  } catch (_err) {
48
49
  throw new Error("Factory output should be a valid mustache template when serialized as json");
49
50
  }
@@ -53,7 +54,7 @@ const isValidFactory = (props) => {
53
54
  if (invalidHook) throw new Error(`Factory hook ${invalidHook.name} is invalid`);
54
55
  }
55
56
  if (value.settlement === "instant") {
56
- if (quota.value <= 0 && Object.keys(quota.tokens).every((x) => quota[x] <= 0)) return true;
57
+ if (quota.value <= 0 && Object.keys(quota.tokens ?? {}).every((x) => Number(quota.tokens[x]) <= 0)) return true;
57
58
  if ((0, lodash_isEmpty.default)(value.hooks)) throw new Error("Factory hooks should not be empty for instant settlement");
58
59
  const mintHook = value.hooks.find((x) => x.name === "mint");
59
60
  if (!mintHook) throw new Error("Factory hook mint should not be empty for instant settlement that consumes token");
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@ocap/asset",
3
3
  "description": "Utility to work with asset and factory on ArcBlock blockchain",
4
4
  "type": "module",
5
- "version": "1.30.23",
5
+ "version": "1.30.25",
6
6
  "author": {
7
7
  "name": "wangshijun",
8
8
  "email": "shijun@arcblock.io",
@@ -19,15 +19,15 @@
19
19
  "wangshijun <shijun@arcblock.io> (https://github.com/wangshijun)"
20
20
  ],
21
21
  "dependencies": {
22
- "@arcblock/did": "1.30.23",
23
- "@arcblock/did-util": "1.30.23",
24
- "@arcblock/validator": "1.30.23",
25
- "@arcblock/vc": "1.30.23",
26
- "@ocap/contract": "1.30.23",
27
- "@ocap/mcrypto": "1.30.23",
28
- "@ocap/types": "1.30.23",
29
- "@ocap/util": "1.30.23",
30
- "@ocap/wallet": "1.30.23",
22
+ "@arcblock/did": "1.30.25",
23
+ "@arcblock/did-util": "1.30.25",
24
+ "@arcblock/validator": "1.30.25",
25
+ "@arcblock/vc": "1.30.25",
26
+ "@ocap/contract": "1.30.25",
27
+ "@ocap/mcrypto": "1.30.25",
28
+ "@ocap/types": "1.30.25",
29
+ "@ocap/util": "1.30.25",
30
+ "@ocap/wallet": "1.30.25",
31
31
  "debug": "^4.4.3",
32
32
  "flat": "^5.0.2",
33
33
  "is-absolute-url": "^3.0.3",