@koralabs/kora-labs-common 6.9.0 → 6.9.2

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/mpt/index.d.ts CHANGED
@@ -1,20 +1,43 @@
1
+ import { Trie } from '@aiken-lang/merkle-patricia-forestry';
1
2
  import * as labelSet from './labelSet';
2
3
  import * as registryValue from './registryValue';
3
4
  export { labelSet, registryValue };
5
+ export { Trie };
6
+ /**
7
+ * A minting-data MPT entry. A bare string is shorthand for a handle with no registry value
8
+ * (value = ""). The object form carries the registry value the on-chain validator stores at the
9
+ * key: `registryValue.encode(freeNames, labels)`.
10
+ * - `freeNames`: a ROOT's currently-claimed FREE private-virtual sub names (the free-virtual
11
+ * allowance). The DeMi free-virtual mint/burn path writes this into the root key's value, so it
12
+ * MUST be supplied or the off-chain root drifts from chain on every root holding a free virtual.
13
+ * - `labels`: the sorted CIP-67 label set (001-004). The in-band `MintLabelAssets` path is NOT
14
+ * yet deployed on-chain, so callers pass "" today; flip to the real label set when it ships.
15
+ */
16
+ export interface MintingDataEntry {
17
+ name: string;
18
+ freeNames?: string[];
19
+ labels?: string;
20
+ }
21
+ type MintingDataInput = ReadonlyArray<string | MintingDataEntry>;
22
+ /**
23
+ * THE canonical minting-data MPT (a real {@link Trie}, for proof generation during mint/burn).
24
+ * Consumers that only need the root hash should use {@link computeMintingDataRoot}; consumers that
25
+ * generate proofs (the minting engine's verifyRootHash) keep the returned Trie.
26
+ *
27
+ * The value at each key is `registryValue.encode(freeNames, labels)` — byte-identical to what the
28
+ * on-chain `demimntmpt` validator stores (`mpt.update` verifies the old bytes). A handle with no
29
+ * free names and no labels encodes to "" (the legacy `update_root` `mpt.insert(root, name, #"")`
30
+ * case), so passing bare strings reproduces the empty-valued root exactly.
31
+ */
32
+ export declare const buildMintingDataTrie: (handles: MintingDataInput) => Promise<Trie>;
4
33
  /**
5
34
  * THE canonical minting-data MPT root. Every Node service (api.handle.me, the minting engine,
6
35
  * handle.me/bff) and the on-chain mirror (@koralabs/handles-decentralized-minting) MUST compute
7
36
  * the root through this one function, so the off-chain root can never drift from what the
8
- * validator maintains. (It drifted before because api + engine each re-derived it independently.)
9
- *
10
- * Value = "" (empty) per the legacy/DeMi mint validator's `update_root`:
11
- * `mpt.insert(root, handle_name, #"", proof)`
12
- * Labels (001/002…) are NOT in the root until the in-band `MintLabelAssets` path is deployed;
13
- * until then the off-chain root MUST use empty values or it diverges from chain on every labeled
14
- * handle. When labels go in-band, change ONLY this function (accept a registry arg, set
15
- * value = registryValue.encode(freeNames, labels)) and every consumer moves in lockstep.
37
+ * validator maintains. (It drifted before because api + engine each re-derived it independently
38
+ * and again when this function ignored the free-virtual registry value the contract writes.)
16
39
  *
17
40
  * Order-independent (MPT root is determined by the key/value set), so callers need not sort;
18
41
  * duplicate keys are de-duped to avoid a Trie insert throw.
19
42
  */
20
- export declare const computeMintingDataRoot: (handleNames: string[]) => Promise<string>;
43
+ export declare const computeMintingDataRoot: (handles: MintingDataInput) => Promise<string>;
package/mpt/index.js CHANGED
@@ -23,37 +23,60 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.computeMintingDataRoot = exports.registryValue = exports.labelSet = void 0;
26
+ exports.computeMintingDataRoot = exports.buildMintingDataTrie = exports.Trie = exports.registryValue = exports.labelSet = void 0;
27
27
  // Server-only (Node) MPT module. Pulls @aiken-lang/merkle-patricia-forestry (native `level`/`blake2b`)
28
28
  // so it is NOT re-exported from the package root — import it from the subpath:
29
29
  // import { computeMintingDataRoot } from '@koralabs/kora-labs-common/mpt';
30
30
  // Mirrors the './aws' server-only convention; keeps the package root isomorphic/browser-safe.
31
31
  const merkle_patricia_forestry_1 = require("@aiken-lang/merkle-patricia-forestry");
