1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.decodeLedgerData = exports.ledgerHash = exports.transactionTreeHash = exports.accountStateHash = void 0;
|
4 | const shamap_1 = require("./shamap");
|
5 | const hash_prefixes_1 = require("./hash-prefixes");
|
6 | const hashes_1 = require("./hashes");
|
7 | const binary_1 = require("./binary");
|
8 | const hash_256_1 = require("./types/hash-256");
|
9 | const st_object_1 = require("./types/st-object");
|
10 | const uint_64_1 = require("./types/uint-64");
|
11 | const uint_32_1 = require("./types/uint-32");
|
12 | const uint_8_1 = require("./types/uint-8");
|
13 | const binary_parser_1 = require("./serdes/binary-parser");
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function 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 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function 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 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function 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 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function transactionTreeHash(param) {
|
75 | const itemizer = transactionItemizer;
|
76 | return computeHash(itemizer, param);
|
77 | }
|
78 | exports.transactionTreeHash = transactionTreeHash;
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function accountStateHash(param) {
|
86 | const itemizer = entryItemizer;
|
87 | return computeHash(itemizer, param);
|
88 | }
|
89 | exports.accountStateHash = accountStateHash;
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | function 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 | }
|
114 | exports.ledgerHash = ledgerHash;
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | function 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 | }
|
140 | exports.decodeLedgerData = decodeLedgerData;
|
141 |
|
\ | No newline at end of file |