Browse Source

fixed bugs in experimental gui operations.

Ugochukwu Mmaduekwe 6 years ago
parent
commit
2288d302ab

+ 2 - 2
src/gui-experimental/core.utils/UCoreObjects.pas

@@ -24,11 +24,11 @@ type
 
   { TExecuteOperationType }
 
-  TExecuteOperationType = (omtAccount, omtSendPasc, omtChangeKey, omtTransferAccount, omtChangeAccountPrivateKey, omtAddKey, omtEnlistAccountForSale, omtDelistAccountFromSale);
+  TExecuteOperationType = (omtAccount, omtSendPasc, omtChangeKey, omtTransferAccount, omtChangeAccountPrivateKey, omtAddKey, omtEnlistAccountForSale, omtDelistAccountFromSale, omtChangeInfo);
 
   { TPayloadEncryptionMode }
 
-  TPayloadEncryptionMode = (pemEncryptWithSender, pemEncryptWithReceiver, pemEncryptWithPassword, pemNotEncrypt);
+  TPayloadEncryptionMode = (pemEncryptWithSender, pemEncryptWithRecipient, pemEncryptWithPassword, pemNotEncrypt);
 
   { TOperationSigningMode }
 

+ 70 - 73
src/gui-experimental/core.utils/UCoreUtils.pas

@@ -111,8 +111,9 @@ type
 
   TWIZOperationsHelper = class
   private
+    class function IsOwnerOfWallet(AAccount: TAccount; AWalletKeys: TWalletKeys; out AWalletKey: TWalletKey; var AErrorMessage: string): boolean; static;
     class function ValidateOperationsInput(const ASelectedAccounts: TArray<TAccount>; AWalletKeys: TWalletKeys; ANode: TNode; var AErrorMessage: string): boolean; static;
-    class function UpdatePayload(const ASenderPublicKey, ADestinationPublicKey: TAccountKey; const APayloadEncryptionMode: TPayloadEncryptionMode; const APayloadContent: string; var AEncodedPayloadBytes: TRawBytes; const APayloadEncryptionPassword: string; var AErrorMessage: string): boolean;
+    class function UpdatePayload(const ASenderPublicKey, ADestinationPublicKey: TAccountKey; const APayloadEncryptionMode: TPayloadEncryptionMode; const APayloadContent: string; var AEncodedPayloadBytes: TRawBytes; const APayloadEncryptionPassword: string; var AErrorMessage: string): boolean; static;
     class function SendPASCFinalizeAndDisplayMessage(const AOperationsTxt, AOperationToString: string; ANoOfOperations: integer; ATotalAmount, ATotalFee: int64; AOperationsHashTree: TOperationsHashTree; var AErrorMessage: string): boolean; static;
     class function OthersFinalizeAndDisplayMessage(const AOperationsTxt, AOperationToString: string; ANoOfOperations: integer; ATotalFee: int64; AOperationsHashTree: TOperationsHashTree; var AErrorMessage: string): boolean; static;
   public
@@ -674,6 +675,29 @@ begin
   end;
 end;
 
+class function TWIZOperationsHelper.IsOwnerOfWallet(AAccount: TAccount; AWalletKeys: TWalletKeys; out AWalletKey: TWalletKey; var AErrorMessage: string): boolean;
+var
+  LIdx: Int32;
+begin
+  Result := True;
+  LIdx := AWalletKeys.IndexOfAccountKey(AAccount.accountInfo.accountKey);
+  if LIdx < 0 then
+  begin
+    AErrorMessage := Format('Selected Account "%s" Private Key Not Found In Wallet', [AAccount.AccountString]);
+    Exit(False);
+  end;
+  AWalletKey := AWalletKeys.Key[LIdx];
+
+  if not Assigned(AWalletKey.PrivateKey) then
+  begin
+    if AWalletKey.HasPrivateKey then
+      AErrorMessage := 'Wallet is Password Protected. Please Unlock Before You Proceed.'
+    else
+      AErrorMessage := Format('Only Public Key of Account %s Was Found in Wallet. You Cannot Operate This Account', [AAccount.AccountString]);
+    Exit(False);
+  end;
+end;
+
 class function TWIZOperationsHelper.SendPASCFinalizeAndDisplayMessage(const AOperationsTxt, AOperationToString: string; ANoOfOperations: integer; ATotalAmount, ATotalFee: int64; AOperationsHashTree: TOperationsHashTree; var AErrorMessage: string): boolean;
 var
   LAuxs, LOperationsTxt: string;