32
+ Object.defineProperty(exports, "Trie", { enumerable: true, get: function () { return merkle_patricia_forestry_1.Trie; } });
32
33
  const labelSet = __importStar(require("./labelSet"));
33
34
  exports.labelSet = labelSet;
34
35
  const registryValue = __importStar(require("./registryValue"));
35
36
  exports.registryValue = registryValue;
36
37
  const EMPTY_ROOT_HEX = Buffer.alloc(32).toString('hex');
38
+ /**
39
+ * THE canonical minting-data MPT (a real {@link Trie}, for proof generation during mint/burn).
40
+ * Consumers that only need the root hash should use {@link computeMintingDataRoot}; consumers that
41
+ * generate proofs (the minting engine's verifyRootHash) keep the returned Trie.
42
+ *
43
+ * The value at each key is `registryValue.encode(freeNames, labels)` — byte-identical to what the
44
+ * on-chain `demimntmpt` validator stores (`mpt.update` verifies the old bytes). A handle with no
45
+ * free names and no labels encodes to "" (the legacy `update_root` `mpt.insert(root, name, #"")`
46
+ * case), so passing bare strings reproduces the empty-valued root exactly.
47
+ */
48
+ const buildMintingDataTrie = async (handles) => {
49
+ var _a, _b;
50
+ const seen = new Set();
51
+ const entries = [];
52
+ for (const handle of handles) {
53
+ const name = typeof handle === 'string' ? handle : handle.name;
54
+ if (!name || seen.has(name))
55
+ continue;
56
+ seen.add(name);
57
+ const freeNames = typeof handle === 'string' ? [] : (_a = handle.freeNames) !== null && _a !== void 0 ? _a : [];
58
+ const labels = typeof handle === 'string' ? '' : (_b = handle.labels) !== null && _b !== void 0 ? _b : '';
59
+ const encoded = registryValue.encode(freeNames, labels);
60
+ // MPF treats string values as UTF-8; supply the decoded bytes for a non-empty registry
61
+ // value, and "" (an empty value) for the common no-registry case.
62
+ entries.push({ key: name, value: encoded ? Buffer.from(encoded, 'hex') : '' });
63
+ }
64
+ return merkle_patricia_forestry_1.Trie.fromList(entries);
65
+ };
66
+ exports.buildMintingDataTrie = buildMintingDataTrie;
37
67
  /**
38
68
  * THE canonical minting-data MPT root. Every Node service (api.handle.me, the minting engine,
39
69
  * handle.me/bff) and the on-chain mirror (@koralabs/handles-decentralized-minting) MUST compute
40
70
  * the root through this one function, so the off-chain root can never drift from what the
41
- * validator maintains. (It drifted before because api + engine each re-derived it independently.)
42
- *
43
- * Value = "" (empty) per the legacy/DeMi mint validator's `update_root`:
44
- * `mpt.insert(root, handle_name, #"", proof)`
45
- * Labels (001/002…) are NOT in the root until the in-band `MintLabelAssets` path is deployed;
46
- * until then the off-chain root MUST use empty values or it diverges from chain on every labeled
47
- * handle. When labels go in-band, change ONLY this function (accept a registry arg, set
48
- * value = registryValue.encode(freeNames, labels)) and every consumer moves in lockstep.
71
+ * validator maintains. (It drifted before because api + engine each re-derived it independently
72
+ * and again when this function ignored the free-virtual registry value the contract writes.)
49
73
  *
50
74
  * Order-independent (MPT root is determined by the key/value set), so callers need not sort;
51
75
  * duplicate keys are de-duped to avoid a Trie insert throw.
52
76
  */
53
- const computeMintingDataRoot = async (handleNames) => {
77
+ const computeMintingDataRoot = async (handles) => {
54
78
  var _a, _b;
55
- const uniqueKeys = Array.from(new Set(handleNames));
56
- const trie = await merkle_patricia_forestry_1.Trie.fromList(uniqueKeys.map((key) => ({ key, value: '' })));
79
+ const trie = await (0, exports.buildMintingDataTrie)(handles);
57
80
  return (_b = (_a = trie.hash) === null || _a === void 0 ? void 0 : _a.toString('hex')) !== null && _b !== void 0 ? _b : EMPTY_ROOT_HEX;
58
81
  };
59
82
  exports.computeMintingDataRoot = computeMintingDataRoot;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koralabs/kora-labs-common",
3
- "version": "6.9.0",
3
+ "version": "6.9.2",
4
4
  "description": "Kora Labs Common Utilities",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",