@toon-protocol/client 3.2.0 → 4.0.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.
@@ -9,16 +9,18 @@ import {
9
9
  ConfigError,
10
10
  ConnectorError,
11
11
  DEFAULT_DEPOSIT,
12
- DEFAULT_SETTLEMENT_TIMEOUT,
13
12
  DEFAULT_TIMEOUT_MS,
13
+ FacilitatorError,
14
14
  InsufficientBalanceError,
15
15
  InvalidAddressError,
16
16
  NetworkError,
17
17
  PaymentRequiredError,
18
18
  RouteNotPricedError,
19
+ SponsorRefusedError,
19
20
  StaleRpcReadError,
20
21
  ToonClient,
21
22
  ToonClientError,
23
+ TransactionOutcomeError,
22
24
  TransferNotDeliveredError,
23
25
  TransferUnsupportedError,
24
26
  TransportRequiredError,
@@ -29,10 +31,9 @@ import {
29
31
  isInsufficientGasError,
30
32
  resolveConfig,
31
33
  send,
32
- settlementToTerms,
33
34
  toEnvelopeRequest
34
- } from "./chunk-ZHWYFPOP.js";
35
- import "./chunk-7MPONJW5.js";
35
+ } from "./chunk-CG3OXV74.js";
36
+ import "./chunk-WJKJ6Y4N.js";
36
37
  import "./chunk-32MD3CXE.js";
