Skip to content

Commit 364a6d3

Browse files
committed
Transaction project added
1 parent 6828895 commit 364a6d3

File tree

7 files changed

+311
-2
lines changed

7 files changed

+311
-2
lines changed

PrivateKey/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ static void Main()
1717
bool WifIsBitcoinSecret = mainNetPrivateKey == privateKey.GetWif(Network.Main);
1818
Console.WriteLine(WifIsBitcoinSecret); // True
1919

20-
20+
2121
BitcoinSecret bitcoinPrivateKey = privateKey.GetWif(Network.Main); // L5B67zvrndS5c71EjkrTJZ99UaoVbMUAK58GKdQUfYCpAa6jypvn
2222
Key samePrivateKey = bitcoinPrivateKey.PrivateKey;
23-
Console.WriteLine(privateKey == samePrivateKey); // True
2423

2524
PubKey publicKey = privateKey.PubKey;
2625
BitcoinPubKeyAddress bitcoinPubicKey = publicKey.GetAddress(Network.Main); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY

ProgrammingTheBlockchain.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaymentScript", "PaymentScr
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrivateKey", "PrivateKey\PrivateKey.csproj", "{8F4F2562-A2CA-4885-8638-F71CC44F5648}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Transaction", "Transaction\Transaction.csproj", "{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{8F4F2562-A2CA-4885-8638-F71CC44F5648}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{8F4F2562-A2CA-4885-8638-F71CC44F5648}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{8F4F2562-A2CA-4885-8638-F71CC44F5648}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

Transaction/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>

Transaction/Program.cs

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NBitcoin;
5+
using QBitNinja.Client;
6+
using QBitNinja.Client.JsonConverters;
7+
using QBitNinja.Client.Models;
8+
// ReSharper disable All
9+
10+
11+
namespace Transaction
12+
{
13+
class Program
14+
{
15+
static void Main()
16+
{
17+
// Create a client
18+
QBitNinjaClient client = new QBitNinjaClient(Network.Main);
19+
// Parse transaction id to NBitcoin.uint256 so the client can eat it
20+
var transactionId = uint256.Parse("f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94");
21+
// Query the transaction
22+
QBitNinja.Client.Models.GetTransactionResponse transactionResponse = client.GetTransaction(transactionId).Result;
23+
24+
25+
NBitcoin.Transaction transaction = transactionResponse.Transaction;
26+
27+
Console.WriteLine(transactionResponse.TransactionId); // f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94
28+
Console.WriteLine(transaction.GetHash()); // f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94
29+
30+
// RECEIVED COINS
31+
List<ICoin> receivedCoins = transactionResponse.ReceivedCoins;
32+
foreach (Coin coin in receivedCoins)
33+
{
34+
Money amount = coin.Amount;
35+
36+
Console.WriteLine(amount.ToDecimal(MoneyUnit.BTC));
37+
var paymentScript = coin.GetScriptCode();
38+
Console.WriteLine(paymentScript); // It's the ScriptPubKey
39+
var address = paymentScript.GetDestinationAddress(Network.Main);
40+
Console.WriteLine(address);
41+
Console.WriteLine();
42+
}
43+
44+
// RECEIVED COINS
45+
var outputs = transaction.Outputs;
46+
foreach (TxOut output in outputs)
47+
{
48+
Coin coin = new Coin(transaction, output);
49+
Money amount = coin.Amount;
50+
51+
Console.WriteLine(amount.ToDecimal(MoneyUnit.BTC));
52+
var paymentScript = coin.GetScriptCode();
53+
Console.WriteLine(paymentScript); // It's the ScriptPubKey
54+
var address = paymentScript.GetDestinationAddress(Network.Main);
55+
Console.WriteLine(address);
56+
Console.WriteLine();
57+
}
58+
59+
// RECEIVED COINS
60+
foreach (TxOut output in outputs)
61+
{
62+
Money amount = output.Value;
63+
64+
Console.WriteLine(amount.ToDecimal(MoneyUnit.BTC));
65+
var paymentScript = output.ScriptPubKey;
66+
Console.WriteLine(paymentScript); // It's the ScriptPubKey
67+
var address = paymentScript.GetDestinationAddress(Network.Main);
68+
Console.WriteLine(address);
69+
Console.WriteLine();
70+
}
71+
72+
// SPENT COINS
73+
List<ICoin> spentCoins = transactionResponse.SpentCoins;
74+
foreach (Coin coin in spentCoins)
75+
{
76+
Money amount = coin.Amount;
77+
78+
Console.WriteLine(amount.ToDecimal(MoneyUnit.BTC));
79+
var paymentScript = coin.GetScriptCode();
80+
Console.WriteLine(paymentScript); // It's the ScriptPubKey
81+
var address = paymentScript.GetDestinationAddress(Network.Main);
82+
Console.WriteLine(address);
83+
Console.WriteLine();
84+
}
85+
86+
// SPENT COINS
87+
foreach (Coin coin in spentCoins)
88+
{
89+
TxOut previousOutput = coin.TxOut;
90+
Money amount = previousOutput.Value;
91+
92+
Console.WriteLine(amount.ToDecimal(MoneyUnit.BTC));
93+
var paymentScript = previousOutput.ScriptPubKey;
94+
Console.WriteLine(paymentScript); // It's the ScriptPubKey
95+
var address = paymentScript.GetDestinationAddress(Network.Main);
96+
Console.WriteLine(address);
97+
Console.WriteLine();
98+
}
99+
100+
101+
102+
var inputs = transaction.Inputs;
103+
foreach (TxIn input in inputs)
104+
{
105+
OutPoint previousOutpoint = input.PrevOut;
106+
Console.WriteLine(previousOutpoint.Hash); // hash of prev tx
107+
Console.WriteLine(previousOutpoint.N); // idx of out from prev tx, that has been spent in the current tx
108+
Console.WriteLine();
109+
}
110+
111+
// Let's create a txout with 21 bitcoin from the first ScriptPubKey in our current transaction
112+
Money twentyOneBtc = new Money(21, MoneyUnit.BTC);
113+
var scriptPubKey = transaction.Outputs.First().ScriptPubKey;
114+
TxOut txOut = new TxOut(twentyOneBtc, scriptPubKey);
115+
116+
OutPoint firstOutPoint = spentCoins.First().Outpoint;
117+
Console.WriteLine(firstOutPoint.Hash); // 4788c5ef8ffd0463422bcafdfab240f5bf0be690482ceccde79c51cfce209edd
118+
Console.WriteLine(firstOutPoint.N); // 0
119+
120+
Console.WriteLine(transaction.Inputs.Count); // 9
121+
122+
OutPoint firstPreviousOutPoint = transaction.Inputs.First().PrevOut;
123+
var firstPreviousTransactionResponse = client.GetTransaction(firstPreviousOutPoint.Hash).Result;
124+
Console.WriteLine(firstPreviousTransactionResponse.IsCoinbase); // False
125+
NBitcoin.Transaction firstPreviousTransaction = firstPreviousTransactionResponse.Transaction;
126+
127+
//while (firstPreviousTransactionResponse.IsCoinbase == false)
128+
//{
129+
// Console.WriteLine(firstPreviousTransaction.GetHash());
130+
131+
// firstPreviousOutPoint = firstPreviousTransaction.Inputs.First().PrevOut;
132+
// firstPreviousTransactionResponse = client.GetTransaction(firstPreviousOutPoint.Hash).Result;
133+
// firstPreviousTransaction = firstPreviousTransactionResponse.Transaction;
134+
//}
135+
136+
Money spentAmount = Money.Zero;
137+
foreach (var spentCoin in spentCoins)
138+
{
139+
spentAmount = (Money)spentCoin.Amount.Add(spentAmount);
140+
}
141+
Console.WriteLine(spentAmount.ToDecimal(MoneyUnit.BTC)); // 13.19703492
142+
143+
Money receivedAmount = Money.Zero;
144+
foreach (var receivedCoin in receivedCoins)
145+
{
146+
receivedAmount = (Money)receivedCoin.Amount.Add(receivedAmount);
147+
}
148+
Console.WriteLine(receivedAmount.ToDecimal(MoneyUnit.BTC)); // 13.19683492
149+
150+
Console.WriteLine((spentAmount - receivedAmount).ToDecimal(MoneyUnit.BTC));
151+
152+
Console.WriteLine(spentAmount.ToDecimal(MoneyUnit.BTC)-receivedAmount.ToDecimal(MoneyUnit.BTC));
153+
154+
//var inputs = transaction.Inputs;
155+
//foreach (TxIn input in inputs)
156+
//{
157+
// uint256 previousTransactionId = input.PrevOut.Hash;
158+
// GetTransactionResponse previousTransactionResponse = client.GetTransaction(previousTransactionId).Result;
159+
160+
// NBitcoin.Transaction previousTransaction = previousTransactionResponse.Transaction;
161+
162+
// var previousTransactionOutputs = previousTransaction.Outputs;
163+
// foreach (TxOut previousTransactionOutput in previousTransactionOutputs)
164+
// {
165+
// Money amount = previousTransactionOutput.Value;
166+
167+
// Console.WriteLine(amount.ToDecimal(MoneyUnit.BTC));
168+
// var paymentScript = previousTransactionOutput.ScriptPubKey;
169+
// Console.WriteLine(paymentScript); // It's the ScriptPubKey
170+
// var address = paymentScript.GetDestinationAddress(Network.Main);
171+
// Console.WriteLine(address);
172+
// Console.WriteLine();
173+
// }
174+
//}
175+
176+
Console.ReadLine();
177+
}
178+
}
179+
}
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("Transaction")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Transaction")]
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("f50f451c-4d92-468a-bba2-1c18d3b9ea5f")]
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")]

