1
1
using System ;
2
+ using System . IO ;
3
+ using System . Security . Cryptography ;
2
4
using System . Linq ;
3
5
using System . Numerics ;
4
6
using System . Threading . Tasks ;
19
21
using static Stratis . DevEx . Result ;
20
22
using System . IO ;
21
23
using Stratis . DevEx ;
24
+ using System . Diagnostics . Eventing . Reader ;
25
+ using System . Text ;
22
26
23
27
namespace Stratis . VS . StratisEVM . UI
24
28
{
@@ -419,39 +423,7 @@ private void DeleteNetworkCmd_CanExecute(object sender, CanExecuteRoutedEventArg
419
423
e . CanExecute = true ;
420
424
}
421
425
}
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
- }
436
426
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
455
427
456
428
private async void EditAccountCmd_Executed ( object sender , ExecutedRoutedEventArgs e )
457
429
{
@@ -472,7 +444,7 @@ private async void EditAccountCmd_Executed(object sender, ExecutedRoutedEventArg
472
444
var acctpubkey = ( Wpc . TextBlock ) ( ( StackPanel ) sp . Children [ 0 ] ) . Children [ 1 ] ;
473
445
acctpubkey . Text = item . Name ;
474
446
var acctlabel = ( Wpc . TextBox ) ( ( StackPanel ) sp . Children [ 0 ] ) . Children [ 3 ] ;
475
- acctlabel . Text = ( string ) item . Data [ "Label" ] ;
447
+ acctlabel . Text = ( string ) item . Data [ "Label" ] ;
476
448
var validForClose = false ;
477
449
dw . ButtonClicked += ( cd , args ) =>
478
450
{
@@ -485,7 +457,7 @@ private async void EditAccountCmd_Executed(object sender, ExecutedRoutedEventArg
485
457
var r = await dw . ShowAsync ( ) ;
486
458
if ( r != ContentDialogResult . Primary )
487
459
{
488
- acctlabel . Text = ( string ) item . Data [ "Label" ] ;
460
+ acctlabel . Text = ( string ) item . Data [ "Label" ] ;
489
461
return ;
490
462
}
491
463
item . Data [ "Label" ] = acctlabel . Text ;
@@ -514,35 +486,155 @@ private async void EditAccountCmd_Executed(object sender, ExecutedRoutedEventArg
514
486
515
487
private async void NewDeployProfileCmd_Executed ( object sender , ExecutedRoutedEventArgs e )
516
488
{
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 )
521
490
{
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
538
502
{
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 )
542
597
{
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 ;
546
618
}
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
+
547
639
}
548
640
}
0 commit comments