@konemono/nostr-login 1.7.60 → 1.7.62
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/dist/index.esm.js +1 -1
- package/dist/modules/AmberDirectSigner.d.ts +4 -10
- package/dist/types.d.ts +5 -0
- package/dist/unpkg.js +1 -1
- package/package.json +1 -1
- package/src/modules/AmberDirectSigner.ts +75 -170
- package/src/modules/AuthNostrService.ts +14 -14
- package/src/types.ts +7 -0
package/package.json
CHANGED
|
@@ -1,237 +1,142 @@
|
|
|
1
|
+
import { AmberResponse } from '../types';
|
|
1
2
|
import { Signer } from './Nostr';
|
|
2
3
|
|
|
3
4
|
export class AmberDirectSigner implements Signer {
|
|
4
|
-
private _pubkey: string
|
|
5
|
-
private static pendingResolves: Map<string, (result:
|
|
5
|
+
private _pubkey: string;
|
|
6
|
+
private static pendingResolves: Map<string, (result: string) => void> = new Map();
|
|
6
7
|
|
|
7
|
-
constructor(pubkey
|
|
8
|
-
this._pubkey = pubkey
|
|
8
|
+
constructor(pubkey: string = '') {
|
|
9
|
+
this._pubkey = pubkey;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
get pubkey() {
|
|
12
13
|
return this._pubkey;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
set pubkey(v: string) {
|
|
16
17
|
this._pubkey = v;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
nip04 = {
|
|
20
|
+
public nip04 = {
|
|
20
21
|
encrypt: (pubkey: string, plaintext: string) => this.encrypt04(pubkey, plaintext),
|
|
21
22
|
decrypt: (pubkey: string, ciphertext: string) => this.decrypt04(pubkey, ciphertext),
|
|
22
23
|
};
|
|
23
24
|
|
|
24
|
-
nip44 = {
|
|
25
|
+
public nip44 = {
|
|
25
26
|
encrypt: (pubkey: string, plaintext: string) => this.encrypt44(pubkey, plaintext),
|
|
26
27
|
decrypt: (pubkey: string, ciphertext: string) => this.decrypt44(pubkey, ciphertext),
|
|
27
28
|
};
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
let hash = 0;
|
|
31
|
-
for (let i = 0; i < content.length; i++) {
|
|
32
|
-
const char = content.charCodeAt(i);
|
|
33
|
-
hash = (hash << 5) - hash + char;
|
|
34
|
-
hash |= 0;
|
|
35
|
-
}
|
|
36
|
-
return hash.toString(36);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
private checkCache(content: string, type: string): string | null {
|
|
40
|
-
const hash = this.getHash(content);
|
|
41
|
-
const cacheKey = `amber_result_${type}_${hash}`;
|
|
42
|
-
const cached = sessionStorage.getItem(cacheKey);
|
|
43
|
-
if (cached) {
|
|
44
|
-
sessionStorage.removeItem(cacheKey);
|
|
45
|
-
return cached;
|
|
46
|
-
}
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
private setPending(id: string, content: string, type: string) {
|
|
51
|
-
const hash = this.getHash(content);
|
|
52
|
-
sessionStorage.setItem(`amber_pending_${id}`, JSON.stringify({ hash, type }));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public static resolvePending(id: string, type: string, result: string) {
|
|
56
|
-
console.log('AmberDirectSigner: resolvePending called', { id, type, pendingCount: this.pendingResolves.size });
|
|
57
|
-
|
|
58
|
-
// Try resolving by ID first
|
|
59
|
-
let resolve = this.pendingResolves.get(id);
|
|
60
|
-
|
|
61
|
-
// Fallback: If no ID match, try resolving by type if only one is pending of that type
|
|
62
|
-
if (!resolve) {
|
|
63
|
-
console.log('AmberDirectSigner: No direct ID match for resolve, searching by type', { id, type });
|
|
64
|
-
for (const [pendingId, pendingResolve] of this.pendingResolves.entries()) {
|
|
65
|
-
const pending = sessionStorage.getItem(`amber_pending_${pendingId}`);
|
|
66
|
-
if (pending) {
|
|
67
|
-
const { type: pendingType } = JSON.parse(pending);
|
|
68
|
-
if (pendingType === type) {
|
|
69
|
-
resolve = pendingResolve;
|
|
70
|
-
id = pendingId;
|
|
71
|
-
console.log('AmberDirectSigner: Found match by type', { id, type });
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (resolve) {
|
|
79
|
-
console.log('AmberDirectSigner: Resolving pending promise', { id, type });
|
|
80
|
-
resolve(result);
|
|
81
|
-
this.pendingResolves.delete(id);
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
console.log('AmberDirectSigner: No pending promise found to resolve', { id, type });
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async signEvent(event: any): Promise<any> {
|
|
90
|
-
const content = JSON.stringify(event);
|
|
91
|
-
const cached = this.checkCache(content, 'sign_event');
|
|
92
|
-
if (cached) {
|
|
93
|
-
return JSON.parse(cached);
|
|
94
|
-
}
|
|
95
|
-
|
|
30
|
+
public async getPublicKey(): Promise<string> {
|
|
96
31
|
const id = Math.random().toString(36).substring(7);
|
|
97
|
-
this.
|
|
98
|
-
const url = this.generateUrl(content, 'sign_event', id);
|
|
32
|
+
const url = this.generateUrl('', 'get_public_key', id);
|
|
99
33
|
window.location.href = url;
|
|
100
34
|
return new Promise((resolve) => {
|
|
101
|
-
|
|
35
|
+
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
102
36
|
});
|
|
103
37
|
}
|
|
104
38
|
|
|
105
|
-
async
|
|
39
|
+
public async signEvent(event: any): Promise<any> {
|
|
106
40
|
const id = Math.random().toString(36).substring(7);
|
|
107
|
-
const url = this.generateUrl(
|
|
41
|
+
const url = this.generateUrl(JSON.stringify(event), 'sign_event', id);
|
|
108
42
|
window.location.href = url;
|
|
109
43
|
return new Promise((resolve) => {
|
|
110
|
-
|
|
44
|
+
AmberDirectSigner.pendingResolves.set(id, (result: string) => {
|
|
45
|
+
resolve(JSON.parse(result));
|
|
46
|
+
});
|
|
111
47
|
});
|
|
112
48
|
}
|
|
113
49
|
|
|
114
|
-
async encrypt04(pubkey: string, plaintext: string): Promise<string> {
|
|
115
|
-
const cached = this.checkCache(plaintext, 'nip04_encrypt');
|
|
116
|
-
if (cached) return cached;
|
|
117
|
-
|
|
50
|
+
public async encrypt04(pubkey: string, plaintext: string): Promise<string> {
|
|
118
51
|
const id = Math.random().toString(36).substring(7);
|
|
119
|
-
this.setPending(id, plaintext, 'nip04_encrypt');
|
|
120
52
|
const url = this.generateUrl(plaintext, 'nip04_encrypt', id, pubkey);
|
|
121
53
|
window.location.href = url;
|
|
122
54
|
return new Promise((resolve) => {
|
|
123
|
-
|
|
55
|
+
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
124
56
|
});
|
|
125
57
|
}
|
|
126
58
|
|
|
127
|
-
async decrypt04(pubkey: string, ciphertext: string): Promise<string> {
|
|
128
|
-
const cached = this.checkCache(ciphertext, 'nip04_decrypt');
|
|
129
|
-
if (cached) return cached;
|
|
130
|
-
|
|
59
|
+
public async decrypt04(pubkey: string, ciphertext: string): Promise<string> {
|
|
131
60
|
const id = Math.random().toString(36).substring(7);
|
|
132
|
-
this.setPending(id, ciphertext, 'nip04_decrypt');
|
|
133
61
|
const url = this.generateUrl(ciphertext, 'nip04_decrypt', id, pubkey);
|
|
134
62
|
window.location.href = url;
|
|
135
63
|
return new Promise((resolve) => {
|
|
136
|
-
|
|
64
|
+
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
137
65
|
});
|
|
138
66
|
}
|
|
139
67
|
|
|
140
|
-
async encrypt44(pubkey: string, plaintext: string): Promise<string> {
|
|
141
|
-
const cached = this.checkCache(plaintext, 'nip44_encrypt');
|
|
142
|
-
if (cached) return cached;
|
|
143
|
-
|
|
68
|
+
public async encrypt44(pubkey: string, plaintext: string): Promise<string> {
|
|
144
69
|
const id = Math.random().toString(36).substring(7);
|
|
145
|
-
this.setPending(id, plaintext, 'nip44_encrypt');
|
|
146
70
|
const url = this.generateUrl(plaintext, 'nip44_encrypt', id, pubkey);
|
|
147
71
|
window.location.href = url;
|
|
148
72
|
return new Promise((resolve) => {
|
|
149
|
-
|
|
73
|
+
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
150
74
|
});
|
|
151
75
|
}
|
|
152
76
|
|
|
153
|
-
async decrypt44(pubkey: string, ciphertext: string): Promise<string> {
|
|
154
|
-
const cached = this.checkCache(ciphertext, 'nip44_decrypt');
|
|
155
|
-
if (cached) return cached;
|
|
156
|
-
|
|
77
|
+
public async decrypt44(pubkey: string, ciphertext: string): Promise<string> {
|
|
157
78
|
const id = Math.random().toString(36).substring(7);
|
|
158
|
-
this.setPending(id, ciphertext, 'nip44_decrypt');
|
|
159
79
|
const url = this.generateUrl(ciphertext, 'nip44_decrypt', id, pubkey);
|
|
160
80
|
window.location.href = url;
|
|
161
81
|
return new Promise((resolve) => {
|
|
162
|
-
|
|
82
|
+
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
163
83
|
});
|
|
164
84
|
}
|
|
165
85
|
|
|
166
|
-
private generateUrl(content: string, type: string, id: string, recipient?: string): string {
|
|
167
|
-
const callbackUrl = new URL(window.location.origin + window.location.pathname);
|
|
168
|
-
|
|
169
|
-
// Amber parameters we want to carry back
|
|
170
|
-
callbackUrl.searchParams.set('amberType', type);
|
|
171
|
-
callbackUrl.searchParams.set('amberId', id);
|
|
172
|
-
|
|
173
|
-
const name = document.title || window.location.hostname || 'Nostr Login';
|
|
174
|
-
|
|
175
|
-
// NIP-55 standard and some common extensions
|
|
176
|
-
const params: string[] = [];
|
|
177
|
-
params.push(`type=${encodeURIComponent(type)}`);
|
|
178
|
-
params.push(`id=${encodeURIComponent(id)}`);
|
|
179
|
-
params.push(`callbackUrl=${encodeURIComponent(callbackUrl.toString())}`);
|
|
180
|
-
|
|
181
|
-
// Multi-alias for app name to fix the "null" issue
|
|
182
|
-
params.push(`name=${encodeURIComponent(name)}`);
|
|
183
|
-
params.push(`app_name=${encodeURIComponent(name)}`);
|
|
184
|
-
params.push(`title=${encodeURIComponent(name)}`);
|
|
185
|
-
|
|
186
|
-
if (recipient) {
|
|
187
|
-
params.push(`pubkey=${encodeURIComponent(recipient)}`);
|
|
188
|
-
params.push(`pubKey=${encodeURIComponent(recipient)}`);
|
|
189
|
-
params.push(`current_user=${encodeURIComponent(recipient)}`);
|
|
190
|
-
} else if (this._pubkey) {
|
|
191
|
-
params.push(`pubkey=${encodeURIComponent(this._pubkey)}`);
|
|
192
|
-
params.push(`pubKey=${encodeURIComponent(this._pubkey)}`);
|
|
193
|
-
params.push(`current_user=${encodeURIComponent(this._pubkey)}`);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const dataPart = content ? encodeURIComponent(content) : '';
|
|
197
|
-
// Format: nostrsigner:<content>?<params>
|
|
198
|
-
return `nostrsigner:${dataPart}?${params.join('&')}`;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
public static parseResponse(): { type: string; id: string; result: string } | null {
|
|
202
|
-
const url = new URL(window.location.href);
|
|
203
|
-
const params = new URLSearchParams(url.search);
|
|
204
|
-
const hashParams = new URLSearchParams(url.hash.substring(1));
|
|
205
86
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
87
|
+
private generateUrl(content: string, type: string, id: string, pubkey?: string): string {
|
|
88
|
+
const callbackUrl = window.location.href.split('#')[0].split('?')[0];
|
|
89
|
+
const encodedContent = encodeURIComponent(content || '');
|
|
90
|
+
const encodedCallback = encodeURIComponent(callbackUrl);
|
|
91
|
+
|
|
92
|
+
// ★ 追加: sessionStorageに保存(これが無い)
|
|
93
|
+
sessionStorage.setItem('amber_last_type', type);
|
|
94
|
+
sessionStorage.setItem('amber_last_id', id);
|
|
95
|
+
sessionStorage.setItem('amber_last_timestamp', Date.now().toString());
|
|
96
|
+
|
|
97
|
+
// NIP-55準拠: nostrsigner: スキーム
|
|
98
|
+
let url = `nostrsigner:${encodedContent}`;
|
|
99
|
+
url += `?compressionType=none`;
|
|
100
|
+
url += `&returnType=signature`;
|
|
101
|
+
url += `&type=${type}`;
|
|
102
|
+
url += `&callbackUrl=${encodedCallback}`;
|
|
103
|
+
if (pubkey) url += `&pubkey=${pubkey}`;
|
|
104
|
+
if (this._pubkey) url += `¤t_user=${this._pubkey}`; // ★ 追加
|
|
105
|
+
|
|
106
|
+
return url;
|
|
107
|
+
}
|
|
224
108
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
109
|
+
static parseResponse(): AmberResponse | null {
|
|
110
|
+
// NIP-55準拠: クエリパラメータから取得
|
|
111
|
+
const url = new URL(window.location.href);
|
|
112
|
+
const result = url.searchParams.get('event');
|
|
113
|
+
|
|
114
|
+
if (!result) return null;
|
|
115
|
+
|
|
116
|
+
// ★ sessionStorageから id と type を取得
|
|
117
|
+
const id = sessionStorage.getItem('amber_last_id');
|
|
118
|
+
const type = sessionStorage.getItem('amber_last_type');
|
|
119
|
+
|
|
120
|
+
// ★ sessionStorage をクリーンアップ
|
|
121
|
+
sessionStorage.removeItem('amber_last_id');
|
|
122
|
+
sessionStorage.removeItem('amber_last_type');
|
|
123
|
+
sessionStorage.removeItem('amber_last_timestamp');
|
|
124
|
+
|
|
125
|
+
// ★ id と type が存在する場合のみ返す
|
|
126
|
+
if (id && type) {
|
|
127
|
+
return { id, type, result };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
232
132
|
|
|
233
|
-
|
|
133
|
+
static resolvePending(id: string, type: string, result: string): boolean {
|
|
134
|
+
const resolve = this.pendingResolves.get(id);
|
|
135
|
+
if (resolve) {
|
|
136
|
+
this.pendingResolves.delete(id);
|
|
137
|
+
resolve(result);
|
|
138
|
+
return true;
|
|
234
139
|
}
|
|
235
|
-
return
|
|
140
|
+
return false;
|
|
236
141
|
}
|
|
237
142
|
}
|
|
@@ -12,6 +12,7 @@ import { PrivateKeySigner } from './Signer';
|
|
|
12
12
|
import { AmberDirectSigner } from './AmberDirectSigner';
|
|
13
13
|
|
|
14
14
|
|
|
15
|
+
|
|
15
16
|
const OUTBOX_RELAYS = ['wss://user.kindpag.es', 'wss://purplepag.es', 'wss://relay.nos.social'];
|
|
16
17
|
const DEFAULT_NOSTRCONNECT_RELAYS = ['wss://relay.nsec.app/', 'wss://ephemeral.snowflare.cc/'];
|
|
17
18
|
const CONNECT_TIMEOUT = 5000;
|
|
@@ -94,7 +95,7 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
94
95
|
|
|
95
96
|
window.addEventListener('focus', check);
|
|
96
97
|
window.addEventListener('visibilitychange', () => {
|
|
97
|
-
|
|
98
|
+
if (document.visibilityState === 'visible') check();
|
|
98
99
|
});
|
|
99
100
|
window.addEventListener('popstate', check);
|
|
100
101
|
window.addEventListener('hashchange', check);
|
|
@@ -108,13 +109,16 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
108
109
|
if (response) {
|
|
109
110
|
console.log('Amber response detected', response);
|
|
110
111
|
|
|
112
|
+
// Stop the "Connecting..." spinner
|
|
113
|
+
this.emit('onAuthUrl', { url: '' });
|
|
114
|
+
|
|
111
115
|
// Resolve pending promises if any (for non-reload cases)
|
|
112
116
|
const resolved = AmberDirectSigner.resolvePending(response.id, response.type, response.result);
|
|
113
117
|
if (resolved) {
|
|
114
|
-
|
|
118
|
+
console.log('Resolved pending Amber promise via resolvePending');
|
|
115
119
|
}
|
|
116
120
|
|
|
117
|
-
if (response.type === 'get_public_key') {
|
|
121
|
+
if (response.type === 'get_public_key' || response.type.includes('pub')) {
|
|
118
122
|
const info: Info = {
|
|
119
123
|
pubkey: response.result,
|
|
120
124
|
authMethod: 'amber' as any,
|
|
@@ -122,18 +126,14 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
122
126
|
console.log('Amber login success', info);
|
|
123
127
|
this.onAuth('login', info);
|
|
124
128
|
} else {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (pending) {
|
|
128
|
-
const { hash, type } = JSON.parse(pending);
|
|
129
|
-
sessionStorage.removeItem(pendingKey);
|
|
130
|
-
if (type === response.type) {
|
|
131
|
-
const cacheKey = `amber_result_${type}_${hash}`;
|
|
132
|
-
sessionStorage.setItem(cacheKey, response.result);
|
|
133
|
-
console.log('Stored Amber result', cacheKey);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
129
|
+
// ★ 不要: この部分は削除または簡略化
|
|
130
|
+
// NIP-55では直接resultを使用するため、追加のキャッシュは不要
|
|
136
131
|
}
|
|
132
|
+
|
|
133
|
+
// ★ 追加: URLクエリパラメータをクリーンアップ
|
|
134
|
+
const url = new URL(window.location.href);
|
|
135
|
+
url.searchParams.delete('event');
|
|
136
|
+
window.history.replaceState({}, '', url.toString());
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
|