UNPKG

merkle-tree-lib

Version:

Merkle Tree implementation with BIP340 tagged hash support.

60 lines (59 loc) 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TaggedSha256Strategy = void 0; const crypto_1 = require("crypto"); /** * TaggedSha256Strategy - Implementation of HashStrategy using BIP-0340 tagged SHA-256 * This implements tagged hashing as defined in BIP-0340: SHA256(SHA256(tag) || SHA256(tag) || msg) */ class TaggedSha256Strategy { /** * Initialize with a specific tag * * @param tag - Tag string to use for domain separation */ constructor(tag) { this.tag = tag; this.tagHash = (0, crypto_1.createHash)('sha256').update(tag, 'utf8').digest(); } /** * Calculate a tagged SHA-256 hash for the input data * * @param data - The data to hash * @returns The computed tagged SHA-256 hash as a Buffer */ hash(data) { const dataBuffer = typeof data === 'string' ? Buffer.from(data, 'utf8') : data; // Apply the tagged hash: SHA256(tagHash || tagHash || msg) return (0, crypto_1.createHash)('sha256') .update(this.tagHash) .update(this.tagHash) .update(dataBuffer) .digest(); } /** * Get the algorithm name including the tag * * @returns A descriptive string with the algorithm and tag */ getAlgorithmName() { return `Tagged-SHA-256(${this.tag})`; } /** * Get the tag used for this strategy * * @returns The tag string */ getTag() { return this.tag; } /** * Get the pre-computed tag hash * * @returns The tag hash as a Buffer */ getTagHash() { return this.tagHash; } } exports.TaggedSha256Strategy = TaggedSha256Strategy;