UNPKG

2.23 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.transactionID = exports.sha512Half = exports.Sha512Half = void 0;
4const hash_prefixes_1 = require("./hash-prefixes");
5const createHash = require("create-hash");
6const hash_256_1 = require("./types/hash-256");
7const binary_serializer_1 = require("./serdes/binary-serializer");
8const buffer_1 = require("buffer/");
9/**
10 * Class for hashing with SHA512
11 * @extends BytesList So SerializedTypes can write bytes to a Sha512Half
12 */
13class Sha512Half extends binary_serializer_1.BytesList {
14 constructor() {
15 super(...arguments);
16 this.hash = createHash('sha512');
17 }
18 /**
19 * Construct a new Sha512Hash and write bytes this.hash
20 *
21 * @param bytes bytes to write to this.hash
22 * @returns the new Sha512Hash object
23 */
24 static put(bytes) {
25 return new Sha512Half().put(bytes);
26 }
27 /**
28 * Write bytes to an existing Sha512Hash
29 *
30 * @param bytes bytes to write to object
31 * @returns the Sha512 object
32 */
33 put(bytes) {
34 this.hash.update(bytes);
35 return this;
36 }
37 /**
38 * Compute SHA512 hash and slice in half
39 *
40 * @returns half of a SHA512 hash
41 */
42 finish256() {
43 return buffer_1.Buffer.from(this.hash.digest().slice(0, 32));
44 }
45 /**
46 * Constructs a Hash256 from the Sha512Half object
47 *
48 * @returns a Hash256 object
49 */
50 finish() {
51 return new hash_256_1.Hash256(this.finish256());
52 }
53}
54exports.Sha512Half = Sha512Half;
55/**
56 * compute SHA512 hash of a list of bytes
57 *
58 * @param args zero or more arguments to hash
59 * @returns the sha512half hash of the arguments.
60 */
61function sha512Half(...args) {
62 const hash = new Sha512Half();
63 args.forEach((a) => hash.put(a));
64 return hash.finish256();
65}
66exports.sha512Half = sha512Half;
67/**
68 * Construct a transactionID from a Serialized Transaction
69 *
70 * @param serialized bytes to hash
71 * @returns a Hash256 object
72 */
73function transactionID(serialized) {
74 return new hash_256_1.Hash256(sha512Half(hash_prefixes_1.HashPrefix.transactionID, serialized));
75}
76exports.transactionID = transactionID;
77//# sourceMappingURL=hashes.js.map
\No newline at end of file