@totemsdk/omnia-splice 0.3.4 → 0.4.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 +2 -0
- package/dist/cjs/coordination.js +2 -2
- package/dist/cjs/finalize.d.ts +19 -0
- package/dist/cjs/finalize.js +51 -16
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/splice-tx.js +11 -3
- package/dist/cjs/splice.d.ts +21 -4
- package/dist/cjs/splice.js +26 -27
- package/dist/cjs/successor.d.ts +56 -0
- package/dist/cjs/successor.js +100 -5
- package/dist/coordination.d.ts +2 -0
- package/dist/coordination.js +2 -2
- package/dist/finalize.d.ts +19 -0
- package/dist/finalize.js +52 -17
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/splice-tx.js +12 -4
- package/dist/splice.d.ts +21 -4
- package/dist/splice.js +26 -27
- package/dist/successor.d.ts +56 -0
- package/dist/successor.js +98 -5
- package/package.json +6 -6
|
@@ -45,6 +45,8 @@ export interface SpliceProposeParams {
|
|
|
45
45
|
type: 'in' | 'out';
|
|
46
46
|
additionalCoinId?: string;
|
|
47
47
|
additionalAmount?: bigint;
|
|
48
|
+
/** OMN-052: address of the coin being added; required for splice-in. */
|
|
49
|
+
additionalCoinAddress?: string;
|
|
48
50
|
withdrawAmount?: bigint;
|
|
49
51
|
withdrawAddress?: string;
|
|
50
52
|
}
|
package/dist/cjs/coordination.js
CHANGED
|
@@ -75,7 +75,7 @@ function createSpliceCoordination(deps) {
|
|
|
75
75
|
// acceptSplice binds the draft to our channel + validates params, then signs.
|
|
76
76
|
const local = await ensureQuiesced(channel, leaseProvider);
|
|
77
77
|
channels.set(local.channelId, local);
|
|
78
|
-
const acceptance = await (0, splice_js_1.acceptSplice)(local, proposal, leaseProvider);
|
|
78
|
+
const acceptance = await (0, splice_js_1.acceptSplice)(local, proposal, leaseProvider, { verifySignature: verify });
|
|
79
79
|
pendingAcceptances.set(proposal.spliceId, acceptance);
|
|
80
80
|
burnTimers.set(proposal.spliceId, setTimeout(() => burnPending(proposal.spliceId, 'splice-finalize-timeout'), ttlMs));
|
|
81
81
|
await ctx.send('SPLICE_ACCEPT', acceptance);
|
|
@@ -122,7 +122,7 @@ function createSpliceCoordination(deps) {
|
|
|
122
122
|
const local = await ensureQuiesced(channel, leaseProvider);
|
|
123
123
|
channels.set(local.channelId, local);
|
|
124
124
|
const proposal = params.type === 'in'
|
|
125
|
-
? await (0, splice_js_1.proposeSpliceIn)(local, params.additionalCoinId, params.additionalAmount, leaseProvider)
|
|
125
|
+
? await (0, splice_js_1.proposeSpliceIn)(local, params.additionalCoinId, params.additionalAmount, leaseProvider, undefined, params.additionalCoinAddress)
|
|
126
126
|
: await (0, splice_js_1.proposeSpliceOut)(local, params.withdrawAmount, params.withdrawAddress, leaseProvider);
|
|
127
127
|
await coordination.sendProposal({
|
|
128
128
|
peer,
|
package/dist/cjs/finalize.d.ts
CHANGED
|
@@ -51,12 +51,31 @@ export interface FinalizeSpliceOptions {
|
|
|
51
51
|
chainProvider?: SpliceChainObserver;
|
|
52
52
|
/** Confirmations required before activation. Default 1. */
|
|
53
53
|
confirmations?: number;
|
|
54
|
+
/**
|
|
55
|
+
* Require proof that the successor is an output of this splice transaction
|
|
56
|
+
* before the channel is reported active (OMN-074). Default true: without that
|
|
57
|
+
* binding, any coin with a matching address, token and amount would activate
|
|
58
|
+
* the channel. Set false only when provenance is established elsewhere.
|
|
59
|
+
*/
|
|
60
|
+
requireSuccessorLineage?: boolean;
|
|
54
61
|
/** Successor observation timeout. Default 30s. */
|
|
55
62
|
successorTimeoutMs?: number;
|
|
56
63
|
/** Observation poll interval. Default 1s. */
|
|
57
64
|
successorPollMs?: number;
|
|
58
65
|
/** Durable journal written before broadcast, cleared after confirmation. */
|
|
59
66
|
pendingSpliceStore?: PendingSpliceStore;
|
|
67
|
+
/**
|
|
68
|
+
* Persist the successor channel before the recovery journal is cleared
|
|
69
|
+
* (OMN-076). The host owns the channel store, so it supplies this hook;
|
|
70
|
+
* `finalizeSplice` calls it after the successor is constructed and before
|
|
71
|
+
* `pendingSpliceStore.clear`, so a crash in between leaves a recoverable
|
|
72
|
+
* journal entry rather than an on-chain successor with no local record.
|
|
73
|
+
*/
|
|
74
|
+
persistSuccessor?: (channel: SplicedChannel) => Promise<void> | void;
|
|
75
|
+
/** Serialized Minima CoinProof for every spending input (OMN-053). */
|
|
76
|
+
coinProofs?: Uint8Array[];
|
|
77
|
+
/** Serialized Minima ScriptProof for every spending input (OMN-053). */
|
|
78
|
+
scriptProofs?: Uint8Array[];
|
|
60
79
|
}
|
|
61
80
|
/**
|
|
62
81
|
* Finalize a splice by assembling both parties' signatures, mining PoW,
|
package/dist/cjs/finalize.js
CHANGED
|
@@ -10,18 +10,25 @@ const errors_js_1 = require("./errors.js");
|
|
|
10
10
|
function defaultWotsVerify(sig, digest, pkd) {
|
|
11
11
|
return (0, omnia_1.verifyChannelSignature)(sig, digest, pkd);
|
|
12
12
|
}
|
|
13
|
-
function assembleSpliceWitness(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
function assembleSpliceWitness(channel, proposal, acceptance, proofs) {
|
|
14
|
+
// OMN-053: use the shared canonical witness encoder (MiniNumber counts and
|
|
15
|
+
// serialized proof objects) instead of a bespoke 4-byte layout.
|
|
16
|
+
// Minima's MULTISIG verification reads witnesses in key order.
|
|
17
|
+
const byParty = new Map([
|
|
18
|
+
[proposal.proposerPublicKeyDigest, proposal.proposerSignature],
|
|
19
|
+
[acceptance.acceptorPublicKeyDigest, acceptance.acceptorSignature],
|
|
20
|
+
]);
|
|
21
|
+
const signatures = channel.parties
|
|
22
|
+
.map((party) => byParty.get(party.publicKeyDigest))
|
|
23
|
+
.filter((signature) => Boolean(signature));
|
|
24
|
+
if (signatures.length !== 2) {
|
|
25
|
+
throw new errors_js_1.SpliceSignatureMismatchError('splice witness requires both party signatures');
|
|
26
|
+
}
|
|
27
|
+
return (0, omnia_1.serializeOmniaWitness)({
|
|
28
|
+
signatures,
|
|
29
|
+
coinProofs: proofs.coinProofs ?? [],
|
|
30
|
+
scriptProofs: proofs.scriptProofs ?? [],
|
|
31
|
+
});
|
|
25
32
|
}
|
|
26
33
|
/**
|
|
27
34
|
* Structural authorization checks: both parties must be distinct channel
|
|
@@ -148,7 +155,14 @@ async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
148
155
|
// Track which reservations have been committed so we know which to burn on failure.
|
|
149
156
|
const committed = new Set();
|
|
150
157
|
const txBytes = (0, splice_tx_js_1.spliceDraftToMinimaBytes)(proposal.spliceTxDraft);
|
|
151
|
-
|
|
158
|
+
// OMN-053: a broadcast splice must carry proof material for its inputs.
|
|
159
|
+
if (options?.broadcast && (!options.coinProofs?.length || !options.scriptProofs?.length)) {
|
|
160
|
+
throw new Error('finalizeSplice requires coin and script proofs before broadcasting');
|
|
161
|
+
}
|
|
162
|
+
const witnessBytes = assembleSpliceWitness(channel, proposal, acceptance, {
|
|
163
|
+
...(options?.coinProofs ? { coinProofs: options.coinProofs } : {}),
|
|
164
|
+
...(options?.scriptProofs ? { scriptProofs: options.scriptProofs } : {}),
|
|
165
|
+
});
|
|
152
166
|
let spliceFundingTxId;
|
|
153
167
|
try {
|
|
154
168
|
const difficulty = options?.mineDifficulty ?? txpow_1.TX_POW_MIN_DIFFICULTY;
|
|
@@ -164,6 +178,14 @@ async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
164
178
|
txHex,
|
|
165
179
|
txpowId: spliceFundingTxId,
|
|
166
180
|
broadcastAt: Date.now(),
|
|
181
|
+
// OMN-075: name the successor this splice must produce. Recovery prefers
|
|
182
|
+
// this over the caller's global observation parameters, so two
|
|
183
|
+
// interrupted splices can never be reconciled against each other.
|
|
184
|
+
expected: {
|
|
185
|
+
address: channel.fundingAddress,
|
|
186
|
+
tokenId: channel.tokenId,
|
|
187
|
+
amount: proposal.params.newTotalValue.toString(),
|
|
188
|
+
},
|
|
167
189
|
};
|
|
168
190
|
await options.pendingSpliceStore?.save(record);
|
|
169
191
|
const result = await options.broadcast(txHex);
|
|
@@ -212,6 +234,11 @@ async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
212
234
|
confirmations: options.confirmations ?? 1,
|
|
213
235
|
timeoutMs: options.successorTimeoutMs ?? 30000,
|
|
214
236
|
pollIntervalMs: options.successorPollMs ?? 1000,
|
|
237
|
+
// OMN-074: the successor must be an output of this splice and the splice
|
|
238
|
+
// must consume the predecessor funding coin.
|
|
239
|
+
spliceTxId: spliceFundingTxId,
|
|
240
|
+
predecessorCoinId: channel.fundingCoinId,
|
|
241
|
+
requireLineage: options.requireSuccessorLineage ?? true,
|
|
215
242
|
});
|
|
216
243
|
if (observed) {
|
|
217
244
|
spliceFundingCoinId = observed.coinId;
|
|
@@ -221,9 +248,6 @@ async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
221
248
|
successorStatus = 'funding_pending';
|
|
222
249
|
}
|
|
223
250
|
}
|
|
224
|
-
if (successorStatus === 'active') {
|
|
225
|
-
await options?.pendingSpliceStore?.clear(proposal.spliceId);
|
|
226
|
-
}
|
|
227
251
|
const splicedChannel = {
|
|
228
252
|
...channel,
|
|
229
253
|
status: successorStatus,
|
|
@@ -250,8 +274,19 @@ async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
250
274
|
],
|
|
251
275
|
updatedAt: Date.now(),
|
|
252
276
|
};
|
|
277
|
+
// OMN-076: persist the successor, retire the predecessor, then complete the
|
|
278
|
+
// journal entry — in that order. Clearing the journal first left a crash
|
|
279
|
+
// window where the successor was on chain, the predecessor was still local,
|
|
280
|
+
// and no recovery record existed. With `persistSuccessor` supplied by the
|
|
281
|
+
// caller (the host owns the channel store), that window is closed.
|
|
282
|
+
if (options?.persistSuccessor) {
|
|
283
|
+
await options.persistSuccessor(splicedChannel);
|
|
284
|
+
}
|
|
253
285
|
// Mark the old (quiesced) channel as spliced — it is now invalidated.
|
|
254
286
|
// Any further operations on `channel` should be rejected by status checks.
|
|
255
287
|
channel.status = 'spliced';
|
|
288
|
+
if (successorStatus === 'active') {
|
|
289
|
+
await options?.pendingSpliceStore?.clear(proposal.spliceId);
|
|
290
|
+
}
|
|
256
291
|
return splicedChannel;
|
|
257
292
|
}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { buildSpliceTx, spliceDraftToMinimaBytes, computeSpliceTxDigest } from '
|
|
|
3
3
|
export { proposeSpliceIn, proposeSpliceOut, acceptSplice, buildSpliceProposal, buildSpliceInProposal, buildSpliceOutProposal, } from './splice.js';
|
|
4
4
|
export type { UnsignedSpliceProposal } from './splice.js';
|
|
5
5
|
export { finalizeSplice, type FinalizeSpliceOptions } from './finalize.js';
|
|
6
|
-
export { observeSpliceSuccessor, recoverInterruptedSplices, type SpliceChainObserver, type SpliceSuccessorOptions, type ObservedSuccessor, type PendingSpliceRecord, type PendingSpliceStore, type SpliceRecoveryResult, } from './successor.js';
|
|
6
|
+
export { observeSpliceSuccessor, recoverInterruptedSplices, SpliceSuccessorLineageError, type SpliceChainObserver, type SpliceSuccessorOptions, type ObservedSuccessor, type PendingSpliceRecord, type PendingSpliceStore, type SpliceRecoveryResult, } from './successor.js';
|
|
7
7
|
export type { SpliceType, SpliceParams, SpliceTxDraft, SpliceTxInput, SpliceTxOutput, SpliceProposal, SpliceAcceptance, QuiescedChannel, SplicedChannel, SpliceLeaseProvider, SpliceSigningIndices, WotsSignature, } from './types.js';
|
|
8
8
|
export { SpliceError, PendingHTLCError, SpliceChannelStatusError, SpliceBalanceConservationError, SpliceSignatureMismatchError, SpliceMissingPartyError, SpliceInsufficientFundsError, } from './errors.js';
|
|
9
9
|
export { createDurableSpliceStore, } from './durable-splice-store.js';
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createSpliceCoordination = exports.createDurableSpliceStore = exports.SpliceInsufficientFundsError = exports.SpliceMissingPartyError = exports.SpliceSignatureMismatchError = exports.SpliceBalanceConservationError = exports.SpliceChannelStatusError = exports.PendingHTLCError = exports.SpliceError = exports.recoverInterruptedSplices = exports.observeSpliceSuccessor = exports.finalizeSplice = exports.buildSpliceOutProposal = exports.buildSpliceInProposal = exports.buildSpliceProposal = exports.acceptSplice = exports.proposeSpliceOut = exports.proposeSpliceIn = exports.computeSpliceTxDigest = exports.spliceDraftToMinimaBytes = exports.buildSpliceTx = exports.quiesceChannel = void 0;
|
|
3
|
+
exports.createSpliceCoordination = exports.createDurableSpliceStore = exports.SpliceInsufficientFundsError = exports.SpliceMissingPartyError = exports.SpliceSignatureMismatchError = exports.SpliceBalanceConservationError = exports.SpliceChannelStatusError = exports.PendingHTLCError = exports.SpliceError = exports.SpliceSuccessorLineageError = exports.recoverInterruptedSplices = exports.observeSpliceSuccessor = exports.finalizeSplice = exports.buildSpliceOutProposal = exports.buildSpliceInProposal = exports.buildSpliceProposal = exports.acceptSplice = exports.proposeSpliceOut = exports.proposeSpliceIn = exports.computeSpliceTxDigest = exports.spliceDraftToMinimaBytes = exports.buildSpliceTx = exports.quiesceChannel = void 0;
|
|
4
4
|
var quiesce_js_1 = require("./quiesce.js");
|
|
5
5
|
Object.defineProperty(exports, "quiesceChannel", { enumerable: true, get: function () { return quiesce_js_1.quiesceChannel; } });
|
|
6
6
|
var splice_tx_js_1 = require("./splice-tx.js");
|
|
@@ -19,6 +19,7 @@ Object.defineProperty(exports, "finalizeSplice", { enumerable: true, get: functi
|
|
|
19
19
|
var successor_js_1 = require("./successor.js");
|
|
20
20
|
Object.defineProperty(exports, "observeSpliceSuccessor", { enumerable: true, get: function () { return successor_js_1.observeSpliceSuccessor; } });
|
|
21
21
|
Object.defineProperty(exports, "recoverInterruptedSplices", { enumerable: true, get: function () { return successor_js_1.recoverInterruptedSplices; } });
|
|
22
|
+
Object.defineProperty(exports, "SpliceSuccessorLineageError", { enumerable: true, get: function () { return successor_js_1.SpliceSuccessorLineageError; } });
|
|
22
23
|
var errors_js_1 = require("./errors.js");
|
|
23
24
|
Object.defineProperty(exports, "SpliceError", { enumerable: true, get: function () { return errors_js_1.SpliceError; } });
|
|
24
25
|
Object.defineProperty(exports, "PendingHTLCError", { enumerable: true, get: function () { return errors_js_1.PendingHTLCError; } });
|
package/dist/cjs/splice-tx.js
CHANGED
|
@@ -63,9 +63,14 @@ function buildSpliceTx(channel, params) {
|
|
|
63
63
|
},
|
|
64
64
|
];
|
|
65
65
|
if (params.type === 'splice_in' && params.additionalCoinId && params.additionalAmount) {
|
|
66
|
+
// OMN-052: an unspecified address serializes to a placeholder, so the
|
|
67
|
+
// transaction would not describe the coin being added. Fail closed.
|
|
68
|
+
if (!params.additionalCoinAddress) {
|
|
69
|
+
throw new Error('splice-in requires the additional coin address');
|
|
70
|
+
}
|
|
66
71
|
inputs.push({
|
|
67
72
|
coinId: params.additionalCoinId,
|
|
68
|
-
address: params.additionalCoinAddress
|
|
73
|
+
address: params.additionalCoinAddress,
|
|
69
74
|
amount: params.additionalAmount,
|
|
70
75
|
tokenId,
|
|
71
76
|
});
|
|
@@ -156,7 +161,10 @@ function spliceDraftToMinimaBytes(draft) {
|
|
|
156
161
|
};
|
|
157
162
|
const inputs = draft.inputs.map(inp => ({
|
|
158
163
|
coinid: inp.coinId,
|
|
159
|
-
|
|
164
|
+
// OMN-051: draft amounts are atoms; core's JSON serializer parses decimal
|
|
165
|
+
// MINIMA. An integer string here means scale-zero MiniNumber (10^44x too
|
|
166
|
+
// large), so convert at the boundary.
|
|
167
|
+
amount: (0, core_1.atomsToAmount)(inp.amount),
|
|
160
168
|
address: inp.address,
|
|
161
169
|
tokenid: inp.tokenId,
|
|
162
170
|
storestate: false,
|
|
@@ -166,7 +174,7 @@ function spliceDraftToMinimaBytes(draft) {
|
|
|
166
174
|
state: [],
|
|
167
175
|
}));
|
|
168
176
|
const outputs = draft.outputs.map(out => ({
|
|
169
|
-
amount: out.amount
|
|
177
|
+
amount: (0, core_1.atomsToAmount)(out.amount),
|
|
170
178
|
address: out.address,
|
|
171
179
|
tokenid: out.tokenId,
|
|
172
180
|
storestate: out.storeState ?? false,
|
package/dist/cjs/splice.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type OmniaChannel } from '@totemsdk/omnia';
|
|
2
2
|
import type { SpliceParams, SpliceProposal, SpliceAcceptance, SpliceLeaseProvider, QuiescedChannel } from './types.js';
|
|
3
3
|
/** A splice proposal whose proposer signature has not been applied yet. */
|
|
4
4
|
export interface UnsignedSpliceProposal {
|
|
@@ -17,7 +17,9 @@ export interface UnsignedSpliceProposal {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare function buildSpliceProposal(channel: OmniaChannel | QuiescedChannel, params: SpliceParams, proposerPublicKeyDigest: string): UnsignedSpliceProposal;
|
|
19
19
|
/** Build an unsigned splice-in proposal (see `buildSpliceProposal`). */
|
|
20
|
-
export declare function buildSpliceInProposal(channel: OmniaChannel | QuiescedChannel, additionalCoinId: string, additionalAmount: bigint, proposerPublicKeyDigest: string, newBalances?: Record<string, bigint
|
|
20
|
+
export declare function buildSpliceInProposal(channel: OmniaChannel | QuiescedChannel, additionalCoinId: string, additionalAmount: bigint, proposerPublicKeyDigest: string, newBalances?: Record<string, bigint>,
|
|
21
|
+
/** OMN-052: address of the coin being added; required by the builder. */
|
|
22
|
+
additionalCoinAddress?: string): UnsignedSpliceProposal;
|
|
21
23
|
/** Build an unsigned splice-out proposal (see `buildSpliceProposal`). */
|
|
22
24
|
export declare function buildSpliceOutProposal(channel: OmniaChannel | QuiescedChannel, withdrawAmount: bigint, withdrawAddress: string, proposerPublicKeyDigest: string, newBalances?: Record<string, bigint>, extraOutputs?: Array<{
|
|
23
25
|
address: string;
|
|
@@ -45,7 +47,12 @@ export declare function buildSpliceOutProposal(channel: OmniaChannel | QuiescedC
|
|
|
45
47
|
* `sum(newBalances) === newTotalValue` is preserved.
|
|
46
48
|
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
47
49
|
*/
|
|
48
|
-
export declare function proposeSpliceIn(channel: OmniaChannel | QuiescedChannel, additionalCoinId: string, additionalAmount: bigint, leaseProvider: SpliceLeaseProvider, newBalances?: Record<string, bigint
|
|
50
|
+
export declare function proposeSpliceIn(channel: OmniaChannel | QuiescedChannel, additionalCoinId: string, additionalAmount: bigint, leaseProvider: SpliceLeaseProvider, newBalances?: Record<string, bigint>,
|
|
51
|
+
/**
|
|
52
|
+
* OMN-052: the address of the coin being added. Required by the transaction
|
|
53
|
+
* builder because an empty address serializes to a placeholder.
|
|
54
|
+
*/
|
|
55
|
+
additionalCoinAddress?: string): Promise<SpliceProposal>;
|
|
49
56
|
/**
|
|
50
57
|
* Initiating party proposes a splice-out: withdraw funds from the channel on-chain.
|
|
51
58
|
*
|
|
@@ -92,4 +99,14 @@ export declare function proposeSpliceOut(channel: OmniaChannel | QuiescedChannel
|
|
|
92
99
|
* @param leaseProvider - Provides the acceptor signer and WOTS lease.
|
|
93
100
|
* @returns SpliceAcceptance containing acceptor co-signature and lease data.
|
|
94
101
|
*/
|
|
95
|
-
export
|
|
102
|
+
export interface AcceptSpliceOptions {
|
|
103
|
+
/** OMN-023: explicit spending authorization for an economic reduction. */
|
|
104
|
+
authorize?: (request: {
|
|
105
|
+
channel: OmniaChannel | QuiescedChannel;
|
|
106
|
+
proposal: SpliceProposal;
|
|
107
|
+
localPartyId: string;
|
|
108
|
+
}) => Promise<void> | void;
|
|
109
|
+
/** Signature verifier for the proposer (defaults to the WOTS verifier). */
|
|
110
|
+
verifySignature?: (signature: Uint8Array, digest: Uint8Array, publicKeyDigest: string) => boolean;
|
|
111
|
+
}
|
|
112
|
+
export declare function acceptSplice(channel: OmniaChannel | QuiescedChannel, proposal: SpliceProposal, leaseProvider: SpliceLeaseProvider, options?: AcceptSpliceOptions): Promise<SpliceAcceptance>;
|
package/dist/cjs/splice.js
CHANGED
|
@@ -6,6 +6,7 @@ exports.buildSpliceOutProposal = buildSpliceOutProposal;
|
|
|
6
6
|
exports.proposeSpliceIn = proposeSpliceIn;
|
|
7
7
|
exports.proposeSpliceOut = proposeSpliceOut;
|
|
8
8
|
exports.acceptSplice = acceptSplice;
|
|
9
|
+
const omnia_1 = require("@totemsdk/omnia");
|
|
9
10
|
const splice_tx_js_1 = require("./splice-tx.js");
|
|
10
11
|
const errors_js_1 = require("./errors.js");
|
|
11
12
|
/**
|
|
@@ -130,7 +131,9 @@ function buildSpliceProposal(channel, params, proposerPublicKeyDigest) {
|
|
|
130
131
|
};
|
|
131
132
|
}
|
|
132
133
|
/** Build an unsigned splice-in proposal (see `buildSpliceProposal`). */
|
|
133
|
-
function buildSpliceInProposal(channel, additionalCoinId, additionalAmount, proposerPublicKeyDigest, newBalances
|
|
134
|
+
function buildSpliceInProposal(channel, additionalCoinId, additionalAmount, proposerPublicKeyDigest, newBalances,
|
|
135
|
+
/** OMN-052: address of the coin being added; required by the builder. */
|
|
136
|
+
additionalCoinAddress) {
|
|
134
137
|
validateChannelQuiesced(channel);
|
|
135
138
|
const newTotal = channel.totalValue + additionalAmount;
|
|
136
139
|
const effectiveNewBalances = newBalances ?? scaleBalances(channel.balances, channel.totalValue, newTotal);
|
|
@@ -140,6 +143,7 @@ function buildSpliceInProposal(channel, additionalCoinId, additionalAmount, prop
|
|
|
140
143
|
newBalances: effectiveNewBalances,
|
|
141
144
|
additionalCoinId,
|
|
142
145
|
additionalAmount,
|
|
146
|
+
...(additionalCoinAddress ? { additionalCoinAddress } : {}),
|
|
143
147
|
}, proposerPublicKeyDigest);
|
|
144
148
|
}
|
|
145
149
|
/** Build an unsigned splice-out proposal (see `buildSpliceProposal`). */
|
|
@@ -199,7 +203,12 @@ function buildSpliceOutProposal(channel, withdrawAmount, withdrawAddress, propos
|
|
|
199
203
|
* `sum(newBalances) === newTotalValue` is preserved.
|
|
200
204
|
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
201
205
|
*/
|
|
202
|
-
async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leaseProvider, newBalances
|
|
206
|
+
async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leaseProvider, newBalances,
|
|
207
|
+
/**
|
|
208
|
+
* OMN-052: the address of the coin being added. Required by the transaction
|
|
209
|
+
* builder because an empty address serializes to a placeholder.
|
|
210
|
+
*/
|
|
211
|
+
additionalCoinAddress) {
|
|
203
212
|
validateChannelQuiesced(channel);
|
|
204
213
|
const newTotal = channel.totalValue + additionalAmount;
|
|
205
214
|
const effectiveNewBalances = newBalances ?? scaleBalances(channel.balances, channel.totalValue, newTotal);
|
|
@@ -209,6 +218,7 @@ async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leas
|
|
|
209
218
|
newBalances: effectiveNewBalances,
|
|
210
219
|
additionalCoinId,
|
|
211
220
|
additionalAmount,
|
|
221
|
+
...(additionalCoinAddress ? { additionalCoinAddress } : {}),
|
|
212
222
|
};
|
|
213
223
|
const draft = (0, splice_tx_js_1.buildSpliceTx)(channel, params);
|
|
214
224
|
const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(draft);
|
|
@@ -315,31 +325,7 @@ async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress, leaseP
|
|
|
315
325
|
proposedAt: Date.now(),
|
|
316
326
|
};
|
|
317
327
|
}
|
|
318
|
-
|
|
319
|
-
* Counterparty accepts a splice proposal by co-signing the splice TX digest.
|
|
320
|
-
*
|
|
321
|
-
* Requires the channel to be in 'quiesced' state.
|
|
322
|
-
*
|
|
323
|
-
* WOTS lease safety: reserves a key slot before signing. The returned
|
|
324
|
-
* `acceptorReservationId` and `acceptorSigningIndices` are embedded in the
|
|
325
|
-
* `SpliceAcceptance` and consumed by `finalizeSplice` to commit or burn the
|
|
326
|
-
* acceptor's key-slot reservation.
|
|
327
|
-
*
|
|
328
|
-
* The acceptor independently:
|
|
329
|
-
* 1. Validates party membership (proposer and acceptor both in channel).
|
|
330
|
-
* 2. Validates proposal balance conservation and amount constraints.
|
|
331
|
-
* 3. Cryptographically binds the signed draft to the proposal params by
|
|
332
|
-
* recomputing the expected draft from `params + channel` and comparing
|
|
333
|
-
* digests — rejects if they differ (tamper detection).
|
|
334
|
-
* 4. Reserves a WOTS key slot via `leaseProvider.wotsLease`.
|
|
335
|
-
* 5. Signs the splice TX digest with reserved indices.
|
|
336
|
-
*
|
|
337
|
-
* @param channel - Quiesced channel.
|
|
338
|
-
* @param proposal - Splice proposal from the initiating party.
|
|
339
|
-
* @param leaseProvider - Provides the acceptor signer and WOTS lease.
|
|
340
|
-
* @returns SpliceAcceptance containing acceptor co-signature and lease data.
|
|
341
|
-
*/
|
|
342
|
-
async function acceptSplice(channel, proposal, leaseProvider) {
|
|
328
|
+
async function acceptSplice(channel, proposal, leaseProvider, options = {}) {
|
|
343
329
|
validateChannelQuiesced(channel);
|
|
344
330
|
if (proposal.channelId !== channel.channelId) {
|
|
345
331
|
throw new Error(`Proposal channelId '${proposal.channelId}' does not match channel '${channel.channelId}'`);
|
|
@@ -359,6 +345,19 @@ async function acceptSplice(channel, proposal, leaseProvider) {
|
|
|
359
345
|
validateProposalParams(channel, proposal.params);
|
|
360
346
|
bindDraftToParams(channel, proposal);
|
|
361
347
|
const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(proposal.spliceTxDraft);
|
|
348
|
+
// OMN-023: the acceptor must verify the proposer actually signed this draft
|
|
349
|
+
// before contributing a signature of its own.
|
|
350
|
+
const verifyProposer = options.verifySignature ?? omnia_1.verifyChannelSignature;
|
|
351
|
+
if (!verifyProposer(proposal.proposerSignature, digest, proposal.proposerPublicKeyDigest)) {
|
|
352
|
+
throw new Error('Splice proposal proposer signature is invalid');
|
|
353
|
+
}
|
|
354
|
+
if (options.authorize) {
|
|
355
|
+
await options.authorize({
|
|
356
|
+
channel,
|
|
357
|
+
proposal,
|
|
358
|
+
localPartyId: partyInChannel.partyId,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
362
361
|
const reservation = await wotsLease.reserveKeyUse({
|
|
363
362
|
treeId: signer.publicKeyDigest,
|
|
364
363
|
purpose: 'omnia-splice-accept',
|
package/dist/cjs/successor.d.ts
CHANGED
|
@@ -28,6 +28,35 @@ export interface SpliceChainObserver {
|
|
|
28
28
|
getTip(): Promise<{
|
|
29
29
|
block: number;
|
|
30
30
|
}>;
|
|
31
|
+
/**
|
|
32
|
+
* Full transaction body by TxPoW id (OMN-074).
|
|
33
|
+
*
|
|
34
|
+
* Optional only so adapters can be upgraded independently. When it is present,
|
|
35
|
+
* successor observation proves the coin is an output of the splice that named
|
|
36
|
+
* it; when it is absent and lineage is required, observation fails closed.
|
|
37
|
+
*/
|
|
38
|
+
getTransaction?(txpowid: string): Promise<{
|
|
39
|
+
txpowid: string;
|
|
40
|
+
inputs: Array<{
|
|
41
|
+
coinId: string;
|
|
42
|
+
address?: string;
|
|
43
|
+
amount?: string;
|
|
44
|
+
tokenId?: string;
|
|
45
|
+
}>;
|
|
46
|
+
outputs: Array<{
|
|
47
|
+
coinId?: string;
|
|
48
|
+
address: string;
|
|
49
|
+
amount: string;
|
|
50
|
+
tokenId?: string;
|
|
51
|
+
}>;
|
|
52
|
+
} | null>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Raised when a successor coin cannot be tied to the splice transaction that is
|
|
56
|
+
* supposed to have created it (OMN-074).
|
|
57
|
+
*/
|
|
58
|
+
export declare class SpliceSuccessorLineageError extends Error {
|
|
59
|
+
constructor(message: string);
|
|
31
60
|
}
|
|
32
61
|
export interface SpliceSuccessorOptions {
|
|
33
62
|
/** Channel funding address the successor must pay to. */
|
|
@@ -46,9 +75,24 @@ export interface SpliceSuccessorOptions {
|
|
|
46
75
|
now?: () => number;
|
|
47
76
|
/** Injected sleep (tests). */
|
|
48
77
|
sleep?: (ms: number) => Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* The splice transaction this successor must belong to (OMN-074). Supplying
|
|
80
|
+
* it turns lineage verification on by default.
|
|
81
|
+
*/
|
|
82
|
+
spliceTxId?: string;
|
|
83
|
+
/** Coin the splice must consume — normally the channel's funding coin. */
|
|
84
|
+
predecessorCoinId?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Require proof that the successor is an output of `spliceTxId`. Defaults to
|
|
87
|
+
* true when `spliceTxId` is supplied. Set false only for a caller that has
|
|
88
|
+
* established provenance some other way.
|
|
89
|
+
*/
|
|
90
|
+
requireLineage?: boolean;
|
|
49
91
|
}
|
|
50
92
|
export interface ObservedSuccessor {
|
|
51
93
|
coinId: string;
|
|
94
|
+
/** True when the coin was proven to be an output of the splice transaction. */
|
|
95
|
+
lineageVerified: boolean;
|
|
52
96
|
confirmations: number;
|
|
53
97
|
confirmed: boolean;
|
|
54
98
|
coin: {
|
|
@@ -74,6 +118,16 @@ export interface PendingSpliceRecord {
|
|
|
74
118
|
txpowId: string;
|
|
75
119
|
coinId?: string;
|
|
76
120
|
broadcastAt: number;
|
|
121
|
+
/**
|
|
122
|
+
* OMN-014: the successor this splice must produce. Recovery uses the record's
|
|
123
|
+
* own address/token/amount (and coin id once observed) instead of applying one
|
|
124
|
+
* global observation to every pending splice.
|
|
125
|
+
*/
|
|
126
|
+
expected?: {
|
|
127
|
+
address: string;
|
|
128
|
+
tokenId: string;
|
|
129
|
+
amount: string;
|
|
130
|
+
};
|
|
77
131
|
}
|
|
78
132
|
export interface PendingSpliceStore {
|
|
79
133
|
save(record: PendingSpliceRecord): Promise<void> | void;
|
|
@@ -87,6 +141,8 @@ export interface SpliceRecoveryResult {
|
|
|
87
141
|
coinId?: string;
|
|
88
142
|
rebroadcast: boolean;
|
|
89
143
|
error?: string;
|
|
144
|
+
/** True when the successor was proven to be an output of the recorded splice. */
|
|
145
|
+
lineageVerified?: boolean;
|
|
90
146
|
}
|
|
91
147
|
/**
|
|
92
148
|
* Reconcile interrupted broadcasts.
|
package/dist/cjs/successor.js
CHANGED
|
@@ -10,10 +10,66 @@
|
|
|
10
10
|
* between broadcast and confirmation can finish or retry safely.
|
|
11
11
|
*/
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.SpliceSuccessorLineageError = void 0;
|
|
13
14
|
exports.observeSpliceSuccessor = observeSpliceSuccessor;
|
|
14
15
|
exports.recoverInterruptedSplices = recoverInterruptedSplices;
|
|
15
16
|
const core_1 = require("@totemsdk/core");
|
|
17
|
+
/**
|
|
18
|
+
* Raised when a successor coin cannot be tied to the splice transaction that is
|
|
19
|
+
* supposed to have created it (OMN-074).
|
|
20
|
+
*/
|
|
21
|
+
class SpliceSuccessorLineageError extends Error {
|
|
22
|
+
constructor(message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = 'SpliceSuccessorLineageError';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.SpliceSuccessorLineageError = SpliceSuccessorLineageError;
|
|
16
28
|
const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
29
|
+
function sameCoinId(a, b) {
|
|
30
|
+
return a.replace(/^0x/i, '').toLowerCase() === b.replace(/^0x/i, '').toLowerCase();
|
|
31
|
+
}
|
|
32
|
+
function sameText(a, b) {
|
|
33
|
+
return (a ?? '').replace(/^0x/i, '').toLowerCase() === (b ?? '').replace(/^0x/i, '').toLowerCase();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Prove that `coinId` is an output of `spliceTxId` (OMN-074).
|
|
37
|
+
*
|
|
38
|
+
* Address/token/amount matching cannot distinguish "the splice created this
|
|
39
|
+
* coin" from "some other transaction paid a matching coin to the same address".
|
|
40
|
+
* The only trustworthy binding is the transaction body: it must be visible, it
|
|
41
|
+
* must spend the predecessor, and the successor must appear among its outputs.
|
|
42
|
+
*/
|
|
43
|
+
async function assertSuccessorLineage(chain, coinId, options) {
|
|
44
|
+
const required = options.requireLineage ?? options.spliceTxId !== undefined;
|
|
45
|
+
if (!required)
|
|
46
|
+
return;
|
|
47
|
+
if (!options.spliceTxId) {
|
|
48
|
+
throw new SpliceSuccessorLineageError('successor lineage is required but no splice transaction id was supplied');
|
|
49
|
+
}
|
|
50
|
+
if (!chain.getTransaction) {
|
|
51
|
+
throw new SpliceSuccessorLineageError('chain adapter cannot look up transactions; splice successor lineage cannot be proven');
|
|
52
|
+
}
|
|
53
|
+
const transaction = await chain.getTransaction(options.spliceTxId);
|
|
54
|
+
if (!transaction) {
|
|
55
|
+
throw new SpliceSuccessorLineageError(`splice transaction ${options.spliceTxId} is not visible on chain`);
|
|
56
|
+
}
|
|
57
|
+
if (options.predecessorCoinId
|
|
58
|
+
&& !transaction.inputs.some((input) => sameCoinId(input.coinId, options.predecessorCoinId))) {
|
|
59
|
+
throw new SpliceSuccessorLineageError(`splice transaction ${options.spliceTxId} does not spend predecessor ${options.predecessorCoinId}`);
|
|
60
|
+
}
|
|
61
|
+
const matchedByCoinId = transaction.outputs.some((output) => output.coinId !== undefined && sameCoinId(output.coinId, coinId));
|
|
62
|
+
if (matchedByCoinId)
|
|
63
|
+
return;
|
|
64
|
+
// Nodes that omit output coin ids: accept exactly one output that pays the
|
|
65
|
+
// successor address, token and amount. More than one is ambiguous.
|
|
66
|
+
const matches = transaction.outputs.filter((output) => sameText(output.address, options.address)
|
|
67
|
+
&& sameText(output.tokenId, options.tokenId)
|
|
68
|
+
&& (0, core_1.coinAmountAtoms)({ amount: output.amount }) === options.amount);
|
|
69
|
+
if (matches.length !== 1) {
|
|
70
|
+
throw new SpliceSuccessorLineageError(`successor ${coinId} is not an output of splice transaction ${options.spliceTxId}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
17
73
|
/**
|
|
18
74
|
* Find the successor coin at the channel address, excluding the spent old
|
|
19
75
|
* coin. A coin with `spent: true` is never a valid successor. Confirmation
|
|
@@ -47,7 +103,8 @@ async function observeSpliceSuccessor(chain, options) {
|
|
|
47
103
|
const created = candidate.created !== undefined ? Number(candidate.created) : NaN;
|
|
48
104
|
const confirmations = Number.isFinite(created) && created > 0 ? Math.max(0, tip.block - created + 1) : 0;
|
|
49
105
|
if (confirmations >= required) {
|
|
50
|
-
|
|
106
|
+
await assertSuccessorLineage(chain, candidate.coinid, options);
|
|
107
|
+
return { coinId: candidate.coinid, confirmations, confirmed: true, lineageVerified: true, coin: candidate };
|
|
51
108
|
}
|
|
52
109
|
// Found but not deep enough; keep polling until the deadline.
|
|
53
110
|
}
|
|
@@ -56,7 +113,13 @@ async function observeSpliceSuccessor(chain, options) {
|
|
|
56
113
|
const tip = await chain.getTip();
|
|
57
114
|
const created = candidate.created !== undefined ? Number(candidate.created) : NaN;
|
|
58
115
|
const confirmations = Number.isFinite(created) && created > 0 ? Math.max(0, tip.block - created + 1) : 0;
|
|
59
|
-
return {
|
|
116
|
+
return {
|
|
117
|
+
coinId: candidate.coinid,
|
|
118
|
+
confirmations,
|
|
119
|
+
confirmed: confirmations >= required,
|
|
120
|
+
lineageVerified: false,
|
|
121
|
+
coin: candidate,
|
|
122
|
+
};
|
|
60
123
|
}
|
|
61
124
|
return null;
|
|
62
125
|
}
|
|
@@ -74,15 +137,43 @@ async function observeSpliceSuccessor(chain, options) {
|
|
|
74
137
|
async function recoverInterruptedSplices(store, chain, observe, broadcast) {
|
|
75
138
|
const pending = await store.list();
|
|
76
139
|
const results = [];
|
|
140
|
+
/** A successor coin may settle exactly one pending splice. */
|
|
141
|
+
const claimed = new Set();
|
|
77
142
|
for (const record of pending) {
|
|
78
143
|
try {
|
|
79
|
-
const
|
|
144
|
+
const options = {
|
|
145
|
+
...observe,
|
|
146
|
+
...(record.expected
|
|
147
|
+
? {
|
|
148
|
+
address: record.expected.address,
|
|
149
|
+
tokenId: record.expected.tokenId,
|
|
150
|
+
amount: BigInt(record.expected.amount),
|
|
151
|
+
}
|
|
152
|
+
: {}),
|
|
153
|
+
timeoutMs: observe.timeoutMs ?? 0,
|
|
154
|
+
spliceTxId: record.txpowId,
|
|
155
|
+
// Verify automatically when the adapter can, and always when the caller
|
|
156
|
+
// asked for it (OMN-074). Recovery of an already-broadcast splice is the
|
|
157
|
+
// last chance to bind the successor to its transaction.
|
|
158
|
+
requireLineage: observe.requireLineage ?? typeof chain.getTransaction === 'function',
|
|
159
|
+
};
|
|
160
|
+
const observed = await observeSpliceSuccessor(chain, options);
|
|
161
|
+
if (observed) {
|
|
162
|
+
if (claimed.has(observed.coinId)) {
|
|
163
|
+
throw new Error(`successor coin ${observed.coinId} was already claimed by another pending splice`);
|
|
164
|
+
}
|
|
165
|
+
if (record.coinId && record.coinId !== observed.coinId) {
|
|
166
|
+
throw new Error(`observed successor ${observed.coinId} does not match the coin recorded for this splice (${record.coinId})`);
|
|
167
|
+
}
|
|
168
|
+
claimed.add(observed.coinId);
|
|
169
|
+
}
|
|
80
170
|
if (observed?.confirmed) {
|
|
81
171
|
await store.clear(record.spliceId);
|
|
82
172
|
results.push({
|
|
83
173
|
spliceId: record.spliceId,
|
|
84
174
|
status: 'active',
|
|
85
175
|
coinId: observed.coinId,
|
|
176
|
+
lineageVerified: observed.lineageVerified,
|
|
86
177
|
rebroadcast: false,
|
|
87
178
|
});
|
|
88
179
|
continue;
|
|
@@ -97,10 +188,14 @@ async function recoverInterruptedSplices(store, chain, observe, broadcast) {
|
|
|
97
188
|
});
|
|
98
189
|
}
|
|
99
190
|
else {
|
|
191
|
+
if (observed && record.coinId !== observed.coinId) {
|
|
192
|
+
// Persist the observed successor so the next pass is bound to it.
|
|
193
|
+
await store.save({ ...record, coinId: observed.coinId });
|
|
194
|
+
}
|
|
100
195
|
results.push({
|
|
101
196
|
spliceId: record.spliceId,
|
|
102
|
-
status:
|
|
103
|
-
...(observed ? { coinId: observed.coinId } : {}),
|
|
197
|
+
status: 'funding_pending',
|
|
198
|
+
...(observed ? { coinId: observed.coinId, lineageVerified: observed.lineageVerified } : {}),
|
|
104
199
|
rebroadcast: true,
|
|
105
200
|
});
|
|
106
201
|
}
|
package/dist/coordination.d.ts
CHANGED
|
@@ -45,6 +45,8 @@ export interface SpliceProposeParams {
|
|
|
45
45
|
type: 'in' | 'out';
|
|
46
46
|
additionalCoinId?: string;
|
|
47
47
|
additionalAmount?: bigint;
|
|
48
|
+
/** OMN-052: address of the coin being added; required for splice-in. */
|
|
49
|
+
additionalCoinAddress?: string;
|
|
48
50
|
withdrawAmount?: bigint;
|
|
49
51
|
withdrawAddress?: string;
|
|
50
52
|
}
|
package/dist/coordination.js
CHANGED
|
@@ -72,7 +72,7 @@ export function createSpliceCoordination(deps) {
|
|
|
72
72
|
// acceptSplice binds the draft to our channel + validates params, then signs.
|
|
73
73
|
const local = await ensureQuiesced(channel, leaseProvider);
|
|
74
74
|
channels.set(local.channelId, local);
|
|
75
|
-
const acceptance = await acceptSplice(local, proposal, leaseProvider);
|
|
75
|
+
const acceptance = await acceptSplice(local, proposal, leaseProvider, { verifySignature: verify });
|
|
76
76
|
pendingAcceptances.set(proposal.spliceId, acceptance);
|
|
77
77
|
burnTimers.set(proposal.spliceId, setTimeout(() => burnPending(proposal.spliceId, 'splice-finalize-timeout'), ttlMs));
|
|
78
78
|
await ctx.send('SPLICE_ACCEPT', acceptance);
|
|
@@ -119,7 +119,7 @@ export function createSpliceCoordination(deps) {
|
|
|
119
119
|
const local = await ensureQuiesced(channel, leaseProvider);
|
|
120
120
|
channels.set(local.channelId, local);
|
|
121
121
|
const proposal = params.type === 'in'
|
|
122
|
-
? await proposeSpliceIn(local, params.additionalCoinId, params.additionalAmount, leaseProvider)
|
|
122
|
+
? await proposeSpliceIn(local, params.additionalCoinId, params.additionalAmount, leaseProvider, undefined, params.additionalCoinAddress)
|
|
123
123
|
: await proposeSpliceOut(local, params.withdrawAmount, params.withdrawAddress, leaseProvider);
|
|
124
124
|
await coordination.sendProposal({
|
|
125
125
|
peer,
|
package/dist/finalize.d.ts
CHANGED
|
@@ -51,12 +51,31 @@ export interface FinalizeSpliceOptions {
|
|
|
51
51
|
chainProvider?: SpliceChainObserver;
|
|
52
52
|
/** Confirmations required before activation. Default 1. */
|
|
53
53
|
confirmations?: number;
|
|
54
|
+
/**
|
|
55
|
+
* Require proof that the successor is an output of this splice transaction
|
|
56
|
+
* before the channel is reported active (OMN-074). Default true: without that
|
|
57
|
+
* binding, any coin with a matching address, token and amount would activate
|
|
58
|
+
* the channel. Set false only when provenance is established elsewhere.
|
|
59
|
+
*/
|
|
60
|
+
requireSuccessorLineage?: boolean;
|
|
54
61
|
/** Successor observation timeout. Default 30s. */
|
|
55
62
|
successorTimeoutMs?: number;
|
|
56
63
|
/** Observation poll interval. Default 1s. */
|
|
57
64
|
successorPollMs?: number;
|
|
58
65
|
/** Durable journal written before broadcast, cleared after confirmation. */
|
|
59
66
|
pendingSpliceStore?: PendingSpliceStore;
|
|
67
|
+
/**
|
|
68
|
+
* Persist the successor channel before the recovery journal is cleared
|
|
69
|
+
* (OMN-076). The host owns the channel store, so it supplies this hook;
|
|
70
|
+
* `finalizeSplice` calls it after the successor is constructed and before
|
|
71
|
+
* `pendingSpliceStore.clear`, so a crash in between leaves a recoverable
|
|
72
|
+
* journal entry rather than an on-chain successor with no local record.
|
|
73
|
+
*/
|
|
74
|
+
persistSuccessor?: (channel: SplicedChannel) => Promise<void> | void;
|
|
75
|
+
/** Serialized Minima CoinProof for every spending input (OMN-053). */
|
|
76
|
+
coinProofs?: Uint8Array[];
|
|
77
|
+
/** Serialized Minima ScriptProof for every spending input (OMN-053). */
|
|
78
|
+
scriptProofs?: Uint8Array[];
|
|
60
79
|
}
|
|
61
80
|
/**
|
|
62
81
|
* Finalize a splice by assembling both parties' signatures, mining PoW,
|
package/dist/finalize.js
CHANGED
|
@@ -1,24 +1,31 @@
|
|
|
1
1
|
import { concatBytes } from '@totemsdk/core';
|
|
2
2
|
import { serializeTxBody, mineTxPoW, TX_POW_MIN_DIFFICULTY } from '@totemsdk/txpow';
|
|
3
|
-
import { verifyChannelSignature } from '@totemsdk/omnia';
|
|
3
|
+
import { serializeOmniaWitness, verifyChannelSignature } from '@totemsdk/omnia';
|
|
4
4
|
import { buildSpliceTx, computeSpliceTxDigest, spliceDraftToMinimaBytes } from './splice-tx.js';
|
|
5
5
|
import { observeSpliceSuccessor, } from './successor.js';
|
|
6
6
|
import { SpliceSignatureMismatchError, SpliceMissingPartyError, SpliceChannelStatusError, } from './errors.js';
|
|
7
7
|
function defaultWotsVerify(sig, digest, pkd) {
|
|
8
8
|
return verifyChannelSignature(sig, digest, pkd);
|
|
9
9
|
}
|
|
10
|
-
function assembleSpliceWitness(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
10
|
+
function assembleSpliceWitness(channel, proposal, acceptance, proofs) {
|
|
11
|
+
// OMN-053: use the shared canonical witness encoder (MiniNumber counts and
|
|
12
|
+
// serialized proof objects) instead of a bespoke 4-byte layout.
|
|
13
|
+
// Minima's MULTISIG verification reads witnesses in key order.
|
|
14
|
+
const byParty = new Map([
|
|
15
|
+
[proposal.proposerPublicKeyDigest, proposal.proposerSignature],
|
|
16
|
+
[acceptance.acceptorPublicKeyDigest, acceptance.acceptorSignature],
|
|
17
|
+
]);
|
|
18
|
+
const signatures = channel.parties
|
|
19
|
+
.map((party) => byParty.get(party.publicKeyDigest))
|
|
20
|
+
.filter((signature) => Boolean(signature));
|
|
21
|
+
if (signatures.length !== 2) {
|
|
22
|
+
throw new SpliceSignatureMismatchError('splice witness requires both party signatures');
|
|
23
|
+
}
|
|
24
|
+
return serializeOmniaWitness({
|
|
25
|
+
signatures,
|
|
26
|
+
coinProofs: proofs.coinProofs ?? [],
|
|
27
|
+
scriptProofs: proofs.scriptProofs ?? [],
|
|
28
|
+
});
|
|
22
29
|
}
|
|
23
30
|
/**
|
|
24
31
|
* Structural authorization checks: both parties must be distinct channel
|
|
@@ -145,7 +152,14 @@ export async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
145
152
|
// Track which reservations have been committed so we know which to burn on failure.
|
|
146
153
|
const committed = new Set();
|
|
147
154
|
const txBytes = spliceDraftToMinimaBytes(proposal.spliceTxDraft);
|
|
148
|
-
|
|
155
|
+
// OMN-053: a broadcast splice must carry proof material for its inputs.
|
|
156
|
+
if (options?.broadcast && (!options.coinProofs?.length || !options.scriptProofs?.length)) {
|
|
157
|
+
throw new Error('finalizeSplice requires coin and script proofs before broadcasting');
|
|
158
|
+
}
|
|
159
|
+
const witnessBytes = assembleSpliceWitness(channel, proposal, acceptance, {
|
|
160
|
+
...(options?.coinProofs ? { coinProofs: options.coinProofs } : {}),
|
|
161
|
+
...(options?.scriptProofs ? { scriptProofs: options.scriptProofs } : {}),
|
|
162
|
+
});
|
|
149
163
|
let spliceFundingTxId;
|
|
150
164
|
try {
|
|
151
165
|
const difficulty = options?.mineDifficulty ?? TX_POW_MIN_DIFFICULTY;
|
|
@@ -161,6 +175,14 @@ export async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
161
175
|
txHex,
|
|
162
176
|
txpowId: spliceFundingTxId,
|
|
163
177
|
broadcastAt: Date.now(),
|
|
178
|
+
// OMN-075: name the successor this splice must produce. Recovery prefers
|
|
179
|
+
// this over the caller's global observation parameters, so two
|
|
180
|
+
// interrupted splices can never be reconciled against each other.
|
|
181
|
+
expected: {
|
|
182
|
+
address: channel.fundingAddress,
|
|
183
|
+
tokenId: channel.tokenId,
|
|
184
|
+
amount: proposal.params.newTotalValue.toString(),
|
|
185
|
+
},
|
|
164
186
|
};
|
|
165
187
|
await options.pendingSpliceStore?.save(record);
|
|
166
188
|
const result = await options.broadcast(txHex);
|
|
@@ -209,6 +231,11 @@ export async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
209
231
|
confirmations: options.confirmations ?? 1,
|
|
210
232
|
timeoutMs: options.successorTimeoutMs ?? 30000,
|
|
211
233
|
pollIntervalMs: options.successorPollMs ?? 1000,
|
|
234
|
+
// OMN-074: the successor must be an output of this splice and the splice
|
|
235
|
+
// must consume the predecessor funding coin.
|
|
236
|
+
spliceTxId: spliceFundingTxId,
|
|
237
|
+
predecessorCoinId: channel.fundingCoinId,
|
|
238
|
+
requireLineage: options.requireSuccessorLineage ?? true,
|
|
212
239
|
});
|
|
213
240
|
if (observed) {
|
|
214
241
|
spliceFundingCoinId = observed.coinId;
|
|
@@ -218,9 +245,6 @@ export async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
218
245
|
successorStatus = 'funding_pending';
|
|
219
246
|
}
|
|
220
247
|
}
|
|
221
|
-
if (successorStatus === 'active') {
|
|
222
|
-
await options?.pendingSpliceStore?.clear(proposal.spliceId);
|
|
223
|
-
}
|
|
224
248
|
const splicedChannel = {
|
|
225
249
|
...channel,
|
|
226
250
|
status: successorStatus,
|
|
@@ -247,8 +271,19 @@ export async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
247
271
|
],
|
|
248
272
|
updatedAt: Date.now(),
|
|
249
273
|
};
|
|
274
|
+
// OMN-076: persist the successor, retire the predecessor, then complete the
|
|
275
|
+
// journal entry — in that order. Clearing the journal first left a crash
|
|
276
|
+
// window where the successor was on chain, the predecessor was still local,
|
|
277
|
+
// and no recovery record existed. With `persistSuccessor` supplied by the
|
|
278
|
+
// caller (the host owns the channel store), that window is closed.
|
|
279
|
+
if (options?.persistSuccessor) {
|
|
280
|
+
await options.persistSuccessor(splicedChannel);
|
|
281
|
+
}
|
|
250
282
|
// Mark the old (quiesced) channel as spliced — it is now invalidated.
|
|
251
283
|
// Any further operations on `channel` should be rejected by status checks.
|
|
252
284
|
channel.status = 'spliced';
|
|
285
|
+
if (successorStatus === 'active') {
|
|
286
|
+
await options?.pendingSpliceStore?.clear(proposal.spliceId);
|
|
287
|
+
}
|
|
253
288
|
return splicedChannel;
|
|
254
289
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { buildSpliceTx, spliceDraftToMinimaBytes, computeSpliceTxDigest } from '
|
|
|
3
3
|
export { proposeSpliceIn, proposeSpliceOut, acceptSplice, buildSpliceProposal, buildSpliceInProposal, buildSpliceOutProposal, } from './splice.js';
|
|
4
4
|
export type { UnsignedSpliceProposal } from './splice.js';
|
|
5
5
|
export { finalizeSplice, type FinalizeSpliceOptions } from './finalize.js';
|
|
6
|
-
export { observeSpliceSuccessor, recoverInterruptedSplices, type SpliceChainObserver, type SpliceSuccessorOptions, type ObservedSuccessor, type PendingSpliceRecord, type PendingSpliceStore, type SpliceRecoveryResult, } from './successor.js';
|
|
6
|
+
export { observeSpliceSuccessor, recoverInterruptedSplices, SpliceSuccessorLineageError, type SpliceChainObserver, type SpliceSuccessorOptions, type ObservedSuccessor, type PendingSpliceRecord, type PendingSpliceStore, type SpliceRecoveryResult, } from './successor.js';
|
|
7
7
|
export type { SpliceType, SpliceParams, SpliceTxDraft, SpliceTxInput, SpliceTxOutput, SpliceProposal, SpliceAcceptance, QuiescedChannel, SplicedChannel, SpliceLeaseProvider, SpliceSigningIndices, WotsSignature, } from './types.js';
|
|
8
8
|
export { SpliceError, PendingHTLCError, SpliceChannelStatusError, SpliceBalanceConservationError, SpliceSignatureMismatchError, SpliceMissingPartyError, SpliceInsufficientFundsError, } from './errors.js';
|
|
9
9
|
export { createDurableSpliceStore, } from './durable-splice-store.js';
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export { quiesceChannel } from './quiesce.js';
|
|
|
2
2
|
export { buildSpliceTx, spliceDraftToMinimaBytes, computeSpliceTxDigest } from './splice-tx.js';
|
|
3
3
|
export { proposeSpliceIn, proposeSpliceOut, acceptSplice, buildSpliceProposal, buildSpliceInProposal, buildSpliceOutProposal, } from './splice.js';
|
|
4
4
|
export { finalizeSplice } from './finalize.js';
|
|
5
|
-
export { observeSpliceSuccessor, recoverInterruptedSplices, } from './successor.js';
|
|
5
|
+
export { observeSpliceSuccessor, recoverInterruptedSplices, SpliceSuccessorLineageError, } from './successor.js';
|
|
6
6
|
export { SpliceError, PendingHTLCError, SpliceChannelStatusError, SpliceBalanceConservationError, SpliceSignatureMismatchError, SpliceMissingPartyError, SpliceInsufficientFundsError, } from './errors.js';
|
|
7
7
|
export { createDurableSpliceStore, } from './durable-splice-store.js';
|
|
8
8
|
// RFC-035 B1 — splice peer round-trip over the coordination transport.
|
package/dist/splice-tx.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { sha3_256 } from '@totemsdk/core';
|
|
1
|
+
import { atomsToAmount, sha3_256 } from '@totemsdk/core';
|
|
2
2
|
import { serializeTransaction, } from '@totemsdk/core';
|
|
3
3
|
import { COINID_ELTOO, computeStateCommitmentV2 } from '@totemsdk/omnia';
|
|
4
4
|
import { bytesToHex } from '@totemsdk/core';
|
|
@@ -58,9 +58,14 @@ export function buildSpliceTx(channel, params) {
|
|
|
58
58
|
},
|
|
59
59
|
];
|
|
60
60
|
if (params.type === 'splice_in' && params.additionalCoinId && params.additionalAmount) {
|
|
61
|
+
// OMN-052: an unspecified address serializes to a placeholder, so the
|
|
62
|
+
// transaction would not describe the coin being added. Fail closed.
|
|
63
|
+
if (!params.additionalCoinAddress) {
|
|
64
|
+
throw new Error('splice-in requires the additional coin address');
|
|
65
|
+
}
|
|
61
66
|
inputs.push({
|
|
62
67
|
coinId: params.additionalCoinId,
|
|
63
|
-
address: params.additionalCoinAddress
|
|
68
|
+
address: params.additionalCoinAddress,
|
|
64
69
|
amount: params.additionalAmount,
|
|
65
70
|
tokenId,
|
|
66
71
|
});
|
|
@@ -151,7 +156,10 @@ export function spliceDraftToMinimaBytes(draft) {
|
|
|
151
156
|
};
|
|
152
157
|
const inputs = draft.inputs.map(inp => ({
|
|
153
158
|
coinid: inp.coinId,
|
|
154
|
-
|
|
159
|
+
// OMN-051: draft amounts are atoms; core's JSON serializer parses decimal
|
|
160
|
+
// MINIMA. An integer string here means scale-zero MiniNumber (10^44x too
|
|
161
|
+
// large), so convert at the boundary.
|
|
162
|
+
amount: atomsToAmount(inp.amount),
|
|
155
163
|
address: inp.address,
|
|
156
164
|
tokenid: inp.tokenId,
|
|
157
165
|
storestate: false,
|
|
@@ -161,7 +169,7 @@ export function spliceDraftToMinimaBytes(draft) {
|
|
|
161
169
|
state: [],
|
|
162
170
|
}));
|
|
163
171
|
const outputs = draft.outputs.map(out => ({
|
|
164
|
-
amount: out.amount
|
|
172
|
+
amount: atomsToAmount(out.amount),
|
|
165
173
|
address: out.address,
|
|
166
174
|
tokenid: out.tokenId,
|
|
167
175
|
storestate: out.storeState ?? false,
|
package/dist/splice.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type OmniaChannel } from '@totemsdk/omnia';
|
|
2
2
|
import type { SpliceParams, SpliceProposal, SpliceAcceptance, SpliceLeaseProvider, QuiescedChannel } from './types.js';
|
|
3
3
|
/** A splice proposal whose proposer signature has not been applied yet. */
|
|
4
4
|
export interface UnsignedSpliceProposal {
|
|
@@ -17,7 +17,9 @@ export interface UnsignedSpliceProposal {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare function buildSpliceProposal(channel: OmniaChannel | QuiescedChannel, params: SpliceParams, proposerPublicKeyDigest: string): UnsignedSpliceProposal;
|
|
19
19
|
/** Build an unsigned splice-in proposal (see `buildSpliceProposal`). */
|
|
20
|
-
export declare function buildSpliceInProposal(channel: OmniaChannel | QuiescedChannel, additionalCoinId: string, additionalAmount: bigint, proposerPublicKeyDigest: string, newBalances?: Record<string, bigint
|
|
20
|
+
export declare function buildSpliceInProposal(channel: OmniaChannel | QuiescedChannel, additionalCoinId: string, additionalAmount: bigint, proposerPublicKeyDigest: string, newBalances?: Record<string, bigint>,
|
|
21
|
+
/** OMN-052: address of the coin being added; required by the builder. */
|
|
22
|
+
additionalCoinAddress?: string): UnsignedSpliceProposal;
|
|
21
23
|
/** Build an unsigned splice-out proposal (see `buildSpliceProposal`). */
|
|
22
24
|
export declare function buildSpliceOutProposal(channel: OmniaChannel | QuiescedChannel, withdrawAmount: bigint, withdrawAddress: string, proposerPublicKeyDigest: string, newBalances?: Record<string, bigint>, extraOutputs?: Array<{
|
|
23
25
|
address: string;
|
|
@@ -45,7 +47,12 @@ export declare function buildSpliceOutProposal(channel: OmniaChannel | QuiescedC
|
|
|
45
47
|
* `sum(newBalances) === newTotalValue` is preserved.
|
|
46
48
|
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
47
49
|
*/
|
|
48
|
-
export declare function proposeSpliceIn(channel: OmniaChannel | QuiescedChannel, additionalCoinId: string, additionalAmount: bigint, leaseProvider: SpliceLeaseProvider, newBalances?: Record<string, bigint
|
|
50
|
+
export declare function proposeSpliceIn(channel: OmniaChannel | QuiescedChannel, additionalCoinId: string, additionalAmount: bigint, leaseProvider: SpliceLeaseProvider, newBalances?: Record<string, bigint>,
|
|
51
|
+
/**
|
|
52
|
+
* OMN-052: the address of the coin being added. Required by the transaction
|
|
53
|
+
* builder because an empty address serializes to a placeholder.
|
|
54
|
+
*/
|
|
55
|
+
additionalCoinAddress?: string): Promise<SpliceProposal>;
|
|
49
56
|
/**
|
|
50
57
|
* Initiating party proposes a splice-out: withdraw funds from the channel on-chain.
|
|
51
58
|
*
|
|
@@ -92,4 +99,14 @@ export declare function proposeSpliceOut(channel: OmniaChannel | QuiescedChannel
|
|
|
92
99
|
* @param leaseProvider - Provides the acceptor signer and WOTS lease.
|
|
93
100
|
* @returns SpliceAcceptance containing acceptor co-signature and lease data.
|
|
94
101
|
*/
|
|
95
|
-
export
|
|
102
|
+
export interface AcceptSpliceOptions {
|
|
103
|
+
/** OMN-023: explicit spending authorization for an economic reduction. */
|
|
104
|
+
authorize?: (request: {
|
|
105
|
+
channel: OmniaChannel | QuiescedChannel;
|
|
106
|
+
proposal: SpliceProposal;
|
|
107
|
+
localPartyId: string;
|
|
108
|
+
}) => Promise<void> | void;
|
|
109
|
+
/** Signature verifier for the proposer (defaults to the WOTS verifier). */
|
|
110
|
+
verifySignature?: (signature: Uint8Array, digest: Uint8Array, publicKeyDigest: string) => boolean;
|
|
111
|
+
}
|
|
112
|
+
export declare function acceptSplice(channel: OmniaChannel | QuiescedChannel, proposal: SpliceProposal, leaseProvider: SpliceLeaseProvider, options?: AcceptSpliceOptions): Promise<SpliceAcceptance>;
|
package/dist/splice.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { verifyChannelSignature } from '@totemsdk/omnia';
|
|
1
2
|
import { buildSpliceTx, computeSpliceTxDigest, spliceDraftToMinimaBytes } from './splice-tx.js';
|
|
2
3
|
import { PendingHTLCError, SpliceChannelStatusError, SpliceMissingPartyError, } from './errors.js';
|
|
3
4
|
/**
|
|
@@ -122,7 +123,9 @@ export function buildSpliceProposal(channel, params, proposerPublicKeyDigest) {
|
|
|
122
123
|
};
|
|
123
124
|
}
|
|
124
125
|
/** Build an unsigned splice-in proposal (see `buildSpliceProposal`). */
|
|
125
|
-
export function buildSpliceInProposal(channel, additionalCoinId, additionalAmount, proposerPublicKeyDigest, newBalances
|
|
126
|
+
export function buildSpliceInProposal(channel, additionalCoinId, additionalAmount, proposerPublicKeyDigest, newBalances,
|
|
127
|
+
/** OMN-052: address of the coin being added; required by the builder. */
|
|
128
|
+
additionalCoinAddress) {
|
|
126
129
|
validateChannelQuiesced(channel);
|
|
127
130
|
const newTotal = channel.totalValue + additionalAmount;
|
|
128
131
|
const effectiveNewBalances = newBalances ?? scaleBalances(channel.balances, channel.totalValue, newTotal);
|
|
@@ -132,6 +135,7 @@ export function buildSpliceInProposal(channel, additionalCoinId, additionalAmoun
|
|
|
132
135
|
newBalances: effectiveNewBalances,
|
|
133
136
|
additionalCoinId,
|
|
134
137
|
additionalAmount,
|
|
138
|
+
...(additionalCoinAddress ? { additionalCoinAddress } : {}),
|
|
135
139
|
}, proposerPublicKeyDigest);
|
|
136
140
|
}
|
|
137
141
|
/** Build an unsigned splice-out proposal (see `buildSpliceProposal`). */
|
|
@@ -191,7 +195,12 @@ export function buildSpliceOutProposal(channel, withdrawAmount, withdrawAddress,
|
|
|
191
195
|
* `sum(newBalances) === newTotalValue` is preserved.
|
|
192
196
|
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
193
197
|
*/
|
|
194
|
-
export async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leaseProvider, newBalances
|
|
198
|
+
export async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leaseProvider, newBalances,
|
|
199
|
+
/**
|
|
200
|
+
* OMN-052: the address of the coin being added. Required by the transaction
|
|
201
|
+
* builder because an empty address serializes to a placeholder.
|
|
202
|
+
*/
|
|
203
|
+
additionalCoinAddress) {
|
|
195
204
|
validateChannelQuiesced(channel);
|
|
196
205
|
const newTotal = channel.totalValue + additionalAmount;
|
|
197
206
|
const effectiveNewBalances = newBalances ?? scaleBalances(channel.balances, channel.totalValue, newTotal);
|
|
@@ -201,6 +210,7 @@ export async function proposeSpliceIn(channel, additionalCoinId, additionalAmoun
|
|
|
201
210
|
newBalances: effectiveNewBalances,
|
|
202
211
|
additionalCoinId,
|
|
203
212
|
additionalAmount,
|
|
213
|
+
...(additionalCoinAddress ? { additionalCoinAddress } : {}),
|
|
204
214
|
};
|
|
205
215
|
const draft = buildSpliceTx(channel, params);
|
|
206
216
|
const digest = computeSpliceTxDigest(draft);
|
|
@@ -307,31 +317,7 @@ export async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress,
|
|
|
307
317
|
proposedAt: Date.now(),
|
|
308
318
|
};
|
|
309
319
|
}
|
|
310
|
-
|
|
311
|
-
* Counterparty accepts a splice proposal by co-signing the splice TX digest.
|
|
312
|
-
*
|
|
313
|
-
* Requires the channel to be in 'quiesced' state.
|
|
314
|
-
*
|
|
315
|
-
* WOTS lease safety: reserves a key slot before signing. The returned
|
|
316
|
-
* `acceptorReservationId` and `acceptorSigningIndices` are embedded in the
|
|
317
|
-
* `SpliceAcceptance` and consumed by `finalizeSplice` to commit or burn the
|
|
318
|
-
* acceptor's key-slot reservation.
|
|
319
|
-
*
|
|
320
|
-
* The acceptor independently:
|
|
321
|
-
* 1. Validates party membership (proposer and acceptor both in channel).
|
|
322
|
-
* 2. Validates proposal balance conservation and amount constraints.
|
|
323
|
-
* 3. Cryptographically binds the signed draft to the proposal params by
|
|
324
|
-
* recomputing the expected draft from `params + channel` and comparing
|
|
325
|
-
* digests — rejects if they differ (tamper detection).
|
|
326
|
-
* 4. Reserves a WOTS key slot via `leaseProvider.wotsLease`.
|
|
327
|
-
* 5. Signs the splice TX digest with reserved indices.
|
|
328
|
-
*
|
|
329
|
-
* @param channel - Quiesced channel.
|
|
330
|
-
* @param proposal - Splice proposal from the initiating party.
|
|
331
|
-
* @param leaseProvider - Provides the acceptor signer and WOTS lease.
|
|
332
|
-
* @returns SpliceAcceptance containing acceptor co-signature and lease data.
|
|
333
|
-
*/
|
|
334
|
-
export async function acceptSplice(channel, proposal, leaseProvider) {
|
|
320
|
+
export async function acceptSplice(channel, proposal, leaseProvider, options = {}) {
|
|
335
321
|
validateChannelQuiesced(channel);
|
|
336
322
|
if (proposal.channelId !== channel.channelId) {
|
|
337
323
|
throw new Error(`Proposal channelId '${proposal.channelId}' does not match channel '${channel.channelId}'`);
|
|
@@ -351,6 +337,19 @@ export async function acceptSplice(channel, proposal, leaseProvider) {
|
|
|
351
337
|
validateProposalParams(channel, proposal.params);
|
|
352
338
|
bindDraftToParams(channel, proposal);
|
|
353
339
|
const digest = computeSpliceTxDigest(proposal.spliceTxDraft);
|
|
340
|
+
// OMN-023: the acceptor must verify the proposer actually signed this draft
|
|
341
|
+
// before contributing a signature of its own.
|
|
342
|
+
const verifyProposer = options.verifySignature ?? verifyChannelSignature;
|
|
343
|
+
if (!verifyProposer(proposal.proposerSignature, digest, proposal.proposerPublicKeyDigest)) {
|
|
344
|
+
throw new Error('Splice proposal proposer signature is invalid');
|
|
345
|
+
}
|
|
346
|
+
if (options.authorize) {
|
|
347
|
+
await options.authorize({
|
|
348
|
+
channel,
|
|
349
|
+
proposal,
|
|
350
|
+
localPartyId: partyInChannel.partyId,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
354
353
|
const reservation = await wotsLease.reserveKeyUse({
|
|
355
354
|
treeId: signer.publicKeyDigest,
|
|
356
355
|
purpose: 'omnia-splice-accept',
|
package/dist/successor.d.ts
CHANGED
|
@@ -28,6 +28,35 @@ export interface SpliceChainObserver {
|
|
|
28
28
|
getTip(): Promise<{
|
|
29
29
|
block: number;
|
|
30
30
|
}>;
|
|
31
|
+
/**
|
|
32
|
+
* Full transaction body by TxPoW id (OMN-074).
|
|
33
|
+
*
|
|
34
|
+
* Optional only so adapters can be upgraded independently. When it is present,
|
|
35
|
+
* successor observation proves the coin is an output of the splice that named
|
|
36
|
+
* it; when it is absent and lineage is required, observation fails closed.
|
|
37
|
+
*/
|
|
38
|
+
getTransaction?(txpowid: string): Promise<{
|
|
39
|
+
txpowid: string;
|
|
40
|
+
inputs: Array<{
|
|
41
|
+
coinId: string;
|
|
42
|
+
address?: string;
|
|
43
|
+
amount?: string;
|
|
44
|
+
tokenId?: string;
|
|
45
|
+
}>;
|
|
46
|
+
outputs: Array<{
|
|
47
|
+
coinId?: string;
|
|
48
|
+
address: string;
|
|
49
|
+
amount: string;
|
|
50
|
+
tokenId?: string;
|
|
51
|
+
}>;
|
|
52
|
+
} | null>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Raised when a successor coin cannot be tied to the splice transaction that is
|
|
56
|
+
* supposed to have created it (OMN-074).
|
|
57
|
+
*/
|
|
58
|
+
export declare class SpliceSuccessorLineageError extends Error {
|
|
59
|
+
constructor(message: string);
|
|
31
60
|
}
|
|
32
61
|
export interface SpliceSuccessorOptions {
|
|
33
62
|
/** Channel funding address the successor must pay to. */
|
|
@@ -46,9 +75,24 @@ export interface SpliceSuccessorOptions {
|
|
|
46
75
|
now?: () => number;
|
|
47
76
|
/** Injected sleep (tests). */
|
|
48
77
|
sleep?: (ms: number) => Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* The splice transaction this successor must belong to (OMN-074). Supplying
|
|
80
|
+
* it turns lineage verification on by default.
|
|
81
|
+
*/
|
|
82
|
+
spliceTxId?: string;
|
|
83
|
+
/** Coin the splice must consume — normally the channel's funding coin. */
|
|
84
|
+
predecessorCoinId?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Require proof that the successor is an output of `spliceTxId`. Defaults to
|
|
87
|
+
* true when `spliceTxId` is supplied. Set false only for a caller that has
|
|
88
|
+
* established provenance some other way.
|
|
89
|
+
*/
|
|
90
|
+
requireLineage?: boolean;
|
|
49
91
|
}
|
|
50
92
|
export interface ObservedSuccessor {
|
|
51
93
|
coinId: string;
|
|
94
|
+
/** True when the coin was proven to be an output of the splice transaction. */
|
|
95
|
+
lineageVerified: boolean;
|
|
52
96
|
confirmations: number;
|
|
53
97
|
confirmed: boolean;
|
|
54
98
|
coin: {
|
|
@@ -74,6 +118,16 @@ export interface PendingSpliceRecord {
|
|
|
74
118
|
txpowId: string;
|
|
75
119
|
coinId?: string;
|
|
76
120
|
broadcastAt: number;
|
|
121
|
+
/**
|
|
122
|
+
* OMN-014: the successor this splice must produce. Recovery uses the record's
|
|
123
|
+
* own address/token/amount (and coin id once observed) instead of applying one
|
|
124
|
+
* global observation to every pending splice.
|
|
125
|
+
*/
|
|
126
|
+
expected?: {
|
|
127
|
+
address: string;
|
|
128
|
+
tokenId: string;
|
|
129
|
+
amount: string;
|
|
130
|
+
};
|
|
77
131
|
}
|
|
78
132
|
export interface PendingSpliceStore {
|
|
79
133
|
save(record: PendingSpliceRecord): Promise<void> | void;
|
|
@@ -87,6 +141,8 @@ export interface SpliceRecoveryResult {
|
|
|
87
141
|
coinId?: string;
|
|
88
142
|
rebroadcast: boolean;
|
|
89
143
|
error?: string;
|
|
144
|
+
/** True when the successor was proven to be an output of the recorded splice. */
|
|
145
|
+
lineageVerified?: boolean;
|
|
90
146
|
}
|
|
91
147
|
/**
|
|
92
148
|
* Reconcile interrupted broadcasts.
|
package/dist/successor.js
CHANGED
|
@@ -9,7 +9,61 @@
|
|
|
9
9
|
* between broadcast and confirmation can finish or retry safely.
|
|
10
10
|
*/
|
|
11
11
|
import { coinAmountAtoms } from '@totemsdk/core';
|
|
12
|
+
/**
|
|
13
|
+
* Raised when a successor coin cannot be tied to the splice transaction that is
|
|
14
|
+
* supposed to have created it (OMN-074).
|
|
15
|
+
*/
|
|
16
|
+
export class SpliceSuccessorLineageError extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = 'SpliceSuccessorLineageError';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
12
22
|
const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
23
|
+
function sameCoinId(a, b) {
|
|
24
|
+
return a.replace(/^0x/i, '').toLowerCase() === b.replace(/^0x/i, '').toLowerCase();
|
|
25
|
+
}
|
|
26
|
+
function sameText(a, b) {
|
|
27
|
+
return (a ?? '').replace(/^0x/i, '').toLowerCase() === (b ?? '').replace(/^0x/i, '').toLowerCase();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Prove that `coinId` is an output of `spliceTxId` (OMN-074).
|
|
31
|
+
*
|
|
32
|
+
* Address/token/amount matching cannot distinguish "the splice created this
|
|
33
|
+
* coin" from "some other transaction paid a matching coin to the same address".
|
|
34
|
+
* The only trustworthy binding is the transaction body: it must be visible, it
|
|
35
|
+
* must spend the predecessor, and the successor must appear among its outputs.
|
|
36
|
+
*/
|
|
37
|
+
async function assertSuccessorLineage(chain, coinId, options) {
|
|
38
|
+
const required = options.requireLineage ?? options.spliceTxId !== undefined;
|
|
39
|
+
if (!required)
|
|
40
|
+
return;
|
|
41
|
+
if (!options.spliceTxId) {
|
|
42
|
+
throw new SpliceSuccessorLineageError('successor lineage is required but no splice transaction id was supplied');
|
|
43
|
+
}
|
|
44
|
+
if (!chain.getTransaction) {
|
|
45
|
+
throw new SpliceSuccessorLineageError('chain adapter cannot look up transactions; splice successor lineage cannot be proven');
|
|
46
|
+
}
|
|
47
|
+
const transaction = await chain.getTransaction(options.spliceTxId);
|
|
48
|
+
if (!transaction) {
|
|
49
|
+
throw new SpliceSuccessorLineageError(`splice transaction ${options.spliceTxId} is not visible on chain`);
|
|
50
|
+
}
|
|
51
|
+
if (options.predecessorCoinId
|
|
52
|
+
&& !transaction.inputs.some((input) => sameCoinId(input.coinId, options.predecessorCoinId))) {
|
|
53
|
+
throw new SpliceSuccessorLineageError(`splice transaction ${options.spliceTxId} does not spend predecessor ${options.predecessorCoinId}`);
|
|
54
|
+
}
|
|
55
|
+
const matchedByCoinId = transaction.outputs.some((output) => output.coinId !== undefined && sameCoinId(output.coinId, coinId));
|
|
56
|
+
if (matchedByCoinId)
|
|
57
|
+
return;
|
|
58
|
+
// Nodes that omit output coin ids: accept exactly one output that pays the
|
|
59
|
+
// successor address, token and amount. More than one is ambiguous.
|
|
60
|
+
const matches = transaction.outputs.filter((output) => sameText(output.address, options.address)
|
|
61
|
+
&& sameText(output.tokenId, options.tokenId)
|
|
62
|
+
&& coinAmountAtoms({ amount: output.amount }) === options.amount);
|
|
63
|
+
if (matches.length !== 1) {
|
|
64
|
+
throw new SpliceSuccessorLineageError(`successor ${coinId} is not an output of splice transaction ${options.spliceTxId}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
13
67
|
/**
|
|
14
68
|
* Find the successor coin at the channel address, excluding the spent old
|
|
15
69
|
* coin. A coin with `spent: true` is never a valid successor. Confirmation
|
|
@@ -43,7 +97,8 @@ export async function observeSpliceSuccessor(chain, options) {
|
|
|
43
97
|
const created = candidate.created !== undefined ? Number(candidate.created) : NaN;
|
|
44
98
|
const confirmations = Number.isFinite(created) && created > 0 ? Math.max(0, tip.block - created + 1) : 0;
|
|
45
99
|
if (confirmations >= required) {
|
|
46
|
-
|
|
100
|
+
await assertSuccessorLineage(chain, candidate.coinid, options);
|
|
101
|
+
return { coinId: candidate.coinid, confirmations, confirmed: true, lineageVerified: true, coin: candidate };
|
|
47
102
|
}
|
|
48
103
|
// Found but not deep enough; keep polling until the deadline.
|
|
49
104
|
}
|
|
@@ -52,7 +107,13 @@ export async function observeSpliceSuccessor(chain, options) {
|
|
|
52
107
|
const tip = await chain.getTip();
|
|
53
108
|
const created = candidate.created !== undefined ? Number(candidate.created) : NaN;
|
|
54
109
|
const confirmations = Number.isFinite(created) && created > 0 ? Math.max(0, tip.block - created + 1) : 0;
|
|
55
|
-
return {
|
|
110
|
+
return {
|
|
111
|
+
coinId: candidate.coinid,
|
|
112
|
+
confirmations,
|
|
113
|
+
confirmed: confirmations >= required,
|
|
114
|
+
lineageVerified: false,
|
|
115
|
+
coin: candidate,
|
|
116
|
+
};
|
|
56
117
|
}
|
|
57
118
|
return null;
|
|
58
119
|
}
|
|
@@ -70,15 +131,43 @@ export async function observeSpliceSuccessor(chain, options) {
|
|
|
70
131
|
export async function recoverInterruptedSplices(store, chain, observe, broadcast) {
|
|
71
132
|
const pending = await store.list();
|
|
72
133
|
const results = [];
|
|
134
|
+
/** A successor coin may settle exactly one pending splice. */
|
|
135
|
+
const claimed = new Set();
|
|
73
136
|
for (const record of pending) {
|
|
74
137
|
try {
|
|
75
|
-
const
|
|
138
|
+
const options = {
|
|
139
|
+
...observe,
|
|
140
|
+
...(record.expected
|
|
141
|
+
? {
|
|
142
|
+
address: record.expected.address,
|
|
143
|
+
tokenId: record.expected.tokenId,
|
|
144
|
+
amount: BigInt(record.expected.amount),
|
|
145
|
+
}
|
|
146
|
+
: {}),
|
|
147
|
+
timeoutMs: observe.timeoutMs ?? 0,
|
|
148
|
+
spliceTxId: record.txpowId,
|
|
149
|
+
// Verify automatically when the adapter can, and always when the caller
|
|
150
|
+
// asked for it (OMN-074). Recovery of an already-broadcast splice is the
|
|
151
|
+
// last chance to bind the successor to its transaction.
|
|
152
|
+
requireLineage: observe.requireLineage ?? typeof chain.getTransaction === 'function',
|
|
153
|
+
};
|
|
154
|
+
const observed = await observeSpliceSuccessor(chain, options);
|
|
155
|
+
if (observed) {
|
|
156
|
+
if (claimed.has(observed.coinId)) {
|
|
157
|
+
throw new Error(`successor coin ${observed.coinId} was already claimed by another pending splice`);
|
|
158
|
+
}
|
|
159
|
+
if (record.coinId && record.coinId !== observed.coinId) {
|
|
160
|
+
throw new Error(`observed successor ${observed.coinId} does not match the coin recorded for this splice (${record.coinId})`);
|
|
161
|
+
}
|
|
162
|
+
claimed.add(observed.coinId);
|
|
163
|
+
}
|
|
76
164
|
if (observed?.confirmed) {
|
|
77
165
|
await store.clear(record.spliceId);
|
|
78
166
|
results.push({
|
|
79
167
|
spliceId: record.spliceId,
|
|
80
168
|
status: 'active',
|
|
81
169
|
coinId: observed.coinId,
|
|
170
|
+
lineageVerified: observed.lineageVerified,
|
|
82
171
|
rebroadcast: false,
|
|
83
172
|
});
|
|
84
173
|
continue;
|
|
@@ -93,10 +182,14 @@ export async function recoverInterruptedSplices(store, chain, observe, broadcast
|
|
|
93
182
|
});
|
|
94
183
|
}
|
|
95
184
|
else {
|
|
185
|
+
if (observed && record.coinId !== observed.coinId) {
|
|
186
|
+
// Persist the observed successor so the next pass is bound to it.
|
|
187
|
+
await store.save({ ...record, coinId: observed.coinId });
|
|
188
|
+
}
|
|
96
189
|
results.push({
|
|
97
190
|
spliceId: record.spliceId,
|
|
98
|
-
status:
|
|
99
|
-
...(observed ? { coinId: observed.coinId } : {}),
|
|
191
|
+
status: 'funding_pending',
|
|
192
|
+
...(observed ? { coinId: observed.coinId, lineageVerified: observed.lineageVerified } : {}),
|
|
100
193
|
rebroadcast: true,
|
|
101
194
|
});
|
|
102
195
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totemsdk/omnia-splice",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Channel splicing for @totemsdk/omnia — resize Minima eltoo channels without a close+reopen cycle",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"types": "dist/cjs/index.d.ts",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"NOTICE"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@totemsdk/omnia": "^
|
|
29
|
-
"@totemsdk/core": "^1.6.
|
|
30
|
-
"@totemsdk/
|
|
31
|
-
"@totemsdk/tx-builder": "^0.3.4",
|
|
28
|
+
"@totemsdk/omnia": "^4.0.0",
|
|
29
|
+
"@totemsdk/core": "^1.6.1",
|
|
30
|
+
"@totemsdk/tx-builder": "^0.4.0",
|
|
32
31
|
"@totemsdk/wots-lease": "^1.0.3",
|
|
33
|
-
"@totemsdk/
|
|
32
|
+
"@totemsdk/txpow": "^1.1.1",
|
|
33
|
+
"@totemsdk/storage": "^0.2.3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/node": "^20.0.0",
|