@narumitw/pi-chat 0.1.6 → 0.1.7

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.
Files changed (49) hide show
  1. package/README.md +17 -34
  2. package/dist/chunks/{chat-view-EZ24L3LM.js → chat-view-7OBZGYZM.ts} +2 -2
  3. package/dist/chunks/chat-view-7OBZGYZM.ts.map +7 -0
  4. package/dist/chunks/{chunk-ZR2JESR5.js → chunk-7FD7DDSU.ts} +6 -36
  5. package/dist/chunks/chunk-7FD7DDSU.ts.map +7 -0
  6. package/dist/chunks/{chunk-XYZZR6DW.js → chunk-EOZIFFAD.ts} +6 -23
  7. package/dist/chunks/chunk-EOZIFFAD.ts.map +7 -0
  8. package/dist/chunks/{chunk-UE3UAOJX.js → chunk-LN3NAZRC.ts} +1 -1
  9. package/dist/chunks/{chunk-UE3UAOJX.js.map → chunk-LN3NAZRC.ts.map} +1 -1
  10. package/dist/chunks/{chunk-OQLAKS7O.js → chunk-MHSYNYS3.ts} +4 -7
  11. package/dist/chunks/chunk-MHSYNYS3.ts.map +7 -0
  12. package/dist/chunks/{chunk-UQPD3L6X.js → chunk-WUCSLYAK.ts} +1 -1
  13. package/dist/chunks/chunk-WUCSLYAK.ts.map +7 -0
  14. package/dist/chunks/{directory-network-EWFZFLGK.js → directory-network-6OPGA4EH.ts} +7 -19
  15. package/dist/chunks/directory-network-6OPGA4EH.ts.map +7 -0
  16. package/dist/chunks/{menu-2KEDM6NJ.js → menu-S7MZCVYM.ts} +5 -5
  17. package/dist/chunks/menu-S7MZCVYM.ts.map +7 -0
  18. package/dist/chunks/{network-MXCIGGQ2.js → network-Y4Z4JJ5J.ts} +3 -3
  19. package/dist/chunks/network-Y4Z4JJ5J.ts.map +7 -0
  20. package/dist/chunks/{widget-4OBABJDY.js → widget-QT53EBN6.ts} +4 -10
  21. package/dist/chunks/widget-QT53EBN6.ts.map +7 -0
  22. package/dist/index.ts +20 -66
  23. package/dist/index.ts.map +2 -2
  24. package/package.json +54 -55
  25. package/src/chat-session.ts +571 -594
  26. package/src/chat-view.ts +221 -226
  27. package/src/directory-network.ts +347 -365
  28. package/src/external.d.ts +56 -66
  29. package/src/identity.ts +74 -88
  30. package/src/menu.ts +535 -541
  31. package/src/network-contract.ts +5 -5
  32. package/src/network.ts +154 -157
  33. package/src/nickname.ts +6 -6
  34. package/src/pi-chat.ts +700 -774
  35. package/src/protocol.ts +296 -335
  36. package/src/public-room-directory.ts +276 -310
  37. package/src/room.ts +54 -58
  38. package/src/settings.ts +234 -259
  39. package/src/text.ts +57 -57
  40. package/src/widget.ts +56 -72
  41. package/dist/chunks/chat-view-EZ24L3LM.js.map +0 -7
  42. package/dist/chunks/chunk-OQLAKS7O.js.map +0 -7
  43. package/dist/chunks/chunk-UQPD3L6X.js.map +0 -7
  44. package/dist/chunks/chunk-XYZZR6DW.js.map +0 -7
  45. package/dist/chunks/chunk-ZR2JESR5.js.map +0 -7
  46. package/dist/chunks/directory-network-EWFZFLGK.js.map +0 -7
  47. package/dist/chunks/menu-2KEDM6NJ.js.map +0 -7
  48. package/dist/chunks/network-MXCIGGQ2.js.map +0 -7
  49. package/dist/chunks/widget-4OBABJDY.js.map +0 -7
