Blog

Filter posts by Category Or Tag of the Blog section!

Creating wallet in Ethereum network, singing and broadcasting

Tuesday, 10 May 2022

I created a wallet in offline mode for Bitcoin and Tron networks in my previous posts. To create an Ethereum wallet offline, sign it, and then broadcast it using C#, you can use the following steps:

 

  1. Install the Nethereum library, which is a .NET library for working with Ethereum. You can install it via NuGet package manager or via the Nethereum GitHub repository.
  2. Generate a new Ethereum wallet offline using a tool such as MyEtherWallet, which allows you to create an Ethereum wallet and store your private key securely offline.
  3. Import the generated wallet into your C# application using the Nethereum library, which provides functions to load the wallet file and decrypt the private key.
  4. Create and sign a transaction using the Nethereum library. You can use the TransactionSigned event to sign the transaction using the private key of the wallet.
  5. Broadcast the signed transaction to the Ethereum network using the Web3 object of the Nethereum library. You can use the Eth.Transactions.SendRawTransaction method to send the transaction to the Ethereum network.



Now as a sample code, for creating the wallet we have this:
 

using Nethereum.Web3.Accounts;

using Nethereum.Web3;

using Nethereum.Hex.HexConvertors.Extensions;

using Nethereum.Signer;

using Nethereum.Hex.HexTypes;

using Nethereum.RPC.Eth.DTOs;

using Nethereum.Web3.TransactionReceipts;



 public void CreateWallet()

        {

            // Generate a new private key

            var ecKey = Nethereum.Signer.EthECKey.GenerateKey();



            // Use the private key to create an Ethereum account

            var account = new Account(ecKey.GetPrivateKey());

            var privateKey = ecKey.GetPrivateKeyAsBytes();



            // Create a keystore service instance and generate a keystore file

            var password = "myPassword123"; // replace with your own password

            var keystoreService = new KeyStoreService();

            var keystoreJson = keystoreService.EncryptAndGenerateDefaultKeyStoreAsJson(password, privateKey, account.Address);

            var walletAddress = account.Address;

             }



For getting the balance of a wallet:
 

public void GetBalanceAsync(string walletAddress)

        {

           

            var web3 = new Web3($"https://mainnet.infura.io/v3/" + infuraProjectId);

            var balance = await web3.Eth.GetBalance.SendRequestAsync(walletAddress);

            var etherAmount = Web3.Convert.FromWei(balance.Value);



          }


 

And finally, for singing and broadcasting, we can use the Nethereum library as well: 

 

using System;

using Nethereum.Web3;

using Nethereum.Hex.HexConvertors.Extensions;

using Nethereum.Signer;

using Nethereum.Util;

using Nethereum.RPC.Eth.DTOs;



class Program

{

    static void Main(string[] args)

    {

        string privateKey = "YOUR_PRIVATE_KEY";

        string fromAddress = "YOUR_WALLET_ADDRESS";

        string toAddress = "RECIPIENT_WALLET_ADDRESS";

        decimal amountToSend = 1;



        var web3 = new Web3("https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY");



        // Get current gas price

        var gasPriceGwei = web3.Eth.GasPrice.SendRequestAsync().Result.Value / 1000000000;

        var gasPrice = new HexBigInteger(UnitConversion.Convert.ToWei(gasPriceGwei, UnitConversion.EthUnit.Gwei));



        // Get current nonce

        var nonce = web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(fromAddress).Result;



        // Convert amount to Wei

        var amountToSendWei = new BigInteger(UnitConversion.Convert.ToWei(amountToSend));



        // Create transaction object

        var transactionInput = new TransactionInput

        {

            From = fromAddress,

            To = toAddress,

            Value = amountToSendWei,

            Nonce = nonce,

            GasPrice = gasPrice,

            Gas = new HexBigInteger(21000)

        };



        // Sign transaction

        var transaction = new TransactionSigner().SignTransaction(privateKey, transactionInput);



        // Broadcast transaction

        var transactionHash = web3.Eth.Transactions.SendRawTransaction.SendRequestAsync("0x" + transaction.ToHex()).Result;

        Console.WriteLine($"Transaction broadcasted with hash: {transactionHash}");

    }

}

 

How you enjoy!

 

comments powered by Disqus