@totemsdk/omnia-factory 0.7.2 → 0.9.0
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/dist/checkpoint.d.ts +75 -0
- package/dist/checkpoint.js +180 -0
- package/dist/cjs/checkpoint.d.ts +75 -0
- package/dist/cjs/checkpoint.js +186 -0
- package/dist/cjs/distribution.d.ts +47 -0
- package/dist/cjs/distribution.js +111 -0
- package/dist/cjs/factory.d.ts +20 -2
- package/dist/cjs/factory.js +126 -14
- package/dist/cjs/index.d.ts +5 -1
- package/dist/cjs/index.js +13 -1
- package/dist/cjs/script.d.ts +85 -17
- package/dist/cjs/script.js +170 -43
- package/dist/cjs/types.d.ts +26 -0
- package/dist/cjs/virtual.js +12 -0
- package/dist/distribution.d.ts +47 -0
- package/dist/distribution.js +106 -0
- package/dist/factory.d.ts +20 -2
- package/dist/factory.js +127 -15
- package/dist/index.d.ts +5 -1
- package/dist/index.js +3 -1
- package/dist/script.d.ts +85 -17
- package/dist/script.js +153 -30
- package/dist/types.d.ts +26 -0
- package/dist/virtual.js +12 -0
- package/package.json +6 -6
package/dist/cjs/factory.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ChainStateProvider } from '@totemsdk/chain-provider';
|
|
2
2
|
import type { ChannelFactory, FactoryParticipant, FactoryFundingMaterial, WotsLeaseBundle } from './types.js';
|
|
3
|
+
import { buildAndHashFactoryScript } from './script.js';
|
|
3
4
|
/**
|
|
4
5
|
* Enforce the factory's balance conservation invariant:
|
|
5
6
|
* sum(allocations) + sum(virtualChannel.totalValue) === totalValue
|
|
@@ -38,7 +39,7 @@ export declare function enforceConservation(factory: ChannelFactory): void;
|
|
|
38
39
|
* is 0; coloured coins use `tokenAmount = minimaRawAmount × 10^scale`.
|
|
39
40
|
* Defaults to 0.
|
|
40
41
|
*/
|
|
41
|
-
export declare function createFactory(participants: FactoryParticipant[], tokenId: string, bundle: WotsLeaseBundle, chainProvider?: ChainStateProvider, tokenScale?: number, funding?: FactoryFundingMaterial[]): Promise<ChannelFactory>;
|
|
42
|
+
export declare function createFactory(participants: FactoryParticipant[], tokenId: string, bundle: WotsLeaseBundle, chainProvider?: ChainStateProvider, tokenScale?: number, funding?: FactoryFundingMaterial[], scriptOptions?: Parameters<typeof buildAndHashFactoryScript>[1]): Promise<ChannelFactory>;
|
|
42
43
|
/**
|
|
43
44
|
* Sign the opening commitment for a plan and record the proposer's signature.
|
|
44
45
|
* Nothing is broadcast and no funding signature is requested, so callers can
|
|
@@ -59,6 +60,12 @@ export interface FactoryOpeningPlan {
|
|
|
59
60
|
fundingDigest?: Uint8Array;
|
|
60
61
|
fundingCoinId?: string;
|
|
61
62
|
fundingMaterials?: FactoryFundingMaterial[];
|
|
63
|
+
/** State variables seeded into the funding output (OMN-060 roots included). */
|
|
64
|
+
fundingStateVariables?: Array<{
|
|
65
|
+
port: number;
|
|
66
|
+
value: string | bigint;
|
|
67
|
+
type: string;
|
|
68
|
+
}>;
|
|
62
69
|
onChain: boolean;
|
|
63
70
|
}
|
|
64
71
|
export interface FinalizeFactoryOpeningOptions {
|
|
@@ -66,12 +73,23 @@ export interface FinalizeFactoryOpeningOptions {
|
|
|
66
73
|
fundingSignatures?: Record<string, Uint8Array>;
|
|
67
74
|
/** Factory opening signatures to record on the proposal. */
|
|
68
75
|
openingSignatures: Record<string, Uint8Array>;
|
|
76
|
+
/**
|
|
77
|
+
* OMN-063: confirmations required before an on-chain factory becomes active.
|
|
78
|
+
* Default 1. A factory whose funding cannot be observed at depth stays
|
|
79
|
+
* `opening` instead of being advertised as operational.
|
|
80
|
+
*/
|
|
81
|
+
minConfirmations?: number;
|
|
69
82
|
}
|
|
70
83
|
/**
|
|
71
84
|
* Build a factory and (when every participant supplies a funding coin) its
|
|
72
85
|
* deterministic funding draft, without signing or broadcasting.
|
|
73
86
|
*/
|
|
74
|
-
export declare function buildFactoryOpening(participants: FactoryParticipant[], tokenId: string, tokenScale: number, chainProvider?: ChainStateProvider, funding?: FactoryFundingMaterial[]
|
|
87
|
+
export declare function buildFactoryOpening(participants: FactoryParticipant[], tokenId: string, tokenScale: number, chainProvider?: ChainStateProvider, funding?: FactoryFundingMaterial[],
|
|
88
|
+
/**
|
|
89
|
+
* OMN-058: `enforceCheckpoints` uses the checkpoint-mode covenant, so every
|
|
90
|
+
* allocation change must consume and recreate the factory coin (RFC-079 §4).
|
|
91
|
+
*/
|
|
92
|
+
scriptOptions?: Parameters<typeof buildAndHashFactoryScript>[1]): Promise<FactoryOpeningPlan>;
|
|
75
93
|
/**
|
|
76
94
|
* Broadcast the prepared factory funding (when on-chain) and record opening
|
|
77
95
|
* signatures. When every participant signature is supplied the factory commits.
|
package/dist/cjs/factory.js
CHANGED
|
@@ -124,12 +124,12 @@ function generateFactoryId(participants, tokenId, tokenScale) {
|
|
|
124
124
|
* is 0; coloured coins use `tokenAmount = minimaRawAmount × 10^scale`.
|
|
125
125
|
* Defaults to 0.
|
|
126
126
|
*/
|
|
127
|
-
async function createFactory(participants, tokenId, bundle, chainProvider, tokenScale = 0, funding) {
|
|
127
|
+
async function createFactory(participants, tokenId, bundle, chainProvider, tokenScale = 0, funding, scriptOptions = { settlement: 'full-distribution', enforceCheckpoints: true }) {
|
|
128
128
|
// One plan, one factory ID: build the opening once and reuse it for the
|
|
129
129
|
// proposal, the local record and the funding transaction. Rebuilding here
|
|
130
130
|
// would generate a different ID (the ID includes a random nonce) and the
|
|
131
131
|
// peer would accept a factory this host never stored.
|
|
132
|
-
const plan = await buildFactoryOpening(participants, tokenId, tokenScale, chainProvider, funding);
|
|
132
|
+
const plan = await buildFactoryOpening(participants, tokenId, tokenScale, chainProvider, funding, scriptOptions);
|
|
133
133
|
return createFactoryFromPlan(plan, bundle, chainProvider);
|
|
134
134
|
}
|
|
135
135
|
function findProposer(factory, bundle) {
|
|
@@ -196,20 +196,34 @@ async function createFactoryFromPlan(plan, bundle, chainProvider) {
|
|
|
196
196
|
* Build a factory and (when every participant supplies a funding coin) its
|
|
197
197
|
* deterministic funding draft, without signing or broadcasting.
|
|
198
198
|
*/
|
|
199
|
-
async function buildFactoryOpening(participants, tokenId, tokenScale, chainProvider, funding
|
|
199
|
+
async function buildFactoryOpening(participants, tokenId, tokenScale, chainProvider, funding,
|
|
200
|
+
/**
|
|
201
|
+
* OMN-058: `enforceCheckpoints` uses the checkpoint-mode covenant, so every
|
|
202
|
+
* allocation change must consume and recreate the factory coin (RFC-079 §4).
|
|
203
|
+
*/
|
|
204
|
+
scriptOptions = { settlement: 'full-distribution', enforceCheckpoints: true }) {
|
|
200
205
|
if (participants.length < 2) {
|
|
201
206
|
throw new Error(`Factory requires at least 2 participants, got ${participants.length}`);
|
|
202
207
|
}
|
|
208
|
+
// OMN-095: without checkpoint enforcement a unilateral exit pays the funding
|
|
209
|
+
// split, so the latest off-chain allocation is unenforceable. Refuse to build
|
|
210
|
+
// that silently; it is allowed only as an explicit, acknowledged mode.
|
|
211
|
+
if (scriptOptions.enforceCheckpoints !== true && scriptOptions.allowUnenforceableAllocations !== true) {
|
|
212
|
+
throw new Error('refusing to build a factory whose allocations are not checkpoint-enforced: '
|
|
213
|
+
+ 'a unilateral exit would pay the funding split, not the latest allocation. '
|
|
214
|
+
+ 'Pass { allowUnenforceableAllocations: true } only for a factory that will never rely on unilateral exits.');
|
|
215
|
+
}
|
|
203
216
|
const totalValue = participants.reduce((sum, p) => sum + p.contributionAmount, 0n);
|
|
204
217
|
const allocations = {};
|
|
205
218
|
for (const p of participants)
|
|
206
219
|
allocations[p.partyId] = p.contributionAmount;
|
|
207
|
-
const { script, address } = (0, script_js_1.buildAndHashFactoryScript)(participants);
|
|
220
|
+
const { script, address } = (0, script_js_1.buildAndHashFactoryScript)(participants, scriptOptions);
|
|
208
221
|
const factoryId = generateFactoryId(participants, tokenId, tokenScale);
|
|
209
222
|
let fundingTxBytes;
|
|
210
223
|
let fundingDigest;
|
|
211
224
|
let fundingCoinId;
|
|
212
225
|
let pendingCommitment;
|
|
226
|
+
let seededStateVariables;
|
|
213
227
|
const allHaveCoinId = participants.every(p => p.fundingCoinId);
|
|
214
228
|
if (allHaveCoinId && chainProvider) {
|
|
215
229
|
const materialByParty = new Map((funding ?? []).map(m => [m.partyId, m]));
|
|
@@ -222,7 +236,18 @@ async function buildFactoryOpening(participants, tokenId, tokenScale, chainProvi
|
|
|
222
236
|
}
|
|
223
237
|
const tip = await chainProvider.getTip();
|
|
224
238
|
const escapeBlock = tip.block + script_js_1.FACTORY_ESCAPE_DELAY_BLOCKS;
|
|
225
|
-
const
|
|
239
|
+
const fundingStateVariables = [
|
|
240
|
+
...(0, script_js_1.factoryBalanceStateVariables)(participants),
|
|
241
|
+
(0, script_js_1.factoryEscapeStateVariable)(escapeBlock),
|
|
242
|
+
// OMN-058: the checkpoint sequence starts at zero on the funding coin.
|
|
243
|
+
{ port: 104, value: 0n, type: 'number' },
|
|
244
|
+
// OMN-060: the covenant asserts SAMESTATE over these roots, so they must
|
|
245
|
+
// exist in the funding output or the assertion is vacuous.
|
|
246
|
+
(0, script_js_1.factoryPartyRootStateVariable)((0, script_js_1.computeFactoryPartyRoot)(participants)),
|
|
247
|
+
(0, script_js_1.factoryBalanceRootStateVariable)((0, script_js_1.computeFactoryBalanceRoot)(allocations)),
|
|
248
|
+
];
|
|
249
|
+
seededStateVariables = fundingStateVariables;
|
|
250
|
+
const fundingDraft = (0, omnia_1.buildFundingTx)(script, address, totalValue, tokenId, tokenScale, participants.map(p => p.fundingCoinId), participants.map(p => p.contributionAmount), inputAddresses, fundingStateVariables);
|
|
226
251
|
fundingDigest = (0, omnia_1.computeOmniaTxDigest)(fundingDraft);
|
|
227
252
|
fundingTxBytes = (0, omnia_1.omniaDraftToMinimaBytes)(fundingDraft);
|
|
228
253
|
fundingCoinId = (0, omnia_1.minimaOutputCoinIdsForDraft)(fundingDraft)[0];
|
|
@@ -255,8 +280,18 @@ async function buildFactoryOpening(participants, tokenId, tokenScale, chainProvi
|
|
|
255
280
|
fundingCoinId: undefined,
|
|
256
281
|
pendingCommitment: Buffer.from(pendingCommitment).toString('hex'),
|
|
257
282
|
pendingSignatures: {},
|
|
283
|
+
enforceCheckpoints: scriptOptions.enforceCheckpoints === true,
|
|
284
|
+
allocationEnforcement: scriptOptions.enforceCheckpoints === true ? 'checkpoint' : 'none',
|
|
285
|
+
};
|
|
286
|
+
return {
|
|
287
|
+
factory,
|
|
288
|
+
fundingTxBytes,
|
|
289
|
+
fundingDigest,
|
|
290
|
+
fundingCoinId,
|
|
291
|
+
fundingMaterials: funding,
|
|
292
|
+
...(seededStateVariables ? { fundingStateVariables: seededStateVariables } : {}),
|
|
293
|
+
onChain: fundingTxBytes !== undefined,
|
|
258
294
|
};
|
|
259
|
-
return { factory, fundingTxBytes, fundingDigest, fundingCoinId, fundingMaterials: funding, onChain: fundingTxBytes !== undefined };
|
|
260
295
|
}
|
|
261
296
|
/**
|
|
262
297
|
* Broadcast the prepared factory funding (when on-chain) and record opening
|
|
@@ -310,6 +345,21 @@ async function finalizeFactoryOpening(plan, opts, chainProvider) {
|
|
|
310
345
|
fundingTxId,
|
|
311
346
|
fundingCoinId: plan.fundingCoinId,
|
|
312
347
|
};
|
|
348
|
+
// OMN-063: submission is not finality. Only a coin observed at the required
|
|
349
|
+
// depth may become an active factory.
|
|
350
|
+
const required = opts.minConfirmations ?? 1;
|
|
351
|
+
if (required > 0) {
|
|
352
|
+
let confirmed = false;
|
|
353
|
+
if (plan.fundingCoinId && typeof chainProvider.getTip === 'function') {
|
|
354
|
+
const coin = await chainProvider.getCoin(plan.fundingCoinId);
|
|
355
|
+
const tip = await chainProvider.getTip();
|
|
356
|
+
const created = Number(coin?.created ?? 0);
|
|
357
|
+
confirmed = Boolean(coin && !coin.spent && created > 0 && tip.block - created + 1 >= required);
|
|
358
|
+
}
|
|
359
|
+
if (!confirmed) {
|
|
360
|
+
return { ...factory, pendingSignatures: { ...opts.openingSignatures } };
|
|
361
|
+
}
|
|
362
|
+
}
|
|
313
363
|
}
|
|
314
364
|
factory = { ...factory, pendingSignatures: { ...opts.openingSignatures } };
|
|
315
365
|
const allSigned = factory.participants.every(p => factory.pendingSignatures[p.partyId] !== undefined);
|
|
@@ -471,6 +521,10 @@ async function reallocate(factory, fromPartyId, toPartyId, amount, leaseProvider
|
|
|
471
521
|
if (factory.status !== 'active') {
|
|
472
522
|
throw new Error(`reallocate requires factory status 'active', got '${factory.status}'`);
|
|
473
523
|
}
|
|
524
|
+
if (factory.enforceCheckpoints) {
|
|
525
|
+
throw new Error(`factory ${factory.factoryId} uses checkpoint mode: reallocating changes the committed allocation `
|
|
526
|
+
+ 'and must be committed with a factory checkpoint transaction first.');
|
|
527
|
+
}
|
|
474
528
|
if (amount <= 0n) {
|
|
475
529
|
throw new Error(`Reallocation amount must be positive, got ${amount}`);
|
|
476
530
|
}
|
|
@@ -608,23 +662,81 @@ function assertFactoryFinalized(known, incoming, senderPublicKeyDigest) {
|
|
|
608
662
|
if (JSON.stringify(jsonSafeForCompare(last.allocations)) !== JSON.stringify(jsonSafeForCompare(known.allocations))) {
|
|
609
663
|
throw new Error('FACTORY_FINALIZED changes allocations in the accept entry');
|
|
610
664
|
}
|
|
611
|
-
|
|
665
|
+
// Rebuild from local state: the peer may only supply the funding references.
|
|
666
|
+
return {
|
|
667
|
+
...known,
|
|
668
|
+
status: 'active',
|
|
669
|
+
currentSequence: 1,
|
|
670
|
+
stateLog: [...(known.stateLog ?? []), last],
|
|
671
|
+
pendingCommitment: undefined,
|
|
672
|
+
pendingSignatures: {},
|
|
673
|
+
fundingTxId: incoming.fundingTxId ?? known.fundingTxId,
|
|
674
|
+
fundingCoinId: incoming.fundingCoinId ?? known.fundingCoinId,
|
|
675
|
+
};
|
|
612
676
|
}
|
|
613
677
|
if (known.status === 'active' && incoming.status === 'active') {
|
|
614
|
-
// Funding references may be filled in after activation
|
|
615
|
-
|
|
678
|
+
// Funding references may be filled in after activation. Nothing else may
|
|
679
|
+
// change: a peer cannot rewrite sequence, log, allocations or the VC set.
|
|
680
|
+
if (incoming.currentSequence !== known.currentSequence) {
|
|
681
|
+
throw new Error('FACTORY_FINALIZED changes the active sequence');
|
|
682
|
+
}
|
|
683
|
+
if (JSON.stringify(jsonSafeForCompare(incoming.stateLog ?? [])) !== JSON.stringify(jsonSafeForCompare(known.stateLog ?? []))) {
|
|
684
|
+
throw new Error('FACTORY_FINALIZED changes the active state log');
|
|
685
|
+
}
|
|
686
|
+
if (JSON.stringify(jsonSafeForCompare(incoming.allocations)) !== JSON.stringify(jsonSafeForCompare(known.allocations))) {
|
|
687
|
+
throw new Error('FACTORY_FINALIZED changes the active allocations');
|
|
688
|
+
}
|
|
689
|
+
if (JSON.stringify(jsonSafeForCompare(incoming.virtualChannels ?? [])) !== JSON.stringify(jsonSafeForCompare(known.virtualChannels ?? []))) {
|
|
690
|
+
throw new Error('FACTORY_FINALIZED changes the active virtual-channel set');
|
|
691
|
+
}
|
|
692
|
+
if (incoming.pendingCommitment || (incoming.pendingSignatures && Object.keys(incoming.pendingSignatures).length > 0)) {
|
|
693
|
+
throw new Error('FACTORY_FINALIZED carries pending opening state on an active factory');
|
|
694
|
+
}
|
|
695
|
+
return {
|
|
696
|
+
...known,
|
|
697
|
+
fundingTxId: known.fundingTxId ?? incoming.fundingTxId,
|
|
698
|
+
fundingCoinId: known.fundingCoinId ?? incoming.fundingCoinId,
|
|
699
|
+
};
|
|
616
700
|
}
|
|
617
701
|
if (known.status === 'opening' && incoming.status === 'opening') {
|
|
618
|
-
// Funding references
|
|
702
|
+
// Funding references and additional *valid* opening signatures only.
|
|
703
|
+
if (incoming.currentSequence !== known.currentSequence) {
|
|
704
|
+
throw new Error('FACTORY_FINALIZED changes the opening sequence');
|
|
705
|
+
}
|
|
706
|
+
if (JSON.stringify(jsonSafeForCompare(incoming.stateLog ?? [])) !== JSON.stringify(jsonSafeForCompare(known.stateLog ?? []))) {
|
|
707
|
+
throw new Error('FACTORY_FINALIZED changes the opening state log');
|
|
708
|
+
}
|
|
709
|
+
if (!known.pendingCommitment) {
|
|
710
|
+
throw new Error('FACTORY_FINALIZED for a proposal without a pending commitment');
|
|
711
|
+
}
|
|
712
|
+
const commitment = Buffer.from(known.pendingCommitment.replace(/^0x/i, ''), 'hex');
|
|
619
713
|
const knownSigs = known.pendingSignatures ?? {};
|
|
620
714
|
const incomingSigs = incoming.pendingSignatures ?? {};
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
715
|
+
const byParty = new Map(known.participants.map((participant) => [participant.partyId, participant]));
|
|
716
|
+
for (const [partyId, sig] of Object.entries(incomingSigs)) {
|
|
717
|
+
const participant = byParty.get(partyId);
|
|
718
|
+
if (!participant) {
|
|
719
|
+
throw new Error(`FACTORY_FINALIZED carries a signature from unknown party '${partyId}'`);
|
|
720
|
+
}
|
|
721
|
+
const previous = knownSigs[partyId];
|
|
722
|
+
if (previous && Buffer.from(previous).toString('hex') !== Buffer.from(sig).toString('hex')) {
|
|
624
723
|
throw new Error(`FACTORY_FINALIZED removes or alters the signature for '${partyId}'`);
|
|
625
724
|
}
|
|
725
|
+
if (!(0, omnia_1.verifyChannelSignature)(sig, commitment, participant.publicKeyDigest)) {
|
|
726
|
+
throw new Error(`FACTORY_FINALIZED carries an invalid signature for '${partyId}'`);
|
|
727
|
+
}
|
|
626
728
|
}
|
|
627
|
-
|
|
729
|
+
for (const partyId of Object.keys(knownSigs)) {
|
|
730
|
+
if (!incomingSigs[partyId]) {
|
|
731
|
+
throw new Error(`FACTORY_FINALIZED removes or alters the signature for '${partyId}'`);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
return {
|
|
735
|
+
...known,
|
|
736
|
+
fundingTxId: known.fundingTxId ?? incoming.fundingTxId,
|
|
737
|
+
fundingCoinId: known.fundingCoinId ?? incoming.fundingCoinId,
|
|
738
|
+
pendingSignatures: { ...incomingSigs },
|
|
739
|
+
};
|
|
628
740
|
}
|
|
629
741
|
throw new Error('FACTORY_FINALIZED has an unsupported status transition');
|
|
630
742
|
}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export type { FactoryOpeningPlan, FinalizeFactoryOpeningOptions } from './factory.js';
|
|
2
2
|
export type { FactoryStatus, FactoryParticipant, FactoryFundingMaterial, FactorySignature, FactoryLogEntry, ChannelFactory, FactorySettlementPayload, FactoryDisputePayload, FactoryLeaseOps, WotsLeaseBundle, OmniaChannel, } from './types.js';
|
|
3
|
-
export { buildFactoryScript, buildAndHashFactoryScript, factoryBalanceStateVariables, factoryEscapeStateVariable, factoryPartySetRoot, factoryPartyRootStateVariable, factoryPartyProof, factoryBalanceSetRoot, factoryBalanceProof, factoryDistributionOutputs, buildFactoryDistributionScript, FACTORY_BALANCE_PORT_BASE, FACTORY_ESCAPE_PORT, FACTORY_ESCAPE_DELAY_BLOCKS, FACTORY_MAX_PARTICIPANTS, FACTORY_PARTY_ROOT_PORT, FACTORY_BALANCE_ROOT_PORT, MSHS_PORT_FLOOR, scriptAddress, normalizeScript, } from './script.js';
|
|
3
|
+
export { buildFactoryScript, buildAndHashFactoryScript, factoryBalanceStateVariables, factoryEscapeStateVariable, factoryPartySetRoot, factoryPartyRootStateVariable, factoryPartyProof, factoryBalanceSetRoot, factoryBalanceProof, factoryDistributionOutputs, buildFactoryDistributionScript, buildFactoryPresignedDistributionScript, FACTORY_BALANCE_PORT_BASE, FACTORY_ESCAPE_PORT, FACTORY_CHECKPOINT_SEQUENCE_PORT, FACTORY_ESCAPE_DELAY_BLOCKS, FACTORY_MAX_PARTICIPANTS, FACTORY_PARTY_ROOT_PORT, FACTORY_BALANCE_ROOT_PORT, MSHS_PORT_FLOOR, scriptAddress, normalizeScript, } from './script.js';
|
|
4
4
|
export { computeFactoryStateCommitment } from './commitment.js';
|
|
5
|
+
export { assertFactoryDistributable, buildFactoryDistribution, factoryDistributionPayouts, } from './distribution.js';
|
|
6
|
+
export type { FactoryDistribution, FactoryDistributionEntry, } from './distribution.js';
|
|
7
|
+
export { buildFactoryCheckpoint, factoryCheckpointStateVariables, assertFactoryCheckpointTransition, factoryCheckpointSnapshot, } from './checkpoint.js';
|
|
8
|
+
export type { FactoryCheckpoint, FactoryCheckpointParams, FactoryCheckpointSnapshot, } from './checkpoint.js';
|
|
5
9
|
export type { FactoryDistributionOutput, FactoryDistributionScriptConfig } from './script.js';
|
|
6
10
|
export { createFactory, createFactoryFromPlan, proposeFactoryOpening, buildFactoryOpening, finalizeFactoryOpening, acceptFactory, reallocate, enforceConservation, signFactoryOpening, verifyFactorySignature, applyFactorySignature, assertFactoryFinalized, } from './factory.js';
|
|
7
11
|
export type { FactoryOpeningSignature } from './factory.js';
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.assembleVirtualChannelClose = exports.signVirtualChannelClose = exports.buildVirtualChannelClose = exports.signVirtualChannelOpen = exports.buildVirtualChannelOpen = exports.closeVirtualChannel = exports.openVirtualChannel = exports.createFactoryCoordination = exports.assertFactoryFinalized = exports.applyFactorySignature = exports.verifyFactorySignature = exports.signFactoryOpening = exports.enforceConservation = exports.reallocate = exports.acceptFactory = exports.finalizeFactoryOpening = exports.buildFactoryOpening = exports.proposeFactoryOpening = exports.createFactoryFromPlan = exports.createFactory = exports.factoryCheckpointSnapshot = exports.assertFactoryCheckpointTransition = exports.factoryCheckpointStateVariables = exports.buildFactoryCheckpoint = exports.factoryDistributionPayouts = exports.buildFactoryDistribution = exports.assertFactoryDistributable = exports.computeFactoryStateCommitment = exports.normalizeScript = exports.scriptAddress = exports.MSHS_PORT_FLOOR = exports.FACTORY_BALANCE_ROOT_PORT = exports.FACTORY_PARTY_ROOT_PORT = exports.FACTORY_MAX_PARTICIPANTS = exports.FACTORY_ESCAPE_DELAY_BLOCKS = exports.FACTORY_CHECKPOINT_SEQUENCE_PORT = exports.FACTORY_ESCAPE_PORT = exports.FACTORY_BALANCE_PORT_BASE = exports.buildFactoryPresignedDistributionScript = exports.buildFactoryDistributionScript = exports.factoryDistributionOutputs = exports.factoryBalanceProof = exports.factoryBalanceSetRoot = exports.factoryPartyProof = exports.factoryPartyRootStateVariable = exports.factoryPartySetRoot = exports.factoryEscapeStateVariable = exports.factoryBalanceStateVariables = exports.buildAndHashFactoryScript = exports.buildFactoryScript = void 0;
|
|
4
|
+
exports.createDurableFactoryStore = exports.assembleFactorySettlement = exports.verifyFactorySettlementSignature = exports.signFactorySettlement = exports.buildFactorySettlement = exports.buildDisputePayload = exports.closeFactory = exports.assembleVirtualChannelOpen = exports.verifyVirtualChannelOpenSignature = void 0;
|
|
4
5
|
var script_js_1 = require("./script.js");
|
|
5
6
|
Object.defineProperty(exports, "buildFactoryScript", { enumerable: true, get: function () { return script_js_1.buildFactoryScript; } });
|
|
6
7
|
Object.defineProperty(exports, "buildAndHashFactoryScript", { enumerable: true, get: function () { return script_js_1.buildAndHashFactoryScript; } });
|
|
@@ -13,8 +14,10 @@ Object.defineProperty(exports, "factoryBalanceSetRoot", { enumerable: true, get:
|
|
|
13
14
|
Object.defineProperty(exports, "factoryBalanceProof", { enumerable: true, get: function () { return script_js_1.factoryBalanceProof; } });
|
|
14
15
|
Object.defineProperty(exports, "factoryDistributionOutputs", { enumerable: true, get: function () { return script_js_1.factoryDistributionOutputs; } });
|
|
15
16
|
Object.defineProperty(exports, "buildFactoryDistributionScript", { enumerable: true, get: function () { return script_js_1.buildFactoryDistributionScript; } });
|
|
17
|
+
Object.defineProperty(exports, "buildFactoryPresignedDistributionScript", { enumerable: true, get: function () { return script_js_1.buildFactoryPresignedDistributionScript; } });
|
|
16
18
|
Object.defineProperty(exports, "FACTORY_BALANCE_PORT_BASE", { enumerable: true, get: function () { return script_js_1.FACTORY_BALANCE_PORT_BASE; } });
|
|
17
19
|
Object.defineProperty(exports, "FACTORY_ESCAPE_PORT", { enumerable: true, get: function () { return script_js_1.FACTORY_ESCAPE_PORT; } });
|
|
20
|
+
Object.defineProperty(exports, "FACTORY_CHECKPOINT_SEQUENCE_PORT", { enumerable: true, get: function () { return script_js_1.FACTORY_CHECKPOINT_SEQUENCE_PORT; } });
|
|
18
21
|
Object.defineProperty(exports, "FACTORY_ESCAPE_DELAY_BLOCKS", { enumerable: true, get: function () { return script_js_1.FACTORY_ESCAPE_DELAY_BLOCKS; } });
|
|
19
22
|
Object.defineProperty(exports, "FACTORY_MAX_PARTICIPANTS", { enumerable: true, get: function () { return script_js_1.FACTORY_MAX_PARTICIPANTS; } });
|
|
20
23
|
Object.defineProperty(exports, "FACTORY_PARTY_ROOT_PORT", { enumerable: true, get: function () { return script_js_1.FACTORY_PARTY_ROOT_PORT; } });
|
|
@@ -24,6 +27,15 @@ Object.defineProperty(exports, "scriptAddress", { enumerable: true, get: functio
|
|
|
24
27
|
Object.defineProperty(exports, "normalizeScript", { enumerable: true, get: function () { return script_js_1.normalizeScript; } });
|
|
25
28
|
var commitment_js_1 = require("./commitment.js");
|
|
26
29
|
Object.defineProperty(exports, "computeFactoryStateCommitment", { enumerable: true, get: function () { return commitment_js_1.computeFactoryStateCommitment; } });
|
|
30
|
+
var distribution_js_1 = require("./distribution.js");
|
|
31
|
+
Object.defineProperty(exports, "assertFactoryDistributable", { enumerable: true, get: function () { return distribution_js_1.assertFactoryDistributable; } });
|
|
32
|
+
Object.defineProperty(exports, "buildFactoryDistribution", { enumerable: true, get: function () { return distribution_js_1.buildFactoryDistribution; } });
|
|
33
|
+
Object.defineProperty(exports, "factoryDistributionPayouts", { enumerable: true, get: function () { return distribution_js_1.factoryDistributionPayouts; } });
|
|
34
|
+
var checkpoint_js_1 = require("./checkpoint.js");
|
|
35
|
+
Object.defineProperty(exports, "buildFactoryCheckpoint", { enumerable: true, get: function () { return checkpoint_js_1.buildFactoryCheckpoint; } });
|
|
36
|
+
Object.defineProperty(exports, "factoryCheckpointStateVariables", { enumerable: true, get: function () { return checkpoint_js_1.factoryCheckpointStateVariables; } });
|
|
37
|
+
Object.defineProperty(exports, "assertFactoryCheckpointTransition", { enumerable: true, get: function () { return checkpoint_js_1.assertFactoryCheckpointTransition; } });
|
|
38
|
+
Object.defineProperty(exports, "factoryCheckpointSnapshot", { enumerable: true, get: function () { return checkpoint_js_1.factoryCheckpointSnapshot; } });
|
|
27
39
|
var factory_js_1 = require("./factory.js");
|
|
28
40
|
Object.defineProperty(exports, "createFactory", { enumerable: true, get: function () { return factory_js_1.createFactory; } });
|
|
29
41
|
Object.defineProperty(exports, "createFactoryFromPlan", { enumerable: true, get: function () { return factory_js_1.createFactoryFromPlan; } });
|
package/dist/cjs/script.d.ts
CHANGED
|
@@ -3,6 +3,12 @@ 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
|
+
/**
|
|
7
|
+
* OMN-058/RFC-079 §4: checkpoint sequence (STATE(104)). Seeded at funding and
|
|
8
|
+
* advanced by exactly one on every checkpoint spend, so a checkpoint can never
|
|
9
|
+
* be replayed against a newer state.
|
|
10
|
+
*/
|
|
11
|
+
export declare const FACTORY_CHECKPOINT_SEQUENCE_PORT = 104;
|
|
6
12
|
/**
|
|
7
13
|
* State port carrying the committed party-set root (STATE(111)) — the MMR root
|
|
8
14
|
* of the participant public-key digests (RFC-048 §12.4/§12.5). It is pinned on
|
|
@@ -71,13 +77,41 @@ export interface FactoryDistributionScriptConfig {
|
|
|
71
77
|
balanceRootPort?: number;
|
|
72
78
|
}
|
|
73
79
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
80
|
+
* Pre-signed all-party distribution (RFC-048 §12.4) — kept as a **separate**
|
|
81
|
+
* mechanism for a dedicated distribution coin.
|
|
82
|
+
*
|
|
83
|
+
* This script authorizes the spend with `MULTISIG` after the escape height. It
|
|
84
|
+
* does **not** verify the payout set; allocation correctness comes from the
|
|
85
|
+
* pre-signed transaction that the parties exchange. Do not describe or use it as
|
|
86
|
+
* an allocation verifier — use `buildFactoryDistributionScript` (output-enforced)
|
|
87
|
+
* or the `settlement: 'full-distribution'` funding branch instead.
|
|
88
|
+
*/
|
|
89
|
+
export declare function buildFactoryPresignedDistributionScript(config: FactoryDistributionScriptConfig): string;
|
|
90
|
+
/**
|
|
91
|
+
* OMN-058/RFC-079 §4A — output-enforced full distribution for a factory coin.
|
|
92
|
+
*
|
|
93
|
+
* After the committed escape block, any participant may settle the factory by
|
|
94
|
+
* paying **every** committed share. The covenant reads the shares from the spent
|
|
95
|
+
* coin (`PREVSTATE`), pins the output count, each destination, each amount and
|
|
96
|
+
* the total, so neither the broadcaster nor an outsider can redirect or resize a
|
|
97
|
+
* payout. One participant signature authorizes the trigger.
|
|
98
|
+
*
|
|
99
|
+
* Use `buildFactoryDistribution` to materialize the matching transaction.
|
|
79
100
|
*/
|
|
80
101
|
export declare function buildFactoryDistributionScript(config: FactoryDistributionScriptConfig): string;
|
|
102
|
+
/**
|
|
103
|
+
* OMN-060: deterministic party-set root committed by STATE(111).
|
|
104
|
+
* Canonical: parties sorted by id, `id:pubkeyDigest`.
|
|
105
|
+
*/
|
|
106
|
+
export declare function computeFactoryPartyRoot(participants: FactoryParticipant[]): string;
|
|
107
|
+
/** OMN-060: deterministic balance root committed by STATE(112). */
|
|
108
|
+
export declare function computeFactoryBalanceRoot(allocations: Record<string, bigint>): string;
|
|
109
|
+
/** Committed balance-set root state variable (STATE(112)) for the funding output. */
|
|
110
|
+
export declare function factoryBalanceRootStateVariable(root: string): {
|
|
111
|
+
port: number;
|
|
112
|
+
value: string;
|
|
113
|
+
type: 'hex';
|
|
114
|
+
};
|
|
81
115
|
/** Committed party-set root state variable (STATE(111)) for the factory funding output. */
|
|
82
116
|
export declare function factoryPartyRootStateVariable(root: string): {
|
|
83
117
|
port: number;
|
|
@@ -97,6 +131,34 @@ export declare const FACTORY_MAX_PARTICIPANTS: number;
|
|
|
97
131
|
export interface FactoryScriptOptions {
|
|
98
132
|
/** Reserved for callers; the escape block is carried in STATE(103) at funding. */
|
|
99
133
|
escapeBlock?: number;
|
|
134
|
+
/**
|
|
135
|
+
* OMN-058 checkpoint mode (RFC-079 §4). When true, the cooperative branch only
|
|
136
|
+
* accepts a same-script, same-value successor whose sequence advanced by one.
|
|
137
|
+
* Every allocation change then has an on-chain artifact, and the unilateral
|
|
138
|
+
* escape pays the latest checkpoint instead of the original funding split.
|
|
139
|
+
*
|
|
140
|
+
* The factory builders (`buildFactoryOpening`, `openFactory`) pass `true`;
|
|
141
|
+
* this option only defaults to off when the script is built directly.
|
|
142
|
+
*
|
|
143
|
+
* Setting it to `false` selects the legacy off-chain mode, where a unilateral
|
|
144
|
+
* exit pays the funding split and a newer allocation is **not** enforceable.
|
|
145
|
+
* OMN-095: that mode now requires `allowUnenforceableAllocations` so it is a
|
|
146
|
+
* deliberate release restriction rather than an accidental default.
|
|
147
|
+
*/
|
|
148
|
+
enforceCheckpoints?: boolean;
|
|
149
|
+
/**
|
|
150
|
+
* OMN-095: acknowledge that `enforceCheckpoints: false` produces a factory
|
|
151
|
+
* whose latest off-chain allocation cannot be enforced on chain. Required to
|
|
152
|
+
* build such a factory; the resulting `ChannelFactory.allocationEnforcement`
|
|
153
|
+
* is `'none'`.
|
|
154
|
+
*/
|
|
155
|
+
allowUnenforceableAllocations?: boolean;
|
|
156
|
+
/**
|
|
157
|
+
* OMN-058/RFC-079 §4A: add the output-enforced full-distribution branch.
|
|
158
|
+
* Any participant can settle every committed share in one transaction; the
|
|
159
|
+
* covenant pins each destination, amount, the output count and conservation.
|
|
160
|
+
*/
|
|
161
|
+
settlement?: 'per-party' | 'full-distribution';
|
|
100
162
|
}
|
|
101
163
|
/**
|
|
102
164
|
* Per-party allocation state variables (port base+k = party k's balance), seeded
|
|
@@ -107,6 +169,7 @@ export declare function factoryBalanceStateVariables(participants: FactoryPartic
|
|
|
107
169
|
port: number;
|
|
108
170
|
value: bigint;
|
|
109
171
|
type: 'number';
|
|
172
|
+
scale: number;
|
|
110
173
|
}[];
|
|
111
174
|
/** Absolute escape-block state variable (STATE(103)) for the factory funding output. */
|
|
112
175
|
export declare function factoryEscapeStateVariable(escapeBlock: number): {
|
|
@@ -115,23 +178,28 @@ export declare function factoryEscapeStateVariable(escapeBlock: number): {
|
|
|
115
178
|
type: 'number';
|
|
116
179
|
};
|
|
117
180
|
/**
|
|
118
|
-
* Build the
|
|
119
|
-
* (RFC-039, CUSTODY-POLICY §2).
|
|
181
|
+
* Build the factory funding script.
|
|
120
182
|
*
|
|
121
183
|
* Spend paths:
|
|
122
|
-
* - **Cooperative /
|
|
123
|
-
* `
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
184
|
+
* - **Cooperative / checkpoint** (any time, all parties): `MULTISIG(N …)`.
|
|
185
|
+
* With `enforceCheckpoints`, the successor must stay at this script, keep the
|
|
186
|
+
* value and advance `STATE(104)` by exactly one.
|
|
187
|
+
* - **Unilateral escape** (after the absolute block in `STATE(103)`): any single
|
|
188
|
+
* party may withdraw its committed share `PREVSTATE(base+k)` to its own
|
|
189
|
+
* settlement address. With a remainder, the successor keeps the script, the
|
|
190
|
+
* other shares are untouched and the withdrawing port is zeroed.
|
|
191
|
+
* - **Full distribution** (`settlement: 'full-distribution'`, after the same
|
|
192
|
+
* block): one transaction pays **every** committed share. The covenant pins
|
|
193
|
+
* the output count, each destination and each amount read from `PREVSTATE`,
|
|
194
|
+
* so a broadcaster cannot redirect or resize a payout. One participant
|
|
195
|
+
* signature is enough to trigger it.
|
|
196
|
+
*
|
|
197
|
+
* The escape branches are terminal (no successor state), so the state-continuity
|
|
198
|
+
* assertions live in the cooperative path only.
|
|
131
199
|
*
|
|
132
200
|
* Minimum 2 participants required.
|
|
133
201
|
*/
|
|
134
|
-
export declare function buildFactoryScript(participants: FactoryParticipant[],
|
|
202
|
+
export declare function buildFactoryScript(participants: FactoryParticipant[], options?: FactoryScriptOptions): string;
|
|
135
203
|
export declare function normalizeScript(script: string): string;
|
|
136
204
|
/** Compute the SHA3-256 script-hash address for a factory script. */
|
|
137
205
|
export declare function scriptAddress(script: string): string;
|