Transaction/Transaction.csproj

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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>{F50F451C-4D92-468A-BBA2-1C18D3B9EA5F}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Transaction</RootNamespace>
11+
<AssemblyName>Transaction</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.8\lib\net45\Newtonsoft.Json.dll</HintPath>
46+
<Private>True</Private>
47+
</Reference>
48+
<Reference Include="QBitNinja.Client, Version=1.0.3.21, Culture=neutral, processorArchitecture=MSIL">
49+
<HintPath>..\packages\QBitNinja.Client.1.0.3.21\lib\net45\QBitNinja.Client.dll</HintPath>
50+
<Private>True</Private>
51+
</Reference>
52+
<Reference Include="System" />
53+
<Reference Include="System.Core" />
54+
<Reference Include="System.Xml.Linq" />
55+
<Reference Include="System.Data.DataSetExtensions" />
56+
<Reference Include="Microsoft.CSharp" />
57+
<Reference Include="System.Data" />
58+
<Reference Include="System.Net.Http" />
59+
<Reference Include="System.Xml" />
60+
</ItemGroup>
61+
<ItemGroup>
62+
<Compile Include="Program.cs" />
63+
<Compile Include="Properties\AssemblyInfo.cs" />
64+
</ItemGroup>
65+
<ItemGroup>
66+
<None Include="App.config" />
67+
<None Include="packages.config" />
68+
</ItemGroup>
69+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
70+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
71+
Other similar extension points exist, see Microsoft.Common.targets.
72+
<Target Name="BeforeBuild">
73+
</Target>
74+
<Target Name="AfterBuild">
75+
</Target>
76+
-->
77+
</Project>

Transaction/packages.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+
<packages>
3+
<package id="NBitcoin" version="3.0.0.30" targetFramework="net452" />
4+
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />
5+
<package id="QBitNinja.Client" version="1.0.3.21" targetFramework="net452" />
6+
</packages>

0 commit comments

Comments
 (0)