Generate address of 'private key' from user input

0

I want to take a private key as input from the user and convert it to a wallet address using C#.

I have seen this in a tutorial to generate a random key:

Key privateKey = new Key();
PubKey publicKey = privateKey.PubKey;
Console.WriteLine(publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main));

How do I convert a private key string from the user to a wallet address for main net?

svvc

Posted 2020-11-21T10:47:03.577

Reputation: 11

Answers

0

Assuming the private key string in this example is pk

Console.WriteLine("Enter private key:");
string pk = Console.ReadLine();
var bitcoinPrivateKey = new BitcoinSecret(pk, Network.Main);

var legacy_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Legacy);

Console.WriteLine("Legacy Address :" + legacy_address);

And if you want to get p2sh-segwit and bech32 address, add below lines:

var p2shsegwit_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.SegwitP2SH);
var nativesegwit_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Segwit);

Console.WriteLine("P2SH-Segwit Address :" + p2shsegwit_address);
Console.WriteLine("Bech32 Address :" + nativesegwit_address);

Prayank

Posted 2020-11-21T10:47:03.577

Reputation: 2 423