Skip to content

Commit 8b829b9

Browse files
committed
New lessons added
1 parent 364a6d3 commit 8b829b9

File tree

12 files changed

+418
-2
lines changed

12 files changed

+418
-2
lines changed

ProgrammingTheBlockchain.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrivateKey", "PrivateKey\Pr
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Transaction", "Transaction\Transaction.csproj", "{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpendYourCoins", "SpendYourCoins\SpendYourCoins.csproj", "{D8C1E36E-BDB6-414A-A7CD-AAB042BA6F0F}"
15+
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProofOfOwnership", "ProofOfOwnership\ProofOfOwnership.csproj", "{4916EFA5-06BD-45EB-8263-3DD572F93265}"
17+
EndProject
1418
Global
1519
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1620
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +37,14 @@ Global
3337
{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
3438
{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
3539
{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{D8C1E36E-BDB6-414A-A7CD-AAB042BA6F0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{D8C1E36E-BDB6-414A-A7CD-AAB042BA6F0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{D8C1E36E-BDB6-414A-A7CD-AAB042BA6F0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{D8C1E36E-BDB6-414A-A7CD-AAB042BA6F0F}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{4916EFA5-06BD-45EB-8263-3DD572F93265}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{4916EFA5-06BD-45EB-8263-3DD572F93265}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{4916EFA5-06BD-45EB-8263-3DD572F93265}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{4916EFA5-06BD-45EB-8263-3DD572F93265}.Release|Any CPU.Build.0 = Release|Any CPU
3648
EndGlobalSection
3749
GlobalSection(SolutionProperties) = preSolution
3850
HideSolutionNode = FALSE

ProofOfOwnership/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>

ProofOfOwnership/Program.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using NBitcoin;
3+
// ReSharper disable All
4+
5+
namespace ProofOfOwnership
6+
{
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
SignAsCraigWright();
12+
VerifySatoshi();
13+
VerifyDorier();
14+
15+
Console.ReadLine();
16+
}
17+
18+
static void SignAsCraigWright()
19+
{
20+
var bitcoinPrivateKey = new BitcoinSecret("KzgjNRhcJ3HRjxVdFhv14BrYUKrYBzdoxQyR2iJBHG9SNGGgbmtC");
21+
22+
var message = "I am Craig Wright";
23+
string signature = bitcoinPrivateKey.PrivateKey.SignMessage(message);
24+
Console.WriteLine(signature); // IN5v9+3HGW1q71OqQ1boSZTm0/DCiMpI8E4JB1nD67TCbIVMRk/e3KrTT9GvOuu3NGN0w8R2lWOV2cxnBp+Of8c=
25+
}
26+
static void VerifySatoshi()
27+
{
28+
var message = "I am Craig Wright";
29+
var signature = "IN5v9+3HGW1q71OqQ1boSZTm0/DCiMpI8E4JB1nD67TCbIVMRk/e3KrTT9GvOuu3NGN0w8R2lWOV2cxnBp+Of8c=";
30+
31+
var address = new BitcoinPubKeyAddress("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa");
32+
bool isCraigWrightSatoshi = address.VerifyMessage(message, signature);
33+
34+
Console.WriteLine("Is Craig Wright Satoshi? " + isCraigWrightSatoshi);
35+
}
36+
static void VerifyDorier()
37+
{
38+
var address = new BitcoinPubKeyAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB");
39+
var message = "Nicolas Dorier Book Funding Address";
40+
var signature = "H1jiXPzun3rXi0N9v9R5fAWrfEae9WPmlL5DJBj1eTStSvpKdRR8Io6/uT9tGH/3OnzG6ym5yytuWoA9ahkC3dQ=";
41+
Console.WriteLine(address.VerifyMessage(message, signature));
42+
}
43+
}
44+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{4916EFA5-06BD-45EB-8263-3DD572F93265}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>ProofOfOwnership</RootNamespace>
11+
<AssemblyName>ProofOfOwnership</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="NBitcoin, Version=3.0.0.30, Culture=neutral, processorArchitecture=MSIL">
37+
<HintPath>..\packages\NBitcoin.3.0.0.30\lib\net45\NBitcoin.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
40+
<Reference Include="NBitcoin.BouncyCastle, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
41+
<HintPath>..\packages\NBitcoin.3.0.0.30\lib\net45\NBitcoin.BouncyCastle.dll</HintPath>
42+
<Private>True</Private>
43+
</Reference>
44+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
46+
<Private>True</Private>
47+
</Reference>
48+
<Reference Include="System" />
49+
<Reference Include="System.Core" />
50+
<Reference Include="System.Xml.Linq" />
51+
<Reference Include="System.Data.DataSetExtensions" />
52+
<Reference Include="Microsoft.CSharp" />
53+
<Reference Include="System.Data" />
54+
<Reference Include="System.Net.Http" />
55+
<Reference Include="System.Xml" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<Compile Include="Program.cs" />
59+
<Compile Include="Properties\AssemblyInfo.cs" />
60+
</ItemGroup>
61+
<ItemGroup>
62+
<None Include="App.config" />
63+
<None Include="packages.config" />
64+
</ItemGroup>
65+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
66+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
67+
Other similar extension points exist, see Microsoft.Common.targets.
68+
<Target Name="BeforeBuild">
69+
</Target>
70+
<Target Name="AfterBuild">
71+
</Target>
72+
-->
73+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ProofOfOwnership")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ProofOfOwnership")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("4916efa5-06bd-45eb-8263-3dd572f93265")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

ProofOfOwnership/packages.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="NBitcoin" version="3.0.0.30" targetFramework="net452" />
4+
<package id="Newtonsoft.Json" version="6.0.1" targetFramework="net452" />
5+
</packages>

SpendYourCoins/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>

SpendYourCoins/Program.cs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.Text;
3+
using System.Threading.Tasks;
4+
using NBitcoin;
5+
using QBitNinja.Client;
6+
using QBitNinja.Client.Models;
7+
8+
// ReSharper disable All
9+
10+
namespace SpendYourCoins
11+
{
12+
class Program
13+
{
14+
static void Main()
15+
{
16+
#region CREATE NEW PRIVKEY
17+
//var network = Network.TestNet;
18+
//Key privateKey = new Key();
19+
//var bitcoinPrivateKey = privateKey.GetWif(network);
20+
#endregion
21+
22+
#region IMPORT PRIVKEY
23+
var bitcoinPrivateKey = new BitcoinSecret("cSZjE4aJNPpBtU6xvJ6J4iBzDgTmzTjbq8w2kqnYvAprBCyTsG4x");
24+
var network = bitcoinPrivateKey.Network;
25+
#endregion
26+
27+
var address = bitcoinPrivateKey.GetAddress();
28+
29+
Console.WriteLine(bitcoinPrivateKey); // cSZjE4aJNPpBtU6xvJ6J4iBzDgTmzTjbq8w2kqnYvAprBCyTsG4x
30+
Console.WriteLine(address); // mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv
31+
Console.WriteLine();
32+
33+
var client = new QBitNinjaClient(network);
34+
var transactionId = uint256.Parse("e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3");
35+
var transactionResponse = client.GetTransaction(transactionId).Result;
36+
37+
Console.WriteLine(transactionResponse.TransactionId); // e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3
38+
Console.WriteLine(transactionResponse.Block.Confirmations);
39+
Console.WriteLine();
40+
41+
var receivedCoins = transactionResponse.ReceivedCoins;
42+
OutPoint outPointToSpend = null;
43+
Script scriptPubKeyToSpend = null;
44+
foreach (var coin in receivedCoins)
45+
{
46+
if (coin.TxOut.ScriptPubKey == bitcoinPrivateKey.ScriptPubKey)
47+
{
48+
outPointToSpend = coin.Outpoint;
49+
scriptPubKeyToSpend = coin.GetScriptCode();
50+
}
51+
}
52+
if(outPointToSpend == null)
53+
throw new Exception("TxOut doesn't contain our ScriptPubKey");
54+
Console.WriteLine("We want to spend {0}. outpoint:", outPointToSpend.N + 1);
55+
56+
var transaction = new Transaction();
57+
transaction.Inputs.Add(new TxIn()
58+
{
59+
PrevOut = outPointToSpend
60+
});
61+
62+
// var hallOfTheMakersAddress = new BitcoinPubKeyAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB");
63+
var hallOfTheMakersAddress = new BitcoinPubKeyAddress("mzp4No5cmCXjZUpf112B1XWsvWBfws5bbB");
64+
65+
// How much you want to TO
66+
var hallOfTheMakersAmount = new Money((decimal)0.5, MoneyUnit.BTC);
67+
/* At the time of writing the mining fee is 0.05usd
68+
* Depending on the market price and
69+
* On the currently advised mining fee,
70+
* You may consider to increase or decrease it
71+
*/
72+
var minerFee = new Money((decimal)0.0001, MoneyUnit.BTC);
73+
// How much you want to spend FROM
74+
var txInAmount = (Money)receivedCoins[(int) outPointToSpend.N].Amount;
75+
Money changeBackAmount = txInAmount - hallOfTheMakersAmount - minerFee;
76+
77+
TxOut hallOfTheMakersTxOut = new TxOut()
78+
{
79+
Value = hallOfTheMakersAmount,
80+
ScriptPubKey = hallOfTheMakersAddress.ScriptPubKey
81+
};
82+
83+
TxOut changeBackTxOut = new TxOut()
84+
{
85+
Value = changeBackAmount,
86+
ScriptPubKey = bitcoinPrivateKey.ScriptPubKey
87+
};
88+
89+
transaction.Outputs.Add(hallOfTheMakersTxOut);
90+
transaction.Outputs.Add(changeBackTxOut);
91+
92+
var message = "nopara73 loves NBitcoin!";
93+
var bytes = Encoding.UTF8.GetBytes(message);
94+
transaction.Outputs.Add(new TxOut()
95+
{
96+
Value = Money.Zero,
97+
ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
98+
});
99+
100+
101+
TxIn input = transaction.Inputs[0];
102+
input.ScriptSig = hallOfTheMakersAddress.ScriptPubKey;
103+
transaction.Sign(bitcoinPrivateKey, false);
104+
105+
BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result;
106+
107+
if (!broadcastResponse.Success)
108+
{
109+
Console.WriteLine("ErrorCode: " + broadcastResponse.Error.ErrorCode);
110+
Console.WriteLine("Error message: " + broadcastResponse.Error.Reason);
111+
}
112+
113+
114+
Console.ReadLine();
115+
}
116+
}
117+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SpendYourCoins")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SpendYourCoins")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("d8c1e36e-bdb6-414a-a7cd-aab042ba6f0f")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)