This commit is contained in:
Chad Curtis committed 2025-07-13 20:12:42 +00:00
1 parent 8c34dbc8d5
commit 7eecec7506
5 files changed
+18 -161

No files matched your search

+3 -61
View File
@@ -25,8 +25,6 @@ export function useNWCInternal() {
const [activeConnection, setActiveConnection] = useLocalStorage<string | null>('nwc-active-connection', null);
const [connectionInfo, setConnectionInfo] = useState<Record<string, NWCInfo>>({});
// Use connections directly - no filtering needed
// Parse and validate NWC URI
const parseNWCUri = (uri: string): { connectionString: string } | null => {
try {
@@ -70,8 +68,6 @@ export function useNWCInternal() {
}
try {
console.debug('Testing NWC connection:', { uri: uri.substring(0, 50) + '...' });
// Test the connection by creating an LN client with timeout
const testPromise = new Promise((resolve, reject) => {
try {
@@ -81,18 +77,15 @@ export function useNWCInternal() {
reject(error);
}
});
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Connection test timeout')), 10000);
});
const _client = await Promise.race([testPromise, timeoutPromise]) as LN;
await Promise.race([testPromise, timeoutPromise]) as LN;
const connection: NWCConnection = {
connectionString: parsed.connectionString,
alias: alias || 'NWC Wallet',
isConnected: true,
// Don't store the client, create fresh ones for each payment
};
// Store basic connection info
@@ -107,33 +100,9 @@ export function useNWCInternal() {
const newConnections = [...connections, connection];
setConnections(newConnections);
console.debug('NWC connection added:', {
alias: connection.alias,
totalConnections: newConnections.length,
connectionString: parsed.connectionString.substring(0, 50) + '...',
isConnected: connection.isConnected
});
// Set as active if it's the first connection or no active connection is set
if (connections.length === 0 || !activeConnection) {
console.debug('Setting as active connection:', {
alias: connection.alias,
connectionString: parsed.connectionString.substring(0, 50) + '...',
previousActiveConnection: activeConnection
});
if (connections.length === 0 || !activeConnection)
setActiveConnection(parsed.connectionString);
console.debug('Active connection set to:', parsed.connectionString.substring(0, 50) + '...');
}
console.debug('NWC connection successful');
// Force a small delay to ensure state updates are processed
setTimeout(() => {
console.debug('Post-connection state check:', {
connectionsLength: connections.length + 1, // +1 because we just added one
newConnectionAlias: connection.alias
});
}, 100);
toast({
title: 'Wallet connected',
@@ -159,14 +128,9 @@ export function useNWCInternal() {
const filtered = connections.filter(c => c.connectionString !== connectionString);
setConnections(filtered);
console.debug('NWC connection removed:', {
remainingConnections: filtered.length
});
if (activeConnection === connectionString) {
const newActive = filtered.length > 0 ? filtered[0].connectionString : null;
setActiveConnection(newActive);
console.debug('Active connection changed:', { newActive });
}
setConnectionInfo(prev => {
@@ -183,15 +147,8 @@ export function useNWCInternal() {
// Get active connection
const getActiveConnection = useCallback((): NWCConnection | null => {
console.debug('getActiveConnection called:', {
activeConnection,
connectionsLength: connections.length,
connections: connections.map(c => ({ alias: c.alias, connectionString: c.connectionString.substring(0, 50) + '...' }))
});
// If no active connection is set but we have connections, set the first one as active
if (!activeConnection && connections.length > 0) {
console.debug('Setting first connection as active:', connections[0].alias);
setActiveConnection(connections[0].connectionString);
return connections[0];
}
@@ -202,7 +159,6 @@ export function useNWCInternal() {
}
const found = connections.find(c => c.connectionString === activeConnection);
console.debug('Found active connection:', found ? found.alias : 'null');
return found || null;
}, [activeConnection, connections, setActiveConnection]);
@@ -218,7 +174,6 @@ export function useNWCInternal() {
// Always create a fresh client for each payment to avoid stale connections
let client: LN;
try {
console.debug('Creating fresh NWC client for payment...');
client = new LN(connection.connectionString);
} catch (error) {
console.error('Failed to create NWC client:', error);
@@ -226,20 +181,13 @@ export function useNWCInternal() {
}
try {
console.debug('Sending payment via NWC SDK:', {
invoice: invoice.substring(0, 50) + '...',
connectionAlias: connection.alias
});
// Add timeout to prevent hanging
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Payment timeout after 30 seconds')), 30000);
setTimeout(() => reject(new Error('Payment timeout after 15 seconds')), 15);
});
const paymentPromise = client.pay(invoice);
const response = await Promise.race([paymentPromise, timeoutPromise]) as { preimage: string };
console.debug('Payment successful:', { preimage: response.preimage });
return response;
} catch (error) {
console.error('NWC payment failed:', error);
@@ -279,8 +227,6 @@ export function useNWCInternal() {
}
try {
console.debug('Testing NWC connection...', { alias: connection.alias });
// Create a fresh client for testing
const testPromise = new Promise((resolve, reject) => {
try {
@@ -294,10 +240,8 @@ export function useNWCInternal() {
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Connection test timeout')), 5000);
});
await Promise.race([testPromise, timeoutPromise]);
console.debug('NWC connection test successful');
return true;
} catch (error) {
console.error('NWC connection test failed:', error);
@@ -305,8 +249,6 @@ export function useNWCInternal() {
}
}, []);
return {
connections,
activeConnection,