rooms-kit / test / helpers / orbit-address.mjs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * Boot a real room log and read its OrbitDB address — shared by
 * test/vectors/generate-orbit-address.mjs (against the v1.0.0 TypeScript build)
 * and test/orbit-address.test.mjs (against the committed ClojureScript dist), so
 * both implementations are driven through the identical sequence.
 *
 * A stub libp2p is not an option: helia gates on `isLibp2p()`. The relay
 * multiaddr therefore points at an unroutable host — the node comes up, the
 * transports (including the native WebRTC one) load, the dial simply fails, and
 * the address is computed entirely locally.
 */
import { mkdirSync } from "node:fs";
import { join } from "node:path";

/** An unroutable relay: sslip.io resolves 127-0-0-1 to 127.0.0.1, port 1. */
const RELAY =
  "/dns4/127-0-0-1.sslip.io/tcp/1/tls/ws/p2p/12D3KooWQzZvygPwd2F4JAqqf6tfBSJ29YtjzzftUyJ37RLCG5WT";

/** 32 × 0x01, base64url — a fixed seed, so myAuthorId is deterministic too. */
export const SEED = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE";

export const addressCases = [
  { secret: "A".repeat(43), appSalt: "chat.ardegazu.ro/v2", withIdentity: true },
  { secret: "A".repeat(43), appSalt: "chat.ardegazu.ro/v2", withIdentity: false },
  { secret: "B".repeat(43), appSalt: "board.ardegazu.ro/v1", withIdentity: true },
];

const NO_EVENTS = {
  peerState() {}, peerGone() {}, message() {}, binary() {}, peerReady() {}, status() {},
};

/**
 * @param mods {{crypto, log, net, turn, env, id}} the modules under test
 * @param c    one entry of `addressCases`
 * @param dir  a scratch directory for the blockstore/datastore/orbitdb dirs
 */
export async function openAddressCase(mods, c, dir) {
  mkdirSync(dir, { recursive: true });
  // one origin per process: installOrigin throws on a second, different one
  try {
    mods.env.installOrigin("https://chat.ardegazu.ro");
  } catch {
    /* already installed by a previous case */
  }
  // Net's #start registers window listeners (mobile wake), so the DOM shims are
  // required — and must be installed only after every module is imported (see
  // the axios note in the generator's header).
  mods.env.installBrowserGlobals();
  const { privateKey } = await mods.net.newSessionKey();
  const rc = await mods.crypto.RoomCrypto.create(c.secret, c.appSalt, "12D3KooWFixed");
  const net = await mods.net.Net.create({
    privateKey,
    crypto: rc,
    ice: new mods.turn.IceConfig("https://127.0.0.1:1/turn-credentials"),
    wsOrigin: "https://chat.ardegazu.ro",
    relayMultiaddr: RELAY,
    discoveryTopic: "_peer-discovery._p2p._pubsub",
    myName: () => "addr-probe",
    events: NO_EVENTS,
  });
  const helia = await mods.log.openHelia(net.libp2p, {
    blocks: join(dir, "blocks"),
    data: join(dir, "data"),
  });
  const identity = c.withIdentity ? await mods.id.Identity.fromSeed(SEED) : null;
  const log = await mods.log.RoomLog.open(helia, rc, identity, { directory: join(dir, "orbitdb") });
  const result = {
    ...c,
    seed: c.withIdentity ? SEED : null,
    dbName: await rc.dbName(),
    address: log.address,
    // the durable author id: the identity pub for a sueta identity, OrbitDB's
    // own per-device id otherwise (non-deterministic, so only its shape is kept)
    myAuthorId: c.withIdentity ? log.myAuthorId : typeof log.myAuthorId,
  };
  await log.close();
  await net.close();
  return result;
}

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/rooms-kit.git