@@ -749,7 +773,7 @@ begin
         LValid := AEncodedPayloadBytes <> '';
       end;
 
-      pemEncryptWithReceiver:
+      pemEncryptWithRecipient:
       begin
         // With destination public key
         AEncodedPayloadBytes := ECIESEncrypt(ADestinationPublicKey, APayloadContent);
@@ -856,7 +880,7 @@ var
   LTotalAmount, LTotalSignerFee, LAmount, LFee: int64;
   LDoOperation: boolean;
   LOperationsTxt, LOperationToString, LTemp: string;
-  LIdx, LAccountIdx, LNoOfOperations: integer;
+  LAccountIdx, LNoOfOperations: integer;
   LCurrentAccount: TAccount;
   LPayloadEncodedBytes: TRawBytes;
 begin
@@ -879,6 +903,9 @@ begin
       LPCOperation := nil; // reset LPCOperation to Nil
       LCurrentAccount := ASelectedAccounts[LAccountIdx];
 
+    if not TWIZOperationsHelper.IsOwnerOfWallet(LCurrentAccount, LWalletKeys, LWalletKey, AErrorMessage) then
+       Exit(False);
+
       if LCurrentAccount.account = ADestinationAccount.account then
       begin
         AErrorMessage := Format('Sender "%s" And Destination "%s" Accounts Are The Same', [LCurrentAccount.AccountString, ADestinationAccount.AccountString]);
@@ -891,23 +918,6 @@ begin
         Exit(False);
       end;
 
-      LIdx := LWalletKeys.IndexOfAccountKey(LCurrentAccount.accountInfo.accountKey);
-      if LIdx < 0 then
-      begin
-        AErrorMessage := Format('Selected Account "%s" Private Key Not Found In Wallet', [LCurrentAccount.AccountString]);
-        Exit(False);
-      end;
-      LWalletKey := LWalletKeys.Key[LIdx];
-
-      if not Assigned(LWalletKey.PrivateKey) then
-      begin
-        if LWalletKey.HasPrivateKey then
-          AErrorMessage := 'Wallet is Password Protected. Please Unlock Before You Proceed.'
-        else
-          AErrorMessage := Format('Only Public Key of Account %s Was Found in Wallet. You Cannot Operate This Account', [LCurrentAccount.AccountString]);
-        Exit(False);
-      end;
-
       LDoOperation := True;
 
       if LCurrentAccount.balance > 0 then
@@ -983,7 +993,7 @@ var
   LTotalSignerFee, LFee: int64;
   LIsV2: boolean;
   LOperationsTxt, LOperationToString, LTemp: string;
-  LIdx, LAccountIdx, LNoOfOperations: integer;
+  LAccountIdx, LNoOfOperations: integer;
   LCurrentAccount, LSignerAccount: TAccount;
   LPayloadEncodedBytes: TRawBytes;
 label
@@ -1006,9 +1016,12 @@ begin
     for LAccountIdx := Low(ASelectedAccounts) to High(ASelectedAccounts) do
     begin
       loop_start:
-        LPCOperation := nil; // reset LPCOperation to Nil
+      LPCOperation := nil; // reset LPCOperation to Nil
       LCurrentAccount := ASelectedAccounts[LAccountIdx];
 
+    if not TWIZOperationsHelper.IsOwnerOfWallet(LCurrentAccount, LWalletKeys, LWalletKey, AErrorMessage) then
+       Exit(False);
+
       if (TAccountComp.EqualAccountKeys(LCurrentAccount.accountInfo.accountKey,
         APublicKey)) then
       begin
@@ -1016,7 +1029,7 @@ begin
         Exit(False);
       end;
 
-      if LNode.Bank.SafeBox.CurrentProtocol >= 1 then
+      if LNode.Bank.SafeBox.CurrentProtocol >= CT_PROTOCOL_1 then
       begin
         // Signer:
         LSignerAccount := ASignerAccount;
@@ -1042,23 +1055,6 @@ begin
         Exit(False);
       end;
 
