Skip to content

Instantly share code, notes, and snippets.

@reemuru
Created August 27, 2021 00:47
Show Gist options
  • Save reemuru/ee56d7bdb2a04a42a412ddb143f78a36 to your computer and use it in GitHub Desktop.
Save reemuru/ee56d7bdb2a04a42a412ddb143f78a36 to your computer and use it in GitHub Desktop.
Node.js - create preimage and preimage hash for LND hold invoice
// 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
Copy link

angb0ME_460s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment