index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. var ZeroKnowledgeProofService = require('./service');
  11. var validator = require('./input-validator');
  12. module.exports = {
  13. /**
  14. * Creates a new ZeroKnowledgeProofService object, which encapsulates a
  15. * zero-knowledge proof service.
  16. *
  17. * @function newService
  18. * @global
  19. * @param {Object}
  20. * [options] An object containing optional arguments.
  21. * @param {Policy}
  22. * [options.policy=Default policy] The cryptographic policy to
  23. * use.
  24. * @param {MessageDigestService}
  25. * [options.messageDigestService=Created internally] The message
  26. * digest service to use.
  27. * @param {SecureRandomService}
  28. * [options.secureRandomService=Created internally] The secure
  29. * random service to use.
  30. * @param {MathematicalService}
  31. * [options.mathematicalService=Created internally] The
  32. * mathematical service to use.
  33. * @returns {ZeroKnowledgeProofService} The new ZeroKnowledgeProofService
  34. * object.
  35. * @throws {Error}
  36. * If the input data validation fails.
  37. * @example <caption> How to use a cryptographic policy that sets the
  38. * message digest algorithm to <code>SHA-512</code></caption>
  39. *
  40. * var cryptoPolicy = require('scytl-cryptopolicy');
  41. * var zkProof = require('scytl-zkproof');
  42. *
  43. * var myPolicy = cryptoPolicy.newInstance();
  44. *
  45. * myPolicy.proofs.messagedigest.algorithm =
  46. * cryptoPolicy.options.proofs.messageDigest.algorithm.SHA512_224;
  47. *
  48. * var proofService = zkProof.newService({policy: myPolicy});
  49. */
  50. newService: function(options) {
  51. checkData(options);
  52. return new ZeroKnowledgeProofService(options);
  53. }
  54. };
  55. function checkData(options) {
  56. options = options || {};
  57. if (typeof options.policy !== 'undefined') {
  58. validator.checkIsObjectWithProperties(
  59. options.policy,
  60. 'Cryptographic policy provided to zero-knowledge proof service');
  61. }
  62. if (typeof options.messageDigestService !== 'undefined') {
  63. validator.checkIsObjectWithProperties(
  64. options.messageDigestService,
  65. 'Message digest service object provided to zero-knowledge proof service');
  66. }
  67. if (typeof options.secureRandomService !== 'undefined') {
  68. validator.checkIsObjectWithProperties(
  69. options.secureRandomService,
  70. 'Secure random service object provided to zero-knowledge proof service');
  71. }
  72. if (typeof options.mathematicalService !== 'undefined') {
  73. validator.checkIsObjectWithProperties(
  74. options.mathematicalService,
  75. 'Mathematical service object provided to zero-knowledge proof service');
  76. }
  77. }