溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

用C#開發(fā)自己的比特幣錢包

發(fā)布時(shí)間:2020-04-07 17:36:41 來源:網(wǎng)絡(luò) 閱讀:676 作者:小智123 欄目:編程語言

在這個(gè)教程中,我們將使用C#來開發(fā)一個(gè)比特幣錢包,我們使用NBitcoin這個(gè)庫。教程中的代碼實(shí)現(xiàn)了比特幣的存儲(chǔ)、接收和支付功能,可以很容易地移植到其他應(yīng)用中。

如果要快速掌握NBitcoin的使用方法,推薦訪問匯智網(wǎng)的課程:

  • C#比特幣開發(fā)詳解

開發(fā)目標(biāo)

我們要開發(fā)的比特幣錢包要實(shí)現(xiàn)以下功能:

  • 可以使用BIP39助記詞恢復(fù)密鑰
  • 可以創(chuàng)建比特地址,可以接收其他地址轉(zhuǎn)來的比特幣
  • 可以查看比特幣地址余額
  • 可以向其他地址支付比特幣

引入NBitcoin開發(fā)包

首先需要引入NBitcoin開發(fā)包以及QBitNinja開發(fā)包:

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));
  }

}

向其他比特幣地址轉(zhuǎn)賬

下面的代碼可以利用NBitcoin實(shí)現(xiàn)向指定的地址轉(zhuǎn)賬比特幣:

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

匯智網(wǎng)翻譯整理,轉(zhuǎn)載請(qǐng)標(biāo)明出處。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI