UNPKG

pocket-messaging

Version:

A small cryptographic messaging library written in TypeScript both for browser and nodejs supporting TCP and WebSockets

50 lines (49 loc) 3.06 kB
/** * A four way client-server handshake, as excellently described in * https://ssbc.github.io/scuttlebutt-protocol-guide/ with a few additions: * * This protocols has one added version byte, one added difficulty byte, 4 byte added nonce bytes, * and a 6 byte added clock timestamp and added optional client/server data exchange for * swapping application parameters of default maximum 2048 bytes. * * The added difficulty byte can be used to force the client to calculate a nonce to match the * difficulty level set by the server for the handshake to complete. */ /// <reference types="node" /> import { ClientInterface } from "pocket-sockets"; import { HandshakeResult } from "./types"; export declare function writeUInt64BE(target: Buffer, nr: bigint): void; export declare function readUInt64BE(source: Buffer): bigint; /** * On successful handshake return a populated HandshakeResult object. * On unsuccessful throw exception. * @return Promise <HandshakeResult> * @throws */ export declare function HandshakeAsClient(client: ClientInterface, clientLongtermSk: Buffer, clientLongtermPk: Buffer, serverLongtermPk: Buffer, discriminator: Buffer, clientData?: Buffer, clock?: number, maxServerDataSize?: number, timeout?: number): Promise<HandshakeResult>; /** * On successful handshake return a populated HandshakeResult object. * * On failed handshake throw exception. * * @param client to send and retrieve data on * @param serverLongtermSk this side's long term secret key * @param serverLongtermPk this side's long term public key * @param discriminator arbitrary data that must exactly match the client discriminator data * @param allowedClientKeys if Buffer array it contains all client public keys allowed to handshake. * If a function the function has to return true for the client to be allowed. * If undefined then allow all clients to handshake. * @param serverData optional data to pass to the client upon successful handshake. Its length cannot * exceed the client's maximum allowed length. * @param clock timestamp in milliseconds for when starting the handshake * This value will be adjusted upwards to be as near as possible for when the clock was sent * @param difficulty is the number of nibbles the client is required to calculate to mitigate ddos * attacks. Difficulty 6 is a lot. 8 is max. * If the client does not provide a requested nonce then the handshake is aborted after the clients * second message is retrieved. * @param maxClientDataSize the maximum length allowed for the client to pass its optional data. * @param timeout in milliseconds to wait for each message before aborting. * @return Promise<HandshakeResult> * @throws on error */ export declare function HandshakeAsServer(client: ClientInterface, serverLongtermSk: Buffer, serverLongtermPk: Buffer, discriminator: Buffer, allowedClientKeys?: ((clientLongtermPk: Buffer) => boolean) | Buffer[], serverData?: Buffer, clock?: number, difficulty?: number, maxClientDataSize?: number, timeout?: number): Promise<HandshakeResult>;