Browse Source

WalletKeys added IsReadOnly property

PascalCoin 6 years ago
parent
commit
0cd16f28ff
2 changed files with 27 additions and 4 deletions
  1. 26 4
      src/core/UWallet.pas
  2. 1 0
      src/gui-classic/UFRMWalletKeys.pas

+ 26 - 4
src/core/UWallet.pas

@@ -60,12 +60,14 @@ Type
     FWalletFileName: String;
     FWalletFileName: String;
     FIsReadingStream : Boolean;
     FIsReadingStream : Boolean;
     FOnChanged: TNotifyManyEvent;
     FOnChanged: TNotifyManyEvent;
+    FIsReadOnly: Boolean;
     function GetHasPassword : boolean;
     function GetHasPassword : boolean;
     function GetKey(index: Integer): TWalletKey;
     function GetKey(index: Integer): TWalletKey;
     procedure SetWalletPassword(const Value: String);
     procedure SetWalletPassword(const Value: String);
     Procedure GeneratePrivateKeysFromPassword;
     Procedure GeneratePrivateKeysFromPassword;
     procedure SetWalletFileName(const Value: String);
     procedure SetWalletFileName(const Value: String);
     Function Find(Const AccountKey: TAccountKey; var Index: Integer): Boolean;
     Function Find(Const AccountKey: TAccountKey; var Index: Integer): Boolean;
+    procedure SetIsReadOnly(const Value: Boolean);
   public
   public
     Property Key[index : Integer] : TWalletKey read GetKey; default;
     Property Key[index : Integer] : TWalletKey read GetKey; default;
     Constructor Create(AOwner : TComponent); override;
     Constructor Create(AOwner : TComponent); override;
@@ -85,6 +87,7 @@ Type
     Property OnChanged : TNotifyManyEvent read FOnChanged;
     Property OnChanged : TNotifyManyEvent read FOnChanged;
     Procedure SetName(index : Integer; Const newName : String);
     Procedure SetName(index : Integer; Const newName : String);
     Function LockWallet : Boolean;
     Function LockWallet : Boolean;
+    property IsReadOnly : Boolean read FIsReadOnly write SetIsReadOnly;
   End;
   End;
 
 
   TWalletKeysExt = Class(TWalletKeys)
   TWalletKeysExt = Class(TWalletKeys)
@@ -250,6 +253,7 @@ end;
 constructor TWalletKeys.Create(AOwner : TComponent);
 constructor TWalletKeys.Create(AOwner : TComponent);
 begin
 begin
   inherited;
   inherited;
+  FIsReadOnly := False;
   FIsValidPassword := false;
   FIsValidPassword := false;
   FWalletFileStream := Nil;
   FWalletFileStream := Nil;
   FWalletPassword := '';
   FWalletPassword := '';
@@ -328,6 +332,7 @@ begin
               [TAccountComp.AccountPublicKeyExport(LtmpECPrivKey.PublicKey),
               [TAccountComp.AccountPublicKeyExport(LtmpECPrivKey.PublicKey),
                TAccountComp.AccountPublicKeyExport(P^.AccountKey)]));
                TAccountComp.AccountPublicKeyExport(P^.AccountKey)]));
             LtmpECPrivKey.Free;
             LtmpECPrivKey.Free;
+            isOk := False;
           end else P^.PrivateKey := LtmpECPrivKey;
           end else P^.PrivateKey := LtmpECPrivKey;
         except
         except
           on E: Exception do begin
           on E: Exception do begin
@@ -407,7 +412,7 @@ procedure TWalletKeys.SaveToStream(Stream: TStream);
 var i : Integer;
 var i : Integer;
   P : PWalletKey;
   P : PWalletKey;
 begin
 begin
-  if FIsReadingStream then exit;
+  if (FIsReadingStream) or ((FIsReadOnly) and (Stream = FWalletFileStream)) then exit;
   if Not Assigned(Stream) then exit;
   if Not Assigned(Stream) then exit;
   Stream.Size := 0;
   Stream.Size := 0;
   Stream.Position:=0;
   Stream.Position:=0;
@@ -426,6 +431,18 @@ begin
   end;
   end;
 end;
 end;
 
 
+procedure TWalletKeys.SetIsReadOnly(const Value: Boolean);
+var LTmp : String;
+begin
+  if FIsReadOnly = Value then Exit;
+  FIsReadOnly := Value;
+  if (FFileName<>'') then begin
+    LTmp := FFileName;
+    FFileName := '';
+    SetWalletFileName( Ltmp );
+  end;
+end;
+
 procedure TWalletKeys.SetName(index: Integer; const newName: String);
 procedure TWalletKeys.SetName(index: Integer; const newName: String);
 begin
 begin
   if PWalletKey(FSearchableKeys[index])^.Name=newName then exit;
   if PWalletKey(FSearchableKeys[index])^.Name=newName then exit;
@@ -442,9 +459,14 @@ begin
   if Assigned(FWalletFileStream) then FWalletFileStream.Free;
   if Assigned(FWalletFileStream) then FWalletFileStream.Free;
   FWalletFileStream := Nil;
   FWalletFileStream := Nil;
   if Value<>'' then begin
   if Value<>'' then begin
-    if FileExists(Value) then fm := fmOpenReadWrite
-    else fm := fmCreate;
-    FWalletFileStream := TFileStream.Create(WalletfileName,fm+fmShareDenyWrite);
+    if FileExists(Value) then begin
+      if FIsReadOnly then fm := fmOpenRead
+      else fm := fmOpenReadWrite+fmShareDenyWrite;
+    end else begin
+      if FIsReadOnly then Exit
+      else fm := fmCreate+fmShareDenyWrite;
+    end;
+    FWalletFileStream := TFileStream.Create(WalletfileName,fm);
     FWalletFileStream.Position:=0;
     FWalletFileStream.Position:=0;
     LoadFromStream(FWalletFileStream);
     LoadFromStream(FWalletFileStream);
   end;
   end;

+ 1 - 0
src/gui-classic/UFRMWalletKeys.pas

@@ -227,6 +227,7 @@ begin
 
 
   wki := TWalletKeys.Create(Self);
   wki := TWalletKeys.Create(Self);
   try
   try
+    wki.IsReadOnly := True;
     wki.WalletFileName := ifn;
     wki.WalletFileName := ifn;
     if wki.Count<=0 then raise Exception.Create('Wallet file has no valid data');
     if wki.Count<=0 then raise Exception.Create('Wallet file has no valid data');
     pwd := '';
     pwd := '';