24c4c617671733de3a7362d99ced153dd213c56ec138b48dae925eb3ae27faf13707fc2c401b7542409d5d46e21d098395fef4e777139483ce040bb3f111d4 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. var utils = require('../utils');
  3. var assert = utils.assert;
  4. var parseBytes = utils.parseBytes;
  5. var cachedProperty = utils.cachedProperty;
  6. /**
  7. * @param {EDDSA} eddsa - instance
  8. * @param {Object} params - public/private key parameters
  9. *
  10. * @param {Array<Byte>} [params.secret] - secret seed bytes
  11. * @param {Point} [params.pub] - public key point (aka `A` in eddsa terms)
  12. * @param {Array<Byte>} [params.pub] - public key point encoded as bytes
  13. *
  14. */
  15. function KeyPair(eddsa, params) {
  16. this.eddsa = eddsa;
  17. this._secret = parseBytes(params.secret);
  18. if (eddsa.isPoint(params.pub))
  19. this._pub = params.pub;
  20. else
  21. this._pubBytes = parseBytes(params.pub);
  22. }
  23. KeyPair.fromPublic = function fromPublic(eddsa, pub) {
  24. if (pub instanceof KeyPair)
  25. return pub;
  26. return new KeyPair(eddsa, { pub: pub });
  27. };
  28. KeyPair.fromSecret = function fromSecret(eddsa, secret) {
  29. if (secret instanceof KeyPair)
  30. return secret;
  31. return new KeyPair(eddsa, { secret: secret });
  32. };
  33. KeyPair.prototype.secret = function secret() {
  34. return this._secret;
  35. };
  36. cachedProperty(KeyPair, 'pubBytes', function pubBytes() {
  37. return this.eddsa.encodePoint(this.pub());
  38. });
  39. cachedProperty(KeyPair, 'pub', function pub() {
  40. if (this._pubBytes)
  41. return this.eddsa.decodePoint(this._pubBytes);
  42. return this.eddsa.g.mul(this.priv());
  43. });
  44. cachedProperty(KeyPair, 'privBytes', function privBytes() {
  45. var eddsa = this.eddsa;
  46. var hash = this.hash();
  47. var lastIx = eddsa.encodingLength - 1;
  48. var a = hash.slice(0, eddsa.encodingLength);
  49. a[0] &= 248;
  50. a[lastIx] &= 127;
  51. a[lastIx] |= 64;
  52. return a;
  53. });
  54. cachedProperty(KeyPair, 'priv', function priv() {
  55. return this.eddsa.decodeInt(this.privBytes());
  56. });
  57. cachedProperty(KeyPair, 'hash', function hash() {
  58. return this.eddsa.hash().update(this.secret()).digest();
  59. });
  60. cachedProperty(KeyPair, 'messagePrefix', function messagePrefix() {
  61. return this.hash().slice(this.eddsa.encodingLength);
  62. });
  63. KeyPair.prototype.sign = function sign(message) {
  64. assert(this._secret, 'KeyPair can only verify');
  65. return this.eddsa.sign(message, this);
  66. };
  67. KeyPair.prototype.verify = function verify(message, sig) {
  68. return this.eddsa.verify(message, sig, this);
  69. };
  70. KeyPair.prototype.getSecret = function getSecret(enc) {
  71. assert(this._secret, 'KeyPair is public only');
  72. return utils.encode(this.secret(), enc);
  73. };
  74. KeyPair.prototype.getPublic = function getPublic(enc) {
  75. return utils.encode(this.pubBytes(), enc);
  76. };
  77. module.exports = KeyPair;