Last active
February 3, 2021 10:39
-
-
Save hrobeers/214b2a7199875c477f13f48e5f675c4a to your computer and use it in GitHub Desktop.
Generate compressed and uncompressed bitcoin addresses from a private key with bitcore.
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
var bitcore = require("bitcore-lib"); | |
var Buffer = require("buffer").Buffer; | |
var value = new Buffer.from("test", "utf8"); | |
var hash = bitcore.crypto.Hash.sha256(value); | |
// Compressed Key (nu skool) | |
var bn = bitcore.crypto.BN.fromBuffer(hash); | |
var privateKey = new bitcore.PrivateKey(bn); | |
var wif = privateKey.toWIF(); | |
// Uncompressed Key (old skool) | |
var privateKey2 = bitcore.PrivateKey.fromBuffer(hash); | |
var wif2 = privateKey2.toWIF(); | |
var publicKey1 = new bitcore.PublicKey(privateKey); | |
var publicKey2 = new bitcore.PublicKey(privateKey2); | |
var addr1 = publicKey1.toAddress(); //compressed - 19eA3hUfKRt7aZymavdQFXg5EZ6KCVKxr8 | |
var addr2 = publicKey2.toAddress(); //uncompressed - 1HKqKTMpBTZZ8H5zcqYEWYBaaWELrDEXeE | |
console.log('Compressed'); | |
console.log('WIF: ' + wif); | |
console.log('Address: ' + addr1); | |
console.log(''); | |
console.log('Uncompressed'); | |
console.log('WIF: ' + wif2); | |
console.log('Address: ' + addr2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great example, thank you