package/src/external.d.ts CHANGED
@@ -1,83 +1,73 @@
1
1
  declare module "sodium-universal" {
2
- interface SodiumUniversal {
3
- crypto_sign_BYTES: number;
4
- crypto_sign_detached(signature: Uint8Array, message: Uint8Array, secretKey: Uint8Array): void;
5
- crypto_sign_verify_detached(
6
- signature: Uint8Array,
7
- message: Uint8Array,
8
- publicKey: Uint8Array,
9
- ): boolean;
10
- }
11
- const sodium: SodiumUniversal;
12
- export default sodium;
2
+ interface SodiumUniversal {
3
+ crypto_sign_BYTES: number;
4
+ crypto_sign_detached(signature: Uint8Array, message: Uint8Array, secretKey: Uint8Array): void;
5
+ crypto_sign_verify_detached(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): boolean;
6
+ }
7
+ const sodium: SodiumUniversal;
8
+ export default sodium;
13
9
  }
14
10
 
15
11
  declare module "hyperdht" {
16
- export interface HyperDhtKeyPair {
17
- publicKey: Buffer;
18
- secretKey: Buffer;
19
- }
12
+ export interface HyperDhtKeyPair {
13
+ publicKey: Buffer;
14
+ secretKey: Buffer;
15
+ }
20
16
 
21
- interface HyperDhtConstructor {
22
- keyPair(seed?: Uint8Array): HyperDhtKeyPair;
23
- }
17
+ interface HyperDhtConstructor {
18
+ keyPair(seed?: Uint8Array): HyperDhtKeyPair;
19
+ }
24
20
 
25
- const HyperDHT: HyperDhtConstructor;
26
- export default HyperDHT;
21
+ const HyperDHT: HyperDhtConstructor;
22
+ export default HyperDHT;
27
23
  }
28
24
 
29
25
  declare module "hyperswarm" {
30
- import type { Duplex } from "node:stream";
31
- import type { HyperDhtKeyPair } from "hyperdht";
26
+ import type { Duplex } from "node:stream";
27
+ import type { HyperDhtKeyPair } from "hyperdht";
32
28
 
33
- export interface PeerInfo {
34
- publicKey: Buffer;
35
- topics: Buffer[];
36
- ban(value?: boolean): void;
37
- }
29
+ export interface PeerInfo {
30
+ publicKey: Buffer;
31
+ topics: Buffer[];
32
+ ban(value?: boolean): void;
33
+ }
38
34
 
39
- export interface PeerDiscovery {
40
- flushed(): Promise<void>;
41
- destroy(): Promise<void>;
42
- refresh(options?: { client?: boolean; server?: boolean; limit?: number }): Promise<void>;
43
- }
35
+ export interface PeerDiscovery {
36
+ flushed(): Promise<void>;
37
+ destroy(): Promise<void>;
38
+ refresh(options?: { client?: boolean; server?: boolean; limit?: number }): Promise<void>;
39
+ }
44
40
 
45
- export interface HyperswarmOptions {
46
- keyPair?: HyperDhtKeyPair;
47
- seed?: Uint8Array;
48
- maxPeers?: number;
49
- firewall?: (remotePublicKey: Buffer) => boolean;
50
- dht?: unknown;
51
- bootstrap?: unknown[];
52
- }
41
+ export interface HyperswarmOptions {
42
+ keyPair?: HyperDhtKeyPair;
43
+ seed?: Uint8Array;
44
+ maxPeers?: number;
45
+ firewall?: (remotePublicKey: Buffer) => boolean;
46
+ dht?: unknown;
47
+ bootstrap?: unknown[];
48
+ }
53
49
 
54
- export default class Hyperswarm {
55
- constructor(options?: HyperswarmOptions);
56
- connections: Set<Duplex>;
57
- connecting: number;
58
- on(event: "connection", listener: (socket: Duplex, info: PeerInfo) => void): this;
59
- on(event: "update", listener: () => void): this;
60
- on(event: "error", listener: (error: Error) => void): this;
61
- join(
62
- topic: Buffer,
63
- options?: { client?: boolean; server?: boolean; limit?: number },
64
- ): PeerDiscovery;
65
- joinPeer(publicKey: Buffer): void;
66
- leavePeer(publicKey: Buffer): void;
67
- flush(): Promise<void>;
68
- destroy(options?: { force?: boolean }): Promise<void>;
69
- }
50
+ export default class Hyperswarm {
51
+ constructor(options?: HyperswarmOptions);
52
+ connections: Set<Duplex>;
53
+ connecting: number;
54
+ on(event: "connection", listener: (socket: Duplex, info: PeerInfo) => void): this;
55
+ on(event: "update", listener: () => void): this;
56
+ on(event: "error", listener: (error: Error) => void): this;
57
+ join(topic: Buffer, options?: { client?: boolean; server?: boolean; limit?: number }): PeerDiscovery;
58
+ joinPeer(publicKey: Buffer): void;
59
+ leavePeer(publicKey: Buffer): void;
60
+ flush(): Promise<void>;
61
+ destroy(options?: { force?: boolean }): Promise<void>;
62
+ }
70
63
  }
