Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you Create an Ethereum wallet in C# directly

I have exhausted a number of sources to try and find this answer but simply how do you create a wallet or integrate the Ethereum Blockchain in C#? There are a number of technologies such as Web3.js, MyEtherWallet (also js) and Nethereum which is C# but uses Infura as the API call. There is also a service called blockcypher.com

How do you Create an Ethereum wallet that is public to the transfer funds to from one wallet to another? What is the endpoint?

What I am looking to do is programmatically create a wallet for each of my users using my web app and as they earn points I again want to programmatically move funds from my wallet to my user's wallet.

Any advice would be appreciated

Thanks in advance

like image 787
CR41G14 Avatar asked Oct 17 '25 17:10

CR41G14


2 Answers

Here's an example using Nethereum:

string password = "MYSTRONGPASS";

EthECKey key = EthECKey.GenerateKey();
byte[] privateKey = key.GetPrivateKeyAsBytes();
string address = key.GetPublicAddress();
var keyStore = new KeyStoreScryptService();

string json = keyStore.EncryptAndGenerateKeyStoreAsJson(
    password: password,
    privateKey: privateKey,
    addresss: address);

json can be stored in a file. Mist used this file format.

like image 186
LOST Avatar answered Oct 19 '25 06:10

LOST


Try Nethereum you can use any rpc end point, not just Infura. This is the same as Web3js, MyEtherWallet, Web3j, etc.

Obviously you will need to have a Node running like Geth, Parity or Besu.

This is an example of transferring Ether in Nethereum's public test chain.

Also you can combine it with @LOST response to encrypt and decrypt your private key using the KeyStore standard.

You can run this sample in the Nethereum Playground http://playground.nethereum.com/csharp/id/1003.


using System;
using System.Text;
using Nethereum.Hex.HexConvertors.Extensions;
using System.Threading.Tasks;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;

public class Program
{
    private static async Task Main(string[] args)
    {
        //First let's create an account with our private key for the account address 
        var privateKey = "0x7580e7fb49df1c861f0050fae31c2224c6aba908e116b8da44ee8cd927b990b0";
        var account = new Account(privateKey);
        Console.WriteLine("Our account: " + account.Address);
        //Now let's create an instance of Web3 using our account pointing to our nethereum testchain
        var web3 = new Web3(account, "http://testchain.nethereum.com:8545");

        // Check the balance of the account we are going to send the Ether
        var balance = await web3.Eth.GetBalance.SendRequestAsync("0x13f022d72158410433cbd66f5dd8bf6d2d129924");
        Console.WriteLine("Receiver account balance before sending Ether: " + balance.Value + " Wei");
        Console.WriteLine("Receiver account balance before sending Ether: " + Web3.Convert.FromWei(balance.Value) +
                          " Ether");

        // Lets transfer 1.11 Ether
        var transaction = await web3.Eth.GetEtherTransferService()
            .TransferEtherAndWaitForReceiptAsync("0x13f022d72158410433cbd66f5dd8bf6d2d129924", 1.11m);

        balance = await web3.Eth.GetBalance.SendRequestAsync("0x13f022d72158410433cbd66f5dd8bf6d2d129924");
        Console.WriteLine("Receiver account balance after sending Ether: " + balance.Value);
        Console.WriteLine("Receiver account balance after sending Ether: " + Web3.Convert.FromWei(balance.Value) +
                          " Ether");
    }
}

You have many samples of Ethereum wallets using Nethereum, they can be found here http://docs.nethereum.com/en/latest/nethereum-ui-wallets/

like image 44
Juan Blanco Avatar answered Oct 19 '25 07:10

Juan Blanco