-      LIdx := LWalletKeys.IndexOfAccountKey(LCurrentAccount.accountInfo.accountKey);
-      if LIdx < 0 then
-      begin
-        AErrorMessage := Format('Selected Account "%s" Private Key Not Found In Wallet', [LCurrentAccount.AccountString]);
-        Exit(False);
-      end;
-      LWalletKey := LWalletKeys.Key[LIdx];
-
-      if not Assigned(LWalletKey.PrivateKey) then
-      begin
-        if LWalletKey.HasPrivateKey then
-          AErrorMessage := 'Wallet Is Password Protected. Please Unlock Before You Proceed.'
-        else
-          AErrorMessage := Format('Only Public Key of Account %s Was Found In Wallet. You Cannot Operate This Account', [LCurrentAccount.AccountString]);
-        Exit(False);
-      end;
-
       if LIsV2 then
       begin
         // must ensure is Signer account last if included in sender accounts (not necessarily ordered enumeration)
@@ -1071,7 +1067,7 @@ begin
         end;
 
         // Maintain correct signer fee distribution
-        if Uint64(LTotalSignerFee) >= LSignerAccount.balance then
+        if UInt64(LTotalSignerFee) >= LSignerAccount.balance then
           LFee := 0
         else if LSignerAccount.balance - uint64(LTotalSignerFee) >
           UInt64(AFee) then
@@ -1130,7 +1126,7 @@ var
   LOperationsHashTree: TOperationsHashTree;
   LTotalSignerFee, LFee: int64;
   LOperationsTxt, LOperationToString, LTemp: string;
-  LIdx, LAccountIdx, LNoOfOperations: integer;
+  LAccountIdx, LNoOfOperations: integer;
   LCurrentAccount, LSignerAccount: TAccount;
   LPayloadEncodedBytes: TRawBytes;
 begin
@@ -1153,6 +1149,9 @@ begin
       LPCOperation := nil; // reset LPCOperation to Nil
       LCurrentAccount := ASelectedAccounts[LAccountIdx];
 
+      if not TWIZOperationsHelper.IsOwnerOfWallet(LCurrentAccount, LWalletKeys, LWalletKey, AErrorMessage) then
+         Exit(False);
+
       if TAccountComp.IsAccountForSale(LCurrentAccount.accountInfo) then
       begin
         AErrorMessage := Format('Account "%s" Is Already Enlisted For Sale', [LCurrentAccount.AccountString]);
@@ -1177,31 +1176,20 @@ begin
 
         if ALockedUntilBlock = 0 then
         begin
-          AErrorMessage := 'You Didn''t Insert a Locking Block.';
+          AErrorMessage := 'You Didn''t Insert a Valid Locking Block.';
           Exit(False);
         end;
       end;
 
-      if not UpdatePayload(LCurrentAccount.accountInfo.accountKey, ASignerAccount.accountInfo.accountKey, APayloadEncryptionMode, APayloadContent, LPayloadEncodedBytes, APayloadEncryptionPassword, AErrorMessage) then
-      begin
-        AErrorMessage := Format('Error Encoding Payload Of Selected Account "%s. ", Specific Error Is "%s"', [LCurrentAccount.AccountString, AErrorMessage]);
-        Exit(False);
-      end;
-
-      LIdx := LWalletKeys.IndexOfAccountKey(LCurrentAccount.accountInfo.accountKey);
-      if LIdx < 0 then
+      if (LNode.Bank.SafeBox.CurrentProtocol = CT_PROTOCOL_1) then
       begin
-        AErrorMessage := Format('Selected Account "%s" Private Key Not Found In Wallet', [LCurrentAccount.AccountString]);
-        Exit(False);
+          AErrorMessage := 'This Operation Needs PROTOCOL 2 Active';
+          Exit(False);
       end;
-      LWalletKey := LWalletKeys.Key[LIdx];
 
-      if not Assigned(LWalletKey.PrivateKey) then
+      if not UpdatePayload(LCurrentAccount.accountInfo.accountKey, ASignerAccount.accountInfo.accountKey, APayloadEncryptionMode, APayloadContent, LPayloadEncodedBytes, APayloadEncryptionPassword, AErrorMessage) then
       begin
-        if LWalletKey.HasPrivateKey then
-          AErrorMessage := 'Wallet Is Password Protected. Please Unlock Before You Proceed.'
-        else
-          AErrorMessage := Format('Only Public Key Of Account %s Was Found In Wallet. You Cannot Operate This Account', [LCurrentAccount.AccountString]);
+        AErrorMessage := Format('Error Encoding Payload Of Selected Account "%s. ", Specific Error Is "%s"', [LCurrentAccount.AccountString, AErrorMessage]);
         Exit(False);
       end;
 
@@ -1272,7 +1260,7 @@ var
   LOperationsHashTree: TOperationsHashTree;
   LTotalSignerFee, LFee: int64;
   LOperationsTxt, LOperationToString, LTemp: string;
-  LIdx, LAccountIdx, LNoOfOperations: integer;
+  LAccountIdx, LNoOfOperations: integer;
   LCurrentAccount, LSignerAccount: TAccount;
   LPayloadEncodedBytes: TRawBytes;
 begin
@@ -1295,33 +1283,42 @@ begin
       LPCOperation := nil; // reset LPCOperation to Nil
       LCurrentAccount := ASelectedAccounts[LAccountIdx];
 
+      if not TWIZOperationsHelper.IsOwnerOfWallet(LCurrentAccount, LWalletKeys, LWalletKey, AErrorMessage) then
+         Exit(False);
+
       if not TAccountComp.IsAccountForSale(LCurrentAccount.accountInfo) then
       begin
         AErrorMessage := Format('Account "%s" is not enlisted for sale so cannot be delisted', [LCurrentAccount.AccountString]);
         Exit(False);
       end;
 
+      if (TAccountComp.IsAccountLocked(LCurrentAccount.accountInfo, LNode.Bank.BlocksCount)) then
+      begin
+          AErrorMessage := Format('Target Account "%s"  Is Locked Until Block %u', [LCurrentAccount.AccountString, LCurrentAccount.accountInfo.locked_until_block]);
+          Exit(False);
+      end;
+
+      if (TAccountComp.IsAccountLocked(LSignerAccount.accountInfo, LNode.Bank.BlocksCount)) then
+      begin
+          AErrorMessage := Format('Signer Account "%s"  Is Locked Until Block %u', [LSignerAccount.AccountString, LSignerAccount.accountInfo.locked_until_block]);
+          Exit(False);
+      end;
 
-      if not UpdatePayload(LCurrentAccount.accountInfo.accountKey, ASignerAccount.accountInfo.accountKey, APayloadEncryptionMode, APayloadContent, LPayloadEncodedBytes, APayloadEncryptionPassword, AErrorMessage) then
+      if (not TAccountComp.EqualAccountKeys(LSignerAccount.accountInfo.accountKey, LCurrentAccount.accountInfo.accountKey)) then
       begin
-        AErrorMessage := Format('Error Encoding Payload Of Selected Account "%s. ", Specific Error Is "%s"', [LCurrentAccount.AccountString, AErrorMessage]);
-        Exit(False);
+          AErrorMessage := Format('Signer Account %s Is Not The Owner Of Delisted Account %s', [LSignerAccount.AccountString, LCurrentAccount.AccountString]);
+          Exit(False);
       end;
 
-      LIdx := LWalletKeys.IndexOfAccountKey(LCurrentAccount.accountInfo.accountKey);
-      if LIdx < 0 then
+      if (LNode.Bank.SafeBox.CurrentProtocol = CT_PROTOCOL_1) then
       begin
-        AErrorMessage := Format('Selected Account "%s" Private Key Not Found In Wallet', [LCurrentAccount.AccountString]);
-        Exit(False);
+          AErrorMessage := 'This Operation Needs PROTOCOL 2 Active';
+          Exit(False);
       end;
-      LWalletKey := LWalletKeys.Key[LIdx];
 
-      if not Assigned(LWalletKey.PrivateKey) then
+      if not UpdatePayload(LCurrentAccount.accountInfo.accountKey, ASignerAccount.accountInfo.accountKey, APayloadEncryptionMode, APayloadContent, LPayloadEncodedBytes, APayloadEncryptionPassword, AErrorMessage) then
       begin
-        if LWalletKey.HasPrivateKey then
-          AErrorMessage := 'Wallet Is Password Protected. Please Unlock Before You Proceed.'
-        else
-          AErrorMessage := Format('Only Public Key Of Account %s Was Found In Wallet. You Cannot Operate This Account', [LCurrentAccount.AccountString]);
+        AErrorMessage := Format('Error Encoding Payload Of Selected Account "%s. ", Specific Error Is "%s"', [LCurrentAccount.AccountString, AErrorMessage]);
         Exit(False);
       end;
 

+ 18 - 16
src/gui-experimental/wizards/operations/UWIZOperationConfirmation.pas

@@ -91,6 +91,10 @@ var
   LCaption: string;
 begin
 
+  lblBeneficiaryAccount.Visible := False;
+  lblBenAcc.Visible := False;
+  lblSgnAcc.Caption := Model.Signer.SignerAccount.AccountString;
+
   FConfirmationGrid := TVisualGrid.Create(Self);
   FConfirmationGrid.CanSearch := False;
   FConfirmationGrid.SortMode := smMultiColumn;
@@ -149,19 +153,17 @@ begin
   LData.Model := Model;
   FConfirmationGrid.DataSource := LData;
   paGrid.AddControlDockCenter(FConfirmationGrid);
-  lblSgnAcc.Caption := TAccountComp.AccountNumberToAccountTxtNumber(Model.Signer.SignerAccount.account);
-  if not (Model.ExecuteOperationType = omtEnlistAccountForSale) then
-  begin
-    lblBeneficiaryAccount.Visible := False;
-    lblBenAcc.Visible := False;
-  end
-  else
-  begin
-    lblBeneficiaryAccount.Visible := True;
-    lblBenAcc.Visible := True;
-    lblBenAcc.Caption := Model.EnlistAccountForSale.SellerAccount.AccountString;
-  end;
 
+  case Model.ExecuteOperationType of
+    omtChangeInfo:
+      lblSgnAcc.Caption := IIF(Length(Model.Account.SelectedAccounts) > 1, '**Signer Account *is* Processed Account**', lblSgnAcc.Caption);
+    omtEnlistAccountForSale:
+    begin
+      lblBeneficiaryAccount.Visible := True;
+      lblBenAcc.Visible := True;
+      lblBenAcc.Caption := Model.EnlistAccountForSale.SellerAccount.AccountString;
+    end;
+  end;
 
   if not Model.Payload.HasPayload then
   begin
@@ -183,7 +185,8 @@ var
   LLocked: boolean;
 begin
   LLocked := (not TWallet.Keys.HasPassword) or (not TWallet.Keys.IsValidPassword);
-  if LLocked then begin
+  if LLocked then
+  begin
     TUserInterface.UnlockWallet(Self);
     TWIZOperationsModel.RelockOnFinish := LLocked;
   end;
