Created
August 27, 2021 00:47
-
-
Save reemuru/ee56d7bdb2a04a42a412ddb143f78a36 to your computer and use it in GitHub Desktop.
Node.js - create preimage and preimage hash for LND hold invoice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import crypto module | |
const crypto = require("crypto") | |
// use cryptographically secure random bytes generation | |
const preimage = crypto.randomBytes(32) | |
// get the raw bytes from the buffer | |
const preimageRaw = JSON.parse(JSON.stringify(preimage)).data | |
// log the preimage raw bytes | |
console.log(`preimage bytes: ${preimageRaw}`) | |
// create SHA-256 hash | |
const hash = crypto.createHash('sha256') | |
hash.update(preimage); | |
// log preimage hash raw bytes | |
console.log(`hash bytes: ${new Uint32Array(hash.copy().digest())}`) | |
// log hex-encoded preimage | |
console.log(`hex: ${preimage.toString('hex')}`) | |
// log hex-encoded preimage hash | |
console.log(`hash: ${hash.copy().digest('hex')}`); |
SergheyDimitroglo
commented
Jul 31, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment