您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用C#開發(fā)比特幣錢包”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“怎么用C#開發(fā)比特幣錢包”吧!
我們要開發(fā)的比特幣錢包要實(shí)現(xiàn)以下功能:
可以使用BIP39助記詞恢復(fù)密鑰
可以創(chuàng)建比特地址,可以接收其他地址轉(zhuǎn)來(lái)的比特幣
可以查看比特幣地址余額
可以向其他地址支付比特幣
首先需要引入NBitcoin開發(fā)包以及QBitNinja開發(fā)包:
using NBitcoin; using QBitNinja.Client; using QBitNinja.Client.Models;
我們需要保存下來(lái)生成的助記詞:
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)); } }
感謝各位的閱讀,以上就是“怎么用C#開發(fā)比特幣錢包”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用C#開發(fā)比特幣錢包這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。