简体   繁体   中英

How to get Bitcoin Private Key from an ExtPrivKey using NBitcoin

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

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

Transaction signed = txBuilder.SignTransaction(tx2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM