chirli 0.1.0 → 0.2.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/ARCHITECTURE.md +18 -20
- package/CONTRIBUTING.md +7 -3
- package/README.md +63 -55
- package/SECURITY.md +11 -11
- package/dist/allowlist.d.ts +3 -0
- package/dist/allowlist.js +9 -0
- package/dist/announcements.d.ts +4 -4
- package/dist/announcements.js +66 -34
- package/dist/attachments.d.ts +43 -0
- package/dist/attachments.js +340 -0
- package/dist/blossom.d.ts +2 -2
- package/dist/blossom.js +120 -66
- package/dist/bot-handlers.d.ts +9 -18
- package/dist/bot-handlers.js +125 -66
- package/dist/bot.d.ts +17 -20
- package/dist/bot.js +150 -55
- package/dist/client.d.ts +43 -39
- package/dist/client.js +476 -290
- package/dist/command-discovery.d.ts +2 -2
- package/dist/command-discovery.js +16 -7
- package/dist/command-schema.d.ts +2 -2
- package/dist/command-schema.js +62 -31
- package/dist/commands.d.ts +7 -6
- package/dist/commands.js +116 -51
- package/dist/defaults.js +13 -6
- package/dist/errors.d.ts +12 -12
- package/dist/errors.js +55 -18
- package/dist/inbox.d.ts +9 -7
- package/dist/inbox.js +107 -62
- package/dist/index.d.ts +25 -24
- package/dist/index.js +11 -11
- package/dist/key-sync.d.ts +4 -4
- package/dist/key-sync.js +88 -50
- package/dist/keys.d.ts +7 -6
- package/dist/keys.js +187 -93
- package/dist/message-storage-sqlite.d.ts +1 -1
- package/dist/message-storage-sqlite.js +32 -27
- package/dist/message-storage.d.ts +2 -2
- package/dist/messaging-metadata.d.ts +4 -4
- package/dist/messaging-metadata.js +73 -46
- package/dist/metadata.d.ts +3 -3
- package/dist/metadata.js +29 -15
- package/dist/modes.d.ts +7 -7
- package/dist/modes.js +53 -24
- package/dist/nip4e-compat.d.ts +2 -2
- package/dist/nip4e-compat.js +2 -2
- package/dist/nipcd.d.ts +14 -14
- package/dist/nipcd.js +225 -126
- package/dist/protocol.d.ts +7 -6
- package/dist/protocol.js +101 -54
- package/dist/public-inbox.d.ts +17 -15
- package/dist/public-inbox.js +129 -138
- package/dist/public-thread.d.ts +7 -4
- package/dist/public-thread.js +205 -88
- package/dist/relay-pool.d.ts +5 -5
- package/dist/relay-pool.js +172 -67
- package/dist/relay-roles.js +22 -13
- package/dist/relay.d.ts +11 -11
- package/dist/relay.js +194 -100
- package/dist/request-timing.d.ts +2 -2
- package/dist/request-timing.js +19 -7
- package/dist/session.js +7 -5
- package/dist/signer.d.ts +1 -1
- package/dist/signers/bunker-relays.d.ts +2 -2
- package/dist/signers/bunker-relays.js +16 -13
- package/dist/signers/bunker.d.ts +2 -2
- package/dist/signers/bunker.js +57 -37
- package/dist/signers/nsec.d.ts +2 -2
- package/dist/signers/nsec.js +8 -8
- package/dist/signers.d.ts +8 -8
- package/dist/signers.js +25 -18
- package/dist/storage-sqlite.d.ts +3 -1
- package/dist/storage-sqlite.js +110 -58
- package/dist/storage.d.ts +10 -6
- package/dist/store.d.ts +12 -8
- package/dist/store.js +156 -78
- package/docs/ACCESS.md +14 -25
- package/docs/API.md +125 -79
- package/docs/KEYS.md +3 -3
- package/docs/NIP-CD.md +29 -19
- package/docs/PUBLIC.md +33 -23
- package/docs/RECOVERY.md +7 -5
- package/docs/RELAYS.md +6 -6
- package/docs/SESSIONS.md +25 -21
- package/docs/SIGNERS.md +5 -3
- package/docs/STORAGE.md +15 -15
- package/docs/TUTORIAL.md +42 -39
- package/package.json +1 -37
- package/dist/inbound-policy.d.ts +0 -4
- package/dist/inbound-policy.js +0 -9
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { verifyEvent } from
|
|
2
|
-
import { AbstractSimplePool } from
|
|
3
|
-
import { websocketURL } from
|
|
4
|
-
import { DEFAULT_RELAY_TIMEOUTS, WebSocketTransport, subscribeRequests } from
|
|
1
|
+
import { verifyEvent } from "nostr-tools";
|
|
2
|
+
import { AbstractSimplePool, } from "nostr-tools/abstract-pool";
|
|
3
|
+
import { websocketURL } from "../metadata.js";
|
|
4
|
+
import { DEFAULT_RELAY_TIMEOUTS, WebSocketTransport, subscribeRequests, } from "../relay.js";
|
|
5
5
|
export function bunkerRelayURLs(values) {
|
|
6
6
|
if (!Array.isArray(values) || !values.length)
|
|
7
|
-
throw new TypeError(
|
|
7
|
+
throw new TypeError("At least one bunker relay is required");
|
|
8
8
|
if (!Array.from(values).every(websocketURL))
|
|
9
|
-
throw new TypeError(
|
|
10
|
-
return [...new Set(values.map(value => new URL(value).href))];
|
|
9
|
+
throw new TypeError("Invalid bunker relay URL");
|
|
10
|
+
return [...new Set(values.map((value) => new URL(value).href))];
|
|
11
11
|
}
|
|
12
12
|
/** Bridge the two pool operations used by nostr-tools' BunkerSigner. */
|
|
13
13
|
export class BunkerRelayPool extends AbstractSimplePool {
|
|
@@ -44,23 +44,26 @@ export class BunkerRelayPool extends AbstractSimplePool {
|
|
|
44
44
|
reasons.set(url, error.message);
|
|
45
45
|
if (!subscriptions.size) {
|
|
46
46
|
close();
|
|
47
|
-
params.onclose?.(urls.map(url => ({
|
|
47
|
+
params.onclose?.(urls.map((url) => ({
|
|
48
|
+
url,
|
|
49
|
+
reason: reasons.get(url) ?? "Subscription closed",
|
|
50
|
+
})));
|
|
48
51
|
}
|
|
49
52
|
};
|
|
50
|
-
const sub = subscribeRequests(this.transport, [url], filter, event => params.onevent?.(event), failed);
|
|
53
|
+
const sub = subscribeRequests(this.transport, [url], filter, (event) => params.onevent?.(event), failed);
|
|
51
54
|
subscriptions.set(url, sub);
|
|
52
55
|
void sub.ready.catch(failed);
|
|
53
56
|
return sub;
|
|
54
57
|
};
|
|
55
58
|
const publish = async (url, event) => {
|
|
56
59
|
if (closed || !urls.includes(url))
|
|
57
|
-
throw new Error(
|
|
60
|
+
throw new Error("Bunker subscription unavailable");
|
|
58
61
|
const sub = subscriptions.get(url) ?? open(url);
|
|
59
62
|
await sub.ready;
|
|
60
63
|
if (subscriptions.get(url) !== sub)
|
|
61
|
-
throw new Error(
|
|
64
|
+
throw new Error("Bunker subscription unavailable");
|
|
62
65
|
await this.transport.publish([url], event);
|
|
63
|
-
return
|
|
66
|
+
return "";
|
|
64
67
|
};
|
|
65
68
|
for (const url of urls)
|
|
66
69
|
open(url);
|
|
@@ -72,7 +75,7 @@ export class BunkerRelayPool extends AbstractSimplePool {
|
|
|
72
75
|
const publish = this.current;
|
|
73
76
|
return bunkerRelayURLs(relays).map(async (url) => {
|
|
74
77
|
if (!publish)
|
|
75
|
-
throw new Error(
|
|
78
|
+
throw new Error("Bunker subscription unavailable");
|
|
76
79
|
return publish(url, event);
|
|
77
80
|
});
|
|
78
81
|
}
|
package/dist/signers/bunker.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { Signer } from
|
|
1
|
+
import type { Signer } from "../signer.js";
|
|
2
2
|
export interface BunkerOptions {
|
|
3
3
|
clientSecret: Uint8Array;
|
|
4
4
|
timeoutMs?: number;
|
|
5
5
|
}
|
|
6
6
|
/** Lazy, shared NIP-46 handshake; closing releases this session, never logs out/deletes an account. */
|
|
7
|
-
export declare function bunkerSigner(input: string, options: BunkerOptions): Signer & Required<Pick<Signer,
|
|
7
|
+
export declare function bunkerSigner(input: string, options: BunkerOptions): Signer & Required<Pick<Signer, "close" | "nip04Encrypt" | "nip04Decrypt">>;
|
package/dist/signers/bunker.js
CHANGED
|
@@ -1,64 +1,76 @@
|
|
|
1
|
-
import { getPublicKey, getEventHash, verifyEvent } from
|
|
2
|
-
import { BunkerSigner } from
|
|
3
|
-
import { BunkerRelayPool, bunkerRelayURLs } from
|
|
4
|
-
import { hexKey } from
|
|
5
|
-
import { SignerError } from
|
|
1
|
+
import { getPublicKey, getEventHash, verifyEvent } from "nostr-tools";
|
|
2
|
+
import { BunkerSigner } from "nostr-tools/nip46";
|
|
3
|
+
import { BunkerRelayPool, bunkerRelayURLs } from "./bunker-relays.js";
|
|
4
|
+
import { hexKey } from "../protocol.js";
|
|
5
|
+
import { SignerError } from "../errors.js";
|
|
6
6
|
/** Lazy, shared NIP-46 handshake; closing releases this session, never logs out/deletes an account. */
|
|
7
7
|
export function bunkerSigner(input, options) {
|
|
8
8
|
let pointer;
|
|
9
9
|
try {
|
|
10
10
|
const url = new URL(input);
|
|
11
|
-
if (url.protocol !==
|
|
11
|
+
if (url.protocol !== "bunker:" ||
|
|
12
|
+
!hexKey(url.hostname) ||
|
|
13
|
+
url.username ||
|
|
14
|
+
url.password ||
|
|
15
|
+
url.port ||
|
|
16
|
+
(url.pathname && url.pathname !== "/") ||
|
|
17
|
+
url.hash) {
|
|
12
18
|
throw new Error();
|
|
13
19
|
}
|
|
14
20
|
pointer = {
|
|
15
21
|
pubkey: url.hostname,
|
|
16
|
-
relays: bunkerRelayURLs(url.searchParams.getAll(
|
|
17
|
-
secret: url.searchParams.get(
|
|
22
|
+
relays: bunkerRelayURLs(url.searchParams.getAll("relay")),
|
|
23
|
+
secret: url.searchParams.get("secret"),
|
|
18
24
|
};
|
|
19
25
|
}
|
|
20
26
|
catch {
|
|
21
|
-
throw new TypeError(
|
|
27
|
+
throw new TypeError("Invalid bunker connection URI");
|
|
22
28
|
}
|
|
23
|
-
if (!(options?.clientSecret instanceof Uint8Array) ||
|
|
24
|
-
|
|
29
|
+
if (!(options?.clientSecret instanceof Uint8Array) ||
|
|
30
|
+
options.clientSecret.length !== 32) {
|
|
31
|
+
throw new TypeError("clientSecret must be a 32-byte NIP-46 client private key");
|
|
25
32
|
}
|
|
26
33
|
const timeoutMs = options.timeoutMs ?? 15000;
|
|
27
34
|
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 300000) {
|
|
28
|
-
throw new TypeError(
|
|
35
|
+
throw new TypeError("timeoutMs must be in 1..300000");
|
|
29
36
|
}
|
|
30
37
|
const clientSecret = new Uint8Array(options.clientSecret);
|
|
31
38
|
try {
|
|
32
39
|
getPublicKey(clientSecret);
|
|
33
40
|
}
|
|
34
41
|
catch {
|
|
35
|
-
throw new TypeError(
|
|
42
|
+
throw new TypeError("clientSecret must be a valid NIP-46 client private key");
|
|
36
43
|
}
|
|
37
44
|
let closed = false, inner, connecting;
|
|
38
45
|
let pinnedAccount;
|
|
39
46
|
const candidates = new Map();
|
|
40
47
|
const check = () => {
|
|
41
48
|
if (closed) {
|
|
42
|
-
throw new SignerError(
|
|
49
|
+
throw new SignerError("session", "SIGNER_CLOSED");
|
|
43
50
|
}
|
|
44
51
|
};
|
|
45
52
|
async function bounded(operation, promise) {
|
|
46
53
|
let timer;
|
|
47
54
|
try {
|
|
48
|
-
const value = await Promise.race([
|
|
49
|
-
|
|
50
|
-
|
|
55
|
+
const value = await Promise.race([
|
|
56
|
+
promise,
|
|
57
|
+
new Promise((_resolve, reject) => {
|
|
58
|
+
timer = setTimeout(() => reject(new SignerError(operation, "SIGNER_TIMEOUT")), timeoutMs);
|
|
59
|
+
}),
|
|
60
|
+
]);
|
|
51
61
|
check();
|
|
52
62
|
return value;
|
|
53
63
|
}
|
|
54
64
|
catch (error) {
|
|
55
65
|
if (closed) {
|
|
56
|
-
throw new SignerError(operation,
|
|
66
|
+
throw new SignerError(operation, "SIGNER_CLOSED");
|
|
57
67
|
}
|
|
58
68
|
if (error instanceof SignerError) {
|
|
59
69
|
throw error;
|
|
60
70
|
}
|
|
61
|
-
throw new SignerError(operation, error instanceof Error && /timeout/i.test(error.message)
|
|
71
|
+
throw new SignerError(operation, error instanceof Error && /timeout/i.test(error.message)
|
|
72
|
+
? "SIGNER_TIMEOUT"
|
|
73
|
+
: "SIGNER_FAILED");
|
|
62
74
|
}
|
|
63
75
|
finally {
|
|
64
76
|
clearTimeout(timer);
|
|
@@ -73,8 +85,7 @@ export function bunkerSigner(input, options) {
|
|
|
73
85
|
if (inner === signer) {
|
|
74
86
|
inner = undefined;
|
|
75
87
|
}
|
|
76
|
-
await signer.close().catch(() => {
|
|
77
|
-
});
|
|
88
|
+
await signer.close().catch(() => { });
|
|
78
89
|
pool.destroy();
|
|
79
90
|
}
|
|
80
91
|
function ensure() {
|
|
@@ -92,7 +103,8 @@ export function bunkerSigner(input, options) {
|
|
|
92
103
|
}
|
|
93
104
|
const pool = new BunkerRelayPool(timeoutMs);
|
|
94
105
|
const candidate = BunkerSigner.fromBunker(clientSecret, {
|
|
95
|
-
...pointer,
|
|
106
|
+
...pointer,
|
|
107
|
+
relays: [...pointer.relays],
|
|
96
108
|
}, {
|
|
97
109
|
pool,
|
|
98
110
|
timeout: timeoutMs,
|
|
@@ -100,14 +112,14 @@ export function bunkerSigner(input, options) {
|
|
|
100
112
|
candidates.set(candidate, pool);
|
|
101
113
|
const pending = (async () => {
|
|
102
114
|
try {
|
|
103
|
-
await bounded(
|
|
115
|
+
await bounded("connect", candidate.connect({ name: "Chirli" }));
|
|
104
116
|
pointer.secret = null; // Reconnect with the same client key, never reuse the single-use token.
|
|
105
|
-
const account = await bounded(
|
|
117
|
+
const account = await bounded("get_public_key", candidate.getPublicKey());
|
|
106
118
|
if (!hexKey(account) || (pinnedAccount && pinnedAccount !== account)) {
|
|
107
|
-
throw new SignerError(
|
|
119
|
+
throw new SignerError("account binding");
|
|
108
120
|
}
|
|
109
121
|
pinnedAccount = account;
|
|
110
|
-
await bounded(
|
|
122
|
+
await bounded("switch_relays", candidate.switchRelays());
|
|
111
123
|
bunkerRelayURLs(candidate.bp.relays);
|
|
112
124
|
check();
|
|
113
125
|
inner = candidate;
|
|
@@ -125,13 +137,18 @@ export function bunkerSigner(input, options) {
|
|
|
125
137
|
connecting = pending;
|
|
126
138
|
return pending;
|
|
127
139
|
}
|
|
140
|
+
function accountKey() {
|
|
141
|
+
if (!pinnedAccount)
|
|
142
|
+
throw new SignerError("account binding");
|
|
143
|
+
return pinnedAccount;
|
|
144
|
+
}
|
|
128
145
|
async function request(operation, run) {
|
|
129
146
|
const signer = await ensure();
|
|
130
147
|
try {
|
|
131
148
|
return await bounded(operation, run(signer));
|
|
132
149
|
}
|
|
133
150
|
catch (error) {
|
|
134
|
-
if (error instanceof SignerError && error.code ===
|
|
151
|
+
if (error instanceof SignerError && error.code === "SIGNER_TIMEOUT") {
|
|
135
152
|
await discard(signer);
|
|
136
153
|
}
|
|
137
154
|
throw error;
|
|
@@ -141,25 +158,28 @@ export function bunkerSigner(input, options) {
|
|
|
141
158
|
async getPublicKey() {
|
|
142
159
|
await ensure();
|
|
143
160
|
check();
|
|
144
|
-
return
|
|
161
|
+
return accountKey();
|
|
145
162
|
},
|
|
146
163
|
async signEvent(template) {
|
|
147
164
|
const snapshot = structuredClone(template);
|
|
148
165
|
await ensure();
|
|
149
|
-
const account =
|
|
166
|
+
const account = accountKey();
|
|
150
167
|
const expected = getEventHash({
|
|
151
|
-
...snapshot,
|
|
168
|
+
...snapshot,
|
|
169
|
+
pubkey: account,
|
|
152
170
|
});
|
|
153
|
-
const signed = await request(
|
|
154
|
-
if (signed.pubkey !== account ||
|
|
155
|
-
|
|
171
|
+
const signed = await request("sign_event", (signer) => signer.signEvent(snapshot));
|
|
172
|
+
if (signed.pubkey !== account ||
|
|
173
|
+
signed.id !== expected ||
|
|
174
|
+
!verifyEvent(JSON.parse(JSON.stringify(signed)))) {
|
|
175
|
+
throw new SignerError("sign_event validation");
|
|
156
176
|
}
|
|
157
177
|
return signed;
|
|
158
178
|
},
|
|
159
|
-
nip44Encrypt: (peer, text) => request(
|
|
160
|
-
nip44Decrypt: (peer, content) => request(
|
|
161
|
-
nip04Encrypt: (peer, text) => request(
|
|
162
|
-
nip04Decrypt: (peer, content) => request(
|
|
179
|
+
nip44Encrypt: (peer, text) => request("nip44_encrypt", (signer) => signer.nip44Encrypt(peer, text)),
|
|
180
|
+
nip44Decrypt: (peer, content) => request("nip44_decrypt", (signer) => signer.nip44Decrypt(peer, content)),
|
|
181
|
+
nip04Encrypt: (peer, text) => request("nip04_encrypt", (signer) => signer.nip04Encrypt(peer, text)),
|
|
182
|
+
nip04Decrypt: (peer, content) => request("nip04_decrypt", (signer) => signer.nip04Decrypt(peer, content)),
|
|
163
183
|
async close() {
|
|
164
184
|
if (closed) {
|
|
165
185
|
return;
|
package/dist/signers/nsec.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { Signer } from
|
|
1
|
+
import type { Signer } from "../signer.js";
|
|
2
2
|
/** Local account signer. Secret bytes live only in this instance's closure. */
|
|
3
|
-
export declare function nsecSigner(nsec: string | Uint8Array): Signer & Required<Pick<Signer,
|
|
3
|
+
export declare function nsecSigner(nsec: string | Uint8Array): Signer & Required<Pick<Signer, "close" | "nip04Encrypt" | "nip04Decrypt">>;
|
package/dist/signers/nsec.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { finalizeEvent, getPublicKey, nip44 } from
|
|
2
|
-
import { decode } from
|
|
3
|
-
import * as nip04 from
|
|
4
|
-
import { SignerError } from
|
|
1
|
+
import { finalizeEvent, getPublicKey, nip44 } from "nostr-tools";
|
|
2
|
+
import { decode } from "nostr-tools/nip19";
|
|
3
|
+
import * as nip04 from "nostr-tools/nip04";
|
|
4
|
+
import { SignerError } from "../errors.js";
|
|
5
5
|
/** Local account signer. Secret bytes live only in this instance's closure. */
|
|
6
6
|
export function nsecSigner(nsec) {
|
|
7
7
|
let secret;
|
|
8
8
|
let pubkey;
|
|
9
9
|
try {
|
|
10
|
-
if (typeof nsec ===
|
|
10
|
+
if (typeof nsec === "string") {
|
|
11
11
|
const decoded = decode(nsec);
|
|
12
|
-
if (decoded.type !==
|
|
12
|
+
if (decoded.type !== "nsec") {
|
|
13
13
|
throw new Error();
|
|
14
14
|
}
|
|
15
15
|
secret = new Uint8Array(decoded.data);
|
|
@@ -21,12 +21,12 @@ export function nsecSigner(nsec) {
|
|
|
21
21
|
pubkey = getPublicKey(secret);
|
|
22
22
|
}
|
|
23
23
|
catch {
|
|
24
|
-
throw new TypeError(
|
|
24
|
+
throw new TypeError("Invalid nsec secret key");
|
|
25
25
|
}
|
|
26
26
|
let closed = false;
|
|
27
27
|
const check = () => {
|
|
28
28
|
if (closed) {
|
|
29
|
-
throw new SignerError(
|
|
29
|
+
throw new SignerError("local", "SIGNER_CLOSED");
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
32
|
return {
|
package/dist/signers.d.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { type Identity, type LocalIdentity } from
|
|
2
|
-
import type { Signer } from
|
|
3
|
-
import { nsecSigner } from
|
|
4
|
-
import { bunkerSigner } from
|
|
1
|
+
import { type Identity, type LocalIdentity } from "./protocol.js";
|
|
2
|
+
import type { Signer } from "./signer.js";
|
|
3
|
+
import { nsecSigner } from "./signers/nsec.js";
|
|
4
|
+
import { bunkerSigner } from "./signers/bunker.js";
|
|
5
5
|
export { nsecSigner, bunkerSigner };
|
|
6
|
-
export type { Signer } from
|
|
7
|
-
export type { BunkerOptions } from
|
|
6
|
+
export type { Signer } from "./signer.js";
|
|
7
|
+
export type { BunkerOptions } from "./signers/bunker.js";
|
|
8
8
|
/** Internal protocol capabilities resolved once from the account signer. */
|
|
9
9
|
export declare function identityFromSigner(signer: Signer, options: {
|
|
10
|
-
mode?:
|
|
10
|
+
mode?: "nip4e";
|
|
11
11
|
encryptionKey: Uint8Array;
|
|
12
12
|
oldEncryptionKeys?: Uint8Array[];
|
|
13
13
|
}): Promise<LocalIdentity>;
|
|
14
14
|
export declare function identityFromSigner(signer: Signer, options?: {
|
|
15
|
-
mode?:
|
|
15
|
+
mode?: "nip17" | "nip4e";
|
|
16
16
|
encryptionKey?: Uint8Array;
|
|
17
17
|
oldEncryptionKeys?: Uint8Array[];
|
|
18
18
|
}): Promise<Identity>;
|
package/dist/signers.js
CHANGED
|
@@ -1,44 +1,51 @@
|
|
|
1
|
-
import { getPublicKey } from
|
|
2
|
-
import { hexKey } from
|
|
3
|
-
import { SignerError } from
|
|
4
|
-
import { nsecSigner } from
|
|
5
|
-
import { bunkerSigner } from
|
|
1
|
+
import { getPublicKey } from "nostr-tools";
|
|
2
|
+
import { hexKey } from "./protocol.js";
|
|
3
|
+
import { SignerError } from "./errors.js";
|
|
4
|
+
import { nsecSigner } from "./signers/nsec.js";
|
|
5
|
+
import { bunkerSigner } from "./signers/bunker.js";
|
|
6
6
|
export { nsecSigner, bunkerSigner };
|
|
7
7
|
export async function identityFromSigner(signer, options = {}) {
|
|
8
|
-
if (options.mode !== undefined &&
|
|
9
|
-
|
|
8
|
+
if (options.mode !== undefined &&
|
|
9
|
+
options.mode !== "nip4e" &&
|
|
10
|
+
options.mode !== "nip17") {
|
|
11
|
+
throw new TypeError("Invalid signer identity mode");
|
|
10
12
|
}
|
|
11
13
|
// Supplying a messaging key is the legacy explicit split-key form.
|
|
12
|
-
const mode = options.mode ?? (options.encryptionKey ?
|
|
13
|
-
if (mode ===
|
|
14
|
-
throw new TypeError(
|
|
14
|
+
const mode = options.mode ?? (options.encryptionKey ? "nip4e" : "nip17");
|
|
15
|
+
if (mode === "nip17" && options.encryptionKey) {
|
|
16
|
+
throw new TypeError("nip17 signer identities do not accept split messaging keys");
|
|
15
17
|
}
|
|
16
18
|
// Every supported DM profile needs identity-key NIP-44, even with split outgoing keys.
|
|
17
|
-
for (const capability of [
|
|
18
|
-
|
|
19
|
+
for (const capability of [
|
|
20
|
+
"getPublicKey",
|
|
21
|
+
"signEvent",
|
|
22
|
+
"nip44Encrypt",
|
|
23
|
+
"nip44Decrypt",
|
|
24
|
+
]) {
|
|
25
|
+
if (typeof signer?.[capability] !== "function") {
|
|
19
26
|
throw new TypeError(`Signer must implement ${capability}`);
|
|
20
27
|
}
|
|
21
28
|
}
|
|
22
29
|
const pubkey = await signer.getPublicKey();
|
|
23
30
|
if (!hexKey(pubkey)) {
|
|
24
|
-
throw new SignerError(
|
|
31
|
+
throw new SignerError("get_public_key validation");
|
|
25
32
|
}
|
|
26
33
|
const crypto = {
|
|
27
34
|
publicKey: pubkey,
|
|
28
35
|
asynchronous: true,
|
|
29
36
|
encrypt: (text, peer) => signer.nip44Encrypt(peer, text),
|
|
30
|
-
decrypt: (content, peer) => signer.nip44Decrypt(peer, content)
|
|
37
|
+
decrypt: (content, peer) => signer.nip44Decrypt(peer, content),
|
|
31
38
|
};
|
|
32
39
|
const common = {
|
|
33
40
|
pubkey,
|
|
34
41
|
sign: (template) => signer.signEvent(template),
|
|
35
|
-
identityEncryption: crypto
|
|
42
|
+
identityEncryption: crypto,
|
|
36
43
|
};
|
|
37
|
-
if (mode ===
|
|
44
|
+
if (mode === "nip17") {
|
|
38
45
|
return {
|
|
39
46
|
...common,
|
|
40
47
|
messaging: crypto,
|
|
41
|
-
oldEncryptionKeys: options.oldEncryptionKeys
|
|
48
|
+
oldEncryptionKeys: options.oldEncryptionKeys,
|
|
42
49
|
};
|
|
43
50
|
}
|
|
44
51
|
if (options.encryptionKey) {
|
|
@@ -47,6 +54,6 @@ export async function identityFromSigner(signer, options = {}) {
|
|
|
47
54
|
return {
|
|
48
55
|
...common,
|
|
49
56
|
encryptionKey: options.encryptionKey,
|
|
50
|
-
oldEncryptionKeys: options.oldEncryptionKeys
|
|
57
|
+
oldEncryptionKeys: options.oldEncryptionKeys,
|
|
51
58
|
};
|
|
52
59
|
}
|
package/dist/storage-sqlite.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { StorageAdapter, StoragePayload, StoredMessage, StoredOutbox, MessageState, KeyEvidence, StoredDeclaration, MessageQuery, OutboxQuery, OutboxState, StorageRecord } from
|
|
1
|
+
import type { StorageAdapter, StoragePayload, StoredMessage, StoredOutbox, MessageState, KeyEvidence, StoredDeclaration, MessageQuery, OutboxQuery, OutboxState, StorageRecord } from "./storage.js";
|
|
2
2
|
/** Indexed relational storage. Query planning stays in the database. */
|
|
3
3
|
export declare class SQLiteStorage implements StorageAdapter {
|
|
4
4
|
private db;
|
|
@@ -6,6 +6,8 @@ export declare class SQLiteStorage implements StorageAdapter {
|
|
|
6
6
|
/** Remove a committed handoff only after the message backend has durably accepted it. */
|
|
7
7
|
discardStagedMessage(account: string, id: string): void;
|
|
8
8
|
insert(account: string, value: StorageRecord): boolean;
|
|
9
|
+
ensureDmActivation(pubkey: string, proposedAt: number): number;
|
|
10
|
+
claimInput(account: string, id: string): boolean;
|
|
9
11
|
queryMessages(account: string, query?: MessageQuery): StoredMessage[];
|
|
10
12
|
queryOutbox(account: string, query?: OutboxQuery): StoredOutbox[];
|
|
11
13
|
updateMessageState(account: string, id: string, state: MessageState): void;
|