Initial commit: libp2p-native-bridge package

- Extracted libp2p component from main app
- Created modular package structure with interfaces and implementations
- Added dependency injection for NativeModules
- Configured for IOR loading from Gitea
- Added comprehensive README and documentation
This commit is contained in:
Chris Daßler
2025-08-29 11:18:37 +02:00
commit 6f1d6ec37b
14 changed files with 2060 additions and 0 deletions

45
utils/constants.ts Normal file
View File

@@ -0,0 +1,45 @@
/**
* Shared constants for libp2p configuration across platforms
*/
export const LIBP2P_CONFIG = {
// Default ports
DEFAULT_TCP_PORT: 10000,
DEFAULT_WS_PORT: 10001,
// Connection timeouts (in milliseconds)
CONNECTION_PENDING_TIMEOUT: 30000, // 30 seconds before removing pending connections
CONNECTION_FAILED_VISIBILITY: 5000, // 5 seconds to keep failed connections visible
CONNECTION_ACCEPTANCE_CHECK_DELAY: 5000, // 5 seconds before checking acceptance
// Monitoring intervals (in milliseconds)
CONNECTION_MONITOR_INTERVAL: 3000, // Check connections every 3 seconds
CONNECTION_SYNC_INTERVAL: 5000, // Sync connections every 5 seconds
// Protocol IDs
PROTOCOLS: {
CONNECTION_ACCEPT: '/metatrom/connection-accept/1.0.0',
CONNECTION_ACCEPTED: '/metatrom/connection-accepted/1.0.0',
},
// Connection states
CONNECTION_STATUS: {
PENDING: 'pending',
CONNECTED: 'connected',
DISCONNECTED: 'disconnected',
FAILED: 'failed',
CLOSING: 'closing',
CLOSED: 'closed',
} as const,
// Connection directions
CONNECTION_DIRECTION: {
INBOUND: 'inbound',
OUTBOUND: 'outbound',
} as const,
} as const;
export type ConnectionStatus =
(typeof LIBP2P_CONFIG.CONNECTION_STATUS)[keyof typeof LIBP2P_CONFIG.CONNECTION_STATUS];
export type ConnectionDirection =
(typeof LIBP2P_CONFIG.CONNECTION_DIRECTION)[keyof typeof LIBP2P_CONFIG.CONNECTION_DIRECTION];

103
utils/eventHelpers.ts Normal file
View File

@@ -0,0 +1,103 @@
/**
* Helper functions for standardized event emission across platforms
* Import this file in native modules to ensure consistent event structures
*/
import type {
ConnectionStatusEvent,
ErrorEvent,
LogEvent,
PeerDiscoveredEvent,
PeerInfoEvent,
PeerLostEvent,
} from './types';
/**
* Creates a standardized connection status event payload
*/
export function createConnectionStatusEvent(
peerId: string,
status: string,
direction?: string,
multiaddr?: string,
error?: string,
reason?: string,
): ConnectionStatusEvent {
return {
peerId,
status: status as ConnectionStatus,
direction: direction as ConnectionDirection,
multiaddr,
error,
reason,
};
}
/**
* Creates a standardized peer discovered event payload
*/
export function createPeerDiscoveredEvent(
peerId: string,
addresses: string[],
): PeerDiscoveredEvent {
return {
peerId,
addresses,
multiaddrs: addresses, // Use same array for both for compatibility
};
}
/**
* Creates a standardized peer lost event payload
*/
export function createPeerLostEvent(peerId: string): PeerLostEvent {
return {
peerId,
};
}
/**
* Creates a standardized log event payload
*/
export function createLogEvent(
message: string,
level: 'debug' | 'info' | 'warn' | 'error' = 'info',
): LogEvent {
return {
message,
level,
};
}
/**
* Creates a standardized error event payload
*/
export function createErrorEvent(message: string, code?: string): ErrorEvent {
return {
message,
code,
};
}
/**
* Creates a standardized peer info event payload
*/
export function createPeerInfoEvent(peerId: string, multiaddrs: string[]): PeerInfoEvent {
return {
peerId,
multiaddrs,
};
}
/**
* Event names used across platforms
*/
export const EVENT_NAMES = {
PEER_INFO: 'onPeerInfo',
PEER_DISCOVERED: 'onPeerDiscovered',
PEER_LOST: 'onPeerLost',
CONNECTION_STATUS: 'onConnectionStatus',
INCOMING_CONNECTION: 'onIncomingConnection',
LOG: 'onLog',
ERROR: 'onError',
} as const;

54
utils/types.ts Normal file
View File

@@ -0,0 +1,54 @@
/**
* Shared types for libp2p implementation across platforms
*/
import type { ConnectionDirection, ConnectionStatus } from './constants';
/**
* Unified connection information structure used by both iOS and Android
*/
export interface ConnectionInfo {
peerId: string;
status: ConnectionStatus;
direction: ConnectionDirection;
multiaddr?: string;
timestamp: number; // Unix timestamp in seconds
error?: string;
}
/**
* Event payloads for consistent event handling across platforms
*/
export interface ConnectionStatusEvent {
peerId: string;
status: ConnectionStatus;
direction?: ConnectionDirection;
multiaddr?: string;
error?: string;
reason?: string;
}
export interface PeerDiscoveredEvent {
peerId: string;
addresses?: string[];
multiaddrs?: string[];
}
export interface PeerLostEvent {
peerId: string;
}
export interface LogEvent {
message: string;
level?: 'debug' | 'info' | 'warn' | 'error';
}
export interface ErrorEvent {
message: string;
code?: string;
}
export interface PeerInfoEvent {
peerId: string;
multiaddrs: string[];
}