@totemsdk/omnia-router 0.1.3 → 0.1.5

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 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
@@ -89,7 +89,7 @@ await announceSwap({
89
89
 
90
90
  ## See also
91
91
 
92
- - [`@totemsdk/omnia`](../omnia) — channel state machine powering each hop
93
- - [`@totemsdk/omnia-factory`](../omnia-factory) — factory channels included in the routing graph
94
- - [`@totemsdk/omnia-hyperswarm`](../omnia-hyperswarm) — P2P transport for propagating routes
95
- - [`@totemsdk/lookup-node`](../lookup-node) — peer discovery for channel graph construction
92
+ - [`@totemsdk/omnia`](https://www.npmjs.com/package/@totemsdk/omnia) — channel state machine powering each hop
93
+ - [`@totemsdk/omnia-factory`](https://www.npmjs.com/package/@totemsdk/omnia-factory) — factory channels included in the routing graph
94
+ - [`@totemsdk/omnia`](https://www.npmjs.com/package/@totemsdk/omnia) — Omnia messaging layer for propagating routes
95
+ - [`@totemsdk/lookup-node`](https://www.npmjs.com/package/@totemsdk/lookup-node) — peer discovery for channel graph construction
package/dist/execute.js CHANGED
@@ -1,3 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.executeMultiHopPayment = executeMultiHopPayment;
4
+ exports.cancelPayment = cancelPayment;
5
+ exports.executeCrossTokenPayment = executeCrossTokenPayment;
1
6
  // ─── Type guard ───────────────────────────────────────────────────────────────
2
7
  function isSwapHop(hop) {
3
8
  return hop.isSwap === true;
@@ -44,7 +49,7 @@ async function cancelHtlcs(ops, channels, locked, leaseProviders) {
44
49
  *
45
50
  * The `channels` map is updated in-place after each HTLC operation.
46
51
  */
47
- export async function executeMultiHopPayment(ops, channels, route, paymentRequest, leaseProviders) {
52
+ async function executeMultiHopPayment(ops, channels, route, paymentRequest, leaseProviders) {
48
53
  const preimage = paymentRequest.preimage;
49
54
  if (!preimage) {
50
55
  return { success: false, error: 'paymentRequest.preimage is required for execution', settledHops: [] };
@@ -121,7 +126,7 @@ export async function executeMultiHopPayment(ops, channels, route, paymentReques
121
126
  * Cancel all pending HTLCs on a route by calling timeoutHTLC on each hop.
122
127
  * Call this explicitly to roll back before or without attempting execution.
123
128
  */
124
- export async function cancelPayment(ops, channels, route, leaseProviders) {
129
+ async function cancelPayment(ops, channels, route, leaseProviders) {
125
130
  const hopsWithHtlc = route.hops.filter(h => h.htlcId && !isSwapHop(h));
126
131
  await cancelHtlcs(ops, channels, hopsWithHtlc.map(h => ({ channelId: h.channelId, htlcId: h.htlcId })), leaseProviders);
127
132
  }
@@ -141,7 +146,7 @@ export async function cancelPayment(ops, channels, route, leaseProviders) {
141
146
  * Rollback fires on ANY failure (forward OR backward phase) by timing out
142
147
  * all still-pending locked HTLCs.
143
148
  */
144
- export async function executeCrossTokenPayment(ops, channels, route, paymentRequest, leaseProviders) {
149
+ async function executeCrossTokenPayment(ops, channels, route, paymentRequest, leaseProviders) {
145
150
  const preimage = paymentRequest.preimage;
146
151
  if (!preimage) {
147
152
  return { success: false, error: 'paymentRequest.preimage is required for execution', settledHops: [] };
package/dist/graph.js CHANGED
@@ -1,6 +1,13 @@
1
- import { parseRateToScaled } from './pathfind.js';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createChannelGraph = createChannelGraph;
4
+ exports.addChannel = addChannel;
5
+ exports.removeChannel = removeChannel;
6
+ exports.announceSwap = announceSwap;
7
+ exports.getSwapAnnouncements = getSwapAnnouncements;
8
+ const pathfind_js_1 = require("./pathfind.js");
2
9
  /** Create an empty ChannelGraph. */
3
- export function createChannelGraph() {
10
+ function createChannelGraph() {
4
11
  return {
5
12
  nodeEdges: new Map(),
6
13
  edgesByChannel: new Map(),
@@ -16,7 +23,7 @@ export function createChannelGraph() {
16
23
  * `addChannel` twice with the same `(channelId, from)` replaces the first
17
24
  * entry (balance update semantics).
18
25
  */
19
- export function addChannel(graph, edge) {
26
+ function addChannel(graph, edge) {
20
27
  const existing = graph.edgesByChannel.get(edge.channelId) ?? [];
21
28
  // Replace any edge for the same (channelId, from) direction.
22
29
  const idx = existing.findIndex(e => e.from === edge.from);
@@ -37,7 +44,7 @@ export function addChannel(graph, edge) {
37
44
  * Remove ALL directed edges for `channelId` (i.e. both directions).
38
45
  * No-op if the channelId is not present.
39
46
  */
40
- export function removeChannel(graph, channelId) {
47
+ function removeChannel(graph, channelId) {
41
48
  const edges = graph.edgesByChannel.get(channelId);
42
49
  if (!edges)
43
50
  return;
@@ -67,8 +74,8 @@ function _removeFromNodeEdges(graph, edge) {
67
74
  * Duplicate announcements (same `intermediaryPubKey` + `inboundChannelId`)
68
75
  * are replaced to prevent stale rate data.
69
76
  */
70
- export function announceSwap(graph, announcement) {
71
- const rateScaled = parseRateToScaled(announcement.rate);
77
+ function announceSwap(graph, announcement) {
78
+ const rateScaled = (0, pathfind_js_1.parseRateToScaled)(announcement.rate);
72
79
  if (rateScaled <= 0n) {
73
80
  throw new Error(`Invalid swap rate "${announcement.rate}" from intermediary ${announcement.intermediaryPubKey}: rate must be positive`);
74
81
  }
@@ -87,7 +94,7 @@ export function announceSwap(graph, announcement) {
87
94
  /**
88
95
  * Return all swap announcements for a given token pair, or an empty array.
89
96
  */
90
- export function getSwapAnnouncements(graph, tokenIn, tokenOut) {
97
+ function getSwapAnnouncements(graph, tokenIn, tokenOut) {
91
98
  return graph.swapIndex.get(_swapKey(tokenIn, tokenOut)) ?? [];
92
99
  }
93
100
  function _swapKey(tokenIn, tokenOut) {
package/dist/index.js CHANGED
@@ -1,4 +1,21 @@
1
- export { createChannelGraph, addChannel, removeChannel, announceSwap, getSwapAnnouncements, } from './graph.js';
2
- export { findRoute, findCrossTokenRoute, applyRate, parseRateToScaled, } from './pathfind.js';
3
- export { buildPaymentRequest, buildCrossTokenRequest, } from './request.js';
4
- export { executeMultiHopPayment, executeCrossTokenPayment, cancelPayment, } from './execute.js';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cancelPayment = exports.executeCrossTokenPayment = exports.executeMultiHopPayment = exports.buildCrossTokenRequest = exports.buildPaymentRequest = exports.parseRateToScaled = exports.applyRate = exports.findCrossTokenRoute = exports.findRoute = exports.getSwapAnnouncements = exports.announceSwap = exports.removeChannel = exports.addChannel = exports.createChannelGraph = void 0;
4
+ var graph_js_1 = require("./graph.js");
5
+ Object.defineProperty(exports, "createChannelGraph", { enumerable: true, get: function () { return graph_js_1.createChannelGraph; } });
6
+ Object.defineProperty(exports, "addChannel", { enumerable: true, get: function () { return graph_js_1.addChannel; } });
7
+ Object.defineProperty(exports, "removeChannel", { enumerable: true, get: function () { return graph_js_1.removeChannel; } });
8
+ Object.defineProperty(exports, "announceSwap", { enumerable: true, get: function () { return graph_js_1.announceSwap; } });
9
+ Object.defineProperty(exports, "getSwapAnnouncements", { enumerable: true, get: function () { return graph_js_1.getSwapAnnouncements; } });
10
+ var pathfind_js_1 = require("./pathfind.js");
11
+ Object.defineProperty(exports, "findRoute", { enumerable: true, get: function () { return pathfind_js_1.findRoute; } });
12
+ Object.defineProperty(exports, "findCrossTokenRoute", { enumerable: true, get: function () { return pathfind_js_1.findCrossTokenRoute; } });
13
+ Object.defineProperty(exports, "applyRate", { enumerable: true, get: function () { return pathfind_js_1.applyRate; } });
14
+ Object.defineProperty(exports, "parseRateToScaled", { enumerable: true, get: function () { return pathfind_js_1.parseRateToScaled; } });
15
+ var request_js_1 = require("./request.js");
16
+ Object.defineProperty(exports, "buildPaymentRequest", { enumerable: true, get: function () { return request_js_1.buildPaymentRequest; } });
17
+ Object.defineProperty(exports, "buildCrossTokenRequest", { enumerable: true, get: function () { return request_js_1.buildCrossTokenRequest; } });
18
+ var execute_js_1 = require("./execute.js");
19
+ Object.defineProperty(exports, "executeMultiHopPayment", { enumerable: true, get: function () { return execute_js_1.executeMultiHopPayment; } });
20
+ Object.defineProperty(exports, "executeCrossTokenPayment", { enumerable: true, get: function () { return execute_js_1.executeCrossTokenPayment; } });
21
+ Object.defineProperty(exports, "cancelPayment", { enumerable: true, get: function () { return execute_js_1.cancelPayment; } });
package/dist/pathfind.js CHANGED
@@ -1,11 +1,17 @@
1
- import { getSwapAnnouncements } from './graph.js';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseRateToScaled = parseRateToScaled;
4
+ exports.applyRate = applyRate;
5
+ exports.findRoute = findRoute;
6
+ exports.findCrossTokenRoute = findCrossTokenRoute;
7
+ const graph_js_1 = require("./graph.js");
2
8
  const SCALE = 100000000n;
3
9
  // ─── Numeric helpers ──────────────────────────────────────────────────────────
4
10
  /**
5
11
  * Parse a decimal rate string to a scaled bigint (SCALE = 10^8).
6
12
  * "0.95" → 95_000_000n, "1.5" → 150_000_000n, "2" → 200_000_000n.
7
13
  */
8
- export function parseRateToScaled(rate) {
14
+ function parseRateToScaled(rate) {
9
15
  const neg = rate.startsWith('-');
10
16
  const abs = neg ? rate.slice(1) : rate;
11
17
  const dot = abs.indexOf('.');
@@ -17,7 +23,7 @@ export function parseRateToScaled(rate) {
17
23
  return neg ? -result : result;
18
24
  }
19
25
  /** Apply rate to amount: amountOut = amountIn × rateScaled / SCALE */
20
- export function applyRate(amountIn, rate) {
26
+ function applyRate(amountIn, rate) {
21
27
  const rateScaled = parseRateToScaled(rate);
22
28
  return (amountIn * rateScaled) / SCALE;
23
29
  }
@@ -28,7 +34,7 @@ export function applyRate(amountIn, rate) {
28
34
  *
29
35
  * Returns null if no path exists within maxHops.
30
36
  */
31
- export function findRoute(graph, from, to, amount, tokenId, opts) {
37
+ function findRoute(graph, from, to, amount, tokenId, opts) {
32
38
  return _reconstruct(graph, from, to, amount, tokenId, opts?.maxHops ?? 8);
33
39
  }
34
40
  /** Full path reconstruction (used once Dijkstra confirms a route exists). */
@@ -92,8 +98,8 @@ function _reconstruct(graph, from, to, amount, tokenId, maxHops) {
92
98
  * Selection criteria: lowest total fee (including swap fee), then fewest hops,
93
99
  * then fewest swap hops.
94
100
  */
95
- export function findCrossTokenRoute(graph, from, to, amountIn, tokenIn, tokenOut, opts) {
96
- const announcements = getSwapAnnouncements(graph, tokenIn, tokenOut);
101
+ function findCrossTokenRoute(graph, from, to, amountIn, tokenIn, tokenOut, opts) {
102
+ const announcements = (0, graph_js_1.getSwapAnnouncements)(graph, tokenIn, tokenOut);
97
103
  if (announcements.length === 0)
98
104
  return null;
99
105
  let best = null;
package/dist/request.js CHANGED
@@ -1,4 +1,8 @@
1
- import { F, hex } from '@totemsdk/core';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildPaymentRequest = buildPaymentRequest;
4
+ exports.buildCrossTokenRequest = buildCrossTokenRequest;
5
+ const core_1 = require("@totemsdk/core");
2
6
  /**
3
7
  * Generate a random 32-byte preimage, compute SHA3-256(preimage) as the
4
8
  * hashlock, and return a PaymentRequest that is ready to share.
@@ -10,11 +14,11 @@ import { F, hex } from '@totemsdk/core';
10
14
  * Compatible with Bare/Pear environments: uses globalThis.crypto.getRandomValues
11
15
  * (Web Crypto API, available in Node ≥18, browsers, and Pear/Bare runtimes).
12
16
  */
13
- export function buildPaymentRequest(amount, tokenId, expiryBlock, description) {
17
+ function buildPaymentRequest(amount, tokenId, expiryBlock, description) {
14
18
  const preimageBytes = new Uint8Array(32);
15
19
  globalThis.crypto.getRandomValues(preimageBytes);
16
- const preimage = '0x' + hex(preimageBytes);
17
- const hashlock = '0x' + hex(F(preimageBytes));
20
+ const preimage = '0x' + (0, core_1.hex)(preimageBytes);
21
+ const hashlock = '0x' + (0, core_1.hex)((0, core_1.F)(preimageBytes));
18
22
  return {
19
23
  hashlock,
20
24
  preimage,
@@ -29,6 +33,6 @@ export function buildPaymentRequest(amount, tokenId, expiryBlock, description) {
29
33
  * to receive. The API is identical to buildPaymentRequest — the distinction
30
34
  * is conceptual (recipient generates hashlock; sender sources liquidity).
31
35
  */
32
- export function buildCrossTokenRequest(amountOut, tokenOut, expiryBlock, description) {
36
+ function buildCrossTokenRequest(amountOut, tokenOut, expiryBlock, description) {
33
37
  return buildPaymentRequest(amountOut, tokenOut, expiryBlock, description);
34
38
  }
package/dist/types.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  /**
2
3
  * @totemsdk/omnia-router — public types
3
4
  *
@@ -6,4 +7,4 @@
6
7
  * has no hard runtime dependency on @totemsdk/omnia — callers pass the
7
8
  * real objects (structural typing ensures compatibility).
8
9
  */
9
- export {};
10
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,41 +1,38 @@
1
1
  {
2
2
  "name": "@totemsdk/omnia-router",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Multi-hop payment routing over Omnia channels — single-token and cross-token paths",
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
- "src"
16
+ "README.md",
17
+ "LICENSE"
17
18
  ],
18
19
  "dependencies": {
19
- "@totemsdk/core": "1.0.9"
20
+ "@totemsdk/core": "^1.2.7"
20
21
  },
21
22
  "peerDependencies": {
22
- "@totemsdk/omnia": "0.1.3",
23
- "@totemsdk/omnia-hyperswarm": "0.1.3"
23
+ "@totemsdk/omnia": "^0.1.9"
24
24
  },
25
25
  "peerDependenciesMeta": {
26
26
  "@totemsdk/omnia": {
27
27
  "optional": true
28
- },
29
- "@totemsdk/omnia-hyperswarm": {
30
- "optional": true
31
28
  }
32
29
  },
33
30
  "devDependencies": {
34
- "@types/jest": "^29.0.0",
31
+ "@types/jest": "^30.0.0",
35
32
  "@types/node": "^20.0.0",
36
- "jest": "^29.0.0",
33
+ "jest": "^30.4.2",
37
34
  "ts-jest": "^29.0.0",
38
- "typescript": "^5.0.0"
35
+ "typescript": "^7.0.2"
39
36
  },
40
37
  "engines": {
41
38
  "node": ">=18.0.0"
@@ -44,9 +41,37 @@
44
41
  "access": "public"
45
42
  },
46
43
  "license": "MIT",
44
+ "author": "Totem SDK",
45
+ "homepage": "https://totem.ing",
46
+ "bugs": {
47
+ "url": "https://github.com/totem-sdk/totem-sdk/issues"
48
+ },
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "git+https://github.com/totem-sdk/totem-sdk.git",
52
+ "directory": "packages/omnia-router"
53
+ },
54
+ "keywords": [
55
+ "totem",
56
+ "totemsdk",
57
+ "minima",
58
+ "blockchain",
59
+ "quantum-resistant",
60
+ "wots",
61
+ "utxo",
62
+ "omnia",
63
+ "payment-channel",
64
+ "eltoo",
65
+ "layer2",
66
+ "routing",
67
+ "multi-hop",
68
+ "htlc",
69
+ "dijkstra"
70
+ ],
47
71
  "scripts": {
48
72
  "build": "tsc",
49
- "clean": "rm -rf dist",
50
- "test": "node ../../node_modules/jest/bin/jest.js --config jest.config.cjs --passWithNoTests"
73
+ "build:go": "cd go && go build -o ../bin/omnia-router ./cmd/omnia-router",
74
+ "clean": "rm -rf dist bin",
75
+ "test": "jest"
51
76
  }
52
77
  }