OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-sync / lib / topics.js

23 lines · javascript ref:
const crypto = require('hypercore-crypto');

/**
 * Derive a DHT topic for a repo.
 * topic = hash(serverPublicKey + repoName)
 * Deterministic — same key+name = same topic on all machines.
 */
function deriveTopic (serverKey, repoName) {
  const keyBuf = typeof serverKey === 'string'
    ? Buffer.from(serverKey, 'hex')
    : serverKey;
  // crypto.data (NOT crypto.hash) — has a domain-separation prefix so
  // per-repo topics can't collide with crypto.hash-based topics
  // elsewhere (daemon identity swarm, hyper-svc swarm-RPC, etc.).
  // Without the prefix, repos named the same string used in those
  // topics' inputs (e.g. 'pear-git-v3') land on the same swarm topic,
  // breaking all three. git-remote-punch must also use crypto.data
  // to match — see git-remote-punch/lib/derive-topic.js.
  return crypto.data(Buffer.concat([keyBuf, Buffer.from(repoName, 'utf-8')]));
}

module.exports = { deriveTopic };