@koralabs/kora-labs-common 6.9.1 → 6.9.3

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,29 +1,39 @@
1
1
  import { Trie } from '@aiken-lang/merkle-patricia-forestry';
2
2
  import * as labelSet from './labelSet';
3
- import * as registryValue from './registryValue';
4
- export { labelSet, registryValue };
3
+ export { labelSet };
5
4
  export { Trie };
5
+ /**
6
+ * A minting-data MPT entry. A bare string is shorthand for a handle with no registry value
7
+ * (value = ""). The object form carries the key's on-chain value — the sorted CIP-67 label set
8
+ * (001-004) bytes. (The 3-free-virtual feature was removed, so keys no longer carry a free-name
9
+ * value; the only non-empty value is the label set, once the in-band MintLabelAssets path ships.)
10
+ */
11
+ export interface MintingDataEntry {
12
+ name: string;
13
+ /** Sorted CIP-67 label set (001-004) hex. Empty/absent => value "". The in-band MintLabelAssets
14
+ * path is not on-chain yet, so callers pass "" today; flip to the real set when it ships. */
15
+ labels?: string;
16
+ }
17
+ type MintingDataInput = ReadonlyArray<string | MintingDataEntry>;
6
18
  /**
7
19
  * THE canonical minting-data MPT (a real {@link Trie}, for proof generation during mint/burn).
8
20
  * Consumers that only need the root hash should use {@link computeMintingDataRoot}; consumers that
9
- * generate proofs (the minting engine's verifyRootHash) keep the returned Trie. Value = "" per the
10
- * validator's `update_root` — see {@link computeMintingDataRoot} for the full rationale.
21
+ * generate proofs (the minting engine's verifyRootHash) keep the returned Trie.
22
+ *
23
+ * The value at each key is the sorted label-set bytes (CIP-67 001-004), byte-identical to what the
24
+ * on-chain `demimntmpt` validator stores (`mpt.update` verifies the old bytes). A handle with no
25
+ * labels => value "" (the `update_root` `mpt.insert(root, name, #"")` case), so passing bare strings
26
+ * reproduces the empty-valued root exactly — which is every key today (labels not yet on-chain).
11
27
  */
12
- export declare const buildMintingDataTrie: (handleNames: string[]) => Promise<Trie>;
28
+ export declare const buildMintingDataTrie: (handles: MintingDataInput) => Promise<Trie>;
13
29
  /**
14
30
  * THE canonical minting-data MPT root. Every Node service (api.handle.me, the minting engine,
15
31
  * handle.me/bff) and the on-chain mirror (@koralabs/handles-decentralized-minting) MUST compute
16
32
  * the root through this one function, so the off-chain root can never drift from what the
17
- * validator maintains. (It drifted before because api + engine each re-derived it independently.)
18
- *
19
- * Value = "" (empty) per the legacy/DeMi mint validator's `update_root`:
20
- * `mpt.insert(root, handle_name, #"", proof)`
21
- * Labels (001/002…) are NOT in the root until the in-band `MintLabelAssets` path is deployed;
22
- * until then the off-chain root MUST use empty values or it diverges from chain on every labeled
23
- * handle. When labels go in-band, change ONLY this function (accept a registry arg, set
24
- * value = registryValue.encode(freeNames, labels)) and every consumer moves in lockstep.
33
+ * validator maintains. (It drifted before because api + engine each re-derived it independently
34
+ * and again when this function ignored the free-virtual registry value the contract writes.)
25
35
  *
26
36
  * Order-independent (MPT root is determined by the key/value set), so callers need not sort;
27
37
  * duplicate keys are de-duped to avoid a Trie insert throw.
28
38
  */
