@totemsdk/omnia-factory 0.5.2 → 0.5.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.
@@ -1,5 +1,5 @@
1
1
  export type { FactoryStatus, FactoryParticipant, FactorySignature, FactoryLogEntry, ChannelFactory, FactorySettlementPayload, FactoryDisputePayload, FactoryLeaseOps, WotsLeaseBundle, OmniaChannel, } from './types.js';
2
- export { buildFactoryScript, buildAndHashFactoryScript, factoryBalanceStateVariables, factoryEscapeStateVariable, scriptAddress, normalizeScript, } from './script.js';
2
+ export { buildFactoryScript, buildAndHashFactoryScript, factoryBalanceStateVariables, factoryEscapeStateVariable, factoryPartySetRoot, factoryPartyRootStateVariable, factoryPartyProof, factoryBalanceSetRoot, factoryBalanceProof, FACTORY_BALANCE_PORT_BASE, FACTORY_ESCAPE_PORT, FACTORY_ESCAPE_DELAY_BLOCKS, FACTORY_MAX_PARTICIPANTS, FACTORY_PARTY_ROOT_PORT, MSHS_PORT_FLOOR, scriptAddress, normalizeScript, } from './script.js';
3
3
  export { computeFactoryStateCommitment } from './commitment.js';
4
4
  export { createFactory, acceptFactory, reallocate, enforceConservation, signFactoryOpening, verifyFactorySignature, applyFactorySignature, } from './factory.js';
5
5
  export type { FactoryOpeningSignature } from './factory.js';
package/dist/cjs/index.js CHANGED
@@ -1,11 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createDurableFactoryStore = exports.assembleFactorySettlement = exports.verifyFactorySettlementSignature = exports.signFactorySettlement = exports.buildFactorySettlement = exports.buildDisputePayload = exports.closeFactory = exports.assembleVirtualChannelOpen = exports.verifyVirtualChannelOpenSignature = exports.signVirtualChannelOpen = exports.buildVirtualChannelOpen = exports.closeVirtualChannel = exports.openVirtualChannel = exports.createFactoryCoordination = exports.applyFactorySignature = exports.verifyFactorySignature = exports.signFactoryOpening = exports.enforceConservation = exports.reallocate = exports.acceptFactory = exports.createFactory = exports.computeFactoryStateCommitment = exports.normalizeScript = exports.scriptAddress = exports.factoryEscapeStateVariable = exports.factoryBalanceStateVariables = exports.buildAndHashFactoryScript = exports.buildFactoryScript = void 0;
3
+ exports.createDurableFactoryStore = exports.assembleFactorySettlement = exports.verifyFactorySettlementSignature = exports.signFactorySettlement = exports.buildFactorySettlement = exports.buildDisputePayload = exports.closeFactory = exports.assembleVirtualChannelOpen = exports.verifyVirtualChannelOpenSignature = exports.signVirtualChannelOpen = exports.buildVirtualChannelOpen = exports.closeVirtualChannel = exports.openVirtualChannel = exports.createFactoryCoordination = exports.applyFactorySignature = exports.verifyFactorySignature = exports.signFactoryOpening = exports.enforceConservation = exports.reallocate = exports.acceptFactory = exports.createFactory = exports.computeFactoryStateCommitment = exports.normalizeScript = exports.scriptAddress = exports.MSHS_PORT_FLOOR = exports.FACTORY_PARTY_ROOT_PORT = exports.FACTORY_MAX_PARTICIPANTS = exports.FACTORY_ESCAPE_DELAY_BLOCKS = exports.FACTORY_ESCAPE_PORT = exports.FACTORY_BALANCE_PORT_BASE = exports.factoryBalanceProof = exports.factoryBalanceSetRoot = exports.factoryPartyProof = exports.factoryPartyRootStateVariable = exports.factoryPartySetRoot = exports.factoryEscapeStateVariable = exports.factoryBalanceStateVariables = exports.buildAndHashFactoryScript = exports.buildFactoryScript = void 0;
4
4
  var script_js_1 = require("./script.js");
5
5
  Object.defineProperty(exports, "buildFactoryScript", { enumerable: true, get: function () { return script_js_1.buildFactoryScript; } });
6
6
  Object.defineProperty(exports, "buildAndHashFactoryScript", { enumerable: true, get: function () { return script_js_1.buildAndHashFactoryScript; } });
7
7
  Object.defineProperty(exports, "factoryBalanceStateVariables", { enumerable: true, get: function () { return script_js_1.factoryBalanceStateVariables; } });
8
8
  Object.defineProperty(exports, "factoryEscapeStateVariable", { enumerable: true, get: function () { return script_js_1.factoryEscapeStateVariable; } });
