-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledger-hashes.js
More file actions
72 lines (65 loc) · 2.15 KB
/
ledger-hashes.js
File metadata and controls
72 lines (65 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const _ = require('lodash');
const BN = require('bn.js');
const assert = require('assert');
const types = require('./types');
const {STObject, Hash256} = types;
const {ShaMap} = require('./shamap');
const {HashPrefix} = require('./hash-prefixes');
const {Sha512Half} = require('./hashes');
const {BinarySerializer, serializeObject} = require('./binary');
function computeHash(itemizer, itemsJson) {
const map = new ShaMap();
itemsJson.forEach(item => map.addItem(...itemizer(item)));
return map.hash();
}
function transactionItem(json) {
assert(json.hash);
const index = Hash256.from(json.hash);
const item = {
hashPrefix() {
return HashPrefix.transaction;
},
toBytesSink(sink) {
const serializer = new BinarySerializer(sink);
serializer.writeLengthEncoded(STObject.from(json));
serializer.writeLengthEncoded(STObject.from(json.metaData));
}
};
return [index, item];
}
function entryItem(json) {
const index = Hash256.from(json.index);
const bytes = serializeObject(json);
const item = {
hashPrefix() {
return HashPrefix.accountStateEntry;
},
toBytesSink(sink) {
sink.put(bytes);
}
};
return [index, item];
}
const transactionTreeHash = _.partial(computeHash, transactionItem);
const accountStateHash = _.partial(computeHash, entryItem);
function ledgerHash(header) {
const hash = new Sha512Half();
hash.put(HashPrefix.ledgerHeader);
assert(header.parent_close_time !== undefined);
assert(header.close_flags !== undefined);
types.UInt32.from(header.ledger_index).toBytesSink(hash);
types.UInt64.from(new BN(header.total_coins)).toBytesSink(hash);
types.Hash256.from(header.parent_hash).toBytesSink(hash);
types.Hash256.from(header.transaction_hash).toBytesSink(hash);
types.Hash256.from(header.account_hash).toBytesSink(hash);
types.UInt32.from(header.parent_close_time).toBytesSink(hash);
types.UInt32.from(header.close_time).toBytesSink(hash);
types.UInt8.from(header.close_time_resolution).toBytesSink(hash);
types.UInt8.from(header.close_flags).toBytesSink(hash);
return hash.finish();
}
module.exports = {
accountStateHash,
transactionTreeHash,
ledgerHash
};