@@ -14,8 +14,6 @@ import type {
1414 NDKZapMethodInfo ,
1515} from "../index.js" ;
1616import { NDKCashuMintList } from "../events/kinds/nutzap/mint-list.js" ;
17- import type { LNPaymentRequest , NDKLnUrlData } from "../zapper/ln.js" ;
18- import { getNip57ZapSpecFromLud , LnPaymentInfo } from "../zapper/ln.js" ;
1917
2018export type Hexpubkey = string ;
2119
@@ -51,6 +49,7 @@ export interface NDKUserParams {
5149export class NDKUser {
5250 public ndk : NDK | undefined ;
5351 public profile ?: NDKUserProfile ;
52+ public profileEvent ?: NDKEvent ;
5453 private _npub ?: Npub ;
5554 private _pubkey ?: Hexpubkey ;
5655 readonly relayUrls : string [ ] = [ ] ;
@@ -76,35 +75,17 @@ export class NDKUser {
7675 }
7776
7877 get nprofile ( ) : string {
79- console . log ( 'encoding with pubkey' , this . pubkey )
78+ const relays = this . profileEvent ?. onRelays ?. map ( ( r ) => r . url ) ;
8079 return nip19 . nprofileEncode ( {
81- pubkey : this . pubkey
80+ pubkey : this . pubkey ,
81+ relays
8282 } ) ;
8383 }
8484
8585 set npub ( npub : Npub ) {
8686 this . _npub = npub ;
8787 }
8888
89- /**
90- * Get the user's hexpubkey
91- * @returns {Hexpubkey } The user's hexpubkey
92- *
93- * @deprecated Use `pubkey` instead
94- */
95- get hexpubkey ( ) : Hexpubkey {
96- return this . pubkey ;
97- }
98-
99- /**
100- * Set the user's hexpubkey
101- * @param pubkey {Hexpubkey} The user's hexpubkey
102- * @deprecated Use `pubkey` instead
103- */
104- set hexpubkey ( pubkey : Hexpubkey ) {
105- this . _pubkey = pubkey ;
106- }
107-
10889 /**
10990 * Get the user's pubkey
11091 * @returns {string } The user's pubkey
@@ -130,7 +111,7 @@ export class NDKUser {
130111 * Equivalent to NDKEvent.filters().
131112 * @returns {NDKFilter }
132113 */
133- public filters ( ) : NDKFilter {
114+ public filter ( ) : NDKFilter {
134115 return { "#p" : [ this . pubkey ] }
135116 }
136117
@@ -139,139 +120,48 @@ export class NDKUser {
139120 *
140121 * @param getAll {boolean} Whether to get all zap info or just the first one
141122 */
142- async getZapInfo (
143- getAll = true ,
144- methods : NDKZapMethod [ ] = [ "nip61" , "nip57" ]
145- ) : Promise < NDKZapMethodInfo [ ] > {
123+ async getZapInfo ( timeoutMs ?: number ) : Promise < Map < NDKZapMethod , NDKZapMethodInfo > > {
146124 if ( ! this . ndk ) throw new Error ( "No NDK instance found" ) ;
147125
148- const kinds : NDKKind [ ] = [ ] ;
149-
150- if ( methods . includes ( "nip61" ) ) kinds . push ( NDKKind . CashuMintList ) ;
151- if ( methods . includes ( "nip57" ) ) kinds . push ( NDKKind . Metadata ) ;
152-
153- if ( kinds . length === 0 ) return [ ] ;
154-
155- let events = await this . ndk . fetchEvents (
156- { kinds, authors : [ this . pubkey ] } ,
157- {
158- cacheUsage : NDKSubscriptionCacheUsage . ONLY_CACHE ,
159- groupable : false ,
126+ const promiseWithTimeout = async < T > ( promise : Promise < T > ) : Promise < T | undefined > => {
127+ if ( ! timeoutMs ) return promise ;
128+ try {
129+ return await Promise . race ( [
130+ promise ,
131+ new Promise < T > ( ( _ , reject ) => setTimeout ( ( ) => reject ( ) , timeoutMs ) )
132+ ] ) ;
133+ } catch {
134+ return undefined ;
160135 }
161- ) ;
162-
163- if ( events . size < methods . length ) {
164- events = await this . ndk . fetchEvents (
165- { kinds, authors : [ this . pubkey ] } ,
166- {
167- cacheUsage : NDKSubscriptionCacheUsage . ONLY_RELAY ,
168- }
169- ) ;
170- }
171-
172- const res : NDKZapMethodInfo [ ] = [ ] ;
136+ } ;
173137
174- const nip61 = Array . from ( events ) . find ( ( e ) => e . kind === NDKKind . CashuMintList ) ;
175- const nip57 = Array . from ( events ) . find ( ( e ) => e . kind === NDKKind . Metadata ) ;
138+ const [ userProfile , mintListEvent ] = await Promise . all ( [
139+ promiseWithTimeout ( this . fetchProfile ( ) ) ,
140+ promiseWithTimeout ( this . ndk . fetchEvent ( { kinds : [ NDKKind . CashuMintList ] , authors : [ this . pubkey ] } ) )
141+ ] ) ;
142+
143+ const res : Map < NDKZapMethod , NDKZapMethodInfo > = new Map ( ) ;
176144
177- if ( nip61 ) {
178- const mintList = NDKCashuMintList . from ( nip61 ) ;
145+ if ( mintListEvent ) {
146+ const mintList = NDKCashuMintList . from ( mintListEvent ) ;
179147
180148 if ( mintList . mints . length > 0 ) {
181- res . push ( {
182- type : "nip61" ,
183- data : {
184- mints : mintList . mints ,
185- relays : mintList . relays ,
186- p2pk : mintList . p2pk ,
187- } ,
149+ res . set ( "nip61" , {
150+ mints : mintList . mints ,
151+ relays : mintList . relays ,
152+ p2pk : mintList . p2pk ,
188153 } ) ;
189154 }
190-
191- // if we are just getting one and already have one, go back
192- if ( ! getAll ) return res ;
193155 }
194156
195- if ( nip57 ) {
196- const profile = profileFromEvent ( nip57 ) ;
197- const { lud06, lud16 } = profile ;
198- try {
199- const zapSpec = await getNip57ZapSpecFromLud ( { lud06, lud16 } , this . ndk ) ;
200-
201- if ( zapSpec ) {
202- res . push ( { type : "nip57" , data : zapSpec } ) ;
203- }
204- } catch ( e ) {
205- console . error ( "Error getting NIP-57 zap spec" , e ) ;
206- }
157+ if ( userProfile ) {
158+ const { lud06, lud16 } = userProfile ;
159+ res . set ( "nip57" , { lud06, lud16 } ) ;
207160 }
208161
209162 return res ;
210163 }
211164
212- /**
213- * Determines whether this user
214- * has signaled support for NIP-60 zaps
215- **/
216- // export type UserZapConfiguration = {
217-
218- // }
219- // async getRecipientZapConfig(): Promise<> {
220-
221- // }
222-
223- /**
224- * Retrieves the zapper this pubkey has designated as an issuer of zap receipts
225- */
226- async getZapConfiguration ( ndk ?: NDK ) : Promise < NDKLnUrlData | undefined > {
227- ndk ??= this . ndk ;
228-
229- if ( ! ndk ) throw new Error ( "No NDK instance found" ) ;
230-
231- const process = async ( ) : Promise < NDKLnUrlData | undefined > => {
232- if ( this . ndk ?. cacheAdapter ?. loadUsersLNURLDoc ) {
233- const doc = await this . ndk . cacheAdapter . loadUsersLNURLDoc ( this . pubkey ) ;
234-
235- if ( doc !== "missing" ) {
236- if ( doc === null ) return ;
237- if ( doc ) return doc ;
238- }
239- }
240-
241- let lnurlspec : NDKLnUrlData | undefined ;
242- try {
243- await this . fetchProfile ( { groupable : false } ) ;
244- if ( this . profile ) {
245- const { lud06, lud16 } = this . profile ;
246- lnurlspec = await getNip57ZapSpecFromLud ( { lud06, lud16 } , ndk ! ) ;
247- }
248- } catch { }
249-
250- if ( this . ndk ?. cacheAdapter ?. saveUsersLNURLDoc ) {
251- this . ndk . cacheAdapter . saveUsersLNURLDoc ( this . pubkey , lnurlspec || null ) ;
252- }
253-
254- if ( ! lnurlspec ) return ;
255-
256- return lnurlspec ;
257- } ;
258-
259- return await ndk . queuesZapConfig . add ( {
260- id : this . pubkey ,
261- func : process ,
262- } ) ;
263- }
264-
265- /**
266- * Fetches the zapper's pubkey for the zapped user
267- * @returns The zapper's pubkey if one can be found
268- */
269- async getZapperPubkey ( ) : Promise < Hexpubkey | undefined > {
270- const zapConfig = await this . getZapConfiguration ( ) ;
271-
272- return zapConfig ?. nostrPubkey ;
273- }
274-
275165 /**
276166 * Instantiate an NDKUser from a NIP-05 string
277167 * @param nip05Id {string} The user's NIP-05
@@ -316,7 +206,7 @@ export class NDKUser {
316206
317207 if ( ! this . profile ) this . profile = { } ;
318208
319- let setMetadataEvents : Set < NDKEvent > | null = null ;
209+ let setMetadataEvent : NDKEvent | null = null ;
320210
321211 if (
322212 this . ndk . cacheAdapter &&
@@ -340,7 +230,7 @@ export class NDKUser {
340230 this . ndk . cacheAdapter && // and we have a cache
341231 this . ndk . cacheAdapter . locking // and the cache identifies itself as fast 😂
342232 ) {
343- setMetadataEvents = await this . ndk . fetchEvents (
233+ setMetadataEvent = await this . ndk . fetchEvent (
344234 {
345235 kinds : [ 0 ] ,
346236 authors : [ this . pubkey ] ,
@@ -360,8 +250,8 @@ export class NDKUser {
360250 } ;
361251 }
362252
363- if ( ! setMetadataEvents || setMetadataEvents . size === 0 ) {
364- setMetadataEvents = await this . ndk . fetchEvents (
253+ if ( ! setMetadataEvent ) {
254+ setMetadataEvent = await this . ndk . fetchEvent (
365255 {
366256 kinds : [ 0 ] ,
367257 authors : [ this . pubkey ] ,
@@ -370,19 +260,14 @@ export class NDKUser {
370260 ) ;
371261 }
372262
373- const sortedSetMetadataEvents = Array . from ( setMetadataEvents ) . sort (
374- ( a , b ) => ( a . created_at as number ) - ( b . created_at as number )
375- ) ;
376-
377- if ( sortedSetMetadataEvents . length === 0 ) return null ;
263+ if ( ! setMetadataEvent ) return null ;
378264
379265 // return the most recent profile
380- const event = sortedSetMetadataEvents [ 0 ] ;
381- this . profile = profileFromEvent ( event ) ;
266+ this . profile = profileFromEvent ( setMetadataEvent ) ;
382267
383268 if ( storeProfileEvent ) {
384269 // Store the event as a stringified JSON
385- this . profile . profileEvent = JSON . stringify ( event ) ;
270+ this . profile . profileEvent = JSON . stringify ( setMetadataEvent ) ;
386271 }
387272
388273 if ( this . profile && this . ndk . cacheAdapter && this . ndk . cacheAdapter . saveProfile ) {
0 commit comments