【问题标题】:How do you Create an Ethereum wallet in C# directly如何直接在 C# 中创建以太坊钱包
【发布时间】:2018-12-05 23:03:31
【问题描述】:
我已经用尽了许多资源来尝试找到这个答案,但您如何创建钱包或将Ethereum Blockchain 集成到 C# 中?有许多技术,例如 Web3.js、MyEtherWallet(也是 js)和 Nethereum,它们是 C#,但使用 Infura 作为 API 调用。还有一个服务叫blockcypher.com
如何创建一个公开的以太坊钱包,以便将资金从一个钱包转移到另一个钱包?什么是终点?
我想做的是以编程方式为使用我的网络应用程序的每个用户创建一个钱包,当他们赚取积分时,我想再次以编程方式将资金从我的钱包转移到我用户的钱包。
任何建议将不胜感激
提前致谢
【问题讨论】:
标签:
c#
blockchain
ethereum
【解决方案1】:
这是一个使用 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 可以存储在文件中。 Mist 使用了这种文件格式。
【解决方案2】:
试试 Nethereum,你可以使用任何 rpc 端点,而不仅仅是 Infura。这与 Web3js、MyEtherWallet、Web3j 等相同。
显然,您需要运行一个像 Geth、Parity 或 Besu 这样的节点。
这是在 Nethereum 的公共测试链中转移 Ether 的示例。
您还可以将它与@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/