Skip to content

Instantly share code, notes, and snippets.

@lautarodragan
Created July 11, 2019 06:20
Show Gist options
  • Save lautarodragan/d6d76cc913334490b66b2848db134fd6 to your computer and use it in GitHub Desktop.
Save lautarodragan/d6d76cc913334490b66b2848db134fd6 to your computer and use it in GitHub Desktop.
Bitcoin Private Key to WIF
// https://en.bitcoin.it/wiki/Wallet_import_format
const crypto = require('crypto');
const base58 = require('base-58')
const sha256 = buffer => crypto.createHash('sha256').update(buffer).digest()
const privateKeyToWIF = (privateKey, prefix = 'ef', compressed = '01') => {
const prefixedPrivateKey = prefix + privateKey + compressed
const prefixedPrivateKeyBuffer = Buffer.from(prefixedPrivateKey, 'hex')
const check = sha256(sha256(prefixedPrivateKeyBuffer)).slice(0, 4)
const wifBuf = Buffer.concat([prefixedPrivateKeyBuffer, check])
return base58.encode(wifBuf)
}
console.log(privateKeyToWIF('hex private key'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment