MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.86k stars 844 forks source link

Get Uncompressed Private key out of Compressed #1066

Closed fr4gn4tic closed 2 years ago

fr4gn4tic commented 2 years ago

Hi i am trying to generate a Private Key and get all Address Types out of it. But i need to compress/decompress the Private key to get the compressed Adresses and the uncompressed.

Thanks for your Help.

static void Main(string[] args)
        {

            Random rand = new Random(Guid.NewGuid().GetHashCode());
            var privateKey = new Key(fCompressedIn: true);

            var bitcoinPrivateKey = privateKey.GetWif(Network.Main);
            var bitcoinPublicKey = bitcoinPrivateKey.PubKey;

            Console.WriteLine("Legacy(compressed): "+bitcoinPublicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main));
            Console.WriteLine("Segwit: " + bitcoinPublicKey.GetAddress(ScriptPubKeyType.Segwit, Network.Main));
            Console.WriteLine("SegwitP2SH: " + bitcoinPublicKey.GetAddress(ScriptPubKeyType.SegwitP2SH, Network.Main));
            Console.WriteLine("TaprootBIP86: " + bitcoinPublicKey.GetAddress(ScriptPubKeyType.TaprootBIP86, Network.Main));

            Console.ReadLine();

        }

My Output:

  1. Legacy(compressed): 1Q8Nd6AVEJs6kBeaoaNEtpNFCrRSZecfiK
  2. Segwit: bc1qlkha4ffcx7sgn78m86m0qhgkdk3hshh0kyt89q
  3. SegwitP2SH: 36SbqkBoU5EpwJRTDtfR3puhx5dpNveM8A
  4. TaprootBIP86: bc1pclm6jaqcx6c32vlqceaunhh50ckxlnp5xectwk08ra5yu332usasqmnz8e
NicolasDorier commented 2 years ago

using fCompressedIn: true instead fCompressedIn: false would solve your issue probably.

I don't know what you want to do with uncompressed pubkeys, but this is obsolete by now.