@toon-protocol/client 0.29.5 → 0.29.6

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
@@ -3323,6 +3323,22 @@ declare class ToonClient {
3323
3323
  * when it has discovered no peers at all yet.
3324
3324
  */
3325
3325
  private resolveTerminatorEndpoint;
3326
+ /**
3327
+ * Whether the announce that TERMINATES `destination` declares
3328
+ * `requiredTransport: "btp"` (issue #558) — the connector accepts `POST
3329
+ * /ilp` for identity/price reads, but rejects a PAID WRITE over HTTP with
3330
+ * `402 Payment Required` unless it rode a BTP session.
3331
+ *
3332
+ * `requiredTransport` is read off the announce's raw content by
3333
+ * `DiscoverySubscription.requiredTransportFor` (`discovery-subscription.ts`),
3334
+ * never through the parsed `IlpPeerInfo`: the installed
3335
+ * `@toon-protocol/core`'s `parseIlpPeerInfo` drops unknown fields, the same
3336
+ * gap toon-client#544 hit for `notice`. Silent `false` (never sealed HTTP is
3337
+ * fine) whenever a tracker, its winning claim, or the subscription's raw
3338
+ * feed is unavailable — this is an ADDITIONAL constraint on top of
3339
+ * `resolveTerminatorEndpoint`'s own claim gating, not a replacement for it.
3340
+ */
3341
+ private terminatorRequiresBtp;
3326
3342
  /**
3327
3343
  * Payment-aware HTTP fetch over TOON (issue #50). A `fetch()`-like method that
3328
3344
  * makes paying for an HTTP resource transparent:
@@ -3456,8 +3472,19 @@ declare class ToonClient {
3456
3472
  * proxy) does NOT implement `sendIlpPacketWithClaim`; in that case there is no
3457
3473
  * paid-write transport and we throw a clear, actionable error.
3458
3474
  *
3475
+ * `destination`, when given, is checked against {@link terminatorRequiresBtp}
3476
+ * FIRST, overriding both orderings above (issue #558): a terminating
3477
+ * announce that declares `requiredTransport: "btp"` gets `btpClient`
3478
+ * exclusively — never `runtimeClient`, even when HTTP would otherwise have
3479
+ * been tried first — because the connector enforces that requirement with
3480
+ * a `402` on every paid write, so a retry over HTTP can only ever repeat
3481
+ * the same failure. Omitting `destination` (or a tracker that cannot
3482
+ * resolve one) reproduces the exact precedence above, unchanged.
3483
+ *
3459
3484
  * @throws {ToonClientError} NO_ILP_TRANSPORT when no active transport can send
3460
3485
  * a packet+claim.
3486
+ * @throws {ToonClientError} BTP_REQUIRED when `destination`'s terminator
3487
+ * requires BTP but this client has no `btpClient` configured at all.
3461
3488
  */
3462
3489
  private getClaimTransport;
3463
3490
  /**
package/dist/index.js CHANGED
@@ -5203,8 +5203,22 @@ async function initializeHttpMode(config, hooks = {}) {
5203
5203
  import { ILP_PEER_INFO_KIND } from "@toon-protocol/core";
5204
5204
  async function subscribeToDiscovery(relayUrls, tracker) {
5205
5205
  const urls = Array.from(new Set(relayUrls.filter((url) => url !== "")));
5206
+ const requiredTransports = /* @__PURE__ */ new Map();
5207
+ const requiredTransportSeenAt = /* @__PURE__ */ new Map();
5208
+ const requiredTransportFor = (pubkey) => requiredTransports.get(pubkey);
5209
+ const recordRequiredTransport = (event) => {
5210
+ const lastSeen = requiredTransportSeenAt.get(event.pubkey) ?? 0;
5211
+ if (event.created_at <= lastSeen) return;
5212
+ requiredTransportSeenAt.set(event.pubkey, event.created_at);
5213
+ const requiredTransport = extractRequiredTransport(event);
5214
+ if (requiredTransport === void 0) {
5215
+ requiredTransports.delete(event.pubkey);
5216
+ } else {
5217
+ requiredTransports.set(event.pubkey, requiredTransport);
5218
+ }
5219
+ };
5206
5220
  if (urls.length === 0) {
5207
- return { close: () => void 0 };
5221
+ return { close: () => void 0, requiredTransportFor };
5208
5222
  }
5209
5223
  const { SimplePool } = await import("nostr-tools/pool");
5210
5224
  const pool = new SimplePool();
@@ -5212,16 +5226,30 @@ async function subscribeToDiscovery(relayUrls, tracker) {
5212
5226
  urls,
5213
5227
  { kinds: [ILP_PEER_INFO_KIND] },
5214
5228
  {
5215
- onevent: (event) => tracker.processEvent(event)
5229
+ onevent: (event) => {
5230
+ tracker.processEvent(event);
5231
+ recordRequiredTransport(event);
5232
+ }
5216
5233
  }
5217
5234
  );
5218
5235
  return {
5219
5236
  close: () => {
5220
5237
  sub.close();
5221
5238
  pool.close(urls);
5222
- }
5239
+ },
5240
+ requiredTransportFor
5223
5241
  };
