metadata.d.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /// <reference types="node" />
  2. import { MetadataKind, Signable } from './base';
  3. import { Root } from './root';
  4. import { Signature } from './signature';
  5. import { Snapshot } from './snapshot';
  6. import { Targets } from './targets';
  7. import { Timestamp } from './timestamp';
  8. import { JSONObject, JSONValue } from './utils';
  9. type MetadataType = Root | Timestamp | Snapshot | Targets;
  10. /***
  11. * A container for signed TUF metadata.
  12. *
  13. * Provides methods to convert to and from json, read and write to and
  14. * from JSON and to create and verify metadata signatures.
  15. *
  16. * ``Metadata[T]`` is a generic container type where T can be any one type of
  17. * [``Root``, ``Timestamp``, ``Snapshot``, ``Targets``]. The purpose of this
  18. * is to allow static type checking of the signed attribute in code using
  19. * Metadata::
  20. *
  21. * root_md = Metadata[Root].fromJSON("root.json")
  22. * # root_md type is now Metadata[Root]. This means signed and its
  23. * # attributes like consistent_snapshot are now statically typed and the
  24. * # types can be verified by static type checkers and shown by IDEs
  25. *
  26. * Using a type constraint is not required but not doing so means T is not a
  27. * specific type so static typing cannot happen. Note that the type constraint
  28. * ``[Root]`` is not validated at runtime (as pure annotations are not available
  29. * then).
  30. *
  31. * Apart from ``expires`` all of the arguments to the inner constructors have
  32. * reasonable default values for new metadata.
  33. */
  34. export declare class Metadata<T extends MetadataType> implements Signable {
  35. signed: T;
  36. signatures: Record<string, Signature>;
  37. unrecognizedFields: Record<string, JSONValue>;
  38. constructor(signed: T, signatures?: Record<string, Signature>, unrecognizedFields?: Record<string, JSONValue>);
  39. sign(signer: (data: Buffer) => Signature, append?: boolean): void;
  40. verifyDelegate(delegatedRole: string, delegatedMetadata: Metadata<MetadataType>): void;
  41. equals(other: T): boolean;
  42. toJSON(): JSONObject;
  43. static fromJSON(type: MetadataKind.Root, data: JSONObject): Metadata<Root>;
  44. static fromJSON(type: MetadataKind.Timestamp, data: JSONObject): Metadata<Timestamp>;
  45. static fromJSON(type: MetadataKind.Snapshot, data: JSONObject): Metadata<Snapshot>;
  46. static fromJSON(type: MetadataKind.Targets, data: JSONObject): Metadata<Targets>;
  47. }
  48. export {};