soulprint-network 0.4.2 → 0.4.3

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.
@@ -1,7 +1,7 @@
1
1
  {
2
- "codeHash": "740841a4e4069e7576a21b2da277ae7bb5b65bed028121cbc3277c865f58544f",
3
- "codeHashHex": "0x740841a4e4069e7576a21b2da277ae7bb5b65bed028121cbc3277c865f58544f",
4
- "computedAt": "2026-03-01T01:46:41.787Z",
2
+ "codeHash": "7f708d77d85f66abb7951d5e2761d23b3f07a64c5e71fdb4db02d6bdb958bbd9",
3
+ "codeHashHex": "0x7f708d77d85f66abb7951d5e2761d23b3f07a64c5e71fdb4db02d6bdb958bbd9",
4
+ "computedAt": "2026-03-01T01:59:34.396Z",
5
5
  "fileCount": 21,
6
6
  "files": [
7
7
  "blockchain/PeerRegistryClient.ts",
package/dist/server.js CHANGED
@@ -43,41 +43,76 @@ catch (err) {
43
43
  console.warn(`⚠️ P2P no disponible — solo HTTP gossip activo`);
44
44
  console.warn(` Error: ${err?.message ?? String(err)}\n`);
45
45
  }
46
- // ─── On-chain PeerRegistry (auto-registro) ────────────────────────────────────
46
+ // ─── On-chain PeerRegistry (auto-registro + bootstrap desde chain) ────────────
47
47
  const adminPrivateKey = process.env.ADMIN_PRIVATE_KEY;
48
48
  const peerRegistry = new PeerRegistryClient({
49
49
  privateKey: adminPrivateKey,
50
50
  });
51
51
  setPeerRegistryClient(peerRegistry);
52
- // Register self after P2P is ready (non-blocking)
52
+ // Bootstrap P2P + register self after node is ready (non-blocking)
53
53
  setTimeout(async () => {
54
54
  try {
55
+ // 1. Leer peers on-chain y hacer dial P2P a sus multiaddrs
56
+ const chainPeers = await peerRegistry.getAllPeers().catch(() => []);
57
+ if (chainPeers.length > 0) {
58
+ console.log(`[peer-registry] 🔗 ${chainPeers.length} peer(s) encontrados on-chain — conectando...`);
59
+ for (const peer of chainPeers) {
60
+ try {
61
+ if (!peer.multiaddr)
62
+ continue;
63
+ // Si es multiaddr P2P (/ip4/.../tcp/.../p2p/...) intentamos dial
64
+ if (peer.multiaddr.startsWith("/ip4") || peer.multiaddr.startsWith("/dns")) {
65
+ // Agregar como peer conocido via HTTP bootstrap (más estable que dial directo)
66
+ const httpUrl = peer.multiaddr.replace(/\/ip4\/([^/]+)\/tcp\/(\d+).*/, "http://$1:$2").replace("/p2p/", "");
67
+ if (httpUrl.startsWith("http")) {
68
+ const PROTOCOL_HASH = "dfe1ccca1270ec86f93308dc4b981bab1d6bd74bdcc334059f4380b407ca07ca";
69
+ await fetch(`http://localhost:${HTTP_PORT}/peers/register`, {
70
+ method: "POST",
71
+ headers: { "Content-Type": "application/json" },
72
+ body: JSON.stringify({ url: httpUrl, protocol_hash: PROTOCOL_HASH }),
73
+ signal: AbortSignal.timeout(5_000),
74
+ }).catch(() => null);
75
+ }
76
+ console.log(`[peer-registry] ✅ Peer P2P registrado: ${peer.multiaddr.slice(0, 40)}`);
77
+ }
78
+ else if (peer.multiaddr.startsWith("http")) {
79
+ // HTTP peer — registrar vía /peers/register
80
+ const PROTOCOL_HASH = "dfe1ccca1270ec86f93308dc4b981bab1d6bd74bdcc334059f4380b407ca07ca";
81
+ await fetch(`http://localhost:${HTTP_PORT}/peers/register`, {
82
+ method: "POST",
83
+ headers: { "Content-Type": "application/json" },
84
+ body: JSON.stringify({ url: peer.multiaddr, protocol_hash: PROTOCOL_HASH }),
85
+ signal: AbortSignal.timeout(5_000),
86
+ }).catch(() => null);
87
+ console.log(`[peer-registry] 🌐 Peer HTTP registrado: ${peer.multiaddr}`);
88
+ }
89
+ }
90
+ catch (e) {
91
+ console.warn(`[peer-registry] ⚠️ No se pudo conectar a ${peer.multiaddr}: ${e.message}`);
92
+ }
93
+ }
94
+ }
95
+ else {
96
+ console.log("[peer-registry] ℹ️ No hay peers registrados on-chain aún — primer nodo de la red");
97
+ }
98
+ // 2. Registrar self on-chain
55
99
  if (!adminPrivateKey) {
56
100
  console.warn("[peer-registry] ⚠️ ADMIN_PRIVATE_KEY not set — skipping on-chain registration");
57
101
  return;
58
102
  }
59
- // Get node identity from validator (fetched via HTTP)
60
- const infoRes = await fetch(`http://localhost:${HTTP_PORT}/health`, { signal: AbortSignal.timeout(5000) });
61
- const info = await infoRes.json();
62
- // Get P2P multiaddr (first non-localhost if available)
63
103
  let multiaddr = `http://localhost:${HTTP_PORT}`;
64
104
  if (p2pNode) {
65
105
  const addrs = p2pNode.getMultiaddrs().map((m) => m.toString());
66
106
  const publicAddr = addrs.find(a => !a.includes("127.0.0.1") && !a.includes("/ip4/0.0.0.0"));
67
107
  multiaddr = publicAddr ?? addrs[0] ?? multiaddr;
68
108
  }
69
- // Use node DID from keypair (read from health endpoint) and P2P peerId
70
109
  const nodeDid = globalThis._nodeDid ?? `did:soulprint:node:${Date.now()}`;
71
110
  const nodePeer = p2pNode?.peerId?.toString() ?? "";
72
- await peerRegistry.registerSelf({
73
- peerDid: nodeDid,
74
- peerId: nodePeer,
75
- multiaddr,
76
- score: 0,
77
- });
111
+ await peerRegistry.registerSelf({ peerDid: nodeDid, peerId: nodePeer, multiaddr, score: 0 });
112
+ console.log(`[peer-registry] ✅ Registrado on-chain: ${nodeDid.slice(0, 30)}…`);
78
113
  }
79
114
  catch (e) {
80
- console.warn(`[peer-registry] ⚠️ Could not register self: ${e.message}`);
115
+ console.warn(`[peer-registry] ⚠️ Error en bootstrap on-chain: ${e.message}`);
81
116
  }
82
117
  }, 3_000);
83
118
  // ─── HTTP Bootstrap Peers (auto-registro) ─────────────────────────────────────
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soulprint-network",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "Soulprint validator node — HTTP server that verifies ZK proofs, co-signs SPTs, anti-Sybil registry",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",