5224
5242
  }
5243
+ function extractRequiredTransport(event) {
5244
+ try {
5245
+ const parsed = JSON.parse(event.content);
5246
+ if (typeof parsed !== "object" || parsed === null) return void 0;
5247
+ const value = parsed["requiredTransport"];
5248
+ return typeof value === "string" ? value : void 0;
5249
+ } catch {
5250
+ return void 0;
5251
+ }
5252
+ }
5225
5253
 
5226
5254
  // src/signing/solana-signer.ts
5227
5255
  import { ed25519 as ed255193 } from "@noble/curves/ed25519.js";
@@ -7841,13 +7869,19 @@ function claimsTermination(peerInfo, address, destination) {
7841
7869
  if (destination === address) return true;
7842
7870
  return peerInfo.ilpAddresses === void 0 && ilpAddressTerminates(address, destination);
7843
7871
  }
7872
+ function sendsClaims(candidate) {
7873
+ return typeof candidate?.sendIlpPacketWithClaim === "function";
7874
+ }
7844
7875
  function outranks(claim, best) {
7845
7876
  if (claim.segments !== best.segments) return claim.segments > best.segments;
7846
7877
  return claim.isPrimary && !best.isPrimary;
7847
7878
  }
7848
7879
  function resolveTerminatorHttpEndpoint(destination, peers) {
7880
+ return findBestTerminatorClaim(destination, peers)?.endpoint;
7881
+ }
7882
+ function findBestTerminatorClaim(destination, peers) {
7849
7883
  let best;
7850
- for (const { peerInfo } of peers) {
7884
+ for (const { peerInfo, pubkey } of peers) {
7851
7885
  const httpEndpoint = peerInfo.httpEndpoint;
7852
7886
  if (!httpEndpoint) continue;
7853
7887
  const addresses = peerInfo.ilpAddresses ?? [peerInfo.ilpAddress];
@@ -7856,12 +7890,13 @@ function resolveTerminatorHttpEndpoint(destination, peers) {
7856
7890
  const claim = {
7857
7891
  endpoint: httpEndpoint,
7858
7892
  segments: address.split(".").length,
7859
- isPrimary: index === 0
7893
+ isPrimary: index === 0,
7894
+ pubkey
7860
7895
  };
7861
7896
  if (!best || outranks(claim, best)) best = claim;
7862
7897
  }
7863
7898
  }
7864
- return best?.endpoint;
7899
+ return best;
7865
7900
  }
7866
7901
  function canonicalChainId(chain) {
7867
7902
  const parts = chain.split(":");
@@ -8326,7 +8361,7 @@ var ToonClient = class {
8326
8361
  },
8327
8362
  identity.publicKey
8328
8363
  );
8329
- const transport = this.getClaimTransport();
8364
+ const transport = this.getClaimTransport(destination);
8330
8365
  const claimMessage = await this.resolveClaimForDestination(
8331
8366
  destination,
8332
8367
  BigInt(amount),
@@ -8378,7 +8413,7 @@ var ToonClient = class {
8378
8413
  String(error),
8379
8414
  error instanceof Error ? error.stack : ""
8380
8415
  );
8381
- if (error instanceof ToonClientError && (error.code === "NO_TERMINATED_ROUTE" || error.code === "NO_CONNECTOR_EDGE" || error.code === "PEER_NOT_NEGOTIATED" || error.code === "CHANNEL_FUNDING" || error.code === "TERMINATOR_UNRESOLVED")) {
8416
+ if (error instanceof ToonClientError && (error.code === "NO_TERMINATED_ROUTE" || error.code === "NO_CONNECTOR_EDGE" || error.code === "PEER_NOT_NEGOTIATED" || error.code === "CHANNEL_FUNDING" || error.code === "TERMINATOR_UNRESOLVED" || error.code === "BTP_REQUIRED")) {
8382
8417
  throw error;
8383
8418
  }
8384
8419
  throw new ToonClientError(
@@ -8512,6 +8547,33 @@ var ToonClient = class {
8512
8547
  }
8513
8548
  return connectorEdgeBaseUrl(httpEndpoint);
8514
8549
  }
8550
+ /**
8551
+ * Whether the announce that TERMINATES `destination` declares
8552
+ * `requiredTransport: "btp"` (issue #558) — the connector accepts `POST
8553
+ * /ilp` for identity/price reads, but rejects a PAID WRITE over HTTP with
8554
+ * `402 Payment Required` unless it rode a BTP session.
8555
+ *
8556
+ * `requiredTransport` is read off the announce's raw content by
8557
+ * `DiscoverySubscription.requiredTransportFor` (`discovery-subscription.ts`),
8558
+ * never through the parsed `IlpPeerInfo`: the installed
8559
+ * `@toon-protocol/core`'s `parseIlpPeerInfo` drops unknown fields, the same
8560
+ * gap toon-client#544 hit for `notice`. Silent `false` (never sealed HTTP is
8561
+ * fine) whenever a tracker, its winning claim, or the subscription's raw
8562
+ * feed is unavailable — this is an ADDITIONAL constraint on top of
8563
+ * `resolveTerminatorEndpoint`'s own claim gating, not a replacement for it.
8564
+ */
8565
+ terminatorRequiresBtp(destination) {
8566
+ const tracker = this.state?.discoveryTracker;
8567
+ if (typeof tracker?.getAllDiscoveredPeers !== "function") return false;
8568
+ const subscription = this.state?.discoverySubscription;
8569
+ if (typeof subscription?.requiredTransportFor !== "function") return false;
8570
+ const claim = findBestTerminatorClaim(
8571
+ destination,
8572
+ tracker.getAllDiscoveredPeers()
8573
+ );
8574
+ if (!claim) return false;
8575
+ return subscription.requiredTransportFor(claim.pubkey) === "btp";
8576
+ }
8515
8577
  /**
8516
8578
  * Payment-aware HTTP fetch over TOON (issue #50). A `fetch()`-like method that
8517
8579
  * makes paying for an HTTP resource transparent:
@@ -8618,7 +8680,7 @@ var ToonClient = class {
8618
8680
  "INVALID_STATE"
8619
8681
  );
8620
8682
  }
8621
- const transport = this.getClaimTransport();
8683
+ const transport = this.getClaimTransport(params.destination);
8622
8684
  const claimMessage = await this.resolveClaimForDestination(
8623
8685
  params.destination,
8624
8686
  params.amount,
@@ -8693,10 +8755,21 @@ var ToonClient = class {
8693
8755
  * proxy) does NOT implement `sendIlpPacketWithClaim`; in that case there is no
8694
8756
  * paid-write transport and we throw a clear, actionable error.
8695
8757
  *
8758
+ * `destination`, when given, is checked against {@link terminatorRequiresBtp}
8759
+ * FIRST, overriding both orderings above (issue #558): a terminating
8760
+ * announce that declares `requiredTransport: "btp"` gets `btpClient`
8761
+ * exclusively — never `runtimeClient`, even when HTTP would otherwise have
8762
+ * been tried first — because the connector enforces that requirement with
8763
+ * a `402` on every paid write, so a retry over HTTP can only ever repeat
8764
+ * the same failure. Omitting `destination` (or a tracker that cannot
8765
+ * resolve one) reproduces the exact precedence above, unchanged.
8766
+ *
8696
8767
  * @throws {ToonClientError} NO_ILP_TRANSPORT when no active transport can send
8697
8768
  * a packet+claim.
8769
+ * @throws {ToonClientError} BTP_REQUIRED when `destination`'s terminator
8770
+ * requires BTP but this client has no `btpClient` configured at all.
8698
8771
  */
8699
- getClaimTransport() {
8772
+ getClaimTransport(destination) {
8700
8773
  const state = this.state;
8701
8774
  if (!state) {
8702
8775
  throw new ToonClientError(
@@ -8704,11 +8777,16 @@ var ToonClient = class {
8704
8777
  "INVALID_STATE"
8705
8778
  );
8706
8779
  }
8780
+ if (destination !== void 0 && this.terminatorRequiresBtp(destination)) {
8781
+ if (sendsClaims(state.btpClient)) return state.btpClient;
8782
+ throw new ToonClientError(
8783
+ `The connector terminating "${destination}" requires BTP for paid writes (its kind:10032 announce declares requiredTransport: "btp"), but this client has no BTP uplink (\`btpUrl\`) configured.`,
8784
+ "BTP_REQUIRED"
8785
+ );
8786
+ }
8707
8787
  const candidates = this.config.preferBtpForPaidWrites ? [state.btpClient, state.runtimeClient] : [state.runtimeClient, state.btpClient];
8708
8788
  for (const candidate of candidates) {
8709
- if (candidate && typeof candidate.sendIlpPacketWithClaim === "function") {
8710
- return candidate;
8711
- }
8789
+ if (sendsClaims(candidate)) return candidate;
8712
8790
  }
8713
8791
  throw new ToonClientError(
8714
8792
  "No ILP transport for paid writes. Configure `proxyUrl`/`connectorHttpEndpoint` (route through the connector proxy over ILP-over-HTTP) or `btpUrl` (BTP socket).",
@@ -9683,7 +9761,7 @@ var ToonClient = class {
9683
9761
  "MISSING_CLAIM"
9684
9762
  );
9685
9763
  }
9686
- const transport = this.getClaimTransport();
9764
+ const transport = this.getClaimTransport(params.destination);
9687
9765
  const claimMessage = this.buildClaimMessageForProof(params.claim);
9688
9766
  return transport.sendIlpPacketWithClaim(
9689
9767
  ilpParams,