Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Client

Client provides an interface for you to use a vex chat server and send end to end encrypted messages to other users.

example
import { Client } from "@vex-chat/libvex";

async function main() {
    // generate a secret key to use, save this somewhere permanent
    const privateKey = Client.generateSecretKey();

    const client = new Client(privateKey);

    // the ready event is emitted when init() is finished.
    // you must wait until this event fires to perform
    // registration or login.
    client.on("ready", async () => {
        // you must register once before you can log in
        await client.register(Client.randomUsername());
        await client.login();
    })

    // The authed event fires when login() successfully completes
    // and the server indicates you are authorized. You must wait to
    // perform any operations besides register() and login() until
    // this occurs.
    client.on("authed", async () => {
        const me = await client.users.me();

        // send a message
        await client.messages.send(me.userID, "Hello world!");
    })

    // Outgoing and incoming messages are emitted here.
    client.on("message", (message) => {
        console.log("message:", message);
    })

    // you must call init() to initialize the keyring and
    // start the client.
    client.init();
}

main();

Hierarchy

  • EventEmitter
    • Client

Index

Constructors

constructor

Events

on

  • on(event: "ready", callback: () => void): this
  • on(event: "authed", callback: () => void): this
  • on(event: "message", callback: (message: IMessage) => void): this
  • on(event: "permission", callback: (permission: IPermission) => void): this
  • on(event: "session", callback: (session: ISession, user: IUser) => void): this
  • on(event: "disconnect", callback: () => void): this
  • on(event: "closed", callback: () => void): this
  • This is emitted whenever the keyring is done initializing after an init() call. You must wait to login or register until after this event.

    Example:

      client.on("ready", () => {
          await client.register()
      });

    Parameters

    • event: "ready"
    • callback: () => void
        • (): void
        • Returns void

    Returns this

  • This is emitted when you are logged in succesfully. You can now call the rest of the methods in the api.

    Example:

      client.on("authed", (user) => {
          // do something
      });

    Parameters

    • event: "authed"
    • callback: () => void
        • (): void
        • Returns void

    Returns this

  • This is emitted for every sent and received message.

    Example:

    
      client.on("message", (msg: IMessage) => {
          console.log(message);
      });

    Parameters

    • event: "message"
    • callback: (message: IMessage) => void

    Returns this

  • This is emitted when the user is granted a new permission.

    Example:

    
      client.on("permission", (perm: IPermission) => {
          console.log(perm);
      });

    Parameters

    Returns this

  • This is emitted for a new encryption session being created with a specific user.

    Example:

    
      client.on("session", (session: ISession, user: IUser) => {
          console.log(session);
          console.log(user);
      });

    Parameters

    Returns this

  • This is emitted whenever the connection is closed. You must discard the client and connect again with a fresh one.

    Example:

    
      client.on("disconnect", () => {
        // do something
      });

    Parameters

    • event: "disconnect"
    • callback: () => void
        • (): void
        • Returns void

    Returns this

  • This is emitted whenever the close() event is called and completed successfully. Note this is not fired for an unintentional disconnect, see the disconnect event.

    Example:

    
      client.on("closed", () => {
          process.exit(0);
      });

    Parameters

    • event: "closed"
    • callback: () => void
        • (): void
        • Returns void

    Returns this

Properties

hasInit

hasInit: boolean = false

This is true if the client has ever been initialized. You can only initialize a client once.

hasLoggedIn

hasLoggedIn: boolean = false

This is true if the client has ever logged in before. You can only login a client once.

Static loadKeyFile

loadKeyFile: (path: string, password: string) => string = XUtils.loadKeyFile

Type declaration

    • (path: string, password: string): string
    • Parameters

      • path: string
      • password: string

      Returns string

Static saveKeyFile

saveKeyFile: (path: string, password: string, keyToSave: string, iterationOverride?: number | undefined) => void = XUtils.saveKeyFile

Type declaration

    • (path: string, password: string, keyToSave: string, iterationOverride?: number | undefined): void
    • Parameters

      • path: string
      • password: string
      • keyToSave: string
      • Optional iterationOverride: number | undefined

      Returns void

Methods

close

  • close(muteEvent?: boolean): Promise<void>
  • Manually closes the client. Emits the closed event on successful shutdown.

    Parameters

    • Default value muteEvent: boolean = false

    Returns Promise<void>

getKeys

  • Gets the hex string representations of the public and private keys.

    Returns IKeys

init

  • init(): Promise<undefined | Error>
  • Initializes the keyring. This must be called before anything else.

    Returns Promise<undefined | Error>

login

  • login(): Promise<Error | null>
  • Logs in to the server. You must have registered() before with your current private key.

    Returns Promise<Error | null>

