Bitcoin with C-Sharp

bolum

Bolum

Posted on June 26, 2020

Bitcoin with C-Sharp

In this tutorial, we are going to introduce Bitcoin using C. We will be using C#’s bitcoin library, called NBitcoin.

Install Nbitcoin

dotnet add package NBitcoin

Generate a private key

A private key in the context of Bitcoin is a secret number that allows bitcoins to be spent. Every Bitcoin wallet contains one or more private keys.

This is how you generate a private key with NBitcoin:

var privateKey = new Key();

var bitcoinPrivateKey = privateKey.GetWif(Network.Main).ToString();

Console.WriteLine(bitcoinPrivateKey);

//L3X8YdfNAgPGhcQs1tbiAiyqG3Z5BJ4nYzfJPmxA7BJRGMi3AMPp

Generate a public key

var privateKey = new Key();

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

var bitcoinPublicKey = bitcoinPrivateKey.PubKey.ToString();

Console.WriteLine(bitcoinPublicKey);

//027f34ed880f6ee198b5b583472d7dcd640326a6f3d8530c6105cb3bf20b1b629e

Create a Bitcoin address

You know that your Bitcoin Address is what you share to the world to get paid.

var privateKey = new Key();

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

var bitcoinPublicKey = bitcoinPrivateKey.PubKey;

var address = bitcoinPublicKey.GetAddress(ScriptPubKeyType.Segwit, Network.Main);

Console.WriteLine(address);


//bc1q7sq2samnm4nkpsylusuqw95t3ma8z5ayn7s5zk

Generate Address from a known private key

var key = "L1TmYw2edTAyF9CtSF64k2Qc3AkKa8t9xn1vMa92xCe9VCzTbYzV";

var bitcoinSecret = new BitcoinSecret(key);

var address = bitcoinSecret.PubKey.GetAddress(ScriptPubKeyType.Segwit,Network.Main);

Console.WriteLine(address);

//bc1qlq037jz8u9mlkwh3ccmky92yxflrqrmaz5dtyr

Generate Mnemonic

A mnemonic recovery phrase or bitcoin seed is a list of words which store all the information needed to recover a bitcoin wallet

var mnemo = new Mnemonic(Wordlist.English, WordCount.Twelve);

//key victory fashion rough torch ice lesson main grape actual horn dinosaur

Generate Address from Mnemonic

var mnemo = new Mnemonic("dry brown drive parade drastic shine embrace hard report loan fold iron", Wordlist.English);

var hdroot = mnemo.DeriveExtKey();

var pKey = hdroot.Derive(new KeyPath("m/84'/0'/0'/0/0"));

var address = pKey.PrivateKey.PubKey.GetAddress(ScriptPubKeyType.Segwit, Network.Main));

Console.WriteLine(address);

//bc1qv7z69wxrew84vv43xrnfrckkzj4wzalf99e2cs

Generate Multiple Addresses from Mnemonic

Derivation path follows this convention:

m / purpose' / coin_type' / account' / change / address_index

So you can increase address_index from 0 to get new addresses from same mnemonic.

For more details: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki

var mnemo = new Mnemonic("orange soup note oil token top slab elder mercy mad wool congress", Wordlist.English); 

var hdroot = mnemo.DeriveExtKey(); 

var pKey = hdroot.Derive(new KeyPath("m/84'/0'/0'/0/0")); 

var pKey2 = hdroot.Derive(new KeyPath("m/84'/0'/0'/0/1")); 

Console.WriteLine(pKey.PrivateKey.PubKey.GetAddress(ScriptPubKeyType.Segwit, Network.Main)); 

Console.WriteLine(pKey2.PrivateKey.PubKey.GetAddress(ScriptPubKeyType.Segwit, Network.Main));

//bc1q3sfs9z8zl5c4jw5hxujv9p5gf2hseha7vkfeav

//bc1qk3myyedlqyvjg69cfmatypaep9hqa6rzcx9dcu

Conclusion

In this tutorial, we introduced bitcoin with c#. We saw how to generate a private key, public key, bitcoin address, mnemonic and also generate address from a mnemonic recovery phrase.

**Source : **https://programmingblockchain.gitbook.io/programmingblockchain/

coffee: bc1qr0mfxvc94j5732r450z3uw9jfujmm9syn2uy3q

Connect:

*Linkedin*

💖 💪 🙅 🚩
bolum
Bolum

Posted on June 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related