Commit 8069e4ee authored by henry's avatar henry
Browse files

fixup! Lox integration

parent 9209ad02
Loading
Loading
Loading
Loading
+46 −37
Original line number Diff line number Diff line
@@ -128,9 +128,9 @@ class LoxImpl {
  }

  /**
   * Formats and returns bridges from the stored Lox credential
   * Formats and returns bridges from the stored Lox credential.
   *
   * @param {string} loxid The id string associated with a lox credential
   * @param {string} loxid The id string associated with a lox credential.
   *
   * @returns {string[]} An array of formatted bridge lines. The array is empty
   *   if there are no bridges.
@@ -250,6 +250,7 @@ class LoxImpl {
      this.#pubKeyPromise = this.#makeRequest("pubkeys", [])
        .then(pubKeys => {
          this.#pubKeys = JSON.stringify(pubKeys);
          this.#store();
        })
        .catch(() => {
          // We always try to update, but if that doesn't work fall back to stored data
@@ -266,6 +267,7 @@ class LoxImpl {
      this.#encTablePromise = this.#makeRequest("reachability", [])
        .then(encTable => {
          this.#encTable = JSON.stringify(encTable);
          this.#store();
        })
        .catch(() => {
          // Try to update first, but if that doesn't work fall back to stored data
@@ -283,6 +285,7 @@ class LoxImpl {
      this.#constantsPromise = this.#makeRequest("constants", [])
        .then(constants => {
          this.#constants = JSON.stringify(constants);
          this.#store();
        })
        .catch(() => {
          if (!this.#constants) {
@@ -389,18 +392,17 @@ class LoxImpl {
  }

  /**
   * Parses an input string to check if it is a valid Lox invitation
   * Parses an input string to check if it is a valid Lox invitation.
   *
   * @param {string} invite A Lox invitation
   * @returns {Promise<bool>} A promise that resolves to true if the value passed
   * in was a Lox invitation and false if it is not
   * @param {string} invite A Lox invitation.
   * @returns {bool} Whether the value passed in was a Lox invitation.
   */
  async validateInvitation(invite) {
  validateInvitation(invite) {
    if (!this.#initialized) {
      throw new LoxError(LoxErrors.NotInitialized);
    }
    try {
      await lazy.invitation_is_trusted(invite);
      lazy.invitation_is_trusted(invite);
    } catch (err) {
      console.log(err);
      return false;
@@ -420,22 +422,27 @@ class LoxImpl {
  }

  /**
   * Redeems a Lox invitation to obtain a credential and bridges
   * Redeems a Lox invitation to obtain a credential and bridges.
   *
   * @param {string} invite A Lox invitation
   * @returns {Promise<string>} A promise with the loxid of the associated credential on success
   * @param {string} invite A Lox invitation.
   * @returns {string} The loxid of the associated credential on success.
   */
  async redeemInvite(invite) {
    if (!this.#initialized) {
      throw new LoxError(LoxErrors.NotInitialized);
    }
    await this.#getPubKeys();
    let request = await lazy.open_invite(invite.invite);
    let request = await lazy.open_invite(JSON.parse(invite).invite);
    let id = this.#genLoxId();
    let response = await this.#makeRequest(
    let response;
    try {
      response = await this.#makeRequest(
        "openreq",
        JSON.parse(request).request
      );
    } catch {
      throw new LoxError(LoxErrors.LoxServerUnreachable);
    }
    console.log("openreq response: ", response);
    if (response.hasOwnProperty("error")) {
      throw new LoxError(LoxErrors.BadInvite);
@@ -451,9 +458,9 @@ class LoxImpl {
  }

  /**
   * Get metadata on all invites historically generated by this credential
   * Get metadata on all invites historically generated by this credential.
   *
   * @returns {object[]} A list of all historical invites
   * @returns {string[]} A list of all historical invites.
   */
  getInvites() {
    if (!this.#initialized) {
@@ -463,12 +470,14 @@ class LoxImpl {
  }

  /**
   * Generates a new trusted Lox invitation that a user can pass to their contacts
   * Generates a new trusted Lox invitation that a user can pass to their
   * contacts.
   *
   * @returns {Promise<string>} A promise that resolves to a valid Lox invitation. The promise
   *    will reject if:
   *      - there is no saved Lox credential
   *      - the saved credential does not have any invitations available
   * Throws if:
   *  - there is no saved Lox credential, or
   *  - the saved credential does not have any invitations available.
   *
   * @returns {string} A valid Lox invitation.
   */
  async generateInvite() {
    if (!this.#initialized) {
@@ -514,11 +523,10 @@ class LoxImpl {
  }

  /**
   * Get the number of invites that a user has remaining
   * Get the number of invites that a user has remaining.
   *
   * @returns {Promise<int>} A promise with the number of invites that can still be generated
   *    by a user's credential. This promise will reject if:
   *        - There is no credential
   * @returns {int} The number of invites that can still be generated by a
   *   user's credential.
   */
  getRemainingInviteCount() {
    if (!this.#initialized) {
@@ -691,11 +699,10 @@ class LoxImpl {
   */

  /**
   * Get a list of accumulated events
   * Get a list of accumulated events.
   *
   * @returns {Promise<EventData[]>} A promise with a list of the accumulated,
   *   unacknowledged events associated with a user's credential. This promise will reject if
   *        - There is no credential
   * @returns {EventData[]} A list of the accumulated, unacknowledged events
   *   associated with a user's credential.
   */
  getEventData() {
    if (!this.#initialized) {
@@ -709,7 +716,7 @@ class LoxImpl {
  }

  /**
   * Clears accumulated event data
   * Clears accumulated event data.
   */
  clearEventData() {
    if (!this.#initialized) {
@@ -720,7 +727,7 @@ class LoxImpl {
  }

  /**
   * Clears accumulated invitations
   * Clears accumulated invitations.
   */
  clearInvites() {
    if (!this.#initialized) {
@@ -739,7 +746,9 @@ class LoxImpl {
   */

  /**
   * Get dates at which access to new features will unlock
   * Get details about the next feature unlock.
   *
   * @returns {UnlockData} - Details about the next unlock.
   */
  async getNextUnlock() {
    if (!this.#initialized) {
@@ -753,10 +762,10 @@ class LoxImpl {
    let nextUnlocks = JSON.parse(
      lazy.get_next_unlock(this.#constants, this.#credentials[loxid])
    );
    const level = lazy.get_trust_level(this.#credentials[loxid]);
    const level = parseInt(lazy.get_trust_level(this.#credentials[loxid]));
    const unlocks = {
      date: nextUnlocks.trust_level_unlock_date,
      level: level + 1,
      nextLevel: level + 1,
    };
    return unlocks;
  }