您好,登錄后才能下訂單哦!
在這個教程中,我們將使用C#來開發一個比特幣錢包,我們使用NBitcoin這個庫。教程中的代碼實現了比特幣的存儲、接收和支付功能,可以很容易地移植到其他應用中。
如果要快速掌握NBitcoin的使用方法,推薦訪問匯智網的課程:
- C#比特幣開發詳解
我們要開發的比特幣錢包要實現以下功能:
首先需要引入NBitcoin開發包以及QBitNinja開發包:
using NBitcoin;
using QBitNinja.Client;
using QBitNinja.Client.Models;
我們需要保存下來生成的助記詞:
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
匯智網翻譯整理,轉載請標明出處。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。