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.

index.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.hashTxTree = exports.hashStateTree = exports.hashLedger = exports.hashSignedTx = exports.hashLedgerHeader = exports.hashPaymentChannel = exports.hashEscrow = exports.hashTrustline = exports.hashOfferId = exports.hashSignerListId = exports.hashAccountRoot = exports.hashTx = void 0;
  30. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  31. const ripple_address_codec_1 = require("ripple-address-codec");
  32. const hashLedger_1 = __importStar(require("./hashLedger"));
  33. exports.hashLedger = hashLedger_1.default;
  34. Object.defineProperty(exports, "hashLedgerHeader", { enumerable: true, get: function () { return hashLedger_1.hashLedgerHeader; } });
  35. Object.defineProperty(exports, "hashSignedTx", { enumerable: true, get: function () { return hashLedger_1.hashSignedTx; } });
  36. Object.defineProperty(exports, "hashTxTree", { enumerable: true, get: function () { return hashLedger_1.hashTxTree; } });
  37. Object.defineProperty(exports, "hashStateTree", { enumerable: true, get: function () { return hashLedger_1.hashStateTree; } });
  38. const HashPrefix_1 = __importDefault(require("./HashPrefix"));
  39. const ledgerSpaces_1 = __importDefault(require("./ledgerSpaces"));
  40. const sha512Half_1 = __importDefault(require("./sha512Half"));
  41. const HEX = 16;
  42. const BYTE_LENGTH = 4;
  43. function addressToHex(address) {
  44. return Buffer.from((0, ripple_address_codec_1.decodeAccountID)(address)).toString('hex');
  45. }
  46. function ledgerSpaceHex(name) {
  47. return ledgerSpaces_1.default[name].charCodeAt(0).toString(HEX).padStart(4, '0');
  48. }
  49. const MASK = 0xff;
  50. function currencyToHex(currency) {
  51. if (currency.length !== 3) {
  52. return currency;
  53. }
  54. const bytes = Array(20).fill(0);
  55. bytes[12] = currency.charCodeAt(0) & MASK;
  56. bytes[13] = currency.charCodeAt(1) & MASK;
  57. bytes[14] = currency.charCodeAt(2) & MASK;
  58. return Buffer.from(bytes).toString('hex');
  59. }
  60. function hashTx(txBlobHex) {
  61. const prefix = HashPrefix_1.default.TRANSACTION_SIGN.toString(HEX).toUpperCase();
  62. return (0, sha512Half_1.default)(prefix + txBlobHex);
  63. }
  64. exports.hashTx = hashTx;
  65. function hashAccountRoot(address) {
  66. return (0, sha512Half_1.default)(ledgerSpaceHex('account') + addressToHex(address));
  67. }
  68. exports.hashAccountRoot = hashAccountRoot;
  69. function hashSignerListId(address) {
  70. return (0, sha512Half_1.default)(`${ledgerSpaceHex('signerList') + addressToHex(address)}00000000`);
  71. }
  72. exports.hashSignerListId = hashSignerListId;
  73. function hashOfferId(address, sequence) {
  74. const hexPrefix = ledgerSpaces_1.default.offer
  75. .charCodeAt(0)
  76. .toString(HEX)
  77. .padStart(2, '0');
  78. const hexSequence = sequence.toString(HEX).padStart(8, '0');
  79. const prefix = `00${hexPrefix}`;
  80. return (0, sha512Half_1.default)(prefix + addressToHex(address) + hexSequence);
  81. }
  82. exports.hashOfferId = hashOfferId;
  83. function hashTrustline(address1, address2, currency) {
  84. const address1Hex = addressToHex(address1);
  85. const address2Hex = addressToHex(address2);
  86. const swap = new bignumber_js_1.default(address1Hex, 16).isGreaterThan(new bignumber_js_1.default(address2Hex, 16));
  87. const lowAddressHex = swap ? address2Hex : address1Hex;
  88. const highAddressHex = swap ? address1Hex : address2Hex;
  89. const prefix = ledgerSpaceHex('rippleState');
  90. return (0, sha512Half_1.default)(prefix + lowAddressHex + highAddressHex + currencyToHex(currency));
  91. }
  92. exports.hashTrustline = hashTrustline;
  93. function hashEscrow(address, sequence) {
  94. return (0, sha512Half_1.default)(ledgerSpaceHex('escrow') +
  95. addressToHex(address) +
  96. sequence.toString(HEX).padStart(BYTE_LENGTH * 2, '0'));
  97. }
  98. exports.hashEscrow = hashEscrow;
  99. function hashPaymentChannel(address, dstAddress, sequence) {
  100. return (0, sha512Half_1.default)(ledgerSpaceHex('paychan') +
  101. addressToHex(address) +
  102. addressToHex(dstAddress) +
  103. sequence.toString(HEX).padStart(BYTE_LENGTH * 2, '0'));
  104. }
  105. exports.hashPaymentChannel = hashPaymentChannel;
  106. //# sourceMappingURL=index.js.map