"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.computePaymentChannelHash = exports.computeEscrowHash = exports.computeLedgerHash = exports.computeStateTreeHash = exports.computeTransactionTreeHash = exports.computeTrustlineHash = exports.computeOrderID = exports.computeSignerListLedgerObjectID = exports.computeAccountLedgerObjectID = exports.computeBinaryTransactionSigningHash = exports.computeTransactionHash = exports.computeBinaryTransactionHash = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const ripple_address_codec_1 = require("ripple-address-codec"); const sha512Half_1 = __importDefault(require("./sha512Half")); const hash_prefix_1 = __importDefault(require("./hash-prefix")); const shamap_1 = require("./shamap"); const ripple_binary_codec_1 = require("ripple-binary-codec"); const ledgerspaces_1 = __importDefault(require("./ledgerspaces")); const padLeftZero = (string, length) => { return Array(length - string.length + 1).join('0') + string; }; const intToHex = (integer, byteLength) => { return padLeftZero(Number(integer).toString(16), byteLength * 2); }; const bytesToHex = (bytes) => { return Buffer.from(bytes).toString('hex'); }; const bigintToHex = (integerString, byteLength) => { const hex = new bignumber_js_1.default(integerString).toString(16); return padLeftZero(hex, byteLength * 2); }; const ledgerSpaceHex = (name) => { return intToHex(ledgerspaces_1.default[name].charCodeAt(0), 2); }; const addressToHex = (address) => { return Buffer.from(ripple_address_codec_1.decodeAccountID(address)).toString('hex'); }; const currencyToHex = (currency) => { if (currency.length === 3) { const bytes = new Array(20 + 1).join('0').split('').map(parseFloat); bytes[12] = currency.charCodeAt(0) & 0xff; bytes[13] = currency.charCodeAt(1) & 0xff; bytes[14] = currency.charCodeAt(2) & 0xff; return bytesToHex(bytes); } return currency; }; const addLengthPrefix = (hex) => { const length = hex.length / 2; if (length <= 192) { return bytesToHex([length]) + hex; } else if (length <= 12480) { const x = length - 193; return bytesToHex([193 + (x >>> 8), x & 0xff]) + hex; } else if (length <= 918744) { const x = length - 12481; return bytesToHex([241 + (x >>> 16), (x >>> 8) & 0xff, x & 0xff]) + hex; } throw new Error('Variable integer overflow.'); }; exports.computeBinaryTransactionHash = (txBlobHex) => { const prefix = hash_prefix_1.default.TRANSACTION_ID.toString(16).toUpperCase(); return sha512Half_1.default(prefix + txBlobHex); }; exports.computeTransactionHash = (txJSON) => { return exports.computeBinaryTransactionHash(ripple_binary_codec_1.encode(txJSON)); }; exports.computeBinaryTransactionSigningHash = (txBlobHex) => { const prefix = hash_prefix_1.default.TRANSACTION_SIGN.toString(16).toUpperCase(); return sha512Half_1.default(prefix + txBlobHex); }; exports.computeAccountLedgerObjectID = (address) => { return sha512Half_1.default(ledgerSpaceHex('account') + addressToHex(address)); }; exports.computeSignerListLedgerObjectID = (address) => { return sha512Half_1.default(ledgerSpaceHex('signerList') + addressToHex(address) + '00000000'); }; exports.computeOrderID = (address, sequence) => { const prefix = '00' + intToHex(ledgerspaces_1.default.offer.charCodeAt(0), 1); return sha512Half_1.default(prefix + addressToHex(address) + intToHex(sequence, 4)); }; exports.computeTrustlineHash = (address1, address2, currency) => { const address1Hex = addressToHex(address1); const address2Hex = addressToHex(address2); const swap = new bignumber_js_1.default(address1Hex, 16).isGreaterThan(new bignumber_js_1.default(address2Hex, 16)); const lowAddressHex = swap ? address2Hex : address1Hex; const highAddressHex = swap ? address1Hex : address2Hex; const prefix = ledgerSpaceHex('rippleState'); return sha512Half_1.default(prefix + lowAddressHex + highAddressHex + currencyToHex(currency)); }; exports.computeTransactionTreeHash = (transactions) => { const shamap = new shamap_1.SHAMap(); transactions.forEach((txJSON) => { const txBlobHex = ripple_binary_codec_1.encode(txJSON); const metaHex = ripple_binary_codec_1.encode(txJSON.metaData); const txHash = exports.computeBinaryTransactionHash(txBlobHex); const data = addLengthPrefix(txBlobHex) + addLengthPrefix(metaHex); shamap.addItem(txHash, data, shamap_1.NodeType.TRANSACTION_METADATA); }); return shamap.hash; }; exports.computeStateTreeHash = (entries) => { const shamap = new shamap_1.SHAMap(); entries.forEach((ledgerEntry) => { const data = ripple_binary_codec_1.encode(ledgerEntry); shamap.addItem(ledgerEntry.index, data, shamap_1.NodeType.ACCOUNT_STATE); }); return shamap.hash; }; exports.computeLedgerHash = (ledgerHeader) => { const prefix = hash_prefix_1.default.LEDGER.toString(16).toUpperCase(); return sha512Half_1.default(prefix + intToHex(ledgerHeader.ledger_index, 4) + bigintToHex(ledgerHeader.total_coins, 8) + ledgerHeader.parent_hash + ledgerHeader.transaction_hash + ledgerHeader.account_hash + intToHex(ledgerHeader.parent_close_time, 4) + intToHex(ledgerHeader.close_time, 4) + intToHex(ledgerHeader.close_time_resolution, 1) + intToHex(ledgerHeader.close_flags, 1)); }; exports.computeEscrowHash = (address, sequence) => { return sha512Half_1.default(ledgerSpaceHex('escrow') + addressToHex(address) + intToHex(sequence, 4)); }; exports.computePaymentChannelHash = (address, dstAddress, sequence) => { return sha512Half_1.default(ledgerSpaceHex('paychan') + addressToHex(address) + addressToHex(dstAddress) + intToHex(sequence, 4)); }; //# sourceMappingURL=index.js.map