@@ -194,7 +197,7 @@ end;
 function TOperationConfirmationDataSource.GetColumns: TDataColumns;
 begin
   Result := TArrayTool<TDataColumn>.Concat([
-     Inherited,
+      Inherited,
     // Additional columns
     TDataColumns.Create(
     TDataColumn.From('Operation'),
@@ -275,9 +278,8 @@ begin
         Result := TCellRenderers.OperationShortHash(Result);
       end;
       omtDelistAccountFromSale:
-      begin
         Result := '';
-      end;
+
     end
   else if ABindingName = 'Fee' then
     Result := -Model.Fee.SingleOperationFee

+ 5 - 6
src/gui-experimental/wizards/operations/UWIZOperationPayload_Encryption.lfm

@@ -7,7 +7,6 @@ object WIZOperationPayload_Encryption: TWIZOperationPayload_Encryption
   Caption = 'Form1'
   ClientHeight = 320
   ClientWidth = 511
-  LCLVersion = '1.8.4.0'
   Visible = False
   object grpPayload: TGroupBox
     Left = 8
@@ -33,9 +32,11 @@ object WIZOperationPayload_Encryption: TWIZOperationPayload_Encryption
       Top = 40
       Width = 98
       Caption = 'No Encryption'
+      Checked = True
       Font.Style = [fsBold]
       ParentFont = False
       TabOrder = 0
+      TabStop = True
     end
     object lblNoEncryption: TLabel
       Left = 32
@@ -55,7 +56,7 @@ object WIZOperationPayload_Encryption: TWIZOperationPayload_Encryption
       ParentFont = False
       TabOrder = 1
     end
-    object lblNoEncryption1: TLabel
+    object lblEncryptWithRecipientKey: TLabel
       Left = 32
       Height = 15
       Top = 120
@@ -69,13 +70,11 @@ object WIZOperationPayload_Encryption: TWIZOperationPayload_Encryption
       Top = 160
       Width = 154
       Caption = 'Encrypt with sender key'
-      Checked = True
       Font.Style = [fsBold]
       ParentFont = False
       TabOrder = 2
-      TabStop = True
     end
-    object lblNoEncryption2: TLabel
+    object lblEncryptWithSenderKey: TLabel
       Left = 32
       Height = 40
       Top = 184
@@ -96,7 +95,7 @@ object WIZOperationPayload_Encryption: TWIZOperationPayload_Encryption
       ParentFont = False
       TabOrder = 3
     end
-    object Label1: TLabel
+    object lblEncryptWithPassword: TLabel
       Left = 32
       Height = 15
       Top = 256

+ 28 - 19
src/gui-experimental/wizards/operations/UWIZOperationPayload_Encryption.pas

@@ -31,10 +31,10 @@ type
 
   TWIZOperationPayload_Encryption = class(TWizardForm<TWIZOperationsModel>)
     grpPayload: TGroupBox;
-    Label1: TLabel;
+    lblEncryptWithPassword: TLabel;
     lblNoEncryption: TLabel;
-    lblNoEncryption1: TLabel;
-    lblNoEncryption2: TLabel;
+    lblEncryptWithRecipientKey: TLabel;
+    lblEncryptWithSenderKey: TLabel;
     lblNote1: TLabel;
     paPayload: TPanel;
     rbEncryptedWithRecipient: TRadioButton;
@@ -42,6 +42,7 @@ type
     rbEncryptedWithPassword: TRadioButton;
     rbNotEncrypted: TRadioButton;
   public
+    procedure OnPresent; override;
     procedure OnNext; override;
     function Validate(out message: ansistring): boolean; override;
   end;
@@ -59,36 +60,35 @@ uses
 
 { TWIZOperationPayload_Encryption }
 
+procedure TWIZOperationPayload_Encryption.OnPresent;
+begin
+  case Model.ExecuteOperationType of
+    omtChangeInfo:
+    begin
+      rbEncryptedWithRecipient.Enabled := False;
+      lblEncryptWithRecipientKey.Enabled := False;
+    end;
+  end;
+end;
+
 procedure TWIZOperationPayload_Encryption.OnNext;
 begin
   if rbEncryptedWithRecipient.Checked then
-  begin
-    Model.Payload.PayloadEncryptionMode := pemEncryptWithSender;
-  end
+    Model.Payload.PayloadEncryptionMode := pemEncryptWithRecipient
   else
   if rbEncryptedWithSender.Checked then
-  begin
-    Model.Payload.PayloadEncryptionMode := pemEncryptWithReceiver;
-  end
+    Model.Payload.PayloadEncryptionMode := pemEncryptWithSender
   else
   if rbEncryptedWithPassword.Checked then
-  begin
-    Model.Payload.PayloadEncryptionMode := pemEncryptWithPassword;
-  end
+    Model.Payload.PayloadEncryptionMode := pemEncryptWithPassword
   else
   if rbNotEncrypted.Checked then
-  begin
     Model.Payload.PayloadEncryptionMode := pemNotEncrypt;
-  end;
   case Model.Payload.PayloadEncryptionMode of
     pemEncryptWithPassword:
-    begin
-      UpdatePath(ptInject, [TWIZOperationPayload_Password]);
-    end
+      UpdatePath(ptInject, [TWIZOperationPayload_Password])
     else
-    begin
       UpdatePath(ptInject, [TWIZOperationPayload_Content]);
-    end;
   end;
 end;
 
@@ -102,6 +102,15 @@ begin
     Result := False;
     Exit;
   end;
+
+  case Model.ExecuteOperationType of
+    omtChangeInfo:
+    begin
+      message := 'This operation does not support this type of payload encryption';
+      Result := False;
+      Exit;
+    end;
+  end;
 end;
 
 end.