您好,登錄后才能下訂單哦!
在這個(gè)教程中,我們將使用C#來開發(fā)一個(gè)比特幣錢包,我們使用NBitcoin這個(gè)庫。教程中的代碼實(shí)現(xiàn)了比特幣的存儲(chǔ)、接收和支付功能,可以很容易地移植到其他應(yīng)用中。
如果要快速掌握NBitcoin的使用方法,推薦訪問匯智網(wǎng)的課程:
- C#比特幣開發(fā)詳解
我們要開發(fā)的比特幣錢包要實(shí)現(xiàn)以下功能:
首先需要引入NBitcoin開發(fā)包以及QBitNinja開發(fā)包:
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實(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)明出處。
免責(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)容。