9
+ Object.defineProperty(exports, "factoryPartySetRoot", { enumerable: true, get: function () { return script_js_1.factoryPartySetRoot; } });
10
+ Object.defineProperty(exports, "factoryPartyRootStateVariable", { enumerable: true, get: function () { return script_js_1.factoryPartyRootStateVariable; } });
11
+ Object.defineProperty(exports, "factoryPartyProof", { enumerable: true, get: function () { return script_js_1.factoryPartyProof; } });
12
+ Object.defineProperty(exports, "factoryBalanceSetRoot", { enumerable: true, get: function () { return script_js_1.factoryBalanceSetRoot; } });
13
+ Object.defineProperty(exports, "factoryBalanceProof", { enumerable: true, get: function () { return script_js_1.factoryBalanceProof; } });
14
+ Object.defineProperty(exports, "FACTORY_BALANCE_PORT_BASE", { enumerable: true, get: function () { return script_js_1.FACTORY_BALANCE_PORT_BASE; } });
15
+ Object.defineProperty(exports, "FACTORY_ESCAPE_PORT", { enumerable: true, get: function () { return script_js_1.FACTORY_ESCAPE_PORT; } });
16
+ Object.defineProperty(exports, "FACTORY_ESCAPE_DELAY_BLOCKS", { enumerable: true, get: function () { return script_js_1.FACTORY_ESCAPE_DELAY_BLOCKS; } });
17
+ Object.defineProperty(exports, "FACTORY_MAX_PARTICIPANTS", { enumerable: true, get: function () { return script_js_1.FACTORY_MAX_PARTICIPANTS; } });
18
+ Object.defineProperty(exports, "FACTORY_PARTY_ROOT_PORT", { enumerable: true, get: function () { return script_js_1.FACTORY_PARTY_ROOT_PORT; } });
19
+ Object.defineProperty(exports, "MSHS_PORT_FLOOR", { enumerable: true, get: function () { return script_js_1.MSHS_PORT_FLOOR; } });
9
20
  Object.defineProperty(exports, "scriptAddress", { enumerable: true, get: function () { return script_js_1.scriptAddress; } });
10
21
  Object.defineProperty(exports, "normalizeScript", { enumerable: true, get: function () { return script_js_1.normalizeScript; } });
11
22
  var commitment_js_1 = require("./commitment.js");
@@ -3,14 +3,68 @@ import type { FactoryParticipant } from './types.js';
3
3
  export declare const FACTORY_ESCAPE_DELAY_BLOCKS = 256;
4
4
  /** State port carrying the absolute escape block (STATE(103)). */
5
5
  export declare const FACTORY_ESCAPE_PORT = 103;
6
- /** State port base for per-party allocations: party k ⇒ port 200+k. */
7
- export declare const FACTORY_BALANCE_PORT_BASE = 200;
6
+ /**
7
+ * State port carrying the committed party-set root (STATE(111)) — the MMR root
8
+ * of the participant public-key digests (RFC-048 §12.4/§12.5). It is pinned on
9
+ * every spend (`SAMESTATE`), so the participant set is cryptographically fixed
10
+ * and immutable, independent of the per-party enumeration. This is the factory's
11
+ * adoption of the VTXO committed-set root and the anchor for future per-leaf
12
+ * (proof-based) scaling.
13
+ */
14
+ export declare const FACTORY_PARTY_ROOT_PORT = 111;
15
+ /** MMR root (hex, no `0x`) over the participants' public-key digests.
16
+ *
17
+ * Minima's `MMRTree` builds a perfect binary tree (power-of-two leaves), so the
18
+ * party set is padded to the next power of two with the **zero digest** sentinel
19
+ * (a fixed, publicly known padding leaf). The root is thus a Minima-native MMR
20
+ * root — the same structure the VTXO batch root commits and the on-chain `PROOF`
21
+ * verifies — over an arbitrary number of participants.
22
+ */
23
+ export declare function factoryPartySetRoot(participants: FactoryParticipant[]): string;
24
+ /**
25
+ * Serialized MMR inclusion proof (hex, no `0x`) for participant `index` against
26
+ * the committed party-set root — the witness for a per-leaf
27
+ * `buildMmrMembershipCovenant` escape (RFC-048 §12.4).
28
+ */
29
+ export declare function factoryPartyProof(participants: FactoryParticipant[], index: number): string;
30
+ /**
31
+ * Balance set-root (RFC-048 §12.4) — the MMR root over `(pubkey, balance)` leaves,
32
+ * where each participant's `contributionAmount` is the **leaf sum**. This commits
33
+ * the exact distribution of the factory balance (`rootSum` = the total) and is the
34
+ * scalable, off-chain commitment that replaces per-party balance ports.
35
+ */
36
+ export declare function factoryBalanceSetRoot(participants: FactoryParticipant[]): {
37
+ root: string;
38
+ rootSum: bigint;
39
+ };
40
+ /**
41
+ * Serialized MMR inclusion proof (hex, no `0x`) for participant `index` against
42
+ * the committed balance set-root — the witness for a `buildMmrMembershipCovenant`
43
+ * balance escape (leaf sum = the participant's balance).
44
+ */
45
+ export declare function factoryBalanceProof(participants: FactoryParticipant[], index: number): string;
46
+ /** Committed party-set root state variable (STATE(111)) for the factory funding output. */
47
+ export declare function factoryPartyRootStateVariable(root: string): {
48
+ port: number;
49
+ value: string;
50
+ type: 'hex';
51
+ };
52
+ /** State port base for per-party allocations: party k ⇒ `base + k` (below the MSHS floor). */
53
+ export declare const FACTORY_BALANCE_PORT_BASE = 128;
54
+ /** Lowest port reserved for MSHS-2 (MASH); Omnia ports MUST stay below this.
55
+ * The node installs the MASH authorization bundle at ports **230–255**
56
+ * (`bundleBase = 256 − (4 + k) = 230`): the header R / ROOTS_BLOB /
57
+ * SECRETS_BLOB / TREE_SIG at 230–233 and PROOF at 234–255. A composite
58
+ * MASH + Omnia coin must therefore keep Omnia ports ≤ 229. */
59
+ export declare const MSHS_PORT_FLOOR = 230;
60
+ /** Maximum participants whose balance ports remain below the MSHS floor. */
61
+ export declare const FACTORY_MAX_PARTICIPANTS: number;
8
62
  export interface FactoryScriptOptions {
9
63
  /** Reserved for callers; the escape block is carried in STATE(103) at funding. */
10
64
  escapeBlock?: number;
11
65
  }
12
66
  /**
13
- * Per-party allocation state variables (port 200+k = party k's balance), seeded
67
+ * Per-party allocation state variables (port base+k = party k's balance), seeded
14
68
  * into the factory funding output so the timeout escape can pay each party its
15
69
  * committed share.
16
70
  */
