Last active
July 4, 2017 11:47
-
-
Save gbaeke/ced898b17807f58c31b3a1420633245d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using NBitcoin; | |
using QBitNinja.Client; | |
using QBitNinja.Client.Models; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// generated WIF earlier; use that now to get private key | |
var bitcoinPrivateKey = new | |
BitcoinSecret("BITCOIN SECRET HERE"); | |
// network will be on testnet | |
var network = bitcoinPrivateKey.Network; | |
var address = bitcoinPrivateKey.GetAddress(); | |
Console.WriteLine(bitcoinPrivateKey); | |
Console.WriteLine(address); | |
// tx id from faucet: 42324295bb74b2e10831a6d3e090f8c7bb4c60fe2fff36ec39c3d72cdbf0e3e2 | |
var client = new QBitNinjaClient(network); | |
var transactionId = uint256.Parse("42324295bb74b2e10831a6d3e090f8c7bb4c60fe2fff36ec39c3d72cdbf0e3e2"); | |
var transactionResponse = client.GetTransaction(transactionId).Result; | |
Console.WriteLine(transactionResponse.TransactionId); | |
Console.WriteLine(transactionResponse.Block.Confirmations); | |
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 does not 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 | |
}); | |
// address to spend to; use this for testnet address | |
var copayWallet = BitcoinAddress.Create("myTV1ojgfFt7WUXKxkaz6eoLPJ1Cg9grP7"); | |
// txout to copay wallet address | |
TxOut copayTxOut = new TxOut() | |
{ | |
Value = new Money((decimal)0.5, MoneyUnit.BTC), | |
ScriptPubKey = copayWallet.ScriptPubKey | |
}; | |
// txout of remainder to yourself minus miner fee | |
TxOut changeTxOut = new TxOut() | |
{ | |
Value = new Money((decimal)0.59, MoneyUnit.BTC), | |
ScriptPubKey = bitcoinPrivateKey.ScriptPubKey | |
}; | |
transaction.Outputs.Add(copayTxOut); | |
transaction.Outputs.Add(changeTxOut); | |
var message = "NBitcoin rocks!"; | |
var bytes = Encoding.UTF8.GetBytes(message); | |
transaction.Outputs.Add(new TxOut() | |
{ | |
Value = Money.Zero, | |
ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes) | |
}); | |
transaction.Inputs[0].ScriptSig = bitcoinPrivateKey.ScriptPubKey; | |
transaction.Sign(bitcoinPrivateKey, false); | |
// now we have to propagate the tx | |
// I do not have own Bitcoin Core so use QBitNinja | |
BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result; | |
if (!broadcastResponse.Success) | |
{ | |
Console.WriteLine("ErrorCode: " + 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()); | |
} | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment