@konemono/nostr-login 1.9.14 → 1.10.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.
@@ -1,123 +0,0 @@
1
- import { EventEmitter } from 'tseep';
2
- import { Nip46Client } from './Nip46Client';
3
- import { PrivateKeySigner } from '../Signer';
4
-
5
- export class Nip46Adapter extends EventEmitter {
6
- private client: Nip46Client;
7
- private localSigner: PrivateKeySigner;
8
- public userPubkey: string = '';
9
- public remotePubkey: string;
10
-
11
- constructor(client: Nip46Client, localSigner: PrivateKeySigner) {
12
- super();
13
- this.client = client;
14
- this.localSigner = localSigner;
15
- this.remotePubkey = (client as any).remotePubkey || '';
16
-
17
- // forward events
18
- this.client.on('authUrl', (url: string) => {
19
- this.emit('authUrl', url);
20
- });
21
- this.client.on('response', ({ response, pubkey }: any) => {
22
- this.emit('response', response, pubkey);
23
- });
24
- }
25
-
26
- async initUserPubkey(hintPubkey?: string) {
27
- if (this.userPubkey) throw new Error('Already called initUserPubkey');
28
- if (hintPubkey) {
29
- this.userPubkey = hintPubkey;
30
- return;
31
- }
32
-
33
- const res = await this.client.sendRequest('get_public_key', []);
34
- if (!res) throw new Error('No public key returned');
35
- this.userPubkey = res;
36
- }
37
-
38
- async listen(nostrConnectSecret: string): Promise<string> {
39
- return new Promise<string>((ok, err) => {
40
- const onResponse = ({ response, pubkey }: any) => {
41
- if (!response) return;
42
- if (response.result === 'auth_url') return;
43
- if (response.result === 'ack' || response.result === nostrConnectSecret) {
44
- this.client.off('response', onResponse);
45
- ok(pubkey);
46
- }
47
- };
48
-
49
- this.client.on('response', onResponse);
50
-
51
- // also add a timeout
52
- setTimeout(() => {
53
- this.client.off('response', onResponse);
54
- err(new Error('Listen timeout'));
55
- }, 30000);
56
- });
57
- }
58
-
59
- async connect(token?: string, perms?: string) {
60
- const result = await this.client.sendRequest('connect', [this.localSigner.pubkey, token || '', perms || '']);
61
- if (result !== 'ack') throw new Error(result || 'connect failed');
62
- }
63
-
64
- async setListenReply(reply: any, nostrConnectSecret: string) {
65
- // reply is expected to be a raw event object; we'll try to parse its content
66
- // Attempt to decrypt via the client flow by treating it as a response
67
- try {
68
- const decoded = reply && reply.content ? JSON.parse(reply.content) : null;
69
- if (!decoded) throw new Error('Bad reply');
70
- if (decoded.result === nostrConnectSecret) {
71
- this.userPubkey = reply.pubkey;
72
- } else {
73
- throw new Error('Bad reply');
74
- }
75
- } catch (e) {
76
- throw new Error('Failed to set listen reply');
77
- }
78
- }
79
-
80
- async createAccount2({ bunkerPubkey, name, domain, perms = '' }: { bunkerPubkey: string; name: string; domain: string; perms?: string }) {
81
- const params = [name, domain, '', perms];
82
-
83
- const r = await this.client.sendRequest('create_account', params);
84
- if (!r) throw new Error('create_account failed');
85
- if (r === 'error') throw new Error('create_account error');
86
- return r;
87
- }
88
-
89
- async encrypt(recipientPubkey: string, plaintext: string) {
90
- const r = await this.client.sendRequest('nip04_encrypt', [recipientPubkey, plaintext]);
91
- return r;
92
- }
93
-
94
- async decrypt(recipientPubkey: string, ciphertext: string) {
95
- const r = await this.client.sendRequest('nip04_decrypt', [recipientPubkey, ciphertext]);
96
- return r;
97
- }
98
-
99
- async sign(event: any) {
100
- const r = await this.client.sendRequest('sign_event', [JSON.stringify(event)]);
101
- try {
102
- const parsed = typeof r === 'string' ? JSON.parse(r) : r;
103
- if (parsed && parsed.sig) return parsed.sig;
104
- } catch (e) {
105
- // not JSON
106
- }
107
- return r;
108
- }
109
-
110
- // provide rpc compatibility
111
- get rpc() {
112
- return {
113
- sendRequest: async (remotePubkey: string, method: string, params: string[], kind: number, cb: (res: any) => void) => {
114
- try {
115
- const res = await this.client.sendRequest(method, params);
116
- cb({ result: res });
117
- } catch (err: any) {
118
- cb({ error: err.message });
119
- }
120
- },
121
- };
122
- }
123
- }
@@ -1,248 +0,0 @@
1
- import { SimplePool, Event as NostrEvent, getPublicKey, nip04 } from 'nostr-tools';
2
- import { Nip44 } from '../../utils/nip44';
3
- import { EventEmitter } from 'tseep';
4
- import { Nip46Request, Nip46Response, PendingRequest, Nip46ClientOptions } from './types';
5
- import { getEventHash, getSignature } from 'nostr-tools';
6
-
7
- export class Nip46Client extends EventEmitter {
8
- private pool: SimplePool;
9
- private localPrivateKey: string;
10
- private remotePubkey: string;
11
- private relays: string[];
12
- private pendingRequests: Map<string, PendingRequest> = new Map();
13
- private defaultTimeoutMs: number;
14
- private useNip44: boolean;
15
- private subscription: any = null;
16
- private isSubscribed: boolean = false;
17
- private nip44Codec: Nip44 = new Nip44();
18
-
19
- constructor(options: Nip46ClientOptions) {
20
- super();
21
- this.pool = new SimplePool();
22
- this.localPrivateKey = options.localPrivateKey;
23
- this.remotePubkey = options.remotePubkey;
24
- this.relays = options.relays;
25
- this.defaultTimeoutMs = options.timeoutMs || 30000;
26
- this.useNip44 = options.useNip44 || false;
27
- }
28
-
29
- get localPubkey(): string {
30
- return getPublicKey(this.localPrivateKey);
31
- }
32
-
33
- /**
34
- * NIP-46リクエストを送信
35
- */
36
- async sendRequest(method: string, params: string[] = [], timeoutMs?: number): Promise<string> {
37
- const timeout = timeoutMs || this.defaultTimeoutMs;
38
- const id = this.generateId();
39
- const request: Nip46Request = { id, method, params };
40
-
41
- console.log('[Nip46Client] Sending request:', { id, method, params });
42
-
43
- // レスポンス購読を開始(まだの場合)
44
- if (!this.isSubscribed) {
45
- this.subscribeToResponses();
46
- }
47
-
48
- // リクエストイベントを作成・送信
49
- await this.publishRequest(request);
50
-
51
- // レスポンスを待つPromise
52
- return new Promise<string>((resolve, reject) => {
53
- const timer = setTimeout(() => {
54
- this.pendingRequests.delete(id);
55
- const error = new Error(`Request ${id} (${method}) timed out after ${timeout}ms`);
56
- console.error('[Nip46Client]', error.message);
57
- reject(error);
58
- }, timeout);
59
-
60
- this.pendingRequests.set(id, {
61
- resolve,
62
- reject,
63
- timer,
64
- method,
65
- });
66
- });
67
- }
68
-
69
- /**
70
- * リクエストイベントを作成して送信
71
- */
72
- private async publishRequest(request: Nip46Request): Promise<void> {
73
- const content = JSON.stringify(request);
74
-
75
- // 暗号化
76
- const encrypted = this.useNip44
77
- ? await this.nip44Codec.encrypt(this.localPrivateKey, this.remotePubkey, content)
78
- : await nip04.encrypt(this.localPrivateKey, this.remotePubkey, content);
79
-
80
- // イベント作成
81
- const event: NostrEvent = {
82
- kind: 24133,
83
- pubkey: this.localPubkey,
84
- created_at: Math.floor(Date.now() / 1000),
85
- tags: [['p', this.remotePubkey]],
86
- content: encrypted,
87
- id: '',
88
- sig: '',
89
- };
90
-
91
- // ID計算
92
- event.id = getEventHash(event);
93
-
94
- // 署名
95
- event.sig = getSignature(event, this.localPrivateKey);
96
-
97
- // リレーに送信
98
- await Promise.any(this.pool.publish(this.relays, event));
99
- console.log('[Nip46Client] Request published:', request.id);
100
- }
101
-
102
- /**
103
- * レスポンスイベントを購読
104
- */
105
- private subscribeToResponses(): void {
106
- if (this.isSubscribed) return;
107
-
108
- const filter = {
109
- 'kinds': [24133],
110
- '#p': [this.localPubkey],
111
- 'since': Math.floor(Date.now() / 1000) - 60,
112
- };
113
-
114
- console.log('[Nip46Client] Subscribing to responses');
115
-
116
- // SimplePool subscription
117
- this.subscription = this.pool.sub(this.relays, [filter]);
118
- this.subscription.on('event', async (event: NostrEvent) => {
119
- await this.handleResponseEvent(event);
120
- });
121
- this.subscription.on('eose', () => {
122
- console.log('[Nip46Client] EOSE received');
123
- });
124
-
125
- this.isSubscribed = true;
126
- }
127
-
128
- /**
129
- * レスポンスイベントを処理
130
- */
131
- private async handleResponseEvent(event: NostrEvent): Promise<void> {
132
- try {
133
- // 復号化
134
- const decrypted = this.isNip04(event.content)
135
- ? await nip04.decrypt(this.localPrivateKey, event.pubkey, event.content)
136
- : await this.nip44Codec.decrypt(this.localPrivateKey, event.pubkey, event.content);
137
-
138
- const response: Nip46Response = JSON.parse(decrypted);
139
-
140
- console.log('[Nip46Client] Response received:', {
141
- id: response.id,
142
- hasResult: !!response.result,
143
- hasError: !!response.error,
144
- });
145
-
146
- // Emit response event for consumers (include sender pubkey)
147
- this.emit('response', { response, pubkey: event.pubkey });
148
-
149
- // auth_urlの特別処理
150
- if (response.result === 'auth_url') {
151
- console.log('[Nip46Client] Auth URL received:', response.error);
152
- this.emit('authUrl', response.error);
153
- return;
154
- }
155
-
156
- // 保留中のリクエストを解決
157
- const pending = this.pendingRequests.get(response.id);
158
- if (pending) {
159
- clearTimeout(pending.timer);
160
- this.pendingRequests.delete(response.id);
161
-
162
- if (response.error) {
163
- console.error('[Nip46Client] Request failed:', {
164
- id: response.id,
165
- method: pending.method,
166
- error: response.error,
167
- });
168
- pending.reject(new Error(response.error));
169
- } else if (response.result !== undefined) {
170
- console.log('[Nip46Client] Request succeeded:', {
171
- id: response.id,
172
- method: pending.method,
173
- });
174
- pending.resolve(response.result);
175
- } else {
176
- pending.reject(new Error('Invalid response: no result or error'));
177
- }
178
- } else {
179
- console.warn('[Nip46Client] Received response for unknown request:', response.id);
180
- }
181
- } catch (error) {
182
- console.error('[Nip46Client] Failed to parse response event:', error);
183
- }
184
- }
185
-
186
- /**
187
- * NIP-04かNIP-44かを判定
188
- */
189
- private isNip04(ciphertext: string): boolean {
190
- const l = ciphertext.length;
191
- if (l < 28) return false;
192
- return ciphertext[l - 28] === '?' && ciphertext[l - 27] === 'i' && ciphertext[l - 26] === 'v' && ciphertext[l - 25] === '=';
193
- }
194
-
195
- /**
196
- * ランダムIDを生成
197
- */
198
- private generateId(): string {
199
- return Math.random().toString(36).substring(2, 15);
200
- }
201
-
202
- /**
203
- * NIP-44を使用するかどうかを設定
204
- */
205
- setUseNip44(useNip44: boolean): void {
206
- this.useNip44 = useNip44;
207
- }
208
-
209
- /**
210
- * クリーンアップ
211
- */
212
- cleanup(): void {
213
- console.log('[Nip46Client] Cleaning up');
214
-
215
- // すべての保留中リクエストをキャンセル
216
- for (const [id, pending] of this.pendingRequests) {
217
- clearTimeout(pending.timer);
218
- pending.reject(new Error('Client cleanup'));
219
- }
220
- this.pendingRequests.clear();
221
-
222
- // 購読を停止
223
- if (this.subscription) {
224
- // SimplePool subscriptions may use `unsub` or `close`
225
- try {
226
- if (typeof this.subscription.unsub === 'function') this.subscription.unsub();
227
- if (typeof this.subscription.close === 'function') this.subscription.close();
228
- } catch (e) {
229
- // ignore
230
- }
231
- this.subscription = null;
232
- this.isSubscribed = false;
233
- }
234
-
235
- // リレー接続を閉じる
236
- this.pool.close(this.relays);
237
-
238
- // イベントリスナーをクリア
239
- this.removeAllListeners();
240
- }
241
-
242
- /**
243
- * 接続状態を確認
244
- */
245
- isConnected(): boolean {
246
- return this.isSubscribed;
247
- }
248
- }
@@ -1,26 +0,0 @@
1
- export interface Nip46Request {
2
- id: string;
3
- method: string;
4
- params: string[];
5
- }
6
-
7
- export interface Nip46Response {
8
- id: string;
9
- result?: string;
10
- error?: string;
11
- }
12
-
13
- export interface PendingRequest {
14
- resolve: (result: string) => void;
15
- reject: (error: Error) => void;
16
- timer: NodeJS.Timeout;
17
- method: string;
18
- }
19
-
20
- export interface Nip46ClientOptions {
21
- localPrivateKey: string;
22
- remotePubkey: string;
23
- relays: string[];
24
- timeoutMs?: number;
25
- useNip44?: boolean;
26
- }
package/vitest.config.ts DELETED
@@ -1,9 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- globals: true,
6
- environment: 'node',
7
- include: ['src/**/*.test.ts'],
8
- },
9
- });