@toon-protocol/client 0.29.4 → 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 +27 -0
- package/dist/index.js +122 -11
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
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
|
@@ -5199,6 +5199,58 @@ async function initializeHttpMode(config, hooks = {}) {
|
|
|
5199
5199
|
};
|
|
5200
5200
|
}
|
|
5201
5201
|
|
|
5202
|
+
// src/discovery-subscription.ts
|
|
5203
|
+
import { ILP_PEER_INFO_KIND } from "@toon-protocol/core";
|
|
5204
|
+
async function subscribeToDiscovery(relayUrls, tracker) {
|
|
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
|
+
};
|
|
5220
|
+
if (urls.length === 0) {
|
|
5221
|
+
return { close: () => void 0, requiredTransportFor };
|
|
5222
|
+
}
|
|
5223
|
+
const { SimplePool } = await import("nostr-tools/pool");
|
|
5224
|
+
const pool = new SimplePool();
|
|
5225
|
+
const sub = pool.subscribeMany(
|
|
5226
|
+
urls,
|
|
5227
|
+
{ kinds: [ILP_PEER_INFO_KIND] },
|
|
5228
|
+
{
|
|
5229
|
+
onevent: (event) => {
|
|
5230
|
+
tracker.processEvent(event);
|
|
5231
|
+
recordRequiredTransport(event);
|
|
5232
|
+
}
|
|
5233
|
+
}
|
|
5234
|
+
);
|
|
5235
|
+
return {
|
|
5236
|
+
close: () => {
|
|
5237
|
+
sub.close();
|
|
5238
|
+
pool.close(urls);
|
|
5239
|
+
},
|
|
5240
|
+
requiredTransportFor
|
|
5241
|
+
};
|
|
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
|
+
}
|
|
5253
|
+
|
|
5202
5254
|
// src/signing/solana-signer.ts
|
|
5203
5255
|
import { ed25519 as ed255193 } from "@noble/curves/ed25519.js";
|
|
5204
5256
|
import { base58Encode as base58Encode3, base58Decode as base58Decode2 } from "@toon-protocol/core";
|
|
@@ -7817,13 +7869,19 @@ function claimsTermination(peerInfo, address, destination) {
|
|
|
7817
7869
|
if (destination === address) return true;
|
|
7818
7870
|
return peerInfo.ilpAddresses === void 0 && ilpAddressTerminates(address, destination);
|
|
7819
7871
|
}
|
|
7872
|
+
function sendsClaims(candidate) {
|
|
7873
|
+
return typeof candidate?.sendIlpPacketWithClaim === "function";
|
|
7874
|
+
}
|
|
7820
7875
|
function outranks(claim, best) {
|
|
7821
7876
|
if (claim.segments !== best.segments) return claim.segments > best.segments;
|
|
7822
7877
|
return claim.isPrimary && !best.isPrimary;
|
|
7823
7878
|
}
|
|
7824
7879
|
function resolveTerminatorHttpEndpoint(destination, peers) {
|
|
7880
|
+
return findBestTerminatorClaim(destination, peers)?.endpoint;
|
|
7881
|
+
}
|
|
7882
|
+
function findBestTerminatorClaim(destination, peers) {
|
|
7825
7883
|
let best;
|
|
7826
|
-
for (const { peerInfo } of peers) {
|
|
7884
|
+
for (const { peerInfo, pubkey } of peers) {
|
|
7827
7885
|
const httpEndpoint = peerInfo.httpEndpoint;
|
|
7828
7886
|
if (!httpEndpoint) continue;
|
|
7829
7887
|
const addresses = peerInfo.ilpAddresses ?? [peerInfo.ilpAddress];
|
|
@@ -7832,12 +7890,13 @@ function resolveTerminatorHttpEndpoint(destination, peers) {
|
|
|
7832
7890
|
const claim = {
|
|
7833
7891
|
endpoint: httpEndpoint,
|
|
7834
7892
|
segments: address.split(".").length,
|
|
7835
|
-
isPrimary: index === 0
|
|
7893
|
+
isPrimary: index === 0,
|
|
7894
|
+
pubkey
|
|
7836
7895
|
};
|
|
7837
7896
|
if (!best || outranks(claim, best)) best = claim;
|
|
7838
7897
|
}
|
|
7839
7898
|
}
|
|
7840
|
-
return best
|
|
7899
|
+
return best;
|
|
7841
7900
|
}
|
|
7842
7901
|
function canonicalChainId(chain) {
|
|
7843
7902
|
const parts = chain.split(":");
|
|
@@ -8113,6 +8172,13 @@ var ToonClient = class {
|
|
|
8113
8172
|
btpClient,
|
|
8114
8173
|
btpSession
|
|
8115
8174
|
} = initialization;
|
|
8175
|
+
const discoverySubscription = await subscribeToDiscovery(
|
|
8176
|
+
[
|
|
8177
|
+
this.config.relayUrl,
|
|
8178
|
+
...(this.config.knownPeers ?? []).map((peer) => peer.relayUrl)
|
|
8179
|
+
],
|
|
8180
|
+
discoveryTracker
|
|
8181
|
+
);
|
|
8116
8182
|
if (this.channelManager) {
|
|
8117
8183
|
const cm = this.channelManager;
|
|
8118
8184
|
const nostrPubkey = this.getPublicKey();
|
|
@@ -8208,6 +8274,7 @@ var ToonClient = class {
|
|
|
8208
8274
|
this.state = {
|
|
8209
8275
|
bootstrapService,
|
|
8210
8276
|
discoveryTracker,
|
|
8277
|
+
discoverySubscription,
|
|
8211
8278
|
runtimeClient,
|
|
8212
8279
|
peersDiscovered: bootstrapResults.length,
|
|
8213
8280
|
btpClient: btpClient ?? void 0,
|
|
@@ -8294,7 +8361,7 @@ var ToonClient = class {
|
|
|
8294
8361
|
},
|
|
8295
8362
|
identity.publicKey
|
|
8296
8363
|
);
|
|
8297
|
-
const transport = this.getClaimTransport();
|
|
8364
|
+
const transport = this.getClaimTransport(destination);
|
|
8298
8365
|
const claimMessage = await this.resolveClaimForDestination(
|
|
8299
8366
|
destination,
|
|
8300
8367
|
BigInt(amount),
|
|
@@ -8346,7 +8413,7 @@ var ToonClient = class {
|
|
|
8346
8413
|
String(error),
|
|
8347
8414
|
error instanceof Error ? error.stack : ""
|
|
8348
8415
|
);
|
|
8349
|
-
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")) {
|
|
8350
8417
|
throw error;
|
|
8351
8418
|
}
|
|
8352
8419
|
throw new ToonClientError(
|
|
@@ -8480,6 +8547,33 @@ var ToonClient = class {
|
|
|
8480
8547
|
}
|
|
8481
8548
|
return connectorEdgeBaseUrl(httpEndpoint);
|
|
8482
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
|
+
}
|
|
8483
8577
|
/**
|
|
8484
8578
|
* Payment-aware HTTP fetch over TOON (issue #50). A `fetch()`-like method that
|
|
8485
8579
|
* makes paying for an HTTP resource transparent:
|
|
@@ -8586,7 +8680,7 @@ var ToonClient = class {
|
|
|
8586
8680
|
"INVALID_STATE"
|
|
8587
8681
|
);
|
|
8588
8682
|
}
|
|
8589
|
-
const transport = this.getClaimTransport();
|
|
8683
|
+
const transport = this.getClaimTransport(params.destination);
|
|
8590
8684
|
const claimMessage = await this.resolveClaimForDestination(
|
|
8591
8685
|
params.destination,
|
|
8592
8686
|
params.amount,
|
|
@@ -8661,10 +8755,21 @@ var ToonClient = class {
|
|
|
8661
8755
|
* proxy) does NOT implement `sendIlpPacketWithClaim`; in that case there is no
|
|
8662
8756
|
* paid-write transport and we throw a clear, actionable error.
|
|
8663
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
|
+
*
|
|
8664
8767
|
* @throws {ToonClientError} NO_ILP_TRANSPORT when no active transport can send
|
|
8665
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.
|
|
8666
8771
|
*/
|
|
8667
|
-
getClaimTransport() {
|
|
8772
|
+
getClaimTransport(destination) {
|
|
8668
8773
|
const state = this.state;
|
|
8669
8774
|
if (!state) {
|
|
8670
8775
|
throw new ToonClientError(
|
|
@@ -8672,11 +8777,16 @@ var ToonClient = class {
|
|
|
8672
8777
|
"INVALID_STATE"
|
|
8673
8778
|
);
|
|
8674
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
|
+
}
|
|
8675
8787
|
const candidates = this.config.preferBtpForPaidWrites ? [state.btpClient, state.runtimeClient] : [state.runtimeClient, state.btpClient];
|
|
8676
8788
|
for (const candidate of candidates) {
|
|
8677
|
-
if (candidate
|
|
8678
|
-
return candidate;
|
|
8679
|
-
}
|
|
8789
|
+
if (sendsClaims(candidate)) return candidate;
|
|
8680
8790
|
}
|
|
8681
8791
|
throw new ToonClientError(
|
|
8682
8792
|
"No ILP transport for paid writes. Configure `proxyUrl`/`connectorHttpEndpoint` (route through the connector proxy over ILP-over-HTTP) or `btpUrl` (BTP socket).",
|
|
@@ -9651,7 +9761,7 @@ var ToonClient = class {
|
|
|
9651
9761
|
"MISSING_CLAIM"
|
|
9652
9762
|
);
|
|
9653
9763
|
}
|
|
9654
|
-
const transport = this.getClaimTransport();
|
|
9764
|
+
const transport = this.getClaimTransport(params.destination);
|
|
9655
9765
|
const claimMessage = this.buildClaimMessageForProof(params.claim);
|
|
9656
9766
|
return transport.sendIlpPacketWithClaim(
|
|
9657
9767
|
ilpParams,
|
|
@@ -9672,6 +9782,7 @@ var ToonClient = class {
|
|
|
9672
9782
|
throw new ToonClientError("Client not started", "INVALID_STATE");
|
|
9673
9783
|
}
|
|
9674
9784
|
try {
|
|
9785
|
+
this.state.discoverySubscription.close();
|
|
9675
9786
|
if (this.state.btpClient) {
|
|
9676
9787
|
await this.state.btpClient.disconnect();
|
|
9677
9788
|
}
|