71
64
 
72
65
  declare module "hyperdht/testnet.js" {
73
- interface Testnet {
74
- nodes: unknown[];
75
- bootstrap: unknown[];
76
- createNode(options?: Record<string, unknown>): unknown;
77
- destroy(): Promise<void>;
78
- }
79
- export default function createTestnet(
80
- size?: number,
81
- options?: Record<string, unknown>,
82
- ): Promise<Testnet>;
66
+ interface Testnet {
67
+ nodes: unknown[];
68
+ bootstrap: unknown[];
69
+ createNode(options?: Record<string, unknown>): unknown;
70
+ destroy(): Promise<void>;
71
+ }
72
+ export default function createTestnet(size?: number, options?: Record<string, unknown>): Promise<Testnet>;
83
73
  }
package/src/identity.ts CHANGED
@@ -10,119 +10,105 @@ let hyperDhtImplementation: HyperDht | undefined;
10
10
  let sodiumImplementation: Sodium | undefined;
11
11
 
12
12
  function hyperDht(): HyperDht {
13
- hyperDhtImplementation ??= require("hyperdht") as HyperDht;
14
- return hyperDhtImplementation;
13
+ hyperDhtImplementation ??= require("hyperdht") as HyperDht;
14
+ return hyperDhtImplementation;
15
15
  }
16
16
 
17
17
  function sodium(): Sodium {
18
- sodiumImplementation ??= require("sodium-universal") as Sodium;
19
- return sodiumImplementation;
18
+ sodiumImplementation ??= require("sodium-universal") as Sodium;
19
+ return sodiumImplementation;
20
20
  }
21
21
 
22
22
  const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
23
23
 
24
24
  export interface ChatIdentity {
25
- seed: string;
26
- publicKey: Buffer;
27
- secretKey: Buffer;
28
- tag: string;
25
+ seed: string;
26
+ publicKey: Buffer;
27
+ secretKey: Buffer;
28
+ tag: string;
29
29
  }
30
30
 
