@totemsdk/omnia-router 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 +4 -4
- package/dist/execute.js +8 -3
- package/dist/graph.js +14 -7
- package/dist/index.js +21 -4
- package/dist/pathfind.js +12 -6
- package/dist/request.js +9 -5
- package/dist/types.js +2 -1
- package/package.json +34 -5
- package/src/__tests__/router.test.ts +0 -877
- package/src/execute.ts +0 -329
- package/src/graph.ts +0 -109
- package/src/index.ts +0 -46
- package/src/pathfind.ts +0 -314
- package/src/request.ts +0 -48
- package/src/types.ts +0 -221
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`](
|
|
93
|
-
- [`@totemsdk/omnia-factory`](
|
|
94
|
-
- [`@totemsdk/omnia-hyperswarm`](
|
|
95
|
-
- [`@totemsdk/lookup-node`](
|
|
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-hyperswarm`](https://www.npmjs.com/package/@totemsdk/omnia-hyperswarm) — P2P transport 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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/package.json
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totemsdk/omnia-router",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
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
|
-
"
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
17
18
|
],
|
|
18
19
|
"dependencies": {
|
|
19
20
|
"@totemsdk/core": "1.0.9"
|
|
20
21
|
},
|
|
21
22
|
"peerDependencies": {
|
|
22
|
-
"@totemsdk/omnia": "0.1.3",
|
|
23
|
-
"@totemsdk/omnia-hyperswarm": "0.
|
|
23
|
+
"@totemsdk/omnia": ">=0.1.3",
|
|
24
|
+
"@totemsdk/omnia-hyperswarm": ">=0.2.0"
|
|
24
25
|
},
|
|
25
26
|
"peerDependenciesMeta": {
|
|
26
27
|
"@totemsdk/omnia": {
|
|
@@ -44,6 +45,34 @@
|
|
|
44
45
|
"access": "public"
|
|
45
46
|
},
|
|
46
47
|
"license": "MIT",
|
|
48
|
+
"author": "Totem SDK",
|
|
49
|
+
"homepage": "https://totemsdk.com",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/MrGheek/axia-totem/issues"
|
|
52
|
+
},
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "git+https://github.com/MrGheek/axia-totem.git",
|
|
56
|
+
"directory": "packages/totem-sdk/packages/omnia-router"
|
|
57
|
+
},
|
|
58
|
+
"keywords": [
|
|
59
|
+
"totem",
|
|
60
|
+
"totemsdk",
|
|
61
|
+
"minima",
|
|
62
|
+
"blockchain",
|
|
63
|
+
"quantum-resistant",
|
|
64
|
+
"wots",
|
|
65
|
+
"kissvm",
|
|
66
|
+
"utxo",
|
|
67
|
+
"omnia",
|
|
68
|
+
"payment-channel",
|
|
69
|
+
"eltoo",
|
|
70
|
+
"layer2",
|
|
71
|
+
"routing",
|
|
72
|
+
"multi-hop",
|
|
73
|
+
"htlc",
|
|
74
|
+
"dijkstra"
|
|
75
|
+
],
|
|
47
76
|
"scripts": {
|
|
48
77
|
"build": "tsc",
|
|
49
78
|
"clean": "rm -rf dist",
|