dgb-wallet/packages/p2p/scripts/handshake-demo.ts
Digibyte.X d50286a9a5 DigiByte.X Wallet — recovery-first web wallet monorepo
Packages: core (BIP39/32/44/49/84/86 derivation, WIF), keystore (PBKDF2 +
AES-GCM), psbt, p2p (BIP157/158 SPV daemon, Dandelion++ broadcast with
relay-echo verification), blockbook, rpc, electrum, net, bns, web-ui
(React/Vite), mobile (Expo). ops/ holds the nginx vhost, systemd unit and
push.sh deploy script for digibyte.x/web.
2026-09-20 20:16:52 +02:00

70 lines
2.7 KiB
TypeScript

// Milestone 1 demo: resolve DGB DNS seeds → connect to real peers →
// perform the P2P version/verack handshake → report per-peer service
// flags and user agent.
//
// This proves: correct magic bytes, correct port, correct protocol
// version, correct framing/varint/version-message encoding — all
// against real live peers on the DigiByte mainnet.
import { promises as dns } from 'node:dns';
import { DGB_P2P } from '@dgb-wallet/core';
import { Peer, servicesString } from '../src/index.js';
const PEERS_PER_SEED = 3;
const HANDSHAKE_TIMEOUT_MS = 10_000;
console.log('\n=== DigiByte P2P Handshake Demo ===\n');
console.log(`Discovering peers via DNS seeds on port ${DGB_P2P.defaultPort}...`);
const ips = new Set<string>();
for (const seed of DGB_P2P.dnsSeeds) {
try {
const addrs = await dns.resolve4(seed).catch(() => [] as string[]);
const addrs6 = await dns.resolve6(seed).catch(() => [] as string[]);
// Milestone 1 only supports IPv4 outbound in `writeNetAddr`.
for (const a of addrs.slice(0, PEERS_PER_SEED)) ips.add(a);
console.log(` ${seed} -> ${addrs.length} IPv4 + ${addrs6.length} IPv6`);
} catch (e) {
console.log(` ${seed} -> ${(e as Error).message}`);
}
}
const candidates = [...ips].slice(0, 8);
if (candidates.length === 0) {
console.error('\nNo peer IPs resolved. DGB DNS seed infrastructure may be offline.');
console.error('You can still hand-set a peer: npm run handshake -w @dgb-wallet/p2p -- <ip>');
process.exit(1);
}
// Allow overriding with a hand-supplied IP on the command line.
const override = process.argv.slice(2)[0];
const targets = override ? [override] : candidates;
console.log(`\nAttempting handshake with ${targets.length} peer(s)...\n`);
let successes = 0;
for (const ip of targets) {
const peer = new Peer({ host: ip, handshakeTimeoutMs: HANDSHAKE_TIMEOUT_MS });
try {
const info = await peer.handshake();
successes++;
console.log(`✓ ${ip}:${info.port}`);
console.log(` protocol: ${info.peer.protocolVersion}`);
console.log(` user agent: ${info.peer.userAgent}`);
console.log(` services: ${info.peerServices}`);
console.log(` tip height: ${info.peer.startHeight.toLocaleString()}`);
console.log(` rtt: ${info.durationMs}ms`);
} catch (e) {
console.log(`✗ ${ip} — ${(e as Error).message}`);
} finally {
peer.disconnect();
}
console.log();
}
console.log(`Result: ${successes}/${targets.length} handshakes succeeded.`);
if (successes === 0) {
console.log('\nIf everything timed out, check that outbound TCP to port 12024 is allowed');
console.log('and that at least one peer supports the protocol version we advertised.');
process.exit(1);
}