Created
July 11, 2019 06:20
-
-
Save lautarodragan/d6d76cc913334490b66b2848db134fd6 to your computer and use it in GitHub Desktop.
Bitcoin Private Key to WIF
This file contains 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
// 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