Created
July 7, 2015 11:41
-
-
Save TOMOAKI12345/3b7fa89dd939b31adaee 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.Threading.Tasks; | |
using System.Text; | |
using System.Net; | |
using System.Threading; | |
using System.IO; | |
using System.Diagnostics; | |
using MonoMac.AppKit; | |
using System.Configuration; | |
using NBitcoin; | |
using NBitcoin.Protocol; | |
using NBitcoin.DataEncoders; | |
using NBitcoin.Protocol.Behaviors; | |
namespace ProgrammingBlockchain | |
{ | |
public class Chapter2 | |
{ | |
public Chapter2 () | |
{ | |
} | |
public void Lesson1() | |
{ | |
var blockr = new BlockrTransactionRepository(); | |
Transaction transaction = blockr.Get("e2a724193805814a7ce0c88dc68a5e3e06999ae9edde3a621c4ad1b9a620b2b4"); | |
Console.WriteLine(transaction.ToString()); | |
} | |
public void Lesson2(BitcoinAddress myAddress, BitcoinSecret mySecret) | |
{ | |
var blockr = new BlockrTransactionRepository (); | |
Transaction fundingTransaction = blockr.Get ("e2a724193805814a7ce0c88dc68a5e3e06999ae9edde3a621c4ad1b9a620b2b4"); | |
Transaction pTransaction = new Transaction (); | |
pTransaction.Inputs.Add (new TxIn () { | |
PrevOut = new OutPoint (fundingTransaction.GetHash (), 1) | |
}); | |
BitcoinAddress programmingBlockchainAddress = new BitcoinAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB"); | |
Console.WriteLine("Generating transaction on local."); | |
pTransaction.Outputs.Add(new TxOut() { | |
Value = Money.Coins(0.00059m), | |
ScriptPubKey = myAddress.ScriptPubKey | |
}); | |
pTransaction.Outputs.Add (new TxOut () { | |
Value = Money.Coins (0.0004m), | |
ScriptPubKey = programmingBlockchainAddress.ScriptPubKey | |
}); | |
//Feedback ! | |
var message = "Thanks from Tomoaki at Tokyo !"; | |
var bytes = Encoding.UTF8.GetBytes(message); | |
pTransaction.Outputs.Add(new TxOut(){ | |
Value = Money.Coins(0.00005m), | |
ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes) | |
}); | |
// signing part is last. | |
pTransaction.Inputs[0].ScriptSig = myAddress.ScriptPubKey; | |
pTransaction.Sign (mySecret, false); | |
Console.WriteLine(pTransaction); | |
// connect to the bitcoin node and propagate transaction | |
var node = Node.ConnectToLocal(Network.Main); | |
// using () { | |
// node.VersionHandshake(); //Say hello | |
// //Advertize your transaction (send just the hash) | |
// node.SendMessage(new InvPayload(InventoryType.MSG_TX, pTransaction.GetHash())); | |
// //Send it | |
// node.SendMessage(new TxPayload(pTransaction)); | |
// Thread.Sleep(500); //Wait a bit | |
// } | |
} | |
public void Lesson3() | |
{ | |
// verify the ownership of the address. | |
BitcoinAddress address = new BitcoinAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB"); | |
var msg = "Nicolas Dorier Book Funding Address"; | |
var sig = "H1jiXPzun3rXi0N9v9R5fAWrfEae9WPmlL5DJBj1eTStSvpKdRR8Io6/uT9tGH/3OnzG6ym5yytuWoA9a hkC3dQ="; | |
Console.WriteLine(address.VerifyMessage(msg, sig)); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment