{"size": 1544, "ext": "js", "max_stars_repo_path": "src/keri/core/verfer.js", "max_stars_repo_name": "Spherity/kerijs", "max_stars_repo_stars_event_min_datetime": "2020-06-03T17:55:49.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-02T00:38:47.000Z", "max_issues_repo_path": "src/keri/core/verfer.js", "max_issues_repo_name": "Spherity/kerijs", "max_issues_repo_issues_event_min_datetime": "2020-05-01T21:43:21.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T20:07:52.000Z", "max_forks_repo_path": "src/keri/core/verfer.js", "max_forks_repo_name": "Spherity/kerijs", "max_forks_repo_forks_event_min_datetime": "2020-05-28T08:29:35.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-25T15:54:24.000Z"}
{"max_stars_count": 9, "max_issues_count": 5, "max_forks_count": 2, "avg_line_length": 29.6923076923, "max_line_length": 106, "alphanum_fraction": 0.6444300518}
const libsodium = require('libsodium-wrappers-sumo');
const { Crymat } = require('./cryMat');
const codeAndLength = require('./derivationCode&Length');

/**
 * @description  Verfer :sublclass of crymat,helps to verify signature of serialization
 *  using .raw as verifier key and .code as signature cypher suite
 */
class Verfer extends Crymat {
  // eslint-disable-next-line max-len
  constructor(raw = null, qb64 = null, qb2 = null, code = codeAndLength.oneCharCode.Ed25519N, index = 0) {
    super(raw, qb64, qb2, code, index);
    if (Object.values(codeAndLength.oneCharCode.Ed25519N).includes(this.getCode)
            || Object.values(codeAndLength.oneCharCode.Ed25519).includes(this.getCode)) {
      this.verifySig = this.ed25519;
    } else {
      throw new Error(`Unsupported code = ${this.getCode} for verifier.`);
    }
  }

  /**
     *
     * @param {bytes} sig   bytes signature
     * @param {bytes} ser   bytes serialization
     */
  verify(sig, ser) {
    return this.verifySig(sig, ser, this.raw());
  }

  /**
     * @description This method will verify ed25519 signature on Serialization using  public key
     * @param {bytes} sig
     * @param {bytes} ser
     * @param {bytes} key
     */

  // eslint-disable-next-line class-methods-use-this
  ed25519(sig, ser, key) {
    try {
      const result = libsodium.crypto_sign_verify_detached(sig, ser, key);
      if (result) {
        return true;
      }
      return false;
    } catch (error) {
      throw new Error(error);
    }
  }
}

module.exports = { Verfer };
