@narumitw/pi-chat 0.1.5 → 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.
- package/README.md +47 -64
- package/dist/chunks/{chat-view-EZ24L3LM.js → chat-view-7OBZGYZM.ts} +2 -2
- package/dist/chunks/chat-view-7OBZGYZM.ts.map +7 -0
- package/dist/chunks/{chunk-ZR2JESR5.js → chunk-7FD7DDSU.ts} +6 -36
- package/dist/chunks/chunk-7FD7DDSU.ts.map +7 -0
- package/dist/chunks/{chunk-XYZZR6DW.js → chunk-EOZIFFAD.ts} +6 -23
- package/dist/chunks/chunk-EOZIFFAD.ts.map +7 -0
- package/dist/chunks/{chunk-UE3UAOJX.js → chunk-LN3NAZRC.ts} +1 -1
- package/dist/chunks/{chunk-UE3UAOJX.js.map → chunk-LN3NAZRC.ts.map} +1 -1
- package/dist/chunks/{chunk-OQLAKS7O.js → chunk-MHSYNYS3.ts} +4 -7
- package/dist/chunks/chunk-MHSYNYS3.ts.map +7 -0
- package/dist/chunks/{chunk-UQPD3L6X.js → chunk-WUCSLYAK.ts} +1 -1
- package/dist/chunks/chunk-WUCSLYAK.ts.map +7 -0
- package/dist/chunks/{directory-network-EWFZFLGK.js → directory-network-6OPGA4EH.ts} +7 -19
- package/dist/chunks/directory-network-6OPGA4EH.ts.map +7 -0
- package/dist/chunks/{menu-2XYZAYXF.js → menu-S7MZCVYM.ts} +7 -7
- package/dist/chunks/menu-S7MZCVYM.ts.map +7 -0
- package/dist/chunks/{network-MXCIGGQ2.js → network-Y4Z4JJ5J.ts} +3 -3
- package/dist/chunks/network-Y4Z4JJ5J.ts.map +7 -0
- package/dist/chunks/{widget-4OBABJDY.js → widget-QT53EBN6.ts} +4 -10
- package/dist/chunks/widget-QT53EBN6.ts.map +7 -0
- package/dist/index.ts +21 -69
- package/dist/index.ts.map +2 -2
- package/package.json +54 -58
- package/src/chat-session.ts +571 -594
- package/src/chat-view.ts +221 -226
- package/src/directory-network.ts +347 -365
- package/src/external.d.ts +56 -66
- package/src/identity.ts +74 -88
- package/src/menu.ts +535 -541
- package/src/network-contract.ts +5 -5
- package/src/network.ts +154 -157
- package/src/nickname.ts +6 -6
- package/src/pi-chat.ts +700 -777
- package/src/protocol.ts +296 -335
- package/src/public-room-directory.ts +276 -310
- package/src/room.ts +54 -58
- package/src/settings.ts +234 -259
- package/src/text.ts +57 -57
- package/src/widget.ts +56 -72
- package/dist/chunks/chat-view-EZ24L3LM.js.map +0 -7
- package/dist/chunks/chunk-OQLAKS7O.js.map +0 -7
- package/dist/chunks/chunk-UQPD3L6X.js.map +0 -7
- package/dist/chunks/chunk-XYZZR6DW.js.map +0 -7
- package/dist/chunks/chunk-ZR2JESR5.js.map +0 -7
- package/dist/chunks/directory-network-EWFZFLGK.js.map +0 -7
- package/dist/chunks/menu-2XYZAYXF.js.map +0 -7
- package/dist/chunks/network-MXCIGGQ2.js.map +0 -7
- 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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
export interface HyperDhtKeyPair {
|
|
13
|
+
publicKey: Buffer;
|
|
14
|
+
secretKey: Buffer;
|
|
15
|
+
}
|
|
20
16
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
interface HyperDhtConstructor {
|
|
18
|
+
keyPair(seed?: Uint8Array): HyperDhtKeyPair;
|
|
19
|
+
}
|
|
24
20
|
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
const HyperDHT: HyperDhtConstructor;
|
|
22
|
+
export default HyperDHT;
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
declare module "hyperswarm" {
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
import type { Duplex } from "node:stream";
|
|
27
|
+
import type { HyperDhtKeyPair } from "hyperdht";
|
|
32
28
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
29
|
+
export interface PeerInfo {
|
|
30
|
+
publicKey: Buffer;
|
|
31
|
+
topics: Buffer[];
|
|
32
|
+
ban(value?: boolean): void;
|
|
33
|
+
}
|
|
38
34
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
14
|
-
|
|
13
|
+
hyperDhtImplementation ??= require("hyperdht") as HyperDht;
|
|
14
|
+
return hyperDhtImplementation;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
function sodium(): Sodium {
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
}
|