@konemono/nostr-login 1.11.1 → 1.11.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konemono/nostr-login",
3
- "version": "1.11.1",
3
+ "version": "1.11.3",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
@@ -1,16 +1,15 @@
1
1
  import { localStorageAddAccount, bunkerUrlToInfo, isBunkerUrl, fetchProfile, getBunkerUrl, localStorageRemoveCurrentAccount, createProfile, getIcon } from '../utils';
2
2
  import { ConnectionString, Info } from 'nostr-login-components/dist/types/types';
3
- import { generateSecretKey, getEventHash, getPublicKey, nip19 } from 'nostr-tools';
3
+ import { generatePrivateKey, getEventHash, getPublicKey, nip19 } from 'nostr-tools';
4
4
  import { NostrLoginAuthOptions, Response } from '../types';
5
5
  import NDK, { NDKEvent, NDKNip46Signer, NDKRpcResponse, NDKUser, NostrEvent } from '@nostr-dev-kit/ndk';
6
6
  import { NostrParams } from './';
7
7
  import { EventEmitter } from 'tseep';
8
8
  import { Signer } from './Nostr';
9
9
  import { Nip44 } from '../utils/nip44';
10
- import { IframeNostrRpc, Nip46Signer, ReadyListener, ensureNIP46Connection } from './Nip46';
10
+ import { IframeNostrRpc, Nip46Signer, ReadyListener } from './Nip46';
11
11
  import { PrivateKeySigner } from './Signer';
12
12
  import { DEFAULT_NIP46_RELAYS } from '../const';
13
- import { bytesToHex, hexToBytes } from 'nostr-tools/lib/types/utils';
14
13
 
15
14
  const OUTBOX_RELAYS = ['wss://user.kindpag.es', 'wss://purplepag.es', 'wss://relay.nos.social'];
