Files
corescope-rx/src/names.js
T
efiten cbf786f2cc fix: accept node-discover responses as region-discovery candidates
Every repeater on this network has a 47h advert interval, so real
adverts are almost never heard and the region-discovery candidate map
stayed empty — selectNextTarget always returned null and no region
request was ever sent (five discover rounds, zero region requests in
the field log).

Node-discover responses are what actually arrives constantly, but they
carry only an 8-byte pubkey prefix and no advert timestamp/type. Fixes:

- meshpacket.js: parsePacket now also extracts discoverType (low
  nibble of the DISCOVER_RESP flags byte, per
  simple_repeater/MyMesh.cpp:801) alongside the existing discoverPubkey.
  Purely additive — deriveHeardKey is untouched and its pinned tests
  stay green unmodified.
- names.js: add resolvePubkey, a sibling of resolveName that resolves
  a prefix to the full 32-byte pubkey via the same endpoint and cache
  discipline (network errors never cached; a full 64-hex key needs no
  request).
- app.js: accept hk.src === 'discover' candidates when discoverType is
  ADV_TYPE_REPEATER, resolved to the full pubkey so a repeater never
  keys twice under prefix and pubkey. advertTs is stored null;
  selectNextTarget's existing due() rule already asks it once per
  session and re-asks automatically once a real advert arrives.
- app.js: maybeQueryRegions now logs the half-rate rounds where a
  region request was due but no candidate existed yet — this bug was
  previously silent.
2026-08-18 20:27:33 +02:00

55 lines
2.3 KiB
JavaScript

// On-the-fly node-name resolution: per heard prefix/pubkey, one tiny request to
// the CoreScope resolve endpoint configured as `resolveUrl` in config.json (a
// CORS-enabled URL). Cached in memory for the session, so each distinct node is
// fetched at most once. When resolveUrl is empty the app skips resolution and
// the caller shows the prefix. A name is returned only when the prefix resolves
// uniquely; ambiguous/not-found → '' (caller shows the prefix).
import { getConfig } from './config.js';
const cache = new Map(); // key (lowercase hex) -> name | ''
const pubkeyCache = new Map(); // key (lowercase hex prefix) -> full 32-byte pubkey (hex) | ''
// resolveName resolves a heard key (2-3 byte prefix or full pubkey) to a name.
// Returns '' when unconfigured, ambiguous, or unknown. Network errors are not
// cached (retry later).
export async function resolveName(key) {
const c = getConfig();
const base = c && c.resolveUrl ? c.resolveUrl : '';
if (!base) return '';
const k = key.toLowerCase();
if (cache.has(k)) return cache.get(k);
try {
const r = await fetch(base + '?prefix=' + encodeURIComponent(k));
if (!r.ok) { cache.set(k, ''); return ''; }
const j = await r.json();
const name = !j.ambiguous && j.name ? j.name : '';
cache.set(k, name);
return name;
} catch (e) {
return ''; // transient — leave uncached so it can retry
}
}
// resolvePubkey resolves a heard key (prefix or full pubkey) to the full 32-byte
// pubkey, via the same resolve endpoint as resolveName. Returns '' when
// unconfigured, ambiguous, or unknown. A key that is already a full 64-hex pubkey
// is returned as-is with no request. Network errors are not cached (retry later).
export async function resolvePubkey(key) {
const k = key.toLowerCase();
if (/^[0-9a-f]{64}$/.test(k)) return k;
const c = getConfig();
const base = c && c.resolveUrl ? c.resolveUrl : '';
if (!base) return '';
if (pubkeyCache.has(k)) return pubkeyCache.get(k);
try {
const r = await fetch(base + '?prefix=' + encodeURIComponent(k));
if (!r.ok) { pubkeyCache.set(k, ''); return ''; }
const j = await r.json();
const pubkey = !j.ambiguous && j.pubkey ? j.pubkey : '';
pubkeyCache.set(k, pubkey);
return pubkey;
} catch (e) {
return ''; // transient — leave uncached so it can retry
}
}