UNPKG

xud

Version:
61 lines (60 loc) 2.32 kB
/// <reference types="node" /> import PacketType from './PacketType'; declare type PacketHeader = { /** An identifer for the packet which must be unique for a given socket. */ id: string; /** The id of the received packet to which this packet is responding. */ reqId?: string; }; interface PacketInterface { body?: any; header: PacketHeader; } declare enum PacketDirection { /** A packet that is pushed to a peer without expecting any response. */ Unilateral = 0, /** A packet requesting a response. */ Request = 1, /** A packet that is sent in response to an incoming packet. */ Response = 2 } declare type ResponseType = PacketType | PacketType[] | undefined; declare function isPacketType(val: any): val is PacketType; declare function isPacketTypeArray(val: any): val is PacketType[]; /** * Represents a packet of data that can be transmitted as part of the p2p xud protocol. Packets * are serialized using protobuf, optionally encrypted, and transmitted to peers. Each packet * represents a discrete chunk of information that either sends data to or requests data from a * peer. */ declare abstract class Packet<T = any> implements PacketInterface { abstract get type(): PacketType; abstract get direction(): PacketDirection; abstract get responseType(): ResponseType; body?: T; header: PacketHeader; /** * Create a packet from a deserialized packet message. * @param packet a deserialized object containing a packet header and optional body */ constructor(packet: PacketInterface); /** * Create a packet from a packet body. * @param reqId the id of the requesting packet to set on the header if this packet is a response */ constructor(body?: T, reqId?: string); abstract serialize(): Uint8Array; toJSON: () => string; /** * Serialize this packet to binary Buffer. * @returns Buffer representation of the packet */ toRaw: () => Buffer; /** * Calculating the packet checksum using its JSON representation hash first 4 bytes. */ checksum: () => number; } declare function isPacket(val: any): val is Packet; export default Packet; export { PacketHeader, PacketDirection, ResponseType, PacketInterface, isPacket, isPacketType, isPacketTypeArray };