@totemsdk/omnia-factory 0.7.1 → 0.8.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/README.md +5 -0
- 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 +43 -3
- package/dist/cjs/factory.js +291 -118
- package/dist/cjs/index.d.ts +6 -2
- package/dist/cjs/index.js +16 -1
- package/dist/cjs/script.d.ts +73 -17
- package/dist/cjs/script.js +170 -43
- package/dist/cjs/types.d.ts +8 -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 +43 -3
- package/dist/factory.js +289 -119
- package/dist/index.d.ts +6 -2
- package/dist/index.js +4 -2
- package/dist/script.d.ts +73 -17
- package/dist/script.js +153 -30
- package/dist/types.d.ts +8 -0
- package/dist/virtual.js +12 -0
- package/package.json +7 -10
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OMN-058 / RFC-079 §4A — output-enforced full distribution.
|
|
3
|
+
*
|
|
4
|
+
* The covenant built by `buildFactoryDistributionScript` (and the
|
|
5
|
+
* `settlement: 'full-distribution'` funding branch) pays every committed share
|
|
6
|
+
* of the *spent* coin. This module materializes the matching transaction from
|
|
7
|
+
* the factory's authoritative allocation, so the SDK cannot build a payout set
|
|
8
|
+
* the covenant would reject.
|
|
9
|
+
*
|
|
10
|
+
* Scope: the latest **confirmed** allocation (funding coin or checkpoint). A
|
|
11
|
+
* newer off-chain state is not covered until the contestable protocol lands.
|
|
12
|
+
*/
|
|
13
|
+
import { wotsAddressData } from '@totemsdk/core';
|
|
14
|
+
/**
|
|
15
|
+
* Refuse to distribute while the factory still backs open virtual channels: the
|
|
16
|
+
* distribution covenant pays only the participant shares, so a VC reserve would
|
|
17
|
+
* be stranded or, worse, paid to the wrong party.
|
|
18
|
+
*/
|
|
19
|
+
export function assertFactoryDistributable(factory) {
|
|
20
|
+
const open = (factory.virtualChannels ?? []).filter((channel) => channel.status !== 'closed' && channel.totalValue > 0n);
|
|
21
|
+
if (open.length > 0) {
|
|
22
|
+
const ids = open.map((channel) => channel.channelId).join(', ');
|
|
23
|
+
throw new Error(`factory ${factory.factoryId} cannot be distributed while ${open.length} virtual channel(s) hold value (${ids}); `
|
|
24
|
+
+ 'settle them to separately enforceable outputs first');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The exact payout set for the current committed allocation, in participant
|
|
29
|
+
* order. Addresses default to the participant settlement address.
|
|
30
|
+
*/
|
|
31
|
+
export function factoryDistributionPayouts(factory) {
|
|
32
|
+
const participants = factory.participants ?? [];
|
|
33
|
+
if (participants.length === 0) {
|
|
34
|
+
throw new Error(`factory ${factory.factoryId} has no participants`);
|
|
35
|
+
}
|
|
36
|
+
let sum = 0n;
|
|
37
|
+
const payouts = participants.map((participant, index) => {
|
|
38
|
+
const amount = factory.allocations?.[participant.partyId] ?? 0n;
|
|
39
|
+
if (amount < 0n) {
|
|
40
|
+
throw new Error(`allocation for ${participant.partyId} must be non-negative`);
|
|
41
|
+
}
|
|
42
|
+
// The covenant is built once, at funding, and pins one output per
|
|
43
|
+
// participant (`@TOTOUT EQ N`) with `VERIFYOUT(k … PREVSTATE(port))`. A
|
|
44
|
+
// zero committed share would require a zero-value output, which Minima
|
|
45
|
+
// rejects (`0 < amount`), so the branch cannot settle. Fail closed with the
|
|
46
|
+
// actionable alternative instead of building an invalid transaction.
|
|
47
|
+
if (amount === 0n) {
|
|
48
|
+
throw new Error(`factory ${factory.factoryId} cannot distribute: participant ${participant.partyId} has a zero committed share `
|
|
49
|
+
+ 'and the distribution covenant requires a non-zero output for every participant. '
|
|
50
|
+
+ 'Rebalance with a checkpoint, or use the per-party escape for the other shares.');
|
|
51
|
+
}
|
|
52
|
+
sum += amount;
|
|
53
|
+
const raw = (participant.settlementAddress ?? participant.publicKeyDigest ?? '').replace(/^0x/i, '');
|
|
54
|
+
if (!/^[0-9a-fA-F]{64}$/.test(raw)) {
|
|
55
|
+
throw new Error(`participant[${index}] payout address must be a 64-hex digest`);
|
|
56
|
+
}
|
|
57
|
+
return { partyId: participant.partyId, address: wotsAddressData(raw), amount };
|
|
58
|
+
});
|
|
59
|
+
const vcReserve = (factory.virtualChannels ?? [])
|
|
60
|
+
.filter((channel) => channel.status !== 'closed')
|
|
61
|
+
.reduce((total, channel) => total + channel.totalValue, 0n);
|
|
62
|
+
if (sum + vcReserve !== factory.totalValue) {
|
|
63
|
+
throw new Error(`factory allocations (${sum}) plus virtual-channel reserves (${vcReserve}) do not equal the factory total (${factory.totalValue})`);
|
|
64
|
+
}
|
|
65
|
+
return payouts;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Build the distribution transaction for the factory coin. The output order
|
|
69
|
+
* matches the participant order used by the covenant, and every amount comes
|
|
70
|
+
* from the committed allocation.
|
|
71
|
+
*/
|
|
72
|
+
export function buildFactoryDistribution(factory, params) {
|
|
73
|
+
assertFactoryDistributable(factory);
|
|
74
|
+
if (!params.fundingCoinId) {
|
|
75
|
+
throw new Error('buildFactoryDistribution requires the factory funding coin id');
|
|
76
|
+
}
|
|
77
|
+
const address = params.fundingAddress ?? factory.fundingAddress;
|
|
78
|
+
if (!address) {
|
|
79
|
+
throw new Error('buildFactoryDistribution requires the factory funding address');
|
|
80
|
+
}
|
|
81
|
+
const payouts = factoryDistributionPayouts(factory);
|
|
82
|
+
const total = payouts.reduce((sum, payout) => sum + payout.amount, 0n);
|
|
83
|
+
const outputs = payouts.map((payout) => ({
|
|
84
|
+
partyId: payout.partyId,
|
|
85
|
+
address: payout.address,
|
|
86
|
+
amount: payout.amount,
|
|
87
|
+
tokenId: factory.tokenId,
|
|
88
|
+
storeState: false,
|
|
89
|
+
stateVariables: [],
|
|
90
|
+
}));
|
|
91
|
+
const draft = {
|
|
92
|
+
type: 'settlement',
|
|
93
|
+
inputs: [{
|
|
94
|
+
coinId: params.fundingCoinId,
|
|
95
|
+
address,
|
|
96
|
+
amount: factory.totalValue,
|
|
97
|
+
tokenId: factory.tokenId,
|
|
98
|
+
scriptHex: '',
|
|
99
|
+
storeState: true,
|
|
100
|
+
}],
|
|
101
|
+
outputs,
|
|
102
|
+
storeState: false,
|
|
103
|
+
stateVariables: [],
|
|
104
|
+
};
|
|
105
|
+
return { draft, outputs, total };
|
|
106
|
+
}
|
package/dist/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,20 @@ 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>;
|
|
43
|
+
/**
|
|
44
|
+
* Sign the opening commitment for a plan and record the proposer's signature.
|
|
45
|
+
* Nothing is broadcast and no funding signature is requested, so callers can
|
|
46
|
+
* propose the factory to the peer first (CAN-020 ordering).
|
|
47
|
+
*/
|
|
48
|
+
export declare function proposeFactoryOpening(plan: FactoryOpeningPlan, bundle: WotsLeaseBundle): Promise<ChannelFactory>;
|
|
49
|
+
/**
|
|
50
|
+
* Finish a plan: collect the funding-witness signatures from the plan's
|
|
51
|
+
* funding materials (or use the supplied ones), broadcast the funding
|
|
52
|
+
* transaction when the plan is on-chain, and commit the opening once every
|
|
53
|
+
* participant has signed.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createFactoryFromPlan(plan: FactoryOpeningPlan, bundle: WotsLeaseBundle, chainProvider?: ChainStateProvider): Promise<ChannelFactory>;
|
|
42
56
|
/** A factory opening built without any signatures or broadcast. */
|
|
43
57
|
export interface FactoryOpeningPlan {
|
|
44
58
|
factory: ChannelFactory;
|
|
@@ -46,6 +60,12 @@ export interface FactoryOpeningPlan {
|
|
|
46
60
|
fundingDigest?: Uint8Array;
|
|
47
61
|
fundingCoinId?: string;
|
|
48
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
|
+
}>;
|
|
49
69
|
onChain: boolean;
|
|
50
70
|
}
|
|
51
71
|
export interface FinalizeFactoryOpeningOptions {
|
|
@@ -53,12 +73,23 @@ export interface FinalizeFactoryOpeningOptions {
|
|
|
53
73
|
fundingSignatures?: Record<string, Uint8Array>;
|
|
54
74
|
/** Factory opening signatures to record on the proposal. */
|
|
55
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;
|
|
56
82
|
}
|
|
57
83
|
/**
|
|
58
84
|
* Build a factory and (when every participant supplies a funding coin) its
|
|
59
85
|
* deterministic funding draft, without signing or broadcasting.
|
|
60
86
|
*/
|
|
61
|
-
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>;
|
|
62
93
|
/**
|
|
63
94
|
* Broadcast the prepared factory funding (when on-chain) and record opening
|
|
64
95
|
* signatures. When every participant signature is supplied the factory commits.
|
|
@@ -78,7 +109,9 @@ export declare function finalizeFactoryOpening(plan: FactoryOpeningPlan, opts: F
|
|
|
78
109
|
* @param signer - This counterparty's channel signer (`signer.publicKeyDigest`
|
|
79
110
|
* must match a registered participant not yet signed).
|
|
80
111
|
*/
|
|
81
|
-
export declare function acceptFactory(factory: ChannelFactory, bundle: WotsLeaseBundle
|
|
112
|
+
export declare function acceptFactory(factory: ChannelFactory, bundle: WotsLeaseBundle, opts?: {
|
|
113
|
+
deferCommit?: boolean;
|
|
114
|
+
}): Promise<ChannelFactory>;
|
|
82
115
|
/** A co-signer's opening signature, held until the opening is confirmed. */
|
|
83
116
|
export interface FactoryOpeningSignature {
|
|
84
117
|
factoryId: string;
|
|
@@ -114,3 +147,10 @@ export declare function applyFactorySignature(factory: ChannelFactory, partyId:
|
|
|
114
147
|
* @param leaseProviders - WOTS lease bundles for ALL N factory participants.
|
|
115
148
|
*/
|
|
116
149
|
export declare function reallocate(factory: ChannelFactory, fromPartyId: string, toPartyId: string, amount: bigint, leaseProviders: Record<string, WotsLeaseBundle>): Promise<ChannelFactory>;
|
|
150
|
+
/**
|
|
151
|
+
* Validate a peer-supplied `FACTORY_FINALIZED` transition against the locally
|
|
152
|
+
* accepted factory. The peer may only fill in the funding references and add
|
|
153
|
+
* valid opening signatures; every term, commitment and existing signature must
|
|
154
|
+
* be identical. Returns the validated factory to store.
|
|
155
|
+
*/
|
|
156
|
+
export declare function assertFactoryFinalized(known: ChannelFactory, incoming: ChannelFactory, senderPublicKeyDigest?: string): ChannelFactory;
|