From 620c078848ed62dc60da560f9d421732b6af79c9 Mon Sep 17 00:00:00 2001 From: pablof7z Date: Mon, 13 Jan 2025 11:39:02 +0000 Subject: [PATCH] track pools --- ndk-mobile/src/cache-adapter/migrations.ts | 1 - ndk/src/ndk/index.ts | 2 ++ ndk/src/relay/pool/index.ts | 25 +++++++++++-------- ndk/src/relay/sub-manager.ts | 2 +- ndk/src/user/index.ts | 11 +++++++-- ndk/src/zapper/index.ts | 28 ++++++++++++++++------ 6 files changed, 48 insertions(+), 21 deletions(-) diff --git a/ndk-mobile/src/cache-adapter/migrations.ts b/ndk-mobile/src/cache-adapter/migrations.ts index 7be42186..7a0e1f22 100644 --- a/ndk-mobile/src/cache-adapter/migrations.ts +++ b/ndk-mobile/src/cache-adapter/migrations.ts @@ -64,7 +64,6 @@ export const migrations = [ await db.execAsync(`CREATE INDEX IF NOT EXISTS idx_profiles_pubkey ON profiles (pubkey);`); await db.execAsync(`DROP INDEX IF EXISTS idx_event_tags_tag;`); await db.execAsync(`CREATE INDEX IF NOT EXISTS idx_event_tags_tag ON event_tags (tag, value);`); - console.log('migration 2 done'); }, }, { diff --git a/ndk/src/ndk/index.ts b/ndk/src/ndk/index.ts index f193a1e0..fcad2aae 100644 --- a/ndk/src/ndk/index.ts +++ b/ndk/src/ndk/index.ts @@ -240,6 +240,8 @@ export class NDK extends EventEmitter<{ public publishingFailureHandled = false; + public pools: NDKPool[] = []; + /** * Default relay-auth policy that will be used when a relay requests authentication, * if no other policy is specified for that relay. diff --git a/ndk/src/relay/pool/index.ts b/ndk/src/relay/pool/index.ts index f3be1991..40cb741b 100644 --- a/ndk/src/relay/pool/index.ts +++ b/ndk/src/relay/pool/index.ts @@ -64,6 +64,8 @@ export class NDKPool extends EventEmitter<{ this.relayUrls = relayUrls; this.blacklistRelayUrls = new Set(blacklistedRelayUrls); + + this.ndk.pools.push(this); } get relays() { @@ -340,20 +342,23 @@ export class NDKPool extends EventEmitter<{ } } + const maybeEmitConnect = () => { + const allConnected = this.stats().connected === this.relays.size; + const someConnected = this.stats().connected > 0; + + if (!allConnected && someConnected) { + this.emit("connect"); + } + }; + // If we are running with a timeout, check if we need to emit a `connect` event // in case some, but not all, relays were connected - if (timeoutMs) { - setTimeout(() => { - const allConnected = this.stats().connected === this.relays.size; - const someConnected = this.stats().connected > 0; - - if (!allConnected && someConnected) { - this.emit("connect"); - } - }, timeoutMs); - } + if (timeoutMs) + setTimeout(maybeEmitConnect, timeoutMs); await Promise.all(promises); + + maybeEmitConnect(); } private checkOnFlappingRelays() { diff --git a/ndk/src/relay/sub-manager.ts b/ndk/src/relay/sub-manager.ts index 82b16db2..a21692cf 100644 --- a/ndk/src/relay/sub-manager.ts +++ b/ndk/src/relay/sub-manager.ts @@ -17,7 +17,7 @@ import type { NDKSubscriptionManager } from "../subscription/manager"; */ export class NDKRelaySubscriptionManager { private relay: NDKRelay; - private subscriptions: Map; + public subscriptions: Map; private generalSubManager: NDKSubscriptionManager; /** diff --git a/ndk/src/user/index.ts b/ndk/src/user/index.ts index 80c2434b..3f5c3043 100644 --- a/ndk/src/user/index.ts +++ b/ndk/src/user/index.ts @@ -8,9 +8,8 @@ import { follows } from "./follows.js"; import { type NDKUserProfile, profileFromEvent, serializeProfile } from "./profile.js"; import { getNip05For } from "./nip05.js"; import type { + NDKFilter, NDKRelay, - NDKSigner, - NDKZapDetails, NDKZapMethod, NDKZapMethodInfo, } from "../index.js"; @@ -127,6 +126,14 @@ export class NDKUser { this._pubkey = pubkey; } + /** + * Equivalent to NDKEvent.filters(). + * @returns {NDKFilter} + */ + public filters(): NDKFilter { + return {"#p": [this.pubkey]} + } + /** * Gets NIP-57 and NIP-61 information that this user has signaled * diff --git a/ndk/src/zapper/index.ts b/ndk/src/zapper/index.ts index 5cd0132b..1972b933 100644 --- a/ndk/src/zapper/index.ts +++ b/ndk/src/zapper/index.ts @@ -60,6 +60,11 @@ export type NDKZapDetails = T & { * Description of the payment for the sender's record */ paymentDescription?: string; + + /** + * If this payment is for a nip57 zap, this will contain the zap request. + */ + nip57ZapRequest?: NDKEvent; }; export type NDKZapConfirmation = NDKZapConfirmationLN | NDKZapConfirmationCashu; @@ -194,7 +199,7 @@ class NDKZapper extends EventEmitter<{ try { result = await this.zapSplit(split); } catch (e: any) { - result = e; + result = new Error(e.message); } this.emit("split:complete", split, result); @@ -244,6 +249,7 @@ class NDKZapper extends EventEmitter<{ pr, amount: split.amount, unit: this.unit, + nip57ZapRequest: zapRequest } ); } @@ -441,7 +447,11 @@ class NDKZapper extends EventEmitter<{ * @param pubkey * @returns */ - async getZapMethods(ndk: NDK, recipient: Hexpubkey): Promise { + async getZapMethods( + ndk: NDK, + recipient: Hexpubkey, + timeout = 1500 + ): Promise { const methods: NDKZapMethod[] = []; if (this.cashuPay) methods.push("nip61"); @@ -450,12 +460,16 @@ class NDKZapper extends EventEmitter<{ // if there are no methods available, return an empty array if (methods.length === 0) throw new Error("There are no payment methods available! Please set at least one of lnPay or cashuPay"); - const user = ndk.getUser({ pubkey: recipient }); - const zapInfo = await user.getZapInfo(false, methods); - - d("Zap info for %s: %o", user.npub, zapInfo); + const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout getting information to zap")), timeout)); + const getPromise = new Promise((resolve) => { + const user = ndk.getUser({ pubkey: recipient }); + user.getZapInfo(false, methods).then(resolve) + }); - return zapInfo; + const res = await Promise.race([ + timeoutPromise, getPromise + ]) + return res; } /**