@totemsdk/omnia-splice 0.2.0 → 0.3.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/cjs/coordination.d.ts +55 -0
- package/dist/cjs/coordination.js +137 -0
- package/dist/cjs/durable-splice-store.d.ts +58 -0
- package/dist/cjs/durable-splice-store.js +117 -0
- package/dist/cjs/errors.d.ts +22 -0
- package/dist/cjs/errors.js +54 -0
- package/dist/cjs/finalize.d.ts +92 -0
- package/dist/cjs/finalize.js +222 -0
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/quiesce.d.ts +57 -0
- package/dist/cjs/quiesce.js +67 -0
- package/dist/cjs/splice-tx.d.ts +38 -0
- package/dist/cjs/splice-tx.js +210 -0
- package/dist/cjs/splice.d.ts +71 -0
- package/dist/cjs/splice.js +303 -0
- package/dist/cjs/types.d.ts +130 -0
- package/dist/cjs/types.js +2 -0
- package/dist/coordination.d.ts +55 -0
- package/dist/coordination.js +134 -0
- package/dist/durable-splice-store.d.ts +58 -0
- package/dist/durable-splice-store.js +114 -0
- package/dist/errors.js +7 -17
- package/dist/finalize.d.ts +1 -1
- package/dist/finalize.js +26 -33
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -23
- package/dist/quiesce.js +7 -10
- package/dist/splice-tx.js +13 -18
- package/dist/splice.js +21 -26
- package/dist/types.js +1 -2
- package/package.json +19 -12
- package/rust/pkg/omnia_splice_wasm_bg.wasm +0 -0
- package/rust/pkg/package.json +1 -1
- package/rust/pkg-node/omnia_splice_wasm_bg.wasm +0 -0
- package/rust/pkg-node/package.json +1 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { OmniaChannel } from '@totemsdk/omnia';
|
|
2
|
+
import type { SpliceProposal, SpliceAcceptance, SpliceLeaseProvider, QuiescedChannel } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Initiating party proposes a splice-in: add external funds to the channel.
|
|
5
|
+
*
|
|
6
|
+
* Requires the channel to be in 'quiesced' state (see quiesceChannel).
|
|
7
|
+
*
|
|
8
|
+
* WOTS lease safety: a key slot is reserved via `leaseProvider.wotsLease`
|
|
9
|
+
* before signing. The returned `proposerReservationId` and
|
|
10
|
+
* `proposerSigningIndices` must be passed through to `finalizeSplice`
|
|
11
|
+
* (embedded in the `SpliceProposal`) so the reservation is committed on
|
|
12
|
+
* success or burned on failure, preventing one-time-key reuse.
|
|
13
|
+
*
|
|
14
|
+
* @param channel - Quiesced channel to splice.
|
|
15
|
+
* @param additionalCoinId - CoinId of the external coin being spliced in.
|
|
16
|
+
* @param additionalAmount - Amount of the additional coin.
|
|
17
|
+
* @param leaseProvider - Provides the proposer signer and WOTS lease.
|
|
18
|
+
* @param newBalances - How the new total should be split after splice.
|
|
19
|
+
* When omitted, existing balances are scaled
|
|
20
|
+
* proportionally to the new total value so that
|
|
21
|
+
* `sum(newBalances) === newTotalValue` is preserved.
|
|
22
|
+
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
23
|
+
*/
|
|
24
|
+
export declare function proposeSpliceIn(channel: OmniaChannel | QuiescedChannel, additionalCoinId: string, additionalAmount: bigint, leaseProvider: SpliceLeaseProvider, newBalances?: Record<string, bigint>): Promise<SpliceProposal>;
|
|
25
|
+
/**
|
|
26
|
+
* Initiating party proposes a splice-out: withdraw funds from the channel on-chain.
|
|
27
|
+
*
|
|
28
|
+
* Requires the channel to be in 'quiesced' state (see quiesceChannel).
|
|
29
|
+
*
|
|
30
|
+
* WOTS lease safety: reserves a key slot before signing (same semantics as
|
|
31
|
+
* proposeSpliceIn). The `proposerReservationId` in the returned `SpliceProposal`
|
|
32
|
+
* must be committed/burned by `finalizeSplice`.
|
|
33
|
+
*
|
|
34
|
+
* @param channel - Quiesced channel.
|
|
35
|
+
* @param withdrawAmount - Amount to remove from the channel.
|
|
36
|
+
* @param withdrawAddress - On-chain destination for the withdrawn funds.
|
|
37
|
+
* @param leaseProvider - Provides the proposer signer and WOTS lease.
|
|
38
|
+
* @param newBalances - How the remaining total should be split after splice.
|
|
39
|
+
* @param extraOutputs - Optional additional on-chain outputs (third-party payments).
|
|
40
|
+
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
41
|
+
*/
|
|
42
|
+
export declare function proposeSpliceOut(channel: OmniaChannel | QuiescedChannel, withdrawAmount: bigint, withdrawAddress: string, leaseProvider: SpliceLeaseProvider, newBalances?: Record<string, bigint>, extraOutputs?: Array<{
|
|
43
|
+
address: string;
|
|
44
|
+
amount: bigint;
|
|
45
|
+
tokenId?: string;
|
|
46
|
+
}>): Promise<SpliceProposal>;
|
|
47
|
+
/**
|
|
48
|
+
* Counterparty accepts a splice proposal by co-signing the splice TX digest.
|
|
49
|
+
*
|
|
50
|
+
* Requires the channel to be in 'quiesced' state.
|
|
51
|
+
*
|
|
52
|
+
* WOTS lease safety: reserves a key slot before signing. The returned
|
|
53
|
+
* `acceptorReservationId` and `acceptorSigningIndices` are embedded in the
|
|
54
|
+
* `SpliceAcceptance` and consumed by `finalizeSplice` to commit or burn the
|
|
55
|
+
* acceptor's key-slot reservation.
|
|
56
|
+
*
|
|
57
|
+
* The acceptor independently:
|
|
58
|
+
* 1. Validates party membership (proposer and acceptor both in channel).
|
|
59
|
+
* 2. Validates proposal balance conservation and amount constraints.
|
|
60
|
+
* 3. Cryptographically binds the signed draft to the proposal params by
|
|
61
|
+
* recomputing the expected draft from `params + channel` and comparing
|
|
62
|
+
* digests — rejects if they differ (tamper detection).
|
|
63
|
+
* 4. Reserves a WOTS key slot via `leaseProvider.wotsLease`.
|
|
64
|
+
* 5. Signs the splice TX digest with reserved indices.
|
|
65
|
+
*
|
|
66
|
+
* @param channel - Quiesced channel.
|
|
67
|
+
* @param proposal - Splice proposal from the initiating party.
|
|
68
|
+
* @param leaseProvider - Provides the acceptor signer and WOTS lease.
|
|
69
|
+
* @returns SpliceAcceptance containing acceptor co-signature and lease data.
|
|
70
|
+
*/
|
|
71
|
+
export declare function acceptSplice(channel: OmniaChannel | QuiescedChannel, proposal: SpliceProposal, leaseProvider: SpliceLeaseProvider): Promise<SpliceAcceptance>;
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.proposeSpliceIn = proposeSpliceIn;
|
|
4
|
+
exports.proposeSpliceOut = proposeSpliceOut;
|
|
5
|
+
exports.acceptSplice = acceptSplice;
|
|
6
|
+
const splice_tx_js_1 = require("./splice-tx.js");
|
|
7
|
+
const errors_js_1 = require("./errors.js");
|
|
8
|
+
/**
|
|
9
|
+
* Scale existing balances proportionally to a new total, preserving relative
|
|
10
|
+
* ratios. The last party absorbs rounding so that sum(result) === newTotal
|
|
11
|
+
* exactly. Used as the default newBalances when a caller omits the argument.
|
|
12
|
+
*/
|
|
13
|
+
function scaleBalances(balances, oldTotal, newTotal) {
|
|
14
|
+
if (oldTotal === 0n) {
|
|
15
|
+
const partyIds = Object.keys(balances);
|
|
16
|
+
const each = partyIds.length > 0 ? newTotal / BigInt(partyIds.length) : 0n;
|
|
17
|
+
const result = {};
|
|
18
|
+
let assigned = 0n;
|
|
19
|
+
for (let i = 0; i < partyIds.length - 1; i++) {
|
|
20
|
+
result[partyIds[i]] = each;
|
|
21
|
+
assigned += each;
|
|
22
|
+
}
|
|
23
|
+
const last = partyIds[partyIds.length - 1];
|
|
24
|
+
if (last !== undefined)
|
|
25
|
+
result[last] = newTotal - assigned;
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
const partyIds = Object.keys(balances);
|
|
29
|
+
const scaled = {};
|
|
30
|
+
let assigned = 0n;
|
|
31
|
+
for (let i = 0; i < partyIds.length - 1; i++) {
|
|
32
|
+
const pid = partyIds[i];
|
|
33
|
+
const prev = balances[pid] ?? 0n;
|
|
34
|
+
scaled[pid] = (prev * newTotal) / oldTotal;
|
|
35
|
+
assigned += scaled[pid];
|
|
36
|
+
}
|
|
37
|
+
const last = partyIds[partyIds.length - 1];
|
|
38
|
+
if (last !== undefined)
|
|
39
|
+
scaled[last] = newTotal - assigned;
|
|
40
|
+
return scaled;
|
|
41
|
+
}
|
|
42
|
+
function generateSpliceId() {
|
|
43
|
+
const buf = new Uint8Array(16);
|
|
44
|
+
crypto.getRandomValues(buf);
|
|
45
|
+
return '0x' + buf.reduce((hex, b) => hex + b.toString(16).padStart(2, '0'), '');
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Gate: all splice operations (propose, accept, finalize) require the channel
|
|
49
|
+
* to be in 'quiesced' state. Quiescing ensures all in-flight HTLCs are settled
|
|
50
|
+
* and the pre-splice balance split is mutually signed before any splice TX is built.
|
|
51
|
+
*/
|
|
52
|
+
function validateChannelQuiesced(channel) {
|
|
53
|
+
if (channel.status !== 'quiesced') {
|
|
54
|
+
throw new errors_js_1.SpliceChannelStatusError('quiesced', channel.status);
|
|
55
|
+
}
|
|
56
|
+
const pending = channel.pendingHTLCs.filter(h => h.status === 'pending').length;
|
|
57
|
+
if (pending > 0) {
|
|
58
|
+
throw new errors_js_1.PendingHTLCError(pending);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Validate proposal balance conservation and amount constraints.
|
|
63
|
+
* Called by acceptSplice before signing to independently verify inbound params.
|
|
64
|
+
*/
|
|
65
|
+
function validateProposalParams(channel, params) {
|
|
66
|
+
const balanceSum = Object.values(params.newBalances).reduce((a, b) => a + b, 0n);
|
|
67
|
+
if (balanceSum !== params.newTotalValue) {
|
|
68
|
+
throw new Error(`Proposal integrity check failed: newBalances sum (${balanceSum}) !== newTotalValue (${params.newTotalValue})`);
|
|
69
|
+
}
|
|
70
|
+
if (params.type === 'splice_out' && (params.withdrawAmount ?? 0n) > channel.totalValue) {
|
|
71
|
+
throw new Error(`Proposal integrity check failed: withdrawAmount (${params.withdrawAmount}) exceeds channel totalValue (${channel.totalValue})`);
|
|
72
|
+
}
|
|
73
|
+
const channelOutput = params.newTotalValue;
|
|
74
|
+
const withdrawSum = params.withdrawAmount ?? 0n;
|
|
75
|
+
const extraSum = (params.extraOutputs ?? []).reduce((a, o) => a + o.amount, 0n);
|
|
76
|
+
if (params.type === 'splice_in') {
|
|
77
|
+
const expectedExistingInput = channelOutput + withdrawSum + extraSum - (params.additionalAmount ?? 0n);
|
|
78
|
+
if (expectedExistingInput !== channel.totalValue) {
|
|
79
|
+
throw new Error(`Proposal integrity check failed: splice-in amounts inconsistent ` +
|
|
80
|
+
`(expected existing channel input ${channel.totalValue}, derived ${expectedExistingInput})`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
if (channelOutput + withdrawSum + extraSum !== channel.totalValue) {
|
|
85
|
+
throw new Error(`Proposal integrity check failed: splice-out amounts do not sum to channel total ` +
|
|
86
|
+
`(${channelOutput} + ${withdrawSum} + ${extraSum} !== ${channel.totalValue})`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Verify that `spliceTxDraft` is exactly what the params would produce by
|
|
92
|
+
* rebuilding the draft deterministically and comparing digests.
|
|
93
|
+
*
|
|
94
|
+
* Prevents a tampered proposal where params.newBalances is modified but
|
|
95
|
+
* spliceTxDraft is left unchanged (or vice versa), causing channel state
|
|
96
|
+
* to diverge from the actual on-chain TX outputs.
|
|
97
|
+
*/
|
|
98
|
+
function bindDraftToParams(channel, proposal) {
|
|
99
|
+
const recomputed = (0, splice_tx_js_1.buildSpliceTx)(channel, proposal.params);
|
|
100
|
+
const expected = (0, splice_tx_js_1.computeSpliceTxDigest)(recomputed);
|
|
101
|
+
const actual = (0, splice_tx_js_1.computeSpliceTxDigest)(proposal.spliceTxDraft);
|
|
102
|
+
if (!expected.every((b, i) => b === actual[i])) {
|
|
103
|
+
throw new Error('Proposal spliceTxDraft is inconsistent with proposal.params — ' +
|
|
104
|
+
'the TX draft may have been tampered with');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Initiating party proposes a splice-in: add external funds to the channel.
|
|
109
|
+
*
|
|
110
|
+
* Requires the channel to be in 'quiesced' state (see quiesceChannel).
|
|
111
|
+
*
|
|
112
|
+
* WOTS lease safety: a key slot is reserved via `leaseProvider.wotsLease`
|
|
113
|
+
* before signing. The returned `proposerReservationId` and
|
|
114
|
+
* `proposerSigningIndices` must be passed through to `finalizeSplice`
|
|
115
|
+
* (embedded in the `SpliceProposal`) so the reservation is committed on
|
|
116
|
+
* success or burned on failure, preventing one-time-key reuse.
|
|
117
|
+
*
|
|
118
|
+
* @param channel - Quiesced channel to splice.
|
|
119
|
+
* @param additionalCoinId - CoinId of the external coin being spliced in.
|
|
120
|
+
* @param additionalAmount - Amount of the additional coin.
|
|
121
|
+
* @param leaseProvider - Provides the proposer signer and WOTS lease.
|
|
122
|
+
* @param newBalances - How the new total should be split after splice.
|
|
123
|
+
* When omitted, existing balances are scaled
|
|
124
|
+
* proportionally to the new total value so that
|
|
125
|
+
* `sum(newBalances) === newTotalValue` is preserved.
|
|
126
|
+
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
127
|
+
*/
|
|
128
|
+
async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leaseProvider, newBalances) {
|
|
129
|
+
validateChannelQuiesced(channel);
|
|
130
|
+
const newTotal = channel.totalValue + additionalAmount;
|
|
131
|
+
const effectiveNewBalances = newBalances ?? scaleBalances(channel.balances, channel.totalValue, newTotal);
|
|
132
|
+
const params = {
|
|
133
|
+
type: 'splice_in',
|
|
134
|
+
newTotalValue: newTotal,
|
|
135
|
+
newBalances: effectiveNewBalances,
|
|
136
|
+
additionalCoinId,
|
|
137
|
+
additionalAmount,
|
|
138
|
+
};
|
|
139
|
+
const draft = (0, splice_tx_js_1.buildSpliceTx)(channel, params);
|
|
140
|
+
const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(draft);
|
|
141
|
+
const spliceTxHex = Buffer.from((0, splice_tx_js_1.spliceDraftToMinimaBytes)(draft)).toString('hex');
|
|
142
|
+
const { signer, wotsLease } = leaseProvider;
|
|
143
|
+
const partyInChannel = channel.parties.find(p => p.publicKeyDigest === signer.publicKeyDigest);
|
|
144
|
+
if (!partyInChannel) {
|
|
145
|
+
throw new errors_js_1.SpliceMissingPartyError(signer.publicKeyDigest);
|
|
146
|
+
}
|
|
147
|
+
const reservation = await wotsLease.reserveKeyUse({
|
|
148
|
+
treeId: signer.publicKeyDigest,
|
|
149
|
+
purpose: 'omnia-splice-propose',
|
|
150
|
+
payloadHash: Buffer.from(digest).toString('hex'),
|
|
151
|
+
});
|
|
152
|
+
const signature = await signer.sign(digest, reservation.indices);
|
|
153
|
+
return {
|
|
154
|
+
spliceId: generateSpliceId(),
|
|
155
|
+
channelId: channel.channelId,
|
|
156
|
+
params,
|
|
157
|
+
spliceTxHex,
|
|
158
|
+
spliceTxDraft: draft,
|
|
159
|
+
proposerPublicKeyDigest: signer.publicKeyDigest,
|
|
160
|
+
proposerSignature: signature,
|
|
161
|
+
proposerReservationId: reservation.reservationId,
|
|
162
|
+
proposerSigningIndices: reservation.indices,
|
|
163
|
+
proposedAt: Date.now(),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Initiating party proposes a splice-out: withdraw funds from the channel on-chain.
|
|
168
|
+
*
|
|
169
|
+
* Requires the channel to be in 'quiesced' state (see quiesceChannel).
|
|
170
|
+
*
|
|
171
|
+
* WOTS lease safety: reserves a key slot before signing (same semantics as
|
|
172
|
+
* proposeSpliceIn). The `proposerReservationId` in the returned `SpliceProposal`
|
|
173
|
+
* must be committed/burned by `finalizeSplice`.
|
|
174
|
+
*
|
|
175
|
+
* @param channel - Quiesced channel.
|
|
176
|
+
* @param withdrawAmount - Amount to remove from the channel.
|
|
177
|
+
* @param withdrawAddress - On-chain destination for the withdrawn funds.
|
|
178
|
+
* @param leaseProvider - Provides the proposer signer and WOTS lease.
|
|
179
|
+
* @param newBalances - How the remaining total should be split after splice.
|
|
180
|
+
* @param extraOutputs - Optional additional on-chain outputs (third-party payments).
|
|
181
|
+
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
182
|
+
*/
|
|
183
|
+
async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress, leaseProvider, newBalances, extraOutputs) {
|
|
184
|
+
validateChannelQuiesced(channel);
|
|
185
|
+
const extraSum = (extraOutputs ?? []).reduce((a, o) => a + o.amount, 0n);
|
|
186
|
+
const totalOutflow = withdrawAmount + extraSum;
|
|
187
|
+
if (totalOutflow > channel.totalValue) {
|
|
188
|
+
throw new Error(`proposeSpliceOut: total outflow (withdraw ${withdrawAmount} + extra outputs ${extraSum} = ${totalOutflow}) ` +
|
|
189
|
+
`exceeds channel totalValue (${channel.totalValue})`);
|
|
190
|
+
}
|
|
191
|
+
const remainingTotal = channel.totalValue - totalOutflow;
|
|
192
|
+
const effectiveNewBalances = newBalances ?? (() => {
|
|
193
|
+
const ratio = channel.totalValue > 0n ? remainingTotal * 1000n / channel.totalValue : 0n;
|
|
194
|
+
const adjusted = {};
|
|
195
|
+
let assigned = 0n;
|
|
196
|
+
const partyIds = Object.keys(channel.balances);
|
|
197
|
+
for (let i = 0; i < partyIds.length - 1; i++) {
|
|
198
|
+
const pid = partyIds[i];
|
|
199
|
+
const prev = channel.balances[pid] ?? 0n;
|
|
200
|
+
const scaled = (prev * ratio) / 1000n;
|
|
201
|
+
adjusted[pid] = scaled;
|
|
202
|
+
assigned += scaled;
|
|
203
|
+
}
|
|
204
|
+
const lastId = partyIds[partyIds.length - 1];
|
|
205
|
+
if (lastId)
|
|
206
|
+
adjusted[lastId] = remainingTotal - assigned;
|
|
207
|
+
return adjusted;
|
|
208
|
+
})();
|
|
209
|
+
const params = {
|
|
210
|
+
type: 'splice_out',
|
|
211
|
+
newTotalValue: remainingTotal,
|
|
212
|
+
newBalances: effectiveNewBalances,
|
|
213
|
+
withdrawAmount,
|
|
214
|
+
withdrawAddress,
|
|
215
|
+
extraOutputs,
|
|
216
|
+
};
|
|
217
|
+
const draft = (0, splice_tx_js_1.buildSpliceTx)(channel, params);
|
|
218
|
+
const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(draft);
|
|
219
|
+
const spliceTxHex = Buffer.from((0, splice_tx_js_1.spliceDraftToMinimaBytes)(draft)).toString('hex');
|
|
220
|
+
const { signer, wotsLease } = leaseProvider;
|
|
221
|
+
const partyInChannel = channel.parties.find(p => p.publicKeyDigest === signer.publicKeyDigest);
|
|
222
|
+
if (!partyInChannel) {
|
|
223
|
+
throw new errors_js_1.SpliceMissingPartyError(signer.publicKeyDigest);
|
|
224
|
+
}
|
|
225
|
+
const reservation = await wotsLease.reserveKeyUse({
|
|
226
|
+
treeId: signer.publicKeyDigest,
|
|
227
|
+
purpose: 'omnia-splice-propose',
|
|
228
|
+
payloadHash: Buffer.from(digest).toString('hex'),
|
|
229
|
+
});
|
|
230
|
+
const signature = await signer.sign(digest, reservation.indices);
|
|
231
|
+
return {
|
|
232
|
+
spliceId: generateSpliceId(),
|
|
233
|
+
channelId: channel.channelId,
|
|
234
|
+
params,
|
|
235
|
+
spliceTxHex,
|
|
236
|
+
spliceTxDraft: draft,
|
|
237
|
+
proposerPublicKeyDigest: signer.publicKeyDigest,
|
|
238
|
+
proposerSignature: signature,
|
|
239
|
+
proposerReservationId: reservation.reservationId,
|
|
240
|
+
proposerSigningIndices: reservation.indices,
|
|
241
|
+
proposedAt: Date.now(),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Counterparty accepts a splice proposal by co-signing the splice TX digest.
|
|
246
|
+
*
|
|
247
|
+
* Requires the channel to be in 'quiesced' state.
|
|
248
|
+
*
|
|
249
|
+
* WOTS lease safety: reserves a key slot before signing. The returned
|
|
250
|
+
* `acceptorReservationId` and `acceptorSigningIndices` are embedded in the
|
|
251
|
+
* `SpliceAcceptance` and consumed by `finalizeSplice` to commit or burn the
|
|
252
|
+
* acceptor's key-slot reservation.
|
|
253
|
+
*
|
|
254
|
+
* The acceptor independently:
|
|
255
|
+
* 1. Validates party membership (proposer and acceptor both in channel).
|
|
256
|
+
* 2. Validates proposal balance conservation and amount constraints.
|
|
257
|
+
* 3. Cryptographically binds the signed draft to the proposal params by
|
|
258
|
+
* recomputing the expected draft from `params + channel` and comparing
|
|
259
|
+
* digests — rejects if they differ (tamper detection).
|
|
260
|
+
* 4. Reserves a WOTS key slot via `leaseProvider.wotsLease`.
|
|
261
|
+
* 5. Signs the splice TX digest with reserved indices.
|
|
262
|
+
*
|
|
263
|
+
* @param channel - Quiesced channel.
|
|
264
|
+
* @param proposal - Splice proposal from the initiating party.
|
|
265
|
+
* @param leaseProvider - Provides the acceptor signer and WOTS lease.
|
|
266
|
+
* @returns SpliceAcceptance containing acceptor co-signature and lease data.
|
|
267
|
+
*/
|
|
268
|
+
async function acceptSplice(channel, proposal, leaseProvider) {
|
|
269
|
+
validateChannelQuiesced(channel);
|
|
270
|
+
if (proposal.channelId !== channel.channelId) {
|
|
271
|
+
throw new Error(`Proposal channelId '${proposal.channelId}' does not match channel '${channel.channelId}'`);
|
|
272
|
+
}
|
|
273
|
+
const proposerInChannel = channel.parties.find(p => p.publicKeyDigest === proposal.proposerPublicKeyDigest);
|
|
274
|
+
if (!proposerInChannel) {
|
|
275
|
+
throw new errors_js_1.SpliceMissingPartyError(proposal.proposerPublicKeyDigest);
|
|
276
|
+
}
|
|
277
|
+
const { signer, wotsLease } = leaseProvider;
|
|
278
|
+
const partyInChannel = channel.parties.find(p => p.publicKeyDigest === signer.publicKeyDigest);
|
|
279
|
+
if (!partyInChannel) {
|
|
280
|
+
throw new errors_js_1.SpliceMissingPartyError(signer.publicKeyDigest);
|
|
281
|
+
}
|
|
282
|
+
if (signer.publicKeyDigest === proposal.proposerPublicKeyDigest) {
|
|
283
|
+
throw new Error('Acceptor and proposer cannot be the same party');
|
|
284
|
+
}
|
|
285
|
+
validateProposalParams(channel, proposal.params);
|
|
286
|
+
bindDraftToParams(channel, proposal);
|
|
287
|
+
const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(proposal.spliceTxDraft);
|
|
288
|
+
const reservation = await wotsLease.reserveKeyUse({
|
|
289
|
+
treeId: signer.publicKeyDigest,
|
|
290
|
+
purpose: 'omnia-splice-accept',
|
|
291
|
+
payloadHash: Buffer.from(digest).toString('hex'),
|
|
292
|
+
});
|
|
293
|
+
const signature = await signer.sign(digest, reservation.indices);
|
|
294
|
+
return {
|
|
295
|
+
spliceId: proposal.spliceId,
|
|
296
|
+
channelId: channel.channelId,
|
|
297
|
+
acceptorPublicKeyDigest: signer.publicKeyDigest,
|
|
298
|
+
acceptorSignature: signature,
|
|
299
|
+
acceptorReservationId: reservation.reservationId,
|
|
300
|
+
acceptorSigningIndices: reservation.indices,
|
|
301
|
+
acceptedAt: Date.now(),
|
|
302
|
+
};
|
|
303
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { WotsLeaseProvider, SigningIndices } from '@totemsdk/wots-lease';
|
|
2
|
+
import type { OmniaChannel, ChannelSigner, SignedChannelState } from '@totemsdk/omnia';
|
|
3
|
+
export type { OmniaChannel, ChannelSigner, SignedChannelState };
|
|
4
|
+
export type { WotsLeaseProvider, SigningIndices };
|
|
5
|
+
export type WotsSignature = Uint8Array;
|
|
6
|
+
export type SpliceType = 'splice_in' | 'splice_out';
|
|
7
|
+
export interface SpliceParams {
|
|
8
|
+
type: SpliceType;
|
|
9
|
+
newTotalValue: bigint;
|
|
10
|
+
newBalances: Record<string, bigint>;
|
|
11
|
+
additionalCoinId?: string;
|
|
12
|
+
additionalCoinAddress?: string;
|
|
13
|
+
additionalAmount?: bigint;
|
|
14
|
+
withdrawAmount?: bigint;
|
|
15
|
+
withdrawAddress?: string;
|
|
16
|
+
extraOutputs?: Array<{
|
|
17
|
+
address: string;
|
|
18
|
+
amount: bigint;
|
|
19
|
+
tokenId?: string;
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
22
|
+
export interface SpliceTxInput {
|
|
23
|
+
coinId: string;
|
|
24
|
+
address: string;
|
|
25
|
+
amount: bigint;
|
|
26
|
+
tokenId: string;
|
|
27
|
+
}
|
|
28
|
+
export interface SpliceTxOutput {
|
|
29
|
+
address: string;
|
|
30
|
+
amount: bigint;
|
|
31
|
+
tokenId: string;
|
|
32
|
+
storeState: boolean;
|
|
33
|
+
stateVarSettlement: boolean;
|
|
34
|
+
stateVarSequence: number;
|
|
35
|
+
}
|
|
36
|
+
export interface SpliceTxDraft {
|
|
37
|
+
inputs: SpliceTxInput[];
|
|
38
|
+
outputs: SpliceTxOutput[];
|
|
39
|
+
channelId: string;
|
|
40
|
+
params: SpliceParams;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Splice proposal produced by the initiating party.
|
|
44
|
+
*
|
|
45
|
+
* `spliceTxHex` contains the canonical Minima TX bytes (hex) that both parties
|
|
46
|
+
* independently verify before signing. `spliceTxDraft` retains the structured
|
|
47
|
+
* representation needed for digest computation and output inspection by the
|
|
48
|
+
* acceptor; it is NOT sent over the wire in production but is included here for
|
|
49
|
+
* the v0.1.0 in-process protocol. Future relay integration will transmit only
|
|
50
|
+
* `spliceTxHex` + `params`.
|
|
51
|
+
*
|
|
52
|
+
* `proposerReservationId` and `proposerSigningIndices` carry the WOTS lease
|
|
53
|
+
* reservation used when producing `proposerSignature`. They are consumed by
|
|
54
|
+
* `finalizeSplice` to commit or burn the reservation after the splice TX is
|
|
55
|
+
* settled, preserving key-slot accounting and preventing one-time-key reuse.
|
|
56
|
+
*/
|
|
57
|
+
export interface SpliceProposal {
|
|
58
|
+
spliceId: string;
|
|
59
|
+
channelId: string;
|
|
60
|
+
params: SpliceParams;
|
|
61
|
+
spliceTxHex: string;
|
|
62
|
+
spliceTxDraft: SpliceTxDraft;
|
|
63
|
+
proposerPublicKeyDigest: string;
|
|
64
|
+
proposerSignature: WotsSignature;
|
|
65
|
+
proposerReservationId: string;
|
|
66
|
+
proposerSigningIndices: SigningIndices;
|
|
67
|
+
proposedAt: number;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* `acceptorReservationId` and `acceptorSigningIndices` carry the WOTS lease
|
|
71
|
+
* reservation used when producing `acceptorSignature`. Consumed by
|
|
72
|
+
* `finalizeSplice` to commit or burn the acceptor's key-slot reservation.
|
|
73
|
+
*/
|
|
74
|
+
export interface SpliceAcceptance {
|
|
75
|
+
spliceId: string;
|
|
76
|
+
channelId: string;
|
|
77
|
+
acceptorPublicKeyDigest: string;
|
|
78
|
+
acceptorSignature: WotsSignature;
|
|
79
|
+
acceptorReservationId: string;
|
|
80
|
+
acceptorSigningIndices: SigningIndices;
|
|
81
|
+
acceptedAt: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* A channel returned by `quiesceChannel`.
|
|
85
|
+
*
|
|
86
|
+
* - `status: 'quiesced'` — no new HTLCs may be added until the splice confirms.
|
|
87
|
+
* - `pendingHTLCs: []` — all resolved HTLCs are cleared from the active list.
|
|
88
|
+
* - `quiesceSignedState` — the local party's partial signed state over the
|
|
89
|
+
* final pre-splice balance split (sequence incremented by one). Exchange this
|
|
90
|
+
* with the counterparty to obtain their co-signature before finalizing the splice.
|
|
91
|
+
*/
|
|
92
|
+
export type QuiescedChannel = Omit<OmniaChannel, 'status'> & {
|
|
93
|
+
status: 'quiesced';
|
|
94
|
+
quiesceSignedState: Partial<SignedChannelState>;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* A channel returned by `finalizeSplice`.
|
|
98
|
+
*
|
|
99
|
+
* Extends OmniaChannel with splice provenance fields. The returned channel is
|
|
100
|
+
* immediately usable for new payments: `status: 'active'`, `currentSequence: 0`,
|
|
101
|
+
* fresh WOTS budget. `splicedFrom` identifies the old channel and
|
|
102
|
+
* `spliceFundingCoinId` is the new on-chain UTXO.
|
|
103
|
+
*
|
|
104
|
+
* **Mutation side effect**: `finalizeSplice` mutates the `channel` argument
|
|
105
|
+
* (the `QuiescedChannel` passed in) by setting `status: 'spliced'` after the
|
|
106
|
+
* splice TX is confirmed. This invalidates the old channel in-place; the
|
|
107
|
+
* returned `SplicedChannel` (status: 'active') is the only valid live channel.
|
|
108
|
+
*/
|
|
109
|
+
export type SplicedChannel = OmniaChannel & {
|
|
110
|
+
splicedFrom: string;
|
|
111
|
+
spliceType: SpliceType;
|
|
112
|
+
spliceFundingTxId: string;
|
|
113
|
+
spliceFundingCoinId: string;
|
|
114
|
+
};
|
|
115
|
+
export interface SpliceLeaseProvider {
|
|
116
|
+
/** Local party's WOTS signer. */
|
|
117
|
+
signer: ChannelSigner;
|
|
118
|
+
/** WOTS lease provider for key slot reservation/commit (required for quiesceChannel). */
|
|
119
|
+
wotsLease: WotsLeaseProvider;
|
|
120
|
+
/** Optional function to broadcast the mined splice TxPoW to the chain. */
|
|
121
|
+
broadcast?: (txHex: string) => Promise<{
|
|
122
|
+
txpowid?: string;
|
|
123
|
+
success?: boolean;
|
|
124
|
+
}>;
|
|
125
|
+
}
|
|
126
|
+
export interface SpliceSigningIndices {
|
|
127
|
+
l1: number;
|
|
128
|
+
l2: number;
|
|
129
|
+
addressIndex: number;
|
|
130
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splice peer round-trip (RFC-035 B1).
|
|
3
|
+
*
|
|
4
|
+
* Two-party splice over the coordination transport:
|
|
5
|
+
*
|
|
6
|
+
* proposer ── SPLICE_PROPOSAL ─▶ acceptor
|
|
7
|
+
* proposer ◀─ SPLICE_ACCEPT ── acceptor (acceptor reserves a WOTS leaf)
|
|
8
|
+
* proposer ── SPLICE_FINALIZED ─▶ acceptor (acceptor commits its leaf)
|
|
9
|
+
*
|
|
10
|
+
* Verify-before-sign: the acceptor cryptographically verifies the proposer's
|
|
11
|
+
* signature over the splice digest (and `acceptSplice` binds the draft to the
|
|
12
|
+
* local channel) before it signs. Each party commits/burns its **own** lease:
|
|
13
|
+
* the proposer on `finalizeSplice`, the acceptor on `SPLICE_FINALIZED` (or burns
|
|
14
|
+
* it when no confirmation arrives within the TTL).
|
|
15
|
+
*/
|
|
16
|
+
import type { OmniaChannel, OmniaPeer, OmniaCoordination, CoordinationHandlers } from '@totemsdk/omnia';
|
|
17
|
+
import type { SpliceProposal, SpliceLeaseProvider, SpliceParams, QuiescedChannel, SplicedChannel } from './types.js';
|
|
18
|
+
import { type FinalizeSpliceOptions } from './finalize.js';
|
|
19
|
+
export type VerifySignature = (signature: Uint8Array, digest: Uint8Array, publicKeyDigest: string) => boolean;
|
|
20
|
+
export type ChannelStore = Map<string, OmniaChannel | QuiescedChannel>;
|
|
21
|
+
export interface SpliceApprovalRequest {
|
|
22
|
+
channelId: string;
|
|
23
|
+
spliceId: string;
|
|
24
|
+
proposer: string;
|
|
25
|
+
params: SpliceParams;
|
|
26
|
+
}
|
|
27
|
+
export type SpliceApproval = (request: SpliceApprovalRequest) => Promise<boolean>;
|
|
28
|
+
export interface SpliceCoordinationDeps {
|
|
29
|
+
/** This peer's known channels (its own quiesced copies). */
|
|
30
|
+
channels: ChannelStore;
|
|
31
|
+
/** This peer's signer + WOTS lease provider. */
|
|
32
|
+
leaseProvider: SpliceLeaseProvider;
|
|
33
|
+
/** Signature verifier; defaults to `verifyChannelSignature` (RFC-037). */
|
|
34
|
+
verifySignature?: VerifySignature;
|
|
35
|
+
/** Extra finalize options (broadcast, mineDifficulty). */
|
|
36
|
+
finalizeOptions?: FinalizeSpliceOptions;
|
|
37
|
+
/** Consent gate for signing an acceptance (RFC-014 §6.3). */
|
|
38
|
+
approval?: SpliceApproval;
|
|
39
|
+
/** Called on the proposer when the splice finalizes. */
|
|
40
|
+
onFinalized?: (channel: SplicedChannel, peer: OmniaPeer) => void;
|
|
41
|
+
/** Acceptor reservation TTL before burn (default 24h). */
|
|
42
|
+
ttlMs?: number;
|
|
43
|
+
}
|
|
44
|
+
export interface SpliceProposeParams {
|
|
45
|
+
type: 'in' | 'out';
|
|
46
|
+
additionalCoinId?: string;
|
|
47
|
+
additionalAmount?: bigint;
|
|
48
|
+
withdrawAmount?: bigint;
|
|
49
|
+
withdrawAddress?: string;
|
|
50
|
+
}
|
|
51
|
+
export interface SpliceCoordination {
|
|
52
|
+
handlers: CoordinationHandlers;
|
|
53
|
+
propose(peer: OmniaPeer, coordination: OmniaCoordination, channel: OmniaChannel | QuiescedChannel, params: SpliceProposeParams): Promise<SpliceProposal>;
|
|
54
|
+
}
|
|
55
|
+
export declare function createSpliceCoordination(deps: SpliceCoordinationDeps): SpliceCoordination;
|