UFRMPascalCoinWalletConfig.pas 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. unit UFRMPascalCoinWalletConfig;
  2. {$IFDEF FPC}
  3. {$MODE Delphi}
  4. {$ENDIF}
  5. { Copyright (c) 2016 by Albert Molina
  6. Distributed under the MIT software license, see the accompanying file LICENSE
  7. or visit http://www.opensource.org/licenses/mit-license.php.
  8. This unit is a part of Pascal Coin, a P2P crypto currency without need of
  9. historical operations.
  10. If you like it, consider a donation using BitCoin:
  11. 16K3HCZRhFUtM8GdWRcfKeaa6KsuyxZaYk
  12. }
  13. interface
  14. uses
  15. {$IFnDEF FPC}
  16. Windows,
  17. ShellApi,
  18. {$ELSE}
  19. LCLIntf, LCLType, LMessages,
  20. {$ENDIF}
  21. Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  22. Dialogs, StdCtrls, Buttons, ComCtrls, UAppParams, UWalletKeys;
  23. type
  24. TMinerPrivateKey = (mpk_NewEachTime, mpk_Random, mpk_Selected);
  25. { TFRMPascalCoinWalletConfig }
  26. TFRMPascalCoinWalletConfig = class(TForm)
  27. cbJSONRPCMinerServerActive: TCheckBox;
  28. ebDefaultFee: TEdit;
  29. Label1: TLabel;
  30. cbSaveLogFiles: TCheckBox;
  31. cbShowLogs: TCheckBox;
  32. bbOk: TBitBtn;
  33. bbCancel: TBitBtn;
  34. udInternetServerPort: TUpDown;
  35. ebInternetServerPort: TEdit;
  36. Label2: TLabel;
  37. lblDefaultInternetServerPort: TLabel;
  38. bbUpdatePassword: TBitBtn;
  39. Label3: TLabel;
  40. ebMinerName: TEdit;
  41. Label4: TLabel;
  42. cbShowModalMessages: TCheckBox;
  43. Label5: TLabel;
  44. udJSONRPCMinerServerPort: TUpDown;
  45. ebJSONRPCMinerServerPort: TEdit;
  46. lblDefaultJSONRPCMinerServerPort: TLabel;
  47. gbMinerPrivateKey: TGroupBox;
  48. rbGenerateANewPrivateKeyEachBlock: TRadioButton;
  49. rbUseARandomKey: TRadioButton;
  50. rbMineAllwaysWithThisKey: TRadioButton;
  51. cbPrivateKeyToMine: TComboBox;
  52. cbSaveDebugLogs: TCheckBox;
  53. bbOpenDataFolder: TBitBtn;
  54. procedure FormCreate(Sender: TObject);
  55. procedure bbOkClick(Sender: TObject);
  56. procedure bbUpdatePasswordClick(Sender: TObject);
  57. procedure cbSaveLogFilesClick(Sender: TObject);
  58. procedure bbOpenDataFolderClick(Sender: TObject);
  59. private
  60. FAppParams: TAppParams;
  61. FWalletKeys: TWalletKeys;
  62. procedure SetAppParams(const Value: TAppParams);
  63. procedure SetWalletKeys(const Value: TWalletKeys);
  64. Procedure UpdateWalletConfig;
  65. { Private declarations }
  66. public
  67. { Public declarations }
  68. Property AppParams : TAppParams read FAppParams write SetAppParams;
  69. Property WalletKeys : TWalletKeys read FWalletKeys write SetWalletKeys;
  70. end;
  71. implementation
  72. uses UConst, UAccounts, ULog, UCrypto, UFolderHelper;
  73. {$IFnDEF FPC}
  74. {$R *.dfm}
  75. {$ELSE}
  76. {$R *.lfm}
  77. {$ENDIF}
  78. procedure TFRMPascalCoinWalletConfig.bbOkClick(Sender: TObject);
  79. Var df : Int64;
  80. mpk : TMinerPrivateKey;
  81. i : Integer;
  82. begin
  83. if udInternetServerPort.Position = udJSONRPCMinerServerPort.Position then raise Exception.Create('Server port and JSON-RPC Server miner port are equal!');
  84. if TAccountComp.TxtToMoney(ebDefaultFee.Text,df) then begin
  85. AppParams.ParamByName[CT_PARAM_DefaultFee].SetAsInt64(df);
  86. end else begin
  87. ebDefaultFee.Text := TAccountComp.FormatMoney(AppParams.ParamByName[CT_PARAM_DefaultFee].GetAsInteger(0));
  88. raise Exception.Create('Invalid Fee value');
  89. end;
  90. AppParams.ParamByName[CT_PARAM_InternetServerPort].SetAsInteger(udInternetServerPort.Position );
  91. if rbGenerateANewPrivateKeyEachBlock.Checked then mpk := mpk_NewEachTime
  92. else if rbUseARandomKey.Checked then mpk := mpk_Random
  93. else if rbMineAllwaysWithThisKey.Checked then begin
  94. mpk := mpk_Selected;
  95. if cbPrivateKeyToMine.ItemIndex<0 then raise Exception.Create('Must select a private key');
  96. i := PtrInt(cbPrivateKeyToMine.Items.Objects[cbPrivateKeyToMine.ItemIndex]);
  97. if (i<0) Or (i>=FWalletKeys.Count) then raise Exception.Create('Invalid private key');
  98. AppParams.ParamByName[CT_PARAM_MinerPrivateKeySelectedPublicKey].SetAsString( TAccountComp.AccountKey2RawString( FWalletKeys.Key[i].AccountKey ) );
  99. end else mpk := mpk_Random;
  100. AppParams.ParamByName[CT_PARAM_MinerPrivateKeyType].SetAsInteger(integer(mpk));
  101. AppParams.ParamByName[CT_PARAM_JSONRPCMinerServerActive].SetAsBoolean(cbJSONRPCMinerServerActive.Checked );
  102. AppParams.ParamByName[CT_PARAM_SaveLogFiles].SetAsBoolean(cbSaveLogFiles.Checked );
  103. AppParams.ParamByName[CT_PARAM_ShowLogs].SetAsBoolean(cbShowLogs.Checked );
  104. AppParams.ParamByName[CT_PARAM_SaveDebugLogs].SetAsBoolean(cbSaveDebugLogs.Checked);
  105. AppParams.ParamByName[CT_PARAM_MinerName].SetAsString(ebMinerName.Text);
  106. AppParams.ParamByName[CT_PARAM_ShowModalMessages].SetAsBoolean(cbShowModalMessages.Checked);
  107. AppParams.ParamByName[CT_PARAM_JSONRPCMinerServerPort].SetAsInteger(udJSONRPCMinerServerPort.Position);
  108. ModalResult := MrOk;
  109. end;
  110. procedure TFRMPascalCoinWalletConfig.bbOpenDataFolderClick(Sender: TObject);
  111. begin
  112. {$IFDEF FPC}
  113. OpenDocument(pchar(TFolderHelper.GetPascalCoinDataFolder))
  114. {$ELSE}
  115. shellexecute(0, 'open', pchar(TFolderHelper.GetPascalCoinDataFolder), nil, nil, SW_SHOW)
  116. {$ENDIF}
  117. end;
  118. procedure TFRMPascalCoinWalletConfig.bbUpdatePasswordClick(Sender: TObject);
  119. Var s,s2 : String;
  120. begin
  121. if Not Assigned(FWalletKeys) then exit;
  122. if Not FWalletKeys.IsValidPassword then begin
  123. s := '';
  124. Repeat
  125. if Not InputQuery('Wallet Password','Insert Wallet Password',s) then exit;
  126. FWalletKeys.WalletPassword := s;
  127. if Not FWalletKeys.IsValidPassword then Application.MessageBox(PChar('Invalid password'),PChar(Application.Title),MB_ICONERROR+MB_OK);
  128. Until FWalletKeys.IsValidPassword;
  129. end;
  130. if FWalletKeys.IsValidPassword then begin
  131. s := ''; s2 := '';
  132. if Not InputQuery('Change password','Type new password',s) then exit;
  133. if trim(s)<>s then raise Exception.Create('Password cannot start or end with a space character');
  134. if Not InputQuery('Change password','Type new password again',s2) then exit;
  135. if s<>s2 then raise Exception.Create('Two passwords are different!');
  136. FWalletKeys.WalletPassword := s;
  137. Application.MessageBox(PChar('Password changed!'+#10+#10+
  138. 'Please note that your new password is "'+s+'"'+#10+#10+
  139. '(If you lose this password, you will lose your Wallet forever !)'),
  140. PChar(Application.Title),MB_ICONWARNING+MB_OK);
  141. end;
  142. UpdateWalletConfig;
  143. end;
  144. procedure TFRMPascalCoinWalletConfig.cbSaveLogFilesClick(Sender: TObject);
  145. begin
  146. cbSaveDebugLogs.Enabled := cbSaveLogFiles.Checked;
  147. end;
  148. procedure TFRMPascalCoinWalletConfig.FormCreate(Sender: TObject);
  149. begin
  150. lblDefaultInternetServerPort.Caption := Format('(Default %d)',[CT_NetServer_Port]);
  151. udInternetServerPort.Position := CT_NetServer_Port;
  152. ebDefaultFee.Text := TAccountComp.FormatMoney(0);
  153. ebMinerName.Text := '';
  154. bbUpdatePassword.Enabled := false;
  155. UpdateWalletConfig;
  156. lblDefaultJSONRPCMinerServerPort.Caption := Format('(Default %d)',[CT_JSONRPCMinerServer_Port]);
  157. end;
  158. procedure TFRMPascalCoinWalletConfig.SetAppParams(const Value: TAppParams);
  159. Var i : Integer;
  160. begin
  161. FAppParams := Value;
  162. if Not Assigned(Value) then exit;
  163. Try
  164. udInternetServerPort.Position := AppParams.ParamByName[CT_PARAM_InternetServerPort].GetAsInteger(CT_NetServer_Port);
  165. ebDefaultFee.Text := TAccountComp.FormatMoney(AppParams.ParamByName[CT_PARAM_DefaultFee].GetAsInt64(0));
  166. cbJSONRPCMinerServerActive.Checked := AppParams.ParamByName[CT_PARAM_JSONRPCMinerServerActive].GetAsBoolean(true);
  167. case TMinerPrivateKey(AppParams.ParamByName[CT_PARAM_MinerPrivateKeyType].GetAsInteger(Integer(mpk_Random))) of
  168. mpk_NewEachTime : rbGenerateANewPrivateKeyEachBlock.Checked := true;
  169. mpk_Random : rbUseARandomKey.Checked := true;
  170. mpk_Selected : rbMineAllwaysWithThisKey.Checked := true;
  171. else rbUseARandomKey.Checked := true;
  172. end;
  173. UpdateWalletConfig;
  174. cbSaveLogFiles.Checked := AppParams.ParamByName[CT_PARAM_SaveLogFiles].GetAsBoolean(false);
  175. cbShowLogs.Checked := AppParams.ParamByName[CT_PARAM_ShowLogs].GetAsBoolean(false);
  176. cbSaveDebugLogs.Checked := AppParams.ParamByName[CT_PARAM_SaveDebugLogs].GetAsBoolean(false);
  177. ebMinerName.Text := AppParams.ParamByName[CT_PARAM_MinerName].GetAsString('');
  178. cbShowModalMessages.Checked := AppParams.ParamByName[CT_PARAM_ShowModalMessages].GetAsBoolean(false);
  179. udJSONRPCMinerServerPort.Position := AppParams.ParamByName[CT_PARAM_JSONRPCMinerServerPort].GetAsInteger(CT_JSONRPCMinerServer_Port);
  180. Except
  181. On E:Exception do begin
  182. TLog.NewLog(lterror,ClassName,'Exception at SetAppParams: '+E.Message);
  183. end;
  184. End;
  185. cbSaveLogFilesClick(nil);
  186. end;
  187. procedure TFRMPascalCoinWalletConfig.SetWalletKeys(const Value: TWalletKeys);
  188. begin
  189. FWalletKeys := Value;
  190. UpdateWalletConfig;
  191. end;
  192. procedure TFRMPascalCoinWalletConfig.UpdateWalletConfig;
  193. Var i, iselected : Integer;
  194. s : String;
  195. wk : TWalletKey;
  196. begin
  197. if Assigned(FWalletKeys) then begin
  198. if FWalletKeys.IsValidPassword then begin
  199. if FWalletKeys.WalletPassword='' then begin
  200. bbUpdatePassword.Caption := 'Wallet without password, protect it!';
  201. end else begin
  202. bbUpdatePassword.Caption := 'Change Wallet password';
  203. end;
  204. end else begin
  205. bbUpdatePassword.Caption := 'Wallet with password, change it!';
  206. end;
  207. cbPrivateKeyToMine.Items.Clear;
  208. for i := 0 to FWalletKeys.Count - 1 do begin
  209. wk := FWalletKeys.Key[i];
  210. if (wk.Name='') then begin
  211. s := TCrypto.ToHexaString( TAccountComp.AccountKey2RawString(wk.AccountKey));
  212. end else begin
  213. s := wk.Name;
  214. end;
  215. if wk.CryptedKey<>'' then begin
  216. cbPrivateKeyToMine.Items.AddObject(s,TObject(i));
  217. end;
  218. end;
  219. cbPrivateKeyToMine.Sorted := true;
  220. if Assigned(FAppParams) then begin
  221. s := FAppParams.ParamByName[CT_PARAM_MinerPrivateKeySelectedPublicKey].GetAsString('');
  222. iselected := FWalletKeys.IndexOfAccountKey(TAccountComp.RawString2Accountkey(s));
  223. if iselected>=0 then begin
  224. iselected := cbPrivateKeyToMine.Items.IndexOfObject(TObject(iselected));
  225. cbPrivateKeyToMine.ItemIndex := iselected;
  226. end;
  227. end;
  228. end else bbUpdatePassword.Caption := '(Wallet password)';
  229. bbUpdatePassword.Enabled := Assigned(FWAlletKeys);
  230. end;
  231. end.