@ocap/asset 1.30.2 → 1.30.4
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/index.d.mts +7 -87
- package/esm/index.mjs +4 -248
- package/esm/mint/client.d.mts +17 -0
- package/esm/mint/client.mjs +32 -0
- package/esm/mint/server.d.mts +34 -0
- package/esm/mint/server.mjs +49 -0
- package/esm/mint/shared.d.mts +47 -0
- package/esm/mint/shared.mjs +182 -0
- package/esm/mint.d.mts +3 -0
- package/esm/mint.mjs +4 -0
- package/esm/types.d.mts +39 -0
- package/esm/types.mjs +1 -0
- package/esm/utils.d.mts +13 -0
- package/esm/utils.mjs +24 -0
- package/esm/validate.d.mts +8 -0
- package/esm/validate.mjs +64 -0
- package/lib/index.cjs +11 -266
- package/lib/index.d.cts +6 -87
- package/lib/mint/client.cjs +34 -0
- package/lib/mint/client.d.cts +17 -0
- package/lib/mint/server.cjs +53 -0
- package/lib/mint/server.d.cts +34 -0
- package/lib/mint/shared.cjs +187 -0
- package/lib/mint/shared.d.cts +47 -0
- package/lib/mint.cjs +6 -0
- package/lib/mint.d.cts +3 -0
- package/lib/types.cjs +0 -0
- package/lib/types.d.cts +39 -0
- package/lib/utils.cjs +27 -0
- package/lib/utils.d.cts +13 -0
- package/lib/validate.cjs +71 -0
- package/lib/validate.d.cts +8 -0
- package/package.json +45 -10
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { findPrerenderKeys } from "../utils.mjs";
|
|
2
|
+
import { fromPublicKeyHash, toTypeInfo } from "@arcblock/did";
|
|
3
|
+
import { proofTypes, stableStringify, verify } from "@arcblock/vc";
|
|
4
|
+
import { types } from "@ocap/mcrypto";
|
|
5
|
+
import { toBase58, toBase64 } from "@ocap/util";
|
|
6
|
+
import Debug from "debug";
|
|
7
|
+
import mustache from "mustache";
|
|
8
|
+
|
|
9
|
+
//#region src/mint/shared.ts
|
|
10
|
+
/**
|
|
11
|
+
* Encoding-agnostic mint helpers shared by `./client` (CBOR-only) and
|
|
12
|
+
* `./server` (dispatching) entries.
|
|
13
|
+
*
|
|
14
|
+
* Everything here is pure logic — no `@arcblock/did-util` import. The final
|
|
15
|
+
* `toAssetAddress(asset, encoding?)` call is deferred to each entry so
|
|
16
|
+
* browser/Worker bundles that stick to CBOR never drag the protobuf runtime
|
|
17
|
+
* in via the aggregate did-util entry.
|
|
18
|
+
*/
|
|
19
|
+
const getPath = (obj, path) => {
|
|
20
|
+
if (obj == null) return void 0;
|
|
21
|
+
return path.split(".").reduce((acc, k) => acc == null ? acc : acc[k], obj);
|
|
22
|
+
};
|
|
23
|
+
const setPath = (obj, path, value) => {
|
|
24
|
+
if (obj == null) return obj;
|
|
25
|
+
const parts = path.split(".");
|
|
26
|
+
let current = obj;
|
|
27
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
28
|
+
const part = parts[i];
|
|
29
|
+
if (current[part] == null || typeof current[part] !== "object") current[part] = {};
|
|
30
|
+
current = current[part];
|
|
31
|
+
}
|
|
32
|
+
current[parts[parts.length - 1]] = value;
|
|
33
|
+
return obj;
|
|
34
|
+
};
|
|
35
|
+
const debug = Debug("@ocap/asset");
|
|
36
|
+
const formatFactoryState = (state) => {
|
|
37
|
+
const { address, output, data, numMinted } = state;
|
|
38
|
+
const outputX = structuredClone(output);
|
|
39
|
+
const outputData = outputX.data;
|
|
40
|
+
outputData.value = JSON.parse(outputData.value);
|
|
41
|
+
outputData.type = outputData.typeUrl;
|
|
42
|
+
const stateData = data;
|
|
43
|
+
return {
|
|
44
|
+
address,
|
|
45
|
+
output: outputX,
|
|
46
|
+
data: stateData?.value ? JSON.parse(stateData.value) : {},
|
|
47
|
+
numMinted
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Render the asset payload for `mintFromFactory`. Pure sync template expand —
|
|
52
|
+
* no VC signing, no async calls. Callers compute the address via the
|
|
53
|
+
* encoding-appropriate `toAssetAddress`.
|
|
54
|
+
*/
|
|
55
|
+
const renderMintAsset = ({ factory, inputs, owner, issuer }) => {
|
|
56
|
+
const { output, address: factoryAddress, numMinted, data } = factory;
|
|
57
|
+
debug("renderMintAsset.args", JSON.stringify({
|
|
58
|
+
output,
|
|
59
|
+
factoryAddress,
|
|
60
|
+
numMinted,
|
|
61
|
+
inputs,
|
|
62
|
+
owner,
|
|
63
|
+
issuer,
|
|
64
|
+
data
|
|
65
|
+
}, null, 2));
|
|
66
|
+
return JSON.parse(mustache.render(JSON.stringify(output), {
|
|
67
|
+
input: inputs,
|
|
68
|
+
data: data?.value || data,
|
|
69
|
+
ctx: {
|
|
70
|
+
factory: factoryAddress,
|
|
71
|
+
id: (numMinted ?? 0) + 1,
|
|
72
|
+
owner,
|
|
73
|
+
issuer
|
|
74
|
+
}
|
|
75
|
+
}));
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Build the rendered asset + resolved issuer + resolved variables for a
|
|
79
|
+
* pre-mint flow. Handles credentialSubject prerender + VC proof signing.
|
|
80
|
+
*
|
|
81
|
+
* Callers compute the final address via the encoding-appropriate
|
|
82
|
+
* `toAssetAddress`. Matches the original `preMintFromFactory` behavior one
|
|
83
|
+
* for one — the only factored-out line is the final address derivation.
|
|
84
|
+
*/
|
|
85
|
+
const preparePreMintAsset = async ({ factory, inputs, owner, issuer }) => {
|
|
86
|
+
if (Object.keys(inputs).some((x) => typeof inputs[x] !== "string")) throw new Error("Failed to mint asset from factory: input values must be strings");
|
|
87
|
+
if (!factory) throw new Error("Failed to mint asset from factory: factory is required");
|
|
88
|
+
if (!issuer) throw new Error("Failed to mint asset from factory: issuer is required");
|
|
89
|
+
let asset = null;
|
|
90
|
+
const { output, numMinted, address: factoryAddress, data } = factory;
|
|
91
|
+
const { wallet, name } = issuer;
|
|
92
|
+
debug("preparePreMintAsset.args", JSON.stringify({
|
|
93
|
+
output,
|
|
94
|
+
factoryAddress,
|
|
95
|
+
numMinted,
|
|
96
|
+
inputs,
|
|
97
|
+
owner,
|
|
98
|
+
issuer,
|
|
99
|
+
data
|
|
100
|
+
}, null, 2));
|
|
101
|
+
const extra = {};
|
|
102
|
+
const issuerObject = {
|
|
103
|
+
id: wallet.address,
|
|
104
|
+
pk: toBase58(wallet.publicKey),
|
|
105
|
+
name
|
|
106
|
+
};
|
|
107
|
+
const render = (templateObject) => JSON.parse(mustache.render(JSON.stringify(templateObject), {
|
|
108
|
+
input: {
|
|
109
|
+
...inputs,
|
|
110
|
+
...extra
|
|
111
|
+
},
|
|
112
|
+
data: data?.value || data,
|
|
113
|
+
ctx: {
|
|
114
|
+
factory: factoryAddress,
|
|
115
|
+
id: (numMinted ?? 0) + 1,
|
|
116
|
+
owner,
|
|
117
|
+
issuer: issuerObject
|
|
118
|
+
}
|
|
119
|
+
}));
|
|
120
|
+
const template = structuredClone(output);
|
|
121
|
+
const prerenderKeys = findPrerenderKeys(template, "credentialSubject");
|
|
122
|
+
if (prerenderKeys.length) {
|
|
123
|
+
extra.issuanceDate = (/* @__PURE__ */ new Date()).toISOString();
|
|
124
|
+
for (const key of prerenderKeys) {
|
|
125
|
+
const subjectTemplate = getPath(template, key);
|
|
126
|
+
const subjectObject = Array.isArray(subjectTemplate) ? subjectTemplate.map((x) => render(x)) : render(subjectTemplate);
|
|
127
|
+
setPath(template, key, subjectObject);
|
|
128
|
+
const vcRootPath = key.split(".").slice(0, -1).join(".");
|
|
129
|
+
const vcIdPath = vcRootPath.split(".").concat(["id"]).join(".");
|
|
130
|
+
const typeInfo = toTypeInfo(issuerObject.id);
|
|
131
|
+
const pkType = typeInfo.pk;
|
|
132
|
+
const vcType = {
|
|
133
|
+
...typeInfo,
|
|
134
|
+
role: types.RoleType.ROLE_VC
|
|
135
|
+
};
|
|
136
|
+
const vcId = fromPublicKeyHash(wallet.hash(stableStringify(subjectObject)), vcType);
|
|
137
|
+
extra.id = vcId;
|
|
138
|
+
extra.proofType = proofTypes[pkType];
|
|
139
|
+
setPath(template, vcIdPath, vcId);
|
|
140
|
+
if (!proofTypes[pkType]) throw new Error("Unsupported signer type when create verifiable credential");
|
|
141
|
+
let vcObj = render(getPath(template, vcRootPath));
|
|
142
|
+
vcObj.proof = void 0;
|
|
143
|
+
const vcStr = stableStringify(vcObj);
|
|
144
|
+
const signature = toBase64(await wallet.sign(vcStr));
|
|
145
|
+
vcObj.proof = {
|
|
146
|
+
type: proofTypes[pkType],
|
|
147
|
+
created: extra.issuanceDate,
|
|
148
|
+
proofPurpose: "assertionMethod",
|
|
149
|
+
jws: signature
|
|
150
|
+
};
|
|
151
|
+
extra.signature = signature;
|
|
152
|
+
try {
|
|
153
|
+
asset = render(structuredClone(output));
|
|
154
|
+
vcObj = getPath(asset, vcRootPath);
|
|
155
|
+
debug("preparePreMintAsset.result", JSON.stringify(asset, null, 2));
|
|
156
|
+
await verify({
|
|
157
|
+
vc: vcObj,
|
|
158
|
+
trustedIssuers: [issuerObject.id],
|
|
159
|
+
ownerDid: owner,
|
|
160
|
+
ignoreExpired: true
|
|
161
|
+
});
|
|
162
|
+
} catch (err) {
|
|
163
|
+
console.error(err);
|
|
164
|
+
throw new Error("Failed to mint asset from factory: invalid verifiable credential minted");
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
asset = render(template);
|
|
169
|
+
debug("preparePreMintAsset.result", JSON.stringify(asset, null, 2));
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
asset,
|
|
173
|
+
issuer: issuerObject,
|
|
174
|
+
variables: {
|
|
175
|
+
...inputs,
|
|
176
|
+
...extra
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
export { formatFactoryState, preparePreMintAsset, renderMintAsset };
|
package/esm/mint.d.mts
ADDED
package/esm/mint.mjs
ADDED
package/esm/types.d.mts
ADDED
|
@@ -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/esm/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/esm/utils.d.mts
ADDED
|
@@ -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 };
|
package/esm/utils.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import flatten from "flat";
|
|
2
|
+
|
|
3
|
+
//#region src/utils.ts
|
|
4
|
+
const isValidNotation = (notation) => [
|
|
5
|
+
"ctx",
|
|
6
|
+
"data",
|
|
7
|
+
"input"
|
|
8
|
+
].includes(notation.split(".").shift() ?? "");
|
|
9
|
+
/**
|
|
10
|
+
* Find credentialSubject path in the object
|
|
11
|
+
* Because they need prerender
|
|
12
|
+
*
|
|
13
|
+
* @param {object} obj
|
|
14
|
+
* @param {string} keyword
|
|
15
|
+
* @return {string} list of keys
|
|
16
|
+
*/
|
|
17
|
+
const findPrerenderKeys = (obj, keyword) => {
|
|
18
|
+
const flatObj = flatten(obj, { safe: true });
|
|
19
|
+
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("."));
|
|
20
|
+
return Array.from(new Set(keys));
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { findPrerenderKeys, isValidNotation };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { THook } from "./types.mjs";
|
|
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 };
|
package/esm/validate.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { isValidNotation } from "./utils.mjs";
|
|
2
|
+
import mustache from "mustache";
|
|
3
|
+
import { schemas, vValidate } from "@arcblock/validator";
|
|
4
|
+
import { compile, getQuota, validate } from "@ocap/contract";
|
|
5
|
+
import isAbsoluteUrl from "is-absolute-url";
|
|
6
|
+
import isEmpty from "lodash/isEmpty.js";
|
|
7
|
+
import uniq from "lodash/uniq.js";
|
|
8
|
+
import uniqBy from "lodash/uniqBy.js";
|
|
9
|
+
|
|
10
|
+
//#region src/validate.ts
|
|
11
|
+
const SUPPORTED_HOOK_NAMES = [
|
|
12
|
+
"preMint",
|
|
13
|
+
"mint",
|
|
14
|
+
"postMint"
|
|
15
|
+
];
|
|
16
|
+
const SUPPORTED_HOOK_TYPES = ["contract", "url"];
|
|
17
|
+
const isValidHook = (hook, quota, throwOnError = false) => {
|
|
18
|
+
if (SUPPORTED_HOOK_TYPES.includes(hook.type) === false) return false;
|
|
19
|
+
if (SUPPORTED_HOOK_NAMES.includes(hook.name) === false) return false;
|
|
20
|
+
if (hook.type === "url") return isAbsoluteUrl(hook.hook);
|
|
21
|
+
if (hook.type === "contract") try {
|
|
22
|
+
validate(compile(hook.hook), quota);
|
|
23
|
+
return true;
|
|
24
|
+
} catch (err) {
|
|
25
|
+
if (process.env.NODE_ENV !== "test") console.error("invalid contract hook", err.message);
|
|
26
|
+
if (throwOnError) throw new Error(`Factory hook ${hook.name} is invalid: ${err.message}`);
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
};
|
|
31
|
+
const isValidFactory = (props) => {
|
|
32
|
+
if (!props) throw new Error("Factory props should not be empty");
|
|
33
|
+
const { value, error } = vValidate(schemas.factorySchema, props);
|
|
34
|
+
if (error) throw new Error(`Invalid factory: ${error.details.map((x) => x.message).join(", ")}`);
|
|
35
|
+
if (["tokens", "assets"].every((x) => isEmpty(value.input[x])) && value.input.value <= 0) throw new Error("Factory input should contain at least one token or asset");
|
|
36
|
+
if (uniqBy(value.input.tokens, "address").length !== value.input.tokens.length) throw new Error("Factory token input should not contains duplicate address");
|
|
37
|
+
if (uniq(value.input.assets).length !== value.input.assets.length) throw new Error("Factory asset input should not contains duplicate address");
|
|
38
|
+
try {
|
|
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");
|
|
41
|
+
} catch (_err) {
|
|
42
|
+
throw new Error("Factory output should be a valid mustache template when serialized as json");
|
|
43
|
+
}
|
|
44
|
+
const quota = getQuota(value.input);
|
|
45
|
+
if (Array.isArray(value.hooks)) {
|
|
46
|
+
const invalidHook = value.hooks.find((x) => isValidHook(x, quota, true) === false);
|
|
47
|
+
if (invalidHook) throw new Error(`Factory hook ${invalidHook.name} is invalid`);
|
|
48
|
+
}
|
|
49
|
+
if (value.settlement === "instant") {
|
|
50
|
+
if (quota.value <= 0 && Object.keys(quota.tokens).every((x) => quota[x] <= 0)) return true;
|
|
51
|
+
if (isEmpty(value.hooks)) throw new Error("Factory hooks should not be empty for instant settlement");
|
|
52
|
+
const mintHook = value.hooks.find((x) => x.name === "mint");
|
|
53
|
+
if (!mintHook) throw new Error("Factory hook mint should not be empty for instant settlement that consumes token");
|
|
54
|
+
try {
|
|
55
|
+
validate(compile(mintHook.hook), quota, true);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
throw new Error(`Factory hook mint is invalid: ${err.message}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { isValidFactory, isValidHook };
|
package/lib/index.cjs
CHANGED
|
@@ -1,267 +1,12 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
let _arcblock_vc = require("@arcblock/vc");
|
|
6
|
-
let _ocap_contract = require("@ocap/contract");
|
|
7
|
-
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
8
|
-
let _ocap_util = require("@ocap/util");
|
|
9
|
-
let debug = require("debug");
|
|
10
|
-
debug = require_rolldown_runtime.__toESM(debug);
|
|
11
|
-
let flat = require("flat");
|
|
12
|
-
flat = require_rolldown_runtime.__toESM(flat);
|
|
13
|
-
let is_absolute_url = require("is-absolute-url");
|
|
14
|
-
is_absolute_url = require_rolldown_runtime.__toESM(is_absolute_url);
|
|
15
|
-
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
16
|
-
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
17
|
-
let lodash_get = require("lodash/get");
|
|
18
|
-
lodash_get = require_rolldown_runtime.__toESM(lodash_get);
|
|
19
|
-
let lodash_isEmpty = require("lodash/isEmpty");
|
|
20
|
-
lodash_isEmpty = require_rolldown_runtime.__toESM(lodash_isEmpty);
|
|
21
|
-
let lodash_set = require("lodash/set");
|
|
22
|
-
lodash_set = require_rolldown_runtime.__toESM(lodash_set);
|
|
23
|
-
let lodash_uniq = require("lodash/uniq");
|
|
24
|
-
lodash_uniq = require_rolldown_runtime.__toESM(lodash_uniq);
|
|
25
|
-
let lodash_uniqBy = require("lodash/uniqBy");
|
|
26
|
-
lodash_uniqBy = require_rolldown_runtime.__toESM(lodash_uniqBy);
|
|
27
|
-
let mustache = require("mustache");
|
|
28
|
-
mustache = require_rolldown_runtime.__toESM(mustache);
|
|
1
|
+
const require_utils = require('./utils.cjs');
|
|
2
|
+
const require_mint_shared = require('./mint/shared.cjs');
|
|
3
|
+
const require_mint_server = require('./mint/server.cjs');
|
|
4
|
+
const require_validate = require('./validate.cjs');
|
|
29
5
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const SUPPORTED_HOOK_TYPES = ["contract", "url"];
|
|
38
|
-
const isValidNotation = (notation) => [
|
|
39
|
-
"ctx",
|
|
40
|
-
"data",
|
|
41
|
-
"input"
|
|
42
|
-
].includes(notation.split(".").shift() ?? "");
|
|
43
|
-
const isValidHook = (hook, quota, throwOnError = false) => {
|
|
44
|
-
if (SUPPORTED_HOOK_TYPES.includes(hook.type) === false) return false;
|
|
45
|
-
if (SUPPORTED_HOOK_NAMES.includes(hook.name) === false) return false;
|
|
46
|
-
if (hook.type === "url") return (0, is_absolute_url.default)(hook.hook);
|
|
47
|
-
if (hook.type === "contract") try {
|
|
48
|
-
(0, _ocap_contract.validate)((0, _ocap_contract.compile)(hook.hook), quota);
|
|
49
|
-
return true;
|
|
50
|
-
} catch (err) {
|
|
51
|
-
if (process.env.NODE_ENV !== "test") console.error("invalid contract hook", err.message);
|
|
52
|
-
if (throwOnError) throw new Error(`Factory hook ${hook.name} is invalid: ${err.message}`);
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
return false;
|
|
56
|
-
};
|
|
57
|
-
const isValidFactory = (props) => {
|
|
58
|
-
if (!props) throw new Error("Factory props should not be empty");
|
|
59
|
-
const { value, error } = (0, _arcblock_validator.vValidate)(_arcblock_validator.schemas.factorySchema, props);
|
|
60
|
-
if (error) throw new Error(`Invalid factory: ${error.details.map((x) => x.message).join(", ")}`);
|
|
61
|
-
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");
|
|
62
|
-
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");
|
|
63
|
-
if ((0, lodash_uniq.default)(value.input.assets).length !== value.input.assets.length) throw new Error("Factory asset input should not contains duplicate address");
|
|
64
|
-
try {
|
|
65
|
-
const template = JSON.stringify(value.output, null, 2);
|
|
66
|
-
if (mustache.default.parse(template).filter(([type]) => type === "name").some(([, notation]) => isValidNotation(notation) === false)) throw new Error("Invalid tags found in the output template");
|
|
67
|
-
} catch (_err) {
|
|
68
|
-
throw new Error("Factory output should be a valid mustache template when serialized as json");
|
|
69
|
-
}
|
|
70
|
-
const quota = (0, _ocap_contract.getQuota)(value.input);
|
|
71
|
-
if (Array.isArray(value.hooks)) {
|
|
72
|
-
const invalidHook = value.hooks.find((x) => isValidHook(x, quota, true) === false);
|
|
73
|
-
if (invalidHook) throw new Error(`Factory hook ${invalidHook.name} is invalid`);
|
|
74
|
-
}
|
|
75
|
-
if (value.settlement === "instant") {
|
|
76
|
-
if (quota.value <= 0 && Object.keys(quota.tokens).every((x) => quota[x] <= 0)) return true;
|
|
77
|
-
if ((0, lodash_isEmpty.default)(value.hooks)) throw new Error("Factory hooks should not be empty for instant settlement");
|
|
78
|
-
const mintHook = value.hooks.find((x) => x.name === "mint");
|
|
79
|
-
if (!mintHook) throw new Error("Factory hook mint should not be empty for instant settlement that consumes token");
|
|
80
|
-
try {
|
|
81
|
-
(0, _ocap_contract.validate)((0, _ocap_contract.compile)(mintHook.hook), quota, true);
|
|
82
|
-
} catch (err) {
|
|
83
|
-
throw new Error(`Factory hook mint is invalid: ${err.message}`);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return true;
|
|
87
|
-
};
|
|
88
|
-
/**
|
|
89
|
-
* Find credentialSubject path in the object
|
|
90
|
-
* Because they need prerender
|
|
91
|
-
*
|
|
92
|
-
* @param {object} obj
|
|
93
|
-
* @param {string} keyword
|
|
94
|
-
* @return {string} list of keys
|
|
95
|
-
*/
|
|
96
|
-
const findPrerenderKeys = (obj, keyword) => {
|
|
97
|
-
const flatObj = (0, flat.default)(obj, { safe: true });
|
|
98
|
-
return (0, lodash_uniq.default)(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(".")));
|
|
99
|
-
};
|
|
100
|
-
/**
|
|
101
|
-
* Mint from an asset factory, used on server side
|
|
102
|
-
*
|
|
103
|
-
* @param {object} params { factory, inputs, issuer }
|
|
104
|
-
* @param {object} params.factory factory object
|
|
105
|
-
* @param {object} params.inputs factory input variables
|
|
106
|
-
* @param {string} params.owner owner did for the new asset
|
|
107
|
-
* @param {object} params.issuer issuer object
|
|
108
|
-
*/
|
|
109
|
-
const mintFromFactory = ({ factory, inputs, owner, issuer }) => {
|
|
110
|
-
const { output, address: factoryAddress, numMinted, data } = factory;
|
|
111
|
-
debug$1("mintFromFactory.args", JSON.stringify({
|
|
112
|
-
output,
|
|
113
|
-
factoryAddress,
|
|
114
|
-
numMinted,
|
|
115
|
-
inputs,
|
|
116
|
-
owner,
|
|
117
|
-
issuer,
|
|
118
|
-
data
|
|
119
|
-
}, null, 2));
|
|
120
|
-
const asset = JSON.parse(mustache.default.render(JSON.stringify(output), {
|
|
121
|
-
input: inputs,
|
|
122
|
-
data: data?.value || data,
|
|
123
|
-
ctx: {
|
|
124
|
-
factory: factoryAddress,
|
|
125
|
-
id: (numMinted ?? 0) + 1,
|
|
126
|
-
owner,
|
|
127
|
-
issuer
|
|
128
|
-
}
|
|
129
|
-
}));
|
|
130
|
-
const address = (0, _arcblock_did_util.toAssetAddress)(asset);
|
|
131
|
-
debug$1("mintFromFactory.result", JSON.stringify({
|
|
132
|
-
asset,
|
|
133
|
-
address
|
|
134
|
-
}, null, 2));
|
|
135
|
-
return {
|
|
136
|
-
asset,
|
|
137
|
-
address
|
|
138
|
-
};
|
|
139
|
-
};
|
|
140
|
-
/**
|
|
141
|
-
* Simulate minting from an asset factory, used for client side
|
|
142
|
-
*
|
|
143
|
-
* @param {object} params { factory, inputs, issuer }
|
|
144
|
-
* @param {object} params.factory factory object
|
|
145
|
-
* @param {object} params.inputs factory input variables
|
|
146
|
-
* @param {string} params.owner owner did for the new asset
|
|
147
|
-
* @param {object} params.issuer factory issuer wallet and name
|
|
148
|
-
*/
|
|
149
|
-
const preMintFromFactory = async ({ factory, inputs, owner, issuer }) => {
|
|
150
|
-
if (Object.keys(inputs).some((x) => typeof inputs[x] !== "string")) throw new Error("Failed to mint asset from factory: input values must be strings");
|
|
151
|
-
if (!factory) throw new Error("Failed to mint asset from factory: factory is required");
|
|
152
|
-
if (!issuer) throw new Error("Failed to mint asset from factory: issuer is required");
|
|
153
|
-
let asset = null;
|
|
154
|
-
const { output, numMinted, address: factoryAddress, data } = factory;
|
|
155
|
-
const { wallet, name } = issuer;
|
|
156
|
-
debug$1("preMintFromFactory.args", JSON.stringify({
|
|
157
|
-
output,
|
|
158
|
-
factoryAddress,
|
|
159
|
-
numMinted,
|
|
160
|
-
inputs,
|
|
161
|
-
owner,
|
|
162
|
-
issuer,
|
|
163
|
-
data
|
|
164
|
-
}, null, 2));
|
|
165
|
-
const extra = {};
|
|
166
|
-
const issuerObject = {
|
|
167
|
-
id: wallet.address,
|
|
168
|
-
pk: (0, _ocap_util.toBase58)(wallet.publicKey),
|
|
169
|
-
name
|
|
170
|
-
};
|
|
171
|
-
const render = (templateObject) => JSON.parse(mustache.default.render(JSON.stringify(templateObject), {
|
|
172
|
-
input: {
|
|
173
|
-
...inputs,
|
|
174
|
-
...extra
|
|
175
|
-
},
|
|
176
|
-
data: data?.value || data,
|
|
177
|
-
ctx: {
|
|
178
|
-
factory: factoryAddress,
|
|
179
|
-
id: (numMinted ?? 0) + 1,
|
|
180
|
-
owner,
|
|
181
|
-
issuer: issuerObject
|
|
182
|
-
}
|
|
183
|
-
}));
|
|
184
|
-
const template = (0, lodash_cloneDeep.default)(output);
|
|
185
|
-
const prerenderKeys = findPrerenderKeys(template, "credentialSubject");
|
|
186
|
-
if (prerenderKeys.length) {
|
|
187
|
-
extra.issuanceDate = (/* @__PURE__ */ new Date()).toISOString();
|
|
188
|
-
for (const key of prerenderKeys) {
|
|
189
|
-
const subjectTemplate = (0, lodash_get.default)(template, key);
|
|
190
|
-
const subjectObject = Array.isArray(subjectTemplate) ? subjectTemplate.map((x) => render(x)) : render(subjectTemplate);
|
|
191
|
-
(0, lodash_set.default)(template, key, subjectObject);
|
|
192
|
-
const vcRootPath = key.split(".").slice(0, -1).join(".");
|
|
193
|
-
const vcIdPath = vcRootPath.split(".").concat(["id"]).join(".");
|
|
194
|
-
const typeInfo = (0, _arcblock_did.toTypeInfo)(issuerObject.id);
|
|
195
|
-
const pkType = typeInfo.pk;
|
|
196
|
-
const vcType = {
|
|
197
|
-
...typeInfo,
|
|
198
|
-
role: _ocap_mcrypto.types.RoleType.ROLE_VC
|
|
199
|
-
};
|
|
200
|
-
const vcId = (0, _arcblock_did.fromPublicKeyHash)(wallet.hash((0, _arcblock_vc.stableStringify)(subjectObject)), vcType);
|
|
201
|
-
extra.id = vcId;
|
|
202
|
-
extra.proofType = _arcblock_vc.proofTypes[pkType];
|
|
203
|
-
(0, lodash_set.default)(template, vcIdPath, vcId);
|
|
204
|
-
if (!_arcblock_vc.proofTypes[pkType]) throw new Error("Unsupported signer type when create verifiable credential");
|
|
205
|
-
let vcObj = render((0, lodash_get.default)(template, vcRootPath));
|
|
206
|
-
vcObj.proof = void 0;
|
|
207
|
-
const vcStr = (0, _arcblock_vc.stableStringify)(vcObj);
|
|
208
|
-
const signature = (0, _ocap_util.toBase64)(await wallet.sign(vcStr));
|
|
209
|
-
vcObj.proof = {
|
|
210
|
-
type: _arcblock_vc.proofTypes[pkType],
|
|
211
|
-
created: extra.issuanceDate,
|
|
212
|
-
proofPurpose: "assertionMethod",
|
|
213
|
-
jws: signature
|
|
214
|
-
};
|
|
215
|
-
extra.signature = signature;
|
|
216
|
-
try {
|
|
217
|
-
asset = render((0, lodash_cloneDeep.default)(output));
|
|
218
|
-
vcObj = (0, lodash_get.default)(asset, vcRootPath);
|
|
219
|
-
debug$1("preMintFromFactory.result", JSON.stringify(asset, null, 2));
|
|
220
|
-
await (0, _arcblock_vc.verify)({
|
|
221
|
-
vc: vcObj,
|
|
222
|
-
trustedIssuers: [issuerObject.id],
|
|
223
|
-
ownerDid: owner,
|
|
224
|
-
ignoreExpired: true
|
|
225
|
-
});
|
|
226
|
-
} catch (err) {
|
|
227
|
-
console.error(err);
|
|
228
|
-
throw new Error("Failed to mint asset from factory: invalid verifiable credential minted");
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
} else {
|
|
232
|
-
asset = render(template);
|
|
233
|
-
debug$1("preMintFromFactory.result", JSON.stringify(asset, null, 2));
|
|
234
|
-
}
|
|
235
|
-
return {
|
|
236
|
-
address: (0, _arcblock_did_util.toAssetAddress)(asset),
|
|
237
|
-
issuer: issuerObject,
|
|
238
|
-
variables: {
|
|
239
|
-
...inputs,
|
|
240
|
-
...extra
|
|
241
|
-
},
|
|
242
|
-
asset
|
|
243
|
-
};
|
|
244
|
-
};
|
|
245
|
-
const formatFactoryState = (state) => {
|
|
246
|
-
const { address, output, data, numMinted } = state;
|
|
247
|
-
const outputX = (0, lodash_cloneDeep.default)(output);
|
|
248
|
-
const outputData = outputX.data;
|
|
249
|
-
outputData.value = JSON.parse(outputData.value);
|
|
250
|
-
outputData.type = outputData.typeUrl;
|
|
251
|
-
const stateData = data;
|
|
252
|
-
return {
|
|
253
|
-
address,
|
|
254
|
-
output: outputX,
|
|
255
|
-
data: stateData?.value ? JSON.parse(stateData.value) : {},
|
|
256
|
-
numMinted
|
|
257
|
-
};
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
//#endregion
|
|
261
|
-
exports.findPrerenderKeys = findPrerenderKeys;
|
|
262
|
-
exports.formatFactoryState = formatFactoryState;
|
|
263
|
-
exports.isValidFactory = isValidFactory;
|
|
264
|
-
exports.isValidHook = isValidHook;
|
|
265
|
-
exports.isValidNotation = isValidNotation;
|
|
266
|
-
exports.mintFromFactory = mintFromFactory;
|
|
267
|
-
exports.preMintFromFactory = preMintFromFactory;
|
|
6
|
+
exports.findPrerenderKeys = require_utils.findPrerenderKeys;
|
|
7
|
+
exports.formatFactoryState = require_mint_shared.formatFactoryState;
|
|
8
|
+
exports.isValidFactory = require_validate.isValidFactory;
|
|
9
|
+
exports.isValidHook = require_validate.isValidHook;
|
|
10
|
+
exports.isValidNotation = require_utils.isValidNotation;
|
|
11
|
+
exports.mintFromFactory = require_mint_server.mintFromFactory;
|
|
12
|
+
exports.preMintFromFactory = require_mint_server.preMintFromFactory;
|