You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ledgerhash.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. const _ = __importStar(require("lodash"));
  23. const hashes_1 = require("../common/hashes");
  24. const common = __importStar(require("../common"));
  25. function convertLedgerHeader(header) {
  26. return {
  27. account_hash: header.stateHash,
  28. close_time: common.iso8601ToRippleTime(header.closeTime),
  29. close_time_resolution: header.closeTimeResolution,
  30. close_flags: header.closeFlags,
  31. hash: header.ledgerHash,
  32. ledger_hash: header.ledgerHash,
  33. ledger_index: header.ledgerVersion.toString(),
  34. parent_hash: header.parentLedgerHash,
  35. parent_close_time: common.iso8601ToRippleTime(header.parentCloseTime),
  36. total_coins: header.totalDrops,
  37. transaction_hash: header.transactionHash
  38. };
  39. }
  40. function hashLedgerHeader(ledgerHeader) {
  41. const header = convertLedgerHeader(ledgerHeader);
  42. return hashes_1.computeLedgerHash(header);
  43. }
  44. function computeTransactionHash(ledger, options) {
  45. let transactions;
  46. if (ledger.rawTransactions) {
  47. transactions = JSON.parse(ledger.rawTransactions);
  48. }
  49. else if (ledger.transactions) {
  50. try {
  51. transactions = ledger.transactions.map((tx) => JSON.parse(tx.rawTransaction));
  52. }
  53. catch (e) {
  54. if (e.toString() ===
  55. 'SyntaxError: Unexpected' + ' token u in JSON at position 0') {
  56. throw new common.errors.ValidationError('ledger' + ' is missing raw transactions');
  57. }
  58. }
  59. }
  60. else {
  61. if (options.computeTreeHashes) {
  62. throw new common.errors.ValidationError('transactions' + ' property is missing from the ledger');
  63. }
  64. return ledger.transactionHash;
  65. }
  66. const txs = transactions.map((tx) => {
  67. const mergeTx = Object.assign({}, _.omit(tx, 'tx'), tx.tx || {});
  68. const renameMeta = Object.assign({}, _.omit(mergeTx, 'meta'), tx.meta ? { metaData: tx.meta } : {});
  69. return renameMeta;
  70. });
  71. const transactionHash = hashes_1.computeTransactionTreeHash(txs);
  72. if (ledger.transactionHash != null &&
  73. ledger.transactionHash !== transactionHash) {
  74. throw new common.errors.ValidationError('transactionHash in header' +
  75. ' does not match computed hash of transactions', {
  76. transactionHashInHeader: ledger.transactionHash,
  77. computedHashOfTransactions: transactionHash
  78. });
  79. }
  80. return transactionHash;
  81. }
  82. function computeStateHash(ledger, options) {
  83. if (ledger.rawState == null) {
  84. if (options.computeTreeHashes) {
  85. throw new common.errors.ValidationError('rawState' + ' property is missing from the ledger');
  86. }
  87. return ledger.stateHash;
  88. }
  89. const state = JSON.parse(ledger.rawState);
  90. const stateHash = hashes_1.computeStateTreeHash(state);
  91. if (ledger.stateHash != null && ledger.stateHash !== stateHash) {
  92. throw new common.errors.ValidationError('stateHash in header' + ' does not match computed hash of state');
  93. }
  94. return stateHash;
  95. }
  96. function computeLedgerHeaderHash(ledger, options = {}) {
  97. const subhashes = {
  98. transactionHash: computeTransactionHash(ledger, options),
  99. stateHash: computeStateHash(ledger, options)
  100. };
  101. return hashLedgerHeader(Object.assign({}, ledger, subhashes));
  102. }
  103. exports.default = computeLedgerHeaderHash;
  104. //# sourceMappingURL=ledgerhash.js.map