这篇文章主要讲解了“怎么用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。