Blog

Filter posts by Category Or Tag of the Blog section!

Creating wallet in bitcoin as offline, singing and broadcasting

Friday, 22 April 2022

In my previous post, I considered creating a wallet in Tron network and of course singing and broadcasting it. Now it’s possible to do the same in the Bitcoin network by using NBitcoin. Look at the following piece of code:
 

using NBitcoin;

using System;



namespace BitcoinWallet

{

    class Program

    {

        static void Main(string[] args)

        {

            // Create a new random private key for the wallet

            Key privateKey = new Key();



            // Get the public key and Bitcoin address from the private key

            PubKey publicKey = privateKey.PubKey;

            BitcoinAddress address = publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main);



            Console.WriteLine($"Wallet Address: {address}");



            // Get the balance of the wallet from the blockchain explorer

            QBitNinja.Client.QBitNinjaClient client = new QBitNinja.Client.QBitNinjaClient(Network.Main);

            Balance balance = client.GetBalance(address).Result;

            Money totalBalance = balance.Total;



            Console.WriteLine($"Wallet Balance: {totalBalance}");



            // Create a new transaction with an output of 0.01 BTC to a test address

            BitcoinAddress testAddress = BitcoinAddress.Create("muJ3MqAKMUpE9KeEXbZm4KMY4n4F4fJyqx", Network.Main);

            Money transactionAmount = new Money(0.01m, MoneyUnit.BTC);

            var transaction = new Transaction();

            transaction.Outputs.Add(transactionAmount, testAddress);



            // Get the unspent outputs for the wallet

            var unspentCoins = balance.Coins;

            var coinsToSpend = unspentCoins[0];



            // Create the input for the transaction using the unspent output

            var transactionInput = new TxIn(coinsToSpend.Outpoint);



            // Sign the transaction input with the private key

            var hash = transaction.GetHash();

            var signature = privateKey.Sign(hash);



            // Create the script for the transaction input using the signature and public key

            var scriptSig = new Script(

                signature.ToDER(),

                publicKey.ToBytes()

            );



            // Set the script for the transaction input and add it to the transaction

            transactionInput.ScriptSig = scriptSig;

            transaction.Inputs.Add(transactionInput);



            // Broadcast the transaction to the Bitcoin network

            BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result;



            Console.WriteLine($"Transaction broadcasted: {broadcastResponse.Success}");

        }

    }

}


 

This code creates a new random private key for the wallet, gets the public key and Bitcoin address from the private key, and displays the wallet address and balance by querying the blockchain explorer. It then creates a new transaction with an output of 0.01 BTC to a test address, gets the unspent outputs for the wallet, and signs the transaction input with the private key. Finally, it broadcasts the transaction to the Bitcoin network and displays the result.

As I noted in my previous post as well, this code is for educational purposes only and should not be used for real transactions without proper understanding and precautions

comments powered by Disqus