2025-03-04 08:07:27 -05:00
|
|
|
// lib/initNDK.ts
|
|
|
|
import 'react-native-get-random-values'; // This must be the first import
|
2025-03-09 11:15:28 -04:00
|
|
|
import NDK, { NDKCacheAdapterSqlite } from '@nostr-dev-kit/ndk-mobile';
|
2025-03-04 08:07:27 -05:00
|
|
|
import * as SecureStore from 'expo-secure-store';
|
2025-03-09 11:15:28 -04:00
|
|
|
import { RelayService, DEFAULT_RELAYS } from '@/lib/db/services/RelayService';
|
|
|
|
import { extendNDK } from '@/types/ndk-extensions';
|
2025-03-23 15:53:34 -04:00
|
|
|
import { ConnectivityService } from '@/lib/db/services/ConnectivityService';
|
2025-03-24 15:55:58 -04:00
|
|
|
import { profileImageCache } from '@/lib/db/services/ProfileImageCache';
|
2025-03-23 15:53:34 -04:00
|
|
|
|
|
|
|
// Connection timeout in milliseconds
|
|
|
|
const CONNECTION_TIMEOUT = 5000;
|
2025-03-04 08:07:27 -05:00
|
|
|
|
2025-03-09 11:15:28 -04:00
|
|
|
/**
|
2025-03-11 23:59:00 -04:00
|
|
|
* Initialize NDK with relays
|
2025-03-09 11:15:28 -04:00
|
|
|
*/
|
2025-03-04 08:07:27 -05:00
|
|
|
export async function initializeNDK() {
|
2025-03-09 11:15:28 -04:00
|
|
|
console.log('[NDK] Initializing NDK with mobile adapter...');
|
2025-03-04 08:07:27 -05:00
|
|
|
|
2025-03-06 16:34:50 -05:00
|
|
|
// Create a mobile-specific cache adapter
|
|
|
|
const cacheAdapter = new NDKCacheAdapterSqlite('powr', 1000);
|
2025-03-11 23:59:00 -04:00
|
|
|
await cacheAdapter.initialize();
|
2025-03-04 08:07:27 -05:00
|
|
|
|
2025-03-11 23:59:00 -04:00
|
|
|
// Initialize relay service
|
|
|
|
const relayService = new RelayService();
|
2025-03-09 12:48:24 -04:00
|
|
|
relayService.enableDebug();
|
2025-03-09 11:15:28 -04:00
|
|
|
|
|
|
|
// Create settings store
|
|
|
|
const settingsStore = {
|
|
|
|
get: SecureStore.getItemAsync,
|
|
|
|
set: SecureStore.setItemAsync,
|
|
|
|
delete: SecureStore.deleteItemAsync,
|
|
|
|
getSync: (key: string) => {
|
|
|
|
console.log('[Settings] Warning: getSync called but returning null, not supported in this implementation');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-11 23:59:00 -04:00
|
|
|
// Initialize NDK with default relays first
|
|
|
|
console.log(`[NDK] Creating NDK instance with default relays`);
|
2025-03-09 11:15:28 -04:00
|
|
|
let ndk = new NDK({
|
2025-03-04 08:07:27 -05:00
|
|
|
cacheAdapter,
|
2025-03-11 23:59:00 -04:00
|
|
|
explicitRelayUrls: DEFAULT_RELAYS,
|
2025-03-04 08:07:27 -05:00
|
|
|
enableOutboxModel: true,
|
2025-03-09 11:15:28 -04:00
|
|
|
autoConnectUserRelays: true,
|
2025-03-04 08:07:27 -05:00
|
|
|
clientName: 'powr',
|
|
|
|
});
|
|
|
|
|
2025-03-09 11:15:28 -04:00
|
|
|
// Extend NDK with helper methods for better compatibility
|
|
|
|
ndk = extendNDK(ndk);
|
|
|
|
|
2025-03-24 15:55:58 -04:00
|
|
|
// Set the NDK instance in services
|
2025-03-11 23:59:00 -04:00
|
|
|
relayService.setNDK(ndk);
|
2025-03-24 15:55:58 -04:00
|
|
|
profileImageCache.setNDK(ndk);
|
2025-03-04 08:07:27 -05:00
|
|
|
|
2025-03-23 15:53:34 -04:00
|
|
|
// Check network connectivity before attempting to connect
|
|
|
|
const connectivityService = ConnectivityService.getInstance();
|
|
|
|
const isOnline = await connectivityService.checkNetworkStatus();
|
|
|
|
|
|
|
|
if (!isOnline) {
|
|
|
|
console.log('[NDK] No network connectivity detected, skipping relay connections');
|
|
|
|
return {
|
|
|
|
ndk,
|
|
|
|
relayService,
|
|
|
|
connectedRelayCount: 0,
|
|
|
|
connectedRelays: [],
|
|
|
|
offlineMode: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2025-03-07 10:09:55 -05:00
|
|
|
try {
|
2025-03-23 15:53:34 -04:00
|
|
|
console.log('[NDK] Connecting to relays with timeout...');
|
|
|
|
|
|
|
|
// Create a promise that will reject after the timeout
|
|
|
|
const timeoutPromise = new Promise((_, reject) => {
|
|
|
|
setTimeout(() => reject(new Error('Connection timeout')), CONNECTION_TIMEOUT);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Race the connect operation against the timeout
|
|
|
|
await Promise.race([
|
|
|
|
ndk.connect(),
|
|
|
|
timeoutPromise
|
|
|
|
]).catch(error => {
|
|
|
|
if (error.message === 'Connection timeout') {
|
|
|
|
console.warn('[NDK] Connection timeout reached, continuing in offline mode');
|
|
|
|
throw error; // Re-throw to be caught by outer try/catch
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
2025-03-07 10:09:55 -05:00
|
|
|
|
2025-03-23 15:53:34 -04:00
|
|
|
// Wait a moment for connections to establish (but with a shorter timeout)
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
2025-03-09 11:15:28 -04:00
|
|
|
|
2025-03-11 23:59:00 -04:00
|
|
|
// Get updated relay statuses
|
|
|
|
const relaysWithStatus = await relayService.getAllRelaysWithStatus();
|
|
|
|
|
2025-03-09 11:15:28 -04:00
|
|
|
// Count connected relays
|
2025-03-11 23:59:00 -04:00
|
|
|
const connectedRelays = relaysWithStatus
|
|
|
|
.filter(relay => relay.status === 'connected')
|
|
|
|
.map(relay => relay.url);
|
2025-03-07 10:09:55 -05:00
|
|
|
|
2025-03-11 23:59:00 -04:00
|
|
|
console.log(`[NDK] Connected to ${connectedRelays.length}/${relaysWithStatus.length} relays`);
|
2025-03-07 10:09:55 -05:00
|
|
|
|
2025-03-11 23:59:00 -04:00
|
|
|
// Log detailed relay status
|
2025-03-09 12:48:24 -04:00
|
|
|
console.log('[NDK] Detailed relay status:');
|
2025-03-11 23:59:00 -04:00
|
|
|
relaysWithStatus.forEach(relay => {
|
|
|
|
console.log(` - ${relay.url}: ${relay.status}`);
|
2025-03-09 12:48:24 -04:00
|
|
|
});
|
|
|
|
|
2025-03-07 10:09:55 -05:00
|
|
|
return {
|
|
|
|
ndk,
|
2025-03-09 11:15:28 -04:00
|
|
|
relayService,
|
|
|
|
connectedRelayCount: connectedRelays.length,
|
2025-03-23 15:53:34 -04:00
|
|
|
connectedRelays,
|
|
|
|
offlineMode: connectedRelays.length === 0
|
2025-03-07 10:09:55 -05:00
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.error('[NDK] Error during connection:', error);
|
|
|
|
// Still return the NDK instance so the app can work offline
|
|
|
|
return {
|
|
|
|
ndk,
|
2025-03-09 11:15:28 -04:00
|
|
|
relayService,
|
2025-03-07 10:09:55 -05:00
|
|
|
connectedRelayCount: 0,
|
2025-03-23 15:53:34 -04:00
|
|
|
connectedRelays: [],
|
|
|
|
offlineMode: true
|
2025-03-07 10:09:55 -05:00
|
|
|
};
|
|
|
|
}
|
2025-03-23 15:53:34 -04:00
|
|
|
}
|