@gethashd/bytecave-browser 1.0.58 → 1.0.60
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/{chunk-NLZDUMYM.js → chunk-LMK44RJF.js} +34 -396
- package/dist/client.d.ts +0 -17
- package/dist/index.cjs +34 -396
- package/dist/index.js +1 -1
- package/dist/react/index.cjs +3 -22
- package/dist/react/index.js +1 -1
- package/dist/types.d.ts +0 -4
- package/package.json +1 -1
- package/src/client.ts +35 -301
- package/src/p2p-protocols.ts +6 -27
- package/src/types.ts +0 -4
- package/dist/storage-webtransport.d.ts +0 -47
- package/src/storage-webtransport.ts +0 -201
|
@@ -5726,32 +5726,14 @@ var P2PProtocolClient = class {
|
|
|
5726
5726
|
} catch (e) {
|
|
5727
5727
|
console.warn("[ByteCave P2P] Could not get peer info from peerStore:", e);
|
|
5728
5728
|
}
|
|
5729
|
-
console.log("[ByteCave P2P] Step 2:
|
|
5729
|
+
console.log("[ByteCave P2P] Step 2: Using existing connection...");
|
|
5730
5730
|
if (existingConns.length === 0) {
|
|
5731
5731
|
throw new Error("No connection to peer - cannot open protocol stream");
|
|
5732
5732
|
}
|
|
5733
|
-
|
|
5733
|
+
const connection = existingConns[0];
|
|
5734
5734
|
const isRelayCircuit = connection.remoteAddr.toString().includes("/p2p-circuit");
|
|
5735
|
-
console.log("[ByteCave P2P] Connection type:", isRelayCircuit ? "relay circuit
|
|
5735
|
+
console.log("[ByteCave P2P] Connection type:", isRelayCircuit ? "relay circuit" : "direct");
|
|
5736
5736
|
console.log("[ByteCave P2P] Connection address:", connection.remoteAddr.toString());
|
|
5737
|
-
if (isRelayCircuit) {
|
|
5738
|
-
console.log("[ByteCave P2P] Waiting for DCUtR to upgrade relay circuit to direct WebRTC...");
|
|
5739
|
-
const upgradeStart = Date.now();
|
|
5740
|
-
const hasDirectConnection = await this.waitForDirectConnection(peerId, 5e3);
|
|
5741
|
-
const upgradeDuration = Date.now() - upgradeStart;
|
|
5742
|
-
if (!hasDirectConnection) {
|
|
5743
|
-
console.error("[ByteCave P2P] DCUtR upgrade failed after", upgradeDuration, "ms");
|
|
5744
|
-
throw new Error("Cannot use storage protocol - relay circuit upgrade to WebRTC failed");
|
|
5745
|
-
}
|
|
5746
|
-
console.log("[ByteCave P2P] DCUtR upgrade successful after", upgradeDuration, "ms");
|
|
5747
|
-
const directConns = this.node.getConnections(peerIdObj);
|
|
5748
|
-
const directConn = directConns.find((conn) => !conn.remoteAddr.toString().includes("/p2p-circuit"));
|
|
5749
|
-
if (!directConn) {
|
|
5750
|
-
throw new Error("DCUtR reported success but no direct connection found");
|
|
5751
|
-
}
|
|
5752
|
-
connection = directConn;
|
|
5753
|
-
console.log("[ByteCave P2P] Using direct connection:", connection.remoteAddr.toString());
|
|
5754
|
-
}
|
|
5755
5737
|
console.log("[ByteCave P2P] Step 3: Opening protocol stream...");
|
|
5756
5738
|
const streamStart = Date.now();
|
|
5757
5739
|
let stream;
|
|
@@ -6057,7 +6039,6 @@ var p2pProtocolClient = new P2PProtocolClient();
|
|
|
6057
6039
|
import { createLibp2p } from "libp2p";
|
|
6058
6040
|
import { webRTC } from "@libp2p/webrtc";
|
|
6059
6041
|
import { webSockets } from "@libp2p/websockets";
|
|
6060
|
-
import { webTransport } from "@libp2p/webtransport";
|
|
6061
6042
|
import { noise } from "@chainsafe/libp2p-noise";
|
|
6062
6043
|
import { yamux } from "@chainsafe/libp2p-yamux";
|
|
6063
6044
|
import { floodsub } from "@libp2p/floodsub";
|
|
@@ -6227,136 +6208,6 @@ var StorageWebSocketClient = class {
|
|
|
6227
6208
|
}
|
|
6228
6209
|
};
|
|
6229
6210
|
|
|
6230
|
-
// src/storage-webtransport.ts
|
|
6231
|
-
var StorageWebTransportClient = class {
|
|
6232
|
-
constructor(nodeMultiaddr) {
|
|
6233
|
-
this.nodeMultiaddr = nodeMultiaddr;
|
|
6234
|
-
}
|
|
6235
|
-
/**
|
|
6236
|
-
* Store data via WebTransport direct to node
|
|
6237
|
-
*/
|
|
6238
|
-
async store(request) {
|
|
6239
|
-
try {
|
|
6240
|
-
if (typeof WebTransport === "undefined") {
|
|
6241
|
-
return {
|
|
6242
|
-
success: false,
|
|
6243
|
-
error: "WebTransport not supported in this browser"
|
|
6244
|
-
};
|
|
6245
|
-
}
|
|
6246
|
-
const url = this.multiaddrToWebTransportUrl(this.nodeMultiaddr);
|
|
6247
|
-
if (!url) {
|
|
6248
|
-
return {
|
|
6249
|
-
success: false,
|
|
6250
|
-
error: "Invalid WebTransport multiaddr"
|
|
6251
|
-
};
|
|
6252
|
-
}
|
|
6253
|
-
console.log("[WebTransport] Connecting to node:", url);
|
|
6254
|
-
const transport = new WebTransport(url);
|
|
6255
|
-
await transport.ready;
|
|
6256
|
-
console.log("[WebTransport] Connected, opening bidirectional stream");
|
|
6257
|
-
const stream = await transport.createBidirectionalStream();
|
|
6258
|
-
const writer = stream.writable.getWriter();
|
|
6259
|
-
const reader = stream.readable.getReader();
|
|
6260
|
-
const requestData = {
|
|
6261
|
-
type: "storage-request",
|
|
6262
|
-
data: Array.from(request.data),
|
|
6263
|
-
// Convert Uint8Array to array for JSON
|
|
6264
|
-
contentType: request.contentType,
|
|
6265
|
-
hashIdToken: request.hashIdToken,
|
|
6266
|
-
authorization: request.authorization
|
|
6267
|
-
};
|
|
6268
|
-
const requestJson = JSON.stringify(requestData);
|
|
6269
|
-
const requestBytes = new TextEncoder().encode(requestJson);
|
|
6270
|
-
await writer.write(requestBytes);
|
|
6271
|
-
await writer.close();
|
|
6272
|
-
console.log("[WebTransport] Request sent, waiting for response");
|
|
6273
|
-
const { value, done } = await reader.read();
|
|
6274
|
-
if (done || !value) {
|
|
6275
|
-
return {
|
|
6276
|
-
success: false,
|
|
6277
|
-
error: "No response from node"
|
|
6278
|
-
};
|
|
6279
|
-
}
|
|
6280
|
-
const responseJson = new TextDecoder().decode(value);
|
|
6281
|
-
const response = JSON.parse(responseJson);
|
|
6282
|
-
console.log("[WebTransport] Response received:", response);
|
|
6283
|
-
await transport.close();
|
|
6284
|
-
return response;
|
|
6285
|
-
} catch (error) {
|
|
6286
|
-
console.error("[WebTransport] Storage failed:", error);
|
|
6287
|
-
return {
|
|
6288
|
-
success: false,
|
|
6289
|
-
error: error.message || "WebTransport storage failed"
|
|
6290
|
-
};
|
|
6291
|
-
}
|
|
6292
|
-
}
|
|
6293
|
-
/**
|
|
6294
|
-
* Convert libp2p multiaddr to WebTransport URL
|
|
6295
|
-
*/
|
|
6296
|
-
multiaddrToWebTransportUrl(multiaddr2) {
|
|
6297
|
-
try {
|
|
6298
|
-
const parts = multiaddr2.split("/").filter((p) => p);
|
|
6299
|
-
let ip = "";
|
|
6300
|
-
let port = "";
|
|
6301
|
-
let hasQuic = false;
|
|
6302
|
-
let hasWebTransport = false;
|
|
6303
|
-
for (let i = 0; i < parts.length; i++) {
|
|
6304
|
-
if (parts[i] === "ip4" && i + 1 < parts.length) {
|
|
6305
|
-
ip = parts[i + 1];
|
|
6306
|
-
} else if (parts[i] === "udp" && i + 1 < parts.length) {
|
|
6307
|
-
port = parts[i + 1];
|
|
6308
|
-
} else if (parts[i] === "quic-v1") {
|
|
6309
|
-
hasQuic = true;
|
|
6310
|
-
} else if (parts[i] === "webtransport") {
|
|
6311
|
-
hasWebTransport = true;
|
|
6312
|
-
}
|
|
6313
|
-
}
|
|
6314
|
-
if (!ip || !port || !hasQuic || !hasWebTransport) {
|
|
6315
|
-
return null;
|
|
6316
|
-
}
|
|
6317
|
-
const host = ip === "127.0.0.1" ? "localhost" : ip;
|
|
6318
|
-
return `https://${host}:${port}/storage`;
|
|
6319
|
-
} catch (error) {
|
|
6320
|
-
console.error("[WebTransport] Failed to parse multiaddr:", error);
|
|
6321
|
-
return null;
|
|
6322
|
-
}
|
|
6323
|
-
}
|
|
6324
|
-
/**
|
|
6325
|
-
* Extract certificate hash from multiaddr
|
|
6326
|
-
* Format: /ip4/127.0.0.1/udp/4001/quic-v1/webtransport/certhash/uEi...
|
|
6327
|
-
*/
|
|
6328
|
-
extractCertHash(multiaddr2) {
|
|
6329
|
-
try {
|
|
6330
|
-
const parts = multiaddr2.split("/").filter((p) => p);
|
|
6331
|
-
const certhashIndex = parts.indexOf("certhash");
|
|
6332
|
-
if (certhashIndex !== -1 && certhashIndex + 1 < parts.length) {
|
|
6333
|
-
return parts[certhashIndex + 1];
|
|
6334
|
-
}
|
|
6335
|
-
return null;
|
|
6336
|
-
} catch (error) {
|
|
6337
|
-
console.error("[WebTransport] Failed to extract cert hash:", error);
|
|
6338
|
-
return null;
|
|
6339
|
-
}
|
|
6340
|
-
}
|
|
6341
|
-
/**
|
|
6342
|
-
* Convert libp2p multihash cert hash to SHA-256 ArrayBuffer for WebTransport
|
|
6343
|
-
* The certhash in multiaddr format is: uEi<hex-hash>
|
|
6344
|
-
* where 'u' = base32 multibase, 'Ei' = multihash prefix for SHA-256
|
|
6345
|
-
*/
|
|
6346
|
-
async certHashToSHA256(multihash) {
|
|
6347
|
-
try {
|
|
6348
|
-
let hexHash = multihash.startsWith("uEi") ? multihash.slice(3) : multihash;
|
|
6349
|
-
const bytes = new Uint8Array(hexHash.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
6350
|
-
console.log("[WebTransport] Decoded certificate hash:", hexHash);
|
|
6351
|
-
console.log("[WebTransport] Hash bytes length:", bytes.length);
|
|
6352
|
-
return bytes.buffer;
|
|
6353
|
-
} catch (error) {
|
|
6354
|
-
console.error("[WebTransport] Failed to convert cert hash:", error);
|
|
6355
|
-
throw new Error("Failed to decode certificate hash");
|
|
6356
|
-
}
|
|
6357
|
-
}
|
|
6358
|
-
};
|
|
6359
|
-
|
|
6360
6211
|
// src/contracts/ContentRegistry.ts
|
|
6361
6212
|
var CONTENT_REGISTRY_ABI = [
|
|
6362
6213
|
"function registerContent(bytes32 cid, address owner, bytes32 appId, uint256 hashIdToken) external",
|
|
@@ -6373,7 +6224,6 @@ var ByteCaveClient = class {
|
|
|
6373
6224
|
this.knownPeers = /* @__PURE__ */ new Map();
|
|
6374
6225
|
this.connectionState = "disconnected";
|
|
6375
6226
|
this.eventListeners = /* @__PURE__ */ new Map();
|
|
6376
|
-
this.PEERS_CACHE_KEY = "bytecave_known_peers";
|
|
6377
6227
|
this.config = {
|
|
6378
6228
|
maxPeers: 10,
|
|
6379
6229
|
connectionTimeout: 3e4,
|
|
@@ -6386,7 +6236,6 @@ var ByteCaveClient = class {
|
|
|
6386
6236
|
if (config.vaultNodeRegistryAddress && config.rpcUrl) {
|
|
6387
6237
|
this.contractDiscovery = new ContractDiscovery(config.vaultNodeRegistryAddress, config.rpcUrl);
|
|
6388
6238
|
}
|
|
6389
|
-
this.loadCachedPeers();
|
|
6390
6239
|
}
|
|
6391
6240
|
/**
|
|
6392
6241
|
* Initialize and start the P2P client
|
|
@@ -6416,7 +6265,6 @@ var ByteCaveClient = class {
|
|
|
6416
6265
|
transports: [
|
|
6417
6266
|
webRTC(),
|
|
6418
6267
|
webSockets(),
|
|
6419
|
-
webTransport(),
|
|
6420
6268
|
circuitRelayTransport()
|
|
6421
6269
|
],
|
|
6422
6270
|
connectionEncrypters: [noise()],
|
|
@@ -6557,19 +6405,14 @@ var ByteCaveClient = class {
|
|
|
6557
6405
|
}
|
|
6558
6406
|
const health = await p2pProtocolClient.getHealthFromPeer(peer.peerId);
|
|
6559
6407
|
if (health) {
|
|
6560
|
-
|
|
6408
|
+
this.knownPeers.set(peer.peerId, {
|
|
6561
6409
|
peerId: peer.peerId,
|
|
6562
6410
|
publicKey: health.publicKey || "",
|
|
6563
6411
|
contentTypes: health.contentTypes || "all",
|
|
6564
|
-
httpEndpoint: health.httpEndpoint || "",
|
|
6565
|
-
multiaddrs: peer.multiaddrs || [],
|
|
6566
|
-
relayAddrs: peer.relayAddrs || [],
|
|
6567
6412
|
connected: true,
|
|
6568
|
-
|
|
6569
|
-
|
|
6570
|
-
|
|
6571
|
-
this.knownPeers.set(peer.peerId, peerInfo);
|
|
6572
|
-
this.saveCachedPeers();
|
|
6413
|
+
nodeId: health.nodeId
|
|
6414
|
+
});
|
|
6415
|
+
console.log("[ByteCave] \u2713 Discovered peer:", health.nodeId || peer.peerId.slice(0, 12));
|
|
6573
6416
|
}
|
|
6574
6417
|
} catch (err) {
|
|
6575
6418
|
console.warn("[ByteCave] Failed to process peer from directory:", peer.peerId.slice(0, 12), err.message);
|
|
@@ -6605,9 +6448,6 @@ var ByteCaveClient = class {
|
|
|
6605
6448
|
connectedPeers: connectedPeers.length,
|
|
6606
6449
|
discoveredPeers: this.knownPeers.size
|
|
6607
6450
|
});
|
|
6608
|
-
setTimeout(() => {
|
|
6609
|
-
this.dialCachedPeers();
|
|
6610
|
-
}, 2e3);
|
|
6611
6451
|
} catch (error) {
|
|
6612
6452
|
this.setConnectionState("error");
|
|
6613
6453
|
console.error("Failed to start ByteCave client:", error);
|
|
@@ -6661,19 +6501,14 @@ var ByteCaveClient = class {
|
|
|
6661
6501
|
try {
|
|
6662
6502
|
const health = await p2pProtocolClient.getHealthFromPeer(peer.peerId);
|
|
6663
6503
|
if (health) {
|
|
6664
|
-
|
|
6504
|
+
this.knownPeers.set(peer.peerId, {
|
|
6665
6505
|
peerId: peer.peerId,
|
|
6666
6506
|
publicKey: health.publicKey || "",
|
|
6667
6507
|
contentTypes: health.contentTypes || "all",
|
|
6668
|
-
httpEndpoint: health.httpEndpoint || "",
|
|
6669
|
-
multiaddrs: peer.multiaddrs || [],
|
|
6670
|
-
relayAddrs: peer.relayAddrs || [],
|
|
6671
6508
|
connected: true,
|
|
6672
|
-
|
|
6673
|
-
|
|
6674
|
-
|
|
6675
|
-
this.knownPeers.set(peer.peerId, peerInfo);
|
|
6676
|
-
this.saveCachedPeers();
|
|
6509
|
+
nodeId: health.nodeId
|
|
6510
|
+
});
|
|
6511
|
+
console.log("[ByteCave] Refresh: \u2713 Updated peer info:", health.nodeId || peer.peerId.slice(0, 12));
|
|
6677
6512
|
}
|
|
6678
6513
|
} catch (err) {
|
|
6679
6514
|
console.warn("[ByteCave] Refresh: Failed to get health from peer:", peer.peerId.slice(0, 12), err.message);
|
|
@@ -6738,81 +6573,15 @@ Nonce: ${nonce}`;
|
|
|
6738
6573
|
console.warn("[ByteCave] Failed to create authorization:", err.message);
|
|
6739
6574
|
}
|
|
6740
6575
|
}
|
|
6741
|
-
if (this.config.relayWsUrl) {
|
|
6742
|
-
|
|
6743
|
-
try {
|
|
6744
|
-
if (!this.storageWsClient) {
|
|
6745
|
-
this.storageWsClient = new StorageWebSocketClient(this.config.relayWsUrl);
|
|
6746
|
-
}
|
|
6747
|
-
const wsAuth = authorization ? {
|
|
6748
|
-
signature: authorization.signature,
|
|
6749
|
-
address: authorization.sender,
|
|
6750
|
-
timestamp: authorization.timestamp,
|
|
6751
|
-
nonce: authorization.nonce,
|
|
6752
|
-
appId: authorization.appId,
|
|
6753
|
-
contentHash: authorization.contentHash
|
|
6754
|
-
} : void 0;
|
|
6755
|
-
const result = await this.storageWsClient.store({
|
|
6756
|
-
data: dataArray,
|
|
6757
|
-
contentType: mimeType || "application/octet-stream",
|
|
6758
|
-
hashIdToken,
|
|
6759
|
-
authorization: wsAuth,
|
|
6760
|
-
timeout: 3e4
|
|
6761
|
-
});
|
|
6762
|
-
if (result.success && result.cid) {
|
|
6763
|
-
console.log("[ByteCave] \u2713 WebSocket storage successful:", result.cid);
|
|
6764
|
-
return {
|
|
6765
|
-
success: true,
|
|
6766
|
-
cid: result.cid,
|
|
6767
|
-
peerId: "relay-ws"
|
|
6768
|
-
};
|
|
6769
|
-
}
|
|
6770
|
-
console.warn("[ByteCave] WebSocket storage failed, trying WebTransport:", result.error);
|
|
6771
|
-
} catch (err) {
|
|
6772
|
-
console.warn("[ByteCave] WebSocket storage exception, trying WebTransport:", err.message);
|
|
6773
|
-
}
|
|
6774
|
-
} else {
|
|
6775
|
-
console.log("[ByteCave] WebSocket relay not configured, trying WebTransport");
|
|
6576
|
+
if (!this.config.relayWsUrl) {
|
|
6577
|
+
return { success: false, error: "WebSocket relay URL not configured" };
|
|
6776
6578
|
}
|
|
6777
|
-
console.log("[ByteCave]
|
|
6579
|
+
console.log("[ByteCave] Storing via WebSocket relay");
|
|
6778
6580
|
try {
|
|
6779
|
-
|
|
6780
|
-
|
|
6781
|
-
console.log("[ByteCave] No cached peers, trying discovery services");
|
|
6782
|
-
if (this.relayDiscovery) {
|
|
6783
|
-
nodes = await this.relayDiscovery.getConnectedPeers();
|
|
6784
|
-
} else if (this.contractDiscovery) {
|
|
6785
|
-
nodes = await this.contractDiscovery.getActiveNodes();
|
|
6786
|
-
}
|
|
6787
|
-
} else {
|
|
6788
|
-
console.log("[ByteCave] Using", nodes.length, "cached peers for WebTransport");
|
|
6789
|
-
}
|
|
6790
|
-
if (nodes.length === 0) {
|
|
6791
|
-
return {
|
|
6792
|
-
success: false,
|
|
6793
|
-
error: "No nodes available for WebTransport storage"
|
|
6794
|
-
};
|
|
6795
|
-
}
|
|
6796
|
-
const nodeWithWebTransport = nodes.find(
|
|
6797
|
-
(node) => node.multiaddrs?.some((addr) => addr.includes("/webtransport"))
|
|
6798
|
-
);
|
|
6799
|
-
if (!nodeWithWebTransport || !nodeWithWebTransport.multiaddrs) {
|
|
6800
|
-
return {
|
|
6801
|
-
success: false,
|
|
6802
|
-
error: "No nodes with WebTransport support found"
|
|
6803
|
-
};
|
|
6804
|
-
}
|
|
6805
|
-
const wtMultiaddr = nodeWithWebTransport.multiaddrs.find(
|
|
6806
|
-
(addr) => addr.includes("/webtransport")
|
|
6807
|
-
);
|
|
6808
|
-
if (!wtMultiaddr) {
|
|
6809
|
-
return {
|
|
6810
|
-
success: false,
|
|
6811
|
-
error: "No WebTransport multiaddr found"
|
|
6812
|
-
};
|
|
6581
|
+
if (!this.storageWsClient) {
|
|
6582
|
+
this.storageWsClient = new StorageWebSocketClient(this.config.relayWsUrl);
|
|
6813
6583
|
}
|
|
6814
|
-
const
|
|
6815
|
-
const wtAuth = authorization ? {
|
|
6584
|
+
const wsAuth = authorization ? {
|
|
6816
6585
|
signature: authorization.signature,
|
|
6817
6586
|
address: authorization.sender,
|
|
6818
6587
|
timestamp: authorization.timestamp,
|
|
@@ -6820,25 +6589,26 @@ Nonce: ${nonce}`;
|
|
|
6820
6589
|
appId: authorization.appId,
|
|
6821
6590
|
contentHash: authorization.contentHash
|
|
6822
6591
|
} : void 0;
|
|
6823
|
-
const result = await
|
|
6592
|
+
const result = await this.storageWsClient.store({
|
|
6824
6593
|
data: dataArray,
|
|
6825
6594
|
contentType: mimeType || "application/octet-stream",
|
|
6826
6595
|
hashIdToken,
|
|
6827
|
-
authorization:
|
|
6596
|
+
authorization: wsAuth,
|
|
6597
|
+
timeout: 3e4
|
|
6828
6598
|
});
|
|
6829
6599
|
if (result.success && result.cid) {
|
|
6830
|
-
console.log("[ByteCave] \u2713
|
|
6600
|
+
console.log("[ByteCave] \u2713 WebSocket storage successful:", result.cid);
|
|
6831
6601
|
return {
|
|
6832
6602
|
success: true,
|
|
6833
6603
|
cid: result.cid,
|
|
6834
|
-
peerId:
|
|
6604
|
+
peerId: "relay-ws"
|
|
6835
6605
|
};
|
|
6836
6606
|
}
|
|
6837
|
-
console.
|
|
6838
|
-
return { success: false, error: result.error || "
|
|
6607
|
+
console.warn("[ByteCave] WebSocket storage failed:", result.error);
|
|
6608
|
+
return { success: false, error: result.error || "WebSocket storage failed" };
|
|
6839
6609
|
} catch (err) {
|
|
6840
|
-
console.error("[ByteCave]
|
|
6841
|
-
return { success: false, error:
|
|
6610
|
+
console.error("[ByteCave] WebSocket storage exception:", err.message);
|
|
6611
|
+
return { success: false, error: err.message };
|
|
6842
6612
|
}
|
|
6843
6613
|
}
|
|
6844
6614
|
/**
|
|
@@ -6990,48 +6760,14 @@ Nonce: ${nonce}`;
|
|
|
6990
6760
|
try {
|
|
6991
6761
|
const peerInfo = this.knownPeers.get(peerId);
|
|
6992
6762
|
const relayAddrs = peerInfo?.relayAddrs;
|
|
6993
|
-
|
|
6994
|
-
|
|
6995
|
-
|
|
6996
|
-
|
|
6997
|
-
|
|
6998
|
-
|
|
6999
|
-
|
|
7000
|
-
|
|
7001
|
-
const ma = multiaddr(relayAddrs[0]);
|
|
7002
|
-
await this.node.dial(ma);
|
|
7003
|
-
console.log("[ByteCave] \u2713 Connected via relay");
|
|
7004
|
-
} catch (dialError) {
|
|
7005
|
-
console.warn("[ByteCave] Relay dial failed:", dialError.message);
|
|
7006
|
-
if (multiaddrs && multiaddrs.length > 0) {
|
|
7007
|
-
const wtAddr = multiaddrs.find((addr) => addr.includes("/webtransport"));
|
|
7008
|
-
if (wtAddr) {
|
|
7009
|
-
console.log("[ByteCave] Trying WebTransport address:", wtAddr.slice(0, 60));
|
|
7010
|
-
try {
|
|
7011
|
-
const ma = multiaddr(wtAddr);
|
|
7012
|
-
await this.node.dial(ma);
|
|
7013
|
-
console.log("[ByteCave] \u2713 Connected via WebTransport");
|
|
7014
|
-
} catch (wtError) {
|
|
7015
|
-
console.warn("[ByteCave] WebTransport dial failed:", wtError.message);
|
|
7016
|
-
}
|
|
7017
|
-
}
|
|
7018
|
-
}
|
|
7019
|
-
}
|
|
7020
|
-
} else if (multiaddrs && multiaddrs.length > 0) {
|
|
7021
|
-
const wtAddr = multiaddrs.find((addr) => addr.includes("/webtransport"));
|
|
7022
|
-
if (wtAddr) {
|
|
7023
|
-
console.log("[ByteCave] Trying WebTransport address (no relay):", wtAddr.slice(0, 60));
|
|
7024
|
-
try {
|
|
7025
|
-
const ma = multiaddr(wtAddr);
|
|
7026
|
-
await this.node.dial(ma);
|
|
7027
|
-
console.log("[ByteCave] \u2713 Connected via WebTransport");
|
|
7028
|
-
} catch (wtError) {
|
|
7029
|
-
console.warn("[ByteCave] WebTransport dial failed:", wtError.message);
|
|
7030
|
-
}
|
|
7031
|
-
}
|
|
7032
|
-
}
|
|
7033
|
-
} else {
|
|
7034
|
-
console.log("[ByteCave] Already connected to peer via", connections[0].remoteAddr.toString().slice(0, 60));
|
|
6763
|
+
if (relayAddrs && relayAddrs.length > 0 && this.node) {
|
|
6764
|
+
console.log("[ByteCave] Dialing peer through relay:", peerId.slice(0, 12), relayAddrs[0]);
|
|
6765
|
+
try {
|
|
6766
|
+
const ma = multiaddr(relayAddrs[0]);
|
|
6767
|
+
await this.node.dial(ma);
|
|
6768
|
+
console.log("[ByteCave] Successfully dialed peer through relay");
|
|
6769
|
+
} catch (dialError) {
|
|
6770
|
+
console.warn("[ByteCave] Failed to dial through relay:", dialError);
|
|
7035
6771
|
}
|
|
7036
6772
|
}
|
|
7037
6773
|
const health = await p2pProtocolClient.getHealthFromPeer(peerId);
|
|
@@ -7137,106 +6873,8 @@ Nonce: ${nonce}`;
|
|
|
7137
6873
|
onChainNodeId: announcement.onChainNodeId
|
|
7138
6874
|
};
|
|
7139
6875
|
this.knownPeers.set(announcement.peerId, peerInfo);
|
|
7140
|
-
this.saveCachedPeers();
|
|
7141
|
-
if (this.node && !peerInfo.connected) {
|
|
7142
|
-
this.dialPeer(announcement.peerId, peerInfo.relayAddrs, peerInfo.multiaddrs).catch((err) => {
|
|
7143
|
-
console.warn("[ByteCave] Failed to dial announced peer:", err.message);
|
|
7144
|
-
});
|
|
7145
|
-
}
|
|
7146
6876
|
this.emit("peerAnnounce", peerInfo);
|
|
7147
6877
|
}
|
|
7148
|
-
/**
|
|
7149
|
-
* Attempt to dial a peer using relay or WebTransport addresses
|
|
7150
|
-
*/
|
|
7151
|
-
async dialPeer(peerId, relayAddrs, multiaddrs) {
|
|
7152
|
-
if (!this.node) return;
|
|
7153
|
-
const connections = this.node.getConnections().filter((c) => c.remotePeer.toString() === peerId);
|
|
7154
|
-
if (connections.length > 0) {
|
|
7155
|
-
console.log("[ByteCave] Already connected to peer:", peerId.slice(0, 12));
|
|
7156
|
-
return;
|
|
7157
|
-
}
|
|
7158
|
-
console.log("[ByteCave] Attempting to dial peer:", peerId.slice(0, 12));
|
|
7159
|
-
if (relayAddrs && relayAddrs.length > 0) {
|
|
7160
|
-
console.log("[ByteCave] Trying relay address:", relayAddrs[0].slice(0, 60));
|
|
7161
|
-
try {
|
|
7162
|
-
const ma = multiaddr(relayAddrs[0]);
|
|
7163
|
-
await this.node.dial(ma);
|
|
7164
|
-
console.log("[ByteCave] \u2713 Connected via relay");
|
|
7165
|
-
const peer = this.knownPeers.get(peerId);
|
|
7166
|
-
if (peer) {
|
|
7167
|
-
peer.connected = true;
|
|
7168
|
-
}
|
|
7169
|
-
return;
|
|
7170
|
-
} catch (error) {
|
|
7171
|
-
console.warn("[ByteCave] Relay dial failed:", error.message);
|
|
7172
|
-
}
|
|
7173
|
-
}
|
|
7174
|
-
if (multiaddrs && multiaddrs.length > 0) {
|
|
7175
|
-
const wtAddr = multiaddrs.find((addr) => addr.includes("/webtransport"));
|
|
7176
|
-
if (wtAddr) {
|
|
7177
|
-
console.log("[ByteCave] Trying WebTransport address:", wtAddr.slice(0, 60));
|
|
7178
|
-
try {
|
|
7179
|
-
const ma = multiaddr(wtAddr);
|
|
7180
|
-
await this.node.dial(ma);
|
|
7181
|
-
console.log("[ByteCave] \u2713 Connected via WebTransport");
|
|
7182
|
-
const peer = this.knownPeers.get(peerId);
|
|
7183
|
-
if (peer) {
|
|
7184
|
-
peer.connected = true;
|
|
7185
|
-
}
|
|
7186
|
-
} catch (error) {
|
|
7187
|
-
console.warn("[ByteCave] WebTransport dial failed:", error.message);
|
|
7188
|
-
}
|
|
7189
|
-
}
|
|
7190
|
-
}
|
|
7191
|
-
}
|
|
7192
|
-
/**
|
|
7193
|
-
* Load cached peers from localStorage
|
|
7194
|
-
*/
|
|
7195
|
-
loadCachedPeers() {
|
|
7196
|
-
try {
|
|
7197
|
-
const cached = localStorage.getItem(this.PEERS_CACHE_KEY);
|
|
7198
|
-
if (cached) {
|
|
7199
|
-
const peers = JSON.parse(cached);
|
|
7200
|
-
console.log("[ByteCave] Loaded", peers.length, "cached peers from localStorage");
|
|
7201
|
-
for (const peer of peers) {
|
|
7202
|
-
this.knownPeers.set(peer.peerId, { ...peer, connected: false });
|
|
7203
|
-
}
|
|
7204
|
-
}
|
|
7205
|
-
} catch (error) {
|
|
7206
|
-
console.warn("[ByteCave] Failed to load cached peers:", error);
|
|
7207
|
-
}
|
|
7208
|
-
}
|
|
7209
|
-
/**
|
|
7210
|
-
* Dial all cached peers (used on startup when relay might be down)
|
|
7211
|
-
*/
|
|
7212
|
-
async dialCachedPeers() {
|
|
7213
|
-
console.log("[ByteCave] Dialing", this.knownPeers.size, "cached peers...");
|
|
7214
|
-
for (const [peerId, peerInfo] of this.knownPeers) {
|
|
7215
|
-
if (peerInfo.connected) {
|
|
7216
|
-
continue;
|
|
7217
|
-
}
|
|
7218
|
-
const relayAddrs = peerInfo.relayAddrs;
|
|
7219
|
-
const multiaddrs = peerInfo.multiaddrs;
|
|
7220
|
-
if (relayAddrs || multiaddrs) {
|
|
7221
|
-
this.dialPeer(peerId, relayAddrs, multiaddrs).catch((err) => {
|
|
7222
|
-
console.warn("[ByteCave] Failed to dial cached peer:", peerId.slice(0, 12), err.message);
|
|
7223
|
-
});
|
|
7224
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
7225
|
-
}
|
|
7226
|
-
}
|
|
7227
|
-
}
|
|
7228
|
-
/**
|
|
7229
|
-
* Save known peers to localStorage
|
|
7230
|
-
*/
|
|
7231
|
-
saveCachedPeers() {
|
|
7232
|
-
try {
|
|
7233
|
-
const peers = Array.from(this.knownPeers.values());
|
|
7234
|
-
localStorage.setItem(this.PEERS_CACHE_KEY, JSON.stringify(peers));
|
|
7235
|
-
console.log("[ByteCave] Saved", peers.length, "peers to localStorage");
|
|
7236
|
-
} catch (error) {
|
|
7237
|
-
console.warn("[ByteCave] Failed to save cached peers:", error);
|
|
7238
|
-
}
|
|
7239
|
-
}
|
|
7240
6878
|
/**
|
|
7241
6879
|
* Check if a nodeId is registered in the on-chain registry
|
|
7242
6880
|
*/
|
package/dist/client.d.ts
CHANGED
|
@@ -14,7 +14,6 @@ export declare class ByteCaveClient {
|
|
|
14
14
|
private knownPeers;
|
|
15
15
|
private connectionState;
|
|
16
16
|
private eventListeners;
|
|
17
|
-
private readonly PEERS_CACHE_KEY;
|
|
18
17
|
constructor(config: ByteCaveConfig);
|
|
19
18
|
/**
|
|
20
19
|
* Initialize and start the P2P client
|
|
@@ -98,22 +97,6 @@ export declare class ByteCaveClient {
|
|
|
98
97
|
private setupEventListeners;
|
|
99
98
|
private setupPubsub;
|
|
100
99
|
private handlePeerAnnouncement;
|
|
101
|
-
/**
|
|
102
|
-
* Attempt to dial a peer using relay or WebTransport addresses
|
|
103
|
-
*/
|
|
104
|
-
private dialPeer;
|
|
105
|
-
/**
|
|
106
|
-
* Load cached peers from localStorage
|
|
107
|
-
*/
|
|
108
|
-
private loadCachedPeers;
|
|
109
|
-
/**
|
|
110
|
-
* Dial all cached peers (used on startup when relay might be down)
|
|
111
|
-
*/
|
|
112
|
-
private dialCachedPeers;
|
|
113
|
-
/**
|
|
114
|
-
* Save known peers to localStorage
|
|
115
|
-
*/
|
|
116
|
-
private saveCachedPeers;
|
|
117
100
|
/**
|
|
118
101
|
* Check if a nodeId is registered in the on-chain registry
|
|
119
102
|
*/
|