31
31
  export function createIdentity(seed: Uint8Array): ChatIdentity {
32
- if (seed.byteLength !== 32) throw new Error("Pi Chat identity seed must be exactly 32-byte.");
33
- const stableSeed = Buffer.from(seed);
34
- const keyPair = hyperDht().keyPair(stableSeed);
35
- return {
36
- seed: stableSeed.toString("base64url"),
37
- publicKey: Buffer.from(keyPair.publicKey),
38
- secretKey: Buffer.from(keyPair.secretKey),
39
- tag: identityTag(keyPair.publicKey),
40
- };
32
+ if (seed.byteLength !== 32) throw new Error("Pi Chat identity seed must be exactly 32-byte.");
33
+ const stableSeed = Buffer.from(seed);
34
+ const keyPair = hyperDht().keyPair(stableSeed);
35
+ return {
36
+ seed: stableSeed.toString("base64url"),
37
+ publicKey: Buffer.from(keyPair.publicKey),
38
+ secretKey: Buffer.from(keyPair.secretKey),
39
+ tag: identityTag(keyPair.publicKey),
40
+ };
41
41
  }
42
42
 
43
43
  export function deriveScopedIdentity(identity: ChatIdentity, scope: string): ChatIdentity {
44
- if (!scope || Buffer.byteLength(scope, "utf8") > 256) {
45
- throw new Error("Pi Chat identity scope is invalid.");
46
- }
47
- const seed = Buffer.from(identity.seed, "base64url");
48
- return createIdentity(
49
- Buffer.from(
50
- hkdfSync(
51
- "sha256",
52
- seed,
53
- Buffer.from("pi-chat/scoped-identity/v2", "utf8"),
54
- Buffer.from(scope, "utf8"),
55
- 32,
56
- ),
57
- ),
58
- );
44
+ if (!scope || Buffer.byteLength(scope, "utf8") > 256) {
45
+ throw new Error("Pi Chat identity scope is invalid.");
46
+ }
47
+ const seed = Buffer.from(identity.seed, "base64url");
48
+ return createIdentity(
49
+ Buffer.from(
50
+ hkdfSync("sha256", seed, Buffer.from("pi-chat/scoped-identity/v2", "utf8"), Buffer.from(scope, "utf8"), 32),
51
+ ),
52
+ );
59
53
  }
60
54
 
61
55
  export function signIdentityPayload(identity: ChatIdentity, payload: Uint8Array): string {
62
- const implementation = sodium();
63
- const signature = Buffer.alloc(implementation.crypto_sign_BYTES);
64
- implementation.crypto_sign_detached(signature, payload, identity.secretKey);
65
- return signature.toString("base64url");
56
+ const implementation = sodium();
57
+ const signature = Buffer.alloc(implementation.crypto_sign_BYTES);
58
+ implementation.crypto_sign_detached(signature, payload, identity.secretKey);
59
+ return signature.toString("base64url");
66
60
  }
67
61
 
