如何直接在C#中创建以太坊钱包

3
我已经查阅了许多资料,但还是不知道如何在C#中创建钱包或集成以太坊区块链。有许多技术可以使用,如Web3.js、MyEtherWallet(也是js)和Nethereum(使用Infura作为API调用的C#)。还有一个名为blockcypher.com的服务。
如何创建一个公共的以太坊钱包,以便从一个钱包向另一个钱包转账?它的端点是什么?
我想要做的是,在我的Web应用程序中为每个用户编程创建一个钱包,并且当他们赚取积分时,我想再次通过编程将资金从我的钱包移动到用户的钱包。
任何建议都将不胜感激。
提前致谢!

请了解一下Nethereum,它是一个针对以太坊的开源 .Net 集成库。 - Trevor
2个回答

5
尝试使用Nethereum,您可以使用任何rpc端点,而不仅仅是Infura。这与Web3js、MyEtherWallet、Web3j等相同。
显然,您需要运行像Geth、Parity或Besu之类的节点。
这是在Nethereum公共测试链中转移以太币的示例。
此外,您可以将其与@LOST的响应结合使用,使用KeyStore标准加密和解密私钥。
您可以在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");
    }
}

您可以在此处找到使用Nethereum的以太坊钱包的多个示例 http://docs.nethereum.com/en/latest/nethereum-ui-wallets/


5

下面是使用 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 可以存储在文件中。大多数情况下,人们都使用这种文件格式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接