16
15
  const NOSTRCONNECT_APPS: ConnectionString[] = [
@@ -64,8 +63,6 @@ class AuthNostrService extends EventEmitter implements Signer {
64
63
  this.params = params;
65
64
  this.ndk = new NDK({
66
65
  enableOutboxModel: false,
67
- autoConnectUserRelays: false,
68
- autoFetchUserMutelist: false,
69
66
  });
70
67
 
71
68
  this.profileNdk = new NDK({
@@ -92,13 +89,13 @@ class AuthNostrService extends EventEmitter implements Signer {
92
89
  if (this.signerPromise) {
93
90
  try {
94
91
  await this.signerPromise;
95
- } catch { }
92
+ } catch {}
96
93
  }
97
94
 
98
95
  if (this.readyPromise) {
99
96
  try {
100
97
  await this.readyPromise;
101
- } catch { }
98
+ } catch {}
102
99
  }
103
100
  }
104
101
 
@@ -172,10 +169,10 @@ class AuthNostrService extends EventEmitter implements Signer {
172
169
  }
173
170
 
174
171
  public async createNostrConnect() {
175
- this.nostrConnectKey = bytesToHex(generateSecretKey());
172
+ this.nostrConnectKey = generatePrivateKey();
176
173
  this.nostrConnectSecret = Math.random().toString(36).substring(7);
177
174
 
178
- const pubkey = getPublicKey(hexToBytes(this.nostrConnectKey));
175
+ const pubkey = getPublicKey(this.nostrConnectKey);
179
176
  const meta = {
180
177
  name: encodeURIComponent(document.location.host),
181
178
  url: encodeURIComponent(document.location.origin),
@@ -234,8 +231,8 @@ class AuthNostrService extends EventEmitter implements Signer {
234
231
 
235
232
  public async localSignup(name: string, sk?: string) {
236
233
  const signup = !sk;
237
- sk = sk || bytesToHex(generateSecretKey());
238
- const pubkey = getPublicKey(hexToBytes(sk));
234
+ sk = sk || generatePrivateKey();
235
+ const pubkey = getPublicKey(sk);
239
236
  const info: Info = {
240
237
  pubkey,
241
238
  sk,
@@ -261,7 +258,7 @@ class AuthNostrService extends EventEmitter implements Signer {
261
258
 
262
259
  // for local we export our existing key
263
260
  if (!this.localSigner || this.params.userInfo?.authMethod !== 'local') throw new Error('Most be local keys');
264
- return url + '#import=' + nip19.nsecEncode(hexToBytes(this.localSigner.privateKey!));
261
+ return url + '#import=' + nip19.nsecEncode(this.localSigner.privateKey!);
265
262
  }
266
263
 
267
264
  public async importAndConnect(cs: ConnectionString) {
@@ -364,7 +361,7 @@ class AuthNostrService extends EventEmitter implements Signer {
364
361
  public exportKeys() {
365
362
  if (!this.params.userInfo) return '';
366
363
  if (this.params.userInfo.authMethod !== 'local') return '';
367
- return nip19.nsecEncode(hexToBytes(this.params.userInfo.sk!));
364
+ return nip19.nsecEncode(this.params.userInfo.sk!);
368
365
  }
369
366
 
370
367
  private onAuth(type: 'login' | 'signup' | 'logout', info: Info | null = null) {
@@ -414,7 +411,7 @@ class AuthNostrService extends EventEmitter implements Signer {
414
411
  options.name = info!.name;
415
412
 
416
413
  if (info!.sk) {
417
- options.localNsec = nip19.nsecEncode(hexToBytes(info!.sk));
414
+ options.localNsec = nip19.nsecEncode(info!.sk);
418
415
  }
419
416
 
420
417
  if (info!.relays) {
@@ -555,7 +552,7 @@ class AuthNostrService extends EventEmitter implements Signer {
555
552
  if (this.signerPromise) {
556
553
  try {
557
554
  await this.signerPromise;
558
- } catch { }
555
+ } catch {}
559
556
  }
560
557
 
561
558
  // we remove support for iframe from nip05 and bunker-url methods,
@@ -607,8 +604,7 @@ class AuthNostrService extends EventEmitter implements Signer {
607
604
 
608
605
  // wait until we connect, otherwise
609
606
  // signer won't start properly
610
- // NOTE: Deferred connection. ensureNIP46Connection will be called on demand.
611
- // await this.ndk.connect();
607
+ await this.ndk.connect();
612
608
 
613
609
  // create and prepare the signer
614
610
  const localSigner = new PrivateKeySigner(info.sk!);
@@ -703,15 +699,12 @@ class AuthNostrService extends EventEmitter implements Signer {
703
699
 
704
700
  public async signEvent(event: any) {
705
701
  if (this.localSigner) {
706
- event.pubkey = getPublicKey(hexToBytes(this.localSigner.privateKey!));
702
+ event.pubkey = getPublicKey(this.localSigner.privateKey!);
707
703
  event.id = getEventHash(event);
708
704
  event.sig = await this.localSigner.sign(event);
709
705
  } else {
710
706
  await this.ensureSigner();
711
707
 
712
- // 署名前に接続を確認 (rpc.sendRequest内でも行われるが、安全のため)
713
- await ensureNIP46Connection(this.ndk, 5000);
714
-
715
708
  event.pubkey = this.signer!.remotePubkey;
716
709
  event.id = getEventHash(event);
717
710
  event.sig = await this.signer!.sign(event);
@@ -725,15 +718,41 @@ class AuthNostrService extends EventEmitter implements Signer {
725
718
  if (!this.signer && this.params.userInfo) {
726
719
  console.log('Signer was destroyed, reinitializing...');
727
720
  await this.initSigner(this.params.userInfo);
728
- return;
721
+ return; // initSignerで接続も行われるので終了
729
722
  }
730
723
 
731
724
  if (!this.signer) {
732
725
  throw new Error('No signer available');
733
726
  }
734
727
 
735
- // 接続確立は署名時またはRPCリクエスト時に行われるため、
736
- // ここでの冗長な接続チェックは削除
728
+ // リレー接続を確認・再接続
729
+ const stats = this.ndk.pool.stats();
730
+ console.log('NDK pool stats:', stats);
731
+
732
+ if (stats.connected === 0) {
733
+ console.log('NDK relays disconnected, reinitializing signer...');
734
+
735
+ // リレーが完全に切断されている場合、signerも再初期化する必要がある
736
+ // (RPCサブスクリプションも切断されているため)
737
+ if (this.params.userInfo) {
738
+ // 古いsignerを破棄
739
+ this.signer = null;
740
+
741
+ // 既存のリレーを一度切断
742
+ for (const relay of this.ndk.pool.relays.values()) {
743
+ try {
744
+ relay.disconnect();
745
+ } catch (e) {
746
+ console.log('Error disconnecting relay:', e);
747
+ }
748
+ }
749
+
750
+ // signerを再初期化(リレー接続も含む)
751
+ await this.initSigner(this.params.userInfo);
752
+ } else {
753
+ throw new Error('Cannot reconnect: no user info');
754
+ }
755
+ }
737
756
  }
738
757
 
739
758
  private async codec_call(method: string, pubkey: string, param: string) {
@@ -5,7 +5,6 @@ import { EventEmitter } from 'tseep';
5
5
  import { ConnectionString, Info, RecentType } from 'nostr-login-components/dist/types/types';
6
6
  import { nip19 } from 'nostr-tools';
7
7
  import { setDarkMode } from '..';
8
- import { bytesToHex } from 'nostr-tools/lib/types/utils';
9
8
 
10
9
  class ModalManager extends EventEmitter {
11
10
  private modal: TypeModal | null = null;
@@ -29,7 +28,7 @@ class ModalManager extends EventEmitter {
29
28
  if (this.launcherPromise) {
30
29
  try {
31
30
  await this.launcherPromise;
32
- } catch { }
31
+ } catch {}
33
32
  this.launcherPromise = undefined;
34
33
  }
35
34
  }
@@ -282,7 +281,7 @@ class ModalManager extends EventEmitter {
282
281
  throw new Error('Bad nsec value');
283
282
  }
284
283
  if (decoded.type !== 'nsec') throw new Error('Bad bech32 type');
285
- await this.authNostrService.localSignup('', bytesToHex(decoded.data));
284
+ await this.authNostrService.localSignup('', decoded.data);
286
285
  ok();
287
286
  } else if (nsecOrBunker.startsWith('bunker:')) {
288
287
  await this.authNostrService.authNip46('login', { name: '', bunkerUrl: nsecOrBunker });