Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ledger-hashes.js 5.8KB

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