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:
87
interfaces/ILibp2pComponent.ts
Normal file
87
interfaces/ILibp2pComponent.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Core libp2p component interfaces
|
||||
*/
|
||||
|
||||
export interface PeerId {
|
||||
toString(): string;
|
||||
toBytes(): Uint8Array;
|
||||
equals(other: PeerId): boolean;
|
||||
}
|
||||
|
||||
export interface Multiaddr {
|
||||
toString(): string;
|
||||
bytes: Uint8Array;
|
||||
protos(): Array<{ code: number; name: string }>;
|
||||
getPeerId(): string | null;
|
||||
}
|
||||
|
||||
export interface Connection {
|
||||
id: string;
|
||||
remotePeer: PeerId;
|
||||
remoteAddr: Multiaddr;
|
||||
stat: {
|
||||
direction: 'inbound' | 'outbound';
|
||||
status: 'pending' | 'open' | 'closing' | 'closed';
|
||||
timeline: {
|
||||
open: number;
|
||||
upgraded?: number;
|
||||
close?: number;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export interface PeerInfo {
|
||||
id: PeerId;
|
||||
multiaddrs: Multiaddr[];
|
||||
}
|
||||
|
||||
export interface ProtocolHandler {
|
||||
protocolId: string;
|
||||
handler: (data: { peerId: string; data?: Uint8Array }) => void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface ILibp2pComponent {
|
||||
peerId: PeerId | null;
|
||||
multiaddrs: Multiaddr[];
|
||||
|
||||
start(): Promise<void>;
|
||||
stop(): Promise<void>;
|
||||
dial(multiaddr: string): Promise<Connection>;
|
||||
hangUp(peerId: string): Promise<void>;
|
||||
getConnections(peerId?: string): Promise<Connection[]>;
|
||||
sendProtocolData(peerId: string, protocolId: string, data: Uint8Array): Promise<void>;
|
||||
refreshDiscovery(): Promise<void>;
|
||||
pingPeer(peerId: string): Promise<{ success: boolean; rtt?: number; peerId: string }>;
|
||||
|
||||
addEventListener<K extends keyof Libp2pEvents>(
|
||||
event: K,
|
||||
handler: (evt: Libp2pEvents[K]) => void,
|
||||
): void;
|
||||
|
||||
removeEventListener<K extends keyof Libp2pEvents>(
|
||||
event: K,
|
||||
handler: (evt: Libp2pEvents[K]) => void,
|
||||
): void;
|
||||
}
|
||||
|
||||
export interface Libp2pEvents {
|
||||
'peer:discovery': CustomEvent<PeerInfo>;
|
||||
'peer:lost': CustomEvent<{ id: PeerId }>;
|
||||
'peer:connect': CustomEvent<Connection>;
|
||||
'peer:disconnect': CustomEvent<Connection>;
|
||||
'connection:open': CustomEvent<Connection>;
|
||||
'connection:close': CustomEvent<Connection>;
|
||||
'self:peer:update': CustomEvent<{ peerId: PeerId; multiaddrs: Multiaddr[] }>;
|
||||
}
|
||||
|
||||
export interface Libp2pOptions {
|
||||
config?: {
|
||||
tcpPort?: number;
|
||||
wsPort?: number;
|
||||
};
|
||||
protocols?: ProtocolHandler[];
|
||||
keypair?: {
|
||||
privateKey: Uint8Array;
|
||||
publicKey: Uint8Array;
|
||||
};
|
||||
}
|
||||
6
interfaces/index.ts
Normal file
6
interfaces/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Export all interfaces
|
||||
*/
|
||||
|
||||
export * from './ILibp2pComponent';
|
||||
export * from './types';
|
||||
20
interfaces/types.ts
Normal file
20
interfaces/types.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Type definitions for libp2p native bridge
|
||||
*/
|
||||
|
||||
export type {
|
||||
ConnectionStatusEvent,
|
||||
PeerDiscoveredEvent,
|
||||
PeerInfoEvent
|
||||
} from '../utils/types';
|
||||
|
||||
// Re-export common types from ILibp2pComponent for convenience
|
||||
export type {
|
||||
PeerId,
|
||||
Multiaddr,
|
||||
Connection,
|
||||
PeerInfo,
|
||||
ProtocolHandler,
|
||||
Libp2pEvents,
|
||||
Libp2pOptions
|
||||
} from './ILibp2pComponent';
|
||||
Reference in New Issue
Block a user