@toon-protocol/client 0.29.1 → 0.29.3

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
@@ -1,7 +1,6 @@
1
- import * as _toon_protocol_core from '@toon-protocol/core';
2
- import { IlpSendResult, IlpClient, IlpPeerInfo, SwapPair, NetworkFamilyStatus, ConnectorAdminClient, ConnectorChannelClient, OpenChannelParams, OpenChannelResult, ChannelState } from '@toon-protocol/core';
3
- export { UI_RENDERER_KIND, UI_TAG, UiCoordinate, buildUiCoordinate, getUiCoordinate, parseUiCoordinate, selectLatestAddressable } from '@toon-protocol/core';
4
1
  import { UnsignedEvent, NostrEvent, EventTemplate } from 'nostr-tools/pure';
2
+ import { IlpSendResult, IlpClient, IlpPeerInfo, SwapPair, NetworkFamilyStatus, DiscoveredPeer, ConnectorAdminClient, ConnectorChannelClient, OpenChannelParams, OpenChannelResult, ChannelState } from '@toon-protocol/core';
3
+ export { UI_RENDERER_KIND, UI_TAG, UiCoordinate, buildUiCoordinate, getUiCoordinate, parseUiCoordinate, selectLatestAddressable } from '@toon-protocol/core';
5
4
  import { PrivateKeyAccount } from 'viem/accounts';
6
5
  import { MinaSignerClientLike, SettlementBundle } from '@toon-protocol/sdk';
7
6
  import { AccumulatedClaim } from '@toon-protocol/sdk/swap';
@@ -765,9 +764,14 @@ declare function parseX402Body(body: unknown, baseUrl?: string): ParsedX402Chall
765
764
  * precondition of forming a packet: `Prepare.data` is sealed to that key, and
766
765
  * sealing to the wrong one is a confidentiality failure, not a delivery
767
766
  * failure. ADR 0022 says the connector *answers* — it never announces — so
