@bsv/p2p
Version:
A client for P2P messaging and payments
103 lines • 2.9 kB
TypeScript
import { WalletClient, AuthFetch } from '@bsv/sdk';
import { AuthSocketClient } from '@bsv/authsocket-client';
/**
* Defines the structure of a PeerMessage
*/
export interface PeerMessage {
messageId: string;
body: string;
sender: string;
created_at: string;
updated_at: string;
acknowledged?: boolean;
}
/**
* Defines the structure of a message being sent
*/
export interface SendMessageParams {
recipient: string;
messageBox: string;
body: string | object;
messageId?: string;
}
/**
* Defines the structure of the response from sendMessage
*/
export interface SendMessageResponse {
status: string;
messageId: string;
}
/**
* Defines the structure of a request to acknowledge messages
*/
export interface AcknowledgeMessageParams {
messageIds: string[];
}
/**
* Defines the structure of a request to list messages
*/
export interface ListMessagesParams {
messageBox: string;
}
/**
* Extendable class for interacting with a MessageBoxServer
*/
export declare class MessageBoxClient {
private readonly host;
readonly authFetch: AuthFetch;
private readonly walletClient;
private socket?;
private myIdentityKey?;
constructor({ host, walletClient, enableLogging }: {
host?: string;
walletClient: WalletClient;
enableLogging?: boolean;
});
/**
* Getter for joinedRooms to use in tests
*/
getJoinedRooms(): Set<string>;
getIdentityKey(): string;
get testSocket(): ReturnType<typeof AuthSocketClient> | undefined;
/**
* Establish an initial WebSocket connection (optional)
*/
initializeConnection(): Promise<void>;
/**
* Tracks rooms the client has already joined
*/
private readonly joinedRooms;
/**
* Join a WebSocket room before sending messages
*/
joinRoom(messageBox: string): Promise<void>;
listenForLiveMessages({ onMessage, messageBox }: {
onMessage: (message: PeerMessage) => void;
messageBox: string;
}): Promise<void>;
/**
* Sends a message over WebSocket if connected; falls back to HTTP otherwise.
*/
sendLiveMessage({ recipient, messageBox, body }: SendMessageParams): Promise<SendMessageResponse>;
/**
* Leaves a WebSocket room.
*/
leaveRoom(messageBox: string): Promise<void>;
/**
* Closes WebSocket connection.
*/
disconnectWebSocket(): Promise<void>;
/**
* Sends a message via HTTP
*/
sendMessage(message: SendMessageParams): Promise<SendMessageResponse>;
/**
* Lists messages from MessageBoxServer
*/
listMessages({ messageBox }: ListMessagesParams): Promise<PeerMessage[]>;
/**
* Acknowledges one or more messages as having been received
*/
acknowledgeMessage({ messageIds }: AcknowledgeMessageParams): Promise<string>;
}
//# sourceMappingURL=MessageBoxClient.d.ts.map