37
38
  export {
38
39
  BeforePayRefusedError,
@@ -45,16 +46,18 @@ export {
45
46
  ConfigError,
46
47
  ConnectorError,
47
48
  DEFAULT_DEPOSIT,
48
- DEFAULT_SETTLEMENT_TIMEOUT,
49
49
  DEFAULT_TIMEOUT_MS,
50
+ FacilitatorError,
50
51
  InsufficientBalanceError,
51
52
  InvalidAddressError,
52
53
  NetworkError,
53
54
  PaymentRequiredError,
54
55
  RouteNotPricedError,
56
+ SponsorRefusedError,
55
57
  StaleRpcReadError,
56
58
  ToonClient,
57
59
  ToonClientError,
60
+ TransactionOutcomeError,
58
61
  TransferNotDeliveredError,
59
62
  TransferUnsupportedError,
60
63
  TransportRequiredError,
@@ -65,7 +68,6 @@ export {
65
68
  isInsufficientGasError,
66
69
  resolveConfig,
67
70
  send,
68
- settlementToTerms,
69
71
  toEnvelopeRequest
70
72
  };
71
- //# sourceMappingURL=client-SCEP67S5.js.map
73
+ //# sourceMappingURL=client-7YIJOR5K.js.map
@@ -0,0 +1,180 @@
1
+ /** Which chain family a channel, a key or a payment lives on. */
2
+ type ChainKind = 'evm' | 'solana';
3
+
4
+ /**
5
+ * `socks5h://` URL parsing, kept apart from the transport that uses it.
6
+ *
7
+ * Config validation has to reject a bad proxy URL, and config validation runs
8
+ * everywhere — including in a browser, where `./socks.js` must never be loaded
9
+ * (it reaches for `node:module` on the first line). This module is pure.
10
+ */
11
+ /**
12
+ * Parses and validates a `socks5h://` URL.
13
+ *
14
+ * Enforces the `socks5h://` scheme, not `socks5://`: the trailing `h` means the
15
+ * *proxy* resolves the hostname. Under plain `socks5://` the client resolves it
16
+ * locally first, which for a `.anyone` address means shipping the hidden service
17
+ * you are about to talk to into a plaintext DNS query — the exact fact the
18
+ * hidden service exists to withhold. Mirrors the connector's `transport/socks-url.ts`.
19
+ */
20
+ declare function validateSocks5hUrl(socksProxy: string): {
21
+ host: string;
22
+ port: number;
23
+ };
24
+
25
+ /**
26
+ * The hidden-service carriage: one SOCKS5h proxy, everything that must ride it.
27
+ *
28
+ * A connector deployed as a hidden service is reachable only through a running
29
+ * `anon` daemon's SOCKS5 port. Three separate things in this client have to go
30
+ * through that port, and — annoyingly — each needs a different object to do it:
31
+ *
32
+ * - **The client edge** (`GET /ilp`, `POST /ilp`) goes through `fetch`, which in
33
+ * Node is undici and takes a **dispatcher**. It does not take an `http.Agent`,
34
+ * which is why the `socks-proxy-agent` this package used to depend on cannot
35
+ * serve this path at all.
36
+ * - **Chain RPC** goes through viem, which calls the *global* `fetch` and accepts
37
+ * no injected one. Its only opening is `http(url, { fetchOptions })`, so it
38
+ * needs the same dispatcher handed to it as a fetch option (ADR 0002).
39
+ * - **The BTP carriage** goes through `ws`, which is `node:http` underneath and
40
+ * therefore takes an `http.Agent` and nothing else.
41
+ *
42
+ * So this module builds one SOCKS connection primitive and wraps it three ways.
43
+ * The SOCKS5 handshake itself is `socks`'s job; all we supply is the socket.
44
+ *
45
+ * Chain RPC gets a transport of its own per chain ({@link createChainRpcTransport}),
46
+ * authenticating with a fixed SOCKS username so each chain rides one pinned
47
+ * circuit (connector ADR 0073). The same is true whether the connector is a
48
+ * hidden service or a clearnet host that a hidden payer reaches through the
49
+ * proxy (TOON_Network#167).
50
+ *
51
+ * `undici` is an optional dependency for exactly this reason: Node bundles undici
52
+ * internally but exposes it under no specifier (`require('undici')` is
53
+ * `MODULE_NOT_FOUND`, `node:undici` is `ERR_UNKNOWN_BUILTIN_MODULE`), so a
54
+ * dispatcher cannot be constructed without the package. A dispatcher built by the
55
+ * userland copy *is* honoured by Node's global `fetch`, which calls
56
+ * `dispatcher.dispatch(opts, handler)` with a handler its own bundled undici
57
+ * defines — and that is why the dependency is pinned to **undici 7, not 8**.
58
+ * Node's handler changed shape: Node 22 (undici 6.22) passes the old
59
+ * `onConnect`/`onHeaders`/`onData`/`onComplete` handler, Node 26 (undici 8.9)
60
+ * passes the new `onRequestStart`/`onResponseStart` one. undici 7 accepts both —
61
+ * its `UnwrapHandler.unwrap` passes an old-style handler through untouched.
62
+ * undici 8 dropped the old shape, and on Node 22 every request through such a
63
+ * dispatcher dies in `assertRequestHandler` with
64
+ * `InvalidArgumentError: invalid onRequestStart method`. Verified green on both
65
+ * Node 22.21 and Node 26.7 against the fake proxy in `socks.test.ts`; do not
66
+ * widen the range to `^8` without re-running those on the oldest supported Node.
67
+ *
68
+ * NODE ONLY. This module is imported dynamically, never statically, from the
69
+ * paths that need it; every Node built-in is pulled in through an ESM-safe
70
+ * `require` built off `import.meta.url`, because this package builds as ESM with
71
+ * these deps external and esbuild rewrites a bare `require` into a `__require`
72
+ * shim that throws. A browser cannot reach a `.anyone` address by any means, so
73
+ * a browser bundler must never follow an edge into this file.
74
+ */
75
+
76
+ /**
77
+ * How long to wait for the proxy to build a circuit to the hidden service.
78
+ *
79
+ * `socks` defaults to 30s, which is too short: an introduction-point circuit to
80
+ * a cold hidden service routinely takes longer, and a too-short connect timeout
81
+ * turns "slow" into "unreachable".
82
+ */
83
+ declare const DEFAULT_HS_CONNECT_TIMEOUT_MS = 120000;
84
+ /**
85
+ * How long a chain-RPC dial may take through the proxy: the SOCKS handshake, an
86
+ * exit circuit and the TCP connect behind it (connector ADR 0073, decision 4).
87
+ *
88
+ * Far shorter than {@link DEFAULT_HS_CONNECT_TIMEOUT_MS}, because a public RPC
89
+ * is reached over an ordinary exit circuit, not the introduction-point dance a
90
+ * hidden service needs. ADR 0073 measured 3,200 calls through `anon`: the worst
91
+ * one that had to build a fresh circuit took 13s. Twice that, rounded, and a
92
+ * stalled circuit is reported rather than waited on.
93
+ */
94
+ declare const DEFAULT_RPC_CONNECT_TIMEOUT_MS = 20000;
95
+ /**
96
+ * The longest a pooled chain-RPC connection is kept idle (ADR 0073, decision 4).
97
+ *
98
+ * A circuit can die quietly under a kept-alive socket, and the next request on
99
+ * it then hangs until its own timeout. A short idle cap means such a socket is
100
+ * dropped before it is reused. undici keeps a socket for 4s by default but lets a
101
+ * server's `Keep-Alive` header stretch that to ten minutes; this caps the stretch.
102
+ */
103
+ declare const RPC_POOL_IDLE_TIMEOUT_MS = 30000;
104
+ /**
105
+ * The fixed SOCKS5 usernames chain RPC authenticates with, one per chain.
106
+ *
107
+ * `anon` (like Tor) isolates streams by SOCKS credentials (`IsolateSOCKSAuth`,
108
+ * on by default): streams that present the same username share a circuit, and
109
+ * streams with different ones never do. So a fixed username per chain gives each
110
+ * chain one circuit that stays pinned until the daemon's own
111
+ * `MaxCircuitDirtiness` rotates it (connector ADR 0073, decision 3). No exit then
112
+ * sees both chains' RPC, neither shares a circuit with the client edge, and no
113
+ * call pays for a new circuit of its own. They differ from the connector's
114
+ * `toon-settlement-*` names, so a payer and a node on one daemon do not share
115
+ * circuits either.
116
+ */
117
+ declare const RPC_SOCKS_USERNAMES: Readonly<Record<ChainKind, string>>;
118
+ /** Options for {@link createHiddenServiceTransport}. */
119
+ interface HiddenServiceTransportOptions {
120
+ /** Circuit-build timeout, ms. Default {@link DEFAULT_HS_CONNECT_TIMEOUT_MS}. */
121
+ connectTimeoutMs?: number;
122
+ /**
123
+ * A fixed SOCKS5 username to authenticate with, which pins every stream this
124
+ * transport opens to one circuit (`IsolateSOCKSAuth`). Unset means no
125
+ * authentication, and the daemon's default circuit.
126
+ */
127
+ socksUsername?: string;
128
+ /**
129
+ * Upper bound on how long an idle pooled connection is kept, ms. Unset keeps
130
+ * undici's own behaviour.
131
+ */
132
+ idleTimeoutMs?: number;
133
+ }
134
+ /**
135
+ * The three shapes of one proxy, plus the handle that shuts it down.
136
+ *
137
+ * Spread `fetch` and `createWebSocket` into a {@link ../client/types.js!ToonClientConfig}
138
+ * to route a client by hand; pass `socksProxy` instead to have the client build
139
+ * this itself.
140
+ */
141
+ interface HiddenServiceTransport {
142
+ /** `fetch`, bound to the proxy. Returns ordinary global `Response` objects. */
143
+ fetch: typeof fetch;
144
+ /** A `ws` factory bound to the proxy, for the BTP carriage. */
145
+ createWebSocket: (url: string) => unknown;
146
+ /**
147
+ * The undici dispatcher behind `fetch`, for handing to something that insists
148
+ * on calling the global `fetch` itself — viem's HTTP transport, in practice
149
+ * (`http(url, { fetchOptions: { dispatcher } })`).
150
+ */
151
+ dispatcher: unknown;
152
+ /** Close the dispatcher's pool and the websocket agents' sockets. */
153
+ close(): Promise<void>;
154
+ }
155
+
156
+ /**
157
+ * Builds every proxy-bound object this client needs from one `socks5h://` URL.
158
+ *
159
+ * @throws {Error} if the URL is not `socks5h://`, or if `undici`/`socks` are not
160
+ * installed — they are optional dependencies, so a consumer who never touches
161
+ * a hidden service never pays for them.
162
+ */
163
+ declare function createHiddenServiceTransport(socksProxy: string, options?: HiddenServiceTransportOptions): HiddenServiceTransport;
164
+ /**
165
+ * The proxy-bound transport one chain's RPC rides: its own pinned circuit, a
166
+ * connect timeout sized for an exit circuit, and a capped idle pool (connector
167
+ * ADR 0073, decisions 3 and 4).
168
+ *
169
+ * It has no fallback. A proxy that is down fails the call; nothing here, or in
170
+ * the viem and Solana paths that use it, ever dials the RPC directly instead.
171
+ */
172
+ declare function createChainRpcTransport(socksProxy: string, chain: ChainKind): HiddenServiceTransport;
173
+ /**
174
+ * Probes the proxy with a plain TCP connect. Fail-closed and fail-early: a
175
+ * client configured for a hidden service must not quietly fall back to clearnet,
176
+ * and finding out at packet time costs a signed claim.
177
+ */
178
+ declare function probeSocks5Proxy(socksProxy: string, timeoutMs?: number): Promise<void>;
179
+
180
+ export { type ChainKind as C, DEFAULT_HS_CONNECT_TIMEOUT_MS as D, type HiddenServiceTransport as H, RPC_POOL_IDLE_TIMEOUT_MS as R, type HiddenServiceTransportOptions as a, DEFAULT_RPC_CONNECT_TIMEOUT_MS as b, RPC_SOCKS_USERNAMES as c, createChainRpcTransport as d, createHiddenServiceTransport as e, probeSocks5Proxy as p, validateSocks5hUrl as v };