@totemsdk/omnia-splice 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -21
- package/NOTICE +60 -0
- 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 +10 -0
- package/dist/cjs/index.js +31 -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 +95 -0
- package/dist/cjs/splice.js +377 -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 +3 -1
- package/dist/index.js +8 -25
- package/dist/quiesce.js +7 -10
- package/dist/splice-tx.js +13 -18
- package/dist/splice.d.ts +25 -1
- package/dist/splice.js +92 -26
- package/dist/types.js +1 -2
- package/package.json +22 -15
- package/rust/pkg/omnia_splice_wasm_bg.wasm +0 -0
- package/rust/pkg/package.json +2 -2
- package/rust/pkg-node/omnia_splice_wasm_bg.wasm +0 -0
- package/rust/pkg-node/package.json +2 -2
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { WotsLeaseProvider, SigningIndices } from '@totemsdk/wots-lease';
|
|
2
|
+
import type { OmniaChannel, ChannelSigner, SignedChannelState } from '@totemsdk/omnia';
|
|
3
|
+
export type { OmniaChannel, ChannelSigner, SignedChannelState };
|
|
4
|
+
export type { WotsLeaseProvider, SigningIndices };
|
|
5
|
+
export type WotsSignature = Uint8Array;
|
|
6
|
+
export type SpliceType = 'splice_in' | 'splice_out';
|
|
7
|
+
export interface SpliceParams {
|
|
8
|
+
type: SpliceType;
|
|
9
|
+
newTotalValue: bigint;
|
|
10
|
+
newBalances: Record<string, bigint>;
|
|
11
|
+
additionalCoinId?: string;
|
|
12
|
+
additionalCoinAddress?: string;
|
|
13
|
+
additionalAmount?: bigint;
|
|
14
|
+
withdrawAmount?: bigint;
|
|
15
|
+
withdrawAddress?: string;
|
|
16
|
+
extraOutputs?: Array<{
|
|
17
|
+
address: string;
|
|
18
|
+
amount: bigint;
|
|
19
|
+
tokenId?: string;
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
22
|
+
export interface SpliceTxInput {
|
|
23
|
+
coinId: string;
|
|
24
|
+
address: string;
|
|
25
|
+
amount: bigint;
|
|
26
|
+
tokenId: string;
|
|
27
|
+
}
|
|
28
|
+
export interface SpliceTxOutput {
|
|
29
|
+
address: string;
|
|
30
|
+
amount: bigint;
|
|
31
|
+
tokenId: string;
|
|
32
|
+
storeState: boolean;
|
|
33
|
+
stateVarSettlement: boolean;
|
|
34
|
+
stateVarSequence: number;
|
|
35
|
+
}
|
|
36
|
+
export interface SpliceTxDraft {
|
|
37
|
+
inputs: SpliceTxInput[];
|
|
38
|
+
outputs: SpliceTxOutput[];
|
|
39
|
+
channelId: string;
|
|
40
|
+
params: SpliceParams;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Splice proposal produced by the initiating party.
|
|
44
|
+
*
|
|
45
|
+
* `spliceTxHex` contains the canonical Minima TX bytes (hex) that both parties
|
|
46
|
+
* independently verify before signing. `spliceTxDraft` retains the structured
|
|
47
|
+
* representation needed for digest computation and output inspection by the
|
|
48
|
+
* acceptor; it is NOT sent over the wire in production but is included here for
|
|
49
|
+
* the v0.1.0 in-process protocol. Future relay integration will transmit only
|
|
50
|
+
* `spliceTxHex` + `params`.
|
|
51
|
+
*
|
|
52
|
+
* `proposerReservationId` and `proposerSigningIndices` carry the WOTS lease
|
|
53
|
+
* reservation used when producing `proposerSignature`. They are consumed by
|
|
54
|
+
* `finalizeSplice` to commit or burn the reservation after the splice TX is
|
|
55
|
+
* settled, preserving key-slot accounting and preventing one-time-key reuse.
|
|
56
|
+
*/
|
|
57
|
+
export interface SpliceProposal {
|
|
58
|
+
spliceId: string;
|
|
59
|
+
channelId: string;
|
|
60
|
+
params: SpliceParams;
|
|
61
|
+
spliceTxHex: string;
|
|
62
|
+
spliceTxDraft: SpliceTxDraft;
|
|
63
|
+
proposerPublicKeyDigest: string;
|
|
64
|
+
proposerSignature: WotsSignature;
|
|
65
|
+
proposerReservationId: string;
|
|
66
|
+
proposerSigningIndices: SigningIndices;
|
|
67
|
+
proposedAt: number;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* `acceptorReservationId` and `acceptorSigningIndices` carry the WOTS lease
|
|
71
|
+
* reservation used when producing `acceptorSignature`. Consumed by
|
|
72
|
+
* `finalizeSplice` to commit or burn the acceptor's key-slot reservation.
|
|
73
|
+
*/
|
|
74
|
+
export interface SpliceAcceptance {
|
|
75
|
+
spliceId: string;
|
|
76
|
+
channelId: string;
|
|
77
|
+
acceptorPublicKeyDigest: string;
|
|
78
|
+
acceptorSignature: WotsSignature;
|
|
79
|
+
acceptorReservationId: string;
|
|
80
|
+
acceptorSigningIndices: SigningIndices;
|
|
81
|
+
acceptedAt: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* A channel returned by `quiesceChannel`.
|
|
85
|
+
*
|
|
86
|
+
* - `status: 'quiesced'` — no new HTLCs may be added until the splice confirms.
|
|
87
|
+
* - `pendingHTLCs: []` — all resolved HTLCs are cleared from the active list.
|
|
88
|
+
* - `quiesceSignedState` — the local party's partial signed state over the
|
|
89
|
+
* final pre-splice balance split (sequence incremented by one). Exchange this
|
|
90
|
+
* with the counterparty to obtain their co-signature before finalizing the splice.
|
|
91
|
+
*/
|
|
92
|
+
export type QuiescedChannel = Omit<OmniaChannel, 'status'> & {
|
|
93
|
+
status: 'quiesced';
|
|
94
|
+
quiesceSignedState: Partial<SignedChannelState>;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* A channel returned by `finalizeSplice`.
|
|
98
|
+
*
|
|
99
|
+
* Extends OmniaChannel with splice provenance fields. The returned channel is
|
|
100
|
+
* immediately usable for new payments: `status: 'active'`, `currentSequence: 0`,
|
|
101
|
+
* fresh WOTS budget. `splicedFrom` identifies the old channel and
|
|
102
|
+
* `spliceFundingCoinId` is the new on-chain UTXO.
|
|
103
|
+
*
|
|
104
|
+
* **Mutation side effect**: `finalizeSplice` mutates the `channel` argument
|
|
105
|
+
* (the `QuiescedChannel` passed in) by setting `status: 'spliced'` after the
|
|
106
|
+
* splice TX is confirmed. This invalidates the old channel in-place; the
|
|
107
|
+
* returned `SplicedChannel` (status: 'active') is the only valid live channel.
|
|
108
|
+
*/
|
|
109
|
+
export type SplicedChannel = OmniaChannel & {
|
|
110
|
+
splicedFrom: string;
|
|
111
|
+
spliceType: SpliceType;
|
|
112
|
+
spliceFundingTxId: string;
|
|
113
|
+
spliceFundingCoinId: string;
|
|
114
|
+
};
|
|
115
|
+
export interface SpliceLeaseProvider {
|
|
116
|
+
/** Local party's WOTS signer. */
|
|
117
|
+
signer: ChannelSigner;
|
|
118
|
+
/** WOTS lease provider for key slot reservation/commit (required for quiesceChannel). */
|
|
119
|
+
wotsLease: WotsLeaseProvider;
|
|
120
|
+
/** Optional function to broadcast the mined splice TxPoW to the chain. */
|
|
121
|
+
broadcast?: (txHex: string) => Promise<{
|
|
122
|
+
txpowid?: string;
|
|
123
|
+
success?: boolean;
|
|
124
|
+
}>;
|
|
125
|
+
}
|
|
126
|
+
export interface SpliceSigningIndices {
|
|
127
|
+
l1: number;
|
|
128
|
+
l2: number;
|
|
129
|
+
addressIndex: number;
|
|
130
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splice peer round-trip (RFC-035 B1).
|
|
3
|
+
*
|
|
4
|
+
* Two-party splice over the coordination transport:
|
|
5
|
+
*
|
|
6
|
+
* proposer ── SPLICE_PROPOSAL ─▶ acceptor
|
|
7
|
+
* proposer ◀─ SPLICE_ACCEPT ── acceptor (acceptor reserves a WOTS leaf)
|
|
8
|
+
* proposer ── SPLICE_FINALIZED ─▶ acceptor (acceptor commits its leaf)
|
|
9
|
+
*
|
|
10
|
+
* Verify-before-sign: the acceptor cryptographically verifies the proposer's
|
|
11
|
+
* signature over the splice digest (and `acceptSplice` binds the draft to the
|
|
12
|
+
* local channel) before it signs. Each party commits/burns its **own** lease:
|
|
13
|
+
* the proposer on `finalizeSplice`, the acceptor on `SPLICE_FINALIZED` (or burns
|
|
14
|
+
* it when no confirmation arrives within the TTL).
|
|
15
|
+
*/
|
|
16
|
+
import type { OmniaChannel, OmniaPeer, OmniaCoordination, CoordinationHandlers } from '@totemsdk/omnia';
|
|
17
|
+
import type { SpliceProposal, SpliceLeaseProvider, SpliceParams, QuiescedChannel, SplicedChannel } from './types.js';
|
|
18
|
+
import { type FinalizeSpliceOptions } from './finalize.js';
|
|
19
|
+
export type VerifySignature = (signature: Uint8Array, digest: Uint8Array, publicKeyDigest: string) => boolean;
|
|
20
|
+
export type ChannelStore = Map<string, OmniaChannel | QuiescedChannel>;
|
|
21
|
+
export interface SpliceApprovalRequest {
|
|
22
|
+
channelId: string;
|
|
23
|
+
spliceId: string;
|
|
24
|
+
proposer: string;
|
|
25
|
+
params: SpliceParams;
|
|
26
|
+
}
|
|
27
|
+
export type SpliceApproval = (request: SpliceApprovalRequest) => Promise<boolean>;
|
|
28
|
+
export interface SpliceCoordinationDeps {
|
|
29
|
+
/** This peer's known channels (its own quiesced copies). */
|
|
30
|
+
channels: ChannelStore;
|
|
31
|
+
/** This peer's signer + WOTS lease provider. */
|
|
32
|
+
leaseProvider: SpliceLeaseProvider;
|
|
33
|
+
/** Signature verifier; defaults to `verifyChannelSignature` (RFC-037). */
|
|
34
|
+
verifySignature?: VerifySignature;
|
|
35
|
+
/** Extra finalize options (broadcast, mineDifficulty). */
|
|
36
|
+
finalizeOptions?: FinalizeSpliceOptions;
|
|
37
|
+
/** Consent gate for signing an acceptance (RFC-014 §6.3). */
|
|
38
|
+
approval?: SpliceApproval;
|
|
39
|
+
/** Called on the proposer when the splice finalizes. */
|
|
40
|
+
onFinalized?: (channel: SplicedChannel, peer: OmniaPeer) => void;
|
|
41
|
+
/** Acceptor reservation TTL before burn (default 24h). */
|
|
42
|
+
ttlMs?: number;
|
|
43
|
+
}
|
|
44
|
+
export interface SpliceProposeParams {
|
|
45
|
+
type: 'in' | 'out';
|
|
46
|
+
additionalCoinId?: string;
|
|
47
|
+
additionalAmount?: bigint;
|
|
48
|
+
withdrawAmount?: bigint;
|
|
49
|
+
withdrawAddress?: string;
|
|
50
|
+
}
|
|
51
|
+
export interface SpliceCoordination {
|
|
52
|
+
handlers: CoordinationHandlers;
|
|
53
|
+
propose(peer: OmniaPeer, coordination: OmniaCoordination, channel: OmniaChannel | QuiescedChannel, params: SpliceProposeParams): Promise<SpliceProposal>;
|
|
54
|
+
}
|
|
55
|
+
export declare function createSpliceCoordination(deps: SpliceCoordinationDeps): SpliceCoordination;
|
|
@@ -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
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export { quiesceChannel, type QuiesceOptions } from './quiesce.js';
|
|
2
2
|
export { buildSpliceTx, spliceDraftToMinimaBytes, computeSpliceTxDigest } from './splice-tx.js';
|
|
3
|
-
export { proposeSpliceIn, proposeSpliceOut, acceptSplice } from './splice.js';
|
|
3
|
+
export { proposeSpliceIn, proposeSpliceOut, acceptSplice, buildSpliceProposal, buildSpliceInProposal, buildSpliceOutProposal, } from './splice.js';
|
|
4
|
+
export type { UnsignedSpliceProposal } from './splice.js';
|
|
4
5
|
export { finalizeSplice, type FinalizeSpliceOptions } from './finalize.js';
|
|
5
6
|
export type { SpliceType, SpliceParams, SpliceTxDraft, SpliceTxInput, SpliceTxOutput, SpliceProposal, SpliceAcceptance, QuiescedChannel, SplicedChannel, SpliceLeaseProvider, SpliceSigningIndices, WotsSignature, } from './types.js';
|
|
6
7
|
export { SpliceError, PendingHTLCError, SpliceChannelStatusError, SpliceBalanceConservationError, SpliceSignatureMismatchError, SpliceMissingPartyError, SpliceInsufficientFundsError, } from './errors.js';
|
|
7
8
|
export { createDurableSpliceStore, } from './durable-splice-store.js';
|
|
8
9
|
export type { SpliceRecord, SpliceRecordStatus, SpliceStoreState, DurableSpliceStore, DurableSpliceStoreOptions, } from './durable-splice-store.js';
|
|
10
|
+
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, buildSpliceProposal, buildSpliceInProposal, buildSpliceOutProposal, } 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';
|