ledger-hashes.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.decodeLedgerData = exports.ledgerHash = exports.transactionTreeHash = exports.accountStateHash = void 0;
  4. var assert = require("assert");
  5. var shamap_1 = require("./shamap");
  6. var hash_prefixes_1 = require("./hash-prefixes");
  7. var hashes_1 = require("./hashes");
  8. var binary_1 = require("./binary");
  9. var hash_256_1 = require("./types/hash-256");
  10. var st_object_1 = require("./types/st-object");
  11. var uint_64_1 = require("./types/uint-64");
  12. var uint_32_1 = require("./types/uint-32");
  13. var uint_8_1 = require("./types/uint-8");
  14. var binary_parser_1 = require("./serdes/binary-parser");
  15. var bigInt = require("big-integer");
  16. /**
  17. * Computes the hash of a list of objects
  18. *
  19. * @param itemizer Converts an item into a format that can be added to SHAMap
  20. * @param itemsJson Array of items to add to a SHAMap
  21. * @returns the hash of the SHAMap
  22. */
  23. function computeHash(itemizer, itemsJson) {
  24. var map = new shamap_1.ShaMap();
  25. itemsJson.forEach(function (item) { return map.addItem.apply(map, itemizer(item)); });
  26. return map.hash();
  27. }
  28. /**
  29. * Convert a transaction into an index and an item
  30. *
  31. * @param json transaction with metadata
  32. * @returns a tuple of index and item to be added to SHAMap
  33. */
  34. function transactionItemizer(json) {
  35. assert.ok(json.hash);
  36. var index = hash_256_1.Hash256.from(json.hash);
  37. var item = {
  38. hashPrefix: function () {
  39. return hash_prefixes_1.HashPrefix.transaction;
  40. },
  41. toBytesSink: function (sink) {
  42. var serializer = new binary_1.BinarySerializer(sink);
  43. serializer.writeLengthEncoded(st_object_1.STObject.from(json));
  44. serializer.writeLengthEncoded(st_object_1.STObject.from(json.metaData));
  45. },
  46. };
  47. return [index, item, undefined];
  48. }
  49. /**
  50. * Convert an entry to a pair Hash256 and ShaMapNode
  51. *
  52. * @param json JSON describing a ledger entry item
  53. * @returns a tuple of index and item to be added to SHAMap
  54. */
  55. function entryItemizer(json) {
  56. var index = hash_256_1.Hash256.from(json.index);
  57. var bytes = (0, binary_1.serializeObject)(json);
  58. var item = {
  59. hashPrefix: function () {
  60. return hash_prefixes_1.HashPrefix.accountStateEntry;
  61. },
  62. toBytesSink: function (sink) {
  63. sink.put(bytes);
  64. },
  65. };
  66. return [index, item, undefined];
  67. }
  68. /**
  69. * Function computing the hash of a transaction tree
  70. *
  71. * @param param An array of transaction objects to hash
  72. * @returns A Hash256 object
  73. */
  74. function transactionTreeHash(param) {
  75. var itemizer = transactionItemizer;
  76. return computeHash(itemizer, param);
  77. }
  78. exports.transactionTreeHash = transactionTreeHash;
  79. /**
  80. * Function computing the hash of accountState
  81. *
  82. * @param param A list of accountStates hash
  83. * @returns A Hash256 object
  84. */
  85. function accountStateHash(param) {
  86. var itemizer = entryItemizer;
  87. return computeHash(itemizer, param);
  88. }
  89. exports.accountStateHash = accountStateHash;
  90. /**
  91. * Serialize and hash a ledger header
  92. *
  93. * @param header a ledger header
  94. * @returns the hash of header
  95. */
  96. function ledgerHash(header) {
  97. var hash = new hashes_1.Sha512Half();
  98. hash.put(hash_prefixes_1.HashPrefix.ledgerHeader);
  99. assert.ok(header.parent_close_time !== undefined);
  100. assert.ok(header.close_flags !== undefined);
  101. uint_32_1.UInt32.from(header.ledger_index).toBytesSink(hash);
  102. uint_64_1.UInt64.from(bigInt(String(header.total_coins))).toBytesSink(hash);
  103. hash_256_1.Hash256.from(header.parent_hash).toBytesSink(hash);
  104. hash_256_1.Hash256.from(header.transaction_hash).toBytesSink(hash);
  105. hash_256_1.Hash256.from(header.account_hash).toBytesSink(hash);
  106. uint_32_1.UInt32.from(header.parent_close_time).toBytesSink(hash);
  107. uint_32_1.UInt32.from(header.close_time).toBytesSink(hash);
  108. uint_8_1.UInt8.from(header.close_time_resolution).toBytesSink(hash);
  109. uint_8_1.UInt8.from(header.close_flags).toBytesSink(hash);
  110. return hash.finish();
  111. }
  112. exports.ledgerHash = ledgerHash;
  113. /**
  114. * Decodes a serialized ledger header
  115. *
  116. * @param binary A serialized ledger header
  117. * @returns A JSON object describing a ledger header
  118. */
  119. function decodeLedgerData(binary) {
  120. assert.ok(typeof binary === 'string', 'binary must be a hex string');
  121. var parser = new binary_parser_1.BinaryParser(binary);
  122. return {
  123. ledger_index: parser.readUInt32(),
  124. total_coins: parser.readType(uint_64_1.UInt64).valueOf().toString(),
  125. parent_hash: parser.readType(hash_256_1.Hash256).toHex(),
  126. transaction_hash: parser.readType(hash_256_1.Hash256).toHex(),
  127. account_hash: parser.readType(hash_256_1.Hash256).toHex(),
  128. parent_close_time: parser.readUInt32(),
  129. close_time: parser.readUInt32(),
  130. close_time_resolution: parser.readUInt8(),
  131. close_flags: parser.readUInt8(),
  132. };
  133. }
  134. exports.decodeLedgerData = decodeLedgerData;
  135. //# sourceMappingURL=ledger-hashes.js.map