mycomesh-consumer 0.5.0 → 0.5.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mycomesh-consumer",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "MycoMesh V11 Consumer: a custodied deposit, relay-blind encrypted requests to any Provider, and a local OpenAI-compatible endpoint.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/consumer.mjs CHANGED
@@ -160,11 +160,17 @@ export class Consumer {
160
160
  /** Manifest Relays first, then directory Relays whose /health proves the announced signer. */
161
161
  async relays() {
162
162
  if (this.relayCache && Date.now() - this.relayCache.at < RELAY_CACHE_MS) return this.relayCache.relays;
163
- const relays = [...this.network.relays];
163
+ const relays = this.network.relays.map((relay) => ({ ...relay }));
164
164
  const known = new Set(relays.map((relay) => relay.signer.toLowerCase()));
165
165
  try {
166
166
  for (const relay of await directoryRelays(this.network)) {
167
- if (known.has(relay.signer)) continue;
167
+ if (known.has(relay.signer)) {
168
+ // A manifest Relay that pinned its certificate on-chain is checked by the pin, not the CA.
169
+ const listed = relays.find((entry) => entry.signer.toLowerCase() === relay.signer);
170
+ listed.pin = relay.pin || listed.pin;
171
+ listed.owner = relay.owner;
172
+ continue;
173
+ }
168
174
  try {
169
175
  const { status, body } = await this.fetchJson(`${relay.url}/health`, { ca: this.network.tls_ca, pin: relay.pin, timeoutMs: 5_000 });
170
176
  if (status === 200 && String(body.relay_signer).toLowerCase() === relay.signer
package/src/tlspin.mjs CHANGED
@@ -1,36 +1,34 @@
1
- // TLS without certificate authorities: a Relay pins its self-signed certificate on-chain
1
+ // TLS without certificate authorities: a Relay pins its certificate on-chain
2
2
  // (https://IP:PORT#sha256=<hex> in the RelayDirectoryV11); mirrors mycomesh/tlspin.py.
3
+ // The handshake completes, the fingerprint is compared, and only then does HTTP write a byte,
4
+ // whether the certificate is self-signed or issued by some CA.
3
5
  import { createHash } from "node:crypto";
4
6
  import { connect } from "node:tls";
5
7
 
6
8
  const PIN_PREFIX = "#sha256=";
7
- const trusted = new Map(); // pin -> PEM of the certificate that matched it
8
9
 
9
10
  export function splitPin(endpoint) {
10
11
  const index = String(endpoint).indexOf(PIN_PREFIX);
11
12
  return index < 0 ? [String(endpoint), null] : [endpoint.slice(0, index), endpoint.slice(index + PIN_PREFIX.length).toLowerCase()];
12
13
  }
13
14
 
14
- /**
15
- * Fetch the Relay's certificate once, check it against the pin, and return
16
- * TLS options that trust exactly that certificate (no CA, no host name).
17
- */
18
- export async function pinnedOptions(url, pin) {
19
- if (!trusted.has(pin)) {
20
- const target = new URL(url);
21
- const pem = await new Promise((resolve, reject) => {
22
- const socket = connect({ host: target.hostname, port: Number(target.port || 443), rejectUnauthorized: false }, () => {
23
- const der = socket.getPeerCertificate(true).raw;
24
- socket.end();
15
+ /** Request options whose connection is handed to HTTP only after the certificate matches the pin. */
16
+ export function pinnedOptions(url, pin) {
17
+ const target = new URL(url);
18
+ return {
19
+ createConnection: (_options, ready) => {
20
+ const socket = connect({ host: target.hostname, port: Number(target.port || 443), rejectUnauthorized: false });
21
+ socket.once("secureConnect", () => {
22
+ const der = socket.getPeerCertificate(false)?.raw;
25
23
  if (!der || createHash("sha256").update(der).digest("hex") !== pin) {
26
- return reject(new Error(`Relay ${target.host} certificate does not match its on-chain pin`));
24
+ const error = new Error(`Relay ${target.host} certificate does not match its on-chain pin`);
25
+ socket.destroy(error);
26
+ return ready(error);
27
27
  }
28
- resolve(`-----BEGIN CERTIFICATE-----\n${der.toString("base64").match(/.{1,64}/g).join("\n")}\n-----END CERTIFICATE-----\n`);
28
+ return ready(null, socket);
29
29
  });
30
- socket.setTimeout(10_000, () => socket.destroy(new Error("TLS handshake timed out")));
31
- socket.on("error", reject);
32
- });
33
- trusted.set(pin, pem);
34
- }
35
- return { ca: [trusted.get(pin)], checkServerIdentity: () => undefined };
30
+ socket.once("error", (error) => ready(error));
31
+ return undefined;
32
+ },
33
+ };
36
34
  }