用C#开发自己的比特币钱包
2021-06-06 14:04
标签:ofo 整理 使用方法 指定 list ssi fir 访问 money 如果要快速掌握NBitcoin的使用方法,推荐访问汇智网的课程: 我们要开发的比特币钱包要实现以下功能: 首先需要引入NBitcoin开发包以及QBitNinja开发包: 我们需要保存下来生成的助记词: 下面的代码可以利用NBitcoin生成比特币HD地址: 下面的代码可以获取指定地址的比特币余额: 下面的代码可以利用NBitcoin实现向指定的地址转账比特币: 原文:Step by step guide to programming your own bitcoin wallet 汇智网翻译整理,转载请标明出处。 用C#开发自己的比特币钱包 标签:ofo 整理 使用方法 指定 list ssi fir 访问 money 原文地址:https://blog.51cto.com/xxzhi/2384777
开发目标
引入NBitcoin开发包
using NBitcoin;
using QBitNinja.Client;
using QBitNinja.Client.Models;
生成BIP39助记词
public void MssGenerateMnemo(out string ssMnemo) {
Mnemonic mnemonic = new Mnemonic(Wordlist.English, WordCount.Twelve);
ssMnemo = mnemonic.ToString();
}
生成比特币地址
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
向其他比特币地址转账
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