@totemsdk/omnia-splice 0.1.2 → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +3 -3
- package/dist/errors.js +17 -7
- package/dist/finalize.js +29 -26
- package/dist/index.js +23 -5
- package/dist/quiesce.js +10 -7
- package/dist/splice-tx.js +26 -21
- package/dist/splice.js +26 -21
- package/dist/types.js +2 -1
- package/package.json +38 -10
- package/src/__tests__/omnia-splice.test.ts +0 -919
- package/src/errors.ts +0 -63
- package/src/finalize.ts +0 -344
- package/src/index.ts +0 -29
- package/src/quiesce.ts +0 -103
- package/src/splice-tx.ts +0 -208
- package/src/splice.ts +0 -385
- package/src/types.ts +0 -136
|
@@ -1,919 +0,0 @@
|
|
|
1
|
-
import { sha3_256 } from '@noble/hashes/sha3';
|
|
2
|
-
import { MAX_HASH } from '@totemsdk/txpow';
|
|
3
|
-
import type { OmniaChannel, ChannelSigner } from '@totemsdk/omnia';
|
|
4
|
-
import { _resetChannelWatermarks, COINID_ELTOO } from '@totemsdk/omnia';
|
|
5
|
-
import type { SigningIndices, WotsLeaseProvider } from '@totemsdk/wots-lease';
|
|
6
|
-
import {
|
|
7
|
-
quiesceChannel,
|
|
8
|
-
buildSpliceTx,
|
|
9
|
-
computeSpliceTxDigest,
|
|
10
|
-
spliceDraftToMinimaBytes,
|
|
11
|
-
proposeSpliceIn,
|
|
12
|
-
proposeSpliceOut,
|
|
13
|
-
acceptSplice,
|
|
14
|
-
finalizeSplice,
|
|
15
|
-
PendingHTLCError,
|
|
16
|
-
SpliceChannelStatusError,
|
|
17
|
-
SpliceBalanceConservationError,
|
|
18
|
-
SpliceInsufficientFundsError,
|
|
19
|
-
SpliceMissingPartyError,
|
|
20
|
-
SpliceSignatureMismatchError,
|
|
21
|
-
type SpliceLeaseProvider,
|
|
22
|
-
type SpliceParams,
|
|
23
|
-
type QuiescedChannel,
|
|
24
|
-
type QuiesceOptions,
|
|
25
|
-
} from '../index.js';
|
|
26
|
-
|
|
27
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
28
|
-
// Mock sig verifier: matches sig = sha3_256(pkd_bytes || payload) from makeSigner
|
|
29
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
30
|
-
|
|
31
|
-
function mockVerifySignature(sig: Uint8Array, digest: Uint8Array, pkd: string): boolean {
|
|
32
|
-
const expected = sha3_256(
|
|
33
|
-
new Uint8Array([...Buffer.from(pkd.slice(2), 'hex'), ...digest]),
|
|
34
|
-
);
|
|
35
|
-
return expected.length === sig.length && expected.every((b, i) => b === sig[i]);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const TEST_MINE_OPTS = { mineDifficulty: MAX_HASH, verifySignature: mockVerifySignature };
|
|
39
|
-
|
|
40
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
41
|
-
// Helpers
|
|
42
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
43
|
-
|
|
44
|
-
const PKD_ALICE = '0x' + 'aa'.repeat(32);
|
|
45
|
-
const PKD_BOB = '0x' + 'bb'.repeat(32);
|
|
46
|
-
const FUNDING_COIN_ID = '0x' + 'cc'.repeat(32);
|
|
47
|
-
const FUNDING_ADDRESS = '0x' + 'dd'.repeat(32);
|
|
48
|
-
const EXTRA_ADDRESS = '0x' + 'ee'.repeat(32);
|
|
49
|
-
const THIRD_PARTY_ADDR = '0x' + 'ff'.repeat(32);
|
|
50
|
-
const NEW_COIN_ID = '0x' + '11'.repeat(32);
|
|
51
|
-
const NEW_COIN_ADDR = '0x' + '22'.repeat(32);
|
|
52
|
-
const TOKEN_ID = '0x00';
|
|
53
|
-
|
|
54
|
-
function makeSigner(pkd: string): ChannelSigner {
|
|
55
|
-
return {
|
|
56
|
-
publicKeyDigest: pkd,
|
|
57
|
-
async sign(payload: Uint8Array, _indices: SigningIndices) {
|
|
58
|
-
return sha3_256(new Uint8Array([...Buffer.from(pkd.slice(2), 'hex'), ...payload]));
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function makeMockWotsLease(): WotsLeaseProvider {
|
|
64
|
-
let counter = 0;
|
|
65
|
-
return {
|
|
66
|
-
reserveKeyUse: jest.fn(async (_params) => {
|
|
67
|
-
const idx = ++counter;
|
|
68
|
-
return {
|
|
69
|
-
reservationId: `test-res-${idx}`,
|
|
70
|
-
indices: { l1: idx, l2: 0, addressIndex: 0 },
|
|
71
|
-
expiresAt: Date.now() + 3_600_000,
|
|
72
|
-
};
|
|
73
|
-
}),
|
|
74
|
-
commitKeyUse: jest.fn(async (_id, _txId) => {}),
|
|
75
|
-
burnReservation: jest.fn(async (_id, _reason) => {}),
|
|
76
|
-
getLocalWatermark: jest.fn(async (treeId) => ({
|
|
77
|
-
treeId, addressCursor: 0, l1Cursor: 0, l2Cursor: 0, unavailableCount: 0, capacity: 4096,
|
|
78
|
-
})),
|
|
79
|
-
publishWatermark: jest.fn(async (_treeId) => {}),
|
|
80
|
-
syncLeaseJournal: jest.fn(async () => ({ synced: true, conflicts: [] })),
|
|
81
|
-
verifyLeaseCertificate: jest.fn(async (_cert) => true),
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function makeLeaseProvider(pkd: string): SpliceLeaseProvider {
|
|
86
|
-
return { signer: makeSigner(pkd), wotsLease: makeMockWotsLease() };
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
let _channelIdSeq = 0;
|
|
90
|
-
function makeChannel(overrides: Partial<OmniaChannel> = {}): OmniaChannel {
|
|
91
|
-
const seq = ++_channelIdSeq;
|
|
92
|
-
return {
|
|
93
|
-
channelId: '0xab' + seq.toString(16).padStart(62, '0'),
|
|
94
|
-
fundingTxId: '0x' + 'ef'.repeat(32),
|
|
95
|
-
fundingCoinId: FUNDING_COIN_ID,
|
|
96
|
-
fundingScript: 'RETURN TRUE',
|
|
97
|
-
fundingAddress: FUNDING_ADDRESS,
|
|
98
|
-
tokenId: TOKEN_ID,
|
|
99
|
-
tokenScale: 0,
|
|
100
|
-
totalValue: 1000n,
|
|
101
|
-
parties: [
|
|
102
|
-
{ partyId: 'alice', publicKeyDigest: PKD_ALICE, addressIndex: 0 },
|
|
103
|
-
{ partyId: 'bob', publicKeyDigest: PKD_BOB, addressIndex: 1 },
|
|
104
|
-
],
|
|
105
|
-
balances: { alice: 600n, bob: 400n },
|
|
106
|
-
pendingHTLCs: [],
|
|
107
|
-
currentSequence: 5,
|
|
108
|
-
latestState: null,
|
|
109
|
-
stateLog: [],
|
|
110
|
-
status: 'active',
|
|
111
|
-
channelType: 'direct',
|
|
112
|
-
createdAt: 1_000_000,
|
|
113
|
-
updatedAt: 1_000_000,
|
|
114
|
-
...overrides,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async function quiesce(ch: OmniaChannel): Promise<QuiescedChannel> {
|
|
119
|
-
return quiesceChannel(ch, makeLeaseProvider(PKD_ALICE));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
123
|
-
// quiesceChannel
|
|
124
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
125
|
-
|
|
126
|
-
describe('quiesceChannel', () => {
|
|
127
|
-
beforeEach(() => { _resetChannelWatermarks(); });
|
|
128
|
-
|
|
129
|
-
it('returns a quiesced channel when no HTLCs are pending', async () => {
|
|
130
|
-
const q = await quiesce(makeChannel());
|
|
131
|
-
expect(q.status).toBe('quiesced');
|
|
132
|
-
expect(q.pendingHTLCs).toHaveLength(0);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it('populates quiesceSignedState with local party partial signature', async () => {
|
|
136
|
-
const ch = makeChannel();
|
|
137
|
-
const q = await quiesce(ch);
|
|
138
|
-
expect(q.quiesceSignedState).toBeDefined();
|
|
139
|
-
expect(q.quiesceSignedState.sequence).toBe(ch.currentSequence + 1);
|
|
140
|
-
expect(q.quiesceSignedState.balances).toEqual(ch.balances);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it('clears all resolved HTLCs', async () => {
|
|
144
|
-
const ch = makeChannel({
|
|
145
|
-
pendingHTLCs: [
|
|
146
|
-
{
|
|
147
|
-
htlcId: '0x01', amount: 100n, hashlock: '0x' + 'aa'.repeat(32),
|
|
148
|
-
timeoutBlock: 1000n, direction: 'offered', status: 'fulfilled',
|
|
149
|
-
htlcAddress: '0x' + 'ab'.repeat(32),
|
|
150
|
-
senderPublicKeyDigest: PKD_ALICE, recipientPublicKeyDigest: PKD_BOB,
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
htlcId: '0x02', amount: 50n, hashlock: '0x' + 'bb'.repeat(32),
|
|
154
|
-
timeoutBlock: 1000n, direction: 'received', status: 'timed_out',
|
|
155
|
-
htlcAddress: '0x' + 'bc'.repeat(32),
|
|
156
|
-
senderPublicKeyDigest: PKD_BOB, recipientPublicKeyDigest: PKD_ALICE,
|
|
157
|
-
},
|
|
158
|
-
],
|
|
159
|
-
});
|
|
160
|
-
const q = await quiesce(ch);
|
|
161
|
-
expect(q.status).toBe('quiesced');
|
|
162
|
-
expect(q.pendingHTLCs).toHaveLength(0);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it('throws PendingHTLCError if any HTLC is still pending', async () => {
|
|
166
|
-
const ch = makeChannel({
|
|
167
|
-
pendingHTLCs: [{
|
|
168
|
-
htlcId: '0x01', amount: 100n, hashlock: '0x' + 'aa'.repeat(32),
|
|
169
|
-
timeoutBlock: 1000n, direction: 'offered', status: 'pending',
|
|
170
|
-
htlcAddress: '0x' + 'ab'.repeat(32),
|
|
171
|
-
senderPublicKeyDigest: PKD_ALICE, recipientPublicKeyDigest: PKD_BOB,
|
|
172
|
-
}],
|
|
173
|
-
});
|
|
174
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
175
|
-
await expect(quiesceChannel(ch, lp)).rejects.toThrow(PendingHTLCError);
|
|
176
|
-
await expect(quiesceChannel(ch, lp)).rejects.toThrow('1 HTLC(s) still pending');
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it('throws SpliceChannelStatusError if channel is not active', async () => {
|
|
180
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
181
|
-
await expect(quiesceChannel(makeChannel({ status: 'closing_mutual' }), lp))
|
|
182
|
-
.rejects.toThrow(SpliceChannelStatusError);
|
|
183
|
-
await expect(quiesceChannel(makeChannel({ status: 'closing_mutual' }), lp))
|
|
184
|
-
.rejects.toThrow("'active'");
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('throws SpliceChannelStatusError if channel is closed', async () => {
|
|
188
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
189
|
-
await expect(quiesceChannel(makeChannel({ status: 'closed' }), lp))
|
|
190
|
-
.rejects.toThrow(SpliceChannelStatusError);
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it('awaitResolution: calls callback with pending HTLCs and succeeds after resolution', async () => {
|
|
194
|
-
const htlc = {
|
|
195
|
-
htlcId: '0x01', amount: 100n, hashlock: '0x' + 'aa'.repeat(32),
|
|
196
|
-
timeoutBlock: 1000n, direction: 'offered' as const, status: 'pending' as const,
|
|
197
|
-
htlcAddress: '0x' + 'ab'.repeat(32),
|
|
198
|
-
senderPublicKeyDigest: PKD_ALICE, recipientPublicKeyDigest: PKD_BOB,
|
|
199
|
-
};
|
|
200
|
-
const ch = makeChannel({ pendingHTLCs: [htlc] });
|
|
201
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
202
|
-
const resolveFn = jest.fn(async (pending: OmniaChannel['pendingHTLCs']) => {
|
|
203
|
-
expect(pending).toHaveLength(1);
|
|
204
|
-
expect(pending[0].htlcId).toBe('0x01');
|
|
205
|
-
ch.pendingHTLCs[0] = { ...ch.pendingHTLCs[0], status: 'fulfilled' };
|
|
206
|
-
});
|
|
207
|
-
const q = await quiesceChannel(ch, lp, { awaitResolution: resolveFn } satisfies QuiesceOptions);
|
|
208
|
-
expect(resolveFn).toHaveBeenCalledTimes(1);
|
|
209
|
-
expect(q.status).toBe('quiesced');
|
|
210
|
-
expect(q.pendingHTLCs).toHaveLength(0);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it('awaitResolution: still throws PendingHTLCError if HTLCs unresolved after callback', async () => {
|
|
214
|
-
const htlc = {
|
|
215
|
-
htlcId: '0x01', amount: 100n, hashlock: '0x' + 'aa'.repeat(32),
|
|
216
|
-
timeoutBlock: 1000n, direction: 'offered' as const, status: 'pending' as const,
|
|
217
|
-
htlcAddress: '0x' + 'ab'.repeat(32),
|
|
218
|
-
senderPublicKeyDigest: PKD_ALICE, recipientPublicKeyDigest: PKD_BOB,
|
|
219
|
-
};
|
|
220
|
-
const ch = makeChannel({ pendingHTLCs: [htlc] });
|
|
221
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
222
|
-
await expect(
|
|
223
|
-
quiesceChannel(ch, lp, { awaitResolution: async () => {} }),
|
|
224
|
-
).rejects.toThrow(PendingHTLCError);
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
229
|
-
// buildSpliceTx
|
|
230
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
231
|
-
|
|
232
|
-
describe('buildSpliceTx', () => {
|
|
233
|
-
it('splice-in: 2 inputs, channel uses COINID_ELTOO floating input; additional uses specific coinId', () => {
|
|
234
|
-
const ch = makeChannel();
|
|
235
|
-
const draft = buildSpliceTx(ch, {
|
|
236
|
-
type: 'splice_in', newTotalValue: 1500n,
|
|
237
|
-
newBalances: { alice: 900n, bob: 600n },
|
|
238
|
-
additionalCoinId: NEW_COIN_ID, additionalAmount: 500n,
|
|
239
|
-
});
|
|
240
|
-
expect(draft.inputs).toHaveLength(2);
|
|
241
|
-
expect(draft.inputs[0].coinId).toBe(COINID_ELTOO);
|
|
242
|
-
expect(draft.inputs[0].amount).toBe(1000n);
|
|
243
|
-
expect(draft.inputs[1].coinId).toBe(NEW_COIN_ID);
|
|
244
|
-
expect(draft.inputs[1].amount).toBe(500n);
|
|
245
|
-
expect(draft.outputs).toHaveLength(1);
|
|
246
|
-
expect(draft.outputs[0].amount).toBe(1500n);
|
|
247
|
-
expect(draft.outputs[0].storeState).toBe(true);
|
|
248
|
-
expect(draft.outputs[0].stateVarSettlement).toBe(false);
|
|
249
|
-
expect(draft.outputs[0].stateVarSequence).toBe(0);
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it('channel input always uses COINID_ELTOO regardless of latestCoinId', () => {
|
|
253
|
-
const latest = '0x' + '99'.repeat(32);
|
|
254
|
-
const draft = buildSpliceTx(
|
|
255
|
-
{ ...makeChannel(), latestCoinId: latest } as OmniaChannel,
|
|
256
|
-
{ type: 'splice_in', newTotalValue: 1200n, newBalances: { alice: 700n, bob: 500n }, additionalCoinId: NEW_COIN_ID, additionalAmount: 200n },
|
|
257
|
-
);
|
|
258
|
-
expect(draft.inputs[0].coinId).toBe(COINID_ELTOO);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it('splice-out: 1 input, channel-coin + withdrawal outputs', () => {
|
|
262
|
-
const draft = buildSpliceTx(makeChannel(), {
|
|
263
|
-
type: 'splice_out', newTotalValue: 800n, newBalances: { alice: 480n, bob: 320n },
|
|
264
|
-
withdrawAmount: 200n, withdrawAddress: EXTRA_ADDRESS,
|
|
265
|
-
});
|
|
266
|
-
expect(draft.inputs).toHaveLength(1);
|
|
267
|
-
expect(draft.outputs).toHaveLength(2);
|
|
268
|
-
expect(draft.outputs[0].amount).toBe(800n);
|
|
269
|
-
expect(draft.outputs[0].storeState).toBe(true);
|
|
270
|
-
expect(draft.outputs[0].stateVarSequence).toBe(0);
|
|
271
|
-
expect(draft.outputs[1].address).toBe(EXTRA_ADDRESS);
|
|
272
|
-
expect(draft.outputs[1].amount).toBe(200n);
|
|
273
|
-
expect(draft.outputs[1].storeState).toBe(false);
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
it('splice-out with extra outputs', () => {
|
|
277
|
-
const draft = buildSpliceTx(makeChannel(), {
|
|
278
|
-
type: 'splice_out', newTotalValue: 750n, newBalances: { alice: 450n, bob: 300n },
|
|
279
|
-
withdrawAmount: 200n, withdrawAddress: EXTRA_ADDRESS,
|
|
280
|
-
extraOutputs: [{ address: THIRD_PARTY_ADDR, amount: 50n }],
|
|
281
|
-
});
|
|
282
|
-
expect(draft.outputs).toHaveLength(3);
|
|
283
|
-
expect(draft.outputs[2].address).toBe(THIRD_PARTY_ADDR);
|
|
284
|
-
expect(draft.outputs[2].amount).toBe(50n);
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
it('throws SpliceBalanceConservationError when newBalances do not sum to newTotalValue', () => {
|
|
288
|
-
expect(() => buildSpliceTx(makeChannel(), {
|
|
289
|
-
type: 'splice_in', newTotalValue: 1500n, newBalances: { alice: 900n, bob: 400n },
|
|
290
|
-
additionalCoinId: NEW_COIN_ID, additionalAmount: 500n,
|
|
291
|
-
})).toThrow(SpliceBalanceConservationError);
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
it('throws SpliceInsufficientFundsError when splice-out exceeds channel total', () => {
|
|
295
|
-
expect(() => buildSpliceTx(makeChannel(), {
|
|
296
|
-
type: 'splice_out', newTotalValue: 0n, newBalances: {},
|
|
297
|
-
withdrawAmount: 2000n, withdrawAddress: EXTRA_ADDRESS,
|
|
298
|
-
})).toThrow(SpliceInsufficientFundsError);
|
|
299
|
-
});
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
303
|
-
// computeSpliceTxDigest + spliceDraftToMinimaBytes
|
|
304
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
305
|
-
|
|
306
|
-
describe('computeSpliceTxDigest', () => {
|
|
307
|
-
it('returns a 32-byte deterministic digest', () => {
|
|
308
|
-
const ch = makeChannel();
|
|
309
|
-
const draft = buildSpliceTx(ch, {
|
|
310
|
-
type: 'splice_in', newTotalValue: 1500n, newBalances: { alice: 900n, bob: 600n },
|
|
311
|
-
additionalCoinId: NEW_COIN_ID, additionalAmount: 500n,
|
|
312
|
-
});
|
|
313
|
-
const d1 = computeSpliceTxDigest(draft);
|
|
314
|
-
const d2 = computeSpliceTxDigest(draft);
|
|
315
|
-
expect(d1).toHaveLength(32);
|
|
316
|
-
expect(d1).toEqual(d2);
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
it('different newBalances produce different digests even with same sum', () => {
|
|
320
|
-
const ch = makeChannel();
|
|
321
|
-
const d1 = computeSpliceTxDigest(buildSpliceTx(ch, {
|
|
322
|
-
type: 'splice_in', newTotalValue: 1500n, newBalances: { alice: 900n, bob: 600n },
|
|
323
|
-
additionalCoinId: NEW_COIN_ID, additionalAmount: 500n,
|
|
324
|
-
}));
|
|
325
|
-
const d2 = computeSpliceTxDigest(buildSpliceTx(ch, {
|
|
326
|
-
type: 'splice_in', newTotalValue: 1500n, newBalances: { alice: 800n, bob: 700n },
|
|
327
|
-
additionalCoinId: NEW_COIN_ID, additionalAmount: 500n,
|
|
328
|
-
}));
|
|
329
|
-
expect(d1).not.toEqual(d2);
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
it('spliceDraftToMinimaBytes returns non-empty Uint8Array', () => {
|
|
333
|
-
const bytes = spliceDraftToMinimaBytes(buildSpliceTx(makeChannel(), {
|
|
334
|
-
type: 'splice_out', newTotalValue: 800n, newBalances: { alice: 480n, bob: 320n },
|
|
335
|
-
withdrawAmount: 200n, withdrawAddress: EXTRA_ADDRESS,
|
|
336
|
-
}));
|
|
337
|
-
expect(bytes.length).toBeGreaterThan(0);
|
|
338
|
-
expect(bytes).toBeInstanceOf(Uint8Array);
|
|
339
|
-
});
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
343
|
-
// proposeSpliceIn
|
|
344
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
345
|
-
|
|
346
|
-
describe('proposeSpliceIn', () => {
|
|
347
|
-
beforeEach(() => { _resetChannelWatermarks(); });
|
|
348
|
-
|
|
349
|
-
it('returns SpliceProposal with spliceTxHex, lease reservation data, and proposer signature', async () => {
|
|
350
|
-
const q = await quiesce(makeChannel());
|
|
351
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
352
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, 500n, lp, { alice: 900n, bob: 600n });
|
|
353
|
-
expect(proposal.proposerPublicKeyDigest).toBe(PKD_ALICE);
|
|
354
|
-
expect(proposal.proposerSignature).toBeInstanceOf(Uint8Array);
|
|
355
|
-
expect(proposal.proposerSignature.length).toBe(32);
|
|
356
|
-
expect(proposal.params.type).toBe('splice_in');
|
|
357
|
-
expect(proposal.params.newTotalValue).toBe(1500n);
|
|
358
|
-
expect(proposal.spliceTxHex).toMatch(/^[0-9a-f]+$/);
|
|
359
|
-
expect(proposal.spliceTxDraft.inputs).toHaveLength(2);
|
|
360
|
-
expect(proposal.proposerReservationId).toBeTruthy();
|
|
361
|
-
expect(proposal.proposerSigningIndices).toBeDefined();
|
|
362
|
-
expect(proposal.proposerSigningIndices.l1).toBeGreaterThan(0);
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
it('reserves a WOTS key slot via wotsLease.reserveKeyUse', async () => {
|
|
366
|
-
const q = await quiesce(makeChannel());
|
|
367
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
368
|
-
await proposeSpliceIn(q, NEW_COIN_ID, 500n, lp, { alice: 900n, bob: 600n });
|
|
369
|
-
expect(lp.wotsLease.reserveKeyUse).toHaveBeenCalledWith(
|
|
370
|
-
expect.objectContaining({
|
|
371
|
-
treeId: PKD_ALICE,
|
|
372
|
-
purpose: 'omnia-splice-propose',
|
|
373
|
-
}),
|
|
374
|
-
);
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
it('signs with the reserved indices (not hardcoded zeros)', async () => {
|
|
378
|
-
const q = await quiesce(makeChannel());
|
|
379
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
380
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, 500n, lp, { alice: 900n, bob: 600n });
|
|
381
|
-
expect(proposal.proposerSigningIndices.l1).not.toBe(0);
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
it('either party can be the proposer', async () => {
|
|
385
|
-
const q = await quiesce(makeChannel());
|
|
386
|
-
const lp = makeLeaseProvider(PKD_BOB);
|
|
387
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, 500n, lp, { alice: 900n, bob: 600n });
|
|
388
|
-
expect(proposal.proposerPublicKeyDigest).toBe(PKD_BOB);
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
it('omitting newBalances scales balances proportionally to new total', async () => {
|
|
392
|
-
const q = await quiesce(makeChannel());
|
|
393
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, 500n, makeLeaseProvider(PKD_ALICE));
|
|
394
|
-
const newTotal = 1500n;
|
|
395
|
-
const balanceSum = Object.values(proposal.params.newBalances).reduce((a, b) => a + b, 0n);
|
|
396
|
-
expect(balanceSum).toBe(newTotal);
|
|
397
|
-
expect(proposal.params.newTotalValue).toBe(newTotal);
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
it('throws SpliceChannelStatusError if channel is not quiesced', async () => {
|
|
401
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
402
|
-
await expect(
|
|
403
|
-
proposeSpliceIn(makeChannel(), NEW_COIN_ID, 500n, lp, { alice: 900n, bob: 600n }),
|
|
404
|
-
).rejects.toThrow(SpliceChannelStatusError);
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
it('throws SpliceMissingPartyError if signer is not in channel', async () => {
|
|
408
|
-
const q = await quiesce(makeChannel());
|
|
409
|
-
const stranger = makeLeaseProvider('0x' + '55'.repeat(32));
|
|
410
|
-
await expect(
|
|
411
|
-
proposeSpliceIn(q, NEW_COIN_ID, 500n, stranger, { alice: 900n, bob: 600n }),
|
|
412
|
-
).rejects.toThrow(SpliceMissingPartyError);
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
it('throws PendingHTLCError (defense-in-depth) on quiesced channel with pending HTLC', async () => {
|
|
416
|
-
const fakeQ: QuiescedChannel = {
|
|
417
|
-
...makeChannel({
|
|
418
|
-
pendingHTLCs: [{
|
|
419
|
-
htlcId: '0x01', amount: 100n, hashlock: '0x' + 'aa'.repeat(32),
|
|
420
|
-
timeoutBlock: 1000n, direction: 'offered', status: 'pending',
|
|
421
|
-
htlcAddress: '0x' + 'ab'.repeat(32),
|
|
422
|
-
senderPublicKeyDigest: PKD_ALICE, recipientPublicKeyDigest: PKD_BOB,
|
|
423
|
-
}],
|
|
424
|
-
}),
|
|
425
|
-
status: 'quiesced',
|
|
426
|
-
quiesceSignedState: {},
|
|
427
|
-
};
|
|
428
|
-
await expect(
|
|
429
|
-
proposeSpliceIn(fakeQ, NEW_COIN_ID, 500n, makeLeaseProvider(PKD_ALICE), { alice: 900n, bob: 600n }),
|
|
430
|
-
).rejects.toThrow(PendingHTLCError);
|
|
431
|
-
});
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
435
|
-
// proposeSpliceOut
|
|
436
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
437
|
-
|
|
438
|
-
describe('proposeSpliceOut', () => {
|
|
439
|
-
beforeEach(() => { _resetChannelWatermarks(); });
|
|
440
|
-
|
|
441
|
-
it('returns SpliceProposal for splice-out with lease reservation data', async () => {
|
|
442
|
-
const q = await quiesce(makeChannel());
|
|
443
|
-
const lp = makeLeaseProvider(PKD_ALICE);
|
|
444
|
-
const proposal = await proposeSpliceOut(q, 200n, EXTRA_ADDRESS, lp, { alice: 480n, bob: 320n });
|
|
445
|
-
expect(proposal.params.type).toBe('splice_out');
|
|
446
|
-
expect(proposal.params.newTotalValue).toBe(800n);
|
|
447
|
-
expect(proposal.spliceTxHex).toMatch(/^[0-9a-f]+$/);
|
|
448
|
-
expect(proposal.proposerReservationId).toBeTruthy();
|
|
449
|
-
expect(proposal.proposerSigningIndices).toBeDefined();
|
|
450
|
-
});
|
|
451
|
-
|
|
452
|
-
it('combined splice-out + third-party payment', async () => {
|
|
453
|
-
const q = await quiesce(makeChannel());
|
|
454
|
-
const proposal = await proposeSpliceOut(
|
|
455
|
-
q, 200n, EXTRA_ADDRESS, makeLeaseProvider(PKD_ALICE),
|
|
456
|
-
{ alice: 450n, bob: 300n }, [{ address: THIRD_PARTY_ADDR, amount: 50n }],
|
|
457
|
-
);
|
|
458
|
-
expect(proposal.params.newTotalValue).toBe(750n);
|
|
459
|
-
expect(proposal.spliceTxDraft.outputs).toHaveLength(3);
|
|
460
|
-
});
|
|
461
|
-
|
|
462
|
-
it('throws SpliceChannelStatusError if channel is not quiesced', async () => {
|
|
463
|
-
await expect(
|
|
464
|
-
proposeSpliceOut(makeChannel(), 200n, EXTRA_ADDRESS, makeLeaseProvider(PKD_ALICE), { alice: 480n, bob: 320n }),
|
|
465
|
-
).rejects.toThrow(SpliceChannelStatusError);
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
it('throws SpliceChannelStatusError if channel is already spliced', async () => {
|
|
469
|
-
const ch = makeChannel({ status: 'spliced' });
|
|
470
|
-
await expect(
|
|
471
|
-
proposeSpliceOut(ch, 200n, EXTRA_ADDRESS, makeLeaseProvider(PKD_ALICE), { alice: 480n, bob: 320n }),
|
|
472
|
-
).rejects.toThrow(SpliceChannelStatusError);
|
|
473
|
-
});
|
|
474
|
-
});
|
|
475
|
-
|
|
476
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
477
|
-
// acceptSplice
|
|
478
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
479
|
-
|
|
480
|
-
describe('acceptSplice', () => {
|
|
481
|
-
beforeEach(() => { _resetChannelWatermarks(); });
|
|
482
|
-
|
|
483
|
-
async function makeProposal() {
|
|
484
|
-
const ch = makeChannel();
|
|
485
|
-
const q = await quiesce(ch);
|
|
486
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, 500n, makeLeaseProvider(PKD_ALICE), { alice: 900n, bob: 600n });
|
|
487
|
-
return { ch, q, proposal };
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
it('returns SpliceAcceptance with co-signature and lease reservation data', async () => {
|
|
491
|
-
const { q, proposal } = await makeProposal();
|
|
492
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
493
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
494
|
-
expect(acceptance.spliceId).toBe(proposal.spliceId);
|
|
495
|
-
expect(acceptance.channelId).toBe(q.channelId);
|
|
496
|
-
expect(acceptance.acceptorPublicKeyDigest).toBe(PKD_BOB);
|
|
497
|
-
expect(acceptance.acceptorSignature).toBeInstanceOf(Uint8Array);
|
|
498
|
-
expect(acceptance.acceptorReservationId).toBeTruthy();
|
|
499
|
-
expect(acceptance.acceptorSigningIndices).toBeDefined();
|
|
500
|
-
expect(acceptance.acceptorSigningIndices.l1).toBeGreaterThan(0);
|
|
501
|
-
});
|
|
502
|
-
|
|
503
|
-
it('reserves a WOTS key slot for acceptor via wotsLease.reserveKeyUse', async () => {
|
|
504
|
-
const { q, proposal } = await makeProposal();
|
|
505
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
506
|
-
await acceptSplice(q, proposal, bobLp);
|
|
507
|
-
expect(bobLp.wotsLease.reserveKeyUse).toHaveBeenCalledWith(
|
|
508
|
-
expect.objectContaining({
|
|
509
|
-
treeId: PKD_BOB,
|
|
510
|
-
purpose: 'omnia-splice-accept',
|
|
511
|
-
}),
|
|
512
|
-
);
|
|
513
|
-
});
|
|
514
|
-
|
|
515
|
-
it('acceptor signs with reserved indices (not hardcoded zeros)', async () => {
|
|
516
|
-
const { q, proposal } = await makeProposal();
|
|
517
|
-
const acceptance = await acceptSplice(q, proposal, makeLeaseProvider(PKD_BOB));
|
|
518
|
-
expect(acceptance.acceptorSigningIndices.l1).not.toBe(0);
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
it('throws SpliceChannelStatusError if channel is not quiesced', async () => {
|
|
522
|
-
const { ch, proposal } = await makeProposal();
|
|
523
|
-
await expect(acceptSplice(ch, proposal, makeLeaseProvider(PKD_BOB)))
|
|
524
|
-
.rejects.toThrow(SpliceChannelStatusError);
|
|
525
|
-
});
|
|
526
|
-
|
|
527
|
-
it('throws if acceptor is the same party as proposer', async () => {
|
|
528
|
-
const { q, proposal } = await makeProposal();
|
|
529
|
-
await expect(acceptSplice(q, proposal, makeLeaseProvider(PKD_ALICE)))
|
|
530
|
-
.rejects.toThrow('Acceptor and proposer cannot be the same party');
|
|
531
|
-
});
|
|
532
|
-
|
|
533
|
-
it('throws if channelId in proposal does not match channel', async () => {
|
|
534
|
-
const { proposal } = await makeProposal();
|
|
535
|
-
const fakeQ: QuiescedChannel = { ...makeChannel({ channelId: '0xdeadbeef' }), status: 'quiesced', quiesceSignedState: {} };
|
|
536
|
-
await expect(acceptSplice(fakeQ, proposal, makeLeaseProvider(PKD_BOB)))
|
|
537
|
-
.rejects.toThrow('does not match channel');
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
it('throws SpliceMissingPartyError if acceptor is not in the channel', async () => {
|
|
541
|
-
const { q, proposal } = await makeProposal();
|
|
542
|
-
await expect(acceptSplice(q, proposal, makeLeaseProvider('0x' + '77'.repeat(32))))
|
|
543
|
-
.rejects.toThrow(SpliceMissingPartyError);
|
|
544
|
-
});
|
|
545
|
-
|
|
546
|
-
it('acceptor signs the same digest as proposer', async () => {
|
|
547
|
-
const { q, proposal } = await makeProposal();
|
|
548
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
549
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
550
|
-
const digest = computeSpliceTxDigest(proposal.spliceTxDraft);
|
|
551
|
-
const expectedSig = sha3_256(
|
|
552
|
-
new Uint8Array([...Buffer.from(PKD_BOB.slice(2), 'hex'), ...digest]),
|
|
553
|
-
);
|
|
554
|
-
expect(acceptance.acceptorSignature).toEqual(expectedSig);
|
|
555
|
-
});
|
|
556
|
-
|
|
557
|
-
it('rejects proposal with tampered newBalances (conservation check)', async () => {
|
|
558
|
-
const { q, proposal } = await makeProposal();
|
|
559
|
-
const tampered = { ...proposal, params: { ...proposal.params, newBalances: { alice: 800n, bob: 600n } } };
|
|
560
|
-
await expect(acceptSplice(q, tampered, makeLeaseProvider(PKD_BOB)))
|
|
561
|
-
.rejects.toThrow('Proposal integrity check failed');
|
|
562
|
-
});
|
|
563
|
-
|
|
564
|
-
it('rejects proposal whose spliceTxDraft does not match params (tamper detection)', async () => {
|
|
565
|
-
const q = await quiesce(makeChannel());
|
|
566
|
-
const aliceLp = makeLeaseProvider(PKD_ALICE);
|
|
567
|
-
const p1 = await proposeSpliceIn(q, NEW_COIN_ID, 500n, aliceLp, { alice: 900n, bob: 600n });
|
|
568
|
-
const p2 = await proposeSpliceIn(q, NEW_COIN_ID, 600n, aliceLp, { alice: 960n, bob: 640n });
|
|
569
|
-
const tampered = { ...p1, spliceTxDraft: p2.spliceTxDraft };
|
|
570
|
-
await expect(acceptSplice(q, tampered, makeLeaseProvider(PKD_BOB)))
|
|
571
|
-
.rejects.toThrow('inconsistent');
|
|
572
|
-
});
|
|
573
|
-
});
|
|
574
|
-
|
|
575
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
576
|
-
// finalizeSplice
|
|
577
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
578
|
-
|
|
579
|
-
describe('finalizeSplice', () => {
|
|
580
|
-
beforeEach(() => { _resetChannelWatermarks(); });
|
|
581
|
-
|
|
582
|
-
async function fullSpliceIn(additionalAmount = 500n) {
|
|
583
|
-
const ch = makeChannel();
|
|
584
|
-
const q = await quiesce(ch);
|
|
585
|
-
const aliceLp = makeLeaseProvider(PKD_ALICE);
|
|
586
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
587
|
-
const newBalances = {
|
|
588
|
-
alice: 600n + additionalAmount * 6n / 10n,
|
|
589
|
-
bob: 400n + additionalAmount * 4n / 10n,
|
|
590
|
-
};
|
|
591
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, additionalAmount, aliceLp, newBalances);
|
|
592
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
593
|
-
const MINE_OPTS = {
|
|
594
|
-
...TEST_MINE_OPTS,
|
|
595
|
-
proposerLeaseProvider: aliceLp.wotsLease,
|
|
596
|
-
acceptorLeaseProvider: bobLp.wotsLease,
|
|
597
|
-
};
|
|
598
|
-
return { ch, q, proposal, acceptance, aliceLp, bobLp, MINE_OPTS };
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
async function fullSpliceOut(withdrawAmount = 200n) {
|
|
602
|
-
const ch = makeChannel();
|
|
603
|
-
const q = await quiesce(ch);
|
|
604
|
-
const aliceLp = makeLeaseProvider(PKD_ALICE);
|
|
605
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
606
|
-
const remaining = 1000n - withdrawAmount;
|
|
607
|
-
const newBalances = { alice: remaining * 6n / 10n, bob: remaining - (remaining * 6n / 10n) };
|
|
608
|
-
const proposal = await proposeSpliceOut(q, withdrawAmount, EXTRA_ADDRESS, aliceLp, newBalances);
|
|
609
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
610
|
-
const MINE_OPTS = {
|
|
611
|
-
...TEST_MINE_OPTS,
|
|
612
|
-
proposerLeaseProvider: aliceLp.wotsLease,
|
|
613
|
-
acceptorLeaseProvider: bobLp.wotsLease,
|
|
614
|
-
};
|
|
615
|
-
return { ch, q, proposal, acceptance, aliceLp, bobLp, MINE_OPTS };
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
it('splice-in: SplicedChannel status active, correct totalValue, sequence 0', async () => {
|
|
619
|
-
const { ch, q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn(500n);
|
|
620
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, MINE_OPTS);
|
|
621
|
-
expect(spliced.status).toBe('active');
|
|
622
|
-
expect(spliced.totalValue).toBe(1500n);
|
|
623
|
-
expect(spliced.currentSequence).toBe(0);
|
|
624
|
-
expect(spliced.spliceType).toBe('splice_in');
|
|
625
|
-
expect(spliced.splicedFrom).toBe(ch.channelId);
|
|
626
|
-
});
|
|
627
|
-
|
|
628
|
-
it('splice-out: SplicedChannel status active and reduced totalValue', async () => {
|
|
629
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceOut(200n);
|
|
630
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, MINE_OPTS);
|
|
631
|
-
expect(spliced.status).toBe('active');
|
|
632
|
-
expect(spliced.totalValue).toBe(800n);
|
|
633
|
-
expect(spliced.currentSequence).toBe(0);
|
|
634
|
-
expect(spliced.spliceType).toBe('splice_out');
|
|
635
|
-
});
|
|
636
|
-
|
|
637
|
-
it('new channel has updated balances from proposal', async () => {
|
|
638
|
-
const newBalances = { alice: 900n, bob: 600n };
|
|
639
|
-
const ch = makeChannel();
|
|
640
|
-
const q = await quiesce(ch);
|
|
641
|
-
const aliceLp = makeLeaseProvider(PKD_ALICE);
|
|
642
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
643
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, 500n, aliceLp, newBalances);
|
|
644
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
645
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, {
|
|
646
|
-
...TEST_MINE_OPTS,
|
|
647
|
-
proposerLeaseProvider: aliceLp.wotsLease,
|
|
648
|
-
acceptorLeaseProvider: bobLp.wotsLease,
|
|
649
|
-
});
|
|
650
|
-
expect(spliced.balances).toEqual(newBalances);
|
|
651
|
-
});
|
|
652
|
-
|
|
653
|
-
it('spliceFundingCoinId ends in -0 and becomes new fundingCoinId', async () => {
|
|
654
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
655
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, MINE_OPTS);
|
|
656
|
-
expect(spliced.spliceFundingCoinId).toMatch(/-0$/);
|
|
657
|
-
expect(spliced.fundingCoinId).toBe(spliced.spliceFundingCoinId);
|
|
658
|
-
});
|
|
659
|
-
|
|
660
|
-
it('latestState is null and pendingHTLCs is empty on the new channel', async () => {
|
|
661
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
662
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, MINE_OPTS);
|
|
663
|
-
expect(spliced.latestState).toBeNull();
|
|
664
|
-
expect(spliced.pendingHTLCs).toHaveLength(0);
|
|
665
|
-
});
|
|
666
|
-
|
|
667
|
-
it('stateLog gains a new entry at sequence 0', async () => {
|
|
668
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
669
|
-
const prevLen = q.stateLog.length;
|
|
670
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, MINE_OPTS);
|
|
671
|
-
expect(spliced.stateLog.length).toBeGreaterThan(prevLen);
|
|
672
|
-
expect(spliced.stateLog[spliced.stateLog.length - 1].sequence).toBe(0);
|
|
673
|
-
});
|
|
674
|
-
|
|
675
|
-
it('marks the quiesced channel as status spliced and returns active new channel', async () => {
|
|
676
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
677
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, MINE_OPTS);
|
|
678
|
-
expect((q as unknown as OmniaChannel).status).toBe('spliced');
|
|
679
|
-
expect(spliced.status).toBe('active');
|
|
680
|
-
});
|
|
681
|
-
|
|
682
|
-
it('commits both WOTS lease reservations on success', async () => {
|
|
683
|
-
const { q, proposal, acceptance, aliceLp, bobLp, MINE_OPTS } = await fullSpliceIn();
|
|
684
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, MINE_OPTS);
|
|
685
|
-
expect(aliceLp.wotsLease.commitKeyUse).toHaveBeenCalledWith(
|
|
686
|
-
proposal.proposerReservationId, spliced.spliceFundingTxId,
|
|
687
|
-
);
|
|
688
|
-
expect(bobLp.wotsLease.commitKeyUse).toHaveBeenCalledWith(
|
|
689
|
-
acceptance.acceptorReservationId, spliced.spliceFundingTxId,
|
|
690
|
-
);
|
|
691
|
-
expect(aliceLp.wotsLease.burnReservation).not.toHaveBeenCalled();
|
|
692
|
-
expect(bobLp.wotsLease.burnReservation).not.toHaveBeenCalled();
|
|
693
|
-
});
|
|
694
|
-
|
|
695
|
-
it('burns both WOTS lease reservations when broadcast fails', async () => {
|
|
696
|
-
const { q, proposal, acceptance, aliceLp, bobLp, MINE_OPTS } = await fullSpliceIn();
|
|
697
|
-
const rejectBroadcast = jest.fn().mockResolvedValue({ success: false });
|
|
698
|
-
await expect(
|
|
699
|
-
finalizeSplice(q, proposal, acceptance, { ...MINE_OPTS, broadcast: rejectBroadcast }),
|
|
700
|
-
).rejects.toThrow('broadcast rejected');
|
|
701
|
-
expect(aliceLp.wotsLease.burnReservation).toHaveBeenCalledWith(
|
|
702
|
-
proposal.proposerReservationId, 'finalize-failed',
|
|
703
|
-
);
|
|
704
|
-
expect(bobLp.wotsLease.burnReservation).toHaveBeenCalledWith(
|
|
705
|
-
acceptance.acceptorReservationId, 'finalize-failed',
|
|
706
|
-
);
|
|
707
|
-
expect(aliceLp.wotsLease.commitKeyUse).not.toHaveBeenCalled();
|
|
708
|
-
expect(bobLp.wotsLease.commitKeyUse).not.toHaveBeenCalled();
|
|
709
|
-
});
|
|
710
|
-
|
|
711
|
-
it('calls broadcast when provided and uses returned txpowid', async () => {
|
|
712
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
713
|
-
const mockTxId = '0x' + '44'.repeat(32);
|
|
714
|
-
const broadcastFn = jest.fn().mockResolvedValue({ txpowid: mockTxId });
|
|
715
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, { ...MINE_OPTS, broadcast: broadcastFn });
|
|
716
|
-
expect(broadcastFn).toHaveBeenCalledTimes(1);
|
|
717
|
-
expect(spliced.spliceFundingTxId).toBe(mockTxId);
|
|
718
|
-
expect(spliced.spliceFundingCoinId).toBe(`${mockTxId}-0`);
|
|
719
|
-
});
|
|
720
|
-
|
|
721
|
-
it('throws SpliceChannelStatusError if channel is not quiesced', async () => {
|
|
722
|
-
const { ch, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
723
|
-
await expect(finalizeSplice(ch, proposal, acceptance, MINE_OPTS))
|
|
724
|
-
.rejects.toThrow(SpliceChannelStatusError);
|
|
725
|
-
});
|
|
726
|
-
|
|
727
|
-
it('throws SpliceSignatureMismatchError if proposer signature fails verification', async () => {
|
|
728
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
729
|
-
const wrongSig = sha3_256(new Uint8Array([0xff, 0xfe]));
|
|
730
|
-
await expect(finalizeSplice(q, { ...proposal, proposerSignature: wrongSig }, acceptance, MINE_OPTS))
|
|
731
|
-
.rejects.toThrow(SpliceSignatureMismatchError);
|
|
732
|
-
});
|
|
733
|
-
|
|
734
|
-
it('throws SpliceSignatureMismatchError if acceptor signature fails verification', async () => {
|
|
735
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
736
|
-
const wrongSig = sha3_256(new Uint8Array([0xfe, 0xfd]));
|
|
737
|
-
await expect(finalizeSplice(q, proposal, { ...acceptance, acceptorSignature: wrongSig }, MINE_OPTS))
|
|
738
|
-
.rejects.toThrow(SpliceSignatureMismatchError);
|
|
739
|
-
});
|
|
740
|
-
|
|
741
|
-
it('throws SpliceSignatureMismatchError if spliceIds differ', async () => {
|
|
742
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
743
|
-
await expect(finalizeSplice(q, proposal, { ...acceptance, spliceId: '0xdeadbeef' }, MINE_OPTS))
|
|
744
|
-
.rejects.toThrow(SpliceSignatureMismatchError);
|
|
745
|
-
});
|
|
746
|
-
|
|
747
|
-
it('throws if proposal channelId does not match channel', async () => {
|
|
748
|
-
const { proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
749
|
-
const otherQ: QuiescedChannel = { ...makeChannel({ channelId: '0xdeadbeef' }), status: 'quiesced', quiesceSignedState: {} };
|
|
750
|
-
await expect(finalizeSplice(otherQ, proposal, acceptance, MINE_OPTS))
|
|
751
|
-
.rejects.toThrow('does not match channel');
|
|
752
|
-
});
|
|
753
|
-
|
|
754
|
-
it('throws SpliceMissingPartyError if proposer is not in channel parties', async () => {
|
|
755
|
-
const { proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
756
|
-
const strangerQ: QuiescedChannel = {
|
|
757
|
-
...makeChannel({
|
|
758
|
-
channelId: proposal.channelId,
|
|
759
|
-
parties: [
|
|
760
|
-
{ partyId: 's1', publicKeyDigest: '0x' + '55'.repeat(32), addressIndex: 0 },
|
|
761
|
-
{ partyId: 's2', publicKeyDigest: '0x' + '66'.repeat(32), addressIndex: 1 },
|
|
762
|
-
],
|
|
763
|
-
}),
|
|
764
|
-
status: 'quiesced',
|
|
765
|
-
quiesceSignedState: {},
|
|
766
|
-
};
|
|
767
|
-
await expect(finalizeSplice(strangerQ, proposal, acceptance, MINE_OPTS))
|
|
768
|
-
.rejects.toThrow(SpliceMissingPartyError);
|
|
769
|
-
});
|
|
770
|
-
|
|
771
|
-
it('throws SpliceSignatureMismatchError if proposer signature is empty', async () => {
|
|
772
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
773
|
-
await expect(finalizeSplice(q, { ...proposal, proposerSignature: new Uint8Array(0) }, acceptance, MINE_OPTS))
|
|
774
|
-
.rejects.toThrow(SpliceSignatureMismatchError);
|
|
775
|
-
});
|
|
776
|
-
|
|
777
|
-
it('throws SpliceSignatureMismatchError if acceptor signature is empty', async () => {
|
|
778
|
-
const { q, proposal, acceptance, MINE_OPTS } = await fullSpliceIn();
|
|
779
|
-
await expect(finalizeSplice(q, proposal, { ...acceptance, acceptorSignature: new Uint8Array(0) }, MINE_OPTS))
|
|
780
|
-
.rejects.toThrow(SpliceSignatureMismatchError);
|
|
781
|
-
});
|
|
782
|
-
});
|
|
783
|
-
|
|
784
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
785
|
-
// Full lifecycle: quiesce → propose → accept → finalize
|
|
786
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
787
|
-
|
|
788
|
-
describe('Full splice lifecycle', () => {
|
|
789
|
-
beforeEach(() => { _resetChannelWatermarks(); });
|
|
790
|
-
|
|
791
|
-
it('splice-in: 1000 → 1500 Minima; quiesced channel marked spliced', async () => {
|
|
792
|
-
const ch = makeChannel();
|
|
793
|
-
const aliceLp = makeLeaseProvider(PKD_ALICE);
|
|
794
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
795
|
-
const q = await quiesceChannel(ch, aliceLp);
|
|
796
|
-
|
|
797
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, 500n, aliceLp, { alice: 900n, bob: 600n });
|
|
798
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
799
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, {
|
|
800
|
-
...TEST_MINE_OPTS,
|
|
801
|
-
proposerLeaseProvider: aliceLp.wotsLease,
|
|
802
|
-
acceptorLeaseProvider: bobLp.wotsLease,
|
|
803
|
-
});
|
|
804
|
-
|
|
805
|
-
expect(spliced.totalValue).toBe(1500n);
|
|
806
|
-
expect(spliced.balances).toEqual({ alice: 900n, bob: 600n });
|
|
807
|
-
expect(spliced.currentSequence).toBe(0);
|
|
808
|
-
expect(spliced.status).toBe('active');
|
|
809
|
-
expect(spliced.splicedFrom).toBe(ch.channelId);
|
|
810
|
-
expect((q as unknown as OmniaChannel).status).toBe('spliced');
|
|
811
|
-
expect(ch.status).toBe('active');
|
|
812
|
-
expect(aliceLp.wotsLease.commitKeyUse).toHaveBeenCalled();
|
|
813
|
-
expect(bobLp.wotsLease.commitKeyUse).toHaveBeenCalled();
|
|
814
|
-
});
|
|
815
|
-
|
|
816
|
-
it('splice-out: 1000 → 800 Minima (200 withdrawn)', async () => {
|
|
817
|
-
const ch = makeChannel();
|
|
818
|
-
const aliceLp = makeLeaseProvider(PKD_ALICE);
|
|
819
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
820
|
-
const q = await quiesceChannel(ch, aliceLp);
|
|
821
|
-
|
|
822
|
-
const proposal = await proposeSpliceOut(q, 200n, EXTRA_ADDRESS, aliceLp, { alice: 480n, bob: 320n });
|
|
823
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
824
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, {
|
|
825
|
-
...TEST_MINE_OPTS,
|
|
826
|
-
proposerLeaseProvider: aliceLp.wotsLease,
|
|
827
|
-
acceptorLeaseProvider: bobLp.wotsLease,
|
|
828
|
-
});
|
|
829
|
-
|
|
830
|
-
expect(spliced.totalValue).toBe(800n);
|
|
831
|
-
expect(spliced.status).toBe('active');
|
|
832
|
-
expect((q as unknown as OmniaChannel).status).toBe('spliced');
|
|
833
|
-
});
|
|
834
|
-
|
|
835
|
-
it('quiesce clears resolved HTLCs and provides a signed quiesce state', async () => {
|
|
836
|
-
const ch = makeChannel({
|
|
837
|
-
pendingHTLCs: [{
|
|
838
|
-
htlcId: '0x01', amount: 100n, hashlock: '0x' + 'aa'.repeat(32),
|
|
839
|
-
timeoutBlock: 1000n, direction: 'offered', status: 'fulfilled',
|
|
840
|
-
htlcAddress: '0x' + 'ab'.repeat(32),
|
|
841
|
-
senderPublicKeyDigest: PKD_ALICE, recipientPublicKeyDigest: PKD_BOB,
|
|
842
|
-
}],
|
|
843
|
-
});
|
|
844
|
-
const q = await quiesceChannel(ch, makeLeaseProvider(PKD_ALICE));
|
|
845
|
-
expect(q.status).toBe('quiesced');
|
|
846
|
-
expect(q.pendingHTLCs).toHaveLength(0);
|
|
847
|
-
expect(q.quiesceSignedState.sequence).toBe(ch.currentSequence + 1);
|
|
848
|
-
});
|
|
849
|
-
|
|
850
|
-
it('quiesce rejects channel with still-pending HTLC', async () => {
|
|
851
|
-
const ch = makeChannel({
|
|
852
|
-
pendingHTLCs: [{
|
|
853
|
-
htlcId: '0x01', amount: 100n, hashlock: '0x' + 'aa'.repeat(32),
|
|
854
|
-
timeoutBlock: 1000n, direction: 'offered', status: 'pending',
|
|
855
|
-
htlcAddress: '0x' + 'ab'.repeat(32),
|
|
856
|
-
senderPublicKeyDigest: PKD_ALICE, recipientPublicKeyDigest: PKD_BOB,
|
|
857
|
-
}],
|
|
858
|
-
});
|
|
859
|
-
await expect(quiesceChannel(ch, makeLeaseProvider(PKD_ALICE))).rejects.toThrow(PendingHTLCError);
|
|
860
|
-
});
|
|
861
|
-
|
|
862
|
-
it('combined splice-out + on-chain payment to third address', async () => {
|
|
863
|
-
const ch = makeChannel();
|
|
864
|
-
const aliceLp = makeLeaseProvider(PKD_ALICE);
|
|
865
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
866
|
-
const q = await quiesceChannel(ch, aliceLp);
|
|
867
|
-
|
|
868
|
-
const proposal = await proposeSpliceOut(
|
|
869
|
-
q, 200n, EXTRA_ADDRESS, aliceLp,
|
|
870
|
-
{ alice: 450n, bob: 300n },
|
|
871
|
-
[{ address: THIRD_PARTY_ADDR, amount: 50n }],
|
|
872
|
-
);
|
|
873
|
-
expect(proposal.params.newTotalValue).toBe(750n);
|
|
874
|
-
expect(proposal.spliceTxDraft.outputs).toHaveLength(3);
|
|
875
|
-
|
|
876
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
877
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, {
|
|
878
|
-
...TEST_MINE_OPTS,
|
|
879
|
-
proposerLeaseProvider: aliceLp.wotsLease,
|
|
880
|
-
acceptorLeaseProvider: bobLp.wotsLease,
|
|
881
|
-
});
|
|
882
|
-
expect(spliced.totalValue).toBe(750n);
|
|
883
|
-
expect(spliced.status).toBe('active');
|
|
884
|
-
});
|
|
885
|
-
|
|
886
|
-
it('new channel has fresh fundingCoinId distinct from old channel', async () => {
|
|
887
|
-
const ch = makeChannel();
|
|
888
|
-
const aliceLp = makeLeaseProvider(PKD_ALICE);
|
|
889
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
890
|
-
const q = await quiesceChannel(ch, aliceLp);
|
|
891
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, 500n, aliceLp, { alice: 900n, bob: 600n });
|
|
892
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
893
|
-
const spliced = await finalizeSplice(q, proposal, acceptance, {
|
|
894
|
-
...TEST_MINE_OPTS,
|
|
895
|
-
proposerLeaseProvider: aliceLp.wotsLease,
|
|
896
|
-
acceptorLeaseProvider: bobLp.wotsLease,
|
|
897
|
-
});
|
|
898
|
-
expect(spliced.fundingCoinId).not.toBe(ch.fundingCoinId);
|
|
899
|
-
expect(spliced.fundingCoinId).toBe(spliced.spliceFundingCoinId);
|
|
900
|
-
});
|
|
901
|
-
|
|
902
|
-
it('original active channel (ch) is never mutated by quiesce or finalize', async () => {
|
|
903
|
-
const ch = makeChannel();
|
|
904
|
-
const originalStatus = ch.status;
|
|
905
|
-
const originalCoinId = ch.fundingCoinId;
|
|
906
|
-
const aliceLp = makeLeaseProvider(PKD_ALICE);
|
|
907
|
-
const bobLp = makeLeaseProvider(PKD_BOB);
|
|
908
|
-
const q = await quiesceChannel(ch, aliceLp);
|
|
909
|
-
const proposal = await proposeSpliceIn(q, NEW_COIN_ID, 500n, aliceLp, { alice: 900n, bob: 600n });
|
|
910
|
-
const acceptance = await acceptSplice(q, proposal, bobLp);
|
|
911
|
-
await finalizeSplice(q, proposal, acceptance, {
|
|
912
|
-
...TEST_MINE_OPTS,
|
|
913
|
-
proposerLeaseProvider: aliceLp.wotsLease,
|
|
914
|
-
acceptorLeaseProvider: bobLp.wotsLease,
|
|
915
|
-
});
|
|
916
|
-
expect(ch.status).toBe(originalStatus);
|
|
917
|
-
expect(ch.fundingCoinId).toBe(originalCoinId);
|
|
918
|
-
});
|
|
919
|
-
});
|