768
- * both facts are fetched from the connector's own client edge, on the same
769
- * origin this client already POSTs `/ilp` to
770
- * (`docs/protocol/client-edge-spec.md` §1.7):
767
+ * both facts are fetched from the connector's own client edge
768
+ * (`docs/protocol/client-edge-spec.md` §1.7). That edge is OFTEN the same
769
+ * origin this client already POSTs `/ilp` to, but never assume it: for a
770
+ * forwarded ILP prefix the posting edge and the terminating connector are
771
+ * different machines, and asking the posting edge seals to the wrong key
772
+ * (toon-client#526). Resolving `destination` to the terminator's own client
773
+ * edge — via discovery, before calling into this module — is the caller's
774
+ * job; see `ToonClient.resolveTerminatorEndpoint`.
771
775
  *
772
776
  * - `GET /ilp/identity` → `{ "keyId": "...", "publicKey": "0x04..." }` — the
773
777
  * uncompressed secp256k1 key, 65 bytes, `0x`-prefixed hex.
@@ -1038,7 +1042,16 @@ declare class ConnectorEdgeClient {
1038
1042
  /**
1039
1043
  * The terminating connector's public key, fetched once per endpoint.
1040
1044
  *
1041
- * @param endpoint any client-edge URL (`https://apex`, `https://apex/ilp`).
1045
+ * `endpoint` MUST already be the TERMINATING connector's client edge, not
1046
+ * merely whatever origin a caller is about to POST an ILP packet to —
1047
+ * those are the same machine for most routes today, but never for a
1048
+ * forwarded prefix (ADR 0022, toon-client#526). This method trusts
1049
+ * `endpoint` and fetches exactly that origin's `/ilp/identity`; resolving
1050
+ * a destination address to its terminator first is the caller's job.
1051
+ *
1052
+ * @param endpoint the TERMINATING connector's client-edge URL
1053
+ * (`https://apex`, `https://apex/ilp`) — already resolved from the
1054
+ * destination, never assumed to be the posting edge.
1042
1055
  * @param options `forceRefresh` bypasses (and replaces) the cache entry.
1043
1056
  */
1044
1057
  getIdentity(endpoint: string, options?: {
@@ -2945,6 +2958,45 @@ declare class ToonClient {
2945
2958
  * @throws {ToonClientError} NO_CONNECTOR_EDGE when no origin is known.
2946
2959
  */
2947
2960
  private resolveClientEdgeEndpoint;
2961
+ /**
2962
+ * The client edge to fetch `destination`'s identity from — the connector
2963
+ * that TERMINATES it, not the one this client happens to be posting to
2964
+ * (issue #526). ADR 0022 makes identity answered-not-announced, so it is
2965
+ * fetched from the terminator's own `/ilp/identity`; discovery is only
2966
+ * used to find WHICH origin that is.
2967
+ *
2968
+ * Matches `destination` against every peer this client has discovered
2969
+ * announcing itself via kind:10032 — including ones never peered with,
2970
+ * since a forwarded prefix's terminator need not be a direct peer at all
2971
+ * (that is precisely the shape a forwarded prefix has). Falls back to the
2972
+ * posting edge only when there is no discovery tracker wired up AT ALL —
2973
+ * preserving the (still-common) same-node case where the posting node
2974
+ * terminates its own destination, and the only case that worked before
2975
+ * #526.
2976
+ *
2977
+ * Once a tracker exists, silence — whether zero peers discovered at all,
2978
+ * or peers discovered but none of them claiming `destination` — is treated
2979
+ * as a refusal, not a green light (toon-client#533): zero peers is not
2980
+ * evidence the posting edge terminates the destination, only the absence
2981
+ * of evidence that anything does, and that absence is exactly what
2982
+ * fail-closed exists for. It is also a live production window, not a
2983
+ * theoretical one: `discoveryTracker` is always constructed for a started
2984
+ * client, so a tracker reporting zero peers is what the startup race looks
2985
+ * like before the first announce lands. Likewise, an ancestor-only claim
2986
+ * (a router legitimately owning `g.toon`, which is not the same as owning
2987
+ * everything under it) used to slip through here and get treated as
2988
+ * coverage once a competing announce expired. `resolveTerminatorHttpEndpoint`'s
2989
+ * own claim-gating fixes that; the caller-visible half of the fix is this —
2990
+ * refusing to publish beats sealing to a key that cannot open the wrap,
2991
+ * for a defect whose symptom is "money spent, write lost".
2992
+ *
2993
+ * @throws {ToonClientError} NO_CONNECTOR_EDGE when no origin is known at
2994
+ * all (propagated from {@link resolveClientEdgeEndpoint}).
2995
+ * @throws {ToonClientError} TERMINATOR_UNRESOLVED when a discovery tracker
2996
+ * is present but no discovered announce claims `destination` — including
2997
+ * when it has discovered no peers at all yet.
2998
+ */
2999
+ private resolveTerminatorEndpoint;
2948
3000
  /**
2949
3001
  * Payment-aware HTTP fetch over TOON (issue #50). A `fetch()`-like method that
2950
3002
  * makes paying for an HTTP resource transparent:
@@ -3442,8 +3494,9 @@ declare class ToonClient {
3442
3494
  * first mutually-supported chain (legacy behavior) when unconfigured.
3443
3495
  *
3444
3496
  * Chains are matched by {@link canonicalChainId} rather than exact string
3445
- * equality (#500): the devnet preset's family-qualified `evm:base:84532`
3446
- * and the live apex's unqualified `evm:84532` name the same chain, and
3497
+ * equality (#500): a family-qualified `evm:base:84532` (a caller's config,
3498
+ * or a preset from core <3.2.1) and the live apex's unqualified
3499
+ * `evm:84532` name the same chain, and
3447
3500
  * exact matching skipped straight past it to the next mutually-supported
3448
3501
  * chain (`solana:devnet`) — negotiating a chain nobody asked for instead
3449
3502
  * of failing loudly. The PEER's own chain string is returned (not ours),
@@ -3508,7 +3561,7 @@ declare class ToonClient {
3508
3561
  * @returns Array of discovered peer objects
3509
3562
  * @throws {ToonClientError} If client is not started
3510
3563
  */
3511
- getDiscoveredPeers(): _toon_protocol_core.DiscoveredPeer[];
3564
+ getDiscoveredPeers(): DiscoveredPeer[];
3512
3565
  }
3513
3566
 
3514
3567
  /**
package/dist/index.js CHANGED
@@ -1190,7 +1190,16 @@ var ConnectorEdgeClient = class {
1190
1190
  /**
1191
1191
  * The terminating connector's public key, fetched once per endpoint.
1192
1192
  *
1193
- * @param endpoint any client-edge URL (`https://apex`, `https://apex/ilp`).
1193
+ * `endpoint` MUST already be the TERMINATING connector's client edge, not
1194
+ * merely whatever origin a caller is about to POST an ILP packet to —
1195
+ * those are the same machine for most routes today, but never for a
1196
+ * forwarded prefix (ADR 0022, toon-client#526). This method trusts
1197
+ * `endpoint` and fetches exactly that origin's `/ilp/identity`; resolving
1198
+ * a destination address to its terminator first is the caller's job.
1199
+ *
1200
+ * @param endpoint the TERMINATING connector's client-edge URL
1201
+ * (`https://apex`, `https://apex/ilp`) — already resolved from the
1202
+ * destination, never assumed to be the posting edge.
1194
1203
  * @param options `forceRefresh` bypasses (and replaces) the cache entry.
1195
1204
  */
1196
1205
  async getIdentity(endpoint, options = {}) {
@@ -7758,6 +7767,35 @@ function parseMakerMinaSignature(raw) {
7758
7767
  }
7759
7768
  return void 0;
7760
7769
  }
7770
+ function ilpAddressTerminates(address, destination) {
7771
+ return destination === address || destination.startsWith(`${address}.`);
7772
+ }
7773
+ function claimsTermination(peerInfo, address, destination) {
7774
+ if (destination === address) return true;
7775
+ return peerInfo.ilpAddresses === void 0 && ilpAddressTerminates(address, destination);
7776
+ }
7777
+ function outranks(claim, best) {
7778
+ if (claim.segments !== best.segments) return claim.segments > best.segments;
7779
+ return claim.isPrimary && !best.isPrimary;
7780
+ }
7781
+ function resolveTerminatorHttpEndpoint(destination, peers) {
7782
+ let best;
7783
+ for (const { peerInfo } of peers) {
7784
+ const httpEndpoint = peerInfo.httpEndpoint;
7785
+ if (!httpEndpoint) continue;
7786
+ const addresses = peerInfo.ilpAddresses ?? [peerInfo.ilpAddress];
7787
+ for (const [index, address] of addresses.entries()) {
7788
+ if (!claimsTermination(peerInfo, address, destination)) continue;
7789
+ const claim = {
7790
+ endpoint: httpEndpoint,
7791
+ segments: address.split(".").length,
7792
+ isPrimary: index === 0
7793
+ };
7794
+ if (!best || outranks(claim, best)) best = claim;
7795
+ }
7796
+ }
7797
+ return best?.endpoint;
7798
+ }
7761
7799
  function canonicalChainId(chain) {
7762
7800
  const parts = chain.split(":");
7763
7801
  return parts[0] === "evm" && parts.length >= 3 ? `evm:${parts[2]}` : chain;
@@ -8170,7 +8208,9 @@ var ToonClient = class {
8170
8208
  try {
8171
8209
  const destination = options?.destination ?? this.config.destinationAddress;
8172
8210
  const edge = this.resolveClientEdgeEndpoint();
8173
- const identity = await this.connectorEdge.getIdentity(edge);
8211
+ const identity = await this.connectorEdge.getIdentity(
8212
+ this.resolveTerminatorEndpoint(destination)
8213
+ );
8174
8214
  const amount = options?.ilpAmount !== void 0 ? String(options.ilpAmount) : String(await this.routePriceFor(edge, destination));
8175
8215
  const exchange = sealExchange(
8176
8216
  {
@@ -8241,7 +8281,7 @@ var ToonClient = class {
8241
8281
  String(error),
8242
8282
  error instanceof Error ? error.stack : ""
8243
8283
  );
8244
- if (error instanceof ToonClientError && (error.code === "NO_TERMINATED_ROUTE" || error.code === "NO_CONNECTOR_EDGE" || error.code === "PEER_NOT_NEGOTIATED" || error.code === "CHANNEL_FUNDING")) {
8284
+ 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")) {
8245
8285
  throw error;
8246
8286
  }
8247
8287
  throw new ToonClientError(
@@ -8323,6 +8363,58 @@ var ToonClient = class {
8323
8363
  }
8324
8364
  return connectorEdgeBaseUrl(endpoint);
8325
8365
  }
8366
+ /**
8367
+ * The client edge to fetch `destination`'s identity from — the connector
8368
+ * that TERMINATES it, not the one this client happens to be posting to
8369
+ * (issue #526). ADR 0022 makes identity answered-not-announced, so it is
8370
+ * fetched from the terminator's own `/ilp/identity`; discovery is only
8371
+ * used to find WHICH origin that is.
8372
+ *
8373
+ * Matches `destination` against every peer this client has discovered
8374
+ * announcing itself via kind:10032 — including ones never peered with,
8375
+ * since a forwarded prefix's terminator need not be a direct peer at all
8376
+ * (that is precisely the shape a forwarded prefix has). Falls back to the
8377
+ * posting edge only when there is no discovery tracker wired up AT ALL —
8378
+ * preserving the (still-common) same-node case where the posting node
8379
+ * terminates its own destination, and the only case that worked before
8380
+ * #526.
8381
+ *
8382
+ * Once a tracker exists, silence — whether zero peers discovered at all,
8383
+ * or peers discovered but none of them claiming `destination` — is treated
8384
+ * as a refusal, not a green light (toon-client#533): zero peers is not
8385
+ * evidence the posting edge terminates the destination, only the absence
8386
+ * of evidence that anything does, and that absence is exactly what
8387
+ * fail-closed exists for. It is also a live production window, not a
8388
+ * theoretical one: `discoveryTracker` is always constructed for a started
8389
+ * client, so a tracker reporting zero peers is what the startup race looks
8390
+ * like before the first announce lands. Likewise, an ancestor-only claim
8391
+ * (a router legitimately owning `g.toon`, which is not the same as owning
8392
+ * everything under it) used to slip through here and get treated as
8393
+ * coverage once a competing announce expired. `resolveTerminatorHttpEndpoint`'s
8394
+ * own claim-gating fixes that; the caller-visible half of the fix is this —
8395
+ * refusing to publish beats sealing to a key that cannot open the wrap,
8396
+ * for a defect whose symptom is "money spent, write lost".
8397
+ *
8398
+ * @throws {ToonClientError} NO_CONNECTOR_EDGE when no origin is known at
8399
+ * all (propagated from {@link resolveClientEdgeEndpoint}).
8400
+ * @throws {ToonClientError} TERMINATOR_UNRESOLVED when a discovery tracker
8401
+ * is present but no discovered announce claims `destination` — including
8402
+ * when it has discovered no peers at all yet.
8403
+ */
8404
+ resolveTerminatorEndpoint(destination) {
8405
+ const edge = this.resolveClientEdgeEndpoint();
8406
+ const tracker = this.state?.discoveryTracker;
8407
+ if (typeof tracker?.getAllDiscoveredPeers !== "function") return edge;
8408
+ const peers = tracker.getAllDiscoveredPeers();
8409
+ const httpEndpoint = resolveTerminatorHttpEndpoint(destination, peers);
8410
+ if (!httpEndpoint) {
8411
+ throw new ToonClientError(
8412
+ `No discovered announce terminates "${destination}" (${peers.length} peer(s) discovered, none claims it), so this client cannot confirm the posting edge is who it would be paying. Refusing to publish rather than seal to a key that may not be able to open the wrap.`,
8413
+ "TERMINATOR_UNRESOLVED"
8414
+ );
8415
+ }
8416
+ return connectorEdgeBaseUrl(httpEndpoint);
8417
+ }
8326
8418
  /**
8327
8419
  * Payment-aware HTTP fetch over TOON (issue #50). A `fetch()`-like method that
8328
8420
  * makes paying for an HTTP resource transparent:
@@ -9407,8 +9499,9 @@ var ToonClient = class {
9407
9499
  * first mutually-supported chain (legacy behavior) when unconfigured.
9408
9500
  *
9409
9501
  * Chains are matched by {@link canonicalChainId} rather than exact string
9410
- * equality (#500): the devnet preset's family-qualified `evm:base:84532`
9411
- * and the live apex's unqualified `evm:84532` name the same chain, and
9502
+ * equality (#500): a family-qualified `evm:base:84532` (a caller's config,
9503
+ * or a preset from core <3.2.1) and the live apex's unqualified
9504
+ * `evm:84532` name the same chain, and
9412
9505
  * exact matching skipped straight past it to the next mutually-supported
9413
9506
  * chain (`solana:devnet`) — negotiating a chain nobody asked for instead
9414
9507
  * of failing loudly. The PEER's own chain string is returned (not ours),