@toon-protocol/client 0.29.5 → 0.29.7
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 +55 -0
- package/dist/index.js +203 -29
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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,10 +3472,49 @@ 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;
|
|
3490
|
+
/**
|
|
3491
|
+
* Sends a claim-bearing PREPARE over the ALREADY-RESOLVED `transport` (a
|
|
3492
|
+
* prior {@link getClaimTransport} call — kept as a separate argument so the
|
|
3493
|
+
* NO_ILP_TRANSPORT/BTP_REQUIRED failure modes of resolving a transport at
|
|
3494
|
+
* all still surface before any claim is resolved, matching every caller's
|
|
3495
|
+
* existing behavior), retrying once over the BTP uplink when it comes back
|
|
3496
|
+
* with {@link Http402RequiresBtpError} (issue #561).
|
|
3497
|
+
*
|
|
3498
|
+
* `getClaimTransport`'s own `requiredTransport` check ({@link
|
|
3499
|
+
* terminatorRequiresBtp}) reads the peer's kind:10032 announce — but the
|
|
3500
|
+
* live devnet relay's announce never carries that field, only its `402`
|
|
3501
|
+
* response does, and only once a write has already been posted over HTTP.
|
|
3502
|
+
* So the announce-based guard alone is dead code against that fleet: this
|
|
3503
|
+
* is the fallback that actually avoids the repeat-402 loop, discovered
|
|
3504
|
+
* live rather than assumed from the announce.
|
|
3505
|
+
*
|
|
3506
|
+
* A retry is attempted only when the failing transport was not already the
|
|
3507
|
+
* BTP one (an `Http402RequiresBtpError` can only come from the HTTP
|
|
3508
|
+
* transport in the first place — BTP has no HTTP status codes) and a BTP
|
|
3509
|
+
* uplink is actually configured; otherwise this throws the same
|
|
3510
|
+
* `BTP_REQUIRED` error `getClaimTransport` throws for the announce-based
|
|
3511
|
+
* case, so callers see one consistent code regardless of which guard
|
|
3512
|
+
* caught it.
|
|
3513
|
+
*
|
|
3514
|
+
* @throws {ToonClientError} BTP_REQUIRED when the terminator's 402 demands
|
|
3515
|
+
* BTP but this client has no BTP uplink (`btpUrl`) configured.
|
|
3516
|
+
*/
|
|
3517
|
+
private sendClaimBearingPacket;
|
|
3463
3518
|
/**
|
|
3464
3519
|
* Shared claim-resolution logic used by `publishEvent` and `sendSwapPacket`.
|
|
3465
3520
|
*/
|
package/dist/index.js
CHANGED
|
@@ -65,6 +65,12 @@ var ConnectorError = class extends ToonClientError {
|
|
|
65
65
|
this.name = "ConnectorError";
|
|
66
66
|
}
|
|
67
67
|
};
|
|
68
|
+
var Http402RequiresBtpError = class extends ToonClientError {
|
|
69
|
+
constructor(message2, cause) {
|
|
70
|
+
super(message2, "HTTP_402_REQUIRES_BTP", cause);
|
|
71
|
+
this.name = "Http402RequiresBtpError";
|
|
72
|
+
}
|
|
73
|
+
};
|
|
68
74
|
var ValidationError = class extends ToonClientError {
|
|
69
75
|
constructor(message2, cause) {
|
|
70
76
|
super(message2, "VALIDATION_ERROR", cause);
|
|
@@ -3235,9 +3241,7 @@ var HttpIlpClient = class {
|
|
|
3235
3241
|
...this.authHeaders()
|
|
3236
3242
|
};
|
|
3237
3243
|
if (claim !== void 0) {
|
|
3238
|
-
headers[ILP_CLAIM_HEADER] = toBase64(
|
|
3239
|
-
encodeUtf8(JSON.stringify(claim))
|
|
3240
|
-
);
|
|
3244
|
+
headers[ILP_CLAIM_HEADER] = toBase64(encodeUtf8(JSON.stringify(claim)));
|
|
3241
3245
|
}
|
|
3242
3246
|
const controller = new AbortController();
|
|
3243
3247
|
const timeoutId = setTimeout(() => controller.abort(), requestTimeout);
|
|
@@ -3269,12 +3273,22 @@ var HttpIlpClient = class {
|
|
|
3269
3273
|
if (response.ok) {
|
|
3270
3274
|
const buf = new Uint8Array(await response.arrayBuffer());
|
|
3271
3275
|
if (buf.length === 0) {
|
|
3272
|
-
throw new ConnectorError(
|
|
3276
|
+
throw new ConnectorError(
|
|
3277
|
+
"Empty 200 body from /ilp (expected OER ILP response)"
|
|
3278
|
+
);
|
|
3273
3279
|
}
|
|
3274
3280
|
return mapIlpResponse(deserializeIlpPacket(buf), sentCondition);
|
|
3275
3281
|
}
|
|
3276
3282
|
const body = await response.text().catch(() => "");
|
|
3277
3283
|
const detail = body ? `: ${body}` : "";
|
|
3284
|
+
if (response.status === 402) {
|
|
3285
|
+
const requiredTransport = extractRequiredTransportFrom402Body(body);
|
|
3286
|
+
if (requiredTransport === "btp") {
|
|
3287
|
+
throw new Http402RequiresBtpError(
|
|
3288
|
+
`The connector at ${this.httpEndpoint} refused this paid write with 402, declaring requiredTransport: "btp" in its x402 challenge.`
|
|
3289
|
+
);
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3278
3292
|
if (response.status >= 500) {
|
|
3279
3293
|
throw new ConnectorError(
|
|
3280
3294
|
`Connector transport error (${response.status} ${response.statusText})${detail}`
|
|
@@ -3285,14 +3299,24 @@ var HttpIlpClient = class {
|
|
|
3285
3299
|
);
|
|
3286
3300
|
}
|
|
3287
3301
|
mapTransportError(error, requestTimeout) {
|
|
3288
|
-
if (error instanceof ConnectorError || error instanceof NetworkError
|
|
3302
|
+
if (error instanceof ConnectorError || error instanceof NetworkError || // Thrown by `mapResponse` inside the same try — must pass through
|
|
3303
|
+
// unwrapped so a caller can retry over BTP (toon-client#561). It is
|
|
3304
|
+
// not a `ConnectorError` (a distinct code/type is the whole point of
|
|
3305
|
+
// the class), so it needs its own check here.
|
|
3306
|
+
error instanceof Http402RequiresBtpError) {
|
|
3289
3307
|
return error;
|
|
3290
3308
|
}
|
|
3291
3309
|
if (error instanceof Error && error.name === "AbortError") {
|
|
3292
|
-
return new NetworkError(
|
|
3310
|
+
return new NetworkError(
|
|
3311
|
+
`Request timeout after ${requestTimeout}ms`,
|
|
3312
|
+
error
|
|
3313
|
+
);
|
|
3293
3314
|
}
|
|
3294
3315
|
if (error instanceof TypeError && (error.message.includes("fetch failed") || error.message.includes("ECONNREFUSED") || error.message.includes("ECONNRESET") || error.message.includes("ETIMEDOUT") || error.message.includes("network"))) {
|
|
3295
|
-
return new NetworkError(
|
|
3316
|
+
return new NetworkError(
|
|
3317
|
+
`Network connection failed: ${error.message}`,
|
|
3318
|
+
error
|
|
3319
|
+
);
|
|
3296
3320
|
}
|
|
3297
3321
|
return new ConnectorError(
|
|
3298
3322
|
`Unexpected error during ILP-over-HTTP request: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -3300,6 +3324,26 @@ var HttpIlpClient = class {
|
|
|
3300
3324
|
);
|
|
3301
3325
|
}
|
|
3302
3326
|
};
|
|
3327
|
+
function extractRequiredTransportFrom402Body(body) {
|
|
3328
|
+
let parsed;
|
|
3329
|
+
try {
|
|
3330
|
+
parsed = JSON.parse(body);
|
|
3331
|
+
} catch {
|
|
3332
|
+
return void 0;
|
|
3333
|
+
}
|
|
3334
|
+
if (typeof parsed !== "object" || parsed === null) return void 0;
|
|
3335
|
+
const accepts = parsed["accepts"];
|
|
3336
|
+
if (!Array.isArray(accepts)) return void 0;
|
|
3337
|
+
for (const raw of accepts) {
|
|
3338
|
+
if (typeof raw !== "object" || raw === null) continue;
|
|
3339
|
+
const entry = raw;
|
|
3340
|
+
const extra = entry["extra"];
|
|
3341
|
+
const fromExtra = typeof extra === "object" && extra !== null ? extra["requiredTransport"] : void 0;
|
|
3342
|
+
const value = fromExtra ?? entry["requiredTransport"];
|
|
3343
|
+
if (typeof value === "string") return value;
|
|
3344
|
+
}
|
|
3345
|
+
return void 0;
|
|
3346
|
+
}
|
|
3303
3347
|
function httpEndpointToBtpUrl(httpEndpoint) {
|
|
3304
3348
|
return httpEndpoint.replace(/^https:\/\//i, "wss://").replace(/^http:\/\//i, "ws://");
|
|
3305
3349
|
}
|
|
@@ -5203,8 +5247,22 @@ async function initializeHttpMode(config, hooks = {}) {
|
|
|
5203
5247
|
import { ILP_PEER_INFO_KIND } from "@toon-protocol/core";
|
|
5204
5248
|
async function subscribeToDiscovery(relayUrls, tracker) {
|
|
5205
5249
|
const urls = Array.from(new Set(relayUrls.filter((url) => url !== "")));
|
|
5250
|
+
const requiredTransports = /* @__PURE__ */ new Map();
|
|
5251
|
+
const requiredTransportSeenAt = /* @__PURE__ */ new Map();
|
|
5252
|
+
const requiredTransportFor = (pubkey) => requiredTransports.get(pubkey);
|
|
5253
|
+
const recordRequiredTransport = (event) => {
|
|
5254
|
+
const lastSeen = requiredTransportSeenAt.get(event.pubkey) ?? 0;
|
|
5255
|
+
if (event.created_at <= lastSeen) return;
|
|
5256
|
+
requiredTransportSeenAt.set(event.pubkey, event.created_at);
|
|
5257
|
+
const requiredTransport = extractRequiredTransport(event);
|
|
5258
|
+
if (requiredTransport === void 0) {
|
|
5259
|
+
requiredTransports.delete(event.pubkey);
|
|
5260
|
+
} else {
|
|
5261
|
+
requiredTransports.set(event.pubkey, requiredTransport);
|
|
5262
|
+
}
|
|
5263
|
+
};
|
|
5206
5264
|
if (urls.length === 0) {
|
|
5207
|
-
return { close: () => void 0 };
|
|
5265
|
+
return { close: () => void 0, requiredTransportFor };
|
|
5208
5266
|
}
|
|
5209
5267
|
const { SimplePool } = await import("nostr-tools/pool");
|
|
5210
5268
|
const pool = new SimplePool();
|
|
@@ -5212,16 +5270,30 @@ async function subscribeToDiscovery(relayUrls, tracker) {
|
|
|
5212
5270
|
urls,
|
|
5213
5271
|
{ kinds: [ILP_PEER_INFO_KIND] },
|
|
5214
5272
|
{
|
|
5215
|
-
onevent: (event) =>
|
|
5273
|
+
onevent: (event) => {
|
|
5274
|
+
tracker.processEvent(event);
|
|
5275
|
+
recordRequiredTransport(event);
|
|
5276
|
+
}
|
|
5216
5277
|
}
|
|
5217
5278
|
);
|
|
5218
5279
|
return {
|
|
5219
5280
|
close: () => {
|
|
5220
5281
|
sub.close();
|
|
5221
5282
|
pool.close(urls);
|
|
5222
|
-
}
|
|
5283
|
+
},
|
|
5284
|
+
requiredTransportFor
|
|
5223
5285
|
};
|
|
5224
5286
|
}
|
|
5287
|
+
function extractRequiredTransport(event) {
|
|
5288
|
+
try {
|
|
5289
|
+
const parsed = JSON.parse(event.content);
|
|
5290
|
+
if (typeof parsed !== "object" || parsed === null) return void 0;
|
|
5291
|
+
const value = parsed["requiredTransport"];
|
|
5292
|
+
return typeof value === "string" ? value : void 0;
|
|
5293
|
+
} catch {
|
|
5294
|
+
return void 0;
|
|
5295
|
+
}
|
|
5296
|
+
}
|
|
5225
5297
|
|
|
5226
5298
|
// src/signing/solana-signer.ts
|
|
5227
5299
|
import { ed25519 as ed255193 } from "@noble/curves/ed25519.js";
|
|
@@ -7841,13 +7913,19 @@ function claimsTermination(peerInfo, address, destination) {
|
|
|
7841
7913
|
if (destination === address) return true;
|
|
7842
7914
|
return peerInfo.ilpAddresses === void 0 && ilpAddressTerminates(address, destination);
|
|
7843
7915
|
}
|
|
7916
|
+
function sendsClaims(candidate) {
|
|
7917
|
+
return typeof candidate?.sendIlpPacketWithClaim === "function";
|
|
7918
|
+
}
|
|
7844
7919
|
function outranks(claim, best) {
|
|
7845
7920
|
if (claim.segments !== best.segments) return claim.segments > best.segments;
|
|
7846
7921
|
return claim.isPrimary && !best.isPrimary;
|
|
7847
7922
|
}
|
|
7848
7923
|
function resolveTerminatorHttpEndpoint(destination, peers) {
|
|
7924
|
+
return findBestTerminatorClaim(destination, peers)?.endpoint;
|
|
7925
|
+
}
|
|
7926
|
+
function findBestTerminatorClaim(destination, peers) {
|
|
7849
7927
|
let best;
|
|
7850
|
-
for (const { peerInfo } of peers) {
|
|
7928
|
+
for (const { peerInfo, pubkey } of peers) {
|
|
7851
7929
|
const httpEndpoint = peerInfo.httpEndpoint;
|
|
7852
7930
|
if (!httpEndpoint) continue;
|
|
7853
7931
|
const addresses = peerInfo.ilpAddresses ?? [peerInfo.ilpAddress];
|
|
@@ -7856,12 +7934,13 @@ function resolveTerminatorHttpEndpoint(destination, peers) {
|
|
|
7856
7934
|
const claim = {
|
|
7857
7935
|
endpoint: httpEndpoint,
|
|
7858
7936
|
segments: address.split(".").length,
|
|
7859
|
-
isPrimary: index === 0
|
|
7937
|
+
isPrimary: index === 0,
|
|
7938
|
+
pubkey
|
|
7860
7939
|
};
|
|
7861
7940
|
if (!best || outranks(claim, best)) best = claim;
|
|
7862
7941
|
}
|
|
7863
7942
|
}
|
|
7864
|
-
return best
|
|
7943
|
+
return best;
|
|
7865
7944
|
}
|
|
7866
7945
|
function canonicalChainId(chain) {
|
|
7867
7946
|
const parts = chain.split(":");
|
|
@@ -8196,10 +8275,7 @@ var ToonClient = class {
|
|
|
8196
8275
|
this.config.preferredTokens,
|
|
8197
8276
|
matchedChain
|
|
8198
8277
|
),
|
|
8199
|
-
tokenNetwork: peerInfo.tokenNetworks?.[matchedChain] ?? lookupByCanonicalChain(
|
|
8200
|
-
this.config.tokenNetworks,
|
|
8201
|
-
matchedChain
|
|
8202
|
-
)
|
|
8278
|
+
tokenNetwork: peerInfo.tokenNetworks?.[matchedChain] ?? lookupByCanonicalChain(this.config.tokenNetworks, matchedChain)
|
|
8203
8279
|
});
|
|
8204
8280
|
}
|
|
8205
8281
|
}
|
|
@@ -8326,13 +8402,15 @@ var ToonClient = class {
|
|
|
8326
8402
|
},
|
|
8327
8403
|
identity.publicKey
|
|
8328
8404
|
);
|
|
8329
|
-
const transport = this.getClaimTransport();
|
|
8405
|
+
const transport = this.getClaimTransport(destination);
|
|
8330
8406
|
const claimMessage = await this.resolveClaimForDestination(
|
|
8331
8407
|
destination,
|
|
8332
8408
|
BigInt(amount),
|
|
8333
8409
|
options?.claim
|
|
8334
8410
|
);
|
|
8335
|
-
const response = await
|
|
8411
|
+
const response = await this.sendClaimBearingPacket(
|
|
8412
|
+
destination,
|
|
8413
|
+
transport,
|
|
8336
8414
|
{
|
|
8337
8415
|
destination,
|
|
8338
8416
|
amount,
|
|
@@ -8378,7 +8456,7 @@ var ToonClient = class {
|
|
|
8378
8456
|
String(error),
|
|
8379
8457
|
error instanceof Error ? error.stack : ""
|
|
8380
8458
|
);
|
|
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")) {
|
|
8459
|
+
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
8460
|
throw error;
|
|
8383
8461
|
}
|
|
8384
8462
|
throw new ToonClientError(
|
|
@@ -8512,6 +8590,33 @@ var ToonClient = class {
|
|
|
8512
8590
|
}
|
|
8513
8591
|
return connectorEdgeBaseUrl(httpEndpoint);
|
|
8514
8592
|
}
|
|
8593
|
+
/**
|
|
8594
|
+
* Whether the announce that TERMINATES `destination` declares
|
|
8595
|
+
* `requiredTransport: "btp"` (issue #558) — the connector accepts `POST
|
|
8596
|
+
* /ilp` for identity/price reads, but rejects a PAID WRITE over HTTP with
|
|
8597
|
+
* `402 Payment Required` unless it rode a BTP session.
|
|
8598
|
+
*
|
|
8599
|
+
* `requiredTransport` is read off the announce's raw content by
|
|
8600
|
+
* `DiscoverySubscription.requiredTransportFor` (`discovery-subscription.ts`),
|
|
8601
|
+
* never through the parsed `IlpPeerInfo`: the installed
|
|
8602
|
+
* `@toon-protocol/core`'s `parseIlpPeerInfo` drops unknown fields, the same
|
|
8603
|
+
* gap toon-client#544 hit for `notice`. Silent `false` (never sealed HTTP is
|
|
8604
|
+
* fine) whenever a tracker, its winning claim, or the subscription's raw
|
|
8605
|
+
* feed is unavailable — this is an ADDITIONAL constraint on top of
|
|
8606
|
+
* `resolveTerminatorEndpoint`'s own claim gating, not a replacement for it.
|
|
8607
|
+
*/
|
|
8608
|
+
terminatorRequiresBtp(destination) {
|
|
8609
|
+
const tracker = this.state?.discoveryTracker;
|
|
8610
|
+
if (typeof tracker?.getAllDiscoveredPeers !== "function") return false;
|
|
8611
|
+
const subscription = this.state?.discoverySubscription;
|
|
8612
|
+
if (typeof subscription?.requiredTransportFor !== "function") return false;
|
|
8613
|
+
const claim = findBestTerminatorClaim(
|
|
8614
|
+
destination,
|
|
8615
|
+
tracker.getAllDiscoveredPeers()
|
|
8616
|
+
);
|
|
8617
|
+
if (!claim) return false;
|
|
8618
|
+
return subscription.requiredTransportFor(claim.pubkey) === "btp";
|
|
8619
|
+
}
|
|
8515
8620
|
/**
|
|
8516
8621
|
* Payment-aware HTTP fetch over TOON (issue #50). A `fetch()`-like method that
|
|
8517
8622
|
* makes paying for an HTTP resource transparent:
|
|
@@ -8618,13 +8723,15 @@ var ToonClient = class {
|
|
|
8618
8723
|
"INVALID_STATE"
|
|
8619
8724
|
);
|
|
8620
8725
|
}
|
|
8621
|
-
const transport = this.getClaimTransport();
|
|
8726
|
+
const transport = this.getClaimTransport(params.destination);
|
|
8622
8727
|
const claimMessage = await this.resolveClaimForDestination(
|
|
8623
8728
|
params.destination,
|
|
8624
8729
|
params.amount,
|
|
8625
8730
|
params.claim
|
|
8626
8731
|
);
|
|
8627
|
-
return
|
|
8732
|
+
return this.sendClaimBearingPacket(
|
|
8733
|
+
params.destination,
|
|
8734
|
+
transport,
|
|
8628
8735
|
{
|
|
8629
8736
|
destination: params.destination,
|
|
8630
8737
|
amount: String(params.amount),
|
|
@@ -8693,10 +8800,21 @@ var ToonClient = class {
|
|
|
8693
8800
|
* proxy) does NOT implement `sendIlpPacketWithClaim`; in that case there is no
|
|
8694
8801
|
* paid-write transport and we throw a clear, actionable error.
|
|
8695
8802
|
*
|
|
8803
|
+
* `destination`, when given, is checked against {@link terminatorRequiresBtp}
|
|
8804
|
+
* FIRST, overriding both orderings above (issue #558): a terminating
|
|
8805
|
+
* announce that declares `requiredTransport: "btp"` gets `btpClient`
|
|
8806
|
+
* exclusively — never `runtimeClient`, even when HTTP would otherwise have
|
|
8807
|
+
* been tried first — because the connector enforces that requirement with
|
|
8808
|
+
* a `402` on every paid write, so a retry over HTTP can only ever repeat
|
|
8809
|
+
* the same failure. Omitting `destination` (or a tracker that cannot
|
|
8810
|
+
* resolve one) reproduces the exact precedence above, unchanged.
|
|
8811
|
+
*
|
|
8696
8812
|
* @throws {ToonClientError} NO_ILP_TRANSPORT when no active transport can send
|
|
8697
8813
|
* a packet+claim.
|
|
8814
|
+
* @throws {ToonClientError} BTP_REQUIRED when `destination`'s terminator
|
|
8815
|
+
* requires BTP but this client has no `btpClient` configured at all.
|
|
8698
8816
|
*/
|
|
8699
|
-
getClaimTransport() {
|
|
8817
|
+
getClaimTransport(destination) {
|
|
8700
8818
|
const state = this.state;
|
|
8701
8819
|
if (!state) {
|
|
8702
8820
|
throw new ToonClientError(
|
|
@@ -8704,17 +8822,66 @@ var ToonClient = class {
|
|
|
8704
8822
|
"INVALID_STATE"
|
|
8705
8823
|
);
|
|
8706
8824
|
}
|
|
8825
|
+
if (destination !== void 0 && this.terminatorRequiresBtp(destination)) {
|
|
8826
|
+
if (sendsClaims(state.btpClient)) return state.btpClient;
|
|
8827
|
+
throw new ToonClientError(
|
|
8828
|
+
`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.`,
|
|
8829
|
+
"BTP_REQUIRED"
|
|
8830
|
+
);
|
|
8831
|
+
}
|
|
8707
8832
|
const candidates = this.config.preferBtpForPaidWrites ? [state.btpClient, state.runtimeClient] : [state.runtimeClient, state.btpClient];
|
|
8708
8833
|
for (const candidate of candidates) {
|
|
8709
|
-
if (candidate
|
|
8710
|
-
return candidate;
|
|
8711
|
-
}
|
|
8834
|
+
if (sendsClaims(candidate)) return candidate;
|
|
8712
8835
|
}
|
|
8713
8836
|
throw new ToonClientError(
|
|
8714
8837
|
"No ILP transport for paid writes. Configure `proxyUrl`/`connectorHttpEndpoint` (route through the connector proxy over ILP-over-HTTP) or `btpUrl` (BTP socket).",
|
|
8715
8838
|
"NO_ILP_TRANSPORT"
|
|
8716
8839
|
);
|
|
8717
8840
|
}
|
|
8841
|
+
/**
|
|
8842
|
+
* Sends a claim-bearing PREPARE over the ALREADY-RESOLVED `transport` (a
|
|
8843
|
+
* prior {@link getClaimTransport} call — kept as a separate argument so the
|
|
8844
|
+
* NO_ILP_TRANSPORT/BTP_REQUIRED failure modes of resolving a transport at
|
|
8845
|
+
* all still surface before any claim is resolved, matching every caller's
|
|
8846
|
+
* existing behavior), retrying once over the BTP uplink when it comes back
|
|
8847
|
+
* with {@link Http402RequiresBtpError} (issue #561).
|
|
8848
|
+
*
|
|
8849
|
+
* `getClaimTransport`'s own `requiredTransport` check ({@link
|
|
8850
|
+
* terminatorRequiresBtp}) reads the peer's kind:10032 announce — but the
|
|
8851
|
+
* live devnet relay's announce never carries that field, only its `402`
|
|
8852
|
+
* response does, and only once a write has already been posted over HTTP.
|
|
8853
|
+
* So the announce-based guard alone is dead code against that fleet: this
|
|
8854
|
+
* is the fallback that actually avoids the repeat-402 loop, discovered
|
|
8855
|
+
* live rather than assumed from the announce.
|
|
8856
|
+
*
|
|
8857
|
+
* A retry is attempted only when the failing transport was not already the
|
|
8858
|
+
* BTP one (an `Http402RequiresBtpError` can only come from the HTTP
|
|
8859
|
+
* transport in the first place — BTP has no HTTP status codes) and a BTP
|
|
8860
|
+
* uplink is actually configured; otherwise this throws the same
|
|
8861
|
+
* `BTP_REQUIRED` error `getClaimTransport` throws for the announce-based
|
|
8862
|
+
* case, so callers see one consistent code regardless of which guard
|
|
8863
|
+
* caught it.
|
|
8864
|
+
*
|
|
8865
|
+
* @throws {ToonClientError} BTP_REQUIRED when the terminator's 402 demands
|
|
8866
|
+
* BTP but this client has no BTP uplink (`btpUrl`) configured.
|
|
8867
|
+
*/
|
|
8868
|
+
async sendClaimBearingPacket(destination, transport, params, claim) {
|
|
8869
|
+
try {
|
|
8870
|
+
return await transport.sendIlpPacketWithClaim(params, claim);
|
|
8871
|
+
} catch (error) {
|
|
8872
|
+
if (!(error instanceof Http402RequiresBtpError)) throw error;
|
|
8873
|
+
const uplink = this.state?.btpClient;
|
|
8874
|
+
const btp = sendsClaims(uplink) ? uplink : void 0;
|
|
8875
|
+
if (btp && btp !== transport) {
|
|
8876
|
+
return btp.sendIlpPacketWithClaim(params, claim);
|
|
8877
|
+
}
|
|
8878
|
+
throw new ToonClientError(
|
|
8879
|
+
`The connector terminating "${destination}" requires BTP for paid writes (its 402 response declared requiredTransport: "btp"), but this client has no BTP uplink (\`btpUrl\`) configured.`,
|
|
8880
|
+
"BTP_REQUIRED",
|
|
8881
|
+
error
|
|
8882
|
+
);
|
|
8883
|
+
}
|
|
8884
|
+
}
|
|
8718
8885
|
/**
|
|
8719
8886
|
* Shared claim-resolution logic used by `publishEvent` and `sendSwapPacket`.
|
|
8720
8887
|
*/
|
|
@@ -8886,7 +9053,12 @@ var ToonClient = class {
|
|
|
8886
9053
|
channelAccount: channelId,
|
|
8887
9054
|
expires
|
|
8888
9055
|
});
|
|
8889
|
-
return {
|
|
9056
|
+
return {
|
|
9057
|
+
blockchain: "solana",
|
|
9058
|
+
channelAccount: channelId,
|
|
9059
|
+
expires,
|
|
9060
|
+
signature
|
|
9061
|
+
};
|
|
8890
9062
|
}
|
|
8891
9063
|
return void 0;
|
|
8892
9064
|
}
|
|
@@ -9683,9 +9855,11 @@ var ToonClient = class {
|
|
|
9683
9855
|
"MISSING_CLAIM"
|
|
9684
9856
|
);
|
|
9685
9857
|
}
|
|
9686
|
-
const transport = this.getClaimTransport();
|
|
9858
|
+
const transport = this.getClaimTransport(params.destination);
|
|
9687
9859
|
const claimMessage = this.buildClaimMessageForProof(params.claim);
|
|
9688
|
-
return
|
|
9860
|
+
return this.sendClaimBearingPacket(
|
|
9861
|
+
params.destination,
|
|
9862
|
+
transport,
|
|
9689
9863
|
ilpParams,
|
|
9690
9864
|
claimMessage
|
|
9691
9865
|
);
|