@toon-protocol/client 2.1.1 → 2.2.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.
@@ -1,11 +1,16 @@
1
1
  import {
2
2
  DEVNET,
3
+ assertRoutableHsHostname,
3
4
  base58Decode,
4
5
  base58Encode,
5
6
  defaultRpcUrl,
6
7
  deriveFullIdentity,
7
- evmIdentityFromKey
8
- } from "./chunk-J6JVCKNV.js";
8
+ evmIdentityFromKey,
9
+ isHiddenServiceUrl
10
+ } from "./chunk-7MPONJW5.js";
11
+ import {
12
+ validateSocks5hUrl
13
+ } from "./chunk-32MD3CXE.js";
9
14
 
10
15
  // src/client/errors.ts
11
16
  var ToonClientError = class extends Error {
@@ -710,8 +715,9 @@ function resolveExecutionCondition(condition) {
710
715
  if (condition === void 0) return void 0;
711
716
  return typeof condition === "string" ? fromBase64(condition) : condition;
712
717
  }
718
+ var PACKET_EXPIRY_HEADROOM_MS = 15e3;
713
719
  function resolveExpiresAt(expiresAt, timeoutMs) {
714
- if (expiresAt === void 0) return new Date(Date.now() + timeoutMs);
720
+ if (expiresAt === void 0) return new Date(Date.now() + timeoutMs + PACKET_EXPIRY_HEADROOM_MS);
715
721
  return expiresAt instanceof Date ? new Date(expiresAt.getTime()) : new Date(expiresAt);
716
722
  }
717
723
  var FULFILLMENT_MISMATCH_CODE = "F99";
@@ -2653,11 +2659,11 @@ function parseConnectorRouteTerms(body) {
2653
2659
 
2654
2660
  // src/btp/transport-select.ts
2655
2661
  function selectTransport(description, preference = "auto", baseUrl = description.readFrom) {
2656
- const http4 = resolveEndpoint2(description.httpEndpoint, baseUrl);
2662
+ const http2 = resolveEndpoint2(description.httpEndpoint, baseUrl);
2657
2663
  const btp = resolveEndpoint2(description.btpEndpoint, baseUrl);
2658
2664
  const required = description.requiredTransport;
2659
2665
  if (preference === "http" || preference === "btp") {
2660
- const url = preference === "http" ? http4 : btp;
2666
+ const url = preference === "http" ? http2 : btp;
2661
2667
  if (url === void 0) {
2662
2668
  throw new TransportRequiredError(
2663
2669
  `the connector publishes no ${preference} endpoint` + (required !== void 0 ? `; its self-description requires the ${required} transport` : ""),
@@ -2667,7 +2673,7 @@ function selectTransport(description, preference = "auto", baseUrl = description
2667
2673
  return { kind: preference, url };
2668
2674
  }
2669
2675
  if (required !== void 0) {
2670
- const url = required === "http" ? http4 : btp;
2676
+ const url = required === "http" ? http2 : btp;
2671
2677
  if (url === void 0) {
2672
2678
  throw new TransportRequiredError(
2673
2679
  `the connector requires the ${required} transport but publishes no ${required} endpoint`,
@@ -2676,7 +2682,7 @@ function selectTransport(description, preference = "auto", baseUrl = description
2676
2682
  }
2677
2683
  return { kind: required, url };
2678
2684
  }
2679
- if (http4 !== void 0) return { kind: "http", url: http4 };
2685
+ if (http2 !== void 0) return { kind: "http", url: http2 };
2680
2686
  if (btp !== void 0) return { kind: "btp", url: btp };
2681
2687
  throw new TransportRequiredError(
2682
2688
  "the connector publishes neither an httpEndpoint nor a btpEndpoint"
@@ -3518,11 +3524,25 @@ var ChannelManager = class _ChannelManager {
3518
3524
  // src/channel/OnChainChannelClient.ts
3519
3525
  import { ed25519 as ed255192 } from "@noble/curves/ed25519.js";
3520
3526
 
3527
+ // src/transport/rpc.ts
3528
+ import { http } from "viem";
3529
+ function rpcTransport(url, dispatcher, options = {}) {
3530
+ const { timeout, retryCount } = options;
3531
+ return http(url, {
3532
+ ...timeout !== void 0 ? { timeout } : {},
3533
+ ...retryCount !== void 0 ? { retryCount } : {},
3534
+ ...dispatcher !== void 0 ? { fetchOptions: { dispatcher } } : {}
3535
+ });
3536
+ }
3537
+ function rpcFetch(dispatcher, fetchImpl = globalThis.fetch) {
3538
+ if (dispatcher === void 0) return fetchImpl;
3539
+ return ((input, init) => fetchImpl(input, { ...init, dispatcher }));
3540
+ }
3541
+
3521
3542
  // src/channel/evm/TokenNetworkClient.ts
3522
3543
  import {
3523
3544
  createPublicClient,
3524
3545
  createWalletClient,
3525
- http,
3526
3546
  maxUint256,
3527
3547
  decodeEventLog,
3528
3548
  defineChain
@@ -3721,10 +3741,13 @@ var TokenNetworkClient = class {
3721
3741
  nativeCurrency: { name: "ETH", symbol: "ETH", decimals: 18 },
3722
3742
  rpcUrls: { default: { http: [config.rpcUrl] } }
3723
3743
  });
3724
- this.publicClient = createPublicClient({ transport: http(config.rpcUrl), chain: viemChain });
3744
+ this.publicClient = createPublicClient({
3745
+ transport: rpcTransport(config.rpcUrl, config.rpcDispatcher),
3746
+ chain: viemChain
3747
+ });
3725
3748
  this.walletClient = createWalletClient({
3726
3749
  account: config.signer.account,
3727
- transport: http(config.rpcUrl),
3750
+ transport: rpcTransport(config.rpcUrl, config.rpcDispatcher),
3728
3751
  chain: viemChain
3729
3752
  });
3730
3753
  }
@@ -4287,8 +4310,13 @@ function isAccountNotFoundError(err) {
4287
4310
  err.rpcMessage
4288
4311
  );
4289
4312
  }
4313
+ function resolveRpcTarget(target) {
4314
+ if (typeof target === "string") return { url: target, fetchImpl: globalThis.fetch };
4315
+ return { url: target.url, fetchImpl: target.fetchImpl ?? globalThis.fetch };
4316
+ }
4290
4317
  async function solanaRpc(rpcUrl, method, params = []) {
4291
- const res = await fetch(rpcUrl, {
4318
+ const { url, fetchImpl } = resolveRpcTarget(rpcUrl);
4319
+ const res = await fetchImpl(url, {
4292
4320
  method: "POST",
4293
4321
  headers: { "Content-Type": "application/json" },
4294
4322
  body: JSON.stringify({
@@ -4861,8 +4889,15 @@ var DEFAULT_CHALLENGE_DURATION = 3600n;
4861
4889
  var SolanaChannelClient = class {
4862
4890
  constructor(config) {
4863
4891
  this.config = config;
4892
+ this.rpc = config.rpcFetch === void 0 ? config.rpcUrl : { url: config.rpcUrl, fetchImpl: config.rpcFetch };
4864
4893
  }
4865
4894
  config;
4895
+ /**
4896
+ * Where this client's JSON-RPC goes, and through which `fetch`. Built once:
4897
+ * every call below passes it straight to `payment-channel.js`, which is the
4898
+ * only place that looks inside it.
4899
+ */
4900
+ rpc;
4866
4901
  /**
4867
4902
  * The channel PDA for a counterparty — the claim's `channelAccount`, and the
4868
4903
  * account the connector reads. Deterministic from the pair and the mint, and
@@ -4909,7 +4944,7 @@ var SolanaChannelClient = class {
4909
4944
  payerTokenAccount: params.payerTokenAccount ?? this.ownTokenAccount()
4910
4945
  } : void 0;
4911
4946
  const result = await openSolanaChannel({
4912
- rpcUrl: this.config.rpcUrl,
4947
+ rpcUrl: this.rpc,
4913
4948
  programId: this.config.programId,
4914
4949
  tokenMint: this.config.tokenMint,
4915
4950
  payerSeed: this.config.payerSeed,
@@ -4927,7 +4962,7 @@ var SolanaChannelClient = class {
4927
4962
  /** Add collateral to an existing channel. Incremental, not a target. */
4928
4963
  async deposit(channelId, amount, payerTokenAccount) {
4929
4964
  const { depositTxSignature } = await depositSolanaChannel({
4930
- rpcUrl: this.config.rpcUrl,
4965
+ rpcUrl: this.rpc,
4931
4966
  programId: this.config.programId,
4932
4967
  channelPDA: channelId,
4933
4968
  payerSeed: this.config.payerSeed,
@@ -4943,7 +4978,7 @@ var SolanaChannelClient = class {
4943
4978
  */
4944
4979
  async close(channelId) {
4945
4980
  const { closeTxSignature } = await closeSolanaChannel({
4946
- rpcUrl: this.config.rpcUrl,
4981
+ rpcUrl: this.rpc,
4947
4982
  programId: this.config.programId,
4948
4983
  channelPDA: channelId,
4949
4984
  closerSeed: this.config.payerSeed,
@@ -4973,7 +5008,7 @@ var SolanaChannelClient = class {
4973
5008
  }
4974
5009
  const mint = account.tokenMint ?? this.config.tokenMint;
4975
5010
  const { settleTxSignature } = await settleSolanaChannel({
4976
- rpcUrl: this.config.rpcUrl,
5011
+ rpcUrl: this.rpc,
4977
5012
  programId: this.config.programId,
4978
5013
  channelPDA: channelId,
4979
5014
  callerSeed: this.config.payerSeed,
@@ -4990,7 +5025,7 @@ var SolanaChannelClient = class {
4990
5025
  */
4991
5026
  async redeem(params) {
4992
5027
  const { claimTxSignature } = await claimFromSolanaChannel({
4993
- rpcUrl: this.config.rpcUrl,
5028
+ rpcUrl: this.rpc,
4994
5029
  programId: this.config.programId,
4995
5030
  channelPDA: params.channelId,
4996
5031
  claimerPubkey: params.claimer,
@@ -5012,7 +5047,7 @@ var SolanaChannelClient = class {
5012
5047
  * reads an amply-funded vault as our own headroom.
5013
5048
  */
5014
5049
  async read(channelId) {
5015
- const account = await getChannelAccountState(this.config.rpcUrl, channelId);
5050
+ const account = await getChannelAccountState(this.rpc, channelId);
5016
5051
  const weAreA = account.participantA === this.config.payerPubkey;
5017
5052
  const deadline = settleableAt(account);
5018
5053
  return {
@@ -5037,6 +5072,7 @@ var OnChainChannelClient = class {
5037
5072
  chainRpcUrls;
5038
5073
  solanaConfig;
5039
5074
  readConsistency;
5075
+ rpcDispatcher;
5040
5076
  channelContext = /* @__PURE__ */ new Map();
5041
5077
  evmClients = /* @__PURE__ */ new Map();
5042
5078
  solanaClients = /* @__PURE__ */ new Map();
@@ -5045,6 +5081,7 @@ var OnChainChannelClient = class {
5045
5081
  this.chainRpcUrls = config.chainRpcUrls;
5046
5082
  this.solanaConfig = config.solanaConfig;
5047
5083
  this.readConsistency = config.readConsistency;
5084
+ this.rpcDispatcher = config.rpcDispatcher;
5048
5085
  }
5049
5086
  /**
5050
5087
  * Late-bind the Solana config. The Ed25519 keypair is derived asynchronously
@@ -5092,7 +5129,8 @@ var OnChainChannelClient = class {
5092
5129
  chain,
5093
5130
  rpcUrl,
5094
5131
  signer: this.evmSigner,
5095
- ...this.readConsistency ? { readConsistency: this.readConsistency } : {}
5132
+ ...this.readConsistency ? { readConsistency: this.readConsistency } : {},
5133
+ ...this.rpcDispatcher !== void 0 ? { rpcDispatcher: this.rpcDispatcher } : {}
5096
5134
  });
5097
5135
  this.evmClients.set(chain, client);
5098
5136
  return client;
@@ -5307,6 +5345,7 @@ var OnChainChannelClient = class {
5307
5345
  const payerSeed = cfg.keypair.slice(0, 32);
5308
5346
  const client = new SolanaChannelClient({
5309
5347
  rpcUrl: cfg.rpcUrl,
5348
+ ...cfg.rpcFetch ? { rpcFetch: cfg.rpcFetch } : {},
5310
5349
  programId: program,
5311
5350
  tokenMint: mint,
5312
5351
  payerSeed,
@@ -6193,10 +6232,13 @@ var InMemoryChannelStore = class {
6193
6232
 
6194
6233
  // src/client/config.ts
6195
6234
  var DEFAULT_TIMEOUT_MS2 = 3e4;
6235
+ var DEFAULT_HS_TIMEOUT_MS = 12e4;
6196
6236
  var DEFAULT_DEPOSIT = 100000n;
6197
6237
  var DEFAULT_SETTLEMENT_TIMEOUT = 86400;
6198
6238
  function resolveConfig(config) {
6199
6239
  const connector = resolveConnector(config.connector);
6240
+ const connectorIsHiddenService = isHiddenServiceUrl(connector);
6241
+ const socksProxy = resolveSocksProxy(config.socksProxy, connector, connectorIsHiddenService);
6200
6242
  const accountIndex = resolveAccountIndex(config.accountIndex);
6201
6243
  const keyDerivation = config.keyDerivation ?? "standard";
6202
6244
  const identity = resolveIdentity(config, keyDerivation, accountIndex);
@@ -6217,7 +6259,7 @@ function resolveConfig(config) {
6217
6259
  );
6218
6260
  }
6219
6261
  const deposit = resolveDeposit(config.deposit);
6220
- const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS2;
6262
+ const timeoutMs = config.timeoutMs ?? (connectorIsHiddenService ? DEFAULT_HS_TIMEOUT_MS : DEFAULT_TIMEOUT_MS2);
6221
6263
  if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
6222
6264
  throw new ConfigError(`timeoutMs must be a positive number; got ${String(timeoutMs)}.`);
6223
6265
  }
@@ -6242,6 +6284,10 @@ function resolveConfig(config) {
6242
6284
  transport,
6243
6285
  senderId: config.senderId,
6244
6286
  deposit,
6287
+ connectorIsHiddenService,
6288
+ socksProxy,
6289
+ proxyRpc: config.proxyRpc ?? true,
6290
+ rpcDispatcher: void 0,
6245
6291
  settlementTimeout,
6246
6292
  autoOpenChannel: config.autoOpenChannel ?? true,
6247
6293
  timeoutMs,
@@ -6282,8 +6328,41 @@ function resolveConnector(connector) {
6282
6328
  `connector ${JSON.stringify(connector)} must be an http(s) URL; got ${JSON.stringify(base)}.`
6283
6329
  );
6284
6330
  }
6331
+ const host = new URL(base).hostname;
6332
+ if (/\.(anon|onion)$/.test(host)) {
6333
+ try {
6334
+ assertRoutableHsHostname(host);
6335
+ } catch (error) {
6336
+ throw new ConfigError(
6337
+ `connector ${JSON.stringify(connector)} is not reachable: ` + (error instanceof Error ? error.message : String(error)),
6338
+ error instanceof Error ? error : void 0
6339
+ );
6340
+ }
6341
+ }
6285
6342
  return base;
6286
6343
  }
6344
+ function resolveSocksProxy(socksProxy, connector, connectorIsHiddenService) {
6345
+ if (socksProxy === void 0) {
6346
+ if (!connectorIsHiddenService) return void 0;
6347
+ throw new ConfigError(
6348
+ `connector ${JSON.stringify(connector)} is a hidden service, which is reachable only through a SOCKS5h proxy. Set \`socksProxy\` to a running Anyone Protocol \`anon\` daemon (e.g. "socks5h://127.0.0.1:9050"), or use the \`toon\` CLI, which can start one for you.`
6349
+ );
6350
+ }
6351
+ if (!connectorIsHiddenService) {
6352
+ throw new ConfigError(
6353
+ `socksProxy is set, but connector ${JSON.stringify(connector)} is a clearnet address, so nothing would ride the proxy. Point \`connector\` at the node's .anyone address, or drop \`socksProxy\`.`
6354
+ );
6355
+ }
6356
+ try {
6357
+ validateSocks5hUrl(socksProxy);
6358
+ } catch (error) {
6359
+ throw new ConfigError(
6360
+ error instanceof Error ? error.message : String(error),
6361
+ error instanceof Error ? error : void 0
6362
+ );
6363
+ }
6364
+ return socksProxy;
6365
+ }
6287
6366
  function resolveAccountIndex(accountIndex) {
6288
6367
  const index = accountIndex ?? 0;
6289
6368
  if (!Number.isInteger(index) || index < 0) {
@@ -6679,7 +6758,7 @@ function localStatus(state) {
6679
6758
  }
6680
6759
 
6681
6760
  // src/wallet/balances.ts
6682
- import { createPublicClient as createPublicClient2, http as http2, defineChain as defineChain2 } from "viem";
6761
+ import { createPublicClient as createPublicClient2, defineChain as defineChain2 } from "viem";
6683
6762
  var ERC20_READ_ABI = [
6684
6763
  { name: "balanceOf", type: "function", stateMutability: "view", inputs: [{ name: "account", type: "address" }], outputs: [{ type: "uint256" }] },
6685
6764
  { name: "decimals", type: "function", stateMutability: "view", inputs: [], outputs: [{ type: "uint8" }] },
@@ -6706,7 +6785,10 @@ function rpcAbortSignal() {
6706
6785
  async function readEvmTokenBalance(opts) {
6707
6786
  const chainId = parseEvmChainId2(opts.chainKey);
6708
6787
  const client = createPublicClient2({
6709
- transport: http2(opts.rpcUrl, { timeout: rpcTimeoutMs() || void 0, retryCount: 1 }),
6788
+ transport: rpcTransport(opts.rpcUrl, opts.rpcDispatcher, {
6789
+ ...rpcTimeoutMs() ? { timeout: rpcTimeoutMs() } : {},
6790
+ retryCount: 1
6791
+ }),
6710
6792
  chain: defineChain2({
6711
6793
  id: chainId,
6712
6794
  name: opts.chainKey,
@@ -6729,7 +6811,10 @@ async function readEvmTokenBalance(opts) {
6729
6811
  async function readEvmNativeBalance(opts) {
6730
6812
  const chainId = parseEvmChainId2(opts.chainKey);
6731
6813
  const client = createPublicClient2({
6732
- transport: http2(opts.rpcUrl, { timeout: rpcTimeoutMs() || void 0, retryCount: 1 }),
6814
+ transport: rpcTransport(opts.rpcUrl, opts.rpcDispatcher, {
6815
+ ...rpcTimeoutMs() ? { timeout: rpcTimeoutMs() } : {},
6816
+ retryCount: 1
6817
+ }),
6733
6818
  chain: defineChain2({
6734
6819
  id: chainId,
6735
6820
  name: opts.chainKey,
@@ -6813,14 +6898,14 @@ async function readWalletBalances(sources) {
6813
6898
  const { fetchImpl } = sources;
6814
6899
  const tasks = [];
6815
6900
  if (sources.evm) {
6816
- const { chainKey, rpcUrl, owner, tokenAddress } = sources.evm;
6901
+ const { chainKey, rpcUrl, owner, tokenAddress, rpcDispatcher } = sources.evm;
6817
6902
  tasks.push(
6818
6903
  (async () => {
6819
6904
  const out = { chain: "evm", chainKey, address: owner, tokens: [] };
6820
6905
  const errors = [];
6821
6906
  const [nativeR, tokenR] = await Promise.allSettled([
6822
- readEvmNativeBalance({ rpcUrl, chainKey, owner }),
6823
- tokenAddress ? readEvmTokenBalance({ rpcUrl, chainKey, tokenAddress, owner }) : Promise.resolve(void 0)
6907
+ readEvmNativeBalance({ rpcUrl, chainKey, owner, rpcDispatcher }),
6908
+ tokenAddress ? readEvmTokenBalance({ rpcUrl, chainKey, tokenAddress, owner, rpcDispatcher }) : Promise.resolve(void 0)
6824
6909
  ]);
6825
6910
  foldNative(out, nativeR, errors);
6826
6911
  foldToken(out, tokenR, tokenAddress, errors);
@@ -6856,7 +6941,6 @@ async function readWalletBalances(sources) {
6856
6941
  import {
6857
6942
  createPublicClient as createPublicClient3,
6858
6943
  createWalletClient as createWalletClient2,
6859
- http as http3,
6860
6944
  defineChain as defineChain3
6861
6945
  } from "viem";
6862
6946
  import { ed25519 as ed255195 } from "@noble/curves/ed25519.js";
@@ -6940,6 +7024,9 @@ var ERC20_TRANSFER_ABI = [
6940
7024
  outputs: [{ type: "bool" }]
6941
7025
  }
6942
7026
  ];
7027
+ function solanaRpcTarget(cfg) {
7028
+ return cfg.rpcFetch === void 0 ? cfg.rpcUrl : { url: cfg.rpcUrl, fetchImpl: cfg.rpcFetch };
7029
+ }
6943
7030
  function evmClients(cfg) {
6944
7031
  const chainId = parseEvmChainId2(cfg.chainKey);
6945
7032
  const viemChain = defineChain3({
@@ -6949,12 +7036,12 @@ function evmClients(cfg) {
6949
7036
  rpcUrls: { default: { http: [cfg.rpcUrl] } }
6950
7037
  });
6951
7038
  const publicClient = createPublicClient3({
6952
- transport: http3(cfg.rpcUrl),
7039
+ transport: rpcTransport(cfg.rpcUrl, cfg.rpcDispatcher),
6953
7040
  chain: viemChain
6954
7041
  });
6955
7042
  const walletClient = createWalletClient2({
6956
7043
  account: cfg.signer.account,
6957
- transport: http3(cfg.rpcUrl),
7044
+ transport: rpcTransport(cfg.rpcUrl, cfg.rpcDispatcher),
6958
7045
  chain: viemChain
6959
7046
  });
6960
7047
  return { publicClient, walletClient };
@@ -7058,20 +7145,21 @@ async function sendSolanaTransfer(cfg, params, amount) {
7058
7145
  );
7059
7146
  const payer = { publicKey: base58Decode(payerPubkey), privateKey: payerSeed };
7060
7147
  const timeoutMs = params.confirmTimeoutMs ?? DEFAULT_SOLANA_CONFIRM_TIMEOUT_MS;
7148
+ const rpc = solanaRpcTarget(cfg);
7061
7149
  if (params.asset === "native") {
7062
- const lamports2 = await getLamports(cfg.rpcUrl, payerPubkey);
7150
+ const lamports2 = await getLamports(rpc, payerPubkey);
7063
7151
  const need = amount + SOLANA_LAMPORTS_PER_SIGNATURE;
7064
7152
  if (lamports2 < need) {
7065
7153
  throw new InsufficientBalanceError(
7066
7154
  `Solana wallet ${payerPubkey} holds ${lamports2} lamports, short of the ${amount}-lamport transfer plus the ${SOLANA_LAMPORTS_PER_SIGNATURE}-lamport signature fee.`
7067
7155
  );
7068
7156
  }
7069
- const readDest2 = () => getLamports(cfg.rpcUrl, params.to);
7157
+ const readDest2 = () => getLamports(rpc, params.to);
7070
7158
  const before2 = await readDest2();
7071
7159
  const data = new Uint8Array(12);
7072
7160
  new DataView(data.buffer).setUint32(0, 2, true);
7073
7161
  writeU64LE2(data, 4, amount);
7074
- const txHash2 = await buildAndSendTransaction(cfg.rpcUrl, payer, [
7162
+ const txHash2 = await buildAndSendTransaction(rpc, payer, [
7075
7163
  {
7076
7164
  programId: SOLANA_SYSTEM_PROGRAM_ID,
7077
7165
  keys: [
@@ -7105,20 +7193,20 @@ async function sendSolanaTransfer(cfg, params, amount) {
7105
7193
  );
7106
7194
  }
7107
7195
  const senderAta = deriveAssociatedTokenAccount(payerPubkey, cfg.tokenMint);
7108
- const senderBalance = await getTokenAccountBalance(cfg.rpcUrl, senderAta);
7196
+ const senderBalance = await getTokenAccountBalance(rpc, senderAta);
7109
7197
  if (senderBalance === null || senderBalance < amount) {
7110
7198
  throw new InsufficientBalanceError(
7111
7199
  `Solana token account ${senderAta} holds ${senderBalance ?? 0n} base units of ${cfg.tokenMint}, short of the ${amount} transfer.`
7112
7200
  );
7113
7201
  }
7114
- const lamports = await getLamports(cfg.rpcUrl, payerPubkey);
7202
+ const lamports = await getLamports(rpc, payerPubkey);
7115
7203
  if (lamports < SOLANA_LAMPORTS_PER_SIGNATURE) {
7116
7204
  throw new InsufficientBalanceError(
7117
7205
  `Solana wallet ${payerPubkey} holds ${lamports} lamports, below the ${SOLANA_LAMPORTS_PER_SIGNATURE}-lamport fee this token transfer needs.`
7118
7206
  );
7119
7207
  }
7120
7208
  const destAta = deriveAssociatedTokenAccount(params.to, cfg.tokenMint);
7121
- const readDest = async () => await getTokenAccountBalance(cfg.rpcUrl, destAta) ?? 0n;
7209
+ const readDest = async () => await getTokenAccountBalance(rpc, destAta) ?? 0n;
7122
7210
  const before = await readDest();
7123
7211
  const createAtaIx = {
7124
7212
  programId: SOLANA_ASSOCIATED_TOKEN_PROGRAM_ID,
@@ -7144,7 +7232,7 @@ async function sendSolanaTransfer(cfg, params, amount) {
7144
7232
  ],
7145
7233
  data: transferData
7146
7234
  };
7147
- const txHash = await buildAndSendTransaction(cfg.rpcUrl, payer, [
7235
+ const txHash = await buildAndSendTransaction(rpc, payer, [
7148
7236
  createAtaIx,
7149
7237
  transferIx
7150
7238
  ]);
@@ -7255,6 +7343,10 @@ async function fundWallet(faucetUrl, address, chain, options = {}) {
7255
7343
  }
7256
7344
 
7257
7345
  // src/client/wallet-facade.ts
7346
+ function solanaRpcFetch(config) {
7347
+ if (config.socksProxy === void 0) return config.fetch;
7348
+ return config.rpcDispatcher !== void 0 ? config.fetch : globalThis.fetch;
7349
+ }
7258
7350
  var ClientWalletFacade = class {
7259
7351
  deps;
7260
7352
  evmSigner;
@@ -7273,7 +7365,9 @@ var ClientWalletFacade = class {
7273
7365
  const { config } = this.deps;
7274
7366
  const settlements = await this.settlements();
7275
7367
  const wanted = chain !== void 0 ? [chain] : ["evm", "solana"];
7276
- const sources = { fetchImpl: config.fetch };
7368
+ const sources = {
7369
+ fetchImpl: solanaRpcFetch(config)
7370
+ };
7277
7371
  for (const kind of wanted) {
7278
7372
  const entry = settlements.get(kind);
7279
7373
  if (kind === "evm" && config.identity.evm) {
@@ -7281,6 +7375,7 @@ var ClientWalletFacade = class {
7281
7375
  chainKey: entry?.chain ?? "evm",
7282
7376
  rpcUrl: config.rpcUrls.evm,
7283
7377
  owner: config.identity.evm.address,
7378
+ ...config.rpcDispatcher !== void 0 ? { rpcDispatcher: config.rpcDispatcher } : {},
7284
7379
  ...entry?.tokenAddress ? { tokenAddress: entry.tokenAddress } : {}
7285
7380
  };
7286
7381
  }
@@ -7321,12 +7416,14 @@ var ClientWalletFacade = class {
7321
7416
  chainKey: entry?.kind === "evm" ? entry.chain : "evm",
7322
7417
  rpcUrl: config.rpcUrls.evm,
7323
7418
  signer: this.signer(),
7419
+ ...config.rpcDispatcher !== void 0 ? { rpcDispatcher: config.rpcDispatcher } : {},
7324
7420
  ...entry?.tokenAddress ? { tokenAddress: entry.tokenAddress } : {}
7325
7421
  }
7326
7422
  } : {},
7327
7423
  ...config.identity.solana ? {
7328
7424
  solana: {
7329
7425
  rpcUrl: config.rpcUrls.solana,
7426
+ ...config.rpcDispatcher !== void 0 ? { rpcFetch: solanaRpcFetch(config) } : {},
7330
7427
  keypair: config.identity.solana.secretKey,
7331
7428
  ...entry?.tokenAddress ? { tokenMint: entry.tokenAddress } : {}
7332
7429
  }
@@ -7654,9 +7751,11 @@ var ToonClient = class _ToonClient {
7654
7751
  onChain;
7655
7752
  carriage;
7656
7753
  btpSession;
7754
+ hiddenService;
7657
7755
  closed = false;
7658
7756
  constructor(init) {
7659
7757
  this.config = init.config;
7758
+ this.hiddenService = init.hiddenService;
7660
7759
  this.edge = init.edge;
7661
7760
  this.channels = init.channels;
7662
7761
  this.description = init.description;
@@ -7691,7 +7790,9 @@ var ToonClient = class _ToonClient {
7691
7790
  * @throws {NetworkError} the connector could not be reached.
7692
7791
  */
7693
7792
  static async create(config) {
7694
- const resolved = resolveConfig(config);
7793
+ const base = resolveConfig(config);
7794
+ const hiddenService = await openHiddenService(base, config);
7795
+ const resolved = hiddenService?.config ?? base;
7695
7796
  const edge = new ConnectorEdgeClient({
7696
7797
  fetch: resolved.fetch,
7697
7798
  timeout: resolved.timeoutMs
@@ -7721,7 +7822,15 @@ var ToonClient = class _ToonClient {
7721
7822
  )
7722
7823
  );
7723
7824
  }
7724
- return new _ToonClient({ config: resolved, edge, channels, description, chain, senderId });
7825
+ return new _ToonClient({
7826
+ config: resolved,
7827
+ edge,
7828
+ channels,
7829
+ description,
7830
+ chain,
7831
+ senderId,
7832
+ ...hiddenService ? { hiddenService: hiddenService.transport } : {}
7833
+ });
7725
7834
  }
7726
7835
  /**
7727
7836
  * The node's `GET /ilp`, cached for the life of this client.
@@ -7870,8 +7979,27 @@ var ToonClient = class _ToonClient {
7870
7979
  this.btpSession = void 0;
7871
7980
  this.carriage = void 0;
7872
7981
  if (session) await session.disconnect();
7982
+ if (this.hiddenService) await this.hiddenService.close();
7873
7983
  }
7874
7984
  // ─── Private ──────────────────────────────────────────────────────────────
7985
+ /**
7986
+ * Refuse an endpoint this client has no way to dial.
7987
+ *
7988
+ * A node's endpoints are its own strings, and a hidden-service node may publish
7989
+ * absolute `.anyone` ones. The configured client edge always wins on *whether*
7990
+ * we can reach the node — but a carriage resolved from the description can
7991
+ * still point somewhere the configured transport cannot go. Without a proxy
7992
+ * that address does not merely fail: the hostname goes out in a plaintext DNS
7993
+ * query first, which is precisely what a hidden service exists to prevent. So
7994
+ * this fails loudly, naming the proxy, before anything dials.
7995
+ */
7996
+ assertEndpointReachable(endpoint) {
7997
+ if (this.config.socksProxy !== void 0) return;
7998
+ if (!isHiddenServiceUrl(endpoint)) return;
7999
+ throw new ConfigError(
8000
+ `The connector published the endpoint ${JSON.stringify(endpoint)}, which is a hidden service, but this client has no \`socksProxy\` to reach it through. Set one to a running Anyone Protocol \`anon\` daemon (e.g. "socks5h://127.0.0.1:9050"), or ask the operator for a clearnet endpoint.`
8001
+ );
8002
+ }
7875
8003
  /** The port {@link send} runs against. */
7876
8004
  sendContext() {
7877
8005
  return {
@@ -7939,9 +8067,13 @@ var ToonClient = class _ToonClient {
7939
8067
  const client = new OnChainChannelClient({
7940
8068
  evmSigner: new EvmSigner(this.requireEvmKey()),
7941
8069
  chainRpcUrls,
8070
+ ...this.config.rpcDispatcher !== void 0 ? { rpcDispatcher: this.config.rpcDispatcher } : {},
7942
8071
  ...this.config.identity.solana ? {
7943
8072
  solanaConfig: {
7944
8073
  rpcUrl: this.config.rpcUrls.solana,
8074
+ // Same condition as the EVM dispatcher: proxied only when there is
8075
+ // a proxy and the caller has not opted RPC out of it.
8076
+ ...this.config.rpcDispatcher !== void 0 ? { rpcFetch: this.config.fetch } : {},
7945
8077
  keypair: this.config.identity.solana.secretKey,
7946
8078
  // The DEFAULT only. Each channel opens under the program its own
7947
8079
  // terms name, because ADR 0053 binds that program into the signed
@@ -7986,14 +8118,17 @@ var ToonClient = class _ToonClient {
7986
8118
  }
7987
8119
  if (this.carriage) return this.carriage;
7988
8120
  const choice = selectTransport(description, this.config.transport);
7989
- const http4 = new HttpIlpClient({
7990
- httpEndpoint: choice.kind === "http" ? choice.url : httpEndpointOf(description, this.connector),
8121
+ const httpEndpoint = choice.kind === "http" ? choice.url : httpEndpointOf(description, this.connector);
8122
+ this.assertEndpointReachable(choice.url);
8123
+ this.assertEndpointReachable(httpEndpoint);
8124
+ const http2 = new HttpIlpClient({
8125
+ httpEndpoint,
7991
8126
  timeout: this.config.timeoutMs,
7992
8127
  httpClient: this.config.fetch,
7993
8128
  ...this.config.createWebSocket ? { createWebSocket: this.config.createWebSocket } : {}
7994
8129
  });
7995
8130
  if (choice.kind === "http") {
7996
- const carriage2 = { kind: "http", transport: http4 };
8131
+ const carriage2 = { kind: "http", transport: http2 };
7997
8132
  this.carriage = carriage2;
7998
8133
  return carriage2;
7999
8134
  }
@@ -8017,7 +8152,7 @@ var ToonClient = class _ToonClient {
8017
8152
  // HTTP fallback only where the node did not REQUIRE btp: falling back
8018
8153
  // onto a carriage the route refuses would turn a recoverable socket
8019
8154
  // outage into a `402` per request.
8020
- ...description.requiredTransport === "btp" ? {} : { fallback: http4 },
8155
+ ...description.requiredTransport === "btp" ? {} : { fallback: http2 },
8021
8156
  ...this.config.btp.maxReconnectAttempts !== void 0 ? { maxReconnectAttempts: this.config.btp.maxReconnectAttempts } : {},
8022
8157
  ...this.config.btp.reconnectDelay !== void 0 ? { reconnectDelay: this.config.btp.reconnectDelay } : {}
8023
8158
  })
@@ -8103,6 +8238,22 @@ function httpEndpointOf(description, connector) {
8103
8238
  if (published === void 0) return `${connector}/ilp`;
8104
8239
  return /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(published) ? published : new URL(published, connector).toString();
8105
8240
  }
8241
+ async function openHiddenService(resolved, config) {
8242
+ const socksProxy = resolved.socksProxy;
8243
+ if (socksProxy === void 0) return void 0;
8244
+ const { createHiddenServiceTransport, probeSocks5Proxy } = await import("./socks-CCMHMB35.js");
8245
+ await probeSocks5Proxy(socksProxy);
8246
+ const transport = createHiddenServiceTransport(socksProxy);
8247
+ return {
8248
+ transport,
8249
+ config: {
8250
+ ...resolved,
8251
+ fetch: config.fetch ?? transport.fetch,
8252
+ createWebSocket: config.createWebSocket ?? transport.createWebSocket,
8253
+ rpcDispatcher: resolved.proxyRpc ? transport.dispatcher : void 0
8254
+ }
8255
+ };
8256
+ }
8106
8257
 
8107
8258
  export {
8108
8259
  ToonClientError,
@@ -8147,6 +8298,7 @@ export {
8147
8298
  encodeUtf8,
8148
8299
  decodeUtf8,
8149
8300
  resolveExecutionCondition,
8301
+ PACKET_EXPIRY_HEADROOM_MS,
8150
8302
  resolveExpiresAt,
8151
8303
  FULFILLMENT_MISMATCH_CODE,
8152
8304
  FULFILLMENT_MISMATCH_MESSAGE,
@@ -8187,6 +8339,8 @@ export {
8187
8339
  sameSettlementAddress,
8188
8340
  counterpartyMatch,
8189
8341
  ChannelManager,
8342
+ rpcTransport,
8343
+ rpcFetch,
8190
8344
  TOKEN_NETWORK_ABI,
8191
8345
  TOKEN_NETWORK_REGISTRY_ABI,
8192
8346
  ERC20_ABI,
@@ -8258,4 +8412,4 @@ export {
8258
8412
  toEnvelopeRequest,
8259
8413
  ToonClient
8260
8414
  };
8261
- //# sourceMappingURL=chunk-ENNSPYP4.js.map
8415
+ //# sourceMappingURL=chunk-A3JKQOWQ.js.map