68
- export function verifyIdentityPayload(
69
- publicKey: Uint8Array,
70
- payload: Uint8Array,
71
- signatureValue: string,
72
- ): boolean {
73
- if (publicKey.byteLength !== 32 || !/^[A-Za-z0-9_-]{86}$/u.test(signatureValue)) return false;
74
- const signature = Buffer.from(signatureValue, "base64url");
75
- const implementation = sodium();
76
- if (signature.byteLength !== implementation.crypto_sign_BYTES) return false;
77
- try {
78
- return implementation.crypto_sign_verify_detached(signature, payload, publicKey);
79
- } catch {
80
- return false;
81
- }
62
+ export function verifyIdentityPayload(publicKey: Uint8Array, payload: Uint8Array, signatureValue: string): boolean {
63
+ if (publicKey.byteLength !== 32 || !/^[A-Za-z0-9_-]{86}$/u.test(signatureValue)) return false;
64
+ const signature = Buffer.from(signatureValue, "base64url");
65
+ const implementation = sodium();
66
+ if (signature.byteLength !== implementation.crypto_sign_BYTES) return false;
67
+ try {
68
+ return implementation.crypto_sign_verify_detached(signature, payload, publicKey);
69
+ } catch {
70
+ return false;
71
+ }
82
72
  }
83
73
 
84
74
  export function identityTag(publicKey: Uint8Array, characters = 12): string {
85
- if (characters < 12 || characters > 52) throw new Error("Identity tags use 12 to 52 characters.");
86
- const digest = createHash("sha256").update(publicKey).digest();
87
- let bits = 0;
88
- let bitCount = 0;
89
- let raw = "";
90
- for (const byte of digest) {
91
- bits = (bits << 8) | byte;
92
- bitCount += 8;
93
- while (bitCount >= 5 && raw.length < characters) {
94
- bitCount -= 5;
95
- raw += CROCKFORD[(bits >>> bitCount) & 31];
96
- bits &= (1 << bitCount) - 1;
97
- }
98
- if (raw.length === characters) break;
99
- }
100
- return raw.match(/.{1,4}/gu)?.join("-") ?? raw;
75
+ if (characters < 12 || characters > 52) throw new Error("Identity tags use 12 to 52 characters.");
76
+ const digest = createHash("sha256").update(publicKey).digest();
77
+ let bits = 0;
78
+ let bitCount = 0;
79
+ let raw = "";
80
+ for (const byte of digest) {
81
+ bits = (bits << 8) | byte;
82
+ bitCount += 8;
83
+ while (bitCount >= 5 && raw.length < characters) {
84
+ bitCount -= 5;
85
+ raw += CROCKFORD[(bits >>> bitCount) & 31];
86
+ bits &= (1 << bitCount) - 1;
87
+ }
88
+ if (raw.length === characters) break;
89
+ }
90
+ return raw.match(/.{1,4}/gu)?.join("-") ?? raw;
101
91
  }
102
92
 
103
- export function formatIdentityLabel(
104
- nickname: string,
105
- publicKey: Uint8Array,
106
- characters = 12,
107
- ): string {
108
- return `${nickname}~${identityTag(publicKey, characters)}`;
93
+ export function formatIdentityLabel(nickname: string, publicKey: Uint8Array, characters = 12): string {
94
+ return `${nickname}~${identityTag(publicKey, characters)}`;
109
95
  }
110
96
 
111
97
  export function uniqueIdentityTags(publicKeys: readonly Uint8Array[]): Map<string, string> {
112
- const fullKeys = publicKeys.map((key) => Buffer.from(key).toString("hex"));
113
- const result = new Map<string, string>();
114
- for (let length = 12; length <= 52; length += 4) {
115
- const grouped = new Map<string, string[]>();
116
- for (const [index, publicKey] of publicKeys.entries()) {
117
- const tag = identityTag(publicKey, length);
118
- const fullKey = fullKeys[index] ?? Buffer.from(publicKey).toString("hex");
119
- grouped.set(tag, [...(grouped.get(tag) ?? []), fullKey]);
120
- }
121
- for (const [tag, keys] of grouped) {
122
- const onlyKey = keys.length === 1 ? keys[0] : undefined;
123
- if (onlyKey && !result.has(onlyKey)) result.set(onlyKey, tag);
124
- }
125
- if (result.size === publicKeys.length) break;
126
- }
127
- return result;
98
+ const fullKeys = publicKeys.map((key) => Buffer.from(key).toString("hex"));
99
+ const result = new Map<string, string>();
100
+ for (let length = 12; length <= 52; length += 4) {
101
+ const grouped = new Map<string, string[]>();
102
+ for (const [index, publicKey] of publicKeys.entries()) {
103
+ const tag = identityTag(publicKey, length);
104
+ const fullKey = fullKeys[index] ?? Buffer.from(publicKey).toString("hex");
105
+ grouped.set(tag, [...(grouped.get(tag) ?? []), fullKey]);
106
+ }
107
+ for (const [tag, keys] of grouped) {
108
+ const onlyKey = keys.length === 1 ? keys[0] : undefined;
109
+ if (onlyKey && !result.has(onlyKey)) result.set(onlyKey, tag);
110
+ }
111
+ if (result.size === publicKeys.length) break;
112
+ }
113
+ return result;
128
114
  }