@highway1/core 0.1.29 → 0.1.31

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.js CHANGED
@@ -1092,11 +1092,15 @@ function createDHTOperations(libp2p) {
1092
1092
  break;
1093
1093
  }
1094
1094
  }
1095
- const caps = (card.capabilities ?? []).map(
1096
- (c) => typeof c === "string" ? c : c.name
1097
- ).filter(Boolean);
1095
+ const caps = (card.capabilities ?? []).flatMap((c) => {
1096
+ if (typeof c === "string") return [c];
1097
+ return [c.name, c.id].filter(Boolean);
1098
+ });
1098
1099
  caps.push("__all__");
1099
1100
  const importantCaps = ["__all__"];
1101
+ if (caps.some((cap) => capKey(cap) === "relay")) {
1102
+ importantCaps.push("relay");
1103
+ }
1100
1104
  await Promise.all(importantCaps.map(async (cap) => {
1101
1105
  const capKeyStr = `/clawiverse/cap/${capKey(cap)}`;
1102
1106
  const capDHTKey = fromString(capKeyStr);
@@ -1379,13 +1383,13 @@ function createMessageRouter(libp2p, verifyFn, dht, relayPeers) {
1379
1383
  } catch {
1380
1384
  }
1381
1385
  }
1386
+ let lastError;
1382
1387
  if (!stream) {
1383
1388
  const allRelayAddrs = [
1384
1389
  ...relayMultiaddrs,
1385
1390
  ...(relayPeers ?? []).map((r) => buildCircuitRelayAddr(r, targetPeerIdStr))
1386
1391
  ];
1387
1392
  const uniqueRelayAddrs = [...new Set(allRelayAddrs)];
1388
- let lastError;
1389
1393
  for (const addr of uniqueRelayAddrs) {
1390
1394
  try {
1391
1395
  const conn = await libp2p.dial(multiaddr(addr));
@@ -1398,7 +1402,6 @@ function createMessageRouter(libp2p, verifyFn, dht, relayPeers) {
1398
1402
  lastError = relayErr;
1399
1403
  }
1400
1404
  }
1401
- if (!stream) throw lastError ?? new MessagingError("All dial attempts failed");
1402
1405
  }
1403
1406
  if (!stream && dht && "queryRelayPeers" in dht) {
1404
1407
  const discoveredRelays = await dht.queryRelayPeers();
@@ -1411,9 +1414,12 @@ function createMessageRouter(libp2p, verifyFn, dht, relayPeers) {
1411
1414
  break;
1412
1415
  } catch (e) {
1413
1416
  logger5.warn("DHT relay failed", { relayAddr, error: e.message });
1417
+ lastError = e;
1414
1418
  }
1415
1419
  }
1416
- if (!stream) throw new MessagingError("All dial attempts failed (including DHT-discovered relays)");
1420
+ }
1421
+ if (!stream) {
1422
+ throw lastError ?? new MessagingError("All dial attempts failed (including DHT-discovered relays)");
1417
1423
  }
1418
1424
  const encoded = encodeMessage(envelope);
1419
1425
  await stream.sink(
@@ -1496,7 +1502,7 @@ function createMessageRouter(libp2p, verifyFn, dht, relayPeers) {
1496
1502
  function buildCircuitRelayAddr(relayAddr, targetPeerId) {
1497
1503
  return `${relayAddr}/p2p-circuit/p2p/${targetPeerId}`;
1498
1504
  }
1499
- async function handleIncomingStream(stream, handlers, catchAllHandler, _verifyFn) {
1505
+ async function handleIncomingStream(stream, handlers, catchAllHandler, verifyFn) {
1500
1506
  try {
1501
1507
  const chunks = [];
1502
1508
  for await (const chunk of stream.source) {
@@ -1515,6 +1521,37 @@ async function handleIncomingStream(stream, handlers, catchAllHandler, _verifyFn
1515
1521
  logger5.warn("Received invalid message envelope");
1516
1522
  return;
1517
1523
  }
1524
+ const isValidSignature = await verifyEnvelope(envelope, async (signature, data2) => {
1525
+ const senderPublicKey = extractPublicKey(envelope.from);
1526
+ return verify(signature, data2, senderPublicKey);
1527
+ });
1528
+ if (!isValidSignature) {
1529
+ logger5.warn("Received message with invalid signature", {
1530
+ id: envelope.id,
1531
+ from: envelope.from
1532
+ });
1533
+ return;
1534
+ }
1535
+ try {
1536
+ const { signature, ...envelopeWithoutSig } = envelope;
1537
+ const dataBytes = new TextEncoder().encode(JSON.stringify(envelopeWithoutSig));
1538
+ const signatureBytes = Buffer.from(signature, "hex");
1539
+ const hookValid = await verifyFn(signatureBytes, dataBytes);
1540
+ if (!hookValid) {
1541
+ logger5.warn("Message rejected by custom verifier", {
1542
+ id: envelope.id,
1543
+ from: envelope.from
1544
+ });
1545
+ return;
1546
+ }
1547
+ } catch (error) {
1548
+ logger5.warn("Custom verification hook failed", {
1549
+ id: envelope.id,
1550
+ from: envelope.from,
1551
+ error: error.message
1552
+ });
1553
+ return;
1554
+ }
1518
1555
  logger5.info("Received message", {
1519
1556
  id: envelope.id,
1520
1557
  from: envelope.from,