Skip to content

DaveManager

Manages the DAVE (Discord Audio/Video Encryption) MLS session lifecycle for a voice connection.

Owns the MLS session, encryptor, per-user decryptors, and SSRC-to-user-ID mapping. The owner (Voice.Connection) passes it the voice gateway's DAVE opcodes and sets sendMessage, through which it answers, onTransitionExecuted and onMediaReadyChanged. libdave objects come from createSession, createEncryptor and createDecryptor, which tests replace with stand-ins.

Summary

DaveManager.daveSession  :: Session?
DaveManager.daveEncryptor  :: Encryptor?
DaveManager.daveDecryptors  :: {
{ [string]: dave.Decryptor } }
DaveManager.recognizedUserIds  :: {
{ [string]: true } }

Users connected to the media session: added by op 11, removed by op 13. libdave rejects an add proposal or a welcome naming anyone else. It survives a group reset.

DaveManager.roster  :: {
{ [string]: true } }

Every member of the MLS group, the bot included, mirrored from the change set each commit or welcome returns. Cleared by a group reset.

DaveManager.pendingTransitions  :: {
{ [number]: number } }

The protocol version each prepared transition moves to, by transition id, until it executes.

DaveManager.pendingSelfRatchets  :: {
{ [number]: dave.KeyRatchet } }

The bot's own key ratchet, held by transition id until that transition executes. There is at most one: a newer commit supersedes an older one.

DaveManager.selfRatchetApplied  :: boolean

Whether the encryptor holds a key from the current session. False again after a group reset.

DaveManager.reinitializing  :: boolean

True while recovering from a commit or welcome libdave could not process, so a burst of failures sends one op 31. Cleared by the next processed commit or welcome, or an executed transition.

DaveManager.mediaReady  :: boolean

Whether audio can flow: true when media is not under DAVE (mediaProtocolVersion 0), or under DAVE once the encryptor holds a key from the current session. Never true before Session Description or after cleanup.

DaveManager.ssrcToUserId  :: {
{ [number]: string } }
DaveManager.protocolVersion  :: number

The MLS session's DAVE protocol version: set at Session Description, by a group reset and when a transition executes. 0 without a session.

DaveManager.mediaProtocolVersion  :: number?

The DAVE protocol version media is sent and received under: set at Session Description and when a transition executes, nil before Session Description and after cleanup.

DaveManager.externalSender  :: buffer?

The voice gateway's MLS external sender (op 25), applied to every session a group reset creates.

DaveManager.channelId  :: string

The MLS group id, which is the voice channel's id; set at Session Description.

DaveManager.userId  :: string

The bot's user id; set at Session Description.

DaveManager.ssrc  :: number?

The bot's audio ssrc, which every new encryptor is told is Opus; set at Session Description.

DaveManager.createSession  :: Session

Makes the MLS session; dave.Session.new unless a test replaces it.

DaveManager.createEncryptor  :: Encryptor

Makes the encryptor; dave.Encryptor.new unless a test replaces it.

DaveManager.createDecryptor  :: Decryptor

Makes a decryptor; dave.Decryptor.new unless a test replaces it.

DaveManager.sendMessage  :: ((message: string | buffer, purpose: string) -> ())?

Sends a message to the voice gateway, set by the owner: a string as JSON text, a buffer as a binary frame. purpose names it in logs.

DaveManager.onTransitionExecuted  :: ((rosterSize: number) -> ())?

Set by the owner; called when a transition executes, with the number of group members, the bot included.

DaveManager.onMediaReadyChanged  :: ((ready: boolean) -> ())?

Set by the owner; called with the new value whenever mediaReady changes.

DaveManager.logger  :: Logger
DaveManager : cleanup ( )  -> ()

Destroys the session, the encryptor, every decryptor and every held key ratchet, then resets all DAVE state.

DaveManager : handleSessionDescription ( protocolVersion ,  channelId ,  userId ,  ssrc )  -> ()

Starts DAVE for a new voice session from Session Description's dave_protocol_version. Version 0 needs nothing more; above 0 it creates the MLS session and the encryptor and sends a key package (op 26).

Parameters

protocolVersion: number
channelId: string
userId: string
ssrc: number?
DaveManager : handleClientsConnect ( userIds )  -> ()

Adds users who connected to the media session (op 11) to the recognized set.

Parameters

userIds: { string }
DaveManager : handleClientDisconnect ( userId )  -> ()

Forgets a user who left the media session (op 13): they are no longer recognized, and their decryptor is destroyed.

Parameters

userId: string
DaveManager : handlePrepareTransition ( payload )  -> ()

Handles prepare transition (op 21): records the protocol version the transition moves to, which applies only when it executes. A move to version 0 lets every decryptor accept plain frames now, because senders may switch first. A payload without a numeric transition id and protocol version is ignored.

Parameters

payload: any
DaveManager : handleExecuteTransition ( payload )  -> ()

Handles execute transition (op 22) for a transition this client recorded; any other transition id is ignored, and a non-numeric one is warned.

Parameters

payload: any
DaveManager : hasPendingDowngrade ( )  -> boolean

Whether a transition to protocol version 0 is prepared and not executed yet. Senders may already send plain frames then, so the receive side accepts them.

Returns

boolean
DaveManager : handlePrepareEpoch ( payload )  -> ()

Handles prepare epoch (op 24). Epoch 1 means the voice gateway is creating the group again, so the group is reset at the payload's protocol version, which sends a new key package. Without a numeric version in the payload it falls back to the session's version, and without a version above 0 it keeps the group. Any other epoch keeps the group.

Parameters

payload: any
DaveManager : handleExternalSender ( payload )  -> ()

Stores the voice gateway's MLS external sender (op 25) and applies it to the current session; a group reset applies it to the session it creates. Sends nothing: the key package goes out when a session is created.

Parameters

payload: buffer
DaveManager : handleProposals ( payload )  -> ()

Processes MLS proposals (op 27) against the recognized users and the bot, and sends the resulting commit and welcome (op 28). Sending a commit is not a transition: that arrives with the announced commit (op 29).

Parameters

payload: buffer
DaveManager : handleCommitTransition ( payload )  -> ()

Processes an announced MLS commit (op 29), which starts with a two-byte transition id. A processed commit updates the roster and keys and readies its transition; an ignored one changes nothing; a failed one starts a recovery.

Parameters

payload: buffer
DaveManager : handleWelcome ( payload )  -> ()

Processes an MLS welcome (op 30), which starts with a two-byte transition id, against the recognized users and the bot: libdave checks every member of the welcomed group. A processed welcome updates the roster and keys and readies its transition; a rejected one starts a recovery.

Parameters

payload: buffer
DaveManager . new ( loggerInstance )  -> DaveManager

Responsible for constructing this class.

Parameters

loggerInstance: Logger

Returns

DaveManager