@konemono/nostr-login 1.10.2 → 1.10.4

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.10.2",
3
+ "version": "1.10.4",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
@@ -128,14 +128,17 @@ class AuthNostrService extends EventEmitter implements Signer {
128
128
  link = '',
129
129
  iframeUrl = '',
130
130
  importConnect = false,
131
+ customRelays,
131
132
  }: {
132
133
  domain?: string;
133
134
  link?: string;
134
135
  importConnect?: boolean;
135
136
  iframeUrl?: string;
137
+ customRelays?: string[];
136
138
  } = {},
137
139
  ) {
138
- relay = relay || DEFAULT_NOSTRCONNECT_RELAY;
140
+ // カスタムリレーが指定されていれば使用、そうでなければ単一リレーまたはデフォルト
141
+ const relays = customRelays && customRelays.length > 0 ? customRelays : [relay || DEFAULT_NOSTRCONNECT_RELAY];
139
142
 
140
143
  const info: Info = {
141
144
  authMethod: 'connect',
@@ -143,7 +146,7 @@ class AuthNostrService extends EventEmitter implements Signer {
143
146
  signerPubkey: '', // unknown too!
144
147
  sk: this.nostrConnectKey,
145
148
  domain: domain,
146
- relays: [relay],
149
+ relays: relays,
147
150
  iframeUrl,
148
151
  };
149
152
 
@@ -158,7 +161,9 @@ class AuthNostrService extends EventEmitter implements Signer {
158
161
  // signer learns the remote pubkey
159
162
  if (!info.pubkey || !info.signerPubkey) throw new Error('Bad remote pubkey');
160
163
 
161
- info.bunkerUrl = `bunker://${info.signerPubkey}?relay=${relay}`;
164
+ // bunkerUrl\u306b\u5168\u30ea\u30ec\u30fc\u3092\u542b\u3081\u308b
165
+ const relayParams = relays.map(r => `relay=${encodeURIComponent(r)}`).join('&');
166
+ info.bunkerUrl = `bunker://${info.signerPubkey}?${relayParams}`;
162
167
 
163
168
  // callback
164
169
  if (!importConnect) this.onAuth('login', info);
@@ -166,7 +171,7 @@ class AuthNostrService extends EventEmitter implements Signer {
166
171
  return info;
167
172
  }
168
173
 
169
- public async createNostrConnect(relay?: string) {
174
+ public async createNostrConnect(relays?: string[]) {
170
175
  this.nostrConnectKey = generatePrivateKey();
171
176
  this.nostrConnectSecret = Math.random().toString(36).substring(7);
172
177
 
@@ -178,11 +183,12 @@ class AuthNostrService extends EventEmitter implements Signer {
178
183
  perms: encodeURIComponent(this.params.optionsModal.perms || ''),
179
184
  };
180
185
 
181
- return `nostrconnect://${pubkey}?image=${meta.icon}&url=${meta.url}&name=${meta.name}&perms=${meta.perms}&secret=${this.nostrConnectSecret}${relay ? `&relay=${relay}` : ''}`;
186
+ const relayParams = relays && relays.length > 0 ? relays.map(r => `&relay=${encodeURIComponent(r)}`).join('') : '';
187
+ return `nostrconnect://${pubkey}?image=${meta.icon}&url=${meta.url}&name=${meta.name}&perms=${meta.perms}&secret=${this.nostrConnectSecret}${relayParams}`;
182
188
  }
183
189
 
184
- public async getNostrConnectServices(): Promise<[string, ConnectionString[]]> {
185
- const nostrconnect = await this.createNostrConnect();
190
+ public async getNostrConnectServices(customRelays?: string[]): Promise<[string, ConnectionString[]]> {
191
+ const nostrconnect = await this.createNostrConnect(customRelays);
186
192
 
187
193
  // copy defaults
188
194
  const apps = NOSTRCONNECT_APPS.map(a => ({ ...a }));
@@ -198,20 +204,23 @@ class AuthNostrService extends EventEmitter implements Signer {
198
204
  // }
199
205
 
200
206
  for (const a of apps) {
201
- let relay = DEFAULT_NOSTRCONNECT_RELAY;
207
+ let relays = customRelays && customRelays.length > 0 ? customRelays : [DEFAULT_NOSTRCONNECT_RELAY];
202
208
  if (a.link.startsWith('https://')) {
203
209
  let domain = a.domain || new URL(a.link).hostname;
204
210
  try {
205
211
  const info = await (await fetch(`https://${domain}/.well-known/nostr.json`)).json();
206
212
  const pubkey = info.names['_'];
207
- const relays = info.nip46[pubkey] as string[];
208
- if (relays && relays.length) relay = relays[0];
213
+ const fetchedRelays = info.nip46[pubkey] as string[];
214
+ if (fetchedRelays && fetchedRelays.length && (!customRelays || customRelays.length === 0)) {
215
+ relays = fetchedRelays;
216
+ }
209
217
  a.iframeUrl = info.nip46.iframe_url || '';
210
218
  } catch (e) {
211
219
  console.log('Bad app info', e, a);
212
220
  }
213
221
  }
214
- const nc = nostrconnect + '&relay=' + relay;
222
+ const relayParams = relays.map(r => `&relay=${encodeURIComponent(r)}`).join('');
223
+ const nc = nostrconnect + relayParams;
215
224
  if (a.iframeUrl) {
216
225
  // pass plain nc url for iframe-based flow
217
226
  a.link = nc;
@@ -92,7 +92,7 @@ class ModalManager extends EventEmitter {
92
92
  this.modal.isLoadingExtension = false;
93
93
  this.modal.isLoading = false;
94
94
 
95
- [this.modal.connectionString, this.modal.connectionStringServices] = await this.authNostrService.getNostrConnectServices();
95
+ [this.modal.connectionString, this.modal.connectionStringServices] = await this.authNostrService.getNostrConnectServices(this.customNip46Relays);
96
96
 
97
97
  dialog.appendChild(this.modal);
98
98
  document.body.appendChild(dialog);
@@ -219,7 +219,7 @@ class ModalManager extends EventEmitter {
219
219
  if (!cs) this.modal.isLoading = false;
220
220
  }
221
221
 
222
- await this.authNostrService.nostrConnect(relay, { domain, link, iframeUrl });
222
+ await this.authNostrService.nostrConnect(relay, { domain, link, iframeUrl, customRelays: this.customNip46Relays });
223
223
  });
224
224
  };
225
225