Skip to content

Commit 7fd0c29

Browse files
Add hash collision script template
1 parent 6f377b1 commit 7fd0c29

File tree

6 files changed

+284
-4
lines changed

6 files changed

+284
-4
lines changed

BitcoinTransactionTool/BitcoinTransactionTool.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
<Compile Include="ViewModels\QrViewModel.cs" />
181181
<Compile Include="ViewModels\ScrAddressViewModel.cs" />
182182
<Compile Include="ViewModels\ScrArbitraryDataViewModel.cs" />
183+
<Compile Include="ViewModels\ScrHashCollisionViewModel.cs" />
183184
<Compile Include="ViewModels\ScriptViewModel.cs" />
184185
<Compile Include="ViewModels\ScriptVMBase.cs" />
185186
<Compile Include="ViewModels\ScrMultiSigViewModel.cs" />
@@ -194,6 +195,9 @@
194195
<Compile Include="Views\ScrArbitraryDataView.xaml.cs">
195196
<DependentUpon>ScrArbitraryDataView.xaml</DependentUpon>
196197
</Compile>
198+
<Compile Include="Views\ScrHashCollisionView.xaml.cs">
199+
<DependentUpon>ScrHashCollisionView.xaml</DependentUpon>
200+
</Compile>
197201
<Compile Include="Views\ScriptView.xaml.cs">
198202
<DependentUpon>ScriptView.xaml</DependentUpon>
199203
</Compile>
@@ -230,6 +234,10 @@
230234
<SubType>Designer</SubType>
231235
<Generator>MSBuild:Compile</Generator>
232236
</Page>
237+
<Page Include="Views\ScrHashCollisionView.xaml">
238+
<SubType>Designer</SubType>
239+
<Generator>MSBuild:Compile</Generator>
240+
</Page>
233241
<Page Include="Views\ScriptView.xaml">
234242
<SubType>Designer</SubType>
235243
<Generator>MSBuild:Compile</Generator>
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// Bitcoin Transaction Tool
2+
// Copyright (c) 2017 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using BitcoinTransactionTool.Backend.Blockchain.Scripts;
7+
using BitcoinTransactionTool.Backend.Blockchain.Scripts.Operations;
8+
using BitcoinTransactionTool.Backend.Encoders;
9+
using BitcoinTransactionTool.Backend.MVVM;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
14+
namespace BitcoinTransactionTool.ViewModels
15+
{
16+
public class ScrHashCollisionViewModel : ScriptVMBase
17+
{
18+
public ScrHashCollisionViewModel()
19+
{
20+
VmName = "Hash collision puzzles";
21+
Description = "Build complete signature script for claiming the hash collision puzzles. " +
22+
"More information could be found at https://bitcointalk.org/index.php?topic=293382.0 ";
23+
24+
PuzzleList = Enum.GetValues(typeof(PuzzleTypes)).Cast<PuzzleTypes>();
25+
}
26+
27+
28+
29+
public enum PuzzleTypes
30+
{
31+
SHA1,
32+
SHA256,
33+
RIPEMD160,
34+
RIPEMD160_SHA256,
35+
SHA256_double,
36+
Absolute
37+
}
38+
39+
public IEnumerable<PuzzleTypes> PuzzleList { get; private set; }
40+
41+
private PuzzleTypes _selPuz;
42+
public PuzzleTypes SelectedPuzzle
43+
{
44+
get => _selPuz;
45+
set => SetField(ref _selPuz, value);
46+
}
47+
48+
private bool _isRun;
49+
public bool MakeRunnable
50+
{
51+
get => _isRun;
52+
set => SetField(ref _isRun, value);
53+
}
54+
55+
public string MakeRunnableToolTip =>
56+
"If checked, the redeem script part will be pulled out of the signature script " +
57+
"so that it could be run here." + Environment.NewLine +
58+
"When unchecked, the result is a valid signature script.";
59+
60+
61+
private string _d1;
62+
public string Data1
63+
{
64+
get => _d1;
65+
set => SetField(ref _d1, value);
66+
}
67+
68+
private string _d2;
69+
public string Data2
70+
{
71+
get => _d2;
72+
set => SetField(ref _d2, value);
73+
}
74+
75+
76+
public string Example1ToolTip =>
77+
"SHA1 hash collision example found in txid=8d31992805518fd62daa3bdd2a5c4fd2cd3054c9b3dca1d78055e9528cff6adc";
78+
public string Example2ToolTip => "Absolute valueS of unequal numbers i and -i are equal!";
79+
80+
81+
public RelayCommand Example1Command => new RelayCommand(Example1);
82+
private void Example1()
83+
{
84+
SelectedPuzzle = PuzzleTypes.SHA1;
85+
Data1 = "255044462d312e330a25e2e3cfd30a0a0a312030206f626a0a3c3c2f57696474682032203020522f4865696768742033203020522f547970652034203020522f537562747970652035203020522f46696c7465722036203020522f436f6c6f7253706163652037203020522f4c656e6774682038203020522f42697473506572436f6d706f6e656e7420383e3e0a73747265616d0affd8fffe00245348412d3120697320646561642121212121852fec092339759c39b1a1c63c4c97e1fffe017f46dc93a6b67e013b029aaa1db2560b45ca67d688c7f84b8c4c791fe02b3df614f86db1690901c56b45c1530afedfb76038e972722fe7ad728f0e4904e046c230570fe9d41398abe12ef5bc942be33542a4802d98b5d70f2a332ec37fac3514e74ddc0f2cc1a874cd0c78305a21566461309789606bd0bf3f98cda8044629a1";
86+
Data2 = "255044462d312e330a25e2e3cfd30a0a0a312030206f626a0a3c3c2f57696474682032203020522f4865696768742033203020522f547970652034203020522f537562747970652035203020522f46696c7465722036203020522f436f6c6f7253706163652037203020522f4c656e6774682038203020522f42697473506572436f6d706f6e656e7420383e3e0a73747265616d0affd8fffe00245348412d3120697320646561642121212121852fec092339759c39b1a1c63c4c97e1fffe017346dc9166b67e118f029ab621b2560ff9ca67cca8c7f85ba84c79030c2b3de218f86db3a90901d5df45c14f26fedfb3dc38e96ac22fe7bd728f0e45bce046d23c570feb141398bb552ef5a0a82be331fea48037b8b5d71f0e332edf93ac3500eb4ddc0decc1a864790c782c76215660dd309791d06bd0af3f98cda4bc4629b1";
87+
}
88+
89+
public RelayCommand Example2Command => new RelayCommand(Example2);
90+
private void Example2()
91+
{
92+
SelectedPuzzle = PuzzleTypes.Absolute;
93+
Random rand = new Random();
94+
int i = rand.Next();
95+
Data1 = i.ToString();
96+
Data2 = (-i).ToString();
97+
}
98+
99+
public override bool SetOperations()
100+
{
101+
Errors = null;
102+
103+
PushDataOp push1, push2;
104+
IOperation op = null;
105+
if (SelectedPuzzle == PuzzleTypes.Absolute)
106+
{
107+
if (!int.TryParse(Data1, out int a) || !int.TryParse(Data2, out int b))
108+
{
109+
Errors = "Data inputs for absolute puzzle must be a number.";
110+
return false;
111+
}
112+
else
113+
{
114+
push1 = new PushDataOp(a);
115+
push2 = new PushDataOp(b);
116+
op = new ABSOp();
117+
}
118+
}
119+
else if (!Base16.IsValid(Data1) || !Base16.IsValid(Data2))
120+
{
121+
Errors = "Data inputs for hash puzzles must be using base-16 encoding.";
122+
return false;
123+
}
124+
else
125+
{
126+
byte[] ba1 = Base16.ToByteArray(Data1);
127+
byte[] ba2 = Base16.ToByteArray(Data2);
128+
129+
push1 = new PushDataOp(ba1);
130+
push2 = new PushDataOp(ba2);
131+
132+
switch (SelectedPuzzle)
133+
{
134+
case PuzzleTypes.SHA1:
135+
op = new Sha1Op();
136+
break;
137+
case PuzzleTypes.SHA256:
138+
op = new Sha256Op();
139+
break;
140+
case PuzzleTypes.RIPEMD160:
141+
op = new RipeMd160Op();
142+
break;
143+
case PuzzleTypes.RIPEMD160_SHA256:
144+
new Hash160Op();
145+
break;
146+
case PuzzleTypes.SHA256_double:
147+
op = new Hash256Op();
148+
break;
149+
default:
150+
Errors = "Puzzle type was not defined.";
151+
return false;
152+
}
153+
}
154+
155+
if (MakeRunnable)
156+
{
157+
OpsToAdd = new IOperation[]
158+
{
159+
push1,
160+
push2,
161+
new DUP2Op(),
162+
new EqualOp(),
163+
new NOTOp(),
164+
new VerifyOp(),
165+
op,
166+
new SWAPOp(),
167+
op,
168+
new EqualOp()
169+
};
170+
}
171+
else
172+
{
173+
RedeemScript rdm = new RedeemScript()
174+
{
175+
OperationList = new IOperation[]
176+
{
177+
new DUP2Op(),
178+
new EqualOp(),
179+
new NOTOp(),
180+
new VerifyOp(),
181+
op,
182+
new SWAPOp(),
183+
op,
184+
new EqualOp()
185+
}
186+
};
187+
188+
OpsToAdd = new IOperation[]
189+
{
190+
push1,
191+
push2,
192+
new PushDataOp(rdm)
193+
};
194+
}
195+
196+
197+
return true;
198+
}
199+
}
200+
}

BitcoinTransactionTool/ViewModels/ScriptViewModel.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public ScriptViewModel()
3030
{
3131
new ScrAddressViewModel(),
3232
new ScrArbitraryDataViewModel(),
33-
new ScrMultiSigViewModel()
33+
new ScrMultiSigViewModel(),
34+
new ScrHashCollisionViewModel()
3435
};
3536
SelectedCustomScript = ScriptTemplateList.First();
3637

@@ -311,9 +312,13 @@ private void Run()
311312
success = false;
312313
break;
313314
}
314-
StackItems = new ObservableCollection<string>(opdt.Peek(opdt.ItemCount)
315-
.Select(x => (x.Length == 0) ? "(Empty array)" : x.ToBase16())
316-
.Reverse());
315+
StackItems = new ObservableCollection<string>(
316+
opdt.Peek(opdt.ItemCount)
317+
.Select(x =>
318+
(x.Length == 0) ? "(Empty array = False)" :
319+
(x.Length == 1 && x[0] == 1) ? "01 (True)" :
320+
x.ToBase16())
321+
.Reverse());
317322
}
318323

319324
RunResult = success ?
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<UserControl x:Class="BitcoinTransactionTool.Views.ScrHashCollisionView"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:vm="clr-namespace:BitcoinTransactionTool.ViewModels"
7+
mc:Ignorable="d"
8+
d:DataContext="{d:DesignInstance Type=vm:ScrHashCollisionViewModel, IsDesignTimeCreatable=True}"
9+
d:DesignHeight="150" d:DesignWidth="400">
10+
<Grid>
11+
<Grid.ColumnDefinitions>
12+
<ColumnDefinition Width="auto"/>
13+
<ColumnDefinition/>
14+
<ColumnDefinition Width="auto"/>
15+
</Grid.ColumnDefinitions>
16+
<Grid.RowDefinitions>
17+
<RowDefinition Height="auto"/>
18+
<RowDefinition Height="auto"/>
19+
<RowDefinition Height="auto"/>
20+
</Grid.RowDefinitions>
21+
22+
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="0">
23+
<Button Content="Example 1" Command="{Binding Example1Command}" ToolTip="{Binding Example1ToolTip}"
24+
Margin="3" Height="35"/>
25+
<Button Content="Example 2" Command="{Binding Example2Command}" ToolTip="{Binding Example2ToolTip}"
26+
Margin="3"/>
27+
<CheckBox Content="Make runnable" IsChecked="{Binding MakeRunnable}" ToolTip="{Binding MakeRunnableToolTip}"
28+
VerticalAlignment="Center" Margin="10"/>
29+
</StackPanel>
30+
31+
<TextBlock Text="Data 1: " FontFamily="Comic Sans MS" FontWeight="Bold" VerticalAlignment="Center"
32+
Grid.Column="0" Grid.Row="1"/>
33+
<TextBox Text="{Binding Data1}" VerticalAlignment="Top" TextWrapping="Wrap" Height="45"
34+
Margin="3" Grid.Column="1" Grid.Row="1"/>
35+
36+
<TextBlock Text="Data 2: " FontFamily="Comic Sans MS" FontWeight="Bold" VerticalAlignment="Center"
37+
Grid.Column="0" Grid.Row="2"/>
38+
<TextBox Text="{Binding Data2}" VerticalAlignment="Top" TextWrapping="Wrap" Height="45"
39+
Margin="3" Grid.Column="1" Grid.Row="2"/>
40+
41+
<ListBox ItemsSource="{Binding PuzzleList}" SelectedItem="{Binding SelectedPuzzle}"
42+
Style="{StaticResource RadioButtonListBoxStyle}" Grid.Column="2" Grid.RowSpan="3"/>
43+
</Grid>
44+
</UserControl>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Bitcoin Transaction Tool
2+
// Copyright (c) 2017 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using System.Windows.Controls;
7+
8+
namespace BitcoinTransactionTool.Views
9+
{
10+
/// <summary>
11+
/// Interaction logic for ScrHashCollisionView.xaml
12+
/// </summary>
13+
public partial class ScrHashCollisionView : UserControl
14+
{
15+
public ScrHashCollisionView()
16+
{
17+
InitializeComponent();
18+
}
19+
}
20+
}

BitcoinTransactionTool/Views/ScriptView.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<DataTemplate DataType="{x:Type vm:ScrMultiSigViewModel}">
1919
<v:ScrMultiSigView/>
2020
</DataTemplate>
21+
<DataTemplate DataType="{x:Type vm:ScrHashCollisionViewModel}">
22+
<v:ScrHashCollisionView/>
23+
</DataTemplate>
2124
</UserControl.Resources>
2225

2326
<Grid Margin="3">

0 commit comments

Comments
 (0)