@ocap/asset 1.30.3 → 1.30.5

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/lib/index.d.cts CHANGED
@@ -1,87 +1,6 @@
1
- import { TAssetFactoryState, TCreateAssetTx } from "@ocap/types";
2
- import { WalletObject } from "@ocap/wallet";
3
- import { LiteralUnion, PartialDeep } from "type-fest";
4
-
5
- //#region src/index.d.ts
6
- type $TSFixMe = any;
7
- type TPickedFactoryState = PartialDeep<TAssetFactoryState>;
8
- type TIssuer = {
9
- name: string;
10
- wallet: WalletObject;
11
- };
12
- type TIssuerInput = {
13
- id: string;
14
- pk: string;
15
- name: string;
16
- };
17
- type THook = {
18
- type: LiteralUnion<'contract' | 'url', string>;
19
- name: LiteralUnion<'mint' | 'postMint' | 'preMint', string>;
20
- hook: string;
21
- };
22
- type TInputMap = {
23
- [key: string]: string;
24
- };
25
- type TMintResult = {
26
- address: string;
27
- asset: TCreateAssetTx;
28
- };
29
- type TPreMintResult = TMintResult & {
30
- variables: TInputMap;
31
- issuer: TIssuerInput;
32
- };
33
- declare const isValidNotation: (notation: string) => boolean;
34
- declare const isValidHook: (hook: THook, quota?: $TSFixMe, throwOnError?: boolean) => boolean;
35
- declare const isValidFactory: (props: any) => boolean;
36
- /**
37
- * Find credentialSubject path in the object
38
- * Because they need prerender
39
- *
40
- * @param {object} obj
41
- * @param {string} keyword
42
- * @return {string} list of keys
43
- */
44
- declare const findPrerenderKeys: (obj: $TSFixMe, keyword: string) => string[];
45
- /**
46
- * Mint from an asset factory, used on server side
47
- *
48
- * @param {object} params { factory, inputs, issuer }
49
- * @param {object} params.factory factory object
50
- * @param {object} params.inputs factory input variables
51
- * @param {string} params.owner owner did for the new asset
52
- * @param {object} params.issuer issuer object
53
- */
54
- declare const mintFromFactory: ({
55
- factory,
56
- inputs,
57
- owner,
58
- issuer
59
- }: {
60
- factory: TPickedFactoryState;
61
- inputs: TInputMap;
62
- owner: string;
63
- issuer: TIssuerInput;
64
- }) => TMintResult;
65
- /**
66
- * Simulate minting from an asset factory, used for client side
67
- *
68
- * @param {object} params { factory, inputs, issuer }
69
- * @param {object} params.factory factory object
70
- * @param {object} params.inputs factory input variables
71
- * @param {string} params.owner owner did for the new asset
72
- * @param {object} params.issuer factory issuer wallet and name
73
- */
74
- declare const preMintFromFactory: ({
75
- factory,
76
- inputs,
77
- owner,
78
- issuer
79
- }: {
80
- factory?: TPickedFactoryState;
81
- inputs: TInputMap;
82
- owner?: string;
83
- issuer?: TIssuer;
84
- }) => Promise<TPreMintResult>;
85
- declare const formatFactoryState: (state: TAssetFactoryState) => TPickedFactoryState;
86
- //#endregion
87
- export { THook, TInputMap, TIssuer, TIssuerInput, TMintResult, TPickedFactoryState, TPreMintResult, findPrerenderKeys, formatFactoryState, isValidFactory, isValidHook, isValidNotation, mintFromFactory, preMintFromFactory };
1
+ import { DecodedAny, THook, TInputMap, TIssuer, TIssuerInput, TMintResult, TPickedFactoryState, TPreMintResult } from "./types.cjs";
2
+ import { findPrerenderKeys, isValidNotation } from "./utils.cjs";
3
+ import { formatFactoryState } from "./mint/shared.cjs";
4
+ import { mintFromFactory, preMintFromFactory } from "./mint/server.cjs";
5
+ import { isValidFactory, isValidHook } from "./validate.cjs";
6
+ export { type DecodedAny, type THook, type TInputMap, type TIssuer, type TIssuerInput, type TMintResult, type TPickedFactoryState, type TPreMintResult, findPrerenderKeys, formatFactoryState, isValidFactory, isValidHook, isValidNotation, mintFromFactory, preMintFromFactory };
@@ -0,0 +1,34 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const require_mint_shared = require('./shared.cjs');
3
+ let _arcblock_did_util_cbor = require("@arcblock/did-util/cbor");
4
+
5
+ //#region src/mint/client.ts
6
+ /**
7
+ * `@ocap/asset/mint/client` — CBOR-only mint helpers.
8
+ *
9
+ * Pulls `toAssetAddress` from `@arcblock/did-util/cbor` so browser/Worker
10
+ * bundles that stick to CBOR never drag `google-protobuf` or any `*_pb.js`
11
+ * in through the aggregate did-util entry.
12
+ *
13
+ * The client-facing `preMintFromFactory` drops the legacy `encoding`
14
+ * parameter — the subpath IS the encoding selector. Chain-side callers that
15
+ * need dispatch keep using `@ocap/asset/mint/server` or `@ocap/asset`.
16
+ */
17
+ const preMintFromFactory = async ({ factory, inputs, owner, issuer }) => {
18
+ const { asset, issuer: issuerObject, variables } = await require_mint_shared.preparePreMintAsset({
19
+ factory,
20
+ inputs,
21
+ owner,
22
+ issuer
23
+ });
24
+ return {
25
+ address: (0, _arcblock_did_util_cbor.toAssetAddress)(asset),
26
+ issuer: issuerObject,
27
+ variables,
28
+ asset
29
+ };
30
+ };
31
+
32
+ //#endregion
33
+ exports.formatFactoryState = require_mint_shared.formatFactoryState;
34
+ exports.preMintFromFactory = preMintFromFactory;
@@ -0,0 +1,17 @@
1
+ import { TInputMap, TIssuer, TPickedFactoryState, TPreMintResult } from "../types.cjs";
2
+ import { formatFactoryState } from "./shared.cjs";
3
+
4
+ //#region src/mint/client.d.ts
5
+ declare const preMintFromFactory: ({
6
+ factory,
7
+ inputs,
8
+ owner,
9
+ issuer
10
+ }: {
11
+ factory?: TPickedFactoryState;
12
+ inputs: TInputMap;
13
+ owner?: string;
14
+ issuer?: TIssuer;
15
+ }) => Promise<TPreMintResult>;
16
+ //#endregion
17
+ export { formatFactoryState, preMintFromFactory };
@@ -0,0 +1,53 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const require_mint_shared = require('./shared.cjs');
3
+ let debug = require("debug");
4
+ debug = require_rolldown_runtime.__toESM(debug);
5
+ let _arcblock_did_util = require("@arcblock/did-util");
6
+
7
+ //#region src/mint/server.ts
8
+ /**
9
+ * `@ocap/asset/mint/server` — dual-encoding mint helpers for chain-side use.
10
+ *
11
+ * Pulls `toAssetAddress` from the aggregate `@arcblock/did-util` entry, which
12
+ * dispatches on the explicit `encoding` argument. Server pipes read
13
+ * `context.txEncoding` (filled by the DecodeTx pipe's tag-55799 check) and
14
+ * pass it in.
15
+ */
16
+ const debug$1 = (0, debug.default)("@ocap/asset");
17
+ const mintFromFactory = ({ factory, inputs, owner, issuer, encoding }) => {
18
+ const asset = require_mint_shared.renderMintAsset({
19
+ factory,
20
+ inputs,
21
+ owner,
22
+ issuer
23
+ });
24
+ const address = (0, _arcblock_did_util.toAssetAddress)(asset, encoding);
25
+ debug$1("mintFromFactory.result", JSON.stringify({
26
+ asset,
27
+ address,
28
+ encoding
29
+ }, null, 2));
30
+ return {
31
+ asset,
32
+ address
33
+ };
34
+ };
35
+ const preMintFromFactory = async ({ factory, inputs, owner, issuer, encoding }) => {
36
+ const { asset, issuer: issuerObject, variables } = await require_mint_shared.preparePreMintAsset({
37
+ factory,
38
+ inputs,
39
+ owner,
40
+ issuer
41
+ });
42
+ return {
43
+ address: (0, _arcblock_did_util.toAssetAddress)(asset, encoding),
44
+ issuer: issuerObject,
45
+ variables,
46
+ asset
47
+ };
48
+ };
49
+
50
+ //#endregion
51
+ exports.formatFactoryState = require_mint_shared.formatFactoryState;
52
+ exports.mintFromFactory = mintFromFactory;
53
+ exports.preMintFromFactory = preMintFromFactory;
@@ -0,0 +1,34 @@
1
+ import { TInputMap, TIssuer, TIssuerInput, TMintResult, TPickedFactoryState, TPreMintResult } from "../types.cjs";
2
+ import { formatFactoryState } from "./shared.cjs";
3
+ import { ItxEncoding } from "@arcblock/did-util";
4
+
5
+ //#region src/mint/server.d.ts
6
+
7
+ declare const mintFromFactory: ({
8
+ factory,
9
+ inputs,
10
+ owner,
11
+ issuer,
12
+ encoding
13
+ }: {
14
+ factory: TPickedFactoryState;
15
+ inputs: TInputMap;
16
+ owner: string;
17
+ issuer: TIssuerInput;
18
+ encoding: ItxEncoding;
19
+ }) => TMintResult;
20
+ declare const preMintFromFactory: ({
21
+ factory,
22
+ inputs,
23
+ owner,
24
+ issuer,
25
+ encoding
26
+ }: {
27
+ factory?: TPickedFactoryState;
28
+ inputs: TInputMap;
29
+ owner?: string;
30
+ issuer?: TIssuer;
31
+ encoding: ItxEncoding;
32
+ }) => Promise<TPreMintResult>;
33
+ //#endregion
34
+ export { formatFactoryState, mintFromFactory, preMintFromFactory };
@@ -0,0 +1,187 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const require_utils = require('../utils.cjs');
3
+ let _arcblock_did = require("@arcblock/did");
4
+ let _arcblock_vc = require("@arcblock/vc");
5
+ let _ocap_mcrypto = require("@ocap/mcrypto");
6
+ let _ocap_util = require("@ocap/util");
7
+ let debug = require("debug");
8
+ debug = require_rolldown_runtime.__toESM(debug);
9
+ let mustache = require("mustache");
10
+ mustache = require_rolldown_runtime.__toESM(mustache);
11
+
12
+ //#region src/mint/shared.ts
13
+ /**
14
+ * Encoding-agnostic mint helpers shared by `./client` (CBOR-only) and
15
+ * `./server` (dispatching) entries.
16
+ *
17
+ * Everything here is pure logic — no `@arcblock/did-util` import. The final
18
+ * `toAssetAddress(asset, encoding?)` call is deferred to each entry so
19
+ * browser/Worker bundles that stick to CBOR never drag the protobuf runtime
20
+ * in via the aggregate did-util entry.
21
+ */
22
+ const getPath = (obj, path) => {
23
+ if (obj == null) return void 0;
24
+ return path.split(".").reduce((acc, k) => acc == null ? acc : acc[k], obj);
25
+ };
26
+ const setPath = (obj, path, value) => {
27
+ if (obj == null) return obj;
28
+ const parts = path.split(".");
29
+ let current = obj;
30
+ for (let i = 0; i < parts.length - 1; i++) {
31
+ const part = parts[i];
32
+ if (current[part] == null || typeof current[part] !== "object") current[part] = {};
33
+ current = current[part];
34
+ }
35
+ current[parts[parts.length - 1]] = value;
36
+ return obj;
37
+ };
38
+ const debug$1 = (0, debug.default)("@ocap/asset");
39
+ const formatFactoryState = (state) => {
40
+ const { address, output, data, numMinted } = state;
41
+ const outputX = structuredClone(output);
42
+ const outputData = outputX.data;
43
+ outputData.value = JSON.parse(outputData.value);
44
+ outputData.type = outputData.typeUrl;
45
+ const stateData = data;
46
+ return {
47
+ address,
48
+ output: outputX,
49
+ data: stateData?.value ? JSON.parse(stateData.value) : {},
50
+ numMinted
51
+ };
52
+ };
53
+ /**
54
+ * Render the asset payload for `mintFromFactory`. Pure sync template expand —
55
+ * no VC signing, no async calls. Callers compute the address via the
56
+ * encoding-appropriate `toAssetAddress`.
57
+ */
58
+ const renderMintAsset = ({ factory, inputs, owner, issuer }) => {
59
+ const { output, address: factoryAddress, numMinted, data } = factory;
60
+ debug$1("renderMintAsset.args", JSON.stringify({
61
+ output,
62
+ factoryAddress,
63
+ numMinted,
64
+ inputs,
65
+ owner,
66
+ issuer,
67
+ data
68
+ }, null, 2));
69
+ return JSON.parse(mustache.default.render(JSON.stringify(output), {
70
+ input: inputs,
71
+ data: data?.value || data,
72
+ ctx: {
73
+ factory: factoryAddress,
74
+ id: (numMinted ?? 0) + 1,
75
+ owner,
76
+ issuer
77
+ }
78
+ }));
79
+ };
80
+ /**
81
+ * Build the rendered asset + resolved issuer + resolved variables for a
82
+ * pre-mint flow. Handles credentialSubject prerender + VC proof signing.
83
+ *
84
+ * Callers compute the final address via the encoding-appropriate
85
+ * `toAssetAddress`. Matches the original `preMintFromFactory` behavior one
86
+ * for one — the only factored-out line is the final address derivation.
87
+ */
88
+ const preparePreMintAsset = async ({ factory, inputs, owner, issuer }) => {
89
+ if (Object.keys(inputs).some((x) => typeof inputs[x] !== "string")) throw new Error("Failed to mint asset from factory: input values must be strings");
90
+ if (!factory) throw new Error("Failed to mint asset from factory: factory is required");
91
+ if (!issuer) throw new Error("Failed to mint asset from factory: issuer is required");
92
+ let asset = null;
93
+ const { output, numMinted, address: factoryAddress, data } = factory;
94
+ const { wallet, name } = issuer;
95
+ debug$1("preparePreMintAsset.args", JSON.stringify({
96
+ output,
97
+ factoryAddress,
98
+ numMinted,
99
+ inputs,
100
+ owner,
101
+ issuer,
102
+ data
103
+ }, null, 2));
104
+ const extra = {};
105
+ const issuerObject = {
106
+ id: wallet.address,
107
+ pk: (0, _ocap_util.toBase58)(wallet.publicKey),
108
+ name
109
+ };
110
+ const render = (templateObject) => JSON.parse(mustache.default.render(JSON.stringify(templateObject), {
111
+ input: {
112
+ ...inputs,
113
+ ...extra
114
+ },
115
+ data: data?.value || data,
116
+ ctx: {
117
+ factory: factoryAddress,
118
+ id: (numMinted ?? 0) + 1,
119
+ owner,
120
+ issuer: issuerObject
121
+ }
122
+ }));
123
+ const template = structuredClone(output);
124
+ const prerenderKeys = require_utils.findPrerenderKeys(template, "credentialSubject");
125
+ if (prerenderKeys.length) {
126
+ extra.issuanceDate = (/* @__PURE__ */ new Date()).toISOString();
127
+ for (const key of prerenderKeys) {
128
+ const subjectTemplate = getPath(template, key);
129
+ const subjectObject = Array.isArray(subjectTemplate) ? subjectTemplate.map((x) => render(x)) : render(subjectTemplate);
130
+ setPath(template, key, subjectObject);
131
+ const vcRootPath = key.split(".").slice(0, -1).join(".");
132
+ const vcIdPath = vcRootPath.split(".").concat(["id"]).join(".");
133
+ const typeInfo = (0, _arcblock_did.toTypeInfo)(issuerObject.id);
134
+ const pkType = typeInfo.pk;
135
+ const vcType = {
136
+ ...typeInfo,
137
+ role: _ocap_mcrypto.types.RoleType.ROLE_VC
138
+ };
139
+ const vcId = (0, _arcblock_did.fromPublicKeyHash)(wallet.hash((0, _arcblock_vc.stableStringify)(subjectObject)), vcType);
140
+ extra.id = vcId;
141
+ extra.proofType = _arcblock_vc.proofTypes[pkType];
142
+ setPath(template, vcIdPath, vcId);
143
+ if (!_arcblock_vc.proofTypes[pkType]) throw new Error("Unsupported signer type when create verifiable credential");
144
+ let vcObj = render(getPath(template, vcRootPath));
145
+ vcObj.proof = void 0;
146
+ const vcStr = (0, _arcblock_vc.stableStringify)(vcObj);
147
+ const signature = (0, _ocap_util.toBase64)(await wallet.sign(vcStr));
148
+ vcObj.proof = {
149
+ type: _arcblock_vc.proofTypes[pkType],
150
+ created: extra.issuanceDate,
151
+ proofPurpose: "assertionMethod",
152
+ jws: signature
153
+ };
154
+ extra.signature = signature;
155
+ try {
156
+ asset = render(structuredClone(output));
157
+ vcObj = getPath(asset, vcRootPath);
158
+ debug$1("preparePreMintAsset.result", JSON.stringify(asset, null, 2));
159
+ await (0, _arcblock_vc.verify)({
160
+ vc: vcObj,
161
+ trustedIssuers: [issuerObject.id],
162
+ ownerDid: owner,
163
+ ignoreExpired: true
164
+ });
165
+ } catch (err) {
166
+ console.error(err);
167
+ throw new Error("Failed to mint asset from factory: invalid verifiable credential minted");
168
+ }
169
+ }
170
+ } else {
171
+ asset = render(template);
172
+ debug$1("preparePreMintAsset.result", JSON.stringify(asset, null, 2));
173
+ }
174
+ return {
175
+ asset,
176
+ issuer: issuerObject,
177
+ variables: {
178
+ ...inputs,
179
+ ...extra
180
+ }
181
+ };
182
+ };
183
+
184
+ //#endregion
185
+ exports.formatFactoryState = formatFactoryState;
186
+ exports.preparePreMintAsset = preparePreMintAsset;
187
+ exports.renderMintAsset = renderMintAsset;
@@ -0,0 +1,47 @@
1
+ import { TInputMap, TIssuer, TIssuerInput, TPickedFactoryState } from "../types.cjs";
2
+ import { TAssetFactoryState, TCreateAssetTx } from "@ocap/types";
3
+
4
+ //#region src/mint/shared.d.ts
5
+ type $TSFixMe = any;
6
+ declare const formatFactoryState: (state: TAssetFactoryState) => TPickedFactoryState;
7
+ /**
8
+ * Render the asset payload for `mintFromFactory`. Pure sync template expand —
9
+ * no VC signing, no async calls. Callers compute the address via the
10
+ * encoding-appropriate `toAssetAddress`.
11
+ */
12
+ declare const renderMintAsset: ({
13
+ factory,
14
+ inputs,
15
+ owner,
16
+ issuer
17
+ }: {
18
+ factory: TPickedFactoryState;
19
+ inputs: TInputMap;
20
+ owner: string;
21
+ issuer: TIssuerInput;
22
+ }) => TCreateAssetTx;
23
+ /**
24
+ * Build the rendered asset + resolved issuer + resolved variables for a
25
+ * pre-mint flow. Handles credentialSubject prerender + VC proof signing.
26
+ *
27
+ * Callers compute the final address via the encoding-appropriate
28
+ * `toAssetAddress`. Matches the original `preMintFromFactory` behavior one
29
+ * for one — the only factored-out line is the final address derivation.
30
+ */
31
+ declare const preparePreMintAsset: ({
32
+ factory,
33
+ inputs,
34
+ owner,
35
+ issuer
36
+ }: {
37
+ factory?: TPickedFactoryState;
38
+ inputs: TInputMap;
39
+ owner?: string;
40
+ issuer?: TIssuer;
41
+ }) => Promise<{
42
+ asset: $TSFixMe;
43
+ issuer: TIssuerInput;
44
+ variables: TInputMap;
45
+ }>;
46
+ //#endregion
47
+ export { formatFactoryState, preparePreMintAsset, renderMintAsset };
package/lib/mint.cjs ADDED
@@ -0,0 +1,6 @@
1
+ const require_mint_shared = require('./mint/shared.cjs');
2
+ const require_mint_server = require('./mint/server.cjs');
3
+
4
+ exports.formatFactoryState = require_mint_shared.formatFactoryState;
5
+ exports.mintFromFactory = require_mint_server.mintFromFactory;
6
+ exports.preMintFromFactory = require_mint_server.preMintFromFactory;
package/lib/mint.d.cts ADDED
@@ -0,0 +1,3 @@
1
+ import { formatFactoryState } from "./mint/shared.cjs";
2
+ import { mintFromFactory, preMintFromFactory } from "./mint/server.cjs";
3
+ export { formatFactoryState, mintFromFactory, preMintFromFactory };
package/lib/types.cjs ADDED
File without changes
@@ -0,0 +1,39 @@
1
+ import { TAssetFactoryState, TCreateAssetTx } from "@ocap/types";
2
+ import { WalletObject } from "@ocap/wallet";
3
+ import { LiteralUnion, PartialDeep } from "type-fest";
4
+
5
+ //#region src/types.d.ts
6
+ type TPickedFactoryState = PartialDeep<TAssetFactoryState>;
7
+ type TIssuer = {
8
+ name: string;
9
+ wallet: WalletObject;
10
+ };
11
+ type TIssuerInput = {
12
+ id: string;
13
+ pk: string;
14
+ name: string;
15
+ };
16
+ type THook = {
17
+ type: LiteralUnion<'contract' | 'url', string>;
18
+ name: LiteralUnion<'mint' | 'postMint' | 'preMint', string>;
19
+ hook: string;
20
+ };
21
+ type TInputMap = {
22
+ [key: string]: string;
23
+ };
24
+ type TMintResult = {
25
+ address: string;
26
+ asset: TCreateAssetTx;
27
+ };
28
+ type TPreMintResult = TMintResult & {
29
+ variables: TInputMap;
30
+ issuer: TIssuerInput;
31
+ };
32
+ /** Decoded Any type from protobuf with value property */
33
+ interface DecodedAny {
34
+ typeUrl?: string;
35
+ type?: string;
36
+ value?: string;
37
+ }
38
+ //#endregion
39
+ export { DecodedAny, THook, TInputMap, TIssuer, TIssuerInput, TMintResult, TPickedFactoryState, TPreMintResult };
package/lib/utils.cjs ADDED
@@ -0,0 +1,27 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ let flat = require("flat");
3
+ flat = require_rolldown_runtime.__toESM(flat);
4
+
5
+ //#region src/utils.ts
6
+ const isValidNotation = (notation) => [
7
+ "ctx",
8
+ "data",
9
+ "input"
10
+ ].includes(notation.split(".").shift() ?? "");
11
+ /**
12
+ * Find credentialSubject path in the object
13
+ * Because they need prerender
14
+ *
15
+ * @param {object} obj
16
+ * @param {string} keyword
17
+ * @return {string} list of keys
18
+ */
19
+ const findPrerenderKeys = (obj, keyword) => {
20
+ const flatObj = (0, flat.default)(obj, { safe: true });
21
+ const keys = Object.keys(flatObj).map((x) => x.split(".")).filter((x) => x.includes(keyword)).map((x) => x.slice(0, x.lastIndexOf(keyword) + 1)).sort((a, b) => b.length - a.length).map((x) => x.join("."));
22
+ return Array.from(new Set(keys));
23
+ };
24
+
25
+ //#endregion
26
+ exports.findPrerenderKeys = findPrerenderKeys;
27
+ exports.isValidNotation = isValidNotation;
@@ -0,0 +1,13 @@
1
+ //#region src/utils.d.ts
2
+ declare const isValidNotation: (notation: string) => boolean;
3
+ /**
4
+ * Find credentialSubject path in the object
5
+ * Because they need prerender
6
+ *
7
+ * @param {object} obj
8
+ * @param {string} keyword
9
+ * @return {string} list of keys
10
+ */
11
+ declare const findPrerenderKeys: (obj: any, keyword: string) => string[];
12
+ //#endregion
13
+ export { findPrerenderKeys, isValidNotation };
@@ -0,0 +1,71 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ const require_utils = require('./utils.cjs');
3
+ let mustache = require("mustache");
4
+ mustache = require_rolldown_runtime.__toESM(mustache);
5
+ let _arcblock_validator = require("@arcblock/validator");
6
+ let _ocap_contract = require("@ocap/contract");
7
+ let is_absolute_url = require("is-absolute-url");
8
+ is_absolute_url = require_rolldown_runtime.__toESM(is_absolute_url);
9
+ let lodash_isEmpty = require("lodash/isEmpty");
10
+ lodash_isEmpty = require_rolldown_runtime.__toESM(lodash_isEmpty);
11
+ let lodash_uniq = require("lodash/uniq");
12
+ lodash_uniq = require_rolldown_runtime.__toESM(lodash_uniq);
13
+ let lodash_uniqBy = require("lodash/uniqBy");
14
+ lodash_uniqBy = require_rolldown_runtime.__toESM(lodash_uniqBy);
15
+
16
+ //#region src/validate.ts
17
+ const SUPPORTED_HOOK_NAMES = [
18
+ "preMint",
19
+ "mint",
20
+ "postMint"
21
+ ];
22
+ const SUPPORTED_HOOK_TYPES = ["contract", "url"];
23
+ const isValidHook = (hook, quota, throwOnError = false) => {
24
+ if (SUPPORTED_HOOK_TYPES.includes(hook.type) === false) return false;
25
+ if (SUPPORTED_HOOK_NAMES.includes(hook.name) === false) return false;
26
+ if (hook.type === "url") return (0, is_absolute_url.default)(hook.hook);
27
+ if (hook.type === "contract") try {
28
+ (0, _ocap_contract.validate)((0, _ocap_contract.compile)(hook.hook), quota);
29
+ return true;
30
+ } catch (err) {
31
+ if (process.env.NODE_ENV !== "test") console.error("invalid contract hook", err.message);
32
+ if (throwOnError) throw new Error(`Factory hook ${hook.name} is invalid: ${err.message}`);
33
+ return false;
34
+ }
35
+ return false;
36
+ };
37
+ const isValidFactory = (props) => {
38
+ if (!props) throw new Error("Factory props should not be empty");
39
+ const { value, error } = (0, _arcblock_validator.vValidate)(_arcblock_validator.schemas.factorySchema, props);
40
+ if (error) throw new Error(`Invalid factory: ${error.details.map((x) => x.message).join(", ")}`);
41
+ if (["tokens", "assets"].every((x) => (0, lodash_isEmpty.default)(value.input[x])) && value.input.value <= 0) throw new Error("Factory input should contain at least one token or asset");
42
+ if ((0, lodash_uniqBy.default)(value.input.tokens, "address").length !== value.input.tokens.length) throw new Error("Factory token input should not contains duplicate address");
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
+ try {
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");
47
+ } catch (_err) {
48
+ throw new Error("Factory output should be a valid mustache template when serialized as json");
49
+ }
50
+ const quota = (0, _ocap_contract.getQuota)(value.input);
51
+ if (Array.isArray(value.hooks)) {
52
+ const invalidHook = value.hooks.find((x) => isValidHook(x, quota, true) === false);
53
+ if (invalidHook) throw new Error(`Factory hook ${invalidHook.name} is invalid`);
54
+ }
55
+ if (value.settlement === "instant") {
56
+ if (quota.value <= 0 && Object.keys(quota.tokens).every((x) => quota[x] <= 0)) return true;
57
+ if ((0, lodash_isEmpty.default)(value.hooks)) throw new Error("Factory hooks should not be empty for instant settlement");
58
+ const mintHook = value.hooks.find((x) => x.name === "mint");
59
+ if (!mintHook) throw new Error("Factory hook mint should not be empty for instant settlement that consumes token");
60
+ try {
61
+ (0, _ocap_contract.validate)((0, _ocap_contract.compile)(mintHook.hook), quota, true);
62
+ } catch (err) {
63
+ throw new Error(`Factory hook mint is invalid: ${err.message}`);
64
+ }
65
+ }
66
+ return true;
67
+ };
68
+
69
+ //#endregion
70
+ exports.isValidFactory = isValidFactory;
71
+ exports.isValidHook = isValidHook;
@@ -0,0 +1,8 @@
1
+ import { THook } from "./types.cjs";
2
+
3
+ //#region src/validate.d.ts
4
+ type $TSFixMe = any;
5
+ declare const isValidHook: (hook: THook, quota?: $TSFixMe, throwOnError?: boolean) => boolean;
6
+ declare const isValidFactory: (props: any) => boolean;
7
+ //#endregion
8
+ export { isValidFactory, isValidHook };