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