@totemsdk/omnia-splice 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -3
- package/dist/errors.js +17 -7
- package/dist/finalize.js +29 -26
- package/dist/index.js +23 -5
- package/dist/quiesce.js +10 -7
- package/dist/splice-tx.js +26 -21
- package/dist/splice.js +26 -21
- package/dist/types.js +2 -1
- package/package.json +36 -8
- package/src/__tests__/omnia-splice.test.ts +0 -919
- package/src/errors.ts +0 -63
- package/src/finalize.ts +0 -344
- package/src/index.ts +0 -29
- package/src/quiesce.ts +0 -103
- package/src/splice-tx.ts +0 -208
- package/src/splice.ts +0 -385
- package/src/types.ts +0 -136
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Totem SDK Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -85,6 +85,6 @@ try {
|
|
|
85
85
|
|
|
86
86
|
## See also
|
|
87
87
|
|
|
88
|
-
- [`@totemsdk/omnia`](
|
|
89
|
-
- [`@totemsdk/tx-builder`](
|
|
90
|
-
- [`@totemsdk/wots-lease`](
|
|
88
|
+
- [`@totemsdk/omnia`](https://www.npmjs.com/package/@totemsdk/omnia) — the channel state machine being spliced
|
|
89
|
+
- [`@totemsdk/tx-builder`](https://www.npmjs.com/package/@totemsdk/tx-builder) — used internally by `buildSpliceTx`
|
|
90
|
+
- [`@totemsdk/wots-lease`](https://www.npmjs.com/package/@totemsdk/wots-lease) — signing slots managed during splice
|
package/dist/errors.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
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 {
|
|
2
5
|
constructor(code, message) {
|
|
3
6
|
super(message);
|
|
4
7
|
this.name = 'SpliceError';
|
|
@@ -6,39 +9,46 @@ export class SpliceError extends Error {
|
|
|
6
9
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
7
10
|
}
|
|
8
11
|
}
|
|
9
|
-
|
|
12
|
+
exports.SpliceError = SpliceError;
|
|
13
|
+
class PendingHTLCError extends SpliceError {
|
|
10
14
|
constructor(pendingCount) {
|
|
11
15
|
super('PENDING_HTLC', `Cannot splice: ${pendingCount} HTLC(s) still pending. Fulfill or timeout all HTLCs before splicing.`);
|
|
12
16
|
this.name = 'PendingHTLCError';
|
|
13
17
|
}
|
|
14
18
|
}
|
|
15
|
-
|
|
19
|
+
exports.PendingHTLCError = PendingHTLCError;
|
|
20
|
+
class SpliceChannelStatusError extends SpliceError {
|
|
16
21
|
constructor(required, actual) {
|
|
17
22
|
super('INVALID_CHANNEL_STATUS', `Splice requires channel status '${required}', got '${actual}'.`);
|
|
18
23
|
this.name = 'SpliceChannelStatusError';
|
|
19
24
|
}
|
|
20
25
|
}
|
|
21
|
-
|
|
26
|
+
exports.SpliceChannelStatusError = SpliceChannelStatusError;
|
|
27
|
+
class SpliceBalanceConservationError extends SpliceError {
|
|
22
28
|
constructor(expected, actual) {
|
|
23
29
|
super('BALANCE_CONSERVATION', `Splice balance conservation failed: expected ${expected}, got ${actual}.`);
|
|
24
30
|
this.name = 'SpliceBalanceConservationError';
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
|
-
|
|
33
|
+
exports.SpliceBalanceConservationError = SpliceBalanceConservationError;
|
|
34
|
+
class SpliceSignatureMismatchError extends SpliceError {
|
|
28
35
|
constructor(detail) {
|
|
29
36
|
super('SIGNATURE_MISMATCH', `Splice signature mismatch: ${detail}`);
|
|
30
37
|
this.name = 'SpliceSignatureMismatchError';
|
|
31
38
|
}
|
|
32
39
|
}
|
|
33
|
-
|
|
40
|
+
exports.SpliceSignatureMismatchError = SpliceSignatureMismatchError;
|
|
41
|
+
class SpliceMissingPartyError extends SpliceError {
|
|
34
42
|
constructor(partyId) {
|
|
35
43
|
super('MISSING_PARTY', `Party '${partyId}' not found in channel.`);
|
|
36
44
|
this.name = 'SpliceMissingPartyError';
|
|
37
45
|
}
|
|
38
46
|
}
|
|
39
|
-
|
|
47
|
+
exports.SpliceMissingPartyError = SpliceMissingPartyError;
|
|
48
|
+
class SpliceInsufficientFundsError extends SpliceError {
|
|
40
49
|
constructor(available, requested) {
|
|
41
50
|
super('INSUFFICIENT_FUNDS', `Splice-out withdraws ${requested} but channel only holds ${available}.`);
|
|
42
51
|
this.name = 'SpliceInsufficientFundsError';
|
|
43
52
|
}
|
|
44
53
|
}
|
|
54
|
+
exports.SpliceInsufficientFundsError = SpliceInsufficientFundsError;
|
package/dist/finalize.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.finalizeSplice = finalizeSplice;
|
|
4
|
+
const core_1 = require("@totemsdk/core");
|
|
5
|
+
const txpow_1 = require("@totemsdk/txpow");
|
|
6
|
+
const core_2 = require("@totemsdk/core");
|
|
7
|
+
const splice_tx_js_1 = require("./splice-tx.js");
|
|
8
|
+
const errors_js_1 = require("./errors.js");
|
|
6
9
|
function defaultWotsVerify(sig, digest, pkd) {
|
|
7
10
|
try {
|
|
8
|
-
return wotsVerifyDigest(sig, digest, fromHex(pkd));
|
|
11
|
+
return (0, core_1.wotsVerifyDigest)(sig, digest, (0, core_1.fromHex)(pkd));
|
|
9
12
|
}
|
|
10
13
|
catch {
|
|
11
14
|
return false;
|
|
@@ -20,9 +23,9 @@ function assembleSpliceWitness(sig1, sig2) {
|
|
|
20
23
|
const encodeVarBytes = (b) => {
|
|
21
24
|
const lenBuf = new Uint8Array(4);
|
|
22
25
|
new DataView(lenBuf.buffer).setInt32(0, b.length, false);
|
|
23
|
-
return concatBytes(lenBuf, b);
|
|
26
|
+
return (0, core_2.concatBytes)(lenBuf, b);
|
|
24
27
|
};
|
|
25
|
-
return concatBytes(encodeMiniNumber(2), encodeVarBytes(sig1), encodeVarBytes(sig2), encodeMiniNumber(0), encodeMiniNumber(0));
|
|
28
|
+
return (0, core_2.concatBytes)(encodeMiniNumber(2), encodeVarBytes(sig1), encodeVarBytes(sig2), encodeMiniNumber(0), encodeMiniNumber(0));
|
|
26
29
|
}
|
|
27
30
|
/**
|
|
28
31
|
* Structural authorization checks: both parties must be distinct channel
|
|
@@ -33,18 +36,18 @@ function assembleSpliceWitness(sig1, sig2) {
|
|
|
33
36
|
function validateSpliceAuthorization(channel, proposal, acceptance) {
|
|
34
37
|
const proposerParty = channel.parties.find(p => p.publicKeyDigest === proposal.proposerPublicKeyDigest);
|
|
35
38
|
if (!proposerParty)
|
|
36
|
-
throw new SpliceMissingPartyError(proposal.proposerPublicKeyDigest);
|
|
39
|
+
throw new errors_js_1.SpliceMissingPartyError(proposal.proposerPublicKeyDigest);
|
|
37
40
|
const acceptorParty = channel.parties.find(p => p.publicKeyDigest === acceptance.acceptorPublicKeyDigest);
|
|
38
41
|
if (!acceptorParty)
|
|
39
|
-
throw new SpliceMissingPartyError(acceptance.acceptorPublicKeyDigest);
|
|
42
|
+
throw new errors_js_1.SpliceMissingPartyError(acceptance.acceptorPublicKeyDigest);
|
|
40
43
|
if (proposal.proposerPublicKeyDigest === acceptance.acceptorPublicKeyDigest) {
|
|
41
44
|
throw new Error('Proposer and acceptor must be different channel parties');
|
|
42
45
|
}
|
|
43
46
|
if (!proposal.proposerSignature || proposal.proposerSignature.length === 0) {
|
|
44
|
-
throw new SpliceSignatureMismatchError('Proposer signature is empty');
|
|
47
|
+
throw new errors_js_1.SpliceSignatureMismatchError('Proposer signature is empty');
|
|
45
48
|
}
|
|
46
49
|
if (!acceptance.acceptorSignature || acceptance.acceptorSignature.length === 0) {
|
|
47
|
-
throw new SpliceSignatureMismatchError('Acceptor signature is empty');
|
|
50
|
+
throw new errors_js_1.SpliceSignatureMismatchError('Acceptor signature is empty');
|
|
48
51
|
}
|
|
49
52
|
}
|
|
50
53
|
/**
|
|
@@ -56,12 +59,12 @@ function validateSpliceAuthorization(channel, proposal, acceptance) {
|
|
|
56
59
|
* by partyId), so signatures are bound to the exact per-party balance split.
|
|
57
60
|
*/
|
|
58
61
|
function verifySpliceSignatures(proposal, acceptance, verifier) {
|
|
59
|
-
const digest = computeSpliceTxDigest(proposal.spliceTxDraft);
|
|
62
|
+
const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(proposal.spliceTxDraft);
|
|
60
63
|
if (!verifier(proposal.proposerSignature, digest, proposal.proposerPublicKeyDigest)) {
|
|
61
|
-
throw new SpliceSignatureMismatchError('Proposer signature failed cryptographic verification — digest or key mismatch');
|
|
64
|
+
throw new errors_js_1.SpliceSignatureMismatchError('Proposer signature failed cryptographic verification — digest or key mismatch');
|
|
62
65
|
}
|
|
63
66
|
if (!verifier(acceptance.acceptorSignature, digest, acceptance.acceptorPublicKeyDigest)) {
|
|
64
|
-
throw new SpliceSignatureMismatchError('Acceptor signature failed cryptographic verification — digest or key mismatch');
|
|
67
|
+
throw new errors_js_1.SpliceSignatureMismatchError('Acceptor signature failed cryptographic verification — digest or key mismatch');
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
/**
|
|
@@ -73,9 +76,9 @@ function verifySpliceSignatures(proposal, acceptance, verifier) {
|
|
|
73
76
|
* inconsistent, the new channel state would not match what was actually mined.
|
|
74
77
|
*/
|
|
75
78
|
function bindFinalizeParamsToDraft(channel, proposal) {
|
|
76
|
-
const recomputed = buildSpliceTx(channel, proposal.params);
|
|
77
|
-
const expected = computeSpliceTxDigest(recomputed);
|
|
78
|
-
const actual = computeSpliceTxDigest(proposal.spliceTxDraft);
|
|
79
|
+
const recomputed = (0, splice_tx_js_1.buildSpliceTx)(channel, proposal.params);
|
|
80
|
+
const expected = (0, splice_tx_js_1.computeSpliceTxDigest)(recomputed);
|
|
81
|
+
const actual = (0, splice_tx_js_1.computeSpliceTxDigest)(proposal.spliceTxDraft);
|
|
79
82
|
if (!expected.every((b, i) => b === actual[i])) {
|
|
80
83
|
throw new Error('finalizeSplice: proposal.spliceTxDraft is inconsistent with proposal.params — ' +
|
|
81
84
|
'rejecting to prevent on-chain/off-chain state divergence');
|
|
@@ -127,12 +130,12 @@ function bindFinalizeParamsToDraft(channel, proposal) {
|
|
|
127
130
|
* @param options - Broadcast, difficulty, verifier, and lease providers.
|
|
128
131
|
* @returns SplicedChannel (`status: 'active'`) with updated value and provenance.
|
|
129
132
|
*/
|
|
130
|
-
|
|
133
|
+
async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
131
134
|
if (channel.status !== 'quiesced') {
|
|
132
|
-
throw new SpliceChannelStatusError('quiesced', channel.status);
|
|
135
|
+
throw new errors_js_1.SpliceChannelStatusError('quiesced', channel.status);
|
|
133
136
|
}
|
|
134
137
|
if (proposal.spliceId !== acceptance.spliceId) {
|
|
135
|
-
throw new SpliceSignatureMismatchError(`proposal spliceId '${proposal.spliceId}' !== acceptance spliceId '${acceptance.spliceId}'`);
|
|
138
|
+
throw new errors_js_1.SpliceSignatureMismatchError(`proposal spliceId '${proposal.spliceId}' !== acceptance spliceId '${acceptance.spliceId}'`);
|
|
136
139
|
}
|
|
137
140
|
if (proposal.channelId !== channel.channelId) {
|
|
138
141
|
throw new Error(`proposal.channelId '${proposal.channelId}' does not match channel '${channel.channelId}'`);
|
|
@@ -148,14 +151,14 @@ export async function finalizeSplice(channel, proposal, acceptance, options) {
|
|
|
148
151
|
const acceptorLease = options?.acceptorLeaseProvider;
|
|
149
152
|
// Track which reservations have been committed so we know which to burn on failure.
|
|
150
153
|
const committed = new Set();
|
|
151
|
-
const txBytes = spliceDraftToMinimaBytes(proposal.spliceTxDraft);
|
|
154
|
+
const txBytes = (0, splice_tx_js_1.spliceDraftToMinimaBytes)(proposal.spliceTxDraft);
|
|
152
155
|
const witnessBytes = assembleSpliceWitness(proposal.proposerSignature, acceptance.acceptorSignature);
|
|
153
156
|
let spliceFundingTxId;
|
|
154
157
|
try {
|
|
155
|
-
const difficulty = options?.mineDifficulty ?? TX_POW_MIN_DIFFICULTY;
|
|
156
|
-
const txBody = serializeTxBody(txBytes, witnessBytes);
|
|
157
|
-
const mined = await mineTxPoW(txBody, difficulty);
|
|
158
|
-
const fullTxPoW = concatBytes(mined.minedHeaderBytes, new Uint8Array([0x01]), txBody);
|
|
158
|
+
const difficulty = options?.mineDifficulty ?? txpow_1.TX_POW_MIN_DIFFICULTY;
|
|
159
|
+
const txBody = (0, txpow_1.serializeTxBody)(txBytes, witnessBytes);
|
|
160
|
+
const mined = await (0, txpow_1.mineTxPoW)(txBody, difficulty);
|
|
161
|
+
const fullTxPoW = (0, core_2.concatBytes)(mined.minedHeaderBytes, new Uint8Array([0x01]), txBody);
|
|
159
162
|
const txHex = Buffer.from(fullTxPoW).toString('hex');
|
|
160
163
|
spliceFundingTxId = Buffer.from(mined.txpowId).toString('hex');
|
|
161
164
|
if (options?.broadcast) {
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SpliceInsufficientFundsError = exports.SpliceMissingPartyError = exports.SpliceSignatureMismatchError = exports.SpliceBalanceConservationError = exports.SpliceChannelStatusError = exports.PendingHTLCError = exports.SpliceError = exports.finalizeSplice = exports.acceptSplice = exports.proposeSpliceOut = exports.proposeSpliceIn = exports.computeSpliceTxDigest = exports.spliceDraftToMinimaBytes = exports.buildSpliceTx = exports.quiesceChannel = void 0;
|
|
4
|
+
var quiesce_js_1 = require("./quiesce.js");
|
|
5
|
+
Object.defineProperty(exports, "quiesceChannel", { enumerable: true, get: function () { return quiesce_js_1.quiesceChannel; } });
|
|
6
|
+
var splice_tx_js_1 = require("./splice-tx.js");
|
|
7
|
+
Object.defineProperty(exports, "buildSpliceTx", { enumerable: true, get: function () { return splice_tx_js_1.buildSpliceTx; } });
|
|
8
|
+
Object.defineProperty(exports, "spliceDraftToMinimaBytes", { enumerable: true, get: function () { return splice_tx_js_1.spliceDraftToMinimaBytes; } });
|
|
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; } });
|
package/dist/quiesce.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.quiesceChannel = quiesceChannel;
|
|
4
|
+
const omnia_1 = require("@totemsdk/omnia");
|
|
5
|
+
const errors_js_1 = require("./errors.js");
|
|
3
6
|
/**
|
|
4
7
|
* Quiesce a channel before splicing.
|
|
5
8
|
*
|
|
@@ -32,9 +35,9 @@ import { PendingHTLCError, SpliceChannelStatusError } from './errors.js';
|
|
|
32
35
|
* @throws {SpliceChannelStatusError} If channel is not active.
|
|
33
36
|
* @throws {PendingHTLCError} If HTLCs remain pending after resolution.
|
|
34
37
|
*/
|
|
35
|
-
|
|
38
|
+
async function quiesceChannel(channel, leaseProvider, options) {
|
|
36
39
|
if (channel.status !== 'active') {
|
|
37
|
-
throw new SpliceChannelStatusError('active', channel.status);
|
|
40
|
+
throw new errors_js_1.SpliceChannelStatusError('active', channel.status);
|
|
38
41
|
}
|
|
39
42
|
const pendingHTLCs = channel.pendingHTLCs.filter(h => h.status === 'pending');
|
|
40
43
|
if (pendingHTLCs.length > 0) {
|
|
@@ -42,14 +45,14 @@ export async function quiesceChannel(channel, leaseProvider, options) {
|
|
|
42
45
|
await options.awaitResolution(pendingHTLCs);
|
|
43
46
|
const stillPending = channel.pendingHTLCs.filter(h => h.status === 'pending').length;
|
|
44
47
|
if (stillPending > 0) {
|
|
45
|
-
throw new PendingHTLCError(stillPending);
|
|
48
|
+
throw new errors_js_1.PendingHTLCError(stillPending);
|
|
46
49
|
}
|
|
47
50
|
}
|
|
48
51
|
else {
|
|
49
|
-
throw new PendingHTLCError(pendingHTLCs.length);
|
|
52
|
+
throw new errors_js_1.PendingHTLCError(pendingHTLCs.length);
|
|
50
53
|
}
|
|
51
54
|
}
|
|
52
|
-
const { channel: updatedChannel, signedState, error } = await updateState(channel, { newBalances: channel.balances }, leaseProvider.wotsLease, leaseProvider.signer);
|
|
55
|
+
const { channel: updatedChannel, signedState, error } = await (0, omnia_1.updateState)(channel, { newBalances: channel.balances }, leaseProvider.wotsLease, leaseProvider.signer);
|
|
53
56
|
if (error) {
|
|
54
57
|
throw new Error(`quiesceChannel: updateState blocked — ${error}. Splice cannot proceed.`);
|
|
55
58
|
}
|
package/dist/splice-tx.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildSpliceTx = buildSpliceTx;
|
|
4
|
+
exports.spliceDraftToMinimaBytes = spliceDraftToMinimaBytes;
|
|
5
|
+
exports.computeSpliceTxDigest = computeSpliceTxDigest;
|
|
6
|
+
const sha3_js_1 = require("@noble/hashes/sha3.js");
|
|
7
|
+
const core_1 = require("@totemsdk/core");
|
|
8
|
+
const omnia_1 = require("@totemsdk/omnia");
|
|
9
|
+
const errors_js_1 = require("./errors.js");
|
|
5
10
|
const STATE_SETTLEMENT_PORT = 100;
|
|
6
11
|
const STATE_SEQUENCE_PORT = 101;
|
|
7
12
|
/**
|
|
@@ -26,17 +31,17 @@ const STATE_SEQUENCE_PORT = 101;
|
|
|
26
31
|
* @throws {SpliceBalanceConservationError} If output amounts do not sum to newTotalValue.
|
|
27
32
|
* @throws {SpliceInsufficientFundsError} If splice-out exceeds channel holdings.
|
|
28
33
|
*/
|
|
29
|
-
|
|
34
|
+
function buildSpliceTx(channel, params) {
|
|
30
35
|
const tokenId = channel.tokenId ?? '0x00';
|
|
31
36
|
if (params.type === 'splice_out') {
|
|
32
37
|
const withdrawAmount = params.withdrawAmount ?? 0n;
|
|
33
38
|
const extraSum = (params.extraOutputs ?? []).reduce((a, o) => a + o.amount, 0n);
|
|
34
39
|
const totalOutflow = withdrawAmount + extraSum;
|
|
35
40
|
if (totalOutflow > channel.totalValue) {
|
|
36
|
-
throw new SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
|
|
41
|
+
throw new errors_js_1.SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
|
|
37
42
|
}
|
|
38
43
|
if (params.newTotalValue < 0n) {
|
|
39
|
-
throw new SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
|
|
44
|
+
throw new errors_js_1.SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
|
|
40
45
|
}
|
|
41
46
|
}
|
|
42
47
|
const balanceSum = Object.values(params.newBalances).reduce((a, b) => a + b, 0n);
|
|
@@ -45,11 +50,11 @@ export function buildSpliceTx(channel, params) {
|
|
|
45
50
|
// Withdrawals and extra outputs are separate outputs, already factored out by the
|
|
46
51
|
// caller when computing `newTotalValue`.
|
|
47
52
|
if (balanceSum !== params.newTotalValue) {
|
|
48
|
-
throw new SpliceBalanceConservationError(params.newTotalValue, balanceSum);
|
|
53
|
+
throw new errors_js_1.SpliceBalanceConservationError(params.newTotalValue, balanceSum);
|
|
49
54
|
}
|
|
50
55
|
const inputs = [
|
|
51
56
|
{
|
|
52
|
-
coinId: COINID_ELTOO,
|
|
57
|
+
coinId: omnia_1.COINID_ELTOO,
|
|
53
58
|
address: channel.fundingAddress,
|
|
54
59
|
amount: channel.totalValue,
|
|
55
60
|
tokenId,
|
|
@@ -107,14 +112,14 @@ export function buildSpliceTx(channel, params) {
|
|
|
107
112
|
* suitable for WOTS signing. Both parties sign the same bytes independently;
|
|
108
113
|
* the final splice TX assembles both signatures into the witness.
|
|
109
114
|
*/
|
|
110
|
-
|
|
111
|
-
const tx = createDefaultTransaction();
|
|
115
|
+
function spliceDraftToMinimaBytes(draft) {
|
|
116
|
+
const tx = (0, core_1.createDefaultTransaction)();
|
|
112
117
|
for (const inp of draft.inputs) {
|
|
113
|
-
tx.inputs.push(buildMinimaCoin({
|
|
114
|
-
coinId: fromHex(inp.coinId),
|
|
115
|
-
address: fromHex(inp.address),
|
|
118
|
+
tx.inputs.push((0, core_1.buildMinimaCoin)({
|
|
119
|
+
coinId: (0, core_1.fromHex)(inp.coinId),
|
|
120
|
+
address: (0, core_1.fromHex)(inp.address),
|
|
116
121
|
amount: inp.amount.toString(),
|
|
117
|
-
tokenId: fromHex(inp.tokenId),
|
|
122
|
+
tokenId: (0, core_1.fromHex)(inp.tokenId),
|
|
118
123
|
}));
|
|
119
124
|
}
|
|
120
125
|
for (const out of draft.outputs) {
|
|
@@ -124,10 +129,10 @@ export function spliceDraftToMinimaBytes(draft) {
|
|
|
124
129
|
{ port: STATE_SEQUENCE_PORT, value: BigInt(out.stateVarSequence), type: 'number' },
|
|
125
130
|
]
|
|
126
131
|
: [];
|
|
127
|
-
tx.outputs.push(buildMinimaCoin({
|
|
128
|
-
address: fromHex(out.address),
|
|
132
|
+
tx.outputs.push((0, core_1.buildMinimaCoin)({
|
|
133
|
+
address: (0, core_1.fromHex)(out.address),
|
|
129
134
|
amount: out.amount.toString(),
|
|
130
|
-
tokenId: fromHex(out.tokenId),
|
|
135
|
+
tokenId: (0, core_1.fromHex)(out.tokenId),
|
|
131
136
|
storeState: out.storeState,
|
|
132
137
|
state,
|
|
133
138
|
}));
|
|
@@ -136,13 +141,13 @@ export function spliceDraftToMinimaBytes(draft) {
|
|
|
136
141
|
{ port: STATE_SETTLEMENT_PORT, value: false, type: 'bool' },
|
|
137
142
|
{ port: STATE_SEQUENCE_PORT, value: 0n, type: 'number' },
|
|
138
143
|
];
|
|
139
|
-
return serializeTransaction(tx);
|
|
144
|
+
return (0, core_1.serializeTransaction)(tx);
|
|
140
145
|
}
|
|
141
146
|
/**
|
|
142
147
|
* Compute a 32-byte digest over the splice TX draft for WOTS signing.
|
|
143
148
|
* This is the canonical message both parties sign to authorize the splice.
|
|
144
149
|
*/
|
|
145
|
-
|
|
150
|
+
function computeSpliceTxDigest(draft) {
|
|
146
151
|
const canonical = JSON.stringify({
|
|
147
152
|
channelId: draft.channelId,
|
|
148
153
|
inputs: draft.inputs.map(i => ({
|
|
@@ -173,5 +178,5 @@ export function computeSpliceTxDigest(draft) {
|
|
|
173
178
|
.map(([k, v]) => [k, v.toString()]),
|
|
174
179
|
},
|
|
175
180
|
});
|
|
176
|
-
return sha3_256(new TextEncoder().encode(canonical));
|
|
181
|
+
return (0, sha3_js_1.sha3_256)(new TextEncoder().encode(canonical));
|
|
177
182
|
}
|
package/dist/splice.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.proposeSpliceIn = proposeSpliceIn;
|
|
4
|
+
exports.proposeSpliceOut = proposeSpliceOut;
|
|
5
|
+
exports.acceptSplice = acceptSplice;
|
|
6
|
+
const splice_tx_js_1 = require("./splice-tx.js");
|
|
7
|
+
const errors_js_1 = require("./errors.js");
|
|
3
8
|
/**
|
|
4
9
|
* Scale existing balances proportionally to a new total, preserving relative
|
|
5
10
|
* ratios. The last party absorbs rounding so that sum(result) === newTotal
|
|
@@ -46,11 +51,11 @@ function generateSpliceId() {
|
|
|
46
51
|
*/
|
|
47
52
|
function validateChannelQuiesced(channel) {
|
|
48
53
|
if (channel.status !== 'quiesced') {
|
|
49
|
-
throw new SpliceChannelStatusError('quiesced', channel.status);
|
|
54
|
+
throw new errors_js_1.SpliceChannelStatusError('quiesced', channel.status);
|
|
50
55
|
}
|
|
51
56
|
const pending = channel.pendingHTLCs.filter(h => h.status === 'pending').length;
|
|
52
57
|
if (pending > 0) {
|
|
53
|
-
throw new PendingHTLCError(pending);
|
|
58
|
+
throw new errors_js_1.PendingHTLCError(pending);
|
|
54
59
|
}
|
|
55
60
|
}
|
|
56
61
|
/**
|
|
@@ -91,9 +96,9 @@ function validateProposalParams(channel, params) {
|
|
|
91
96
|
* to diverge from the actual on-chain TX outputs.
|
|
92
97
|
*/
|
|
93
98
|
function bindDraftToParams(channel, proposal) {
|
|
94
|
-
const recomputed = buildSpliceTx(channel, proposal.params);
|
|
95
|
-
const expected = computeSpliceTxDigest(recomputed);
|
|
96
|
-
const actual = computeSpliceTxDigest(proposal.spliceTxDraft);
|
|
99
|
+
const recomputed = (0, splice_tx_js_1.buildSpliceTx)(channel, proposal.params);
|
|
100
|
+
const expected = (0, splice_tx_js_1.computeSpliceTxDigest)(recomputed);
|
|
101
|
+
const actual = (0, splice_tx_js_1.computeSpliceTxDigest)(proposal.spliceTxDraft);
|
|
97
102
|
if (!expected.every((b, i) => b === actual[i])) {
|
|
98
103
|
throw new Error('Proposal spliceTxDraft is inconsistent with proposal.params — ' +
|
|
99
104
|
'the TX draft may have been tampered with');
|
|
@@ -120,7 +125,7 @@ function bindDraftToParams(channel, proposal) {
|
|
|
120
125
|
* `sum(newBalances) === newTotalValue` is preserved.
|
|
121
126
|
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
122
127
|
*/
|
|
123
|
-
|
|
128
|
+
async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leaseProvider, newBalances) {
|
|
124
129
|
validateChannelQuiesced(channel);
|
|
125
130
|
const newTotal = channel.totalValue + additionalAmount;
|
|
126
131
|
const effectiveNewBalances = newBalances ?? scaleBalances(channel.balances, channel.totalValue, newTotal);
|
|
@@ -131,13 +136,13 @@ export async function proposeSpliceIn(channel, additionalCoinId, additionalAmoun
|
|
|
131
136
|
additionalCoinId,
|
|
132
137
|
additionalAmount,
|
|
133
138
|
};
|
|
134
|
-
const draft = buildSpliceTx(channel, params);
|
|
135
|
-
const digest = computeSpliceTxDigest(draft);
|
|
136
|
-
const spliceTxHex = Buffer.from(spliceDraftToMinimaBytes(draft)).toString('hex');
|
|
139
|
+
const draft = (0, splice_tx_js_1.buildSpliceTx)(channel, params);
|
|
140
|
+
const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(draft);
|
|
141
|
+
const spliceTxHex = Buffer.from((0, splice_tx_js_1.spliceDraftToMinimaBytes)(draft)).toString('hex');
|
|
137
142
|
const { signer, wotsLease } = leaseProvider;
|
|
138
143
|
const partyInChannel = channel.parties.find(p => p.publicKeyDigest === signer.publicKeyDigest);
|
|
139
144
|
if (!partyInChannel) {
|
|
140
|
-
throw new SpliceMissingPartyError(signer.publicKeyDigest);
|
|
145
|
+
throw new errors_js_1.SpliceMissingPartyError(signer.publicKeyDigest);
|
|
141
146
|
}
|
|
142
147
|
const reservation = await wotsLease.reserveKeyUse({
|
|
143
148
|
treeId: signer.publicKeyDigest,
|
|
@@ -175,7 +180,7 @@ export async function proposeSpliceIn(channel, additionalCoinId, additionalAmoun
|
|
|
175
180
|
* @param extraOutputs - Optional additional on-chain outputs (third-party payments).
|
|
176
181
|
* @returns SpliceProposal signed by the proposer with lease reservation data.
|
|
177
182
|
*/
|
|
178
|
-
|
|
183
|
+
async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress, leaseProvider, newBalances, extraOutputs) {
|
|
179
184
|
validateChannelQuiesced(channel);
|
|
180
185
|
const extraSum = (extraOutputs ?? []).reduce((a, o) => a + o.amount, 0n);
|
|
181
186
|
const totalOutflow = withdrawAmount + extraSum;
|
|
@@ -209,13 +214,13 @@ export async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress,
|
|
|
209
214
|
withdrawAddress,
|
|
210
215
|
extraOutputs,
|
|
211
216
|
};
|
|
212
|
-
const draft = buildSpliceTx(channel, params);
|
|
213
|
-
const digest = computeSpliceTxDigest(draft);
|
|
214
|
-
const spliceTxHex = Buffer.from(spliceDraftToMinimaBytes(draft)).toString('hex');
|
|
217
|
+
const draft = (0, splice_tx_js_1.buildSpliceTx)(channel, params);
|
|
218
|
+
const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(draft);
|
|
219
|
+
const spliceTxHex = Buffer.from((0, splice_tx_js_1.spliceDraftToMinimaBytes)(draft)).toString('hex');
|
|
215
220
|
const { signer, wotsLease } = leaseProvider;
|
|
216
221
|
const partyInChannel = channel.parties.find(p => p.publicKeyDigest === signer.publicKeyDigest);
|
|
217
222
|
if (!partyInChannel) {
|
|
218
|
-
throw new SpliceMissingPartyError(signer.publicKeyDigest);
|
|
223
|
+
throw new errors_js_1.SpliceMissingPartyError(signer.publicKeyDigest);
|
|
219
224
|
}
|
|
220
225
|
const reservation = await wotsLease.reserveKeyUse({
|
|
221
226
|
treeId: signer.publicKeyDigest,
|
|
@@ -260,26 +265,26 @@ export async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress,
|
|
|
260
265
|
* @param leaseProvider - Provides the acceptor signer and WOTS lease.
|
|
261
266
|
* @returns SpliceAcceptance containing acceptor co-signature and lease data.
|
|
262
267
|
*/
|
|
263
|
-
|
|
268
|
+
async function acceptSplice(channel, proposal, leaseProvider) {
|
|
264
269
|
validateChannelQuiesced(channel);
|
|
265
270
|
if (proposal.channelId !== channel.channelId) {
|
|
266
271
|
throw new Error(`Proposal channelId '${proposal.channelId}' does not match channel '${channel.channelId}'`);
|
|
267
272
|
}
|
|
268
273
|
const proposerInChannel = channel.parties.find(p => p.publicKeyDigest === proposal.proposerPublicKeyDigest);
|
|
269
274
|
if (!proposerInChannel) {
|
|
270
|
-
throw new SpliceMissingPartyError(proposal.proposerPublicKeyDigest);
|
|
275
|
+
throw new errors_js_1.SpliceMissingPartyError(proposal.proposerPublicKeyDigest);
|
|
271
276
|
}
|
|
272
277
|
const { signer, wotsLease } = leaseProvider;
|
|
273
278
|
const partyInChannel = channel.parties.find(p => p.publicKeyDigest === signer.publicKeyDigest);
|
|
274
279
|
if (!partyInChannel) {
|
|
275
|
-
throw new SpliceMissingPartyError(signer.publicKeyDigest);
|
|
280
|
+
throw new errors_js_1.SpliceMissingPartyError(signer.publicKeyDigest);
|
|
276
281
|
}
|
|
277
282
|
if (signer.publicKeyDigest === proposal.proposerPublicKeyDigest) {
|
|
278
283
|
throw new Error('Acceptor and proposer cannot be the same party');
|
|
279
284
|
}
|
|
280
285
|
validateProposalParams(channel, proposal.params);
|
|
281
286
|
bindDraftToParams(channel, proposal);
|
|
282
|
-
const digest = computeSpliceTxDigest(proposal.spliceTxDraft);
|
|
287
|
+
const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(proposal.spliceTxDraft);
|
|
283
288
|
const reservation = await wotsLease.reserveKeyUse({
|
|
284
289
|
treeId: signer.publicKeyDigest,
|
|
285
290
|
purpose: 'omnia-splice-accept',
|
package/dist/types.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/package.json
CHANGED
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totemsdk/omnia-splice",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Channel splicing for @totemsdk/omnia — resize Minima eltoo channels without a close+reopen cycle",
|
|
5
|
-
"type": "module",
|
|
6
5
|
"main": "dist/index.js",
|
|
7
6
|
"types": "dist/index.d.ts",
|
|
8
7
|
"exports": {
|
|
9
8
|
".": {
|
|
10
9
|
"types": "./dist/index.d.ts",
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
15
|
"dist",
|
|
16
|
-
"
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
17
18
|
],
|
|
18
19
|
"dependencies": {
|
|
19
|
-
"@noble/hashes": "^
|
|
20
|
-
"@totemsdk/omnia": "0.1.
|
|
20
|
+
"@noble/hashes": "^2.2.0",
|
|
21
|
+
"@totemsdk/omnia": "0.1.4",
|
|
21
22
|
"@totemsdk/core": "1.0.9",
|
|
22
|
-
"@totemsdk/txpow": "0.1.3",
|
|
23
23
|
"@totemsdk/tx-builder": "0.1.4",
|
|
24
|
-
"@totemsdk/wots-lease": "0.1.2"
|
|
24
|
+
"@totemsdk/wots-lease": "0.1.2",
|
|
25
|
+
"@totemsdk/txpow": "0.1.4"
|
|
25
26
|
},
|
|
26
27
|
"peerDependencies": {
|
|
27
|
-
"@noble/hashes": ">=1.3.0
|
|
28
|
+
"@noble/hashes": ">=1.3.0"
|
|
28
29
|
},
|
|
29
30
|
"peerDependenciesMeta": {
|
|
30
31
|
"@noble/hashes": {
|
|
@@ -45,6 +46,33 @@
|
|
|
45
46
|
"access": "public"
|
|
46
47
|
},
|
|
47
48
|
"license": "MIT",
|
|
49
|
+
"author": "Totem SDK",
|
|
50
|
+
"homepage": "https://totemsdk.com",
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/MrGheek/axia-totem/issues"
|
|
53
|
+
},
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "git+https://github.com/MrGheek/axia-totem.git",
|
|
57
|
+
"directory": "packages/totem-sdk/packages/omnia-splice"
|
|
58
|
+
},
|
|
59
|
+
"keywords": [
|
|
60
|
+
"totem",
|
|
61
|
+
"totemsdk",
|
|
62
|
+
"minima",
|
|
63
|
+
"blockchain",
|
|
64
|
+
"quantum-resistant",
|
|
65
|
+
"wots",
|
|
66
|
+
"kissvm",
|
|
67
|
+
"utxo",
|
|
68
|
+
"omnia",
|
|
69
|
+
"payment-channel",
|
|
70
|
+
"eltoo",
|
|
71
|
+
"layer2",
|
|
72
|
+
"splice",
|
|
73
|
+
"channel-resize",
|
|
74
|
+
"htlc"
|
|
75
|
+
],
|
|
48
76
|
"scripts": {
|
|
49
77
|
"build": "tsc",
|
|
50
78
|
"clean": "rm -rf dist",
|