register

  • register(username: string): Promise<[IUser | null, Error | null]>
  • Registers a new account on the server.

    example

    [user, err] = await client.register("MyUsername");

    Parameters

    • username: string

      The username to register. Must be unique.

    Returns Promise<[IUser | null, Error | null]>

    The error, or the user object.

Static generateSecretKey

  • generateSecretKey(): string
  • Generates an ed25519 secret key as a hex string.

    Returns string

    • A secret key to use for the client. Save it permanently somewhere safe.

Static randomUsername

  • randomUsername(): string
  • Generates a random username using bip39.

    Returns string

    • The username.

Object literals

channels

channels: object

create

create: createChannel = this.createChannel.bind(this)

Creates a new channel in a server.

param

The channel name.

param

The unique serverID to create the channel in.

returns
  • The created IChannel object.

delete

delete: deleteChannel = this.deleteChannel.bind(this)

Deletes a channel.

param

The unique channelID to delete.

retrieve

retrieve: getChannelList = this.getChannelList.bind(this)

Retrieves all channels in a server.

returns
  • The list of IChannel objects.

retrieveByID

retrieveByID: getChannelByID = this.getChannelByID.bind(this)

Retrieves channel details by its unique channelID.

returns
  • The list of IChannel objects.

files

files: object

The IMessages interface contains methods for dealing with messages.

create

create: createFile = this.createFile.bind(this)

Uploads an encrypted file and returns the details and the secret key.

param

The file as a Buffer.

returns

Details of the file uploaded and the key to encrypt in the form [details, key].

retrieve

retrieve: retrieveFile = this.retrieveFile.bind(this)

messages

messages: object

The IMessages interface contains methods for dealing with messages.

group

group: sendGroupMessage = this.sendGroupMessage.bind(this)

Send a group message to a channel.

param

The channelID of the channel to send a message to.

param

The message to send.

retrieve

retrieve: getMessageHistory = this.getMessageHistory.bind(this)

Gets the message history with a specific userID.

param

The userID of the user to retrieve message history for.

returns
  • The list of IMessage objects.

retrieveGroup

retrieveGroup: getGroupHistory = this.getGroupHistory.bind(this)

Gets the group message history with a specific channelID.

param

The channelID of the channel to retrieve message history for.

returns
  • The list of IMessage objects.

send

send: sendMessage = this.sendMessage.bind(this)

Send a direct message.

param

The userID of the user to send a message to.

param

The message to send.

permissions

permissions: object

The IPermissions object contains all methods for dealing with permissions.

create

create: createPermission = this.createPermission.bind(this)

Creates a new permission for the givern resourceID and userID.

param

The new permission details.

returns
  • The list of IPermissions objects.

retrieve

retrieve: getPermissions = this.getPermissions.bind(this)

servers

servers: object

create

create: createServer = this.createServer.bind(this)

Creates a new server.

param

The server name.

returns
  • The created IServer object.

delete

delete: deleteServer = this.deleteServer.bind(this)

Deletes a server.

param

The unique serverID to delete.

retrieve

retrieve: getServerList = this.getServerList.bind(this)

Retrieves all servers the logged in user has access to.

returns
  • The list of IServer objects.

retrieveByID

retrieveByID: getServerByID = this.getServerByID.bind(this)

Retrieves server details by its unique serverID.

returns
  • The requested IServer object, or null if the id does not exist.

sessions

sessions: object

The ISessions interface contains methods for dealing with encryption sessions.

markVerified

markVerified: markSessionVerified = this.markSessionVerified.bind(this)

Marks a mnemonic verified, implying that the the user has confirmed that the session mnemonic matches with the other user.

param

the sessionID of the session to mark.

param

Optionally, what to mark it as. Defaults to true.

retrieve

retrieve: getSessionList = this.getSessionList.bind(this)

Gets all encryption sessions.

returns
  • The list of ISession encryption sessions.

verify

verify: getMnemonic = Client.getMnemonic

Returns a mnemonic for the session, to verify with the other user.

param

the ISession object to get the mnemonic for.

returns
  • The mnemonic representation of the session.

users

users: object

The IUsers interface contains methods for dealing with users.

familiars

familiars: getFamiliars = this.getFamiliars.bind(this)

Retrieves the list of users you can currently access, or are already familiar with.

returns
  • The list of IUser objects.

me

me: getUser = this.getUser.bind(this)

Retrieves the currently logged in user's (you) information.

returns
  • The logged in user's IUser object.

retrieve

retrieve: retrieveUserDBEntry = this.retrieveUserDBEntry.bind(this)

Retrieves a user's information by a string identifier.

param

A userID, hex string public key, or a username.

returns
  • The user's IUser object, or null if the user does not exist.

Generated using TypeDoc