@toon-protocol/client 0.29.6 → 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 +28 -0
- package/dist/index.js +111 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -3487,6 +3487,34 @@ declare class ToonClient {
|
|
|
3487
3487
|
* requires BTP but this client has no `btpClient` configured at all.
|
|
3488
3488
|
*/
|
|
3489
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;
|
|
3490
3518
|
/**
|
|
3491
3519
|
* Shared claim-resolution logic used by `publishEvent` and `sendSwapPacket`.
|
|
3492
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
|
}
|
|
@@ -8231,10 +8275,7 @@ var ToonClient = class {
|
|
|
8231
8275
|
this.config.preferredTokens,
|
|
8232
8276
|
matchedChain
|
|
8233
8277
|
),
|
|
8234
|
-
tokenNetwork: peerInfo.tokenNetworks?.[matchedChain] ?? lookupByCanonicalChain(
|
|
8235
|
-
this.config.tokenNetworks,
|
|
8236
|
-
matchedChain
|
|
8237
|
-
)
|
|
8278
|
+
tokenNetwork: peerInfo.tokenNetworks?.[matchedChain] ?? lookupByCanonicalChain(this.config.tokenNetworks, matchedChain)
|
|
8238
8279
|
});
|
|
8239
8280
|
}
|
|
8240
8281
|
}
|
|
@@ -8367,7 +8408,9 @@ var ToonClient = class {
|
|
|
8367
8408
|
BigInt(amount),
|
|
8368
8409
|
options?.claim
|
|
8369
8410
|
);
|
|
8370
|
-
const response = await
|
|
8411
|
+
const response = await this.sendClaimBearingPacket(
|
|
8412
|
+
destination,
|
|
8413
|
+
transport,
|
|
8371
8414
|
{
|
|
8372
8415
|
destination,
|
|
8373
8416
|
amount,
|
|
@@ -8686,7 +8729,9 @@ var ToonClient = class {
|
|
|
8686
8729
|
params.amount,
|
|
8687
8730
|
params.claim
|
|
8688
8731
|
);
|
|
8689
|
-
return
|
|
8732
|
+
return this.sendClaimBearingPacket(
|
|
8733
|
+
params.destination,
|
|
8734
|
+
transport,
|
|
8690
8735
|
{
|
|
8691
8736
|
destination: params.destination,
|
|
8692
8737
|
amount: String(params.amount),
|
|
@@ -8793,6 +8838,50 @@ var ToonClient = class {
|
|
|
8793
8838
|
"NO_ILP_TRANSPORT"
|
|
8794
8839
|
);
|
|
8795
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
|
+
}
|
|
8796
8885
|
/**
|
|
8797
8886
|
* Shared claim-resolution logic used by `publishEvent` and `sendSwapPacket`.
|
|
8798
8887
|
*/
|
|
@@ -8964,7 +9053,12 @@ var ToonClient = class {
|
|
|
8964
9053
|
channelAccount: channelId,
|
|
8965
9054
|
expires
|
|
8966
9055
|
});
|
|
8967
|
-
return {
|
|
9056
|
+
return {
|
|
9057
|
+
blockchain: "solana",
|
|
9058
|
+
channelAccount: channelId,
|
|
9059
|
+
expires,
|
|
9060
|
+
signature
|
|
9061
|
+
};
|
|
8968
9062
|
}
|
|
8969
9063
|
return void 0;
|
|
8970
9064
|
}
|
|
@@ -9763,7 +9857,9 @@ var ToonClient = class {
|
|
|
9763
9857
|
}
|
|
9764
9858
|
const transport = this.getClaimTransport(params.destination);
|
|
9765
9859
|
const claimMessage = this.buildClaimMessageForProof(params.claim);
|
|
9766
|
-
return
|
|
9860
|
+
return this.sendClaimBearingPacket(
|
|
9861
|
+
params.destination,
|
|
9862
|
+
transport,
|
|
9767
9863
|
ilpParams,
|
|
9768
9864
|
claimMessage
|
|
9769
9865
|
);
|