@konemono/nostr-login 1.7.37 → 1.7.39

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.
@@ -2,7 +2,7 @@ export declare function encryptNip44(plaintext: string, conversationKey: Uint8Ar
2
2
  export declare function decryptNip44(payload: string, conversationKey: Uint8Array): string;
3
3
  export declare class Nip44 {
4
4
  private cache;
5
- createKey(privkey: string, pubkey: string): Uint8Array;
5
+ createKey(privkey: string, pubkey: string): Uint8Array<ArrayBufferLike>;
6
6
  private getKey;
7
7
  encrypt(privkey: string, pubkey: string, text: string): string;
8
8
  decrypt(privkey: string, pubkey: string, data: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konemono/nostr-login",
3
- "version": "1.7.37",
3
+ "version": "1.7.39",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
@@ -11,17 +11,17 @@
11
11
  },
12
12
  "author": "a-fralou",
13
13
  "dependencies": {
14
- "@nostr-dev-kit/ndk": "^2.3.1",
14
+ "@nostr-dev-kit/ndk": "^2.18.1",
15
15
  "nostr-tools": "^1.17.0",
16
- "tseep": "^1.2.1"
16
+ "tseep": "^1.3.1"
17
17
  },
18
18
  "devDependencies": {
19
- "@rollup/plugin-commonjs": "^25.0.7",
20
- "@rollup/plugin-node-resolve": "^15.2.3",
19
+ "@rollup/plugin-commonjs": "^25.0.8",
20
+ "@rollup/plugin-node-resolve": "^15.3.1",
21
21
  "@rollup/plugin-terser": "^0.4.4",
22
22
  "nostr-login-components": "^1.0.3",
23
- "prettier": "^3.2.2",
24
- "rollup": "^4.9.6",
23
+ "prettier": "^3.7.4",
24
+ "rollup": "^4.53.5",
25
25
  "rollup-plugin-typescript2": "^0.36.0"
26
26
  },
27
27
  "license": "MIT"
@@ -1,14 +1,15 @@
1
+ //AuthNostrService.ts
1
2
  import { localStorageAddAccount, bunkerUrlToInfo, isBunkerUrl, fetchProfile, getBunkerUrl, localStorageRemoveCurrentAccount, createProfile, getIcon } from '../utils';
2
3
  import { ConnectionString, Info } from 'nostr-login-components/dist/types/types';
3
4
  import { generatePrivateKey, getEventHash, getPublicKey, nip19 } from 'nostr-tools';
4
5
  import { NostrLoginAuthOptions, Response } from '../types';
5
- import NDK, { NDKEvent, NDKNip46Signer, NDKRpcResponse, NDKUser, NostrEvent } from '@nostr-dev-kit/ndk';
6
+ import NDK, { NDKEvent, NDKNip46Signer, NDKPrivateKeySigner, NDKRpcResponse, NDKUser, NostrEvent } from '@nostr-dev-kit/ndk';
6
7
  import { NostrParams } from './';
7
8
  import { EventEmitter } from 'tseep';
8
9
  import { Signer } from './Nostr';
9
10
  import { Nip44 } from '../utils/nip44';
10
11
  import { IframeNostrRpc, Nip46Signer, ReadyListener } from './Nip46';
11
- import { PrivateKeySigner } from './Signer';
12
+ //import { PrivateKeySigner } from './Signer';
12
13
 
13
14
  const OUTBOX_RELAYS = ['wss://user.kindpag.es', 'wss://purplepag.es', 'wss://relay.nos.social'];
14
15
  const DEFAULT_NOSTRCONNECT_RELAYS = ['wss://relay.nsec.app/', 'wss://ephemeral.snowflare.cc/'];
@@ -40,7 +41,7 @@ class AuthNostrService extends EventEmitter implements Signer {
40
41
  private ndk: NDK;
41
42
  private profileNdk: NDK;
42
43
  private signer: Nip46Signer | null = null;
43
- private localSigner: PrivateKeySigner | null = null;
44
+ private localSigner: NDKPrivateKeySigner | null = null;
44
45
  private params: NostrParams;
45
46
  private signerPromise?: Promise<void>;
46
47
  private signerErrCallback?: (err: string) => void;
@@ -92,13 +93,13 @@ class AuthNostrService extends EventEmitter implements Signer {
92
93
  if (this.signerPromise) {
93
94
  try {
94
95
  await this.signerPromise;
95
- } catch { }
96
+ } catch {}
96
97
  }
97
98
 
98
99
  if (this.readyPromise) {
99
100
  try {
100
101
  await this.readyPromise;
101
- } catch { }
102
+ } catch {}
102
103
  }
103
104
  }
104
105
 
@@ -123,7 +124,6 @@ class AuthNostrService extends EventEmitter implements Signer {
123
124
  ) {
124
125
  relays = relays && relays.length > 0 ? relays : DEFAULT_NOSTRCONNECT_RELAYS;
125
126
 
126
-
127
127
  const info: Info = {
128
128
  authMethod: 'connect',
129
129
  pubkey: '', // unknown yet!
@@ -154,13 +154,6 @@ class AuthNostrService extends EventEmitter implements Signer {
154
154
  }
155
155
 
156
156
  public async createNostrConnect(relays?: string[]) {
157
- /*const relayList = relays
158
- ? relays
159
- .split(",")
160
- .map(r => r.trim().replace(/['"]/g, ""))
161
- .filter(r => r.length > 0)
162
- : [];*/
163
-
164
157
  this.nostrConnectKey = generatePrivateKey();
165
158
  this.nostrConnectSecret = Math.random().toString(36).substring(7);
166
159
 
@@ -172,24 +165,13 @@ class AuthNostrService extends EventEmitter implements Signer {
172
165
  perms: encodeURIComponent(this.params.optionsModal.perms || ''),
173
166
  };
174
167
 
175
- return `nostrconnect://${pubkey}?image=${meta.icon}&url=${meta.url}&name=${meta.name}&perms=${meta.perms}&secret=${this.nostrConnectSecret}${(relays || []).length > 0 ? (relays || []).map((r, i) => `&relay=${r}`) : ""}`;
168
+ return `nostrconnect://${pubkey}?image=${meta.icon}&url=${meta.url}&name=${meta.name}&perms=${meta.perms}&secret=${this.nostrConnectSecret}${(relays || []).length > 0 ? (relays || []).map((r, i) => `&relay=${r}`) : ''}`;
176
169
  }
177
170
 
178
171
  public async getNostrConnectServices(): Promise<[string, ConnectionString[]]> {
179
- const nostrconnect = await this.createNostrConnect();
172
+ const nostrconnect = await this.createNostrConnect(DEFAULT_NOSTRCONNECT_RELAYS);
180
173
 
181
- // copy defaults
182
174
  const apps = NOSTRCONNECT_APPS.map(a => ({ ...a }));
183
- // if (this.params.optionsModal.dev) {
184
- // apps.push({
185
- // name: 'Dev.Nsec.app',
186
- // domain: 'new.nsec.app',
187
- // canImport: true,
188
- // img: 'https://new.nsec.app/assets/favicon.ico',
189
- // link: 'https://dev.nsec.app/<nostrconnect>',
190
- // relay: 'wss://relay.nsec.app/',
191
- // });
192
- // }
193
175
 
194
176
  for (const a of apps) {
195
177
  let relays: string[] = [...DEFAULT_NOSTRCONNECT_RELAYS];
@@ -211,18 +193,12 @@ class AuthNostrService extends EventEmitter implements Signer {
211
193
  }
212
194
  }
213
195
 
214
- const relayParams = relays
215
- .map(r => r.replace(/['"]/g, ''))
216
- .map(r => `&relay=${encodeURIComponent(r)}`)
217
- .join('');
218
-
219
- const nc = nostrconnect + relayParams;
196
+ // nostrconnectから既存のリレーを削除して新しいリレーを追加
197
+ const baseUrl = nostrconnect.split('&relay=')[0];
198
+ const relayParams = relays.map(r => `&relay=${encodeURIComponent(r)}`).join('');
199
+ const nc = baseUrl + relayParams;
220
200
 
221
- if (a.iframeUrl) {
222
- a.link = nc;
223
- } else {
224
- a.link = a.link.replace('<nostrconnect>', nc);
225
- }
201
+ a.link = a.iframeUrl ? nc : a.link.replace('<nostrconnect>', nc);
226
202
  }
227
203
 
228
204
  return [nostrconnect, apps];
@@ -244,7 +220,7 @@ class AuthNostrService extends EventEmitter implements Signer {
244
220
 
245
221
  public async setLocal(info: Info, signup?: boolean) {
246
222
  this.releaseSigner();
247
- this.localSigner = new PrivateKeySigner(info.sk!);
223
+ this.localSigner = new NDKPrivateKeySigner(info.sk!);
248
224
 
249
225
  if (signup) await createProfile(info, this.profileNdk, this.localSigner, this.params.optionsModal.signupRelays, this.params.optionsModal.outboxRelays);
250
226
 
@@ -318,9 +294,7 @@ class AuthNostrService extends EventEmitter implements Signer {
318
294
  const userPubkey = await this.signer!.createAccount2({ bunkerPubkey: info.signerPubkey!, name, domain, perms: this.params.optionsModal.perms });
319
295
 
320
296
  return {
321
- bunkerUrl:
322
- `bunker://${userPubkey}?` +
323
- (info.relays ?? []).map((r: string) => `relay=${encodeURIComponent(r)}`).join('&'),
297
+ bunkerUrl: `bunker://${userPubkey}?` + (info.relays ?? []).map((r: string) => `relay=${encodeURIComponent(r)}`).join('&'),
324
298
  sk: info.sk,
325
299
  };
326
300
  }
@@ -371,7 +345,7 @@ class AuthNostrService extends EventEmitter implements Signer {
371
345
  if (info && this.params.userInfo && (info.pubkey !== this.params.userInfo.pubkey || info.authMethod !== this.params.userInfo.authMethod)) {
372
346
  const event = new CustomEvent('nlAuth', { detail: { type: 'logout' } });
373
347
  console.log('nostr-login auth', event.detail);
374
- document.dispatchEvent(event)
348
+ document.dispatchEvent(event);
375
349
  }
376
350
 
377
351
  this.setUserInfo(info);
@@ -508,7 +482,7 @@ class AuthNostrService extends EventEmitter implements Signer {
508
482
  }
509
483
 
510
484
  public async startAuth() {
511
- console.log("startAuth");
485
+ console.log('startAuth');
512
486
  if (this.readyCallback) throw new Error('Already started');
513
487
 
514
488
  // start the new promise
@@ -552,7 +526,7 @@ class AuthNostrService extends EventEmitter implements Signer {
552
526
  if (this.signerPromise) {
553
527
  try {
554
528
  await this.signerPromise;
555
- } catch { }
529
+ } catch {}
556
530
  }
557
531
 
558
532
  // we remove support for iframe from nip05 and bunker-url methods,
@@ -584,7 +558,7 @@ class AuthNostrService extends EventEmitter implements Signer {
584
558
  await this.ndk.connect(CONNECT_TIMEOUT);
585
559
 
586
560
  // create and prepare the signer
587
- const localSigner = new PrivateKeySigner(info.sk!);
561
+ const localSigner = new NDKPrivateKeySigner(info.sk!);
588
562
  this.signer = new Nip46Signer(this.ndk, localSigner, info.signerPubkey!, iframeOrigin);
589
563
 
590
564
  // we should notify the banner the same way as
@@ -619,7 +593,7 @@ class AuthNostrService extends EventEmitter implements Signer {
619
593
 
620
594
  // ensure, we're using it in callbacks above
621
595
  // and expect info to be valid after this call
622
- info.pubkey = this.signer!.userPubkey;
596
+ info.pubkey = this.signer!.userPubkey!;
623
597
  // learned after nostrconnect flow
624
598
  info.signerPubkey = this.signer!.remotePubkey;
625
599
 
@@ -1,15 +1,27 @@
1
- import NDK, { NDKEvent, NDKFilter, NDKNip46Signer, NDKNostrRpc, NDKRpcRequest, NDKRpcResponse, NDKSubscription, NDKSubscriptionCacheUsage, NostrEvent } from '@nostr-dev-kit/ndk';
1
+ //Nip46.ts
2
+ import NDK, {
3
+ NDKEvent,
4
+ NDKFilter,
5
+ NDKNip46Signer,
6
+ NDKNostrRpc,
7
+ NDKPrivateKeySigner,
8
+ NDKRpcRequest,
9
+ NDKRpcResponse,
10
+ NDKSubscription,
11
+ NDKSubscriptionCacheUsage,
12
+ NostrEvent,
13
+ } from '@nostr-dev-kit/ndk';
2
14
  import { validateEvent, verifySignature } from 'nostr-tools';
3
- import { PrivateKeySigner } from './Signer';
15
+ //import { PrivateKeySigner } from './Signer';
4
16
 
5
17
  class NostrRpc extends NDKNostrRpc {
6
18
  protected _ndk: NDK;
7
- protected _signer: PrivateKeySigner;
19
+ protected _signer: NDKPrivateKeySigner;
8
20
  protected requests: Set<string> = new Set();
9
21
  private sub?: NDKSubscription;
10
22
  protected _useNip44: boolean = false;
11
23
 
12
- public constructor(ndk: NDK, signer: PrivateKeySigner) {
24
+ public constructor(ndk: NDK, signer: NDKPrivateKeySigner) {
13
25
  super(ndk, signer, ndk.debug.extend('nip46:signer:rpc'));
14
26
  this._ndk = ndk;
15
27
  this._signer = signer;
@@ -43,8 +55,10 @@ class NostrRpc extends NDKNostrRpc {
43
55
  public async parseEvent(event: NDKEvent): Promise<NDKRpcRequest | NDKRpcResponse> {
44
56
  const remoteUser = this._ndk.getUser({ pubkey: event.pubkey });
45
57
  remoteUser.ndk = this._ndk;
46
- const decrypt = this.isNip04(event.content) ? this._signer.decrypt : this._signer.decryptNip44;
47
- const decryptedContent = await decrypt.call(this._signer, remoteUser, event.content);
58
+ const decryptedContent = this.isNip04(event.content)
59
+ ? await this._signer.decrypt(remoteUser, event.content, 'nip04')
60
+ : await this._signer.decrypt(remoteUser, event.content, 'nip44');
61
+ //const decryptedContent = await decrypt.call(this._signer, remoteUser, event.content);
48
62
  const parsedContent = JSON.parse(decryptedContent);
49
63
  const { id, method, params, result, error } = parsedContent;
50
64
 
@@ -134,7 +148,7 @@ class NostrRpc extends NDKNostrRpc {
134
148
 
135
149
  // create and sign request
136
150
  const event = await this.createRequestEvent(id, remotePubkey, method, params, kind);
137
- console.log("sendRequest", { event, method, remotePubkey, params });
151
+ console.log('sendRequest', { event, method, remotePubkey, params });
138
152
 
139
153
  // send to relays
140
154
  await event.publish();
@@ -186,8 +200,8 @@ class NostrRpc extends NDKNostrRpc {
186
200
  } as NostrEvent);
187
201
 
188
202
  const useNip44 = this._useNip44 && method !== 'create_account';
189
- const encrypt = useNip44 ? this._signer.encryptNip44 : this._signer.encrypt;
190
- event.content = await encrypt.call(this._signer, remoteUser, event.content);
203
+ event.content = useNip44 ? await this._signer.encrypt(remoteUser, event.content, 'nip44') : await this._signer.encrypt(remoteUser, event.content, 'nip04');
204
+ //event.content = await encrypt.call(this._signer, remoteUser, event.content);
191
205
  await event.sign(this._signer);
192
206
 
193
207
  return event;
@@ -199,7 +213,7 @@ export class IframeNostrRpc extends NostrRpc {
199
213
  private iframePort?: MessagePort;
200
214
  private iframeRequests = new Map<string, { id: string; pubkey: string }>();
201
215
 
202
- public constructor(ndk: NDK, localSigner: PrivateKeySigner, iframePeerOrigin?: string) {
216
+ public constructor(ndk: NDK, localSigner: NDKPrivateKeySigner, iframePeerOrigin?: string) {
203
217
  super(ndk, localSigner);
204
218
  this._ndk = ndk;
205
219
  this.peerOrigin = iframePeerOrigin;
@@ -338,53 +352,54 @@ export class ReadyListener {
338
352
  }
339
353
 
340
354
  export class Nip46Signer extends NDKNip46Signer {
341
- private _userPubkey: string = '';
342
355
  private _rpc: IframeNostrRpc;
356
+ private _remotePubkey: string = '';
343
357
 
344
- constructor(ndk: NDK, localSigner: PrivateKeySigner, signerPubkey: string, iframeOrigin?: string) {
358
+ constructor(ndk: NDK, localSigner: NDKPrivateKeySigner, signerPubkey: string, iframeOrigin?: string) {
345
359
  super(ndk, signerPubkey, localSigner);
360
+ this._remotePubkey = signerPubkey;
346
361
 
347
- // override with our own rpc implementation
348
362
  this._rpc = new IframeNostrRpc(ndk, localSigner, iframeOrigin);
349
- this._rpc.setUseNip44(true); // !!this.params.optionsModal.dev);
363
+ this._rpc.setUseNip44(true);
350
364
  this._rpc.on('authUrl', (url: string) => {
351
365
  this.emit('authUrl', url);
352
366
  });
353
-
354
367
  this.rpc = this._rpc;
355
368
  }
356
369
 
357
- get userPubkey() {
358
- return this._userPubkey;
370
+ get remotePubkey(): string {
371
+ return this._remotePubkey;
359
372
  }
360
373
 
361
- private async setSignerPubkey(signerPubkey: string, sameAsUser: boolean = false) {
362
- console.log("setSignerPubkey", signerPubkey);
374
+ set remotePubkey(value: string) {
375
+ this._remotePubkey = value;
376
+ }
377
+
378
+ // userPubkeyのgetterを削除し、親クラスのプロパティを直接使用
363
379
 
364
- // ensure it's set
380
+ private async setSignerPubkey(signerPubkey: string, sameAsUser: boolean = false) {
381
+ console.log('setSignerPubkey', signerPubkey);
365
382
  this.remotePubkey = signerPubkey;
366
383
 
367
- // when we're sure it's known
368
384
  this._rpc.on(`iframeRestart-${signerPubkey}`, () => {
369
385
  this.emit('iframeRestart');
370
386
  });
371
387
 
372
- // now call getPublicKey and swap remotePubkey w/ that
373
388
  await this.initUserPubkey(sameAsUser ? signerPubkey : '');
374
389
  }
375
390
 
376
391
  public async initUserPubkey(hintPubkey?: string) {
377
- if (this._userPubkey) throw new Error('Already called initUserPubkey');
392
+ // 親クラスのuserPubkeyプロパティを直接設定
393
+ if (this.userPubkey) throw new Error('Already called initUserPubkey');
378
394
 
379
395
  if (hintPubkey) {
380
- this._userPubkey = hintPubkey;
396
+ this.userPubkey = hintPubkey;
381
397
  return;
382
398
  }
383
399
 
384
- this._userPubkey = await new Promise<string>((ok, err) => {
400
+ this.userPubkey = await new Promise((ok, err) => {
385
401
  if (!this.remotePubkey) throw new Error('Signer pubkey not set');
386
-
387
- console.log("get_public_key", this.remotePubkey);
402
+ console.log('get_public_key', this.remotePubkey);
388
403
  this._rpc.sendRequest(this.remotePubkey, 'get_public_key', [], 24133, (response: NDKRpcResponse) => {
389
404
  ok(response.result);
390
405
  });
@@ -408,12 +423,7 @@ export class Nip46Signer extends NDKNip46Signer {
408
423
  }
409
424
 
410
425
  public async createAccount2({ bunkerPubkey, name, domain, perms = '' }: { bunkerPubkey: string; name: string; domain: string; perms?: string }) {
411
- const params = [
412
- name,
413
- domain,
414
- '', // email
415
- perms,
416
- ];
426
+ const params = [name, domain, '', perms];
417
427
 
418
428
  const r = await new Promise<NDKRpcResponse>(ok => {
419
429
  this.rpc.sendRequest(bunkerPubkey, 'create_account', params, undefined, ok);
@@ -423,7 +433,6 @@ export class Nip46Signer extends NDKNip46Signer {
423
433
  if (r.result === 'error') {
424
434
  throw new Error(r.error);
425
435
  }
426
-
427
436
  return r.result;
428
437
  }
429
438
  }
@@ -1,4 +1,4 @@
1
- import { NDKPrivateKeySigner, NDKUser } from '@nostr-dev-kit/ndk';
1
+ /* import { NDKPrivateKeySigner, NDKUser } from '@nostr-dev-kit/ndk';
2
2
  import { Nip44 } from '../utils/nip44';
3
3
  import { getPublicKey } from 'nostr-tools';
4
4
 
@@ -23,3 +23,4 @@ export class PrivateKeySigner extends NDKPrivateKeySigner {
23
23
  return Promise.resolve(this.nip44.decrypt(this.privateKey!, sender.pubkey, value));
24
24
  }
25
25
  }
26
+ */