@totemsdk/omnia-router 0.5.0 → 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/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 type { RouterFeeProof } from './types.js';
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
- /** Validate every hop of a route against live channel state. */
24
- export declare function validateRoute(channels: Map<string, RouterChannel>, route: Route): RouteValidationResult;
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;
@@ -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
- /** Validate every hop of a route against live channel state. */
75
- export function validateRoute(channels, route) {
76
- for (const hop of route.hops) {
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;
@@ -183,6 +197,29 @@ export interface PaymentRequest {
183
197
  * pool's `recordPoolFee` earn-proof consumes. Binds channel + HTLC + recipient
184
198
  * + amount so a fee record traces to a real fulfilled payment.
185
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
+ }
186
223
  export interface RouterFeeProof {
187
224
  channelId: string;
188
225
  htlcId: string;
@@ -190,6 +227,19 @@ export interface RouterFeeProof {
190
227
  amount: bigint;
191
228
  tokenId: string;
192
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;
193
243
  proofHash: string;
194
244
  }
195
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.5.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",
@@ -27,7 +27,7 @@
27
27
  "@totemsdk/storage": "^0.2.3"
28
28
  },
29
29
  "peerDependencies": {
30
- "@totemsdk/omnia": "^3.0.0"
30
+ "@totemsdk/omnia": "^4.0.0"
31
31
  },
32
32
  "peerDependenciesMeta": {
33
33
  "@totemsdk/omnia": {