UNPKG

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