Skip to content

Commit 3d9cb6d

Browse files
committed
Fill out add deploy profile.
1 parent 638ebdb commit 3d9cb6d

File tree

4 files changed

+181
-73
lines changed

4 files changed

+181
-73
lines changed

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainExplorerToolWindowControl.xaml.cs

Lines changed: 152 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.IO;
3+
using System.Security.Cryptography;
24
using System.Linq;
35
using System.Numerics;
46
using System.Threading.Tasks;
@@ -19,6 +21,8 @@
1921
using static Stratis.DevEx.Result;
2022
using System.IO;
2123
using Stratis.DevEx;
24+
using System.Diagnostics.Eventing.Reader;
25+
using System.Text;
2226

2327
namespace Stratis.VS.StratisEVM.UI
2428
{
@@ -419,39 +423,7 @@ private void DeleteNetworkCmd_CanExecute(object sender, CanExecuteRoutedEventArg
419423
e.CanExecute = true;
420424
}
421425
}
422-
#endregion
423-
424-
#region Methods
425-
private BlockchainInfo GetSelectedItem(object sender)
426-
{
427-
var window = (BlockchainExplorerToolWindowControl)sender;
428-
var tree = window.BlockchainExplorerTree;
429-
return tree.SelectedItem;
430-
}
431-
private void ShowValidationErrors(Wpc.TextBlock textBlock, string message)
432-
{
433-
textBlock.Visibility = Visibility.Visible;
434-
textBlock.Text = message;
435-
}
436426

437-
private void ShowProgressRing(ProgressRing progressRing)
438-
{
439-
progressRing.IsEnabled = true;
440-
progressRing.Visibility = Visibility.Visible;
441-
}
442-
443-
private void HideProgressRing(ProgressRing progressRing)
444-
{
445-
progressRing.IsEnabled = false;
446-
progressRing.Visibility = Visibility.Hidden;
447-
}
448-
449-
#endregion
450-
451-
#region Fields
452-
internal BlockchainExplorerToolWindow window;
453-
454-
#endregion
455427

