Ich verwende die NBitcoin-Bibliothek, um ein einfaches Bitcoin-Transferprogramm in C# zu programmieren.
Das Programm nimmt die Coins von einer Transaktions-ID und überträgt sie dann an eine Ziel-Wallet-Adresse
Den Code zu diesem Programm finden Sie im Buch „Programming the BlockChain in C# – (Community Edition)“ auf den Seiten 37 bis 43
Ich habe den C#-Code wie folgt in diesen Thread eingefügt.
Ich habe ein TestNet-Test-Wallet erstellt und verwende die Wallet-Adresse, um die Bitcoins des Programms zu erhalten.
Das TestNet Test Wallet kann erstellt werden unter -
http://testnetwallet.com/wallet
Um die Adresse des TestNet-Test-Wallets zu erhalten, logge ich mich in mein TestNet-Wallet ein und klicke auf den „Receive“-Button. Ein Fenster erscheint und darin ist die Empfangsadresse des TestNet-Wallets.
Ich versuche meine Bitcoins an diese Empfangsadresse zu senden und irgendwie kommen die Bitcoins einfach nicht in die Wallet.
Das Programm sagt, dass die Transaktion erfolgreich ist und gibt sogar eine Transaktions-ID zurück.
Aber bei der Überprüfung der TestNet-Wallet finde ich überhaupt keine Transaktionen und auch keine neu eingezahlten Bitcoins ?
Kann eine freundliche Seele bitte den unten angehängten C#-Code durchsehen, um zu sehen, was falsch ist?
Ich verwende die NBitCoin C#-Bibliothek mit Visual Studio 2015.
Der folgende Code ist in Visual Studio 2015 in einem „Visual C#-Konsolenprogramm“ enthalten.
// ReSharper disable All
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NBitcoin;
using NBitcoin.Protocol;
using QBitNinja.Client;
using QBitNinja.Client.Models;
namespace SpendYourCoins
{
class Program
{
static void Main()
{
/*
#region CREATE NEW PRIVKEY
//var network = Network.TestNet;
//Key privateKey = new Key();
//var bitcoinPrivateKey = privateKey.GetWif(network);
#endregion
*/
#region IMPORT PRIVKEY
var bitcoinPrivateKey = new BitcoinSecret("cTNoQtcdj9ZRV9XRNDFVisbif2XMSbxEbtP5WZTGJ2sGW9gSRr7Q");
var network = bitcoinPrivateKey.Network;
#endregion
var address = bitcoinPrivateKey.GetAddress();
Console.WriteLine(bitcoinPrivateKey); // cSZjE4aJNPpBtU6xvJ6J4iBzDgTmzTjbq8w2kqnYvAprBCyTsG4x
Console.WriteLine(address); // mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv
Console.WriteLine();
var client = new QBitNinjaClient(Network.TestNet);
var transactionId = uint256.Parse("12e548ababf06160486f91bfa2a406f131631258b2db025eb36bcd5230a70524");
var transactionResponse = client.GetTransaction(transactionId).Result;
Console.WriteLine(transactionResponse.TransactionId); // e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3
Console.WriteLine(transactionResponse.Block.Confirmations);
Console.WriteLine();
var receivedCoins = transactionResponse.ReceivedCoins;
OutPoint outPointToSpend = null;
foreach (var coin in receivedCoins)
{
if (coin.TxOut.ScriptPubKey == bitcoinPrivateKey.ScriptPubKey)
{
outPointToSpend = coin.Outpoint;
}
}
if(outPointToSpend == null)
throw new Exception("TxOut doesn't contain our ScriptPubKey");
Console.WriteLine("We want to spend {0}. outpoint:", outPointToSpend.N + 1);
var transaction = new Transaction();
transaction.Inputs.Add(new TxIn()
{
PrevOut = outPointToSpend
});
// var hallOfTheMakersAddress = new BitcoinPubKeyAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB");
//var hallOfTheMakersAddress = new BitcoinPubKeyAddress("2MsNF9f3nsdkEMV6un9u3pMVKcTNJPauqaU");
var hallOfTheMakersAddress = new BitcoinScriptAddress("2MtBDuoyhPa8athzErp8nj4NXMKgUQ2qGq8", Network.TestNet);
// How much you want to TO
var hallOfTheMakersAmount = new Money((decimal)0.5, MoneyUnit.BTC);
/* At the time of writing the mining fee is 0.05usd
* Depending on the market price and
* On the currently advised mining fee,
* You may consider to increase or decrease it
*/
var minerFee = new Money((decimal)0.0001, MoneyUnit.BTC);
// How much you want to spend FROM
var txInAmount = (Money)receivedCoins[(int) outPointToSpend.N].Amount;
Money changeBackAmount = txInAmount - hallOfTheMakersAmount - minerFee;
TxOut hallOfTheMakersTxOut = new TxOut()
{
Value = hallOfTheMakersAmount,
ScriptPubKey = hallOfTheMakersAddress.ScriptPubKey
};
TxOut changeBackTxOut = new TxOut()
{
Value = changeBackAmount,
ScriptPubKey = bitcoinPrivateKey.ScriptPubKey
};
transaction.Outputs.Add(hallOfTheMakersTxOut);
transaction.Outputs.Add(changeBackTxOut);
var message = "nopara73 loves NBitcoin!";
var bytes = Encoding.UTF8.GetBytes(message);
transaction.Outputs.Add(new TxOut()
{
Value = Money.Zero,
ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
});
//Console.WriteLine(transaction);
//var address = new BitcoinPubKeyAddress("mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv");
//transaction.Inputs[0].ScriptSig = address.ScriptPubKey;
// It is also OK:
transaction.Inputs[0].ScriptSig = bitcoinPrivateKey.ScriptPubKey;
transaction.Sign(bitcoinPrivateKey, false);
BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result;
if (!broadcastResponse.Success)
{
Console.WriteLine(string.Format("ErrorCode: {0}", broadcastResponse.Error.ErrorCode));
Console.WriteLine("Error message: " + broadcastResponse.Error.Reason);
}
else
{
Console.WriteLine("Success! You can check out the hash of the transaciton in any block explorer:");
Console.WriteLine(transaction.GetHash());
}
//using (var node = Node.ConnectToLocal(network)) //Connect to the node
//{
// node.VersionHandshake(); //Say hello
// //Advertize your transaction (send just the hash)
// node.SendMessage(new InvPayload(InventoryType.MSG_TX, transaction.GetHash()));
// //Send it
// node.SendMessage(new TxPayload(transaction));
// Thread.Sleep(500); //Wait a bit
//}
Console.ReadLine();
}
}
}
Ich habe Ihre Transaktion hier gefunden: 12e548ababf06160486f91bfa2a406f131631258b2db025eb36bcd5230a70524
Es wurde erfolgreich erstellt, signiert und übermittelt. Ich kann auch eine Nachricht sehen, die du hinzugefügt hast.
Es gibt ein paar Dinge, die ich aufgegriffen habe, die anders sind, wenn ich mir die Transaktion anschaue, und wie Sie sie aufgebaut haben.
Die Adresse, die von diesem privaten Schlüssel abgeleitet wird, ist entweder: mj36bqZz5DcQFhKysoanLirq9f6MNfT1oJ
oder
mweqjRVBWVTmiV8fqLYqWfWAmqzxCWYwJd (komprimiert)
während Sie das als "mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv" notieren. Sie können das hier bitaddress - testnet validieren
Gibt es einen Grund, warum Sie BitcoinScriptAddress verwenden
Mögen:
var hallOfTheMakersAddress = new BitcoinScriptAddress("2MtBDuoyhPa8athzErp8nj4NXMKgUQ2qGq8", Network.TestNet);
Statt so etwas wie:
var hallOfTheMakersAddress = new BitcoinAddress("mweqjRVBWVTmiV8fqLYqWfWAmqzxCWYwJd", Network.TestNet);
Dr. David Bowman
Error CS0144 Cannot create an instance of the abstract class or interface 'BitcoinAddress'
Können Sie bitte noch einmal nachsehen und mir dabei helfen?Kyluke McDougall
BitcoinAddress hallOfTheMakersAddress = BitcoinAddress.Create("mweqjRVBWVTmiV8fqLYqWfWAmqzxCWYwJd", Network.TestNet);
Dr. David Bowman
var hallOfTheMakersAddress = BitcoinAddress.Create("2NBzSQBwACBnMNbTR8QjZQKYwW61RZMd777", Network.TestNet);
Kyluke McDougall