@totemsdk/omnia-splice 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/coordination.d.ts +55 -0
- package/dist/cjs/coordination.js +137 -0
- package/dist/cjs/durable-splice-store.d.ts +58 -0
- package/dist/cjs/durable-splice-store.js +117 -0
- package/dist/cjs/errors.d.ts +22 -0
- package/dist/cjs/errors.js +54 -0
- package/dist/cjs/finalize.d.ts +92 -0
- package/dist/cjs/finalize.js +222 -0
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/quiesce.d.ts +57 -0
- package/dist/cjs/quiesce.js +67 -0
- package/dist/cjs/splice-tx.d.ts +38 -0
- package/dist/cjs/splice-tx.js +210 -0
- package/dist/cjs/splice.d.ts +71 -0
- package/dist/cjs/splice.js +303 -0
- package/dist/cjs/types.d.ts +130 -0
- package/dist/cjs/types.js +2 -0
- package/dist/coordination.d.ts +55 -0
- package/dist/coordination.js +134 -0
- package/dist/durable-splice-store.js +7 -10
- package/dist/errors.js +7 -17
- package/dist/finalize.d.ts +1 -1
- package/dist/finalize.js +26 -33
- package/dist/index.d.ts +1 -0
- package/dist/index.js +8 -25
- package/dist/quiesce.js +7 -10
- package/dist/splice-tx.js +13 -18
- package/dist/splice.js +21 -26
- package/dist/types.js +1 -2
- package/package.json +14 -8
- package/rust/pkg/omnia_splice_wasm_bg.wasm +0 -0
- package/rust/pkg/package.json +1 -1
- package/rust/pkg-node/omnia_splice_wasm_bg.wasm +0 -0
- package/rust/pkg-node/package.json +1 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splice peer round-trip (RFC-035 B1).
|
|
3
|
+
*
|
|
4
|
+
* Two-party splice over the coordination transport:
|
|
5
|
+
*
|
|
6
|
+
* proposer ── SPLICE_PROPOSAL ─▶ acceptor
|
|
7
|
+
* proposer ◀─ SPLICE_ACCEPT ── acceptor (acceptor reserves a WOTS leaf)
|
|
8
|
+
* proposer ── SPLICE_FINALIZED ─▶ acceptor (acceptor commits its leaf)
|
|
9
|
+
*
|
|
10
|
+
* Verify-before-sign: the acceptor cryptographically verifies the proposer's
|
|
11
|
+
* signature over the splice digest (and `acceptSplice` binds the draft to the
|
|
12
|
+
* local channel) before it signs. Each party commits/burns its **own** lease:
|
|
13
|
+
* the proposer on `finalizeSplice`, the acceptor on `SPLICE_FINALIZED` (or burns
|
|
14
|
+
* it when no confirmation arrives within the TTL).
|
|
15
|
+
*/
|
|
16
|
+
import { verifyChannelSignature } from '@totemsdk/omnia';
|
|
17
|
+
import { proposeSpliceIn, proposeSpliceOut, acceptSplice } from './splice.js';
|
|
18
|
+
import { finalizeSplice } from './finalize.js';
|
|
19
|
+
import { quiesceChannel } from './quiesce.js';
|
|
20
|
+
import { computeSpliceTxDigest } from './splice-tx.js';
|
|
21
|
+
const DEFAULT_TTL_MS = 24 * 60 * 60 * 1000;
|
|
22
|
+
async function ensureQuiesced(channel, leaseProvider) {
|
|
23
|
+
return channel.status === 'quiesced'
|
|
24
|
+
? channel
|
|
25
|
+
: quiesceChannel(channel, leaseProvider);
|
|
26
|
+
}
|
|
27
|
+
export function createSpliceCoordination(deps) {
|
|
28
|
+
const { channels, leaseProvider, approval, onFinalized } = deps;
|
|
29
|
+
const verify = deps.verifySignature ?? ((sig, digest, pkd) => verifyChannelSignature(sig, digest, pkd));
|
|
30
|
+
const finalizeOptions = {
|
|
31
|
+
...deps.finalizeOptions,
|
|
32
|
+
verifySignature: deps.finalizeOptions?.verifySignature ?? verify,
|
|
33
|
+
};
|
|
34
|
+
const ttlMs = deps.ttlMs ?? DEFAULT_TTL_MS;
|
|
35
|
+
// Acceptor-side reservations awaiting confirmation (commit) or TTL (burn).
|
|
36
|
+
const pendingAcceptances = new Map();
|
|
37
|
+
const burnTimers = new Map();
|
|
38
|
+
const burnPending = (spliceId, reason) => {
|
|
39
|
+
const pending = pendingAcceptances.get(spliceId);
|
|
40
|
+
if (!pending)
|
|
41
|
+
return;
|
|
42
|
+
pendingAcceptances.delete(spliceId);
|
|
43
|
+
const timer = burnTimers.get(spliceId);
|
|
44
|
+
if (timer) {
|
|
45
|
+
clearTimeout(timer);
|
|
46
|
+
burnTimers.delete(spliceId);
|
|
47
|
+
}
|
|
48
|
+
void leaseProvider.wotsLease.burnReservation(pending.acceptorReservationId, reason).catch(() => { });
|
|
49
|
+
};
|
|
50
|
+
const handleProposal = async (ctx) => {
|
|
51
|
+
const proposal = ctx.message.payload;
|
|
52
|
+
const channel = channels.get(proposal.channelId);
|
|
53
|
+
if (!channel)
|
|
54
|
+
throw new Error(`splice proposal for unknown channel ${proposal.channelId}`);
|
|
55
|
+
// Verify the proposer's signature BEFORE producing ours (RFC-035 §6.5.2).
|
|
56
|
+
const digest = computeSpliceTxDigest(proposal.spliceTxDraft);
|
|
57
|
+
if (!verify(proposal.proposerSignature, digest, proposal.proposerPublicKeyDigest)) {
|
|
58
|
+
throw new Error('splice proposal: proposer signature failed verification');
|
|
59
|
+
}
|
|
60
|
+
if (!ctx.policy.autoAccept) {
|
|
61
|
+
const approved = approval
|
|
62
|
+
? await approval({
|
|
63
|
+
channelId: proposal.channelId,
|
|
64
|
+
spliceId: proposal.spliceId,
|
|
65
|
+
proposer: proposal.proposerPublicKeyDigest,
|
|
66
|
+
params: proposal.params,
|
|
67
|
+
})
|
|
68
|
+
: false;
|
|
69
|
+
if (!approved)
|
|
70
|
+
throw new Error('splice acceptance rejected: approval required');
|
|
71
|
+
}
|
|
72
|
+
// acceptSplice binds the draft to our channel + validates params, then signs.
|
|
73
|
+
const local = await ensureQuiesced(channel, leaseProvider);
|
|
74
|
+
channels.set(local.channelId, local);
|
|
75
|
+
const acceptance = await acceptSplice(local, proposal, leaseProvider);
|
|
76
|
+
pendingAcceptances.set(proposal.spliceId, acceptance);
|
|
77
|
+
burnTimers.set(proposal.spliceId, setTimeout(() => burnPending(proposal.spliceId, 'splice-finalize-timeout'), ttlMs));
|
|
78
|
+
await ctx.send('SPLICE_ACCEPT', acceptance);
|
|
79
|
+
};
|
|
80
|
+
const handleAccept = async (ctx) => {
|
|
81
|
+
const acceptance = ctx.message.payload;
|
|
82
|
+
const record = await ctx.store.getProposal(ctx.correlationId);
|
|
83
|
+
if (!record)
|
|
84
|
+
throw new Error(`no stored splice proposal for ${ctx.correlationId}`);
|
|
85
|
+
const proposal = record.payload;
|
|
86
|
+
const channel = channels.get(proposal.channelId);
|
|
87
|
+
if (!channel)
|
|
88
|
+
throw new Error(`splice acceptance for unknown channel ${proposal.channelId}`);
|
|
89
|
+
// finalizeSplice verifies both signatures + draft binding, then commits the
|
|
90
|
+
// proposer's OWN reservation.
|
|
91
|
+
const spliced = await finalizeSplice(channel, proposal, acceptance, {
|
|
92
|
+
...finalizeOptions,
|
|
93
|
+
proposerLeaseProvider: leaseProvider.wotsLease,
|
|
94
|
+
});
|
|
95
|
+
channels.set(spliced.channelId, spliced);
|
|
96
|
+
await ctx.store.setProposalState(ctx.correlationId, 'committed');
|
|
97
|
+
onFinalized?.(spliced, ctx.peer);
|
|
98
|
+
await ctx.send('SPLICE_FINALIZED', { spliceId: proposal.spliceId });
|
|
99
|
+
};
|
|
100
|
+
const handleFinalized = async (ctx) => {
|
|
101
|
+
const spliceId = ctx.message.payload?.spliceId ?? ctx.correlationId;
|
|
102
|
+
const pending = pendingAcceptances.get(spliceId);
|
|
103
|
+
if (!pending)
|
|
104
|
+
return;
|
|
105
|
+
pendingAcceptances.delete(spliceId);
|
|
106
|
+
const timer = burnTimers.get(spliceId);
|
|
107
|
+
if (timer) {
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
burnTimers.delete(spliceId);
|
|
110
|
+
}
|
|
111
|
+
await leaseProvider.wotsLease.commitKeyUse(pending.acceptorReservationId, `splice-finalized:${spliceId}`);
|
|
112
|
+
};
|
|
113
|
+
const handlers = {
|
|
114
|
+
SPLICE_PROPOSAL: handleProposal,
|
|
115
|
+
SPLICE_ACCEPT: handleAccept,
|
|
116
|
+
SPLICE_FINALIZED: handleFinalized,
|
|
117
|
+
};
|
|
118
|
+
const propose = async (peer, coordination, channel, params) => {
|
|
119
|
+
const local = await ensureQuiesced(channel, leaseProvider);
|
|
120
|
+
channels.set(local.channelId, local);
|
|
121
|
+
const proposal = params.type === 'in'
|
|
122
|
+
? await proposeSpliceIn(local, params.additionalCoinId, params.additionalAmount, leaseProvider)
|
|
123
|
+
: await proposeSpliceOut(local, params.withdrawAmount, params.withdrawAddress, leaseProvider);
|
|
124
|
+
await coordination.sendProposal({
|
|
125
|
+
peer,
|
|
126
|
+
kind: 'SPLICE_PROPOSAL',
|
|
127
|
+
correlationId: proposal.spliceId,
|
|
128
|
+
channelId: proposal.channelId,
|
|
129
|
+
payload: proposal,
|
|
130
|
+
});
|
|
131
|
+
return proposal;
|
|
132
|
+
};
|
|
133
|
+
return { handlers, propose };
|
|
134
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Durable splice-state store (RFC-007 G9).
|
|
4
3
|
*
|
|
@@ -14,22 +13,20 @@
|
|
|
14
13
|
* corruption is surfaced (never treated as absence), no silent downgrade, and
|
|
15
14
|
* concurrent propose/accept transitions across splices are never lost.
|
|
16
15
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const snapshot_1 = require("@totemsdk/storage/snapshot");
|
|
20
|
-
const errors_1 = require("@totemsdk/storage/errors");
|
|
16
|
+
import { createRevisionedSnapshotStore, } from '@totemsdk/storage/snapshot';
|
|
17
|
+
import { StorageError } from '@totemsdk/storage/errors';
|
|
21
18
|
const DEFAULT_NAMESPACE = 'totem_omnia_splice:v1:';
|
|
22
19
|
function assertSpliceState(state) {
|
|
23
20
|
if (typeof state !== 'object' || state === null || Array.isArray(state)) {
|
|
24
|
-
throw new
|
|
21
|
+
throw new StorageError('omnia-splice store state is not an object', 'corrupt');
|
|
25
22
|
}
|
|
26
23
|
const value = state.splices;
|
|
27
24
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
28
|
-
throw new
|
|
25
|
+
throw new StorageError('omnia-splice store state is missing section "splices"', 'corrupt');
|
|
29
26
|
}
|
|
30
27
|
}
|
|
31
|
-
function createDurableSpliceStore(adapter, options = {}) {
|
|
32
|
-
const snapshots =
|
|
28
|
+
export function createDurableSpliceStore(adapter, options = {}) {
|
|
29
|
+
const snapshots = createRevisionedSnapshotStore(adapter, {
|
|
33
30
|
namespace: options.namespace ?? DEFAULT_NAMESPACE,
|
|
34
31
|
requireAckMode: options.requireAckMode,
|
|
35
32
|
empty: () => ({ splices: {} }),
|
|
@@ -64,7 +61,7 @@ function createDurableSpliceStore(adapter, options = {}) {
|
|
|
64
61
|
await mutate((state) => {
|
|
65
62
|
const existing = state.splices[acceptance.spliceId];
|
|
66
63
|
if (!existing) {
|
|
67
|
-
throw new
|
|
64
|
+
throw new StorageError(`acceptance for unknown splice ${acceptance.spliceId}`, 'write-failed', { key: acceptance.spliceId });
|
|
68
65
|
}
|
|
69
66
|
return {
|
|
70
67
|
...state,
|
package/dist/errors.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SpliceInsufficientFundsError = exports.SpliceMissingPartyError = exports.SpliceSignatureMismatchError = exports.SpliceBalanceConservationError = exports.SpliceChannelStatusError = exports.PendingHTLCError = exports.SpliceError = void 0;
|
|
4
|
-
class SpliceError extends Error {
|
|
1
|
+
export class SpliceError extends Error {
|
|
5
2
|
constructor(code, message) {
|
|
6
3
|
super(message);
|
|
7
4
|
this.name = 'SpliceError';
|
|
@@ -9,46 +6,39 @@ class SpliceError extends Error {
|
|
|
9
6
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
10
7
|
}
|
|
11
8
|
}
|
|
12
|
-
|
|
13
|
-
class PendingHTLCError extends SpliceError {
|
|
9
|
+
export class PendingHTLCError extends SpliceError {
|
|
14
10
|
constructor(pendingCount) {
|
|
15
11
|
super('PENDING_HTLC', `Cannot splice: ${pendingCount} HTLC(s) still pending. Fulfill or timeout all HTLCs before splicing.`);
|
|
16
12
|
this.name = 'PendingHTLCError';
|
|
17
13
|
}
|
|
18
14
|
}
|
|
19
|
-
|
|
20
|
-
class SpliceChannelStatusError extends SpliceError {
|
|
15
|
+
export class SpliceChannelStatusError extends SpliceError {
|
|
21
16
|
constructor(required, actual) {
|
|
22
17
|
super('INVALID_CHANNEL_STATUS', `Splice requires channel status '${required}', got '${actual}'.`);
|
|
23
18
|
this.name = 'SpliceChannelStatusError';
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
|
-
|
|
27
|
-
class SpliceBalanceConservationError extends SpliceError {
|
|
21
|
+
export class SpliceBalanceConservationError extends SpliceError {
|
|
28
22
|
constructor(expected, actual) {
|
|
29
23
|
super('BALANCE_CONSERVATION', `Splice balance conservation failed: expected ${expected}, got ${actual}.`);
|
|
30
24
|
this.name = 'SpliceBalanceConservationError';
|
|
31
25
|
}
|
|
32
26
|
}
|
|
33
|
-
|
|
34
|
-
class SpliceSignatureMismatchError extends SpliceError {
|
|
27
|
+
export class SpliceSignatureMismatchError extends SpliceError {
|
|
35
28
|
constructor(detail) {
|
|
36
29
|
super('SIGNATURE_MISMATCH', `Splice signature mismatch: ${detail}`);
|
|
37
30
|
this.name = 'SpliceSignatureMismatchError';
|
|
38
31
|
}
|
|
39
32
|
}
|
|
40
|
-
|
|
41
|
-
class SpliceMissingPartyError extends SpliceError {
|
|
33
|
+
export class SpliceMissingPartyError extends SpliceError {
|
|
42
34
|
constructor(partyId) {
|
|
43
35
|
super('MISSING_PARTY', `Party '${partyId}' not found in channel.`);
|
|
44
36
|
this.name = 'SpliceMissingPartyError';
|
|
45
37
|
}
|
|
46
38
|
}
|
|
47
|
-
|
|
48
|
-
class SpliceInsufficientFundsError extends SpliceError {
|
|
39
|
+
export class SpliceInsufficientFundsError extends SpliceError {
|
|
49
40
|
constructor(available, requested) {
|
|
50
41
|
super('INSUFFICIENT_FUNDS', `Splice-out withdraws ${requested} but channel only holds ${available}.`);
|
|
51
42
|
this.name = 'SpliceInsufficientFundsError';
|
|
52
43
|
}
|
|
53
44
|
}
|
|
54
|
-
exports.SpliceInsufficientFundsError = SpliceInsufficientFundsError;
|
package/dist/finalize.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ import type { SpliceProposal, SpliceAcceptance, SplicedChannel, QuiescedChannel
|
|
|
10
10
|
* @property mineDifficulty - Override PoW difficulty (pass `MAX_HASH` in tests).
|
|
11
11
|
* @property verifySignature - Custom signature verifier called for both the
|
|
12
12
|
* proposer and acceptor signatures before state
|
|
13
|
-
* transition. Defaults to `
|
|
13
|
+
* transition. Defaults to `verifyChannelSignature`.
|
|
14
14
|
* **Override in test environments** that use mock
|
|
15
15
|
* signers (mock signatures are not real WOTS sigs
|
|
16
16
|
* and will not pass the default verifier).
|
package/dist/finalize.js
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const splice_tx_js_1 = require("./splice-tx.js");
|
|
7
|
-
const errors_js_1 = require("./errors.js");
|
|
1
|
+
import { concatBytes } from '@totemsdk/core';
|
|
2
|
+
import { serializeTxBody, mineTxPoW, TX_POW_MIN_DIFFICULTY } from '@totemsdk/txpow';
|
|
3
|
+
import { verifyChannelSignature } from '@totemsdk/omnia';
|
|
4
|
+
import { buildSpliceTx, computeSpliceTxDigest, spliceDraftToMinimaBytes } from './splice-tx.js';
|
|
5
|
+
import { SpliceSignatureMismatchError, SpliceMissingPartyError, SpliceChannelStatusError, } from './errors.js';
|
|
8
6
|
function defaultWotsVerify(sig, digest, pkd) {
|
|
9
|
-
|
|
10
|
-
return (0, core_1.wotsVerifyDigest)(sig, digest, (0, core_1.hexToBytes)(pkd));
|
|
11
|
-
}
|
|
12
|
-
catch {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
7
|
+
return verifyChannelSignature(sig, digest, pkd);
|
|
15
8
|
}
|
|
16
9
|
function assembleSpliceWitness(sig1, sig2) {
|
|
17
10
|
const encodeMiniNumber = (n) => {
|
|
@@ -22,9 +15,9 @@ function assembleSpliceWitness(sig1, sig2) {
|
|
|
22
15
|
const encodeVarBytes = (b) => {
|
|
23
16
|
const lenBuf = new Uint8Array(4);
|
|
24
17
|
new DataView(lenBuf.buffer).setInt32(0, b.length, false);
|
|
25
|
-
return
|
|
18
|
+
return concatBytes(lenBuf, b);
|
|
26
19
|
};
|
|
27
|
-
return
|
|
20
|
+
return concatBytes(concatBytes(concatBytes(concatBytes(encodeMiniNumber(2), encodeVarBytes(sig1)), encodeVarBytes(sig2)), encodeMiniNumber(0)), encodeMiniNumber(0));
|
|
28
21
|
}
|
|
29
22
|
/**
|
|
30
23
|
* Structural authorization checks: both parties must be distinct channel
|
|
@@ -35,18 +28,18 @@ function assembleSpliceWitness(sig1, sig2) {
|
|
|
35
28
|
function validateSpliceAuthorization(channel, proposal, acceptance) {
|
|
36
29
|
const proposerParty = channel.parties.find(p => p.publicKeyDigest === proposal.proposerPublicKeyDigest);
|
|
37
30
|
if (!proposerParty)
|
|
38
|
-
throw new
|
|
31
|
+
throw new SpliceMissingPartyError(proposal.proposerPublicKeyDigest);
|
|
39
32
|
const acceptorParty = channel.parties.find(p => p.publicKeyDigest === acceptance.acceptorPublicKeyDigest);
|
|
40
33
|
if (!acceptorParty)
|
|
41
|
-
throw new
|
|
34
|
+
throw new SpliceMissingPartyError(acceptance.acceptorPublicKeyDigest);
|
|
42
35
|
if (proposal.proposerPublicKeyDigest === acceptance.acceptorPublicKeyDigest) {
|
|
43
36
|
throw new Error('Proposer and acceptor must be different channel parties');
|
|
44
37
|
}
|
|
45
38
|
if (!proposal.proposerSignature || proposal.proposerSignature.length === 0) {
|
|
46
|
-
throw new
|
|
39
|
+
throw new SpliceSignatureMismatchError('Proposer signature is empty');
|
|
47
40
|
}
|
|
48
41
|
if (!acceptance.acceptorSignature || acceptance.acceptorSignature.length === 0) {
|
|
49
|
-
throw new
|
|
42
|
+
throw new SpliceSignatureMismatchError('Acceptor signature is empty');
|
|
50
43
|
}
|
|
51
44
|
}
|
|
52
45
|
/**
|
|
@@ -58,12 +51,12 @@ function validateSpliceAuthorization(channel, proposal, acceptance) {
|
|
|
58
51
|
* by partyId), so signatures are bound to the exact per-party balance split.
|
|
59
52
|
*/
|
|
60
53
|
function verifySpliceSignatures(proposal, acceptance, verifier) {
|
|
61
|
-
const digest =
|
|
54
|
+
const digest = computeSpliceTxDigest(proposal.spliceTxDraft);
|
|
62
55
|
if (!verifier(proposal.proposerSignature, digest, proposal.proposerPublicKeyDigest)) {
|
|
63
|
-
throw new
|
|
56
|
+
throw new SpliceSignatureMismatchError('Proposer signature failed cryptographic verification — digest or key mismatch');
|
|
64
57
|
}
|
|
65
58
|
if (!verifier(acceptance.acceptorSignature, digest, acceptance.acceptorPublicKeyDigest)) {
|
|
66
|
-
throw new
|
|
59
|
+
throw new SpliceSignatureMismatchError('Acceptor signature failed cryptographic verification — digest or key mismatch');
|
|
67
60
|
}
|
|
68
61
|
}
|
|
69
62
|
/**
|
|
@@ -75,9 +68,9 @@ function verifySpliceSignatures(proposal, acceptance, verifier) {
|
|
|
75
68
|
* inconsistent, the new channel state would not match what was actually mined.
|
|
76
69
|
*/
|
|
77
70
|
function bindFinalizeParamsToDraft(channel, proposal) {
|
|
78
|
-
const recomputed =
|
|
79
|
-
const expected =
|
|
80
|
-
const actual =
|
|
71
|
+
const recomputed = buildSpliceTx(channel, proposal.params);
|
|
72
|
+
const expected = computeSpliceTxDigest(recomputed);
|
|
73
|
+
const actual = computeSpliceTxDigest(proposal.spliceTxDraft);
|
|
81
74
|
if (!expected.every((b, i) => b === actual[i])) {
|
|
82
75
|
throw new Error('finalizeSplice: proposal.spliceTxDraft is inconsistent with proposal.params — ' +
|
|
83
76
|
'rejecting to prevent on-chain/off-chain state divergence');
|
|
@@ -129,12 +122,12 @@ function bindFinalizeParamsToDraft(channel, proposal) {
|
|
|
129
122
|
* @param options - Broadcast, difficulty, verifier, and lease providers.
|
|
130
123
|
* @returns SplicedChannel (`status: 'active'`) with updated value and provenance.
|
|
131
124
|
*/
|
|
132
|
-
async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
125
|
+
export async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
133
126
|
if (channel.status !== 'quiesced') {
|
|
134
|
-
throw new
|
|
127
|
+
throw new SpliceChannelStatusError('quiesced', channel.status);
|
|
135
128
|
}
|
|
136
129
|
if (proposal.spliceId !== acceptance.spliceId) {
|
|
137
|
-
throw new
|
|
130
|
+
throw new SpliceSignatureMismatchError(`proposal spliceId '${proposal.spliceId}' !== acceptance spliceId '${acceptance.spliceId}'`);
|
|
138
131
|
}
|
|
139
132
|
if (proposal.channelId !== channel.channelId) {
|
|
140
133
|
throw new Error(`proposal.channelId '${proposal.channelId}' does not match channel '${channel.channelId}'`);
|
|
@@ -150,14 +143,14 @@ async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
150
143
|
const acceptorLease = options?.acceptorLeaseProvider;
|
|
151
144
|
// Track which reservations have been committed so we know which to burn on failure.
|
|
152
145
|
const committed = new Set();
|
|
153
|
-
const txBytes =
|
|
146
|
+
const txBytes = spliceDraftToMinimaBytes(proposal.spliceTxDraft);
|
|
154
147
|
const witnessBytes = assembleSpliceWitness(proposal.proposerSignature, acceptance.acceptorSignature);
|
|
155
148
|
let spliceFundingTxId;
|
|
156
149
|
try {
|
|
157
|
-
const difficulty = options?.mineDifficulty ??
|
|
158
|
-
const txBody =
|
|
159
|
-
const mined = await
|
|
160
|
-
const fullTxPoW =
|
|
150
|
+
const difficulty = options?.mineDifficulty ?? TX_POW_MIN_DIFFICULTY;
|
|
151
|
+
const txBody = serializeTxBody(txBytes, witnessBytes);
|
|
152
|
+
const mined = await mineTxPoW(txBody, difficulty);
|
|
153
|
+
const fullTxPoW = concatBytes(concatBytes(mined.minedHeaderBytes, new Uint8Array([0x01])), txBody);
|
|
161
154
|
const txHex = Buffer.from(fullTxPoW).toString('hex');
|
|
162
155
|
spliceFundingTxId = Buffer.from(mined.txpowId).toString('hex');
|
|
163
156
|
if (options?.broadcast) {
|
package/dist/index.d.ts
CHANGED
|
@@ -6,3 +6,4 @@ export type { SpliceType, SpliceParams, SpliceTxDraft, SpliceTxInput, SpliceTxOu
|
|
|
6
6
|
export { SpliceError, PendingHTLCError, SpliceChannelStatusError, SpliceBalanceConservationError, SpliceSignatureMismatchError, SpliceMissingPartyError, SpliceInsufficientFundsError, } from './errors.js';
|
|
7
7
|
export { createDurableSpliceStore, } from './durable-splice-store.js';
|
|
8
8
|
export type { SpliceRecord, SpliceRecordStatus, SpliceStoreState, DurableSpliceStore, DurableSpliceStoreOptions, } from './durable-splice-store.js';
|
|
9
|
+
export { createSpliceCoordination, type SpliceCoordination, type SpliceCoordinationDeps, type SpliceProposeParams, type SpliceApproval, type SpliceApprovalRequest, type VerifySignature, } from './coordination.js';
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Object.defineProperty(exports, "computeSpliceTxDigest", { enumerable: true, get: function () { return splice_tx_js_1.computeSpliceTxDigest; } });
|
|
10
|
-
var splice_js_1 = require("./splice.js");
|
|
11
|
-
Object.defineProperty(exports, "proposeSpliceIn", { enumerable: true, get: function () { return splice_js_1.proposeSpliceIn; } });
|
|
12
|
-
Object.defineProperty(exports, "proposeSpliceOut", { enumerable: true, get: function () { return splice_js_1.proposeSpliceOut; } });
|
|
13
|
-
Object.defineProperty(exports, "acceptSplice", { enumerable: true, get: function () { return splice_js_1.acceptSplice; } });
|
|
14
|
-
var finalize_js_1 = require("./finalize.js");
|
|
15
|
-
Object.defineProperty(exports, "finalizeSplice", { enumerable: true, get: function () { return finalize_js_1.finalizeSplice; } });
|
|
16
|
-
var errors_js_1 = require("./errors.js");
|
|
17
|
-
Object.defineProperty(exports, "SpliceError", { enumerable: true, get: function () { return errors_js_1.SpliceError; } });
|
|
18
|
-
Object.defineProperty(exports, "PendingHTLCError", { enumerable: true, get: function () { return errors_js_1.PendingHTLCError; } });
|
|
19
|
-
Object.defineProperty(exports, "SpliceChannelStatusError", { enumerable: true, get: function () { return errors_js_1.SpliceChannelStatusError; } });
|
|
20
|
-
Object.defineProperty(exports, "SpliceBalanceConservationError", { enumerable: true, get: function () { return errors_js_1.SpliceBalanceConservationError; } });
|
|
21
|
-
Object.defineProperty(exports, "SpliceSignatureMismatchError", { enumerable: true, get: function () { return errors_js_1.SpliceSignatureMismatchError; } });
|
|
22
|
-
Object.defineProperty(exports, "SpliceMissingPartyError", { enumerable: true, get: function () { return errors_js_1.SpliceMissingPartyError; } });
|
|
23
|
-
Object.defineProperty(exports, "SpliceInsufficientFundsError", { enumerable: true, get: function () { return errors_js_1.SpliceInsufficientFundsError; } });
|
|
24
|
-
var durable_splice_store_js_1 = require("./durable-splice-store.js");
|
|
25
|
-
Object.defineProperty(exports, "createDurableSpliceStore", { enumerable: true, get: function () { return durable_splice_store_js_1.createDurableSpliceStore; } });
|
|
1
|
+
export { quiesceChannel } from './quiesce.js';
|
|
2
|
+
export { buildSpliceTx, spliceDraftToMinimaBytes, computeSpliceTxDigest } from './splice-tx.js';
|
|
3
|
+
export { proposeSpliceIn, proposeSpliceOut, acceptSplice } from './splice.js';
|
|
4
|
+
export { finalizeSplice } from './finalize.js';
|
|
5
|
+
export { SpliceError, PendingHTLCError, SpliceChannelStatusError, SpliceBalanceConservationError, SpliceSignatureMismatchError, SpliceMissingPartyError, SpliceInsufficientFundsError, } from './errors.js';
|
|
6
|
+
export { createDurableSpliceStore, } from './durable-splice-store.js';
|
|
7
|
+
// RFC-035 B1 — splice peer round-trip over the coordination transport.
|
|
8
|
+
export { createSpliceCoordination, } from './coordination.js';
|
package/dist/quiesce.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.quiesceChannel = quiesceChannel;
|
|
4
|
-
const omnia_1 = require("@totemsdk/omnia");
|
|
5
|
-
const errors_js_1 = require("./errors.js");
|
|
1
|
+
import { updateState } from '@totemsdk/omnia';
|
|
2
|
+
import { PendingHTLCError, SpliceChannelStatusError } from './errors.js';
|
|
6
3
|
/**
|
|
7
4
|
* Quiesce a channel before splicing.
|
|
8
5
|
*
|
|
@@ -35,9 +32,9 @@ const errors_js_1 = require("./errors.js");
|
|
|
35
32
|
* @throws {SpliceChannelStatusError} If channel is not active.
|
|
36
33
|
* @throws {PendingHTLCError} If HTLCs remain pending after resolution.
|
|
37
34
|
*/
|
|
38
|
-
async function quiesceChannel(channel, leaseProvider, options) {
|
|
35
|
+
export async function quiesceChannel(channel, leaseProvider, options) {
|
|
39
36
|
if (channel.status !== 'active') {
|
|
40
|
-
throw new
|
|
37
|
+
throw new SpliceChannelStatusError('active', channel.status);
|
|
41
38
|
}
|
|
42
39
|
const pendingHTLCs = channel.pendingHTLCs.filter(h => h.status === 'pending');
|
|
43
40
|
if (pendingHTLCs.length > 0) {
|
|
@@ -45,14 +42,14 @@ async function quiesceChannel(channel, leaseProvider, options) {
|
|
|
45
42
|
await options.awaitResolution(pendingHTLCs);
|
|
46
43
|
const stillPending = channel.pendingHTLCs.filter(h => h.status === 'pending').length;
|
|
47
44
|
if (stillPending > 0) {
|
|
48
|
-
throw new
|
|
45
|
+
throw new PendingHTLCError(stillPending);
|
|
49
46
|
}
|
|
50
47
|
}
|
|
51
48
|
else {
|
|
52
|
-
throw new
|
|
49
|
+
throw new PendingHTLCError(pendingHTLCs.length);
|
|
53
50
|
}
|
|
54
51
|
}
|
|
55
|
-
const { channel: updatedChannel, signedState, error } = await
|
|
52
|
+
const { channel: updatedChannel, signedState, error } = await updateState(channel, { newBalances: channel.balances }, leaseProvider.wotsLease, leaseProvider.signer);
|
|
56
53
|
if (error) {
|
|
57
54
|
throw new Error(`quiesceChannel: updateState blocked — ${error}. Splice cannot proceed.`);
|
|
58
55
|
}
|
package/dist/splice-tx.js
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
exports.computeSpliceTxDigest = computeSpliceTxDigest;
|
|
6
|
-
const core_1 = require("@totemsdk/core");
|
|
7
|
-
const core_2 = require("@totemsdk/core");
|
|
8
|
-
const omnia_1 = require("@totemsdk/omnia");
|
|
9
|
-
const errors_js_1 = require("./errors.js");
|
|
1
|
+
import { sha3_256 } from '@totemsdk/core';
|
|
2
|
+
import { serializeTransaction, } from '@totemsdk/core';
|
|
3
|
+
import { COINID_ELTOO } from '@totemsdk/omnia';
|
|
4
|
+
import { SpliceBalanceConservationError, SpliceInsufficientFundsError } from './errors.js';
|
|
10
5
|
const STATE_SETTLEMENT_PORT = 100;
|
|
11
6
|
const STATE_SEQUENCE_PORT = 101;
|
|
12
7
|
/**
|
|
@@ -31,17 +26,17 @@ const STATE_SEQUENCE_PORT = 101;
|
|
|
31
26
|
* @throws {SpliceBalanceConservationError} If output amounts do not sum to newTotalValue.
|
|
32
27
|
* @throws {SpliceInsufficientFundsError} If splice-out exceeds channel holdings.
|
|
33
28
|
*/
|
|
34
|
-
function buildSpliceTx(channel, params) {
|
|
29
|
+
export function buildSpliceTx(channel, params) {
|
|
35
30
|
const tokenId = channel.tokenId ?? '0x00';
|
|
36
31
|
if (params.type === 'splice_out') {
|
|
37
32
|
const withdrawAmount = params.withdrawAmount ?? 0n;
|
|
38
33
|
const extraSum = (params.extraOutputs ?? []).reduce((a, o) => a + o.amount, 0n);
|
|
39
34
|
const totalOutflow = withdrawAmount + extraSum;
|
|
40
35
|
if (totalOutflow > channel.totalValue) {
|
|
41
|
-
throw new
|
|
36
|
+
throw new SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
|
|
42
37
|
}
|
|
43
38
|
if (params.newTotalValue < 0n) {
|
|
44
|
-
throw new
|
|
39
|
+
throw new SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
|
|
45
40
|
}
|
|
46
41
|
}
|
|
47
42
|
const balanceSum = Object.values(params.newBalances).reduce((a, b) => a + b, 0n);
|
|
@@ -50,11 +45,11 @@ function buildSpliceTx(channel, params) {
|
|
|
50
45
|
// Withdrawals and extra outputs are separate outputs, already factored out by the
|
|
51
46
|
// caller when computing `newTotalValue`.
|
|
52
47
|
if (balanceSum !== params.newTotalValue) {
|
|
53
|
-
throw new
|
|
48
|
+
throw new SpliceBalanceConservationError(params.newTotalValue, balanceSum);
|
|
54
49
|
}
|
|
55
50
|
const inputs = [
|
|
56
51
|
{
|
|
57
|
-
coinId:
|
|
52
|
+
coinId: COINID_ELTOO,
|
|
58
53
|
address: channel.fundingAddress,
|
|
59
54
|
amount: channel.totalValue,
|
|
60
55
|
tokenId,
|
|
@@ -112,7 +107,7 @@ function buildSpliceTx(channel, params) {
|
|
|
112
107
|
* suitable for WOTS signing. Both parties sign the same bytes independently;
|
|
113
108
|
* the final splice TX assembles both signatures into the witness.
|
|
114
109
|
*/
|
|
115
|
-
function spliceDraftToMinimaBytes(draft) {
|
|
110
|
+
export function spliceDraftToMinimaBytes(draft) {
|
|
116
111
|
const toHex = (b) => Buffer.from(b).toString('hex');
|
|
117
112
|
const svToJson = (port, value, type) => {
|
|
118
113
|
let data;
|
|
@@ -169,13 +164,13 @@ function spliceDraftToMinimaBytes(draft) {
|
|
|
169
164
|
svToJson(STATE_SETTLEMENT_PORT, false, 'bool'),
|
|
170
165
|
svToJson(STATE_SEQUENCE_PORT, 0n, 'number'),
|
|
171
166
|
];
|
|
172
|
-
return
|
|
167
|
+
return serializeTransaction(JSON.stringify({ inputs, outputs, state, linkhash: '0x00' }));
|
|
173
168
|
}
|
|
174
169
|
/**
|
|
175
170
|
* Compute a 32-byte digest over the splice TX draft for WOTS signing.
|
|
176
171
|
* This is the canonical message both parties sign to authorize the splice.
|
|
177
172
|
*/
|
|
178
|
-
function computeSpliceTxDigest(draft) {
|
|
173
|
+
export function computeSpliceTxDigest(draft) {
|
|
179
174
|
const canonical = JSON.stringify({
|
|
180
175
|
channelId: draft.channelId,
|
|
181
176
|
inputs: draft.inputs.map(i => ({
|
|
@@ -206,5 +201,5 @@ function computeSpliceTxDigest(draft) {
|
|
|
206
201
|
.map(([k, v]) => [k, v.toString()]),
|
|
207
202
|
},
|
|
208
203
|
});
|
|
209
|
-
return
|
|
204
|
+
return sha3_256(new TextEncoder().encode(canonical));
|
|
210
205
|
}
|