metadata.d.ts 2.2 KB

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