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 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | exports.decodeLedgerData = exports.ledgerHash = exports.transactionTreeHash = exports.accountStateHash = void 0;
|
27 | const assert = __importStar(require("assert"));
|
28 | const shamap_1 = require("./shamap");
|
29 | const hash_prefixes_1 = require("./hash-prefixes");
|
30 | const hashes_1 = require("./hashes");
|
31 | const binary_1 = require("./binary");
|
32 | const hash_256_1 = require("./types/hash-256");
|
33 | const st_object_1 = require("./types/st-object");
|
34 | const uint_64_1 = require("./types/uint-64");
|
35 | const uint_32_1 = require("./types/uint-32");
|
36 | const uint_8_1 = require("./types/uint-8");
|
37 | const binary_parser_1 = require("./serdes/binary-parser");
|
38 | const bigInt = require("big-integer");
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function 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 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function 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 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | function 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 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | function transactionTreeHash(param) {
|
98 | const itemizer = transactionItemizer;
|
99 | return computeHash(itemizer, param);
|
100 | }
|
101 | exports.transactionTreeHash = transactionTreeHash;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function accountStateHash(param) {
|
109 | const itemizer = entryItemizer;
|
110 | return computeHash(itemizer, param);
|
111 | }
|
112 | exports.accountStateHash = accountStateHash;
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function 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 | }
|
135 | exports.ledgerHash = ledgerHash;
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function 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 | }
|
159 | exports.decodeLedgerData = decodeLedgerData;
|
160 |
|
\ | No newline at end of file |