{"size": 1502, "ext": "js", "max_stars_repo_path": "src/services/MemoService.js", "max_stars_repo_name": "mseaward/peerplays-core-gui", "max_stars_repo_stars_event_min_datetime": "2017-06-12T14:34:01.000Z", "max_stars_repo_stars_event_max_datetime": "2019-12-06T13:07:30.000Z", "max_issues_repo_path": "src/services/MemoService.js", "max_issues_repo_name": "mseaward/peerplays-core-gui", "max_issues_repo_issues_event_min_datetime": "2019-04-02T16:33:21.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-25T18:47:30.000Z", "max_forks_repo_path": "src/services/MemoService.js", "max_forks_repo_name": "mseaward/peerplays-core-gui", "max_forks_repo_forks_event_min_datetime": "2017-06-12T17:09:54.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-26T08:12:58.000Z"}
{"max_stars_count": 41, "max_issues_count": 67, "max_forks_count": 18, "avg_line_length": 33.3777777778, "max_line_length": 100, "alphanum_fraction": 0.6704394141}
import {Aes} from 'peerplaysjs-lib';

/**
 * Service for work with memos
 */
class MemoService {
  /**
   * decodeMemo()
   *
   * Decodes a memo using the provided keys
   *
   * @static
   * @param {*} receiverPrivateMemo - The Private Memo Key of the receiver of the memo
   * @param {*} senderPublicMemo - The Public Memo Key of the sender of the memo
   * @param {*} nonce - The generated nonce of the memo
   * @param {*} encryptedMessage - The actual encrypted memo itself.
   *                               This is the message to be decoded.
   * @returns {String} - The unencrypted memo, or an error message if the memo could not be decoded.
   * @memberof MemoService
   */
  static decodeMemo(privateMemo, publicMemo, nonce, encryptedMessage) {
    // Using the AES library to decrypt the memo message using the private Memo key of the receiver
    // and the public memo key of the sender.
    let decryptedMessage = Aes.decrypt_with_checksum(
      privateMemo, publicMemo, nonce, encryptedMessage
    );

    // The message then needs to be decoded using the default browser TextDecoder
    let decoder = new TextDecoder('utf-8');

    // Try to decode the message and throw an error if the decoing is unsuccessful.
    let message;

    try {
      // Parse the resulting plain text into a JSON object
      message = decoder.decode(decryptedMessage);
    } catch (error) {
      message = 'Could not decode memo: ' + error;
    }

    return message;
  }
}

export default MemoService;