UNPKG

6.14 kBJavaScriptView Raw
1"use strict";
2var __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}));
13var __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});
18var __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};
25Object.defineProperty(exports, "__esModule", { value: true });
26exports.decodeLedgerData = exports.ledgerHash = exports.transactionTreeHash = exports.accountStateHash = void 0;
27const assert = __importStar(require("assert"));
28const shamap_1 = require("./shamap");
29const hash_prefixes_1 = require("./hash-prefixes");
30const hashes_1 = require("./hashes");
31const binary_1 = require("./binary");
32const hash_256_1 = require("./types/hash-256");
33const st_object_1 = require("./types/st-object");
34const uint_64_1 = require("./types/uint-64");
35const uint_32_1 = require("./types/uint-32");
36const uint_8_1 = require("./types/uint-8");
37const binary_parser_1 = require("./serdes/binary-parser");
38const 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 */
46function 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 */
57function 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 */
78function 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 */
97function transactionTreeHash(param) {
98 const itemizer = transactionItemizer;
99 return computeHash(itemizer, param);
100}
101exports.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 */
108function accountStateHash(param) {
109 const itemizer = entryItemizer;
110 return computeHash(itemizer, param);
111}
112exports.accountStateHash = accountStateHash;
113/**
114 * Serialize and hash a ledger header
115 *
116 * @param header a ledger header
117 * @returns the hash of header
118 */
119function 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}
135exports.ledgerHash = ledgerHash;
136/**
137 * Decodes a serialized ledger header
138 *
139 * @param binary A serialized ledger header
140 * @param definitions Type definitions to parse the ledger objects.
141 * Used if there are non-default ledger objects to decode.
142 * @returns A JSON object describing a ledger header
143 */
144function decodeLedgerData(binary, definitions) {
145 assert.ok(typeof binary === 'string', 'binary must be a hex string');
146 const parser = new binary_parser_1.BinaryParser(binary, definitions);
147 return {
148 ledger_index: parser.readUInt32(),
149 total_coins: parser.readType(uint_64_1.UInt64).valueOf().toString(),
150 parent_hash: parser.readType(hash_256_1.Hash256).toHex(),
151 transaction_hash: parser.readType(hash_256_1.Hash256).toHex(),
152 account_hash: parser.readType(hash_256_1.Hash256).toHex(),
153 parent_close_time: parser.readUInt32(),
154 close_time: parser.readUInt32(),
155 close_time_resolution: parser.readUInt8(),
156 close_flags: parser.readUInt8(),
157 };
158}
159exports.decodeLedgerData = decodeLedgerData;
160//# sourceMappingURL=ledger-hashes.js.map
\No newline at end of file