shamap.d.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Hash256 } from './types/hash-256';
  2. import { BytesList } from './serdes/binary-serializer';
  3. import { Buffer } from 'buffer/';
  4. /**
  5. * Abstract class describing a SHAMapNode
  6. */
  7. declare abstract class ShaMapNode {
  8. abstract hashPrefix(): Buffer;
  9. abstract isLeaf(): boolean;
  10. abstract isInner(): boolean;
  11. abstract toBytesSink(list: BytesList): void;
  12. abstract hash(): Hash256;
  13. }
  14. /**
  15. * Class describing a Leaf of SHAMap
  16. */
  17. declare class ShaMapLeaf extends ShaMapNode {
  18. index: Hash256;
  19. item?: ShaMapNode | undefined;
  20. constructor(index: Hash256, item?: ShaMapNode | undefined);
  21. /**
  22. * @returns true as ShaMapLeaf is a leaf node
  23. */
  24. isLeaf(): boolean;
  25. /**
  26. * @returns false as ShaMapLeaf is not an inner node
  27. */
  28. isInner(): boolean;
  29. /**
  30. * Get the prefix of the this.item
  31. *
  32. * @returns The hash prefix, unless this.item is undefined, then it returns an empty Buffer
  33. */
  34. hashPrefix(): Buffer;
  35. /**
  36. * Hash the bytes representation of this
  37. *
  38. * @returns hash of this.item concatenated with this.index
  39. */
  40. hash(): Hash256;
  41. /**
  42. * Write the bytes representation of this to a BytesList
  43. * @param list BytesList to write bytes to
  44. */
  45. toBytesSink(list: BytesList): void;
  46. }
  47. /**
  48. * Class defining an Inner Node of a SHAMap
  49. */
  50. declare class ShaMapInner extends ShaMapNode {
  51. private depth;
  52. private slotBits;
  53. private branches;
  54. constructor(depth?: number);
  55. /**
  56. * @returns true as ShaMapInner is an inner node
  57. */
  58. isInner(): boolean;
  59. /**
  60. * @returns false as ShaMapInner is not a leaf node
  61. */
  62. isLeaf(): boolean;
  63. /**
  64. * Get the hash prefix for this node
  65. *
  66. * @returns hash prefix describing an inner node
  67. */
  68. hashPrefix(): Buffer;
  69. /**
  70. * Set a branch of this node to be another node
  71. *
  72. * @param slot Slot to add branch to this.branches
  73. * @param branch Branch to add
  74. */
  75. setBranch(slot: number, branch: ShaMapNode): void;
  76. /**
  77. * @returns true if node is empty
  78. */
  79. empty(): boolean;
  80. /**
  81. * Compute the hash of this node
  82. *
  83. * @returns The hash of this node
  84. */
  85. hash(): Hash256;
  86. /**
  87. * Writes the bytes representation of this node to a BytesList
  88. *
  89. * @param list BytesList to write bytes to
  90. */
  91. toBytesSink(list: BytesList): void;
  92. /**
  93. * Add item to the SHAMap
  94. *
  95. * @param index Hash of the index of the item being inserted
  96. * @param item Item to insert in the map
  97. * @param leaf Leaf node to insert when branch doesn't exist
  98. */
  99. addItem(index?: Hash256, item?: ShaMapNode, leaf?: ShaMapLeaf): void;
  100. }
  101. declare class ShaMap extends ShaMapInner {
  102. }
  103. export { ShaMap, ShaMapNode, ShaMapLeaf };