@totemsdk/omnia-splice 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/coordination.d.ts +55 -0
- package/dist/cjs/coordination.js +137 -0
- package/dist/cjs/durable-splice-store.d.ts +58 -0
- package/dist/cjs/durable-splice-store.js +117 -0
- package/dist/cjs/errors.d.ts +22 -0
- package/dist/cjs/errors.js +54 -0
- package/dist/cjs/finalize.d.ts +92 -0
- package/dist/cjs/finalize.js +222 -0
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/quiesce.d.ts +57 -0
- package/dist/cjs/quiesce.js +67 -0
- package/dist/cjs/splice-tx.d.ts +38 -0
- package/dist/cjs/splice-tx.js +210 -0
- package/dist/cjs/splice.d.ts +71 -0
- package/dist/cjs/splice.js +303 -0
- package/dist/cjs/types.d.ts +130 -0
- package/dist/cjs/types.js +2 -0
- package/dist/coordination.d.ts +55 -0
- package/dist/coordination.js +134 -0
- package/dist/durable-splice-store.d.ts +58 -0
- package/dist/durable-splice-store.js +114 -0
- package/dist/errors.js +7 -17
- package/dist/finalize.d.ts +1 -1
- package/dist/finalize.js +26 -33
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -23
- package/dist/quiesce.js +7 -10
- package/dist/splice-tx.js +13 -18
- package/dist/splice.js +21 -26
- package/dist/types.js +1 -2
- package/package.json +19 -12
- package/rust/pkg/omnia_splice_wasm_bg.wasm +0 -0
- package/rust/pkg/package.json +1 -1
- package/rust/pkg-node/omnia_splice_wasm_bg.wasm +0 -0
- package/rust/pkg-node/package.json +1 -1
|
@@ -0,0 +1,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
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable splice-state store (RFC-007 G9).
|
|
3
|
+
*
|
|
4
|
+
* A splice is a two-party (or multi-party) op that spans propose → quiesce →
|
|
5
|
+
* accept → broadcast → finalize. If the host dies between "proposer signed
|
|
6
|
+
* `SpliceProposal`" and "acceptor co-signed `SpliceAcceptance`", the pending
|
|
7
|
+
* proposal — and the partial acceptance once made — must survive a restart so
|
|
8
|
+
* the partial splice commit is recoverable/reconcilable (never silently
|
|
9
|
+
* discarded or re-proposed with a fresh key slot).
|
|
10
|
+
*
|
|
11
|
+
* The store keeps one record per spliceId holding the proposal plus (once
|
|
12
|
+
* received) the acceptance. Backed by the shared revision-CAS snapshot store:
|
|
13
|
+
* corruption is surfaced (never treated as absence), no silent downgrade, and
|
|
14
|
+
* concurrent propose/accept transitions across splices are never lost.
|
|
15
|
+
*/
|
|
16
|
+
import type { StorageAdapterWithCapabilities, CasStore, WriteAckMode } from '@totemsdk/storage/types';
|
|
17
|
+
import type { SpliceProposal, SpliceAcceptance } from './types.js';
|
|
18
|
+
export type SpliceRecordStatus = 'pending' | 'accepted' | 'finalized';
|
|
19
|
+
/** One in-progress splice: proposal persisted first, acceptance added later. */
|
|
20
|
+
export interface SpliceRecord {
|
|
21
|
+
spliceId: string;
|
|
22
|
+
channelId: string;
|
|
23
|
+
proposal: SpliceProposal;
|
|
24
|
+
acceptance?: SpliceAcceptance;
|
|
25
|
+
status: SpliceRecordStatus;
|
|
26
|
+
updatedAt: number;
|
|
27
|
+
}
|
|
28
|
+
export interface SpliceStoreState {
|
|
29
|
+
/** spliceId → splice record. */
|
|
30
|
+
splices: Record<string, SpliceRecord>;
|
|
31
|
+
}
|
|
32
|
+
export interface DurableSpliceStoreOptions {
|
|
33
|
+
/** Key namespace prefix; default `totem_omnia_splice:v1:`. */
|
|
34
|
+
readonly namespace?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Required write acknowledgment; default `durably-acknowledged`. Pass
|
|
37
|
+
* `volatile` only for tests/scratch adapters (e.g. `MemoryStore`).
|
|
38
|
+
*/
|
|
39
|
+
readonly requireAckMode?: WriteAckMode;
|
|
40
|
+
}
|
|
41
|
+
export interface DurableSpliceStore {
|
|
42
|
+
/** Persist a freshly created splice proposal (pending, pre-acceptance). */
|
|
43
|
+
saveProposal(proposal: SpliceProposal): Promise<void>;
|
|
44
|
+
/** Attach an acceptance; updates status to `accepted`. */
|
|
45
|
+
saveAcceptance(acceptance: SpliceAcceptance): Promise<void>;
|
|
46
|
+
/** Retrieve a splice record by spliceId. */
|
|
47
|
+
getSplice(spliceId: string): Promise<SpliceRecord | undefined>;
|
|
48
|
+
/** All records, optionally filtered by channel. */
|
|
49
|
+
listSplices(channelId?: string): Promise<SpliceRecord[]>;
|
|
50
|
+
/** Pending (proposed but not yet accepted) splices — reconcilable after restart. */
|
|
51
|
+
listPending(): Promise<SpliceRecord[]>;
|
|
52
|
+
/** Mark a splice finalized (splice TX confirmed on-chain). */
|
|
53
|
+
markFinalized(spliceId: string): Promise<void>;
|
|
54
|
+
getRevision(): Promise<number>;
|
|
55
|
+
hasState(): Promise<boolean>;
|
|
56
|
+
getSnapshot(): Promise<SpliceStoreState>;
|
|
57
|
+
}
|
|
58
|
+
export declare function createDurableSpliceStore(adapter: StorageAdapterWithCapabilities & CasStore, options?: DurableSpliceStoreOptions): DurableSpliceStore;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable splice-state store (RFC-007 G9).
|
|
3
|
+
*
|
|
4
|
+
* A splice is a two-party (or multi-party) op that spans propose → quiesce →
|
|
5
|
+
* accept → broadcast → finalize. If the host dies between "proposer signed
|
|
6
|
+
* `SpliceProposal`" and "acceptor co-signed `SpliceAcceptance`", the pending
|
|
7
|
+
* proposal — and the partial acceptance once made — must survive a restart so
|
|
8
|
+
* the partial splice commit is recoverable/reconcilable (never silently
|
|
9
|
+
* discarded or re-proposed with a fresh key slot).
|
|
10
|
+
*
|
|
11
|
+
* The store keeps one record per spliceId holding the proposal plus (once
|
|
12
|
+
* received) the acceptance. Backed by the shared revision-CAS snapshot store:
|
|
13
|
+
* corruption is surfaced (never treated as absence), no silent downgrade, and
|
|
14
|
+
* concurrent propose/accept transitions across splices are never lost.
|
|
15
|
+
*/
|
|
16
|
+
import { createRevisionedSnapshotStore, } from '@totemsdk/storage/snapshot';
|
|
17
|
+
import { StorageError } from '@totemsdk/storage/errors';
|
|
18
|
+
const DEFAULT_NAMESPACE = 'totem_omnia_splice:v1:';
|
|
19
|
+
function assertSpliceState(state) {
|
|
20
|
+
if (typeof state !== 'object' || state === null || Array.isArray(state)) {
|
|
21
|
+
throw new StorageError('omnia-splice store state is not an object', 'corrupt');
|
|
22
|
+
}
|
|
23
|
+
const value = state.splices;
|
|
24
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
25
|
+
throw new StorageError('omnia-splice store state is missing section "splices"', 'corrupt');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function createDurableSpliceStore(adapter, options = {}) {
|
|
29
|
+
const snapshots = createRevisionedSnapshotStore(adapter, {
|
|
30
|
+
namespace: options.namespace ?? DEFAULT_NAMESPACE,
|
|
31
|
+
requireAckMode: options.requireAckMode,
|
|
32
|
+
empty: () => ({ splices: {} }),
|
|
33
|
+
validate: assertSpliceState,
|
|
34
|
+
});
|
|
35
|
+
async function mutate(fn) {
|
|
36
|
+
await snapshots.mutate(fn);
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
async saveProposal(proposal) {
|
|
40
|
+
await mutate((state) => {
|
|
41
|
+
const existing = state.splices[proposal.spliceId];
|
|
42
|
+
// A duplicate proposal for the same spliceId never overrides the first.
|
|
43
|
+
if (existing)
|
|
44
|
+
return state;
|
|
45
|
+
return {
|
|
46
|
+
...state,
|
|
47
|
+
splices: {
|
|
48
|
+
...state.splices,
|
|
49
|
+
[proposal.spliceId]: {
|
|
50
|
+
spliceId: proposal.spliceId,
|
|
51
|
+
channelId: proposal.channelId,
|
|
52
|
+
proposal,
|
|
53
|
+
status: 'pending',
|
|
54
|
+
updatedAt: Date.now(),
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
async saveAcceptance(acceptance) {
|
|
61
|
+
await mutate((state) => {
|
|
62
|
+
const existing = state.splices[acceptance.spliceId];
|
|
63
|
+
if (!existing) {
|
|
64
|
+
throw new StorageError(`acceptance for unknown splice ${acceptance.spliceId}`, 'write-failed', { key: acceptance.spliceId });
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
...state,
|
|
68
|
+
splices: {
|
|
69
|
+
...state.splices,
|
|
70
|
+
[acceptance.spliceId]: {
|
|
71
|
+
...existing,
|
|
72
|
+
acceptance,
|
|
73
|
+
status: 'accepted',
|
|
74
|
+
updatedAt: Date.now(),
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
async getSplice(spliceId) {
|
|
81
|
+
return (await snapshots.load()).splices[spliceId];
|
|
82
|
+
},
|
|
83
|
+
async listSplices(channelId) {
|
|
84
|
+
const state = await snapshots.load();
|
|
85
|
+
const all = Object.values(state.splices);
|
|
86
|
+
if (channelId === undefined)
|
|
87
|
+
return all;
|
|
88
|
+
return all.filter((s) => s.channelId === channelId);
|
|
89
|
+
},
|
|
90
|
+
async listPending() {
|
|
91
|
+
const state = await snapshots.load();
|
|
92
|
+
return Object.values(state.splices).filter((s) => s.status === 'pending');
|
|
93
|
+
},
|
|
94
|
+
async markFinalized(spliceId) {
|
|
95
|
+
await mutate((state) => {
|
|
96
|
+
const existing = state.splices[spliceId];
|
|
97
|
+
if (!existing)
|
|
98
|
+
return state;
|
|
99
|
+
return {
|
|
100
|
+
...state,
|
|
101
|
+
splices: {
|
|
102
|
+
...state.splices,
|
|
103
|
+
[spliceId]: { ...existing, status: 'finalized', updatedAt: Date.now() },
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
getRevision: () => snapshots.getRevision(),
|
|
109
|
+
hasState: () => snapshots.hasState(),
|
|
110
|
+
async getSnapshot() {
|
|
111
|
+
return snapshots.load();
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
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
|
@@ -4,3 +4,6 @@ export { proposeSpliceIn, proposeSpliceOut, acceptSplice } from './splice.js';
|
|
|
4
4
|
export { finalizeSplice, type FinalizeSpliceOptions } from './finalize.js';
|
|
5
5
|
export type { SpliceType, SpliceParams, SpliceTxDraft, SpliceTxInput, SpliceTxOutput, SpliceProposal, SpliceAcceptance, QuiescedChannel, SplicedChannel, SpliceLeaseProvider, SpliceSigningIndices, WotsSignature, } from './types.js';
|
|
6
6
|
export { SpliceError, PendingHTLCError, SpliceChannelStatusError, SpliceBalanceConservationError, SpliceSignatureMismatchError, SpliceMissingPartyError, SpliceInsufficientFundsError, } from './errors.js';
|
|
7
|
+
export { createDurableSpliceStore, } from './durable-splice-store.js';
|
|
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,23 +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; } });
|
|
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
|
}
|