【问题标题】:How to get Bitcoin Private Key from an ExtPrivKey using NBitcoin如何使用 NBitcoin 从 ExtPrivKey 获取比特币私钥
【发布时间】:2018-05-04 12:27:34
【问题描述】:

所以,我正在尝试使用 NBitcoin 汇款,有一个步骤我失败了,那就是创建比特币秘密来签署交易,我有地址和 ExtPrivKey,但我还没有得到祝你好运,任何推荐,这是我下面的代码。

 var priv = mbwallet.SelectedWallet.PrivateKeys[0].ToWif();
        //var ool = new BitcoinSecret(base58, App.Network);
        var privkey = mbwallet.SelectedWallet.PrivateKeys[0].PrivateKey.GetBitcoinSecret(App.Network).ToWif();
        var key = Key.Parse(privkey, App.Network);
        var keysT = key.GetWif(App.Network);
        //var myaddress = mbwallet.SelectedWallet.PrivateKeys[0].PrivateKey.PubKey.GetAddress(App.Network);
        var myaddress = mbwallet.SelectedWallet.CurrentAddress;


        string address = Address.Text;
        var destination = BitcoinAddress.Create(address, App.Network);
        decimal value = Convert.ToDecimal(Value.Text);

        var coins2 = GetCoins(value);


        TransactionBuilder txBuilder = new TransactionBuilder();
        var tx = txBuilder.AddCoins(coins2)
            .AddKeys(keysT)
            .SetChange(myaddress)
            .Send(destination, new Money(value, MoneyUnit.BTC))
            .SendFees("0.0002");
        //.BuildTransaction(true);
        var tx2 = txBuilder.BuildTransaction(true);
        //Console.WriteLine(txBuilder.Verify(tx));

        var hello = tx2.ToHex();
        var txRepo = new NoSqlTransactionRepository();
        //txRepo.Put(tx.GetHash(), tx);
        //Assert(txBuilder.Verify(tx)); //check fully signed


        List<ICoin> GetCoins(decimal sendAmount)
        {
            //var mbwallet = (root.DataContext as MainWindowViewModel);
            var amountMoney = new Money(sendAmount, MoneyUnit.BTC);
            var client = new QBitNinjaClient(App.Network);
            var txInAmount = Money.Zero;
            var coins1 = new List<ICoin>();
            foreach (var balance in client.GetBalance(mbwallet.SelectedWallet.CurrentAddress,//MBWallet.Wallet.Address,
        true).Result.Operations)
            {
                var transactionId = balance.TransactionId;
                var transactionResponse =
        client.GetTransaction(transactionId).Result;
                var receivedCoins = transactionResponse.ReceivedCoins;
                foreach (Coin coin in receivedCoins)
                {
                    if (coin.TxOut.ScriptPubKey ==
                        mbwallet.SelectedWallet.CurrentAddress.ScriptPubKey)//MBWallet.Wallet.BitcoinPrivateKey.ScriptPubKey) // this may not be necessary
                    {
                        coins1.Add(coin);
                        txInAmount += (coin.Amount as Money);
                    }
                }
            }
            return coins1;
        }

【问题讨论】:

    标签: c# bitcoin private-key nbitcoin


    【解决方案1】:

    对于我在代码中看到的内容,您已经将私钥添加到构建器,所以基本上您只需要签名,类似这样

    交易签名 = txBuilder.SignTransaction(tx2);

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2021-12-25
      • 2020-02-13
      • 2023-03-24
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      相关资源
      最近更新 更多