@totemsdk/omnia-router 0.4.3 → 0.6.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/cjs/execute.d.ts +34 -7
- package/dist/cjs/execute.js +195 -16
- package/dist/cjs/expiry.d.ts +73 -0
- package/dist/cjs/expiry.js +65 -0
- package/dist/cjs/forward.d.ts +6 -0
- package/dist/cjs/forward.js +4 -0
- package/dist/cjs/index.d.ts +7 -2
- package/dist/cjs/index.js +9 -1
- package/dist/cjs/route-validation.d.ts +26 -2
- package/dist/cjs/route-validation.js +51 -3
- package/dist/cjs/types.d.ts +56 -0
- package/dist/execute.d.ts +34 -7
- package/dist/execute.js +193 -16
- package/dist/expiry.d.ts +73 -0
- package/dist/expiry.js +61 -0
- package/dist/forward.d.ts +6 -0
- package/dist/forward.js +4 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +3 -1
- package/dist/route-validation.d.ts +26 -2
- package/dist/route-validation.js +51 -3
- package/dist/types.d.ts +56 -0
- package/package.json +3 -3
package/dist/expiry.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deadline of the `index`-th HTLC lock, counting from the payer.
|
|
3
|
+
*
|
|
4
|
+
* Timeouts decrease toward the recipient: the downstream HTLC expires first so
|
|
5
|
+
* the intermediary can claim it before its own incoming HTLC becomes
|
|
6
|
+
* refundable.
|
|
7
|
+
*/
|
|
8
|
+
export function hopTimeout(expiryBlock, delta, index) {
|
|
9
|
+
return expiryBlock - BigInt(index) * delta;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Derive the expiry delta for a route and check it fits the requested window.
|
|
13
|
+
*
|
|
14
|
+
* `lockCount` is the number of HTLC locks the execution will place — one per
|
|
15
|
+
* routing hop, two per swap hop — not the number of route hops.
|
|
16
|
+
*/
|
|
17
|
+
export function planRouteExpiry(channels, route, expiryBlock, lockCount, policy = {}) {
|
|
18
|
+
const errors = [];
|
|
19
|
+
let contestDelayBlocks = 0n;
|
|
20
|
+
for (const hop of route.hops) {
|
|
21
|
+
const channel = channels.get(hop.channelId);
|
|
22
|
+
const declared = channel?.contestDelayBlocks;
|
|
23
|
+
if (declared === undefined) {
|
|
24
|
+
if (policy.requireContestDelay) {
|
|
25
|
+
errors.push(`channel ${hop.channelId} does not declare contestDelayBlocks`);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const fallback = BigInt(Math.max(0, Math.trunc(policy.defaultContestDelayBlocks ?? 0)));
|
|
29
|
+
if (fallback > contestDelayBlocks)
|
|
30
|
+
contestDelayBlocks = fallback;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const value = BigInt(Math.max(0, Math.trunc(declared)));
|
|
34
|
+
if (value > contestDelayBlocks)
|
|
35
|
+
contestDelayBlocks = value;
|
|
36
|
+
}
|
|
37
|
+
const recoveryMarginBlocks = BigInt(Math.max(0, Math.trunc(policy.recoveryMarginBlocks ?? 1)));
|
|
38
|
+
const derived = contestDelayBlocks + recoveryMarginBlocks;
|
|
39
|
+
const floor = derived > 0n ? derived : 1n;
|
|
40
|
+
const override = policy.minHopExpiryDelta ?? 0n;
|
|
41
|
+
const delta = override > floor ? override : floor;
|
|
42
|
+
const locks = Math.max(1, lockCount);
|
|
43
|
+
const earliest = expiryBlock - BigInt(locks - 1) * delta;
|
|
44
|
+
if (earliest < 1n) {
|
|
45
|
+
errors.push(`expiry window ${expiryBlock.toString()} cannot fit ${locks} locks `
|
|
46
|
+
+ `at ${delta.toString()} blocks apart`);
|
|
47
|
+
}
|
|
48
|
+
if (policy.tipBlock !== undefined) {
|
|
49
|
+
const lead = policy.minLeadBlocks ?? 0n;
|
|
50
|
+
if (earliest <= policy.tipBlock + lead) {
|
|
51
|
+
errors.push(`earliest HTLC deadline ${earliest.toString()} does not clear the tip `
|
|
52
|
+
+ `${policy.tipBlock.toString()} by ${lead.toString()} blocks`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (errors.length > 0)
|
|
56
|
+
return { ok: false, error: errors.join('; ') };
|
|
57
|
+
return {
|
|
58
|
+
ok: true,
|
|
59
|
+
plan: { delta, contestDelayBlocks, recoveryMarginBlocks, earliest },
|
|
60
|
+
};
|
|
61
|
+
}
|
package/dist/forward.d.ts
CHANGED
|
@@ -34,6 +34,12 @@ export type PeelResult =
|
|
|
34
34
|
onward?: string;
|
|
35
35
|
};
|
|
36
36
|
export interface RoutingEnvelopeCodec {
|
|
37
|
+
/**
|
|
38
|
+
* What a forwarder can learn from the envelope. `cleartext` means the path is
|
|
39
|
+
* visible to every hop it passes through, so a capability manifest must not
|
|
40
|
+
* claim path privacy while this codec is in use (RFC-035 §6.7).
|
|
41
|
+
*/
|
|
42
|
+
readonly pathPrivacy: 'cleartext' | 'onion';
|
|
37
43
|
/** Sender: build the envelope from the onward hops (already excludes the first hop). */
|
|
38
44
|
build(hops: (RoutingHop | SwapHop)[]): string;
|
|
39
45
|
/** Forwarder: peel one hop for `self`. */
|
package/dist/forward.js
CHANGED
|
@@ -18,12 +18,16 @@
|
|
|
18
18
|
*/
|
|
19
19
|
export function createCleartextRoutingCodec() {
|
|
20
20
|
return {
|
|
21
|
+
pathPrivacy: 'cleartext',
|
|
21
22
|
build(hops) {
|
|
22
23
|
const wire = hops.map((h) => ({
|
|
23
24
|
channelId: h.channelId,
|
|
24
25
|
to: h.to,
|
|
25
26
|
amount: h.amount.toString(),
|
|
26
27
|
tokenId: h.tokenId,
|
|
28
|
+
// Without this the forwarder has no bound to apply and would have to
|
|
29
|
+
// reuse the incoming timeout, which can never be strictly earlier.
|
|
30
|
+
...(h.timeoutBlock !== undefined ? { timeoutBlock: h.timeoutBlock.toString() } : {}),
|
|
27
31
|
}));
|
|
28
32
|
return JSON.stringify(wire);
|
|
29
33
|
},
|
package/dist/index.d.ts
CHANGED
|
@@ -2,8 +2,13 @@ export type { RouterChannel, ChannelParty, ChannelHTLC, HTLCStatus, ChannelSigne
|
|
|
2
2
|
export { createChannelGraph, addChannel, removeChannel, announceSwap, getSwapAnnouncements, } from './graph.js';
|
|
3
3
|
export { findRoute, findCrossTokenRoute, applyRate, parseRateToScaled, } from './pathfind.js';
|
|
4
4
|
export { buildPaymentRequest, buildCrossTokenRequest, } from './request.js';
|
|
5
|
-
export { executeMultiHopPayment, executeCrossTokenPayment, cancelPayment, buildRouterFeeProof, computeRouterFeeProofHash, ROUTER_FEE_PROOF_DOMAIN, } from './execute.js';
|
|
6
|
-
export
|
|
5
|
+
export { executeMultiHopPayment, executeCrossTokenPayment, cancelPayment, buildRouterFeeProof, buildAuthenticatedRouterFeeProof, verifyRouterFeeProof, computeRouterFeeProofHash, ROUTER_FEE_PROOF_DOMAIN, PendingRefundError, } from './execute.js';
|
|
6
|
+
export { planRouteExpiry, hopTimeout } from './expiry.js';
|
|
7
|
+
export type { ExpiryPlan, ExpiryPlanResult, ExpiryPolicy } from './expiry.js';
|
|
8
|
+
export { validateRoute } from './route-validation.js';
|
|
9
|
+
export type { RouteBinding, RouteValidationResult } from './route-validation.js';
|
|
10
|
+
export type { RouterFeeProof, RouterSignedState, RouterStateTransition, } from './types.js';
|
|
11
|
+
export type { VerifyRouterFeeProofOptions } from './execute.js';
|
|
7
12
|
export { createCleartextRoutingCodec } from './forward.js';
|
|
8
13
|
export type { RoutingEnvelopeCodec, ForwardHop, PeelResult } from './forward.js';
|
|
9
14
|
export { createDurableRouterLedger, } from './durable-router-ledger.js';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { createChannelGraph, addChannel, removeChannel, announceSwap, getSwapAnnouncements, } from './graph.js';
|
|
2
2
|
export { findRoute, findCrossTokenRoute, applyRate, parseRateToScaled, } from './pathfind.js';
|
|
3
3
|
export { buildPaymentRequest, buildCrossTokenRequest, } from './request.js';
|
|
4
|
-
export { executeMultiHopPayment, executeCrossTokenPayment, cancelPayment, buildRouterFeeProof, computeRouterFeeProofHash, ROUTER_FEE_PROOF_DOMAIN, } from './execute.js';
|
|
4
|
+
export { executeMultiHopPayment, executeCrossTokenPayment, cancelPayment, buildRouterFeeProof, buildAuthenticatedRouterFeeProof, verifyRouterFeeProof, computeRouterFeeProofHash, ROUTER_FEE_PROOF_DOMAIN, PendingRefundError, } from './execute.js';
|
|
5
|
+
export { planRouteExpiry, hopTimeout } from './expiry.js';
|
|
6
|
+
export { validateRoute } from './route-validation.js';
|
|
5
7
|
// RFC-035 B4 — pluggable forwarding envelopes (cleartext now; onion behind the same seam).
|
|
6
8
|
export { createCleartextRoutingCodec } from './forward.js';
|
|
7
9
|
export { createDurableRouterLedger, } from './durable-router-ledger.js';
|
|
@@ -6,6 +6,21 @@
|
|
|
6
6
|
* have the expected direction and have enough sender balance.
|
|
7
7
|
*/
|
|
8
8
|
import type { RouterChannel, RoutingHop, Route, SwapHop } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* What the route is supposed to do, expressed independently of the route object
|
|
11
|
+
* (OMN-080). Without this a structurally valid route that pays nobody still
|
|
12
|
+
* "passes" validation.
|
|
13
|
+
*/
|
|
14
|
+
export interface RouteBinding {
|
|
15
|
+
/** Party the payment starts from. */
|
|
16
|
+
payer?: string;
|
|
17
|
+
/** Party the payment must reach. */
|
|
18
|
+
recipient?: string;
|
|
19
|
+
/** Amount the recipient must receive. */
|
|
20
|
+
amount?: bigint;
|
|
21
|
+
/** Asset the recipient must receive. */
|
|
22
|
+
tokenId?: string;
|
|
23
|
+
}
|
|
9
24
|
export type RouteValidationResult = {
|
|
10
25
|
ok: true;
|
|
11
26
|
} | {
|
|
@@ -20,5 +35,14 @@ export declare function validateRoutingHop(channels: Map<string, RouterChannel>,
|
|
|
20
35
|
export declare function swapIntermediary(channels: Map<string, RouterChannel>, hop: SwapHop): string | undefined;
|
|
21
36
|
/** Validate both legs of a swap hop. */
|
|
22
37
|
export declare function validateSwapHop(channels: Map<string, RouterChannel>, hop: SwapHop): RouteValidationResult;
|
|
23
|
-
/**
|
|
24
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Validate a route against live channel state **and** against what the payment
|
|
40
|
+
* is supposed to do (OMN-080).
|
|
41
|
+
*
|
|
42
|
+
* Per-hop checks answer "can this channel spend?". They do not answer "does
|
|
43
|
+
* this route pay the right party the right amount?" — an empty route, a route
|
|
44
|
+
* whose hops do not connect, or a route that delivers a different amount all
|
|
45
|
+
* used to pass. `binding` supplies the intent; when it is omitted the route is
|
|
46
|
+
* still required to be non-empty and internally consistent.
|
|
47
|
+
*/
|
|
48
|
+
export declare function validateRoute(channels: Map<string, RouterChannel>, route: Route, binding?: RouteBinding): RouteValidationResult;
|
package/dist/route-validation.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
function sameToken(a, b) {
|
|
2
|
+
return a.toLowerCase() === b.toLowerCase();
|
|
3
|
+
}
|
|
1
4
|
function fail(error) {
|
|
2
5
|
return { ok: false, error };
|
|
3
6
|
}
|
|
@@ -71,14 +74,59 @@ export function validateSwapHop(channels, hop) {
|
|
|
71
74
|
}
|
|
72
75
|
return validateChannelSpend(outbound, intermediary, hop.amountOut, hop.tokenOut);
|
|
73
76
|
}
|
|
74
|
-
/**
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Validate a route against live channel state **and** against what the payment
|
|
79
|
+
* is supposed to do (OMN-080).
|
|
80
|
+
*
|
|
81
|
+
* Per-hop checks answer "can this channel spend?". They do not answer "does
|
|
82
|
+
* this route pay the right party the right amount?" — an empty route, a route
|
|
83
|
+
* whose hops do not connect, or a route that delivers a different amount all
|
|
84
|
+
* used to pass. `binding` supplies the intent; when it is omitted the route is
|
|
85
|
+
* still required to be non-empty and internally consistent.
|
|
86
|
+
*/
|
|
87
|
+
export function validateRoute(channels, route, binding = {}) {
|
|
88
|
+
if (!Array.isArray(route.hops) || route.hops.length === 0) {
|
|
89
|
+
return fail('route has no hops');
|
|
90
|
+
}
|
|
91
|
+
for (const [index, hop] of route.hops.entries()) {
|
|
77
92
|
const result = hop.isSwap
|
|
78
93
|
? validateSwapHop(channels, hop)
|
|
79
94
|
: validateRoutingHop(channels, hop);
|
|
80
95
|
if (!result.ok)
|
|
81
96
|
return result;
|
|
97
|
+
const next = route.hops[index + 1];
|
|
98
|
+
if (next && hop.to !== next.from) {
|
|
99
|
+
return fail(`route is disconnected between hops ${index} and ${index + 1}: `
|
|
100
|
+
+ `${hop.to} != ${next.from}`);
|
|
101
|
+
}
|
|
102
|
+
if (next && sameToken(hop.tokenId, next.tokenId) === false) {
|
|
103
|
+
// A token transition is only legitimate through an explicit swap hop.
|
|
104
|
+
const swap = hop.isSwap ? hop : next.isSwap ? next : undefined;
|
|
105
|
+
if (!swap) {
|
|
106
|
+
return fail(`route changes asset between hops ${index} and ${index + 1} `
|
|
107
|
+
+ `(${hop.tokenId} -> ${next.tokenId}) without a swap`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const first = route.hops[0];
|
|
112
|
+
const last = route.hops[route.hops.length - 1];
|
|
113
|
+
if (binding.payer !== undefined && first.from !== binding.payer) {
|
|
114
|
+
return fail(`route starts at ${first.from}, not at the payer ${binding.payer}`);
|
|
115
|
+
}
|
|
116
|
+
if (binding.recipient !== undefined && last.to !== binding.recipient) {
|
|
117
|
+
return fail(`route ends at ${last.to}, not at the recipient ${binding.recipient}`);
|
|
118
|
+
}
|
|
119
|
+
if (binding.amount !== undefined) {
|
|
120
|
+
const delivered = last.isSwap ? last.amountOut : last.amount;
|
|
121
|
+
if (delivered !== binding.amount) {
|
|
122
|
+
return fail(`route delivers ${delivered.toString()}, not the requested ${binding.amount.toString()}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (binding.tokenId !== undefined) {
|
|
126
|
+
const deliveredToken = last.isSwap ? last.tokenOut : last.tokenId;
|
|
127
|
+
if (!sameToken(deliveredToken, binding.tokenId)) {
|
|
128
|
+
return fail(`route delivers ${deliveredToken}, not the requested ${binding.tokenId}`);
|
|
129
|
+
}
|
|
82
130
|
}
|
|
83
131
|
return { ok: true };
|
|
84
132
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -41,6 +41,13 @@ export interface RouterChannel {
|
|
|
41
41
|
currentSequence: number;
|
|
42
42
|
status: string;
|
|
43
43
|
localSigner?: ChannelSigner;
|
|
44
|
+
/**
|
|
45
|
+
* Eltoo contest window embedded in the funding script (OMN-081). The router
|
|
46
|
+
* derives the per-hop HTLC expiry spread from this: an intermediary needs the
|
|
47
|
+
* full contest period plus a recovery margin between its incoming and
|
|
48
|
+
* outgoing deadlines. Defaults to 256 for channels opened by this SDK.
|
|
49
|
+
*/
|
|
50
|
+
contestDelayBlocks?: number;
|
|
44
51
|
}
|
|
45
52
|
/**
|
|
46
53
|
* Parameters for adding an HTLC — mirrors @totemsdk/omnia's AddHTLCParams.
|
|
@@ -75,6 +82,13 @@ export interface ChannelOps {
|
|
|
75
82
|
fulfillHTLC(channel: RouterChannel, htlcId: string, preimage: string, leaseProvider: LeaseProvider): Promise<{
|
|
76
83
|
channel: RouterChannel;
|
|
77
84
|
error?: string;
|
|
85
|
+
/**
|
|
86
|
+
* OMN-083: the authenticated state pair the fulfilment produced. When an
|
|
87
|
+
* implementation can supply it, the router emits an authenticated fee proof;
|
|
88
|
+
* otherwise the proof is marked unauthenticated.
|
|
89
|
+
*/
|
|
90
|
+
previousState?: RouterSignedState;
|
|
91
|
+
nextState?: RouterSignedState;
|
|
78
92
|
}>;
|
|
79
93
|
timeoutHTLC(channel: RouterChannel, htlcId: string, leaseProvider: LeaseProvider): Promise<{
|
|
80
94
|
channel: RouterChannel;
|
|
@@ -123,6 +137,12 @@ export interface RoutingHop {
|
|
|
123
137
|
to: string;
|
|
124
138
|
amount: bigint;
|
|
125
139
|
tokenId: string;
|
|
140
|
+
/**
|
|
141
|
+
* Absolute timeout (block) for an outgoing HTLC on this hop (RFC-035). Each
|
|
142
|
+
* hop must be strictly earlier than its incoming HTLC so a failed route can
|
|
143
|
+
* unwind in order; the routing envelope carries it to the forwarder.
|
|
144
|
+
*/
|
|
145
|
+
timeoutBlock?: bigint;
|
|
126
146
|
/** Edge fee rate (per SCALE); populated by the routing engine. */
|
|
127
147
|
feeRate?: bigint;
|
|
128
148
|
/** Populated during payment execution */
|
|
@@ -177,6 +197,29 @@ export interface PaymentRequest {
|
|
|
177
197
|
* pool's `recordPoolFee` earn-proof consumes. Binds channel + HTLC + recipient
|
|
178
198
|
* + amount so a fee record traces to a real fulfilled payment.
|
|
179
199
|
*/
|
|
200
|
+
/**
|
|
201
|
+
* A channel state the router can reason about without depending on `omnia`
|
|
202
|
+
* (OMN-083). Only the fields the fee proof binds are modelled.
|
|
203
|
+
*/
|
|
204
|
+
export interface RouterSignedState {
|
|
205
|
+
sequence: number;
|
|
206
|
+
balances: Record<string, bigint>;
|
|
207
|
+
pendingHTLCs?: Array<{
|
|
208
|
+
htlcId: string;
|
|
209
|
+
amount: bigint;
|
|
210
|
+
recipientPublicKeyDigest?: string;
|
|
211
|
+
status?: string;
|
|
212
|
+
}>;
|
|
213
|
+
stateVariables?: Array<{
|
|
214
|
+
port: number;
|
|
215
|
+
value: unknown;
|
|
216
|
+
}>;
|
|
217
|
+
signatures?: Record<string, unknown>;
|
|
218
|
+
}
|
|
219
|
+
export interface RouterStateTransition {
|
|
220
|
+
previous: RouterSignedState;
|
|
221
|
+
next: RouterSignedState;
|
|
222
|
+
}
|
|
180
223
|
export interface RouterFeeProof {
|
|
181
224
|
channelId: string;
|
|
182
225
|
htlcId: string;
|
|
@@ -184,6 +227,19 @@ export interface RouterFeeProof {
|
|
|
184
227
|
amount: bigint;
|
|
185
228
|
tokenId: string;
|
|
186
229
|
settledAt: number;
|
|
230
|
+
/**
|
|
231
|
+
* OMN-083: true only when the proof was derived from an authenticated
|
|
232
|
+
* predecessor→successor transition. A hash over descriptive fields is not
|
|
233
|
+
* settlement evidence, so `false` proofs must not be treated as income.
|
|
234
|
+
*/
|
|
235
|
+
authenticated: boolean;
|
|
236
|
+
/** Fee the intermediary retained on this hop, when both legs are known. */
|
|
237
|
+
feeAmount?: bigint;
|
|
238
|
+
previousSequence?: number;
|
|
239
|
+
sequence?: number;
|
|
240
|
+
previousStateCommitmentV2?: string;
|
|
241
|
+
stateCommitmentV2?: string;
|
|
242
|
+
balanceDelta?: bigint;
|
|
187
243
|
proofHash: string;
|
|
188
244
|
}
|
|
189
245
|
/** An HTLC that failed to release during rollback and is still locked. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totemsdk/omnia-router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Multi-hop payment routing over Omnia channels — single-token and cross-token paths",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"types": "dist/cjs/index.d.ts",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@totemsdk/core": "^1.6.1",
|
|
27
|
-
"@totemsdk/storage": "^0.2.
|
|
27
|
+
"@totemsdk/storage": "^0.2.3"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@totemsdk/omnia": "^
|
|
30
|
+
"@totemsdk/omnia": "^4.0.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependenciesMeta": {
|
|
33
33
|
"@totemsdk/omnia": {
|