@@ -33,9 +87,9 @@ export declare function factoryEscapeStateVariable(escapeBlock: number): {
33
87
  * - **Cooperative / update** (any time): `MULTISIG(N …)` — all parties sign.
34
88
  * `SETTLEMENT=true` closes after minimal coinage (1 block).
35
89
  * - **Unilateral escape** (after the absolute block in `STATE(103)`): any
36
- * single party k may withdraw its allocation `SHARE_k = PREVSTATE(200+k)`
90
+ * single party k may withdraw its allocation `SHARE_k = PREVSTATE(base+k)`
37
91
  * (the spent coin's share) to its own settlement address, zero its state in
38
- * the successor (`STATE(200+k) EQ 0`), keep the other parties' balances
92
+ * the successor (`STATE(base+k) EQ 0`), keep the other parties' balances
39
93
  * unchanged, and return the remainder to the factory script. Repeated
40
94
  * withdrawals cannot double-claim because each zeroes its own port. `STATE(103)` is immutable on every spend (`SAMESTATE(103 103)`),
41
95
  * so the escape clock cannot be pushed by any counterparty.
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FACTORY_BALANCE_PORT_BASE = exports.FACTORY_ESCAPE_PORT = exports.FACTORY_ESCAPE_DELAY_BLOCKS = void 0;
3
+ exports.FACTORY_MAX_PARTICIPANTS = exports.MSHS_PORT_FLOOR = exports.FACTORY_BALANCE_PORT_BASE = exports.FACTORY_PARTY_ROOT_PORT = exports.FACTORY_ESCAPE_PORT = exports.FACTORY_ESCAPE_DELAY_BLOCKS = void 0;
4
+ exports.factoryPartySetRoot = factoryPartySetRoot;
5
+ exports.factoryPartyProof = factoryPartyProof;
6
+ exports.factoryBalanceSetRoot = factoryBalanceSetRoot;
7
+ exports.factoryBalanceProof = factoryBalanceProof;
8
+ exports.factoryPartyRootStateVariable = factoryPartyRootStateVariable;
4
9
  exports.factoryBalanceStateVariables = factoryBalanceStateVariables;
5
10
  exports.factoryEscapeStateVariable = factoryEscapeStateVariable;
6
11
  exports.buildFactoryScript = buildFactoryScript;
@@ -8,6 +13,7 @@ exports.normalizeScript = normalizeScript;
8
13
  exports.scriptAddress = scriptAddress;
9
14
  exports.buildAndHashFactoryScript = buildAndHashFactoryScript;
10
15
  const scripts_1 = require("@totemsdk/core/scripts");
16
+ const core_1 = require("@totemsdk/core");
11
17
  /**
12
18
  * Normalize a hex public key digest to the KISSVM-compatible format:
13
19
  * strip leading 0x/0X and render as upper-case hex with leading '0X'.
@@ -20,10 +26,129 @@ function kissHex(hex) {
20
26
  exports.FACTORY_ESCAPE_DELAY_BLOCKS = 256;
21
27
  /** State port carrying the absolute escape block (STATE(103)). */
22
28
  exports.FACTORY_ESCAPE_PORT = 103;
23
- /** State port base for per-party allocations: party k ⇒ port 200+k. */
24
- exports.FACTORY_BALANCE_PORT_BASE = 200;
25
29
  /**
26
- * Per-party allocation state variables (port 200+k = party k's balance), seeded
30
+ * State port carrying the committed party-set root (STATE(111)) — the MMR root
31
+ * of the participant public-key digests (RFC-048 §12.4/§12.5). It is pinned on
32
+ * every spend (`SAMESTATE`), so the participant set is cryptographically fixed
33
+ * and immutable, independent of the per-party enumeration. This is the factory's
34
+ * adoption of the VTXO committed-set root and the anchor for future per-leaf
35
+ * (proof-based) scaling.
36
+ */
37
+ exports.FACTORY_PARTY_ROOT_PORT = 111;
38
+ /** Public-key digests for the party set, padded to a power of two (Minima MMR). */
39
+ function padPartySet(participants) {
40
+ if (!Array.isArray(participants) || participants.length === 0) {
41
+ throw new Error('factory party set requires at least one participant');
42
+ }
43
+ const pubkeys = participants.map((p, i) => {
44
+ const raw = (p.publicKeyDigest ?? '').replace(/^0x/i, '');
45
+ if (raw.length === 0 || !/^[0-9a-fA-F]+$/.test(raw)) {
46
+ throw new Error(`participant[${i}].publicKeyDigest must be a hex string`);
47
+ }
48
+ return (0, core_1.hexToBytes)(raw);
49
+ });
50
+ let size = 1;
51
+ while (size < pubkeys.length)
52
+ size <<= 1;
53
+ while (pubkeys.length < size)
54
+ pubkeys.push(new Uint8Array(32));
55
+ return pubkeys;
56
+ }
57
+ /** MMR root (hex, no `0x`) over the participants' public-key digests.
58
+ *
59
+ * Minima's `MMRTree` builds a perfect binary tree (power-of-two leaves), so the
60
+ * party set is padded to the next power of two with the **zero digest** sentinel
61
+ * (a fixed, publicly known padding leaf). The root is thus a Minima-native MMR
62
+ * root — the same structure the VTXO batch root commits and the on-chain `PROOF`
63
+ * verifies — over an arbitrary number of participants.
64
+ */
65
+ function factoryPartySetRoot(participants) {
66
+ const root = core_1.MMRTree.fromPublicKeys(padPartySet(participants)).getRoot();
67
+ if (!root)
68
+ throw new Error('factoryPartySetRoot: failed to compute the party-set root');
69
+ return (0, core_1.bytesToHex)(root.data).toLowerCase();
70
+ }
71
+ /**
72
+ * Serialized MMR inclusion proof (hex, no `0x`) for participant `index` against
73
+ * the committed party-set root — the witness for a per-leaf
74
+ * `buildMmrMembershipCovenant` escape (RFC-048 §12.4).
75
+ */
76
+ function factoryPartyProof(participants, index) {
77
+ if (!Number.isInteger(index) || index < 0 || index >= participants.length) {
78
+ throw new Error(`factoryPartyProof: index ${index} is out of range (0..${participants.length - 1})`);
79
+ }
80
+ const proof = core_1.MMRTree.fromPublicKeys(padPartySet(participants)).getProof(index);
81
+ return (0, core_1.bytesToHex)((0, core_1.serializeMMRProof)(proof)).toLowerCase();
82
+ }
83
+ /**
84
+ * Balance set-root (RFC-048 §12.4) — the MMR root over `(pubkey, balance)` leaves,
85
+ * where each participant's `contributionAmount` is the **leaf sum**. This commits
86
+ * the exact distribution of the factory balance (`rootSum` = the total) and is the
87
+ * scalable, off-chain commitment that replaces per-party balance ports.
88
+ */
89
+ function factoryBalanceSetRoot(participants) {
90
+ const tree = buildBalanceTree(participants);
91
+ const root = tree.getRoot();
92
+ if (!root)
93
+ throw new Error('factoryBalanceSetRoot: failed to compute the balance set-root');
94
+ return { root: (0, core_1.bytesToHex)(root.data).toLowerCase(), rootSum: root.value };
95
+ }
96
+ /**
97
+ * Serialized MMR inclusion proof (hex, no `0x`) for participant `index` against
98
+ * the committed balance set-root — the witness for a `buildMmrMembershipCovenant`
99
+ * balance escape (leaf sum = the participant's balance).
100
+ */
101
+ function factoryBalanceProof(participants, index) {
102
+ if (!Number.isInteger(index) || index < 0 || index >= participants.length) {
103
+ throw new Error(`factoryBalanceProof: index ${index} is out of range (0..${participants.length - 1})`);
104
+ }
105
+ const proof = buildBalanceTree(participants).getProof(index);
106
+ return (0, core_1.bytesToHex)((0, core_1.serializeMMRProof)(proof)).toLowerCase();
107
+ }
108
+ /** Padded (power-of-two) MMR tree over `(pubkey, balance)` leaves. */
109
+ function buildBalanceTree(participants) {
110
+ if (!Array.isArray(participants) || participants.length === 0) {
111
+ throw new Error('factory balance set requires at least one participant');
112
+ }
113
+ const tree = new core_1.MMRTree();
114
+ for (const [i, p] of participants.entries()) {
115
+ const raw = (p.publicKeyDigest ?? '').replace(/^0x/i, '');
116
+ if (raw.length === 0 || !/^[0-9a-fA-F]+$/.test(raw)) {
117
+ throw new Error(`participant[${i}].publicKeyDigest must be a hex string`);
118
+ }
119
+ if (p.contributionAmount < 0n) {
120
+ throw new Error(`participant[${i}].contributionAmount must be non-negative`);
121
+ }
122
+ tree.addLeaf((0, core_1.createMMRDataLeafNode)((0, core_1.hexToBytes)(raw), p.contributionAmount));
123
+ }
124
+ let size = 1;
125
+ while (size < participants.length)
126
+ size <<= 1;
127
+ for (let i = participants.length; i < size; i++) {
128
+ tree.addLeaf((0, core_1.createMMRDataLeafNode)(new Uint8Array(32), 0n));
129
+ }
130
+ return tree;
131
+ }
132
+ /** Committed party-set root state variable (STATE(111)) for the factory funding output. */
133
+ function factoryPartyRootStateVariable(root) {
134
+ const raw = root.replace(/^0x/i, '');
135
+ if (!/^[0-9a-fA-F]{64}$/.test(raw)) {
136
+ throw new Error(`factoryPartyRootStateVariable: root must be 32 bytes of hex; got ${JSON.stringify(root)}`);
137
+ }
138
+ return { port: exports.FACTORY_PARTY_ROOT_PORT, value: '0x' + raw, type: 'hex' };
139
+ }
140
+ /** State port base for per-party allocations: party k ⇒ `base + k` (below the MSHS floor). */
141
+ exports.FACTORY_BALANCE_PORT_BASE = 128;
142
+ /** Lowest port reserved for MSHS-2 (MASH); Omnia ports MUST stay below this.
143
+ * The node installs the MASH authorization bundle at ports **230–255**
144
+ * (`bundleBase = 256 − (4 + k) = 230`): the header R / ROOTS_BLOB /
145
+ * SECRETS_BLOB / TREE_SIG at 230–233 and PROOF at 234–255. A composite
146
+ * MASH + Omnia coin must therefore keep Omnia ports ≤ 229. */
147
+ exports.MSHS_PORT_FLOOR = 230;
148
+ /** Maximum participants whose balance ports remain below the MSHS floor. */
149
+ exports.FACTORY_MAX_PARTICIPANTS = exports.MSHS_PORT_FLOOR - exports.FACTORY_BALANCE_PORT_BASE;
150
+ /**
151
+ * Per-party allocation state variables (port base+k = party k's balance), seeded
27
152
  * into the factory funding output so the timeout escape can pay each party its
28
153
  * committed share.
29
154
  */
@@ -49,9 +174,9 @@ function factoryEscapeStateVariable(escapeBlock) {
49
174
  * - **Cooperative / update** (any time): `MULTISIG(N …)` — all parties sign.
50
175
  * `SETTLEMENT=true` closes after minimal coinage (1 block).
51
176
  * - **Unilateral escape** (after the absolute block in `STATE(103)`): any
52
- * single party k may withdraw its allocation `SHARE_k = PREVSTATE(200+k)`
177
+ * single party k may withdraw its allocation `SHARE_k = PREVSTATE(base+k)`
53
178
  * (the spent coin's share) to its own settlement address, zero its state in
54
- * the successor (`STATE(200+k) EQ 0`), keep the other parties' balances
179
+ * the successor (`STATE(base+k) EQ 0`), keep the other parties' balances
55
180
  * unchanged, and return the remainder to the factory script. Repeated
56
181
  * withdrawals cannot double-claim because each zeroes its own port. `STATE(103)` is immutable on every spend (`SAMESTATE(103 103)`),
57
182
  * so the escape clock cannot be pushed by any counterparty.
@@ -62,12 +187,17 @@ function buildFactoryScript(participants, _options = {}) {
62
187
  if (participants.length < 2) {
63
188
  throw new Error(`Factory requires at least 2 participants, got ${participants.length}`);
64
189
  }
190
+ if (participants.length > exports.FACTORY_MAX_PARTICIPANTS) {
191
+ throw new Error(`Factory supports at most ${exports.FACTORY_MAX_PARTICIPANTS} participants (got ${participants.length}) — balance ports would reach the MSHS-2 band (≥${exports.MSHS_PORT_FLOOR})`);
192
+ }
65
193
  const count = participants.length;
66
194
  const pks = participants.map((p) => kissHex(p.publicKeyDigest));
67
195
  const payouts = participants.map((p) => kissHex(p.settlementAddress ?? p.publicKeyDigest));
68
196
  const lines = [
69
197
  'LET SETTLEMENT=STATE(100)',
70
198
  `ASSERT SAMESTATE(${exports.FACTORY_ESCAPE_PORT} ${exports.FACTORY_ESCAPE_PORT})`,
199
+ // RFC-048 §12.4/§12.5: the committed party-set root is immutable on every spend.
200
+ `ASSERT SAMESTATE(${exports.FACTORY_PARTY_ROOT_PORT} ${exports.FACTORY_PARTY_ROOT_PORT})`,
71
201
  `IF @BLOCK GT PREVSTATE(${exports.FACTORY_ESCAPE_PORT}) THEN`,
72
202
  ];
73
203
  participants.forEach((_, k) => {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export type { FactoryStatus, FactoryParticipant, FactorySignature, FactoryLogEntry, ChannelFactory, FactorySettlementPayload, FactoryDisputePayload, FactoryLeaseOps, WotsLeaseBundle, OmniaChannel, } from './types.js';
2
- export { buildFactoryScript, buildAndHashFactoryScript, factoryBalanceStateVariables, factoryEscapeStateVariable, scriptAddress, normalizeScript, } from './script.js';
2
+ export { buildFactoryScript, buildAndHashFactoryScript, factoryBalanceStateVariables, factoryEscapeStateVariable, factoryPartySetRoot, factoryPartyRootStateVariable, factoryPartyProof, factoryBalanceSetRoot, factoryBalanceProof, FACTORY_BALANCE_PORT_BASE, FACTORY_ESCAPE_PORT, FACTORY_ESCAPE_DELAY_BLOCKS, FACTORY_MAX_PARTICIPANTS, FACTORY_PARTY_ROOT_PORT, MSHS_PORT_FLOOR, scriptAddress, normalizeScript, } from './script.js';
3
3
  export { computeFactoryStateCommitment } from './commitment.js';
4
4
  export { createFactory, acceptFactory, reallocate, enforceConservation, signFactoryOpening, verifyFactorySignature, applyFactorySignature, } from './factory.js';
5
5
  export type { FactoryOpeningSignature } from './factory.js';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { buildFactoryScript, buildAndHashFactoryScript, factoryBalanceStateVariables, factoryEscapeStateVariable, scriptAddress, normalizeScript, } from './script.js';
1
+ export { buildFactoryScript, buildAndHashFactoryScript, factoryBalanceStateVariables, factoryEscapeStateVariable, factoryPartySetRoot, factoryPartyRootStateVariable, factoryPartyProof, factoryBalanceSetRoot, factoryBalanceProof, FACTORY_BALANCE_PORT_BASE, FACTORY_ESCAPE_PORT, FACTORY_ESCAPE_DELAY_BLOCKS, FACTORY_MAX_PARTICIPANTS, FACTORY_PARTY_ROOT_PORT, MSHS_PORT_FLOOR, scriptAddress, normalizeScript, } from './script.js';
2
2
  export { computeFactoryStateCommitment } from './commitment.js';
3
3
  export { createFactory, acceptFactory, reallocate, enforceConservation, signFactoryOpening, verifyFactorySignature, applyFactorySignature, } from './factory.js';
4
4
  // RFC-035 B2 — factory opening round-trip over the coordination transport.
package/dist/script.d.ts CHANGED
@@ -3,14 +3,68 @@ import type { FactoryParticipant } from './types.js';
3
3
  export declare const FACTORY_ESCAPE_DELAY_BLOCKS = 256;
4
4
  /** State port carrying the absolute escape block (STATE(103)). */
5
5
  export declare const FACTORY_ESCAPE_PORT = 103;
6
- /** State port base for per-party allocations: party k ⇒ port 200+k. */
7
- export declare const FACTORY_BALANCE_PORT_BASE = 200;
6
+ /**
7
+ * State port carrying the committed party-set root (STATE(111)) — the MMR root
8
+ * of the participant public-key digests (RFC-048 §12.4/§12.5). It is pinned on
9
+ * every spend (`SAMESTATE`), so the participant set is cryptographically fixed
10
+ * and immutable, independent of the per-party enumeration. This is the factory's
11
+ * adoption of the VTXO committed-set root and the anchor for future per-leaf
12
+ * (proof-based) scaling.
13
+ */
14
+ export declare const FACTORY_PARTY_ROOT_PORT = 111;
15
+ /** MMR root (hex, no `0x`) over the participants' public-key digests.
16
+ *
17
+ * Minima's `MMRTree` builds a perfect binary tree (power-of-two leaves), so the
18
+ * party set is padded to the next power of two with the **zero digest** sentinel
19
+ * (a fixed, publicly known padding leaf). The root is thus a Minima-native MMR
20
+ * root — the same structure the VTXO batch root commits and the on-chain `PROOF`
21
+ * verifies — over an arbitrary number of participants.
22
+ */
23
+ export declare function factoryPartySetRoot(participants: FactoryParticipant[]): string;
24
+ /**
25
+ * Serialized MMR inclusion proof (hex, no `0x`) for participant `index` against
26
+ * the committed party-set root — the witness for a per-leaf
27
+ * `buildMmrMembershipCovenant` escape (RFC-048 §12.4).
28
+ */
29
+ export declare function factoryPartyProof(participants: FactoryParticipant[], index: number): string;
30
+ /**
31
+ * Balance set-root (RFC-048 §12.4) — the MMR root over `(pubkey, balance)` leaves,
32
+ * where each participant's `contributionAmount` is the **leaf sum**. This commits
33
+ * the exact distribution of the factory balance (`rootSum` = the total) and is the
34
+ * scalable, off-chain commitment that replaces per-party balance ports.
35
+ */
36
+ export declare function factoryBalanceSetRoot(participants: FactoryParticipant[]): {
37
+ root: string;
38
+ rootSum: bigint;
39
+ };
40
+ /**
41
+ * Serialized MMR inclusion proof (hex, no `0x`) for participant `index` against
42
+ * the committed balance set-root — the witness for a `buildMmrMembershipCovenant`
43
+ * balance escape (leaf sum = the participant's balance).
44
+ */
45
+ export declare function factoryBalanceProof(participants: FactoryParticipant[], index: number): string;
46
+ /** Committed party-set root state variable (STATE(111)) for the factory funding output. */
47
+ export declare function factoryPartyRootStateVariable(root: string): {
48
+ port: number;
49
+ value: string;
50
+ type: 'hex';
51
+ };
52
+ /** State port base for per-party allocations: party k ⇒ `base + k` (below the MSHS floor). */
53
+ export declare const FACTORY_BALANCE_PORT_BASE = 128;
54
+ /** Lowest port reserved for MSHS-2 (MASH); Omnia ports MUST stay below this.
55
+ * The node installs the MASH authorization bundle at ports **230–255**
56
+ * (`bundleBase = 256 − (4 + k) = 230`): the header R / ROOTS_BLOB /
57
+ * SECRETS_BLOB / TREE_SIG at 230–233 and PROOF at 234–255. A composite
58
+ * MASH + Omnia coin must therefore keep Omnia ports ≤ 229. */
59
+ export declare const MSHS_PORT_FLOOR = 230;
60
+ /** Maximum participants whose balance ports remain below the MSHS floor. */
61
+ export declare const FACTORY_MAX_PARTICIPANTS: number;
8
62
  export interface FactoryScriptOptions {
9
63
  /** Reserved for callers; the escape block is carried in STATE(103) at funding. */
10
64
  escapeBlock?: number;
11
65
  }
12
66
  /**
13
- * Per-party allocation state variables (port 200+k = party k's balance), seeded
67
+ * Per-party allocation state variables (port base+k = party k's balance), seeded
14
68
  * into the factory funding output so the timeout escape can pay each party its
15
69
  * committed share.
16
70
  */
@@ -33,9 +87,9 @@ export declare function factoryEscapeStateVariable(escapeBlock: number): {
33
87
  * - **Cooperative / update** (any time): `MULTISIG(N …)` — all parties sign.
34
88
  * `SETTLEMENT=true` closes after minimal coinage (1 block).
35
89
  * - **Unilateral escape** (after the absolute block in `STATE(103)`): any
36
- * single party k may withdraw its allocation `SHARE_k = PREVSTATE(200+k)`
90
+ * single party k may withdraw its allocation `SHARE_k = PREVSTATE(base+k)`
37
91
  * (the spent coin's share) to its own settlement address, zero its state in
38
- * the successor (`STATE(200+k) EQ 0`), keep the other parties' balances
92
+ * the successor (`STATE(base+k) EQ 0`), keep the other parties' balances
39
93
  * unchanged, and return the remainder to the factory script. Repeated
40
94
  * withdrawals cannot double-claim because each zeroes its own port. `STATE(103)` is immutable on every spend (`SAMESTATE(103 103)`),
41
95
  * so the escape clock cannot be pushed by any counterparty.
package/dist/script.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { computeScriptAddress } from '@totemsdk/core/scripts';
2
+ import { MMRTree, hexToBytes, bytesToHex, serializeMMRProof, createMMRDataLeafNode } from '@totemsdk/core';
2
3
  /**
3
4
  * Normalize a hex public key digest to the KISSVM-compatible format:
4
5
  * strip leading 0x/0X and render as upper-case hex with leading '0X'.
@@ -11,10 +12,129 @@ function kissHex(hex) {
11
12
  export const FACTORY_ESCAPE_DELAY_BLOCKS = 256;
12
13
  /** State port carrying the absolute escape block (STATE(103)). */
13
14
  export const FACTORY_ESCAPE_PORT = 103;
14
- /** State port base for per-party allocations: party k ⇒ port 200+k. */
15
- export const FACTORY_BALANCE_PORT_BASE = 200;
16
15
  /**
17
- * Per-party allocation state variables (port 200+k = party k's balance), seeded
16
+ * State port carrying the committed party-set root (STATE(111)) — the MMR root
17
+ * of the participant public-key digests (RFC-048 §12.4/§12.5). It is pinned on
18
+ * every spend (`SAMESTATE`), so the participant set is cryptographically fixed
19
+ * and immutable, independent of the per-party enumeration. This is the factory's
20
+ * adoption of the VTXO committed-set root and the anchor for future per-leaf
21
+ * (proof-based) scaling.
22
+ */
23
+ export const FACTORY_PARTY_ROOT_PORT = 111;
24
+ /** Public-key digests for the party set, padded to a power of two (Minima MMR). */
25
+ function padPartySet(participants) {
26
+ if (!Array.isArray(participants) || participants.length === 0) {
27
+ throw new Error('factory party set requires at least one participant');
28
+ }
29
+ const pubkeys = participants.map((p, i) => {
30
+ const raw = (p.publicKeyDigest ?? '').replace(/^0x/i, '');
31
+ if (raw.length === 0 || !/^[0-9a-fA-F]+$/.test(raw)) {
32
+ throw new Error(`participant[${i}].publicKeyDigest must be a hex string`);
33
+ }
34
+ return hexToBytes(raw);
35
+ });
36
+ let size = 1;
37
+ while (size < pubkeys.length)
38
+ size <<= 1;
39
+ while (pubkeys.length < size)
40
+ pubkeys.push(new Uint8Array(32));
41
+ return pubkeys;
42
+ }
43
+ /** MMR root (hex, no `0x`) over the participants' public-key digests.
44
+ *
45
+ * Minima's `MMRTree` builds a perfect binary tree (power-of-two leaves), so the
46
+ * party set is padded to the next power of two with the **zero digest** sentinel
47
+ * (a fixed, publicly known padding leaf). The root is thus a Minima-native MMR
48
+ * root — the same structure the VTXO batch root commits and the on-chain `PROOF`
49
+ * verifies — over an arbitrary number of participants.
50
+ */
51
+ export function factoryPartySetRoot(participants) {
52
+ const root = MMRTree.fromPublicKeys(padPartySet(participants)).getRoot();
53
+ if (!root)
54
+ throw new Error('factoryPartySetRoot: failed to compute the party-set root');
55
+ return bytesToHex(root.data).toLowerCase();
56
+ }
57
+ /**
58
+ * Serialized MMR inclusion proof (hex, no `0x`) for participant `index` against
59
+ * the committed party-set root — the witness for a per-leaf
60
+ * `buildMmrMembershipCovenant` escape (RFC-048 §12.4).
61
+ */
62
+ export function factoryPartyProof(participants, index) {
63
+ if (!Number.isInteger(index) || index < 0 || index >= participants.length) {
64
+ throw new Error(`factoryPartyProof: index ${index} is out of range (0..${participants.length - 1})`);
65
+ }
66
+ const proof = MMRTree.fromPublicKeys(padPartySet(participants)).getProof(index);
67
+ return bytesToHex(serializeMMRProof(proof)).toLowerCase();
68
+ }
69
+ /**
70
+ * Balance set-root (RFC-048 §12.4) — the MMR root over `(pubkey, balance)` leaves,
71
+ * where each participant's `contributionAmount` is the **leaf sum**. This commits
72
+ * the exact distribution of the factory balance (`rootSum` = the total) and is the
73
+ * scalable, off-chain commitment that replaces per-party balance ports.
74
+ */
75
+ export function factoryBalanceSetRoot(participants) {
76
+ const tree = buildBalanceTree(participants);
77
+ const root = tree.getRoot();
78
+ if (!root)
79
+ throw new Error('factoryBalanceSetRoot: failed to compute the balance set-root');
80
+ return { root: bytesToHex(root.data).toLowerCase(), rootSum: root.value };
81
+ }
82
+ /**
83
+ * Serialized MMR inclusion proof (hex, no `0x`) for participant `index` against
84
+ * the committed balance set-root — the witness for a `buildMmrMembershipCovenant`
85
+ * balance escape (leaf sum = the participant's balance).
86
+ */
87
+ export function factoryBalanceProof(participants, index) {
88
+ if (!Number.isInteger(index) || index < 0 || index >= participants.length) {
89
+ throw new Error(`factoryBalanceProof: index ${index} is out of range (0..${participants.length - 1})`);
90
+ }
91
+ const proof = buildBalanceTree(participants).getProof(index);
92
+ return bytesToHex(serializeMMRProof(proof)).toLowerCase();
93
+ }
94
+ /** Padded (power-of-two) MMR tree over `(pubkey, balance)` leaves. */
95
+ function buildBalanceTree(participants) {
96
+ if (!Array.isArray(participants) || participants.length === 0) {
97
+ throw new Error('factory balance set requires at least one participant');
98
+ }
99
+ const tree = new MMRTree();
100
+ for (const [i, p] of participants.entries()) {
101
+ const raw = (p.publicKeyDigest ?? '').replace(/^0x/i, '');
102
+ if (raw.length === 0 || !/^[0-9a-fA-F]+$/.test(raw)) {
103
+ throw new Error(`participant[${i}].publicKeyDigest must be a hex string`);
104
+ }
105
+ if (p.contributionAmount < 0n) {
106
+ throw new Error(`participant[${i}].contributionAmount must be non-negative`);
107
+ }
108
+ tree.addLeaf(createMMRDataLeafNode(hexToBytes(raw), p.contributionAmount));
109
+ }
110
+ let size = 1;
111
+ while (size < participants.length)
112
+ size <<= 1;
113
+ for (let i = participants.length; i < size; i++) {
114
+ tree.addLeaf(createMMRDataLeafNode(new Uint8Array(32), 0n));
115
+ }
116
+ return tree;
117
+ }
118
+ /** Committed party-set root state variable (STATE(111)) for the factory funding output. */
119
+ export function factoryPartyRootStateVariable(root) {
120
+ const raw = root.replace(/^0x/i, '');
121
+ if (!/^[0-9a-fA-F]{64}$/.test(raw)) {
122
+ throw new Error(`factoryPartyRootStateVariable: root must be 32 bytes of hex; got ${JSON.stringify(root)}`);
123
+ }
124
+ return { port: FACTORY_PARTY_ROOT_PORT, value: '0x' + raw, type: 'hex' };
125
+ }
126
+ /** State port base for per-party allocations: party k ⇒ `base + k` (below the MSHS floor). */
127
+ export const FACTORY_BALANCE_PORT_BASE = 128;
128
+ /** Lowest port reserved for MSHS-2 (MASH); Omnia ports MUST stay below this.
129
+ * The node installs the MASH authorization bundle at ports **230–255**
130
+ * (`bundleBase = 256 − (4 + k) = 230`): the header R / ROOTS_BLOB /
131
+ * SECRETS_BLOB / TREE_SIG at 230–233 and PROOF at 234–255. A composite
132
+ * MASH + Omnia coin must therefore keep Omnia ports ≤ 229. */
133
+ export const MSHS_PORT_FLOOR = 230;
134
+ /** Maximum participants whose balance ports remain below the MSHS floor. */
135
+ export const FACTORY_MAX_PARTICIPANTS = MSHS_PORT_FLOOR - FACTORY_BALANCE_PORT_BASE;
136
+ /**
137
+ * Per-party allocation state variables (port base+k = party k's balance), seeded
18
138
  * into the factory funding output so the timeout escape can pay each party its
19
139
  * committed share.
20
140
  */
@@ -40,9 +160,9 @@ export function factoryEscapeStateVariable(escapeBlock) {
40
160
  * - **Cooperative / update** (any time): `MULTISIG(N …)` — all parties sign.
41
161
  * `SETTLEMENT=true` closes after minimal coinage (1 block).
42
162
  * - **Unilateral escape** (after the absolute block in `STATE(103)`): any
43
- * single party k may withdraw its allocation `SHARE_k = PREVSTATE(200+k)`
163
+ * single party k may withdraw its allocation `SHARE_k = PREVSTATE(base+k)`
44
164
  * (the spent coin's share) to its own settlement address, zero its state in
45
- * the successor (`STATE(200+k) EQ 0`), keep the other parties' balances
165
+ * the successor (`STATE(base+k) EQ 0`), keep the other parties' balances
46
166
  * unchanged, and return the remainder to the factory script. Repeated
47
167
  * withdrawals cannot double-claim because each zeroes its own port. `STATE(103)` is immutable on every spend (`SAMESTATE(103 103)`),
48
168
  * so the escape clock cannot be pushed by any counterparty.
@@ -53,12 +173,17 @@ export function buildFactoryScript(participants, _options = {}) {
53
173
  if (participants.length < 2) {
54
174
  throw new Error(`Factory requires at least 2 participants, got ${participants.length}`);
55
175
  }
176
+ if (participants.length > FACTORY_MAX_PARTICIPANTS) {
177
+ throw new Error(`Factory supports at most ${FACTORY_MAX_PARTICIPANTS} participants (got ${participants.length}) — balance ports would reach the MSHS-2 band (≥${MSHS_PORT_FLOOR})`);
178
+ }
56
179
  const count = participants.length;
57
180
  const pks = participants.map((p) => kissHex(p.publicKeyDigest));
58
181
  const payouts = participants.map((p) => kissHex(p.settlementAddress ?? p.publicKeyDigest));
59
182
  const lines = [
60
183
  'LET SETTLEMENT=STATE(100)',
61
184
  `ASSERT SAMESTATE(${FACTORY_ESCAPE_PORT} ${FACTORY_ESCAPE_PORT})`,
185
+ // RFC-048 §12.4/§12.5: the committed party-set root is immutable on every spend.
186
+ `ASSERT SAMESTATE(${FACTORY_PARTY_ROOT_PORT} ${FACTORY_PARTY_ROOT_PORT})`,
62
187
  `IF @BLOCK GT PREVSTATE(${FACTORY_ESCAPE_PORT}) THEN`,
63
188
  ];
64
189
  participants.forEach((_, k) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@totemsdk/omnia-factory",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "Channel factory layer for Omnia — N-of-N MULTISIG funding, virtual channel management, and factory settlement",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/cjs/index.d.ts",
@@ -25,12 +25,12 @@
25
25
  ],
26
26
  "dependencies": {
27
27
  "@totemsdk/omnia": "^2.0.1",
28
- "@totemsdk/wots-lease": "^1.0.1",
29
- "@totemsdk/tx-builder": "^0.2.1",
30
28
  "@totemsdk/core": "^1.3.0",
29
+ "@totemsdk/tx-builder": "^0.2.1",
30
+ "@totemsdk/wots-lease": "^1.0.1",
31
31
  "@totemsdk/txpow": "^1.0.2",
32
- "@totemsdk/storage": "^0.1.0",
33
- "@totemsdk/chain-provider": "^0.2.1"
32
+ "@totemsdk/chain-provider": "^0.2.1",
33
+ "@totemsdk/storage": "^0.1.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/jest": "^30.0.0",