TypeScript
Documentation in code

by Benny Neugebauer

/**
 * @param {string} session - Session ID
 * @param {Uint8Array} typedArray - Plaintext
 * @param {string} encodedPreKey - Base64-encoded PreKey
 * @returns {Promise<SessionPayloadBundle>} Object containing session ID & encrypted payload.
 */
private encryptPayloadForSession(session: string,
                                 typedArray: Uint8Array,
                                 encodedPreKey: string): Promise<SessionPayloadBundle> {
  const decodedPreKeyBundle: Uint8Array = Decoder.fromBase64(encodedPreKey).asBytes;
  return this.cryptobox
    .encrypt(session, typedArray, decodedPreKeyBundle.buffer)
    .then(encryptedPayload => Encoder.toBase64(encryptedPayload).asString)
    .catch(error => '๐Ÿ’ฃ')
    .then(encryptedPayload => ({sessionId: session, encryptedPayload}));
}

private encryptPayloadForSession(sessionId: string,
                                 plainText: Uint8Array,
                                 base64EncodedPreKey: string): Promise<SessionPayloadBundle> {
  const decodedPreKeyBundle: Uint8Array = Decoder.fromBase64(base64EncodedPreKey).asBytes;
  return this.cryptobox
    .encrypt(sessionId, plainText, decodedPreKeyBundle.buffer)
    .then(encryptedPayload => Encoder.toBase64(encryptedPayload).asString)
    .catch(error => '๐Ÿ’ฃ')
    .then(encryptedPayload => ({sessionId, encryptedPayload}));
}