用C#开发自己的比特币钱包-创新互联

在这个教程中,我们将使用C#来开发一个比特币钱包,我们使用NBitcoin这个库。教程中的代码实现了比特币的存储、接收和支付功能,可以很容易地移植到其他应用中。

在蓬莱等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计制作、网站建设 网站设计制作定制网站建设,公司网站建设,企业网站建设,品牌网站制作,成都全网营销推广,成都外贸网站建设公司,蓬莱网站建设费用合理。

如果要快速掌握NBitcoin的使用方法,推荐访问汇智网的课程:

  • C#比特币开发详解

开发目标

我们要开发的比特币钱包要实现以下功能:

  • 可以使用BIP39助记词恢复密钥
  • 可以创建比特地址,可以接收其他地址转来的比特币
  • 可以查看比特币地址余额
  • 可以向其他地址支付比特币

引入NBitcoin开发包

首先需要引入NBitcoin开发包以及QBitNinja开发包:

using NBitcoin;
using QBitNinja.Client;
using QBitNinja.Client.Models;

生成BIP39助记词

我们需要保存下来生成的助记词:

public void MssGenerateMnemo(out string ssMnemo) {

Mnemonic mnemonic = new Mnemonic(Wordlist.English, WordCount.Twelve);

ssMnemo = mnemonic.ToString();

}

生成比特币地址

下面的代码可以利用NBitcoin生成比特币HD地址:

public void MssGenerateAddress(
    string ssMnemo, 
    int ssKeynumber, 
    bool ssIsTestNet, 
    out string ssAddress, 
    out string ssPrivateKey) {

  Network net;
  if (ssIsTestNet)
      net = Network.TestNet;
  else
      net = Network.Main;

  Mnemonic restoreNnemo = new Mnemonic(ssMnemo);

  ExtKey masterKey = restoreNnemo.DeriveExtKey();

  KeyPath keypth = new KeyPath("m/44'/0'/0'/0/" + ssKeynumber);
  ExtKey key = masterKey.Derive(keypth);

  ssAddress = key.PrivateKey.PubKey.GetAddress(net).ToString();
  ssPrivateKey = key.PrivateKey.GetBitcoinSecret(net).ToString();

}

获取比特币地址余额

下面的代码可以获取指定地址的比特币余额:

public void MssGetBalance(
    string ssAddress, 
    bool ssIsUnspentOnly, 
    bool ssIsTestNet, 
    out decimal ssBalance, 
    out decimal ssConfirmedBalance) {

  Network net;
  if (ssIsTestNet)
    net = Network.TestNet;
  else
    net = Network.Main;

  QBitNinjaClient client = new QBitNinjaClient(net);
  var balance = client.GetBalance(new BitcoinPubKeyAddress(ssAddress), ssIsUnspentOnly).Result;

  ssBalance = 0.0M;
  ssConfirmedBalance = 0.0M;

  if (balance.Operations.Count > 0)
  {
    var unspentCoins = new List();
    var unspentCoinsConfirmed = new List();    
    foreach (var operation in balance.Operations)
    {
        unspentCoins.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));
        if(operation.Confirmations > 0)
            unspentCoinsConfirmed.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));

    }

    ssBalance = unspentCoins.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
    ssConfirmedBalance = unspentCoinsConfirmed.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
  }

}

向其他比特币地址转账

下面的代码可以利用NBitcoin实现向指定的地址转账比特币:

public void MssGetBalance(
    string ssAddress, 
    bool ssIsUnspentOnly, 
    bool ssIsTestNet, 
    out decimal ssBalance, 
    out decimal ssConfirmedBalance) {

  Network net;
  if (ssIsTestNet)
      net = Network.TestNet;
  else
      net = Network.Main;

  QBitNinjaClient client = new QBitNinjaClient(net);
  var balance = client.GetBalance(new BitcoinPubKeyAddress(ssAddress), ssIsUnspentOnly).Result;

  ssBalance = 0.0M;
  ssConfirmedBalance = 0.0M;

  if (balance.Operations.Count > 0)
  {
    var unspentCoins = new List();
    var unspentCoinsConfirmed = new List();    
    foreach (var operation in balance.Operations)
    {
      unspentCoins.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));
      if(operation.Confirmations > 0)
          unspentCoinsConfirmed.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));

    }

    ssBalance = unspentCoins.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
    ssConfirmedBalance = unspentCoinsConfirmed.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
  }
}

原文:Step by step guide to programming your own bitcoin wallet

汇智网翻译整理,转载请标明出处。

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网站栏目:用C#开发自己的比特币钱包-创新互联
链接分享:http://azwzsj.com/article/jdjdo.html