How to get Bitcoin Private Key from an ExtPrivKey using NBitcoin

0 votes

So, I am trying to send some money over using NBitcoin, there is a step where I am failing and that is creating de bitcoin secret to sign the transaction, I have the address, and the ExtPrivKey but I haven't gotten any luck signing it, any recommendation, this is my code below.

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; }

Apr 5, 2022 in Blockchain by Rahul
• 9,680 points
1,270 views

1 answer to this question.

0 votes

From what I see in the code you already add the private key to the builder so basically you only need to sign, like this:-

Transaction signed = txBuilder.SignTransaction(tx2);

answered Apr 7, 2022 by Aditya
• 7,680 points

Related Questions In Blockchain

0 votes
1 answer

Bitcoin private key: translate from bash to perl

use bignum; # Get arbitrary precision arithmetic # ...READ MORE

answered Sep 4, 2018 in Blockchain by slayer
• 29,370 points
1,310 views
0 votes
1 answer
0 votes
1 answer

How to get value from solidity contract using java?

web3j caters for this very use case. It ...READ MORE

answered Sep 28, 2018 in Blockchain by slayer
• 29,370 points
2,056 views
0 votes
1 answer

Hyperledger Fabric: How to get transaction history using key?

history, err := stub.GetHistoryForKey(key_value) for history.HasNext() { ...READ MORE

answered Nov 20, 2018 in Blockchain by Omkar
• 69,220 points
3,555 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,460 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,947 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,470 views
0 votes
1 answer

How to get price from bitcoin to USD with api

If I'm not wrong, is this what ...READ MORE

answered Apr 7, 2022 in Blockchain by Aditya
• 7,680 points
2,911 views
0 votes
1 answer

How to get bitcoins for testing?

In order to see how Bitcoin works, ...READ MORE

answered Apr 6, 2022 in Blockchain by Aditya
• 7,680 points
587 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP