@peer2peer/cli 0.0.0-stage → 0.1.0
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/LICENSE +201 -0
- package/README.md +366 -2
- package/dist/ancestry.d.ts +13 -0
- package/dist/ancestry.js +71 -0
- package/dist/app.d.ts +363 -0
- package/dist/app.js +1177 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +56 -0
- package/dist/circle-ops.d.ts +402 -0
- package/dist/circle-ops.js +1725 -0
- package/dist/cli.d.ts +611 -0
- package/dist/cli.js +1650 -0
- package/dist/codes.d.ts +231 -0
- package/dist/codes.js +998 -0
- package/dist/config.d.ts +112 -0
- package/dist/config.js +243 -0
- package/dist/doctor.d.ts +99 -0
- package/dist/doctor.js +817 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +29 -0
- package/dist/ext-host.d.ts +32 -0
- package/dist/ext-host.js +259 -0
- package/dist/extensions.d.ts +386 -0
- package/dist/extensions.js +395 -0
- package/dist/features.d.ts +91 -0
- package/dist/features.js +164 -0
- package/dist/format.d.ts +57 -0
- package/dist/format.js +248 -0
- package/dist/identity.d.ts +236 -0
- package/dist/identity.js +751 -0
- package/dist/inbound.d.ts +32 -0
- package/dist/inbound.js +258 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +14 -0
- package/dist/join-answers.d.ts +49 -0
- package/dist/join-answers.js +149 -0
- package/dist/outbox.d.ts +109 -0
- package/dist/outbox.js +337 -0
- package/dist/pretty.d.ts +100 -0
- package/dist/pretty.js +256 -0
- package/dist/procstart.d.ts +1 -0
- package/dist/procstart.js +66 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +78 -0
- package/dist/projects.d.ts +240 -0
- package/dist/projects.js +699 -0
- package/dist/prompt/picker.d.ts +64 -0
- package/dist/prompt/picker.js +152 -0
- package/dist/prompt/tty.d.ts +29 -0
- package/dist/prompt/tty.js +122 -0
- package/dist/readonly.d.ts +20 -0
- package/dist/readonly.js +50 -0
- package/dist/receipts.d.ts +99 -0
- package/dist/receipts.js +180 -0
- package/dist/relay-client.d.ts +277 -0
- package/dist/relay-client.js +873 -0
- package/dist/relay-info.d.ts +24 -0
- package/dist/relay-info.js +53 -0
- package/dist/rotation.d.ts +402 -0
- package/dist/rotation.js +2054 -0
- package/dist/running.d.ts +5 -0
- package/dist/running.js +74 -0
- package/dist/sessions.d.ts +340 -0
- package/dist/sessions.js +1152 -0
- package/dist/shortcode.d.ts +149 -0
- package/dist/shortcode.js +465 -0
- package/dist/social.d.ts +139 -0
- package/dist/social.js +485 -0
- package/dist/state.d.ts +483 -0
- package/dist/state.js +217 -0
- package/dist/stdin.d.ts +17 -0
- package/dist/stdin.js +46 -0
- package/dist/sync.d.ts +77 -0
- package/dist/sync.js +375 -0
- package/dist/transient.d.ts +1 -0
- package/dist/transient.js +28 -0
- package/dist/trust.d.ts +37 -0
- package/dist/trust.js +134 -0
- package/dist/tty-confirm.d.ts +2 -0
- package/dist/tty-confirm.js +53 -0
- package/dist/tui/command.d.ts +20 -0
- package/dist/tui/command.js +70 -0
- package/dist/tui/frame.d.ts +43 -0
- package/dist/tui/frame.js +111 -0
- package/dist/tui/model.d.ts +215 -0
- package/dist/tui/model.js +255 -0
- package/dist/tui/sanitize.d.ts +13 -0
- package/dist/tui/sanitize.js +151 -0
- package/dist/tui/terminal.d.ts +88 -0
- package/dist/tui/terminal.js +354 -0
- package/dist/tui/tui.d.ts +71 -0
- package/dist/tui/tui.js +571 -0
- package/dist/tui/view.d.ts +4 -0
- package/dist/tui/view.js +356 -0
- package/dist/unknown-kinds.d.ts +12 -0
- package/dist/unknown-kinds.js +66 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +5 -0
- package/dist/want-pow.d.ts +12 -0
- package/dist/want-pow.js +46 -0
- package/dist/watch.d.ts +35 -0
- package/dist/watch.js +325 -0
- package/dist/xstate.d.ts +125 -0
- package/dist/xstate.js +422 -0
- package/package.json +60 -4
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { type LookupAddress } from "node:dns";
|
|
2
|
+
import type { LookupFunction } from "node:net";
|
|
3
|
+
/** 100 bits, carried in 13 bytes whose top 4 bits are zero. */
|
|
4
|
+
export declare const CODE_SECRET_LEN = 13;
|
|
5
|
+
export declare const CODE_CHARS = 20;
|
|
6
|
+
export declare const INVITE_ID_PREFIX = "p2p/v3/invite-id|";
|
|
7
|
+
export declare const INVITE_KEY_INFO = "p2p/v3/invite-key";
|
|
8
|
+
export declare const INVITE_AAD_PREFIX = "p2p/v3/invite|";
|
|
9
|
+
export declare const INVITE_ADMIT_INFO = "p2p/v3/relay-admit";
|
|
10
|
+
/** The relay's default cap on a blob (it advertises `inviteBlobBytes`). */
|
|
11
|
+
export declare const INVITE_BLOB_MAX: number;
|
|
12
|
+
/** P4: a code lives at most 7 days. */
|
|
13
|
+
export declare const CODE_MAX_TTL_SEC: number;
|
|
14
|
+
/** At most this many circles one code joins (`circle` plus `circles`). */
|
|
15
|
+
export declare const CODE_MAX_CIRCLES = 5;
|
|
16
|
+
/**
|
|
17
|
+
* The web form of a code: `https://ix-infra.com/p2p/invite#<relay part>/<CODE>`. It wraps the same
|
|
18
|
+
* single-use, relay-held code; the part after `#` (the secret) never reaches the web server.
|
|
19
|
+
*/
|
|
20
|
+
export declare const CODE_WEB_PREFIX = "https://ix-infra.com/p2p/invite#";
|
|
21
|
+
export declare function newCodeSecret(): Uint8Array;
|
|
22
|
+
/** The 100-bit secret -> 20 Crockford base32 characters (no padding). */
|
|
23
|
+
export declare function encodeCodeChars(s: Uint8Array): string;
|
|
24
|
+
/** Inverse of encodeCodeChars, forgiving of case, hyphens, spaces and O/I/L. Undefined when it isn't a code. */
|
|
25
|
+
export declare function decodeCodeChars(input: string): Uint8Array | undefined;
|
|
26
|
+
/** `K7QM3XPD9TWAHF2N6RJC` -> `K7QM-3XPD-9TWA-HF2N-6RJC`. */
|
|
27
|
+
export declare function groupCode(chars: string): string;
|
|
28
|
+
/** The relay part of a short code: the canonical URL without `wss://`, and without a trailing `/v3`. */
|
|
29
|
+
export declare function codeRelayPart(relay: string): string;
|
|
30
|
+
/** `<relay part>/<K7QM-3XPD-9TWA-HF2N-6RJC>`. */
|
|
31
|
+
export declare function formatShortCode(relay: string, s: Uint8Array): string;
|
|
32
|
+
/** The code as a web link: `https://ix-infra.com/p2p/invite#<relay part>/<CODE>`. */
|
|
33
|
+
export declare function codeWebLink(short: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* The code inside a web link, or undefined when `input` isn't one. Strict: https only, host exactly
|
|
36
|
+
* ix-infra.com (no port, no user info), path exactly /p2p/invite, no query, and a fragment that is a
|
|
37
|
+
* relay part and a code. Anything else that looks like a URL is refused, never guessed at.
|
|
38
|
+
*/
|
|
39
|
+
export declare function codeFromWebLink(input: string): string | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Parses `host[:port][/path]/CODE` (an optional `p2p join ` or `wss://`
|
|
42
|
+
* prefix is ignored) or a bare CODE. `relay` is undefined for a bare code.
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* What a private-relay refusal says: what it is, and exactly what to do about it. `what` names the
|
|
46
|
+
* thing ("code's relay", "invite's relay"); `detail` the address it resolved to; `code` adds the
|
|
47
|
+
* Claude Code form for codes.
|
|
48
|
+
*/
|
|
49
|
+
export declare function privateRelayMessage(what: string, host: string, opts?: {
|
|
50
|
+
detail?: string;
|
|
51
|
+
code?: boolean;
|
|
52
|
+
}): string;
|
|
53
|
+
/** Is `relay` one of `own` (the user's own relays: never refused or guarded, whatever their address)? */
|
|
54
|
+
export declare function isOwnRelay(relay: string, own: readonly string[]): boolean;
|
|
55
|
+
export declare function parseShortCode(input: string, opts?: {
|
|
56
|
+
allowPrivate?: boolean;
|
|
57
|
+
own?: readonly string[];
|
|
58
|
+
}): {
|
|
59
|
+
secret: Uint8Array;
|
|
60
|
+
relay?: string;
|
|
61
|
+
};
|
|
62
|
+
/** The code as typed, rebuilt from its parsed parts (never the raw input): `<relay part>/<CODE>`, or the bare CODE. */
|
|
63
|
+
export declare function canonicalShortCode(p: {
|
|
64
|
+
secret: Uint8Array;
|
|
65
|
+
relay?: string;
|
|
66
|
+
}): string;
|
|
67
|
+
/**
|
|
68
|
+
* An IP literal (v4 or v6, no brackets) that is loopback, link-local, private, multicast or otherwise
|
|
69
|
+
* not public unicast, including IPv6 forms that carry such an IPv4 address: IPv4-compatible
|
|
70
|
+
* (`::/96`, refused whole), IPv4-mapped (`::ffff:0:0/96`), IPv4-translated (`::ffff:0:0:0/96`), Teredo
|
|
71
|
+
* (`2001::/32`, refused whole), NAT64 (`64:ff9b::/96`, and the local-use
|
|
72
|
+
* `64:ff9b:1::/48`) and 6to4 (`2002::/16`); and fc00::/7, fe80::/10, fec0::/10 and ff00::/8.
|
|
73
|
+
* Not an IP literal: false (names are checked by isPrivateRelayHost, and when resolved, by the lookup).
|
|
74
|
+
*/
|
|
75
|
+
export declare function isPrivateAddress(ip: string): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* A relay host a code may not name without the opt-in: `localhost`, `*.localhost`, `*.local`, and
|
|
78
|
+
* every IP literal isPrivateAddress refuses. `host` is a URL hostname (IPv6 in brackets). A DNS
|
|
79
|
+
* name that resolves to such an address is caught when the code's relay is dialed (privateGuardLookup).
|
|
80
|
+
*/
|
|
81
|
+
export declare function isPrivateRelayHost(host: string): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Refuses relays named by a link (a long invite's or a circle link's) whose host isPrivateRelayHost
|
|
84
|
+
* refuses, unless `allow` (--allow-private-relay, P2P_ALLOW_PRIVATE_RELAY=1): the link's relays are
|
|
85
|
+
* dialed before anyone says yes. Names that aren't URLs are left to the link's own checks.
|
|
86
|
+
*/
|
|
87
|
+
export declare function refusePrivateRelays(relays: readonly string[], what: string, allow: boolean, opts?: {
|
|
88
|
+
code?: boolean;
|
|
89
|
+
}): void;
|
|
90
|
+
/**
|
|
91
|
+
* Resolves each relay's dialed host once, up front, for a clear error before anything is accepted;
|
|
92
|
+
* a name that doesn't resolve is skipped (the connection fails on its own). The connections
|
|
93
|
+
* themselves still go through privateGuardLookup (App.guardRelays): this only says it early.
|
|
94
|
+
*/
|
|
95
|
+
export declare function refusePrivateResolved(relays: readonly {
|
|
96
|
+
relay: string;
|
|
97
|
+
dial: string;
|
|
98
|
+
}[], what: string, opts?: {
|
|
99
|
+
code?: boolean;
|
|
100
|
+
}, resolve?: Resolver): Promise<void>;
|
|
101
|
+
/** What privateGuardLookup's error says, so a caller can tell it from an unreachable relay. */
|
|
102
|
+
export declare const PRIVATE_RESOLVE_MARK = "resolves to a loopback, link-local or private address";
|
|
103
|
+
type Resolver = (host: string, opts: {
|
|
104
|
+
all: true;
|
|
105
|
+
family?: number;
|
|
106
|
+
}, cb: (err: NodeJS.ErrnoException | null, addrs: LookupAddress[]) => void) => void;
|
|
107
|
+
/**
|
|
108
|
+
* A `lookup` for the socket that dials a code's relay: the name is resolved once, here, and refused
|
|
109
|
+
* when any address it resolves to is one isPrivateAddress refuses (`127.0.0.1.nip.io`,
|
|
110
|
+
* `localtest.me`, a rebinding name). The socket connects to the address handed back, so nothing is
|
|
111
|
+
* resolved again after the check. IP literals never reach a lookup: the parser checked those.
|
|
112
|
+
*/
|
|
113
|
+
export declare function privateGuardLookup(resolve?: Resolver): LookupFunction;
|
|
114
|
+
/** lid = b32(H("p2p/v3/invite-id|" || s))[:26]. */
|
|
115
|
+
export declare function inviteLid(s: Uint8Array): string;
|
|
116
|
+
/**
|
|
117
|
+
* Members-only relays: the key a redeemer presents at auth (`auth.admit`). The inviter's
|
|
118
|
+
* `invite-put` carries only its hash, so the relay learns the key when it is used, and only
|
|
119
|
+
* someone who holds the code can use it.
|
|
120
|
+
*/
|
|
121
|
+
export declare function inviteAdmitKey(s: Uint8Array): string;
|
|
122
|
+
/** What `invite-put` carries: b64u(SHA-256(key)). */
|
|
123
|
+
export declare function inviteAdmitHash(key: string): string;
|
|
124
|
+
/** Seals `bundle` (JSON) under the code: b64u(nonce || ciphertext || tag). */
|
|
125
|
+
export declare function sealInviteBundle(s: Uint8Array, bundle: unknown): string;
|
|
126
|
+
/** Opens a blob sealed under `s`; throws `code_invalid` if it was sealed under anything else, or altered. */
|
|
127
|
+
export declare function openInviteBundle(s: Uint8Array, blob: string): unknown;
|
|
128
|
+
/** The sealed bundle (version 1). */
|
|
129
|
+
export interface InviteBundle {
|
|
130
|
+
v: 1;
|
|
131
|
+
/** The invite JWS (§3.1): ik, relays, identity document and contact cap. */
|
|
132
|
+
invite: string;
|
|
133
|
+
/** The (first) circle the code joins: its head and (when it fits) the chain from version 1. */
|
|
134
|
+
circle?: {
|
|
135
|
+
head: string;
|
|
136
|
+
chain?: string[];
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* More circles the code joins (protocol 3.1, at most CODE_MAX_CIRCLES - 1): heads only, so the bundle
|
|
140
|
+
* fits the relay's blob cap; the inviter sends each chain when it adds the redeemer. A client from
|
|
141
|
+
* before this field ignores it (the bundle is parsed field by field) and joins only `circle`: an
|
|
142
|
+
* accepted downgrade, so it isn't critical.
|
|
143
|
+
*/
|
|
144
|
+
circles?: {
|
|
145
|
+
head: string;
|
|
146
|
+
}[];
|
|
147
|
+
}
|
|
148
|
+
export declare function parseInviteBundle(v: unknown): InviteBundle;
|
|
149
|
+
export {};
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// Short-code invites (docs/protocol.md P4): the pure part.
|
|
4
|
+
//
|
|
5
|
+
// - The secret `s` is 100 random bits, as 13 bytes big-endian whose top 4
|
|
6
|
+
// bits are zero. The code shows them as 20 Crockford base32 characters in
|
|
7
|
+
// groups of four, `K7QM-3XPD-9TWA-HF2N-6RJC`
|
|
8
|
+
// (case-insensitive; O reads as 0, I and L as 1; hyphens and spaces
|
|
9
|
+
// ignored), after the relay part: `<host[:port][/path]>/<CODE>`, where the
|
|
10
|
+
// relay part is the canonical relay URL without `wss://` and without a
|
|
11
|
+
// trailing `/v3`.
|
|
12
|
+
// - lid = b32(SHA-256("p2p/v3/invite-id|" || s))[:26]: the relay's key.
|
|
13
|
+
// - The bundle is sealed with ChaCha20-Poly1305 (RFC 8439) under
|
|
14
|
+
// HKDF-SHA256(ikm = s, salt = empty, info = "p2p/v3/invite-key", 32), a
|
|
15
|
+
// random 12-byte nonce and aad "p2p/v3/invite|" || lid:
|
|
16
|
+
// blob = b64u(nonce || ciphertext || tag). The relay holds only lid and
|
|
17
|
+
// blob, so it can neither read the bundle nor substitute another: either
|
|
18
|
+
// fails to open under the code.
|
|
19
|
+
import { createCipheriv, createDecipheriv, hkdfSync, randomBytes as nodeRandom } from "node:crypto";
|
|
20
|
+
import { lookup as dnsLookup } from "node:dns";
|
|
21
|
+
import { b32Encode, b64uDecode, b64uEncode, canonicalRelayUrl, relayFromLinkPart, relayLinkPart, sha256 } from "@peer2peer/core/v3";
|
|
22
|
+
import { CliError } from "./errors.js";
|
|
23
|
+
/** 100 bits, carried in 13 bytes whose top 4 bits are zero. */
|
|
24
|
+
export const CODE_SECRET_LEN = 13;
|
|
25
|
+
export const CODE_CHARS = 20;
|
|
26
|
+
export const INVITE_ID_PREFIX = "p2p/v3/invite-id|";
|
|
27
|
+
export const INVITE_KEY_INFO = "p2p/v3/invite-key";
|
|
28
|
+
export const INVITE_AAD_PREFIX = "p2p/v3/invite|";
|
|
29
|
+
export const INVITE_ADMIT_INFO = "p2p/v3/relay-admit";
|
|
30
|
+
/** The relay's default cap on a blob (it advertises `inviteBlobBytes`). */
|
|
31
|
+
export const INVITE_BLOB_MAX = 32 * 1024;
|
|
32
|
+
/** P4: a code lives at most 7 days. */
|
|
33
|
+
export const CODE_MAX_TTL_SEC = 7 * 86_400;
|
|
34
|
+
/** At most this many circles one code joins (`circle` plus `circles`). */
|
|
35
|
+
export const CODE_MAX_CIRCLES = 5;
|
|
36
|
+
/**
|
|
37
|
+
* The web form of a code: `https://ix-infra.com/p2p/invite#<relay part>/<CODE>`. It wraps the same
|
|
38
|
+
* single-use, relay-held code; the part after `#` (the secret) never reaches the web server.
|
|
39
|
+
*/
|
|
40
|
+
export const CODE_WEB_PREFIX = "https://ix-infra.com/p2p/invite#";
|
|
41
|
+
const CODE_WEB_HOST = "ix-infra.com";
|
|
42
|
+
const CODE_WEB_PATH = "/p2p/invite";
|
|
43
|
+
const CODE_WEB_ORIGIN = `https://${CODE_WEB_HOST}`;
|
|
44
|
+
/** What a web link's fragment may hold: a relay part (host, port, path) and the code. */
|
|
45
|
+
const CODE_FRAGMENT_RE = /^(?:[A-Za-z0-9.[\]:-]+(?:\/[A-Za-z0-9._~-]+)*\/)?[0-9A-Za-z][0-9A-Za-z-]{18,28}$/;
|
|
46
|
+
const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
47
|
+
const enc = new TextEncoder();
|
|
48
|
+
export function newCodeSecret() {
|
|
49
|
+
const s = new Uint8Array(nodeRandom(CODE_SECRET_LEN));
|
|
50
|
+
s[0] &= 0x0f;
|
|
51
|
+
return s;
|
|
52
|
+
}
|
|
53
|
+
function checkSecret(s) {
|
|
54
|
+
if (!(s instanceof Uint8Array) || s.length !== CODE_SECRET_LEN || s[0] > 0x0f)
|
|
55
|
+
throw new CliError("usage", "a code secret is 100 bits (13 bytes, top 4 bits zero)");
|
|
56
|
+
}
|
|
57
|
+
/** The 100-bit secret -> 20 Crockford base32 characters (no padding). */
|
|
58
|
+
export function encodeCodeChars(s) {
|
|
59
|
+
checkSecret(s);
|
|
60
|
+
let out = "";
|
|
61
|
+
let acc = 0n;
|
|
62
|
+
for (const b of s)
|
|
63
|
+
acc = (acc << 8n) | BigInt(b);
|
|
64
|
+
for (let i = CODE_CHARS - 1; i >= 0; i--)
|
|
65
|
+
out += CROCKFORD[Number((acc >> BigInt(i * 5)) & 31n)];
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
/** Inverse of encodeCodeChars, forgiving of case, hyphens, spaces and O/I/L. Undefined when it isn't a code. */
|
|
69
|
+
export function decodeCodeChars(input) {
|
|
70
|
+
const c = input.toUpperCase().replace(/[\s-]/g, "").replace(/O/g, "0").replace(/[IL]/g, "1");
|
|
71
|
+
if (c.length !== CODE_CHARS)
|
|
72
|
+
return undefined;
|
|
73
|
+
let acc = 0n;
|
|
74
|
+
for (const ch of c) {
|
|
75
|
+
const v = CROCKFORD.indexOf(ch);
|
|
76
|
+
if (v < 0)
|
|
77
|
+
return undefined;
|
|
78
|
+
acc = (acc << 5n) | BigInt(v);
|
|
79
|
+
}
|
|
80
|
+
const out = new Uint8Array(CODE_SECRET_LEN);
|
|
81
|
+
for (let i = CODE_SECRET_LEN - 1; i >= 0; i--) {
|
|
82
|
+
out[i] = Number(acc & 0xffn);
|
|
83
|
+
acc >>= 8n;
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
/** `K7QM3XPD9TWAHF2N6RJC` -> `K7QM-3XPD-9TWA-HF2N-6RJC`. */
|
|
88
|
+
export function groupCode(chars) {
|
|
89
|
+
return chars.match(/.{1,4}/g).join("-");
|
|
90
|
+
}
|
|
91
|
+
/** The relay part of a short code: the canonical URL without `wss://`, and without a trailing `/v3`. */
|
|
92
|
+
export function codeRelayPart(relay) {
|
|
93
|
+
const p = relayLinkPart(relay);
|
|
94
|
+
const i = p.indexOf("/");
|
|
95
|
+
return p.slice(i) === "/v3" ? p.slice(0, i) : p;
|
|
96
|
+
}
|
|
97
|
+
/** `<relay part>/<K7QM-3XPD-9TWA-HF2N-6RJC>`. */
|
|
98
|
+
export function formatShortCode(relay, s) {
|
|
99
|
+
return `${codeRelayPart(relay)}/${groupCode(encodeCodeChars(s))}`;
|
|
100
|
+
}
|
|
101
|
+
/** The code as a web link: `https://ix-infra.com/p2p/invite#<relay part>/<CODE>`. */
|
|
102
|
+
export function codeWebLink(short) {
|
|
103
|
+
return CODE_WEB_PREFIX + short;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The code inside a web link, or undefined when `input` isn't one. Strict: https only, host exactly
|
|
107
|
+
* ix-infra.com (no port, no user info), path exactly /p2p/invite, no query, and a fragment that is a
|
|
108
|
+
* relay part and a code. Anything else that looks like a URL is refused, never guessed at.
|
|
109
|
+
*/
|
|
110
|
+
export function codeFromWebLink(input) {
|
|
111
|
+
const t = input.trim();
|
|
112
|
+
// A URL has a scheme and `//`; a relay part with a port (`relay.example:8443/…`) does not.
|
|
113
|
+
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(t))
|
|
114
|
+
return undefined;
|
|
115
|
+
// Never left to URL normalisation: whitespace or control characters anywhere are refused.
|
|
116
|
+
if (/[\s\u0000-\u001f\u007f]/.test(t))
|
|
117
|
+
throw new CliError("usage", "that link has spaces or control characters in it: copy it again, whole");
|
|
118
|
+
// The other links have their own commands: say which, rather than "not an invite".
|
|
119
|
+
if (/^(?:https:\/\/(?:ix-infra\.com\/p2p|peer2peer\.dev)\/i#|p2p:\/\/invite\/)/i.test(t))
|
|
120
|
+
throw new CliError("usage", "that is a long invite link: use `p2p accept <link>`");
|
|
121
|
+
if (/^(?:https:\/\/(?:ix-infra\.com\/p2p|peer2peer\.dev)\/c#|p2p:\/\/circle\/)/i.test(t))
|
|
122
|
+
throw new CliError("usage", "that is a circle link: use `p2p circle join <link>`");
|
|
123
|
+
const refuse = (why) => new CliError("usage", `that is not a Peer2Peer invite link (${why}): it looks like ${CODE_WEB_PREFIX}relay.example/K7QM-3XPD-9TWA-HF2N-6RJC`);
|
|
124
|
+
let u;
|
|
125
|
+
try {
|
|
126
|
+
u = new URL(t);
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
throw refuse("not a URL");
|
|
130
|
+
}
|
|
131
|
+
if (u.protocol !== "https:")
|
|
132
|
+
throw refuse("only https links");
|
|
133
|
+
if (u.username !== "" || u.password !== "" || u.port !== "" || u.hostname !== CODE_WEB_HOST || !t.toLowerCase().startsWith(`https://${CODE_WEB_HOST}/`))
|
|
134
|
+
throw refuse(`the host must be ${CODE_WEB_HOST}`);
|
|
135
|
+
if (u.pathname !== CODE_WEB_PATH)
|
|
136
|
+
throw refuse(`the path must be ${CODE_WEB_PATH}`);
|
|
137
|
+
if (u.search !== "" || t.includes("?"))
|
|
138
|
+
throw refuse("a query string");
|
|
139
|
+
// The raw text, not the parsed URL: `/p2p/./invite`, `/x/../p2p/invite`, `%2e` and the like are
|
|
140
|
+
// refused rather than normalised into the one path that is accepted.
|
|
141
|
+
if (t.slice(0, CODE_WEB_ORIGIN.length).toLowerCase() !== CODE_WEB_ORIGIN || !t.slice(CODE_WEB_ORIGIN.length).startsWith(`${CODE_WEB_PATH}#`)) {
|
|
142
|
+
throw refuse(`the path must be ${CODE_WEB_PATH}`);
|
|
143
|
+
}
|
|
144
|
+
const frag = t.slice(CODE_WEB_ORIGIN.length + CODE_WEB_PATH.length + 1);
|
|
145
|
+
if (frag === "" || !CODE_FRAGMENT_RE.test(frag))
|
|
146
|
+
throw refuse("the part after # isn't an invite code");
|
|
147
|
+
return frag;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Parses `host[:port][/path]/CODE` (an optional `p2p join ` or `wss://`
|
|
151
|
+
* prefix is ignored) or a bare CODE. `relay` is undefined for a bare code.
|
|
152
|
+
*/
|
|
153
|
+
/**
|
|
154
|
+
* What a private-relay refusal says: what it is, and exactly what to do about it. `what` names the
|
|
155
|
+
* thing ("code's relay", "invite's relay"); `detail` the address it resolved to; `code` adds the
|
|
156
|
+
* Claude Code form for codes.
|
|
157
|
+
*/
|
|
158
|
+
export function privateRelayMessage(what, host, opts = {}) {
|
|
159
|
+
const how = opts.code ? "run the same command again with --allow-private-relay (in Claude Code: /peers join --allow-private-relay <code>)" : "run the same command again with --allow-private-relay";
|
|
160
|
+
return `the ${what} ${host}${opts.detail ? ` (${opts.detail})` : ""} is on a private network (for example Tailscale or a LAN). If you trust the person who sent it, ${how}, or set P2P_ALLOW_PRIVATE_RELAY=1. Your own relays never need this`;
|
|
161
|
+
}
|
|
162
|
+
/** Is `relay` one of `own` (the user's own relays: never refused or guarded, whatever their address)? */
|
|
163
|
+
export function isOwnRelay(relay, own) {
|
|
164
|
+
try {
|
|
165
|
+
const c = canonicalRelayUrl(relay);
|
|
166
|
+
return own.some((r) => {
|
|
167
|
+
try {
|
|
168
|
+
return canonicalRelayUrl(r) === c;
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export function parseShortCode(input, opts = {}) {
|
|
180
|
+
let t = (typeof input === "string" ? input : "").trim().replace(/^p2p\s+join\s+/i, "").trim();
|
|
181
|
+
// Pasted with the shell quoting the web page adds (`p2p join 'https://…'`): one pair of quotes is dropped.
|
|
182
|
+
const q = /^(['"])(.*)\1$/s.exec(t);
|
|
183
|
+
if (q)
|
|
184
|
+
t = q[2].trim();
|
|
185
|
+
// The web link wraps the same code (codeFromWebLink is strict); wss:// is a relay part's own prefix.
|
|
186
|
+
if (!/^wss:\/\//i.test(t))
|
|
187
|
+
t = codeFromWebLink(t) ?? t;
|
|
188
|
+
t = t.replace(/^wss:\/\//i, "");
|
|
189
|
+
t = t.replace(/\/+$/, "");
|
|
190
|
+
const slash = t.lastIndexOf("/");
|
|
191
|
+
const codePart = slash >= 0 ? t.slice(slash + 1) : t;
|
|
192
|
+
const secret = decodeCodeChars(codePart);
|
|
193
|
+
if (!secret)
|
|
194
|
+
throw new CliError("usage", "that is not an invite code (it looks like relay.example/K7QM-3XPD-9TWA-HF2N-6RJC)");
|
|
195
|
+
if (slash < 0)
|
|
196
|
+
return { secret };
|
|
197
|
+
let relay;
|
|
198
|
+
try {
|
|
199
|
+
relay = relayFromLinkPart(t.slice(0, slash));
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
throw new CliError("usage", "the code's relay part is not a relay host");
|
|
203
|
+
}
|
|
204
|
+
// A code from someone else connects to its relay before anyone says yes (to open it): a loopback,
|
|
205
|
+
// link-local or private host would let it reach this machine or the local network. A team relay on
|
|
206
|
+
// a private network is opted into (`p2p join --allow-private-relay`, or P2P_ALLOW_PRIVATE_RELAY=1).
|
|
207
|
+
// The user's own relays are theirs to choose (a team relay on Tailscale, say): never refused.
|
|
208
|
+
const host = new URL(relay).hostname;
|
|
209
|
+
if (!opts.allowPrivate && isPrivateRelayHost(host) && !isOwnRelay(relay, opts.own ?? [])) {
|
|
210
|
+
throw new CliError("private_relay", privateRelayMessage("code's relay", host, { code: true }));
|
|
211
|
+
}
|
|
212
|
+
return { secret, relay };
|
|
213
|
+
}
|
|
214
|
+
/** The code as typed, rebuilt from its parsed parts (never the raw input): `<relay part>/<CODE>`, or the bare CODE. */
|
|
215
|
+
export function canonicalShortCode(p) {
|
|
216
|
+
return p.relay ? formatShortCode(p.relay, p.secret) : groupCode(encodeCodeChars(p.secret));
|
|
217
|
+
}
|
|
218
|
+
/** An IPv4 address's four octets, or undefined. */
|
|
219
|
+
function parseV4(x) {
|
|
220
|
+
const m = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(x);
|
|
221
|
+
if (!m)
|
|
222
|
+
return undefined;
|
|
223
|
+
const o = m.slice(1).map(Number);
|
|
224
|
+
return o.every((n) => n <= 255) ? o : undefined;
|
|
225
|
+
}
|
|
226
|
+
/** An IPv6 address's eight 16-bit groups (a trailing dotted quad and a %zone allowed), or undefined. */
|
|
227
|
+
function parseV6(x) {
|
|
228
|
+
let h = x.replace(/%.*$/, "");
|
|
229
|
+
const tail = /^(.*:)(\d{1,3}(?:\.\d{1,3}){3})$/.exec(h);
|
|
230
|
+
if (tail) {
|
|
231
|
+
const o = parseV4(tail[2]);
|
|
232
|
+
if (!o)
|
|
233
|
+
return undefined;
|
|
234
|
+
h = `${tail[1]}${((o[0] << 8) | o[1]).toString(16)}:${((o[2] << 8) | o[3]).toString(16)}`;
|
|
235
|
+
}
|
|
236
|
+
const halves = h.split("::");
|
|
237
|
+
if (halves.length > 2)
|
|
238
|
+
return undefined;
|
|
239
|
+
const part = (p) => (p === "" ? [] : p.split(":"));
|
|
240
|
+
const head = part(halves[0]);
|
|
241
|
+
const rest = halves.length === 2 ? part(halves[1]) : [];
|
|
242
|
+
const fill = halves.length === 2 ? 8 - head.length - rest.length : 0;
|
|
243
|
+
if (fill < (halves.length === 2 ? 1 : 0))
|
|
244
|
+
return undefined;
|
|
245
|
+
const groups = [...head, ...Array(fill).fill("0"), ...rest];
|
|
246
|
+
if (groups.length !== 8 || !groups.every((g) => /^[0-9a-f]{1,4}$/i.test(g)))
|
|
247
|
+
return undefined;
|
|
248
|
+
return groups.map((g) => parseInt(g, 16));
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* IPv4 that is not a public unicast address: 0/8, 10/8, 100.64/10, 127/8, 169.254/16, 172.16/12,
|
|
252
|
+
* 192.168/16, 198.18/15 (benchmarking), 224/4 (multicast) and 240/4 (reserved, with 255.255.255.255).
|
|
253
|
+
*/
|
|
254
|
+
function v4Private(o) {
|
|
255
|
+
const [a, b] = o;
|
|
256
|
+
return (a === 0 ||
|
|
257
|
+
a === 10 ||
|
|
258
|
+
a === 127 ||
|
|
259
|
+
a >= 224 ||
|
|
260
|
+
(a === 100 && b >= 64 && b <= 127) ||
|
|
261
|
+
(a === 169 && b === 254) ||
|
|
262
|
+
(a === 172 && b >= 16 && b <= 31) ||
|
|
263
|
+
(a === 192 && b === 168) ||
|
|
264
|
+
(a === 198 && (b === 18 || b === 19)));
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* An IP literal (v4 or v6, no brackets) that is loopback, link-local, private, multicast or otherwise
|
|
268
|
+
* not public unicast, including IPv6 forms that carry such an IPv4 address: IPv4-compatible
|
|
269
|
+
* (`::/96`, refused whole), IPv4-mapped (`::ffff:0:0/96`), IPv4-translated (`::ffff:0:0:0/96`), Teredo
|
|
270
|
+
* (`2001::/32`, refused whole), NAT64 (`64:ff9b::/96`, and the local-use
|
|
271
|
+
* `64:ff9b:1::/48`) and 6to4 (`2002::/16`); and fc00::/7, fe80::/10, fec0::/10 and ff00::/8.
|
|
272
|
+
* Not an IP literal: false (names are checked by isPrivateRelayHost, and when resolved, by the lookup).
|
|
273
|
+
*/
|
|
274
|
+
export function isPrivateAddress(ip) {
|
|
275
|
+
const o = parseV4(ip);
|
|
276
|
+
if (o)
|
|
277
|
+
return v4Private(o);
|
|
278
|
+
const g = parseV6(ip.toLowerCase());
|
|
279
|
+
if (!g)
|
|
280
|
+
return false;
|
|
281
|
+
const v4of = (hi, lo) => [hi >> 8, hi & 255, lo >> 8, lo & 255];
|
|
282
|
+
if (g.slice(0, 6).every((x) => x === 0))
|
|
283
|
+
return true;
|
|
284
|
+
if (g.slice(0, 5).every((x) => x === 0) && g[5] === 0xffff)
|
|
285
|
+
return v4Private(v4of(g[6], g[7]));
|
|
286
|
+
// IPv4-translated (`::ffff:0:a.b.c.d`, ::ffff:0:0/96).
|
|
287
|
+
if (g.slice(0, 4).every((x) => x === 0) && g[4] === 0xffff && g[5] === 0)
|
|
288
|
+
return v4Private(v4of(g[6], g[7]));
|
|
289
|
+
// Teredo (2001::/32): the client address inside is obfuscated; refused whole.
|
|
290
|
+
if (g[0] === 0x2001 && g[1] === 0)
|
|
291
|
+
return true;
|
|
292
|
+
if (g[0] === 0x64 && g[1] === 0xff9b)
|
|
293
|
+
return g[2] === 1 || v4Private(v4of(g[6], g[7]));
|
|
294
|
+
if (g[0] === 0x2002)
|
|
295
|
+
return v4Private(v4of(g[1], g[2]));
|
|
296
|
+
return (g[0] & 0xfe00) === 0xfc00 || (g[0] & 0xffc0) === 0xfe80 || (g[0] & 0xffc0) === 0xfec0 || (g[0] & 0xff00) === 0xff00;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* A relay host a code may not name without the opt-in: `localhost`, `*.localhost`, `*.local`, and
|
|
300
|
+
* every IP literal isPrivateAddress refuses. `host` is a URL hostname (IPv6 in brackets). A DNS
|
|
301
|
+
* name that resolves to such an address is caught when the code's relay is dialed (privateGuardLookup).
|
|
302
|
+
*/
|
|
303
|
+
export function isPrivateRelayHost(host) {
|
|
304
|
+
let h = host.toLowerCase().replace(/\.$/, "");
|
|
305
|
+
if (h.startsWith("[") && h.endsWith("]"))
|
|
306
|
+
h = h.slice(1, -1);
|
|
307
|
+
if (h === "localhost" || h.endsWith(".localhost") || h === "local" || h.endsWith(".local"))
|
|
308
|
+
return true;
|
|
309
|
+
return isPrivateAddress(h);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Refuses relays named by a link (a long invite's or a circle link's) whose host isPrivateRelayHost
|
|
313
|
+
* refuses, unless `allow` (--allow-private-relay, P2P_ALLOW_PRIVATE_RELAY=1): the link's relays are
|
|
314
|
+
* dialed before anyone says yes. Names that aren't URLs are left to the link's own checks.
|
|
315
|
+
*/
|
|
316
|
+
export function refusePrivateRelays(relays, what, allow, opts = {}) {
|
|
317
|
+
if (allow)
|
|
318
|
+
return;
|
|
319
|
+
for (const r of relays) {
|
|
320
|
+
let host;
|
|
321
|
+
try {
|
|
322
|
+
host = new URL(r).hostname;
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
if (isPrivateRelayHost(host)) {
|
|
328
|
+
throw new CliError("private_relay", privateRelayMessage(`${what}'s relay`, host, opts));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Resolves each relay's dialed host once, up front, for a clear error before anything is accepted;
|
|
334
|
+
* a name that doesn't resolve is skipped (the connection fails on its own). The connections
|
|
335
|
+
* themselves still go through privateGuardLookup (App.guardRelays): this only says it early.
|
|
336
|
+
*/
|
|
337
|
+
export async function refusePrivateResolved(relays, what, opts = {}, resolve = dnsLookup) {
|
|
338
|
+
for (const { relay, dial } of relays) {
|
|
339
|
+
let host;
|
|
340
|
+
try {
|
|
341
|
+
host = new URL(dial).hostname;
|
|
342
|
+
}
|
|
343
|
+
catch {
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
if (host.startsWith("[") || parseV4(host) || parseV6(host))
|
|
347
|
+
continue; // literals: refusePrivateRelays
|
|
348
|
+
const addrs = await new Promise((r) => resolve(host, { all: true }, (err, a) => r(err ? [] : a)));
|
|
349
|
+
const bad = addrs.find((a) => isPrivateAddress(a.address));
|
|
350
|
+
if (bad) {
|
|
351
|
+
throw new CliError("private_relay", privateRelayMessage(`${what}'s relay`, new URL(relay).hostname, { ...opts, detail: `it resolves to ${bad.address}` }));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
/** What privateGuardLookup's error says, so a caller can tell it from an unreachable relay. */
|
|
356
|
+
export const PRIVATE_RESOLVE_MARK = "resolves to a loopback, link-local or private address";
|
|
357
|
+
/**
|
|
358
|
+
* A `lookup` for the socket that dials a code's relay: the name is resolved once, here, and refused
|
|
359
|
+
* when any address it resolves to is one isPrivateAddress refuses (`127.0.0.1.nip.io`,
|
|
360
|
+
* `localtest.me`, a rebinding name). The socket connects to the address handed back, so nothing is
|
|
361
|
+
* resolved again after the check. IP literals never reach a lookup: the parser checked those.
|
|
362
|
+
*/
|
|
363
|
+
export function privateGuardLookup(resolve = dnsLookup) {
|
|
364
|
+
return (hostname, options, callback) => {
|
|
365
|
+
const family = typeof options === "object" && options !== null && typeof options.family === "number" ? options.family : undefined;
|
|
366
|
+
resolve(hostname, { all: true, ...(family ? { family } : {}) }, (err, addrs) => {
|
|
367
|
+
if (err)
|
|
368
|
+
return callback(err, "", 0);
|
|
369
|
+
const bad = addrs.find((a) => isPrivateAddress(a.address));
|
|
370
|
+
if (bad || addrs.length === 0) {
|
|
371
|
+
const e = new Error(bad ? `${hostname} ${PRIVATE_RESOLVE_MARK} (${bad.address})` : `${hostname} has no address`);
|
|
372
|
+
e.code = bad ? "EP2PPRIVATE" : "ENOTFOUND";
|
|
373
|
+
return callback(e, "", 0);
|
|
374
|
+
}
|
|
375
|
+
if (typeof options === "object" && options !== null && options.all)
|
|
376
|
+
return callback(null, addrs);
|
|
377
|
+
callback(null, addrs[0].address, addrs[0].family);
|
|
378
|
+
});
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
/** lid = b32(H("p2p/v3/invite-id|" || s))[:26]. */
|
|
382
|
+
export function inviteLid(s) {
|
|
383
|
+
checkSecret(s);
|
|
384
|
+
const pre = enc.encode(INVITE_ID_PREFIX);
|
|
385
|
+
const buf = new Uint8Array(pre.length + s.length);
|
|
386
|
+
buf.set(pre);
|
|
387
|
+
buf.set(s, pre.length);
|
|
388
|
+
return b32Encode(sha256(buf)).slice(0, 26);
|
|
389
|
+
}
|
|
390
|
+
function inviteKey(s) {
|
|
391
|
+
return Buffer.from(hkdfSync("sha256", s, new Uint8Array(0), enc.encode(INVITE_KEY_INFO), 32));
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Members-only relays: the key a redeemer presents at auth (`auth.admit`). The inviter's
|
|
395
|
+
* `invite-put` carries only its hash, so the relay learns the key when it is used, and only
|
|
396
|
+
* someone who holds the code can use it.
|
|
397
|
+
*/
|
|
398
|
+
export function inviteAdmitKey(s) {
|
|
399
|
+
checkSecret(s);
|
|
400
|
+
return b64uEncode(new Uint8Array(hkdfSync("sha256", s, new Uint8Array(0), enc.encode(INVITE_ADMIT_INFO), 32)));
|
|
401
|
+
}
|
|
402
|
+
/** What `invite-put` carries: b64u(SHA-256(key)). */
|
|
403
|
+
export function inviteAdmitHash(key) {
|
|
404
|
+
return b64uEncode(sha256(b64uDecode(key, 32)));
|
|
405
|
+
}
|
|
406
|
+
const aad = (lid) => enc.encode(INVITE_AAD_PREFIX + lid);
|
|
407
|
+
/** Seals `bundle` (JSON) under the code: b64u(nonce || ciphertext || tag). */
|
|
408
|
+
export function sealInviteBundle(s, bundle) {
|
|
409
|
+
const lid = inviteLid(s);
|
|
410
|
+
const nonce = nodeRandom(12);
|
|
411
|
+
const pt = Buffer.from(JSON.stringify(bundle), "utf8");
|
|
412
|
+
const c = createCipheriv("chacha20-poly1305", inviteKey(s), nonce, { authTagLength: 16 });
|
|
413
|
+
c.setAAD(aad(lid), { plaintextLength: pt.length });
|
|
414
|
+
const ct = Buffer.concat([c.update(pt), c.final()]);
|
|
415
|
+
return b64uEncode(new Uint8Array(Buffer.concat([nonce, ct, c.getAuthTag()])));
|
|
416
|
+
}
|
|
417
|
+
/** Opens a blob sealed under `s`; throws `code_invalid` if it was sealed under anything else, or altered. */
|
|
418
|
+
export function openInviteBundle(s, blob) {
|
|
419
|
+
const bad = () => new CliError("code_invalid", "what the relay returned doesn't open under this code: it was altered or swapped (nothing was accepted)");
|
|
420
|
+
let raw;
|
|
421
|
+
try {
|
|
422
|
+
raw = b64uDecode(blob);
|
|
423
|
+
}
|
|
424
|
+
catch {
|
|
425
|
+
throw bad();
|
|
426
|
+
}
|
|
427
|
+
if (raw.length < 12 + 16 + 2)
|
|
428
|
+
throw bad();
|
|
429
|
+
const nonce = raw.subarray(0, 12);
|
|
430
|
+
const tag = raw.subarray(raw.length - 16);
|
|
431
|
+
const ct = raw.subarray(12, raw.length - 16);
|
|
432
|
+
try {
|
|
433
|
+
const d = createDecipheriv("chacha20-poly1305", inviteKey(s), nonce, { authTagLength: 16 });
|
|
434
|
+
d.setAAD(aad(inviteLid(s)), { plaintextLength: ct.length });
|
|
435
|
+
d.setAuthTag(tag);
|
|
436
|
+
const pt = Buffer.concat([d.update(ct), d.final()]).toString("utf8");
|
|
437
|
+
return JSON.parse(pt);
|
|
438
|
+
}
|
|
439
|
+
catch {
|
|
440
|
+
throw bad();
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
export function parseInviteBundle(v) {
|
|
444
|
+
const o = v;
|
|
445
|
+
const bad = () => new CliError("code_invalid", "the invite under this code is malformed");
|
|
446
|
+
if (typeof o !== "object" || o === null || o.v !== 1 || typeof o.invite !== "string")
|
|
447
|
+
throw bad();
|
|
448
|
+
const out = { v: 1, invite: o.invite };
|
|
449
|
+
if (o.circle !== undefined) {
|
|
450
|
+
const c = o.circle;
|
|
451
|
+
if (typeof c !== "object" || c === null || typeof c.head !== "string")
|
|
452
|
+
throw bad();
|
|
453
|
+
if (c.chain !== undefined && (!Array.isArray(c.chain) || c.chain.some((x) => typeof x !== "string") || c.chain.length > 512))
|
|
454
|
+
throw bad();
|
|
455
|
+
out.circle = { head: c.head, ...(c.chain ? { chain: c.chain } : {}) };
|
|
456
|
+
}
|
|
457
|
+
if (o.circles !== undefined) {
|
|
458
|
+
const cs = o.circles;
|
|
459
|
+
if (!out.circle || !Array.isArray(cs) || cs.length > CODE_MAX_CIRCLES - 1 || cs.some((c) => typeof c !== "object" || c === null || typeof c.head !== "string"))
|
|
460
|
+
throw bad();
|
|
461
|
+
if (cs.length > 0)
|
|
462
|
+
out.circles = cs.map((c) => ({ head: c.head }));
|
|
463
|
+
}
|
|
464
|
+
return out;
|
|
465
|
+
}
|