key.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.Key = void 0;
  7. const util_1 = __importDefault(require("util"));
  8. const error_1 = require("./error");
  9. const utils_1 = require("./utils");
  10. const key_1 = require("./utils/key");
  11. // A container class representing the public portion of a Key.
  12. class Key {
  13. constructor(options) {
  14. const { keyID, keyType, scheme, keyVal, unrecognizedFields } = options;
  15. this.keyID = keyID;
  16. this.keyType = keyType;
  17. this.scheme = scheme;
  18. this.keyVal = keyVal;
  19. this.unrecognizedFields = unrecognizedFields || {};
  20. }
  21. // Verifies the that the metadata.signatures contains a signature made with
  22. // this key and is correctly signed.
  23. verifySignature(metadata) {
  24. const signature = metadata.signatures[this.keyID];
  25. if (!signature)
  26. throw new error_1.UnsignedMetadataError('no signature for key found in metadata');
  27. if (!this.keyVal.public)
  28. throw new error_1.UnsignedMetadataError('no public key found');
  29. const publicKey = (0, key_1.getPublicKey)({
  30. keyType: this.keyType,
  31. scheme: this.scheme,
  32. keyVal: this.keyVal.public,
  33. });
  34. const signedData = metadata.signed.toJSON();
  35. try {
  36. if (!utils_1.crypto.verifySignature(signedData, publicKey, signature.sig)) {
  37. throw new error_1.UnsignedMetadataError(`failed to verify ${this.keyID} signature`);
  38. }
  39. }
  40. catch (error) {
  41. if (error instanceof error_1.UnsignedMetadataError) {
  42. throw error;
  43. }
  44. throw new error_1.UnsignedMetadataError(`failed to verify ${this.keyID} signature`);
  45. }
  46. }
  47. equals(other) {
  48. if (!(other instanceof Key)) {
  49. return false;
  50. }
  51. return (this.keyID === other.keyID &&
  52. this.keyType === other.keyType &&
  53. this.scheme === other.scheme &&
  54. util_1.default.isDeepStrictEqual(this.keyVal, other.keyVal) &&
  55. util_1.default.isDeepStrictEqual(this.unrecognizedFields, other.unrecognizedFields));
  56. }
  57. toJSON() {
  58. return {
  59. keytype: this.keyType,
  60. scheme: this.scheme,
  61. keyval: this.keyVal,
  62. ...this.unrecognizedFields,
  63. };
  64. }
  65. static fromJSON(keyID, data) {
  66. const { keytype, scheme, keyval, ...rest } = data;
  67. if (typeof keytype !== 'string') {
  68. throw new TypeError('keytype must be a string');
  69. }
  70. if (typeof scheme !== 'string') {
  71. throw new TypeError('scheme must be a string');
  72. }
  73. if (!utils_1.guard.isStringRecord(keyval)) {
  74. throw new TypeError('keyval must be a string record');
  75. }
  76. return new Key({
  77. keyID,
  78. keyType: keytype,
  79. scheme,
  80. keyVal: keyval,
  81. unrecognizedFields: rest,
  82. });
  83. }
  84. }
  85. exports.Key = Key;