456428
private async void EditAccountCmd_Executed(object sender, ExecutedRoutedEventArgs e)
457429
{
@@ -472,7 +444,7 @@ private async void EditAccountCmd_Executed(object sender, ExecutedRoutedEventArg
472444
var acctpubkey = (Wpc.TextBlock)((StackPanel)sp.Children[0]).Children[1];
473445
acctpubkey.Text = item.Name;
474446
var acctlabel = (Wpc.TextBox)((StackPanel)sp.Children[0]).Children[3];
475-
acctlabel.Text = (string)item.Data["Label"];
447+
acctlabel.Text = (string)item.Data["Label"];
476448
var validForClose = false;
477449
dw.ButtonClicked += (cd, args) =>
478450
{
@@ -485,7 +457,7 @@ private async void EditAccountCmd_Executed(object sender, ExecutedRoutedEventArg
485457
var r = await dw.ShowAsync();
486458
if (r != ContentDialogResult.Primary)
487459
{
488-
acctlabel.Text = (string) item.Data["Label"];
460+
acctlabel.Text = (string)item.Data["Label"];
489461
return;
490462
}
491463
item.Data["Label"] = acctlabel.Text;
@@ -514,35 +486,155 @@ private async void EditAccountCmd_Executed(object sender, ExecutedRoutedEventArg
514486

515487
private async void NewDeployProfileCmd_Executed(object sender, ExecutedRoutedEventArgs e)
516488
{
517-
var window = (BlockchainExplorerToolWindowControl)sender;
518-
var tree = window.BlockchainExplorerTree;
519-
var item = GetSelectedItem(sender);
520-
var dw = new BlockchainExplorerDialog(RootContentDialog)
489+
int roundUp(int numToRound, int multiple)
521490
{
522-
Title = "Add Deploy Profile",
523-
PrimaryButtonIcon = new SymbolIcon(SymbolRegular.Save20),
524-
Content = (StackPanel)TryFindResource("AddDeployProfileDialog"),
525-
PrimaryButtonText = "Save",
526-
CloseButtonText = "Cancel",
527-
};
528-
var sp = (StackPanel)dw.Content;
529-
var name = (Wpc.TextBox)((StackPanel)sp.Children[0]).Children[1];
530-
var endpoint = (ComboBox)((StackPanel)sp.Children[1]).Children[1];
531-
var accounts = (ComboBox)((StackPanel)sp.Children[2]).Children[1];
532-
endpoint.ItemsSource = item.GetNetworkEndPoints();
533-
endpoint.SelectedIndex = 0;
534-
accounts.ItemsSource = item.GetNetworkAccounts();
535-
var validForClose = false;
536-
537-
dw.ButtonClicked += (cd, args) =>
491+
if (multiple == 0)
492+
return numToRound;
493+
494+
int remainder = numToRound % multiple;
495+
if (remainder == 0)
496+
return numToRound;
497+
498+
return numToRound + multiple - remainder;
499+
}
500+
501+
try
538502
{
539-
validForClose = true;
540-
};
541-
dw.Closing += (d, args) =>
503+
var window = (BlockchainExplorerToolWindowControl)sender;
504+
var tree = window.BlockchainExplorerTree;
505+
var item = GetSelectedItem(sender);
506+
var dw = new BlockchainExplorerDialog(RootContentDialog)
507+
{
508+
Title = "Add Deploy Profile",
509+
PrimaryButtonIcon = new SymbolIcon(SymbolRegular.Save20),
510+
Content = (StackPanel)TryFindResource("AddDeployProfileDialog"),
511+
PrimaryButtonText = "Save",
512+
CloseButtonText = "Cancel",
513+
};
514+
var sp = (StackPanel)dw.Content;
515+
var name = (Wpc.TextBox)((StackPanel)sp.Children[0]).Children[1];
516+
var endpoint = (ComboBox)((StackPanel)sp.Children[1]).Children[1];
517+
var accounts = (ComboBox)((StackPanel)sp.Children[2]).Children[1];
518+
var pkey = (Wpc.TextBox)((StackPanel)sp.Children[3]).Children[1];
519+
var errors = (Wpc.TextBlock)((StackPanel)sp.Children[4]).Children[0];
520+
endpoint.ItemsSource = item.GetNetworkEndPoints();
521+
endpoint.SelectedIndex = 0;
522+
accounts.ItemsSource = item.GetNetworkAccounts();
523+
var validForClose = false;
524+
525+
dw.ButtonClicked += (cd, args) =>
526+
{
527+
validForClose = false;
528+
errors.Visibility = Visibility.Hidden;
529+
530+
if (args.Button == ContentDialogButton.Primary)
531+
{
532+
if (!string.IsNullOrEmpty(name.Text) && accounts.SelectedValue != null && endpoint.SelectedValue != null)
533+
{
534+
var dp = item.GetNetworkDeployProfiles();
535+
if (dp.Contains(name.Text))
536+
{
537+
ShowValidationErrors(errors, "The " + name.Text + " deploy profile already exists.");
538+
}
539+
else
540+
{
541+
validForClose = true;
542+
}
543+
}
544+
else
545+
{
546+
ShowValidationErrors(errors, "Enter a deploy profile name and select a valid endpoint and account");
547+
}
548+
}
549+
else
550+
{
551+
validForClose = true;
552+
}
553+
};
554+
555+
dw.Closing += (d, args) =>
556+
{
557+
args.Cancel = !validForClose;
558+
};
559+
560+
var r = await dw.ShowAsync();
561+
if (r != ContentDialogResult.Primary)
562+
{
563+
name.Text = "";
564+
endpoint.ItemsSource = null;
565+
accounts.ItemsSource = null;
566+
return;
567+
}
568+
else if (!string.IsNullOrEmpty(pkey.Text))
569+
{
570+
byte[] pkeydata = UnicodeEncoding.ASCII.GetBytes(pkey.Text);
571+
var len = pkeydata.Length;
572+
var lb = BitConverter.GetBytes(len);
573+
var round = roundUp(pkeydata.Length, 16);
574+
Array.Resize(ref pkeydata, round);
575+
ProtectedMemory.Protect(pkeydata, MemoryProtectionScope.SameLogon);
576+
var encodeddata = lb.Concat(pkeydata).ToArray();
577+
item.AddDeployProfile(name.Text, (string)endpoint.SelectedValue, (string)accounts.SelectedValue, encodeddata);
578+
}
579+
else
580+
{
581+
item.AddDeployProfile(name.Text, (string)endpoint.SelectedValue, (string)accounts.SelectedValue);
582+
}
583+
if (!tree.RootItem.Save("BlockchainExplorerTree", out var ex))
584+
{
585+
#if IS_VSIX
586+
VSUtil.ShowModalErrorDialogBox("Error saving tree data: " + ex?.Message);
587+
#else
588+
System.Windows.MessageBox.Show("Error saving tree data: " + ex?.Message);
589+
#endif
590+
}
591+
else
592+
{
593+
tree.Refresh();
594+
}
595+
}
596+
catch (Exception ex)
542597
{
543-
args.Cancel = !validForClose;
544-
};
545-
var r = await dw.ShowAsync();
598+
#if IS_VSIX
599+
VSUtil.ShowModalErrorDialogBox(ex?.Message);
600+
#else
601+
System.Windows.MessageBox.Show(ex?.Message);
602+
#endif
603+
}
604+
}
605+
#endregion
606+
607+
#region Methods
608+
private BlockchainInfo GetSelectedItem(object sender)
609+
{
610+
var window = (BlockchainExplorerToolWindowControl)sender;
611+
var tree = window.BlockchainExplorerTree;
612+
return tree.SelectedItem;
613+
}
614+
private void ShowValidationErrors(Wpc.TextBlock textBlock, string message)
615+
{
616+
textBlock.Visibility = Visibility.Visible;
617+
textBlock.Text = message;
546618
}
619+
620+
private void ShowProgressRing(ProgressRing progressRing)
621+
{
622+
progressRing.IsEnabled = true;
623+
progressRing.Visibility = Visibility.Visible;
624+
}
625+
626+
private void HideProgressRing(ProgressRing progressRing)
627+
{
628+
progressRing.IsEnabled = false;
629+
progressRing.Visibility = Visibility.Hidden;
630+
}
631+
632+
#endregion
633+
634+
#region Fields
635+
internal BlockchainExplorerToolWindow window;
636+
637+
#endregion
638+
547639
}
548640
}

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainExplorerTreeResources.xaml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@
300300
<DataTrigger Binding="{Binding Path=Kind}" Value="Account">
301301
<Setter TargetName="NodeImage" Property="Source" Value="{StaticResource AccountDrawingImage}" />
302302
</DataTrigger>
303+
<DataTrigger Binding="{Binding Path=Kind}" Value="DeployProfile">
304+
<Setter TargetName="NodeImage" Property="Source" Value="{StaticResource DeployProfileDrawingImage}" />
305+
</DataTrigger>
303306
<MultiDataTrigger>
304307
<MultiDataTrigger.Conditions>
305308
<Condition Binding="{Binding Path=Kind}" Value="Network" />
@@ -450,16 +453,10 @@
450453
<!-- Dialog style -->
451454
<Style x:Key="DialogStyle" TargetType="{x:Type StackPanel}">
452455
<Style.Resources>
453-
<Style TargetType="{x:Type ui:TextBox}">
454-
<Setter Property="FontSize" Value="12" />
455-
</Style>
456456
<Style TargetType="{x:Type ui:NumberBox}">
457457
<Setter Property="FontSize" Value="12" />
458458
<Setter Property="HorizontalContentAlignment" Value="Right" />
459459
</Style>
460-
<Style TargetType="{x:Type ui:TextBlock}">
461-
<Setter Property="FontSize" Value="12" />
462-
</Style>
463460
</Style.Resources>
464461
</Style>
465462

@@ -517,26 +514,25 @@
517514

518515
<StackPanel x:Key="AddDeployProfileDialog" Orientation="Vertical" VerticalAlignment="Top" Style="{DynamicResource DialogStyle}">
519516
<StackPanel Orientation="Vertical" >
520-
<ui:TextBlock Text="Deploy Profile Name:" VerticalAlignment="Center" Margin="1,0,0,2" />
517+
<ui:TextBlock Text="Name:" VerticalAlignment="Center" Margin="1,0,0,2" />
521518
<ui:TextBox x:Name="DeployProfileNameTextBox" Width="150" Margin="0,2,10,4" VerticalAlignment="Center" />
522519
</StackPanel>
523520
<StackPanel Orientation="Vertical">
524-
<ui:TextBlock Text="Select the deployment endpoint:" VerticalAlignment="Center" Margin="1,0,0,2" />
521+
<ui:TextBlock Text="Endpoint:" VerticalAlignment="Center" Margin="1,0,0,2" />
525522
<ComboBox x:Name="DeployProfileEndpointListView" Width="150" Margin="0,2,10,4" VerticalAlignment="Center"/>
526523
</StackPanel>
527524
<StackPanel Orientation="Vertical">
528-
<ui:TextBlock Text="Select the account to use:" VerticalAlignment="Center" Margin="1,0,0,2" />
525+
<ui:TextBlock Text="Account:" VerticalAlignment="Center" Margin="1,0,0,2" />
529526
<ComboBox x:Name="DeployProfileAccountListView" Width="150" Margin="0,2,10,4" VerticalAlignment="Center"/>
530527
</StackPanel>
531528
<StackPanel Orientation="Vertical">
532-
<ui:TextBlock Text="Account private key (optional):" VerticalAlignment="Center" Margin="1,0,0,2" />
529+
<ui:TextBlock Text="Private Key (optional):" VerticalAlignment="Center" Margin="1,0,0,2" />
533530
<ui:PasswordBox Name="DeployProfileAccountPrivateKeyPasswordBox" Width="150" Margin="0,2,10,4" VerticalAlignment="Center"/>
534531
</StackPanel>
535532
<StackPanel Orientation="Vertical">
536-
<Grid>
537-
<ui:TextBlock Name = "AddDeployProfileDialogValidationErrors" Grid.Row="0" Foreground="Red" Visibility="Hidden" VerticalAlignment="Center" TextWrapping="WrapWithOverflow"
533+
<ui:TextBlock Name = "AddDeployProfileDialogValidationErrors" Grid.Row="0" Foreground="Red" Visibility="Hidden" VerticalAlignment="Center" TextWrapping="WrapWithOverflow"
538534
ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True"/>
539-
</Grid>
535+
540536
</StackPanel>
541537
</StackPanel>
542538
</ResourceDictionary>

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainViewModel.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Collections.Specialized;
55
using System.ComponentModel;
66
using System.IO;
7+
using System.Text;
8+
using System.Security.Cryptography;
79
using System.Linq;
810
using System.Numerics;
911
using System.Net.Http;
@@ -115,6 +117,21 @@ public BlockchainInfo AddAccount(string pubkey, string label = null)
115117
};
116118
return AddChild(BlockchainInfoKind.Account, pubkey, data);
117119
}
120+
121+
public BlockchainInfo AddDeployProfile(string name, string endpoint, string account, byte[] pkey = null)
122+
{
123+
var data = new Dictionary<string, object>()
124+
{
125+
{"EndPoint", endpoint},
126+
{"Account", account},
127+
};
128+
if (pkey != null)
129+
{
130+
data["PrivateKey"] = pkey;
131+
}
132+
return AddChild(BlockchainInfoKind.DeployProfile, name, data);
133+
}
134+
118135
public override int GetHashCode() => Key.GetHashCode();
119136

120137
public override bool Equals(object obj) => obj is BlockchainInfo bi ? Key == bi.Key : false;
@@ -133,6 +150,8 @@ public BlockchainInfo AddAccount(string pubkey, string label = null)
133150

134151
public IEnumerable<string> GetNetworkAccounts() => GetChild("Accounts", BlockchainInfoKind.Folder).GetChildren(BlockchainInfoKind.Account).Select(bi => bi.Name);
135152

153+
public IEnumerable<string> GetNetworkDeployProfiles() => GetChild("Deploy Profiles", BlockchainInfoKind.Folder).GetChildren(BlockchainInfoKind.DeployProfile).Select(bi => bi.Name);
154+
136155
public bool Save(string path, out Exception e)
137156
{
138157
try

tests/Stratis.DevEx.Wpf.Test/Stratis.DevEx.Wpf.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Reference Include="System" />
4545
<Reference Include="System.Data" />
4646
<Reference Include="System.Numerics" />
47+
<Reference Include="System.Security" />
4748
<Reference Include="System.Xml" />
4849
<Reference Include="Microsoft.CSharp" />
4950
<Reference Include="System.Core" />

0 commit comments

Comments
 (0)