在這個教程中,我們將使用C#來開發(fā)一個比特幣錢包,我們使用NBitcoin這個庫。教程中的代碼實(shí)現(xiàn)了比特幣的存儲、接收和支付功能,可以很容易地移植到其他應(yīng)用中。
在蓬萊等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作定制網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,蓬萊網(wǎng)站建設(shè)費(fèi)用合理。如果要快速掌握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)載請標(biāo)明出處。
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
網(wǎng)頁名稱:用C#開發(fā)自己的比特幣錢包-創(chuàng)新互聯(lián)
標(biāo)題來源:http://www.rwnh.cn/article10/jdjdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、品牌網(wǎng)站設(shè)計、移動網(wǎng)站建設(shè)、虛擬主機(jī)、用戶體驗、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容