29
- export declare const computeMintingDataRoot: (handleNames: string[]) => Promise<string>;
39
+ export declare const computeMintingDataRoot: (handles: MintingDataInput) => Promise<string>;
package/mpt/index.js CHANGED
@@ -23,7 +23,7 @@ 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.buildMintingDataTrie = exports.Trie = exports.registryValue = exports.labelSet = void 0;
26
+ exports.computeMintingDataRoot = exports.buildMintingDataTrie = exports.Trie = 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';
@@ -32,39 +32,48 @@ const merkle_patricia_forestry_1 = require("@aiken-lang/merkle-patricia-forestry
32
32
  Object.defineProperty(exports, "Trie", { enumerable: true, get: function () { return merkle_patricia_forestry_1.Trie; } });
33
33
  const labelSet = __importStar(require("./labelSet"));
34
34
  exports.labelSet = labelSet;
35
- const registryValue = __importStar(require("./registryValue"));
36
- exports.registryValue = registryValue;
37
35
  const EMPTY_ROOT_HEX = Buffer.alloc(32).toString('hex');
38
36
  /**
39
37
  * THE canonical minting-data MPT (a real {@link Trie}, for proof generation during mint/burn).
40
38
  * 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. Value = "" per the
42
- * validator's `update_root` — see {@link computeMintingDataRoot} for the full rationale.
39
+ * generate proofs (the minting engine's verifyRootHash) keep the returned Trie.
40
+ *
41
+ * The value at each key is the sorted label-set bytes (CIP-67 001-004), byte-identical to what the
42
+ * on-chain `demimntmpt` validator stores (`mpt.update` verifies the old bytes). A handle with no
43
+ * labels => value "" (the `update_root` `mpt.insert(root, name, #"")` case), so passing bare strings
44
+ * reproduces the empty-valued root exactly — which is every key today (labels not yet on-chain).
43
45
  */
44
- const buildMintingDataTrie = async (handleNames) => {
45
- const uniqueKeys = Array.from(new Set(handleNames));
46
- return merkle_patricia_forestry_1.Trie.fromList(uniqueKeys.map((key) => ({ key, value: '' })));
46
+ const buildMintingDataTrie = async (handles) => {
47
+ var _a;
48
+ const seen = new Set();
49
+ const entries = [];
50
+ for (const handle of handles) {
51
+ const name = typeof handle === 'string' ? handle : handle.name;
52
+ if (!name || seen.has(name))
53
+ continue;
54
+ seen.add(name);
55
+ const labels = typeof handle === 'string' ? '' : (_a = handle.labels) !== null && _a !== void 0 ? _a : '';
56
+ // The key's value is the sorted label-set bytes (CIP-67 001-004), or "" when it holds none.
57
+ // The 3-free-virtual feature was removed, so no key carries a free-name value any more.
58
+ // MPF treats string values as UTF-8; supply decoded bytes for a non-empty label set.
59
+ entries.push({ key: name, value: labels ? Buffer.from(labels, 'hex') : '' });
60
+ }
61
+ return merkle_patricia_forestry_1.Trie.fromList(entries);
47
62
  };
48
63
  exports.buildMintingDataTrie = buildMintingDataTrie;
49
64
  /**
50
65
  * THE canonical minting-data MPT root. Every Node service (api.handle.me, the minting engine,
51
66
  * handle.me/bff) and the on-chain mirror (@koralabs/handles-decentralized-minting) MUST compute
52
67
  * the root through this one function, so the off-chain root can never drift from what the
53
- * validator maintains. (It drifted before because api + engine each re-derived it independently.)
54
- *
55
- * Value = "" (empty) per the legacy/DeMi mint validator's `update_root`:
56
- * `mpt.insert(root, handle_name, #"", proof)`
57
- * Labels (001/002…) are NOT in the root until the in-band `MintLabelAssets` path is deployed;
58
- * until then the off-chain root MUST use empty values or it diverges from chain on every labeled
59
- * handle. When labels go in-band, change ONLY this function (accept a registry arg, set
60
- * value = registryValue.encode(freeNames, labels)) and every consumer moves in lockstep.
68
+ * validator maintains. (It drifted before because api + engine each re-derived it independently
69
+ * and again when this function ignored the free-virtual registry value the contract writes.)
61
70
  *
62
71
  * Order-independent (MPT root is determined by the key/value set), so callers need not sort;
63
72
  * duplicate keys are de-duped to avoid a Trie insert throw.
64
73
  */
65
- const computeMintingDataRoot = async (handleNames) => {
74
+ const computeMintingDataRoot = async (handles) => {
66
75
  var _a, _b;
67
- const trie = await (0, exports.buildMintingDataTrie)(handleNames);
76
+ const trie = await (0, exports.buildMintingDataTrie)(handles);
68
77
  return (_b = (_a = trie.hash) === null || _a === void 0 ? void 0 : _a.toString('hex')) !== null && _b !== void 0 ? _b : EMPTY_ROOT_HEX;
69
78
  };
70
79
  exports.computeMintingDataRoot = computeMintingDataRoot;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koralabs/kora-labs-common",
3
- "version": "6.9.1",
3
+ "version": "6.9.3",
4
4
  "description": "Kora Labs Common Utilities",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -1,25 +0,0 @@
1
- /**
2
- * Byte-identical to on-chain `registry_value.encode(free_names, labels)`.
3
- * free_names == [] -> labels (an empty handle stays "", any pure-label handle is WS1-identical)
4
- * free_names != [] -> "ff" ++ serialise_data(free_names) ++ labels
5
- */
6
- export declare const encode: (freeNames: string[], labels: string) => string;
7
- /** A free slot is available iff the root holds fewer than the configured free count. */
8
- export declare const hasFreeSlot: (freeNames: string[], freeVirtualCount: number) => boolean;
9
- /** Is this sub-name one of the root's current free names? */
10
- export declare const hasFreeName: (freeNames: string[], name: string) => boolean;
11
- /**
12
- * Add a sub-name to the free set (a free private-virtual MINT). Prepends (mirrors aiken
13
- * `list.push`) and is idempotent — the prepend order is part of the encoded bytes, so it MUST
14
- * match the contract's `add_free_name`.
15
- */
16
- export declare const addFreeName: (freeNames: string[], name: string) => string[];
17
- /**
18
- * Remove a sub-name from the free set (a free private-virtual BURN → reopen its slot). No-op if
19
- * the name isn't in the set (a paid sub's burn doesn't touch the allowance). Preserves order.
20
- */
21
- export declare const removeFreeName: (freeNames: string[], name: string) => string[];
22
- /** Parse a stored comma-hex free-name list ("" -> []). */
23
- export declare const parseFreeNames: (csv: string | null | undefined) => string[];
24
- /** Serialize a free-name set back to comma-hex for storage. */
25
- export declare const serializeFreeNames: (freeNames: string[]) => string;
@@ -1,78 +0,0 @@
1
- "use strict";
2
- // DSH-402 — TypeScript port of the on-chain `registry_value.ak`. The MPT value at a ROOT
3
- // handle key carries, alongside the WS1 label set, the SET OF NAMES of the root's currently-held
4
- // FREE private virtual subs (at most `free_virtual_count`). This MUST stay byte-identical to the
5
- // aiken `encode` so the off-chain `old_value`/`new_value` match what the validator reconstructs
6
- // (`mpt.update` verifies the old bytes are in the trie). If these diverge, every free-virtual
7
- // `mpt.update` fails on-chain.
8
- //
9
- // CANONICAL HOME: see ./labelSet.ts — this is the single shared definition for all Node services.
10
- //
11
- // Representation: free names and labels are lowercase hex strings; `encode` returns a hex string
12
- // ("" = empty value). Names are sub-handle name bytes; labels are concatenated 4-byte CIP-67
13
- // prefixes (each starting 0x00), so a leading 0xff marker can never collide with a label set.
14
- //
15
- // Encoding (mirrors `registry_value.ak`):
16
- // - free_names == [] -> labels (backward compatible with WS1)
17
- // - free_names != [] -> 0xff ++ serialise_data(free_names) ++ labels
18
- Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.serializeFreeNames = exports.parseFreeNames = exports.removeFreeName = exports.addFreeName = exports.hasFreeName = exports.hasFreeSlot = exports.encode = void 0;
20
- // CBOR header for a Plutus `Data` byte string (major type 2). Handle/sub names are CIP-67 asset
21
- // names (<= 32 bytes), so a single definite-length chunk always suffices; Plutus only chunks
22
- // byte strings longer than 64 bytes, which a valid handle name can never be — throw rather than
23
- // emit divergent bytes for an impossible input.
24
- const cborByteString = (hex) => {
25
- const byteLen = hex.length / 2;
26
- if (!Number.isInteger(byteLen)) {
27
- throw new Error(`free name is not valid hex (odd length): ${hex}`);
28
- }
29
- if (byteLen < 24)
30
- return (0x40 | byteLen).toString(16).padStart(2, '0') + hex;
31
- if (byteLen <= 64)
32
- return '58' + byteLen.toString(16).padStart(2, '0') + hex;
33
- throw new Error(`free name exceeds 64 bytes (Plutus would chunk): ${hex}`);
34
- };
35
- // Plutus `Data` CBOR of a non-empty `List<ByteArray>` (indefinite-length array).
36
- const serialiseData = (freeNames) => '9f' + freeNames.map((n) => cborByteString(n.toLowerCase())).join('') + 'ff';
37
- /**
38
- * Byte-identical to on-chain `registry_value.encode(free_names, labels)`.
39
- * free_names == [] -> labels (an empty handle stays "", any pure-label handle is WS1-identical)
40
- * free_names != [] -> "ff" ++ serialise_data(free_names) ++ labels
41
- */
42
- const encode = (freeNames, labels) => {
43
- const l = labels.toLowerCase();
44
- return freeNames.length === 0 ? l : 'ff' + serialiseData(freeNames) + l;
45
- };
46
- exports.encode = encode;
47
- /** A free slot is available iff the root holds fewer than the configured free count. */
48
- const hasFreeSlot = (freeNames, freeVirtualCount) => freeNames.length < freeVirtualCount;
49
- exports.hasFreeSlot = hasFreeSlot;
50
- /** Is this sub-name one of the root's current free names? */
51
- const hasFreeName = (freeNames, name) => freeNames.map((n) => n.toLowerCase()).includes(name.toLowerCase());
52
- exports.hasFreeName = hasFreeName;
53
- /**
54
- * Add a sub-name to the free set (a free private-virtual MINT). Prepends (mirrors aiken
55
- * `list.push`) and is idempotent — the prepend order is part of the encoded bytes, so it MUST
56
- * match the contract's `add_free_name`.
57
- */
58
- const addFreeName = (freeNames, name) => {
59
- const n = name.toLowerCase();
60
- const lower = freeNames.map((x) => x.toLowerCase());
61
- return lower.includes(n) ? lower : [n, ...lower];
62
- };
63
- exports.addFreeName = addFreeName;
64
- /**
65
- * Remove a sub-name from the free set (a free private-virtual BURN → reopen its slot). No-op if
66
- * the name isn't in the set (a paid sub's burn doesn't touch the allowance). Preserves order.
67
- */
68
- const removeFreeName = (freeNames, name) => {
69
- const n = name.toLowerCase();
70
- return freeNames.map((x) => x.toLowerCase()).filter((x) => x !== n);
71
- };
72
- exports.removeFreeName = removeFreeName;
73
- /** Parse a stored comma-hex free-name list ("" -> []). */
74
- const parseFreeNames = (csv) => (csv ? csv.split(',').map((s) => s.trim().toLowerCase()).filter(Boolean) : []);
75
- exports.parseFreeNames = parseFreeNames;
76
- /** Serialize a free-name set back to comma-hex for storage. */
77
- const serializeFreeNames = (freeNames) => freeNames.map((n) => n.toLowerCase()).join(',');
78
- exports.serializeFreeNames = serializeFreeNames;