I've been looking through the tests and I can't find out how to do it.
Here's what I have:
var bjs = require('bitcoinjs-lib'); //dev version
var keyPair = bjs.ECPair.makeRandom();
var address = keyPair.getAddress().toString();
console.log(address);
I get the address, but I want to get a private key in hex somehow from "keyPair".
Thanks.
You can't (shouldn't) get the hex of just the Bitcoin private key, because unlike a typical ECDSA private key, we need the compression information typically used for that key.
Therefore, a Bitcoin private key is typically represented in a custom format (known as WIF) to ensure the compression information is not lost.
The WIF format is by definition base58 check, thereby, you could use the bs58check module to decode it. But the decoded format won't necessarily be _only_ the private key data, it may have extra information (a compression flag etc).
Perhaps we could add an encoding parameter for toWIF that allowed you to avoid the bs58check encoding entirely?
@jprichardson thoughts?
Thanks, WIF works for what I needed.
Perhaps we could add an encoding parameter for toWIF that allowed you to avoid the bs58check encoding entirely?
Why would we want to do that? What would be the use case?
@jprichardson well, it is something that other libraries offer. I personally have never seen a use case for it.
If @smithb1994 actually needed it, I might have considered it, but it seems the alternative of not providing it has steered him to a better solution anyway.
@dcousens since WIF is primarily used for P2PKH addresses. what would be the alternative for P2SH(P2WPKH) or native P2WPKH ?
@youssefgh when you decode the WIF, you get an ECPair. If you do key.getPublicKeyBuffer(), you can make the public key hash bitcoin.crypto.hash160(pubKeyBuffer) and use script templates to obtain the output script you want. bitcoin.script.witnessPubKeyHash.output.encode(hash), and use it as our scriptPubKey. If you want P2SH, check the examples for creating a P2SH scriptPubKey from a redeemscript (your p2wpkh script)
Then, pass the output script to bitcoin.address.fromOutputScript(scriptPubKey)
@afk11 i already can create P2SH(P2WPKH) and native P2WPKH and retrieve there private key using : Wif.decode(ecPair.toWIF()).privateKey
But since WIF is used as an export format for P2PKH addresses (am not sure though) it will be weird to go through WIF just to get the private key for P2SH(P2WPKH) and P2WPKH
Please correct me if am wrong, and i appreciate taking the time to write me the detailed explanation above
But since WIF is used as an export format for P2PKH addresses
That is not true.
Wallet Import Format (WIF, also known as Wallet Export Format) is a way of encoding a private ECDSA key so as to make it easier to copy.
Thanks a lot @dcousens for the correction !
Most helpful comment
You can't (shouldn't) get the
hexof just the Bitcoin private key, because unlike a typical ECDSA private key, we need the compression information typically used for that key.Therefore, a Bitcoin private key is typically represented in a custom format (known as WIF) to ensure the compression information is not lost.
The WIF format is by definition base58 check, thereby, you could use the
bs58checkmodule to decode it. But the decoded format won't necessarily be _only_ the private key data, it may have extra information (a compression flag etc).