UNPKG

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