用C#开发自己的比特币钱包

发布时间:2020-04-07 17:36:41 作者:小智123
来源:网络 阅读:677

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

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

开发目标

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

引入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<Coin>();
    var unspentCoinsConfirmed = new List<Coin>();    
    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<Coin>();
    var unspentCoinsConfirmed = new List<Coin>();    
    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

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

推荐阅读:
  1. 10个C#比特币应用开发必备的NuGet开发包
  2. 区块链去中化钱包开发,多币种钱包开发app

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

区块链

上一篇:cdn常见问题有哪些

下一篇:mysqlbinlog 查看binlog日志时报错unknown variable 'default-character-set=utf8'

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》