@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/splice-tx.js CHANGED
@@ -1,12 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildSpliceTx = buildSpliceTx;
4
- exports.spliceDraftToMinimaBytes = spliceDraftToMinimaBytes;
5
- exports.computeSpliceTxDigest = computeSpliceTxDigest;
6
- const core_1 = require("@totemsdk/core");
7
- const core_2 = require("@totemsdk/core");
8
- const omnia_1 = require("@totemsdk/omnia");
9
- const errors_js_1 = require("./errors.js");
1
+ import { sha3_256 } from '@totemsdk/core';
2
+ import { serializeTransaction, } from '@totemsdk/core';
3
+ import { COINID_ELTOO } from '@totemsdk/omnia';
4
+ import { SpliceBalanceConservationError, SpliceInsufficientFundsError } from './errors.js';
10
5
  const STATE_SETTLEMENT_PORT = 100;
11
6
  const STATE_SEQUENCE_PORT = 101;
12
7
  /**
@@ -31,17 +26,17 @@ const STATE_SEQUENCE_PORT = 101;
31
26
  * @throws {SpliceBalanceConservationError} If output amounts do not sum to newTotalValue.
32
27
  * @throws {SpliceInsufficientFundsError} If splice-out exceeds channel holdings.
33
28
  */
34
- function buildSpliceTx(channel, params) {
29
+ export function buildSpliceTx(channel, params) {
35
30
  const tokenId = channel.tokenId ?? '0x00';
36
31
  if (params.type === 'splice_out') {
37
32
  const withdrawAmount = params.withdrawAmount ?? 0n;
38
33
  const extraSum = (params.extraOutputs ?? []).reduce((a, o) => a + o.amount, 0n);
39
34
  const totalOutflow = withdrawAmount + extraSum;
40
35
  if (totalOutflow > channel.totalValue) {
41
- throw new errors_js_1.SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
36
+ throw new SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
42
37
  }
43
38
  if (params.newTotalValue < 0n) {
44
- throw new errors_js_1.SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
39
+ throw new SpliceInsufficientFundsError(channel.totalValue, totalOutflow);
45
40
  }
46
41
  }
47
42
  const balanceSum = Object.values(params.newBalances).reduce((a, b) => a + b, 0n);
@@ -50,11 +45,11 @@ function buildSpliceTx(channel, params) {
50
45
  // Withdrawals and extra outputs are separate outputs, already factored out by the
51
46
  // caller when computing `newTotalValue`.
52
47
  if (balanceSum !== params.newTotalValue) {
53
- throw new errors_js_1.SpliceBalanceConservationError(params.newTotalValue, balanceSum);
48
+ throw new SpliceBalanceConservationError(params.newTotalValue, balanceSum);
54
49
  }
55
50
  const inputs = [
56
51
  {
57
- coinId: omnia_1.COINID_ELTOO,
52
+ coinId: COINID_ELTOO,
58
53
  address: channel.fundingAddress,
59
54
  amount: channel.totalValue,
60
55
  tokenId,
@@ -112,7 +107,7 @@ function buildSpliceTx(channel, params) {
112
107
  * suitable for WOTS signing. Both parties sign the same bytes independently;
113
108
  * the final splice TX assembles both signatures into the witness.
114
109
  */
115
- function spliceDraftToMinimaBytes(draft) {
110
+ export function spliceDraftToMinimaBytes(draft) {
116
111
  const toHex = (b) => Buffer.from(b).toString('hex');
117
112
  const svToJson = (port, value, type) => {
118
113
  let data;
@@ -169,13 +164,13 @@ function spliceDraftToMinimaBytes(draft) {
169
164
  svToJson(STATE_SETTLEMENT_PORT, false, 'bool'),
170
165
  svToJson(STATE_SEQUENCE_PORT, 0n, 'number'),
171
166
  ];
172
- return (0, core_2.serializeTransaction)(JSON.stringify({ inputs, outputs, state, linkhash: '0x00' }));
167
+ return serializeTransaction(JSON.stringify({ inputs, outputs, state, linkhash: '0x00' }));
173
168
  }
174
169
  /**
175
170
  * Compute a 32-byte digest over the splice TX draft for WOTS signing.
176
171
  * This is the canonical message both parties sign to authorize the splice.
177
172
  */
178
- function computeSpliceTxDigest(draft) {
173
+ export function computeSpliceTxDigest(draft) {
179
174
  const canonical = JSON.stringify({
180
175
  channelId: draft.channelId,
181
176
  inputs: draft.inputs.map(i => ({
@@ -206,5 +201,5 @@ function computeSpliceTxDigest(draft) {
206
201
  .map(([k, v]) => [k, v.toString()]),
207
202
  },
208
203
  });
209
- return (0, core_1.sha3_256)(new TextEncoder().encode(canonical));
204
+ return sha3_256(new TextEncoder().encode(canonical));
210
205
  }
package/dist/splice.js CHANGED
@@ -1,10 +1,5 @@
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");
1
+ import { buildSpliceTx, computeSpliceTxDigest, spliceDraftToMinimaBytes } from './splice-tx.js';
2
+ import { PendingHTLCError, SpliceChannelStatusError, SpliceMissingPartyError, } from './errors.js';
8
3
  /**
9
4
  * Scale existing balances proportionally to a new total, preserving relative
10
5
  * ratios. The last party absorbs rounding so that sum(result) === newTotal
@@ -51,11 +46,11 @@ function generateSpliceId() {
51
46
  */
52
47
  function validateChannelQuiesced(channel) {
53
48
  if (channel.status !== 'quiesced') {
54
- throw new errors_js_1.SpliceChannelStatusError('quiesced', channel.status);
49
+ throw new SpliceChannelStatusError('quiesced', channel.status);
55
50
  }
56
51
  const pending = channel.pendingHTLCs.filter(h => h.status === 'pending').length;
57
52
  if (pending > 0) {
58
- throw new errors_js_1.PendingHTLCError(pending);
53
+ throw new PendingHTLCError(pending);
59
54
  }
60
55
  }
61
56
  /**
@@ -96,9 +91,9 @@ function validateProposalParams(channel, params) {
96
91
  * to diverge from the actual on-chain TX outputs.
97
92
  */
98
93
  function bindDraftToParams(channel, proposal) {
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);
94
+ const recomputed = buildSpliceTx(channel, proposal.params);
95
+ const expected = computeSpliceTxDigest(recomputed);
96
+ const actual = computeSpliceTxDigest(proposal.spliceTxDraft);
102
97
  if (!expected.every((b, i) => b === actual[i])) {
103
98
  throw new Error('Proposal spliceTxDraft is inconsistent with proposal.params — ' +
104
99
  'the TX draft may have been tampered with');
@@ -125,7 +120,7 @@ function bindDraftToParams(channel, proposal) {
125
120
  * `sum(newBalances) === newTotalValue` is preserved.
126
121
  * @returns SpliceProposal signed by the proposer with lease reservation data.
127
122
  */
128
- async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leaseProvider, newBalances) {
123
+ export async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leaseProvider, newBalances) {
129
124
  validateChannelQuiesced(channel);
130
125
  const newTotal = channel.totalValue + additionalAmount;
131
126
  const effectiveNewBalances = newBalances ?? scaleBalances(channel.balances, channel.totalValue, newTotal);
@@ -136,13 +131,13 @@ async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leas
136
131
  additionalCoinId,
137
132
  additionalAmount,
138
133
  };
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');
134
+ const draft = buildSpliceTx(channel, params);
135
+ const digest = computeSpliceTxDigest(draft);
136
+ const spliceTxHex = Buffer.from(spliceDraftToMinimaBytes(draft)).toString('hex');
142
137
  const { signer, wotsLease } = leaseProvider;
143
138
  const partyInChannel = channel.parties.find(p => p.publicKeyDigest === signer.publicKeyDigest);
144
139
  if (!partyInChannel) {
145
- throw new errors_js_1.SpliceMissingPartyError(signer.publicKeyDigest);
140
+ throw new SpliceMissingPartyError(signer.publicKeyDigest);
146
141
  }
147
142
  const reservation = await wotsLease.reserveKeyUse({
148
143
  treeId: signer.publicKeyDigest,
@@ -180,7 +175,7 @@ async function proposeSpliceIn(channel, additionalCoinId, additionalAmount, leas
180
175
  * @param extraOutputs - Optional additional on-chain outputs (third-party payments).
181
176
  * @returns SpliceProposal signed by the proposer with lease reservation data.
182
177
  */
183
- async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress, leaseProvider, newBalances, extraOutputs) {
178
+ export async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress, leaseProvider, newBalances, extraOutputs) {
184
179
  validateChannelQuiesced(channel);
185
180
  const extraSum = (extraOutputs ?? []).reduce((a, o) => a + o.amount, 0n);
186
181
  const totalOutflow = withdrawAmount + extraSum;
@@ -214,13 +209,13 @@ async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress, leaseP
214
209
  withdrawAddress,
215
210
  extraOutputs,
216
211
  };
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');
212
+ const draft = buildSpliceTx(channel, params);
213
+ const digest = computeSpliceTxDigest(draft);
214
+ const spliceTxHex = Buffer.from(spliceDraftToMinimaBytes(draft)).toString('hex');
220
215
  const { signer, wotsLease } = leaseProvider;
221
216
  const partyInChannel = channel.parties.find(p => p.publicKeyDigest === signer.publicKeyDigest);
222
217
  if (!partyInChannel) {
223
- throw new errors_js_1.SpliceMissingPartyError(signer.publicKeyDigest);
218
+ throw new SpliceMissingPartyError(signer.publicKeyDigest);
224
219
  }
225
220
  const reservation = await wotsLease.reserveKeyUse({
226
221
  treeId: signer.publicKeyDigest,
@@ -265,26 +260,26 @@ async function proposeSpliceOut(channel, withdrawAmount, withdrawAddress, leaseP
265
260
  * @param leaseProvider - Provides the acceptor signer and WOTS lease.
266
261
  * @returns SpliceAcceptance containing acceptor co-signature and lease data.
267
262
  */
268
- async function acceptSplice(channel, proposal, leaseProvider) {
263
+ export async function acceptSplice(channel, proposal, leaseProvider) {
269
264
  validateChannelQuiesced(channel);
270
265
  if (proposal.channelId !== channel.channelId) {
271
266
  throw new Error(`Proposal channelId '${proposal.channelId}' does not match channel '${channel.channelId}'`);
272
267
  }
273
268
  const proposerInChannel = channel.parties.find(p => p.publicKeyDigest === proposal.proposerPublicKeyDigest);
274
269
  if (!proposerInChannel) {
275
- throw new errors_js_1.SpliceMissingPartyError(proposal.proposerPublicKeyDigest);
270
+ throw new SpliceMissingPartyError(proposal.proposerPublicKeyDigest);
276
271
  }
277
272
  const { signer, wotsLease } = leaseProvider;
278
273
  const partyInChannel = channel.parties.find(p => p.publicKeyDigest === signer.publicKeyDigest);
279
274
  if (!partyInChannel) {
280
- throw new errors_js_1.SpliceMissingPartyError(signer.publicKeyDigest);
275
+ throw new SpliceMissingPartyError(signer.publicKeyDigest);
281
276
  }
282
277
  if (signer.publicKeyDigest === proposal.proposerPublicKeyDigest) {
283
278
  throw new Error('Acceptor and proposer cannot be the same party');
284
279
  }
285
280
  validateProposalParams(channel, proposal.params);
286
281
  bindDraftToParams(channel, proposal);
287
- const digest = (0, splice_tx_js_1.computeSpliceTxDigest)(proposal.spliceTxDraft);
282
+ const digest = computeSpliceTxDigest(proposal.spliceTxDraft);
288
283
  const reservation = await wotsLease.reserveKeyUse({
289
284
  treeId: signer.publicKeyDigest,
290
285
  purpose: 'omnia-splice-accept',
package/dist/types.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/package.json CHANGED
@@ -1,14 +1,19 @@
1
1
  {
2
2
  "name": "@totemsdk/omnia-splice",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Channel splicing for @totemsdk/omnia — resize Minima eltoo channels without a close+reopen cycle",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
5
+ "main": "dist/cjs/index.js",
6
+ "types": "dist/cjs/index.d.ts",
7
7
  "exports": {
8
8
  ".": {
9
- "types": "./dist/index.d.ts",
10
- "require": "./dist/index.js",
11
- "import": "./dist/index.js"
9
+ "import": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "require": {
14
+ "types": "./dist/cjs/index.d.ts",
15
+ "default": "./dist/cjs/index.js"
16
+ }
12
17
  }
13
18
  },
14
19
  "files": [
@@ -19,11 +24,12 @@
19
24
  "LICENSE"
20
25
  ],
21
26
  "dependencies": {
22
- "@totemsdk/omnia": "^1.0.0",
23
- "@totemsdk/txpow": "^1.0.0",
24
- "@totemsdk/wots-lease": "^1.0.0",
25
- "@totemsdk/tx-builder": "^0.2.0",
26
- "@totemsdk/core": "^1.2.9"
27
+ "@totemsdk/core": "^1.2.11",
28
+ "@totemsdk/omnia": "^2.0.0",
29
+ "@totemsdk/tx-builder": "^0.2.1",
30
+ "@totemsdk/txpow": "^1.0.2",
31
+ "@totemsdk/wots-lease": "^1.0.1",
32
+ "@totemsdk/storage": "^0.1.0"
27
33
  },
28
34
  "devDependencies": {
29
35
  "@types/jest": "^30.0.0",
@@ -65,8 +71,9 @@
65
71
  "channel-resize",
66
72
  "htlc"
67
73
  ],
74
+ "type": "module",
68
75
  "scripts": {
69
- "build": "npm run build:wasm && tsc && node ../../scripts/clean-wasm-output-ignores.mjs",
76
+ "build": "tsc && tsc -p tsconfig.cjs.json && node -e \"require('fs').writeFileSync('dist/cjs/package.json', JSON.stringify({type:'commonjs'})+'\\n')\"",
70
77
  "clean": "rm -rf dist rust/pkg rust/pkg-node rust/target",
71
78
  "test": "jest",
72
79
  "build:wasm": "cd rust && wasm-pack build --target bundler --out-dir pkg && wasm-pack build --target nodejs --out-dir pkg-node"
Binary file
@@ -24,4 +24,4 @@
24
24
  "./omnia_splice_wasm.js",
25
25
  "./snippets/*"
26
26
  ]
27
- }
27
+ }
@@ -18,4 +18,4 @@
18
18
  "main": "omnia_splice_wasm.js",
19
19
  "homepage": "https://totem.ing",
20
20
  "types": "omnia_splice_wasm.d.ts"
21
- }
21
+ }