@toon-protocol/client 0.29.7 → 0.29.8
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 +117 -18
- package/dist/index.js +237 -62
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -71,6 +71,12 @@ var Http402RequiresBtpError = class extends ToonClientError {
|
|
|
71
71
|
this.name = "Http402RequiresBtpError";
|
|
72
72
|
}
|
|
73
73
|
};
|
|
74
|
+
var Http401RequiresBtpError = class extends ToonClientError {
|
|
75
|
+
constructor(message2, cause) {
|
|
76
|
+
super(message2, "HTTP_401_REQUIRES_BTP", cause);
|
|
77
|
+
this.name = "Http401RequiresBtpError";
|
|
78
|
+
}
|
|
79
|
+
};
|
|
74
80
|
var ValidationError = class extends ToonClientError {
|
|
75
81
|
constructor(message2, cause) {
|
|
76
82
|
super(message2, "VALIDATION_ERROR", cause);
|
|
@@ -3211,11 +3217,37 @@ var HttpIlpClient = class {
|
|
|
3211
3217
|
return client;
|
|
3212
3218
|
}
|
|
3213
3219
|
// ─── Private ──────────────────────────────────────────────────────────────
|
|
3220
|
+
/**
|
|
3221
|
+
* The identity headers to present, if any.
|
|
3222
|
+
*
|
|
3223
|
+
* A peer id and its bearer secret are ONE credential, so both are sent or
|
|
3224
|
+
* neither is (issue #565). Presenting a `peerId` this client cannot back
|
|
3225
|
+
* with a secret is strictly WORSE than presenting nothing: the connector's
|
|
3226
|
+
* client edge resolves a PRESENTED identity first and answers `401` before
|
|
3227
|
+
* it ever looks at the route
|
|
3228
|
+
* (connector `crates/connector-client-edge/src/lib.rs`, whose own comment
|
|
3229
|
+
* reads "A request presenting no `ILP-Peer-Id` is unaffected here — it is
|
|
3230
|
+
* anonymous, resolved once the claim (if any) has been admitted"), whereas
|
|
3231
|
+
* anonymous + a valid payment-channel claim is the supported permissionless
|
|
3232
|
+
* path. The live devnet daemon shipped `ILP-Peer-Id: g.toon.client` with an
|
|
3233
|
+
* EMPTY token — an id no connector has a registration for — so every default
|
|
3234
|
+
* paid write over `POST /ilp` 401'd before the claim was read at all.
|
|
3235
|
+
*
|
|
3236
|
+
* An empty-string token is not a credential; `''` is falsy here on purpose.
|
|
3237
|
+
*
|
|
3238
|
+
* This REMOVES the 401 rather than recovering from it, so it complements
|
|
3239
|
+
* #569's {@link Http401RequiresBtpError} fallback instead of replacing it:
|
|
3240
|
+
* that fallback still catches a 401 with any other cause, but it costs a
|
|
3241
|
+
* failed round trip per write and can only rescue a client that has a BTP
|
|
3242
|
+
* uplink — a proxy-only client had no route to a paid write at all, even
|
|
3243
|
+
* though anonymous + claim is exactly the path that would have worked.
|
|
3244
|
+
*/
|
|
3214
3245
|
authHeaders() {
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3246
|
+
if (!this.peerId || !this.authToken) return {};
|
|
3247
|
+
return {
|
|
3248
|
+
[ILP_PEER_ID_HEADER]: this.peerId,
|
|
3249
|
+
Authorization: `Bearer ${this.authToken}`
|
|
3250
|
+
};
|
|
3219
3251
|
}
|
|
3220
3252
|
/**
|
|
3221
3253
|
* Single attempt: serialize the PREPARE, POST it, and map the response.
|
|
@@ -3289,6 +3321,11 @@ var HttpIlpClient = class {
|
|
|
3289
3321
|
);
|
|
3290
3322
|
}
|
|
3291
3323
|
}
|
|
3324
|
+
if (response.status === 401) {
|
|
3325
|
+
throw new Http401RequiresBtpError(
|
|
3326
|
+
`The connector at ${this.httpEndpoint} refused this paid write with 401${detail}.`
|
|
3327
|
+
);
|
|
3328
|
+
}
|
|
3292
3329
|
if (response.status >= 500) {
|
|
3293
3330
|
throw new ConnectorError(
|
|
3294
3331
|
`Connector transport error (${response.status} ${response.statusText})${detail}`
|
|
@@ -3300,10 +3337,10 @@ var HttpIlpClient = class {
|
|
|
3300
3337
|
}
|
|
3301
3338
|
mapTransportError(error, requestTimeout) {
|
|
3302
3339
|
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).
|
|
3304
|
-
//
|
|
3305
|
-
//
|
|
3306
|
-
error instanceof Http402RequiresBtpError) {
|
|
3340
|
+
// unwrapped so a caller can retry over BTP (toon-client#561, #565).
|
|
3341
|
+
// Neither is a `ConnectorError` (a distinct code/type is the whole
|
|
3342
|
+
// point of those classes), so each needs its own check here.
|
|
3343
|
+
error instanceof Http402RequiresBtpError || error instanceof Http401RequiresBtpError) {
|
|
3307
3344
|
return error;
|
|
3308
3345
|
}
|
|
3309
3346
|
if (error instanceof Error && error.name === "AbortError") {
|
|
@@ -4886,7 +4923,7 @@ var OnChainChannelClient = class {
|
|
|
4886
4923
|
chain
|
|
4887
4924
|
});
|
|
4888
4925
|
}
|
|
4889
|
-
return { channelId, status: "opening" };
|
|
4926
|
+
return { channelId, status: "opening", depositTotal: deposit };
|
|
4890
4927
|
}
|
|
4891
4928
|
/**
|
|
4892
4929
|
* Poll the TokenNetwork until it reports the just-opened channel (non-zero
|
|
@@ -5177,6 +5214,12 @@ async function initializeHttpMode(config, hooks = {}) {
|
|
|
5177
5214
|
if (transportChoice && (transportChoice.kind === "http" || transportChoice.kind === "http-upgradable")) {
|
|
5178
5215
|
httpIlpClient = new HttpIlpClient({
|
|
5179
5216
|
httpEndpoint: transportChoice.httpEndpoint,
|
|
5217
|
+
// Carried for the BTP-upgrade auth frame; it only reaches the `POST
|
|
5218
|
+
// /ilp` wire as `ILP-Peer-Id` when a non-empty `btpAuthToken` backs it
|
|
5219
|
+
// (issue #565 — see `HttpIlpClient.authHeaders`). The BTP greeting is a
|
|
5220
|
+
// different contract: the connector accepts an unregistered id there and
|
|
5221
|
+
// resolves the payer from the claim, which is why `btpPeerId` stays the
|
|
5222
|
+
// per-client id `toon-client#503` made it.
|
|
5180
5223
|
peerId: btpPeerId,
|
|
5181
5224
|
...config.btpAuthToken !== void 0 ? { authToken: config.btpAuthToken } : {},
|
|
5182
5225
|
timeout: config.queryTimeout,
|
|
@@ -5867,8 +5910,11 @@ var ChannelManager = class _ChannelManager {
|
|
|
5867
5910
|
};
|
|
5868
5911
|
this.trackChannel(result.channelId, {
|
|
5869
5912
|
...context,
|
|
5870
|
-
// On-chain depositTotal
|
|
5871
|
-
//
|
|
5913
|
+
// On-chain depositTotal, reported by every opener since issue #565
|
|
5914
|
+
// (it was Mina-only before, so an EVM channel's collateral read back
|
|
5915
|
+
// as 0). The Mina signer needs it to bind balanceB = depositTotal −
|
|
5916
|
+
// balanceA (connector#133); every chain needs it for the spendable
|
|
5917
|
+
// balance `depositTotal - cumulativeAmount`.
|
|
5872
5918
|
depositTotal: result.depositTotal
|
|
5873
5919
|
});
|
|
5874
5920
|
this.peerChannels.set(key2, result.channelId);
|
|
@@ -5902,9 +5948,16 @@ var ChannelManager = class _ChannelManager {
|
|
|
5902
5948
|
tokenAddress: negotiation.tokenAddress,
|
|
5903
5949
|
recipient: negotiation.settlementAddress
|
|
5904
5950
|
};
|
|
5905
|
-
this.
|
|
5951
|
+
const bound = this.store?.loadBinding?.(key2);
|
|
5952
|
+
const deposited = bound?.channelId === channelId ? bound.depositTotal : void 0;
|
|
5953
|
+
this.trackChannel(channelId, {
|
|
5954
|
+
...context,
|
|
5955
|
+
...deposited !== void 0 ? { depositTotal: deposited } : {}
|
|
5956
|
+
});
|
|
5906
5957
|
this.peerChannels.set(key2, channelId);
|
|
5907
|
-
this.bindChannel(key2, channelId, context, {
|
|
5958
|
+
this.bindChannel(key2, channelId, context, {
|
|
5959
|
+
...deposited !== void 0 ? { depositTotal: deposited } : {}
|
|
5960
|
+
});
|
|
5908
5961
|
this.adoptOnChainContext(channelId, negotiation);
|
|
5909
5962
|
}
|
|
5910
5963
|
/**
|
|
@@ -6148,6 +6201,10 @@ var ChannelManager = class _ChannelManager {
|
|
|
6148
6201
|
* Update the tracked on-chain deposit total after a successful deposit, so the
|
|
6149
6202
|
* available balance (`depositTotal - cumulativeAmount`) reflects the new
|
|
6150
6203
|
* collateral on the next read.
|
|
6204
|
+
*
|
|
6205
|
+
* Also written through to the peer BINDING (issue #565) — the deposit is an
|
|
6206
|
+
* on-chain fact, so a restart must resume with the collateral that is
|
|
6207
|
+
* actually locked, not the amount recorded when the channel was first opened.
|
|
6151
6208
|
*/
|
|
6152
6209
|
setDepositTotal(channelId, total) {
|
|
6153
6210
|
const tracking = this.channels.get(channelId);
|
|
@@ -6155,6 +6212,23 @@ var ChannelManager = class _ChannelManager {
|
|
|
6155
6212
|
throw new Error(`Channel "${channelId}" is not being tracked.`);
|
|
6156
6213
|
}
|
|
6157
6214
|
tracking.depositTotal = total;
|
|
6215
|
+
this.persistDepositTotal(channelId, total);
|
|
6216
|
+
}
|
|
6217
|
+
/**
|
|
6218
|
+
* Write `total` into whichever peer binding names `channelId`. No-op without
|
|
6219
|
+
* a store that persists bindings, or when no binding names this channel (an
|
|
6220
|
+
* untracked/foreign channel has no collateral of ours to record).
|
|
6221
|
+
*/
|
|
6222
|
+
persistDepositTotal(channelId, total) {
|
|
6223
|
+
const store = this.store;
|
|
6224
|
+
if (!store?.saveBinding || !store.loadBinding) return;
|
|
6225
|
+
for (const [key2, bound] of this.peerChannels) {
|
|
6226
|
+
if (bound !== channelId) continue;
|
|
6227
|
+
const binding = store.loadBinding(key2);
|
|
6228
|
+
if (!binding) continue;
|
|
6229
|
+
store.saveBinding(key2, { ...binding, depositTotal: total });
|
|
6230
|
+
return;
|
|
6231
|
+
}
|
|
6158
6232
|
}
|
|
6159
6233
|
/** Persist a channel's full nonce/amount + withdraw-timer state to the store. */
|
|
6160
6234
|
persist(channelId) {
|
|
@@ -7916,6 +7990,18 @@ function claimsTermination(peerInfo, address, destination) {
|
|
|
7916
7990
|
function sendsClaims(candidate) {
|
|
7917
7991
|
return typeof candidate?.sendIlpPacketWithClaim === "function";
|
|
7918
7992
|
}
|
|
7993
|
+
function btpFallbackSignal(error) {
|
|
7994
|
+
if (error instanceof Http402RequiresBtpError) {
|
|
7995
|
+
return { error, reason: '402 response declared requiredTransport: "btp"' };
|
|
7996
|
+
}
|
|
7997
|
+
if (error instanceof Http401RequiresBtpError) {
|
|
7998
|
+
return {
|
|
7999
|
+
error,
|
|
8000
|
+
reason: "401 response rejected the discovered/unconfigured peer identity"
|
|
8001
|
+
};
|
|
8002
|
+
}
|
|
8003
|
+
return void 0;
|
|
8004
|
+
}
|
|
7919
8005
|
function outranks(claim, best) {
|
|
7920
8006
|
if (claim.segments !== best.segments) return claim.segments > best.segments;
|
|
7921
8007
|
return claim.isPrimary && !best.isPrimary;
|
|
@@ -7925,7 +8011,7 @@ function resolveTerminatorHttpEndpoint(destination, peers) {
|
|
|
7925
8011
|
}
|
|
7926
8012
|
function findBestTerminatorClaim(destination, peers) {
|
|
7927
8013
|
let best;
|
|
7928
|
-
for (const { peerInfo, pubkey } of peers) {
|
|
8014
|
+
for (const { peerInfo, pubkey, peerId } of peers) {
|
|
7929
8015
|
const httpEndpoint = peerInfo.httpEndpoint;
|
|
7930
8016
|
if (!httpEndpoint) continue;
|
|
7931
8017
|
const addresses = peerInfo.ilpAddresses ?? [peerInfo.ilpAddress];
|
|
@@ -7935,7 +8021,9 @@ function findBestTerminatorClaim(destination, peers) {
|
|
|
7935
8021
|
endpoint: httpEndpoint,
|
|
7936
8022
|
segments: address.split(".").length,
|
|
7937
8023
|
isPrimary: index === 0,
|
|
7938
|
-
pubkey
|
|
8024
|
+
pubkey,
|
|
8025
|
+
peerId,
|
|
8026
|
+
peerInfo
|
|
7939
8027
|
};
|
|
7940
8028
|
if (!best || outranks(claim, best)) best = claim;
|
|
7941
8029
|
}
|
|
@@ -8254,29 +8342,12 @@ var ToonClient = class {
|
|
|
8254
8342
|
tokenNetwork: r.tokenNetwork
|
|
8255
8343
|
});
|
|
8256
8344
|
} else if (result.registeredPeerId && !this.peerNegotiations.has(result.registeredPeerId)) {
|
|
8257
|
-
const
|
|
8258
|
-
|
|
8259
|
-
const ourChains = this.config.supportedChains ?? [];
|
|
8260
|
-
const matchedChain = this.matchNegotiatedChain(
|
|
8261
|
-
ourChains,
|
|
8262
|
-
peerChains,
|
|
8345
|
+
const announced = this.negotiationFromAnnounce(
|
|
8346
|
+
result.peerInfo,
|
|
8263
8347
|
result.registeredPeerId
|
|
8264
8348
|
);
|
|
8265
|
-
|
|
8266
|
-
|
|
8267
|
-
const chainId = parts.length >= 3 ? parseInt(parts[2] ?? "0", 10) : parts.length >= 2 ? parseInt(parts[1] ?? "0", 10) : 0;
|
|
8268
|
-
if (peerAddr) {
|
|
8269
|
-
this.peerNegotiations.set(result.registeredPeerId, {
|
|
8270
|
-
chain: matchedChain,
|
|
8271
|
-
chainType: parts[0] ?? "evm",
|
|
8272
|
-
chainId: isNaN(chainId) ? 0 : chainId,
|
|
8273
|
-
settlementAddress: peerAddr,
|
|
8274
|
-
tokenAddress: peerInfo.preferredTokens?.[matchedChain] ?? lookupByCanonicalChain(
|
|
8275
|
-
this.config.preferredTokens,
|
|
8276
|
-
matchedChain
|
|
8277
|
-
),
|
|
8278
|
-
tokenNetwork: peerInfo.tokenNetworks?.[matchedChain] ?? lookupByCanonicalChain(this.config.tokenNetworks, matchedChain)
|
|
8279
|
-
});
|
|
8349
|
+
if (announced) {
|
|
8350
|
+
this.peerNegotiations.set(result.registeredPeerId, announced);
|
|
8280
8351
|
}
|
|
8281
8352
|
}
|
|
8282
8353
|
if (this.channelManager && result.channelId && !this.channelManager.isTracking(result.channelId)) {
|
|
@@ -8844,8 +8915,9 @@ var ToonClient = class {
|
|
|
8844
8915
|
* NO_ILP_TRANSPORT/BTP_REQUIRED failure modes of resolving a transport at
|
|
8845
8916
|
* all still surface before any claim is resolved, matching every caller's
|
|
8846
8917
|
* existing behavior), retrying once over the BTP uplink when it comes back
|
|
8847
|
-
* with {@link
|
|
8918
|
+
* with a transport signal that says so — {@link btpFallbackSignal}.
|
|
8848
8919
|
*
|
|
8920
|
+
* {@link Http402RequiresBtpError} (issue #561) is the first such signal.
|
|
8849
8921
|
* `getClaimTransport`'s own `requiredTransport` check ({@link
|
|
8850
8922
|
* terminatorRequiresBtp}) reads the peer's kind:10032 announce — but the
|
|
8851
8923
|
* live devnet relay's announce never carries that field, only its `402`
|
|
@@ -8854,31 +8926,39 @@ var ToonClient = class {
|
|
|
8854
8926
|
* is the fallback that actually avoids the repeat-402 loop, discovered
|
|
8855
8927
|
* live rather than assumed from the announce.
|
|
8856
8928
|
*
|
|
8929
|
+
* `Http401RequiresBtpError` (issue #565) is the same shape of problem from
|
|
8930
|
+
* a different connector generation: the rust edge live on the two-box
|
|
8931
|
+
* devnet answers a discovered/unconfigured peer identity with a bare `401`
|
|
8932
|
+
* rather than the 402 x402 greeting, so it gets the identical retry — both
|
|
8933
|
+
* error types are transport-signal-only and neither implies the write
|
|
8934
|
+
* itself was invalid.
|
|
8935
|
+
*
|
|
8857
8936
|
* A retry is attempted only when the failing transport was not already the
|
|
8858
|
-
* BTP one (
|
|
8859
|
-
*
|
|
8860
|
-
*
|
|
8861
|
-
* `
|
|
8862
|
-
*
|
|
8863
|
-
* caught it.
|
|
8937
|
+
* BTP one (neither error can come from the BTP transport in the first
|
|
8938
|
+
* place — BTP has no HTTP status codes) and a BTP uplink is actually
|
|
8939
|
+
* configured; otherwise this throws the same `BTP_REQUIRED` error
|
|
8940
|
+
* `getClaimTransport` throws for the announce-based case, so callers see
|
|
8941
|
+
* one consistent code regardless of which guard caught it.
|
|
8864
8942
|
*
|
|
8865
|
-
* @throws {ToonClientError} BTP_REQUIRED when the terminator's 402
|
|
8866
|
-
* BTP but this client has no BTP uplink (`btpUrl`)
|
|
8943
|
+
* @throws {ToonClientError} BTP_REQUIRED when the terminator's 401/402
|
|
8944
|
+
* response demands BTP but this client has no BTP uplink (`btpUrl`)
|
|
8945
|
+
* configured.
|
|
8867
8946
|
*/
|
|
8868
8947
|
async sendClaimBearingPacket(destination, transport, params, claim) {
|
|
8869
8948
|
try {
|
|
8870
8949
|
return await transport.sendIlpPacketWithClaim(params, claim);
|
|
8871
8950
|
} catch (error) {
|
|
8872
|
-
|
|
8951
|
+
const signal = btpFallbackSignal(error);
|
|
8952
|
+
if (!signal) throw error;
|
|
8873
8953
|
const uplink = this.state?.btpClient;
|
|
8874
8954
|
const btp = sendsClaims(uplink) ? uplink : void 0;
|
|
8875
8955
|
if (btp && btp !== transport) {
|
|
8876
8956
|
return btp.sendIlpPacketWithClaim(params, claim);
|
|
8877
8957
|
}
|
|
8878
8958
|
throw new ToonClientError(
|
|
8879
|
-
`The connector terminating "${destination}" requires BTP for paid writes (its
|
|
8959
|
+
`The connector terminating "${destination}" requires BTP for paid writes (its ${signal.reason}), but this client has no BTP uplink (\`btpUrl\`) configured.`,
|
|
8880
8960
|
"BTP_REQUIRED",
|
|
8881
|
-
error
|
|
8961
|
+
signal.error
|
|
8882
8962
|
);
|
|
8883
8963
|
}
|
|
8884
8964
|
}
|
|
@@ -8891,7 +8971,7 @@ var ToonClient = class {
|
|
|
8891
8971
|
}
|
|
8892
8972
|
if (this.channelManager) {
|
|
8893
8973
|
const peerId = this.peerIdForClaim(destination);
|
|
8894
|
-
const negotiation =
|
|
8974
|
+
const negotiation = await this.negotiationFor(destination, peerId);
|
|
8895
8975
|
if (!negotiation) {
|
|
8896
8976
|
throw new ToonClientError(
|
|
8897
8977
|
`No negotiation metadata for peer "${peerId}" \u2014 was bootstrap completed? (and the route's x402 greeting carried no settlement facts to bootstrap from)`,
|
|
@@ -8946,6 +9026,13 @@ var ToonClient = class {
|
|
|
8946
9026
|
* can force the open. Idempotent — returns the existing channel ID for the
|
|
8947
9027
|
* peer if one is already open.
|
|
8948
9028
|
*
|
|
9029
|
+
* The counterparty is driven by `destination` (issue #565): the channel is
|
|
9030
|
+
* opened against the settlement address of whoever TERMINATES it — an
|
|
9031
|
+
* existing negotiation for that peer, else the terminating kind:10032
|
|
9032
|
+
* announce's own `settlementAddresses`, else the route's x402 greeting. Two
|
|
9033
|
+
* destinations that terminate at different nodes therefore get two channels,
|
|
9034
|
+
* each with the right counterparty.
|
|
9035
|
+
*
|
|
8949
9036
|
* @param destination - Optional ILP destination address. Defaults to
|
|
8950
9037
|
* `config.destinationAddress`.
|
|
8951
9038
|
* @returns The channel ID of the (now) open channel.
|
|
@@ -8973,7 +9060,7 @@ var ToonClient = class {
|
|
|
8973
9060
|
);
|
|
8974
9061
|
}
|
|
8975
9062
|
const peerId = this.peerIdForClaim(dest);
|
|
8976
|
-
const negotiation =
|
|
9063
|
+
const negotiation = await this.negotiationFor(dest, peerId);
|
|
8977
9064
|
if (!negotiation) {
|
|
8978
9065
|
throw new ToonClientError(
|
|
8979
9066
|
`No negotiation metadata for peer "${peerId}" \u2014 was bootstrap completed? (and the route's x402 greeting carried no settlement facts to bootstrap from)`,
|
|
@@ -9007,7 +9094,7 @@ var ToonClient = class {
|
|
|
9007
9094
|
);
|
|
9008
9095
|
}
|
|
9009
9096
|
const peerId = this.peerIdForClaim(destination);
|
|
9010
|
-
const negotiation =
|
|
9097
|
+
const negotiation = await this.negotiationFor(destination, peerId);
|
|
9011
9098
|
if (!negotiation) {
|
|
9012
9099
|
throw new ToonClientError(
|
|
9013
9100
|
`No negotiation metadata for peer "${peerId}" \u2014 cannot adopt channel "${channelId}" without knowing its chain and token network.`,
|
|
@@ -9551,17 +9638,19 @@ var ToonClient = class {
|
|
|
9551
9638
|
return sendTransfer(config, params);
|
|
9552
9639
|
}
|
|
9553
9640
|
/**
|
|
9554
|
-
*
|
|
9555
|
-
*
|
|
9556
|
-
*
|
|
9557
|
-
|
|
9558
|
-
|
|
9559
|
-
* The peer id a claim for `destination` accounts under. Normally
|
|
9560
|
-
* `resolvePeerId` — but where that throws `PEER_NOT_FOUND` (an empty
|
|
9561
|
-
* negotiation map: nothing announced, nothing registered), the
|
|
9641
|
+
* The peer id a claim for `destination` accounts under — and therefore WHICH
|
|
9642
|
+
* counterparty's channel that claim is drawn on. Normally `resolvePeerId`,
|
|
9643
|
+
* which resolves by identity (an existing negotiation key, or the announce
|
|
9644
|
+
* that terminates `destination`) — but where that throws `PEER_NOT_FOUND`
|
|
9645
|
+
* (nothing announced, nothing registered, nothing unambiguous to guess), the
|
|
9562
9646
|
* destination itself is the key, so the announce-less greeting bootstrap
|
|
9563
9647
|
* (connector #617) has a stable identity to remember its synthesized
|
|
9564
9648
|
* negotiation under.
|
|
9649
|
+
*
|
|
9650
|
+
* Never a positional guess across several peers (issue #565): keying a
|
|
9651
|
+
* second destination onto the first peer's negotiation opens the channel
|
|
9652
|
+
* against the WRONG settlement address, and the real terminator then rejects
|
|
9653
|
+
* every claim (F01 — "names a channel this connector has no record of").
|
|
9565
9654
|
*/
|
|
9566
9655
|
peerIdForClaim(destination) {
|
|
9567
9656
|
try {
|
|
@@ -9738,14 +9827,100 @@ var ToonClient = class {
|
|
|
9738
9827
|
return peerId;
|
|
9739
9828
|
}
|
|
9740
9829
|
}
|
|
9741
|
-
const
|
|
9742
|
-
if (
|
|
9743
|
-
|
|
9830
|
+
const claim = this.terminatorClaimFor(destination);
|
|
9831
|
+
if (claim) return claim.peerId;
|
|
9832
|
+
if (this.peerNegotiations.size === 1) {
|
|
9833
|
+
const only = this.peerNegotiations.keys().next();
|
|
9834
|
+
if (!only.done && only.value) return only.value;
|
|
9835
|
+
}
|
|
9744
9836
|
throw new ToonClientError(
|
|
9745
9837
|
`Cannot resolve peer for destination: ${destination}`,
|
|
9746
9838
|
"PEER_NOT_FOUND"
|
|
9747
9839
|
);
|
|
9748
9840
|
}
|
|
9841
|
+
/**
|
|
9842
|
+
* The winning kind:10032 claim on `destination` from this client's discovery
|
|
9843
|
+
* tracker, or `undefined` when there is no tracker yet or no announce claims
|
|
9844
|
+
* it. Probed rather than assumed, exactly as {@link terminatorRequiresBtp}
|
|
9845
|
+
* does — a half-built state (a stub, a client mid-`start()`) simply has
|
|
9846
|
+
* nothing to resolve against, and a missing announce is a normal state, not
|
|
9847
|
+
* an error.
|
|
9848
|
+
*/
|
|
9849
|
+
terminatorClaimFor(destination) {
|
|
9850
|
+
const tracker = this.state?.discoveryTracker;
|
|
9851
|
+
if (typeof tracker?.getAllDiscoveredPeers !== "function") return void 0;
|
|
9852
|
+
return findBestTerminatorClaim(
|
|
9853
|
+
destination,
|
|
9854
|
+
tracker.getAllDiscoveredPeers()
|
|
9855
|
+
);
|
|
9856
|
+
}
|
|
9857
|
+
/**
|
|
9858
|
+
* The negotiation to settle `destination` with, in order of authority
|
|
9859
|
+
* (issue #565):
|
|
9860
|
+
*
|
|
9861
|
+
* 1. an ALREADY-negotiated peer (bootstrap, or a host that injected one);
|
|
9862
|
+
* 2. the terminating announce's OWN settlement facts — the counterparty
|
|
9863
|
+
* states its `settlementAddresses`/`tokenNetworks` in the same
|
|
9864
|
+
* kind:10032 event that claims the destination, so this is the
|
|
9865
|
+
* counterparty naming itself rather than us inferring one;
|
|
9866
|
+
* 3. the route's x402 greeting ({@link negotiateFromGreeting}, connector
|
|
9867
|
+
* #617) for an announce-less route.
|
|
9868
|
+
*
|
|
9869
|
+
* Step 2 is what makes a SECOND destination openable at all: bootstrap only
|
|
9870
|
+
* negotiates with `knownPeers`, so a client seeded with the relay had no
|
|
9871
|
+
* negotiation for the store box and used to fall through to the relay's.
|
|
9872
|
+
*/
|
|
9873
|
+
async negotiationFor(destination, peerId) {
|
|
9874
|
+
const known = this.peerNegotiations.get(peerId);
|
|
9875
|
+
if (known) return known;
|
|
9876
|
+
const claim = this.terminatorClaimFor(destination);
|
|
9877
|
+
if (claim && claim.peerId === peerId) {
|
|
9878
|
+
let announced;
|
|
9879
|
+
try {
|
|
9880
|
+
announced = this.negotiationFromAnnounce(claim.peerInfo, peerId);
|
|
9881
|
+
} catch {
|
|
9882
|
+
announced = void 0;
|
|
9883
|
+
}
|
|
9884
|
+
if (announced) {
|
|
9885
|
+
this.peerNegotiations.set(peerId, announced);
|
|
9886
|
+
return announced;
|
|
9887
|
+
}
|
|
9888
|
+
}
|
|
9889
|
+
return this.negotiateFromGreeting(destination, peerId);
|
|
9890
|
+
}
|
|
9891
|
+
/**
|
|
9892
|
+
* The {@link PeerNegotiation} a peer's own kind:10032 announce supports: the
|
|
9893
|
+
* chain both sides support, plus the settlement address / token / token
|
|
9894
|
+
* network THAT PEER advertises for it (falling back to this client's
|
|
9895
|
+
* configured maps only where the announce is silent).
|
|
9896
|
+
*
|
|
9897
|
+
* `undefined` when the announce names no settlement address for the matched
|
|
9898
|
+
* chain — there is nothing to open a channel against, and guessing one is how
|
|
9899
|
+
* a claim ends up unverifiable at the counterparty.
|
|
9900
|
+
*
|
|
9901
|
+
* @throws {ToonClientError} CHAIN_NOT_SUPPORTED when no mutually supported
|
|
9902
|
+
* chain exists (from {@link matchNegotiatedChain}) — bootstrap surfaces
|
|
9903
|
+
* this; the lazy per-destination path catches it.
|
|
9904
|
+
*/
|
|
9905
|
+
negotiationFromAnnounce(peerInfo, peerId) {
|
|
9906
|
+
const matchedChain = this.matchNegotiatedChain(
|
|
9907
|
+
this.config.supportedChains ?? [],
|
|
9908
|
+
peerInfo.supportedChains ?? [],
|
|
9909
|
+
peerId
|
|
9910
|
+
);
|
|
9911
|
+
const peerAddr = peerInfo.settlementAddresses?.[matchedChain];
|
|
9912
|
+
if (!peerAddr) return void 0;
|
|
9913
|
+
const parts = matchedChain.split(":");
|
|
9914
|
+
const chainId = parts.length >= 3 ? parseInt(parts[2] ?? "0", 10) : parts.length >= 2 ? parseInt(parts[1] ?? "0", 10) : 0;
|
|
9915
|
+
return {
|
|
9916
|
+
chain: matchedChain,
|
|
9917
|
+
chainType: parts[0] ?? "evm",
|
|
9918
|
+
chainId: isNaN(chainId) ? 0 : chainId,
|
|
9919
|
+
settlementAddress: peerAddr,
|
|
9920
|
+
tokenAddress: peerInfo.preferredTokens?.[matchedChain] ?? lookupByCanonicalChain(this.config.preferredTokens, matchedChain),
|
|
9921
|
+
tokenNetwork: peerInfo.tokenNetworks?.[matchedChain] ?? lookupByCanonicalChain(this.config.tokenNetworks, matchedChain)
|
|
9922
|
+
};
|
|
9923
|
+
}
|
|
9749
9924
|
/**
|
|
9750
9925
|
* Extracts chain context (chainId + tokenNetworkAddress) from a chain key like 'evm:base:421614'.
|
|
9751
9926
|
*/
|