@ocap/asset 1.27.16 → 1.28.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/index.d.ts +52 -39
- package/esm/index.js +236 -239
- package/esm/lodash.d.ts +6 -0
- package/lib/_virtual/rolldown_runtime.js +29 -0
- package/lib/index.d.ts +52 -39
- package/lib/index.js +248 -247
- package/lib/lodash.d.ts +6 -0
- package/package.json +23 -17
package/lib/index.d.ts
CHANGED
|
@@ -1,36 +1,38 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { LiteralUnion, PartialDeep } from "type-fest";
|
|
2
|
+
import { WalletObject } from "@ocap/wallet";
|
|
3
|
+
import { TAssetFactoryState, TCreateAssetTx } from "@ocap/types";
|
|
4
|
+
|
|
5
|
+
//#region src/index.d.ts
|
|
4
6
|
type $TSFixMe = any;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
type TPickedFactoryState = PartialDeep<TAssetFactoryState>;
|
|
8
|
+
type TIssuer = {
|
|
9
|
+
name: string;
|
|
10
|
+
wallet: WalletObject;
|
|
9
11
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
type TIssuerInput = {
|
|
13
|
+
id: string;
|
|
14
|
+
pk: string;
|
|
15
|
+
name: string;
|
|
14
16
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
type THook = {
|
|
18
|
+
type: LiteralUnion<'contract' | 'url', string>;
|
|
19
|
+
name: LiteralUnion<'mint' | 'postMint' | 'preMint', string>;
|
|
20
|
+
hook: string;
|
|
19
21
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
type TInputMap = {
|
|
23
|
+
[key: string]: string;
|
|
22
24
|
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
type TMintResult = {
|
|
26
|
+
address: string;
|
|
27
|
+
asset: TCreateAssetTx;
|
|
26
28
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
type TPreMintResult = TMintResult & {
|
|
30
|
+
variables: TInputMap;
|
|
31
|
+
issuer: TIssuerInput;
|
|
30
32
|
};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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;
|
|
34
36
|
/**
|
|
35
37
|
* Find credentialSubject path in the object
|
|
36
38
|
* Because they need prerender
|
|
@@ -39,7 +41,7 @@ export declare const isValidFactory: (props: any) => boolean;
|
|
|
39
41
|
* @param {string} keyword
|
|
40
42
|
* @return {string} list of keys
|
|
41
43
|
*/
|
|
42
|
-
|
|
44
|
+
declare const findPrerenderKeys: (obj: $TSFixMe, keyword: string) => string[];
|
|
43
45
|
/**
|
|
44
46
|
* Mint from an asset factory, used on server side
|
|
45
47
|
*
|
|
@@ -49,11 +51,16 @@ export declare const findPrerenderKeys: (obj: $TSFixMe, keyword: string) => stri
|
|
|
49
51
|
* @param {string} params.owner owner did for the new asset
|
|
50
52
|
* @param {object} params.issuer issuer object
|
|
51
53
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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;
|
|
57
64
|
}) => TMintResult;
|
|
58
65
|
/**
|
|
59
66
|
* Simulate minting from an asset factory, used for client side
|
|
@@ -64,11 +71,17 @@ export declare const mintFromFactory: ({ factory, inputs, owner, issuer, }: {
|
|
|
64
71
|
* @param {string} params.owner owner did for the new asset
|
|
65
72
|
* @param {object} params.issuer factory issuer wallet and name
|
|
66
73
|
*/
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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;
|
|
72
84
|
}) => Promise<TPreMintResult>;
|
|
73
|
-
|
|
74
|
-
|
|
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 };
|
package/lib/index.js
CHANGED
|
@@ -1,261 +1,262 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
|
+
let lodash_get = require("lodash/get");
|
|
3
|
+
lodash_get = require_rolldown_runtime.__toESM(lodash_get);
|
|
4
|
+
let lodash_set = require("lodash/set");
|
|
5
|
+
lodash_set = require_rolldown_runtime.__toESM(lodash_set);
|
|
6
|
+
let lodash_uniq = require("lodash/uniq");
|
|
7
|
+
lodash_uniq = require_rolldown_runtime.__toESM(lodash_uniq);
|
|
8
|
+
let lodash_uniqBy = require("lodash/uniqBy");
|
|
9
|
+
lodash_uniqBy = require_rolldown_runtime.__toESM(lodash_uniqBy);
|
|
10
|
+
let lodash_isEmpty = require("lodash/isEmpty");
|
|
11
|
+
lodash_isEmpty = require_rolldown_runtime.__toESM(lodash_isEmpty);
|
|
12
|
+
let flat = require("flat");
|
|
13
|
+
flat = require_rolldown_runtime.__toESM(flat);
|
|
14
|
+
let mustache = require("mustache");
|
|
15
|
+
mustache = require_rolldown_runtime.__toESM(mustache);
|
|
16
|
+
let is_absolute_url = require("is-absolute-url");
|
|
17
|
+
is_absolute_url = require_rolldown_runtime.__toESM(is_absolute_url);
|
|
18
|
+
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
19
|
+
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
20
|
+
let _arcblock_did = require("@arcblock/did");
|
|
21
|
+
let _arcblock_vc = require("@arcblock/vc");
|
|
22
|
+
let _arcblock_validator = require("@arcblock/validator");
|
|
23
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
24
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
25
|
+
let _ocap_util = require("@ocap/util");
|
|
26
|
+
let _ocap_contract = require("@ocap/contract");
|
|
27
|
+
let debug = require("debug");
|
|
28
|
+
debug = require_rolldown_runtime.__toESM(debug);
|
|
29
|
+
|
|
30
|
+
//#region src/index.ts
|
|
31
|
+
const debug$1 = (0, debug.default)("@ocap/asset");
|
|
32
|
+
const SUPPORTED_HOOK_NAMES = [
|
|
33
|
+
"preMint",
|
|
34
|
+
"mint",
|
|
35
|
+
"postMint"
|
|
36
|
+
];
|
|
37
|
+
const SUPPORTED_HOOK_TYPES = ["contract", "url"];
|
|
38
|
+
const isValidNotation = (notation) => [
|
|
39
|
+
"ctx",
|
|
40
|
+
"data",
|
|
41
|
+
"input"
|
|
42
|
+
].includes(notation.split(".").shift());
|
|
30
43
|
const isValidHook = (hook, quota, throwOnError = false) => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
(0, contract_1.validate)(compiled, quota);
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
catch (err) {
|
|
47
|
-
if (process.env.NODE_ENV !== 'test') {
|
|
48
|
-
console.error('invalid contract hook', err.message);
|
|
49
|
-
}
|
|
50
|
-
if (throwOnError) {
|
|
51
|
-
throw new Error(`Factory hook ${hook.name} is invalid: ${err.message}`);
|
|
52
|
-
}
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return 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;
|
|
57
56
|
};
|
|
58
|
-
exports.isValidHook = isValidHook;
|
|
59
|
-
// @link https://github.com/improbable-eng/ts-protoc-gen
|
|
60
57
|
const isValidFactory = (props) => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const quota = (0, contract_1.getQuota)(value.input);
|
|
91
|
-
if (Array.isArray(value.hooks)) {
|
|
92
|
-
const invalidHook = value.hooks.find((x) => (0, exports.isValidHook)(x, quota, true) === false);
|
|
93
|
-
if (invalidHook) {
|
|
94
|
-
throw new Error(`Factory hook ${invalidHook.name} is invalid`);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
// ensure input and hook are zero-sum for factories that consumes token
|
|
98
|
-
if (value.settlement === 'instant') {
|
|
99
|
-
if (quota.value <= 0 && Object.keys(quota.tokens).every((x) => quota[x] <= 0)) {
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
if ((0, isEmpty_1.default)(value.hooks)) {
|
|
103
|
-
throw new Error('Factory hooks should not be empty for instant settlement');
|
|
104
|
-
}
|
|
105
|
-
const mintHook = value.hooks.find((x) => x.name === 'mint');
|
|
106
|
-
if (!mintHook) {
|
|
107
|
-
throw new Error('Factory hook mint should not be empty for instant settlement that consumes token');
|
|
108
|
-
}
|
|
109
|
-
try {
|
|
110
|
-
const compiled = (0, contract_1.compile)(mintHook.hook);
|
|
111
|
-
(0, contract_1.validate)(compiled, quota, true);
|
|
112
|
-
}
|
|
113
|
-
catch (err) {
|
|
114
|
-
throw new Error(`Factory hook mint is invalid: ${err.message}`);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return true;
|
|
58
|
+
if (!props) throw new Error("Factory props should not be empty");
|
|
59
|
+
const { value, error } = _arcblock_validator.schemas.factorySchema.validate(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;
|
|
118
87
|
};
|
|
119
|
-
exports.isValidFactory = isValidFactory;
|
|
120
88
|
/**
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
+
*/
|
|
128
96
|
const findPrerenderKeys = (obj, keyword) => {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
.map((x) => x.split('.'))
|
|
132
|
-
.filter((x) => x.includes(keyword))
|
|
133
|
-
.map((x) => x.slice(0, x.lastIndexOf(keyword) + 1))
|
|
134
|
-
.sort((a, b) => b.length - a.length)
|
|
135
|
-
.map((x) => x.join('.'));
|
|
136
|
-
return (0, uniq_1.default)(keys);
|
|
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(".")));
|
|
137
99
|
};
|
|
138
|
-
exports.findPrerenderKeys = findPrerenderKeys;
|
|
139
100
|
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const mintFromFactory = ({ factory, inputs, owner, issuer
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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 + 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
|
+
};
|
|
159
139
|
};
|
|
160
|
-
exports.mintFromFactory = mintFromFactory;
|
|
161
140
|
/**
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const preMintFromFactory = async ({ factory, inputs, owner, issuer
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
+
let asset = null;
|
|
152
|
+
const { output, numMinted, address: factoryAddress, data } = factory;
|
|
153
|
+
const { wallet, name } = issuer;
|
|
154
|
+
debug$1("preMintFromFactory.args", JSON.stringify({
|
|
155
|
+
output,
|
|
156
|
+
factoryAddress,
|
|
157
|
+
numMinted,
|
|
158
|
+
inputs,
|
|
159
|
+
owner,
|
|
160
|
+
issuer,
|
|
161
|
+
data
|
|
162
|
+
}, null, 2));
|
|
163
|
+
const extra = {};
|
|
164
|
+
const issuerObject = {
|
|
165
|
+
id: wallet.address,
|
|
166
|
+
pk: (0, _ocap_util.toBase58)(wallet.publicKey),
|
|
167
|
+
name
|
|
168
|
+
};
|
|
169
|
+
const render = (templateObject) => JSON.parse(mustache.default.render(JSON.stringify(templateObject), {
|
|
170
|
+
input: {
|
|
171
|
+
...inputs,
|
|
172
|
+
...extra
|
|
173
|
+
},
|
|
174
|
+
data: data.value || data,
|
|
175
|
+
ctx: {
|
|
176
|
+
factory: factoryAddress,
|
|
177
|
+
id: numMinted + 1,
|
|
178
|
+
owner,
|
|
179
|
+
issuer: issuerObject
|
|
180
|
+
}
|
|
181
|
+
}));
|
|
182
|
+
const template = (0, lodash_cloneDeep.default)(output);
|
|
183
|
+
const prerenderKeys = findPrerenderKeys(template, "credentialSubject");
|
|
184
|
+
if (prerenderKeys.length) {
|
|
185
|
+
extra.issuanceDate = (/* @__PURE__ */ new Date()).toISOString();
|
|
186
|
+
for (const key of prerenderKeys) {
|
|
187
|
+
const subjectTemplate = (0, lodash_get.default)(template, key);
|
|
188
|
+
const subjectObject = Array.isArray(subjectTemplate) ? subjectTemplate.map((x) => render(x)) : render(subjectTemplate);
|
|
189
|
+
(0, lodash_set.default)(template, key, subjectObject);
|
|
190
|
+
const vcRootPath = key.split(".").slice(0, -1).join(".");
|
|
191
|
+
const vcIdPath = vcRootPath.split(".").concat(["id"]).join(".");
|
|
192
|
+
const typeInfo = (0, _arcblock_did.toTypeInfo)(issuerObject.id);
|
|
193
|
+
const vcType = {
|
|
194
|
+
...typeInfo,
|
|
195
|
+
role: _ocap_mcrypto.types.RoleType.ROLE_VC
|
|
196
|
+
};
|
|
197
|
+
const vcId = (0, _arcblock_did.fromPublicKeyHash)(wallet.hash((0, _arcblock_vc.stableStringify)(subjectObject)), vcType);
|
|
198
|
+
extra.id = vcId;
|
|
199
|
+
extra.proofType = _arcblock_vc.proofTypes[typeInfo.pk];
|
|
200
|
+
(0, lodash_set.default)(template, vcIdPath, vcId);
|
|
201
|
+
if (!_arcblock_vc.proofTypes[typeInfo.pk]) throw new Error("Unsupported signer type when create verifiable credential");
|
|
202
|
+
let vcObj = render((0, lodash_get.default)(template, vcRootPath));
|
|
203
|
+
delete vcObj.proof;
|
|
204
|
+
const vcStr = (0, _arcblock_vc.stableStringify)(vcObj);
|
|
205
|
+
const signature = (0, _ocap_util.toBase64)(await wallet.sign(vcStr));
|
|
206
|
+
vcObj.proof = {
|
|
207
|
+
type: _arcblock_vc.proofTypes[typeInfo.pk],
|
|
208
|
+
created: extra.issuanceDate,
|
|
209
|
+
proofPurpose: "assertionMethod",
|
|
210
|
+
jws: signature
|
|
211
|
+
};
|
|
212
|
+
extra.signature = signature;
|
|
213
|
+
try {
|
|
214
|
+
asset = render((0, lodash_cloneDeep.default)(output));
|
|
215
|
+
vcObj = (0, lodash_get.default)(asset, vcRootPath);
|
|
216
|
+
debug$1("preMintFromFactory.result", JSON.stringify(asset, null, 2));
|
|
217
|
+
await (0, _arcblock_vc.verify)({
|
|
218
|
+
vc: vcObj,
|
|
219
|
+
trustedIssuers: [issuerObject.id],
|
|
220
|
+
ownerDid: owner,
|
|
221
|
+
ignoreExpired: true
|
|
222
|
+
});
|
|
223
|
+
} catch (err) {
|
|
224
|
+
console.error(err);
|
|
225
|
+
throw new Error("Failed to mint asset from factory: invalid verifiable credential minted");
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} else {
|
|
229
|
+
asset = render(template);
|
|
230
|
+
debug$1("preMintFromFactory.result", JSON.stringify(asset, null, 2));
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
address: (0, _arcblock_did_util.toAssetAddress)(asset),
|
|
234
|
+
issuer: issuerObject,
|
|
235
|
+
variables: {
|
|
236
|
+
...inputs,
|
|
237
|
+
...extra
|
|
238
|
+
},
|
|
239
|
+
asset
|
|
240
|
+
};
|
|
247
241
|
};
|
|
248
|
-
exports.preMintFromFactory = preMintFromFactory;
|
|
249
242
|
const formatFactoryState = (state) => {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
243
|
+
const { address, output, data, numMinted } = state;
|
|
244
|
+
const outputX = (0, lodash_cloneDeep.default)(output);
|
|
245
|
+
outputX.data.value = JSON.parse(outputX.data.value);
|
|
246
|
+
outputX.data.type = outputX.data.typeUrl;
|
|
247
|
+
return {
|
|
248
|
+
address,
|
|
249
|
+
output: outputX,
|
|
250
|
+
data: data.value ? JSON.parse(data.value) : {},
|
|
251
|
+
numMinted
|
|
252
|
+
};
|
|
260
253
|
};
|
|
254
|
+
|
|
255
|
+
//#endregion
|
|
256
|
+
exports.findPrerenderKeys = findPrerenderKeys;
|
|
261
257
|
exports.formatFactoryState = formatFactoryState;
|
|
258
|
+
exports.isValidFactory = isValidFactory;
|
|
259
|
+
exports.isValidHook = isValidHook;
|
|
260
|
+
exports.isValidNotation = isValidNotation;
|
|
261
|
+
exports.mintFromFactory = mintFromFactory;
|
|
262
|
+
exports.preMintFromFactory = preMintFromFactory;
|