UFRMPascalCoinWalletConfig.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. unit UFRMPascalCoinWalletConfig;
  2. { Copyright (c) 2016 by Albert Molina
  3. Distributed under the MIT software license, see the accompanying file LICENSE
  4. or visit http://www.opensource.org/licenses/mit-license.php.
  5. This unit is a part of the PascalCoin Project, an infinitely scalable
  6. cryptocurrency. Find us here:
  7. Web: https://www.pascalcoin.org
  8. Source: https://github.com/PascalCoin/PascalCoin
  9. If you like it, consider a donation using Bitcoin:
  10. 16K3HCZRhFUtM8GdWRcfKeaa6KsuyxZaYk
  11. THIS LICENSE HEADER MUST NOT BE REMOVED.
  12. }
  13. {$IFDEF FPC}
  14. {$MODE Delphi}
  15. {$ENDIF}
  16. interface
  17. uses
  18. {$IFnDEF FPC}
  19. Windows,
  20. ShellApi,
  21. {$ELSE}
  22. LCLIntf, LCLType, LMessages,
  23. {$ENDIF}
  24. Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  25. Dialogs, StdCtrls, Buttons, ComCtrls, UAppParams, USettings, UWallet;
  26. type
  27. { TFRMPascalCoinWalletConfig }
  28. TFRMPascalCoinWalletConfig = class(TForm)
  29. bbChangeLanguage: TBitBtn;
  30. cbJSONRPCMinerServerActive: TCheckBox;
  31. cbDownloadNewCheckpoint: TCheckBox;
  32. ebDefaultFee: TEdit;
  33. ebMinFutureBlocksToDownloadNewSafebox: TEdit;
  34. Label1: TLabel;
  35. cbSaveLogFiles: TCheckBox;
  36. cbShowLogs: TCheckBox;
  37. bbOk: TBitBtn;
  38. bbCancel: TBitBtn;
  39. udInternetServerPort: TUpDown;
  40. ebInternetServerPort: TEdit;
  41. Label2: TLabel;
  42. lblDefaultInternetServerPort: TLabel;
  43. bbUpdatePassword: TBitBtn;
  44. Label3: TLabel;
  45. ebMinerName: TEdit;
  46. Label4: TLabel;
  47. cbShowModalMessages: TCheckBox;
  48. Label5: TLabel;
  49. udJSONRPCMinerServerPort: TUpDown;
  50. ebJSONRPCMinerServerPort: TEdit;
  51. lblDefaultJSONRPCMinerServerPort: TLabel;
  52. gbMinerPrivateKey: TGroupBox;
  53. rbGenerateANewPrivateKeyEachBlock: TRadioButton;
  54. rbUseARandomKey: TRadioButton;
  55. rbMineAllwaysWithThisKey: TRadioButton;
  56. cbPrivateKeyToMine: TComboBox;
  57. cbSaveDebugLogs: TCheckBox;
  58. bbOpenDataFolder: TBitBtn;
  59. cbJSONRPCPortEnabled: TCheckBox;
  60. ebJSONRPCAllowedIPs: TEdit;
  61. Label6: TLabel;
  62. Label7: TLabel;
  63. procedure bbChangeLanguageClick(Sender: TObject);
  64. procedure cbDownloadNewCheckpointClick(Sender: TObject);
  65. procedure FormCreate(Sender: TObject);
  66. procedure bbOkClick(Sender: TObject);
  67. procedure bbUpdatePasswordClick(Sender: TObject);
  68. procedure cbSaveLogFilesClick(Sender: TObject);
  69. procedure bbOpenDataFolderClick(Sender: TObject);
  70. procedure cbJSONRPCPortEnabledClick(Sender: TObject);
  71. private
  72. FAppParams: TAppParams;
  73. FWalletKeys: TWalletKeys;
  74. FNewUILanguage:String;
  75. procedure SetAppParams(const Value: TAppParams);
  76. procedure SetWalletKeys(const Value: TWalletKeys);
  77. Procedure UpdateWalletConfig;
  78. { Private declarations }
  79. public
  80. { Public declarations }
  81. Property AppParams : TAppParams read FAppParams write SetAppParams;
  82. Property WalletKeys : TWalletKeys read FWalletKeys write SetWalletKeys;
  83. end;
  84. implementation
  85. uses
  86. {$IFDEF USE_GNUGETTEXT}gnugettext, UFRMSelectLanguage, {$ENDIF}UConst, UAccounts, ULog, UCrypto, UNode, UGUIUtils, UNetProtocol;
  87. {$IFnDEF FPC}
  88. {$R *.dfm}
  89. {$ELSE}
  90. {$R *.lfm}
  91. {$ENDIF}
  92. procedure TFRMPascalCoinWalletConfig.bbOkClick(Sender: TObject);
  93. Var df : Int64;
  94. mpk : TMinerPrivateKeyType;
  95. i : Integer;
  96. begin
  97. if udInternetServerPort.Position = udJSONRPCMinerServerPort.Position then raise Exception.Create('Server port and JSON-RPC Server miner port are equal!');
  98. if TAccountComp.TxtToMoney(ebDefaultFee.Text,df) then begin
  99. AppParams.ParamByName[CT_PARAM_DefaultFee].SetAsInt64(df);
  100. end else begin
  101. ebDefaultFee.Text := TAccountComp.FormatMoney(AppParams.ParamByName[CT_PARAM_DefaultFee].GetAsInteger(0));
  102. raise Exception.Create('Invalid Fee value');
  103. end;
  104. AppParams.ParamByName[CT_PARAM_InternetServerPort].SetAsInteger(udInternetServerPort.Position );
  105. if rbGenerateANewPrivateKeyEachBlock.Checked then mpk := mpk_NewEachTime
  106. else if rbUseARandomKey.Checked then mpk := mpk_Random
  107. else if rbMineAllwaysWithThisKey.Checked then begin
  108. mpk := mpk_Selected;
  109. if cbPrivateKeyToMine.ItemIndex<0 then raise Exception.Create('Must select a private key');
  110. i := PtrInt(cbPrivateKeyToMine.Items.Objects[cbPrivateKeyToMine.ItemIndex]);
  111. if (i<0) Or (i>=FWalletKeys.Count) then raise Exception.Create('Invalid private key');
  112. AppParams.ParamByName[CT_PARAM_MinerPrivateKeySelectedPublicKey].SetAsTBytes( TAccountComp.AccountKey2RawString( FWalletKeys.Key[i].AccountKey ) );
  113. end else mpk := mpk_Random;
  114. AppParams.ParamByName[CT_PARAM_MinerPrivateKeyType].SetAsInteger(integer(mpk));
  115. AppParams.ParamByName[CT_PARAM_JSONRPCMinerServerActive].SetAsBoolean(cbJSONRPCMinerServerActive.Checked );
  116. AppParams.ParamByName[CT_PARAM_SaveLogFiles].SetAsBoolean(cbSaveLogFiles.Checked );
  117. AppParams.ParamByName[CT_PARAM_ShowLogs].SetAsBoolean(cbShowLogs.Checked );
  118. AppParams.ParamByName[CT_PARAM_SaveDebugLogs].SetAsBoolean(cbSaveDebugLogs.Checked);
  119. AppParams.ParamByName[CT_PARAM_MinerName].SetAsString(ebMinerName.Text);
  120. AppParams.ParamByName[CT_PARAM_ShowModalMessages].SetAsBoolean(cbShowModalMessages.Checked);
  121. AppParams.ParamByName[CT_PARAM_JSONRPCMinerServerPort].SetAsInteger(udJSONRPCMinerServerPort.Position);
  122. AppParams.ParamByName[CT_PARAM_JSONRPCEnabled].SetAsBoolean(cbJSONRPCPortEnabled.Checked);
  123. AppParams.ParamByName[CT_PARAM_JSONRPCAllowedIPs].SetAsString(ebJSONRPCAllowedIPs.Text);
  124. if cbDownloadNewCheckpoint.Checked then begin
  125. i := StrToIntDef(ebMinFutureBlocksToDownloadNewSafebox.Text,0);
  126. AppParams.ParamByName[CT_PARAM_MinFutureBlocksToDownloadNewSafebox].SetAsInteger(i);
  127. AppParams.ParamByName[CT_PARAM_AllowDownloadNewCheckpointIfOlderThan].SetAsBoolean(i>200);
  128. end else AppParams.ParamByName[CT_PARAM_AllowDownloadNewCheckpointIfOlderThan].SetAsBoolean(False);
  129. AppParams.ParamByName[CT_PARAM_UILanguage].SetAsString(FNewUILanguage);
  130. ModalResult := MrOk;
  131. end;
  132. procedure TFRMPascalCoinWalletConfig.bbOpenDataFolderClick(Sender: TObject);
  133. begin
  134. {$IFDEF FPC}
  135. OpenDocument(pchar(TNode.GetPascalCoinDataFolder))
  136. {$ELSE}
  137. shellexecute(0, 'open', pchar(TNode.GetPascalCoinDataFolder), nil, nil, SW_SHOW)
  138. {$ENDIF}
  139. end;
  140. procedure TFRMPascalCoinWalletConfig.bbUpdatePasswordClick(Sender: TObject);
  141. Var s,s2 : String;
  142. begin
  143. if Not Assigned(FWalletKeys) then exit;
  144. if Not FWalletKeys.IsValidPassword then begin
  145. s := '';
  146. Repeat
  147. if Not InputQueryPassword('Wallet Password','Insert Wallet Password',s) then exit;
  148. FWalletKeys.WalletPassword := s;
  149. if Not FWalletKeys.IsValidPassword then Application.MessageBox(PChar('Invalid password'),PChar(Application.Title),MB_ICONERROR+MB_OK);
  150. Until FWalletKeys.IsValidPassword;
  151. end;
  152. if FWalletKeys.IsValidPassword then begin
  153. s := ''; s2 := '';
  154. if Not InputQueryPassword('Change password','Type new password',s) then exit;
  155. if trim(s)<>s then raise Exception.Create('Password cannot start or end with a space character');
  156. if Not InputQueryPassword('Change password','Type new password again',s2) then exit;
  157. if s<>s2 then raise Exception.Create('Two passwords are different!');
  158. FWalletKeys.WalletPassword := s;
  159. Application.MessageBox(PChar('Password changed!'+#10+#10+
  160. 'Please note that your new password is "'+s+'"'+#10+#10+
  161. '(If you lose this password, you will lose your Wallet forever !)'),
  162. PChar(Application.Title),MB_ICONWARNING+MB_OK);
  163. end;
  164. UpdateWalletConfig;
  165. end;
  166. procedure TFRMPascalCoinWalletConfig.cbJSONRPCPortEnabledClick(Sender: TObject);
  167. begin
  168. ebJSONRPCAllowedIPs.Enabled := cbJSONRPCPortEnabled.Checked;
  169. end;
  170. procedure TFRMPascalCoinWalletConfig.cbSaveLogFilesClick(Sender: TObject);
  171. begin
  172. cbSaveDebugLogs.Enabled := cbSaveLogFiles.Checked;
  173. end;
  174. procedure TFRMPascalCoinWalletConfig.FormCreate(Sender: TObject);
  175. begin
  176. {$IFDEF USE_GNUGETTEXT}TranslateComponent(self);{$ENDIF}
  177. //
  178. lblDefaultInternetServerPort.Caption := Format('(Default %d)',[CT_NetServer_Port]);
  179. udInternetServerPort.Position := CT_NetServer_Port;
  180. ebDefaultFee.Text := TAccountComp.FormatMoney(0);
  181. ebMinerName.Text := '';
  182. bbUpdatePassword.Enabled := false;
  183. UpdateWalletConfig;
  184. lblDefaultJSONRPCMinerServerPort.Caption := Format('(Default %d)',[CT_JSONRPCMinerServer_Port]);
  185. {$ifdef fpc}{$ifdef darwin}
  186. Caption:='Preferences';
  187. {$endif}{$endif}
  188. end;
  189. procedure TFRMPascalCoinWalletConfig.cbDownloadNewCheckpointClick(
  190. Sender: TObject);
  191. begin
  192. UpdateWalletConfig;
  193. end;
  194. procedure TFRMPascalCoinWalletConfig.bbChangeLanguageClick(Sender: TObject);
  195. begin
  196. {$IFDEF USE_GNUGETTEXT}
  197. fNewUILanguage := AppParams.ParamByName[CT_PARAM_UILanguage].GetAsString(GetCurrentLanguage);
  198. fNewUILanguage := SelectUILanguage(fNewUILanguage);
  199. if fNewUILanguage<>AppParams.ParamByName[CT_PARAM_UILanguage].GetAsString(GetCurrentLanguage) then // new language selected
  200. begin
  201. UseLanguage(fNewUILanguage);
  202. RetranslateComponent(Self);
  203. end;
  204. {$ENDIF}
  205. end;
  206. procedure TFRMPascalCoinWalletConfig.SetAppParams(const Value: TAppParams);
  207. Var i : Integer;
  208. begin
  209. FAppParams := Value;
  210. if Not Assigned(Value) then exit;
  211. Try
  212. udInternetServerPort.Position := AppParams.ParamByName[CT_PARAM_InternetServerPort].GetAsInteger(CT_NetServer_Port);
  213. ebDefaultFee.Text := TAccountComp.FormatMoney(AppParams.ParamByName[CT_PARAM_DefaultFee].GetAsInt64(0));
  214. cbJSONRPCMinerServerActive.Checked := AppParams.ParamByName[CT_PARAM_JSONRPCMinerServerActive].GetAsBoolean(true);
  215. case TMinerPrivateKeyType(AppParams.ParamByName[CT_PARAM_MinerPrivateKeyType].GetAsInteger(Integer(mpk_Random))) of
  216. mpk_NewEachTime : rbGenerateANewPrivateKeyEachBlock.Checked := true;
  217. mpk_Random : rbUseARandomKey.Checked := true;
  218. mpk_Selected : rbMineAllwaysWithThisKey.Checked := true;
  219. else rbUseARandomKey.Checked := true;
  220. end;
  221. UpdateWalletConfig;
  222. cbSaveLogFiles.Checked := AppParams.ParamByName[CT_PARAM_SaveLogFiles].GetAsBoolean(false);
  223. cbShowLogs.Checked := AppParams.ParamByName[CT_PARAM_ShowLogs].GetAsBoolean(false);
  224. cbSaveDebugLogs.Checked := AppParams.ParamByName[CT_PARAM_SaveDebugLogs].GetAsBoolean(false);
  225. ebMinerName.Text := AppParams.ParamByName[CT_PARAM_MinerName].GetAsString('');
  226. cbShowModalMessages.Checked := AppParams.ParamByName[CT_PARAM_ShowModalMessages].GetAsBoolean(false);
  227. udJSONRPCMinerServerPort.Position := AppParams.ParamByName[CT_PARAM_JSONRPCMinerServerPort].GetAsInteger(CT_JSONRPCMinerServer_Port);
  228. cbJSONRPCPortEnabled.Checked := AppParams.ParamByName[CT_PARAM_JSONRPCEnabled].GetAsBoolean(false);
  229. ebJSONRPCAllowedIPs.Text := AppParams.ParamByName[CT_PARAM_JSONRPCAllowedIPs].GetAsString('127.0.0.1;');
  230. ebMinFutureBlocksToDownloadNewSafebox.Text := IntToStr(AppParams.ParamByName[CT_PARAM_MinFutureBlocksToDownloadNewSafebox].GetAsInteger(TNetData.NetData.MinFutureBlocksToDownloadNewSafebox));
  231. cbDownloadNewCheckpoint.Checked:= AppParams.ParamByName[CT_PARAM_AllowDownloadNewCheckpointIfOlderThan].GetAsBoolean(TNetData.NetData.MinFutureBlocksToDownloadNewSafebox>200);
  232. Except
  233. On E:Exception do begin
  234. TLog.NewLog(lterror,ClassName,'Exception at SetAppParams: '+E.Message);
  235. end;
  236. End;
  237. cbSaveLogFilesClick(nil);
  238. cbJSONRPCPortEnabledClick(nil);
  239. UpdateWalletConfig;
  240. end;
  241. procedure TFRMPascalCoinWalletConfig.SetWalletKeys(const Value: TWalletKeys);
  242. begin
  243. FWalletKeys := Value;
  244. UpdateWalletConfig;
  245. end;
  246. procedure TFRMPascalCoinWalletConfig.UpdateWalletConfig;
  247. Var i, iselected : Integer;
  248. raw : TBytes;
  249. wk : TWalletKey;
  250. auxs : String;
  251. begin
  252. if Assigned(FWalletKeys) then begin
  253. if FWalletKeys.IsValidPassword then begin
  254. if FWalletKeys.WalletPassword='' then begin
  255. bbUpdatePassword.Caption := 'Wallet without password, protect it!';
  256. end else begin
  257. bbUpdatePassword.Caption := 'Change Wallet password';
  258. end;
  259. end else begin
  260. bbUpdatePassword.Caption := 'Wallet with password, change it!';
  261. end;
  262. cbPrivateKeyToMine.Items.Clear;
  263. for i := 0 to FWalletKeys.Count - 1 do begin
  264. wk := FWalletKeys.Key[i];
  265. if (wk.Name='') then begin
  266. auxs := TCrypto.ToHexaString( TAccountComp.AccountKey2RawString(wk.AccountKey));
  267. end else begin
  268. auxs := wk.Name;
  269. end;
  270. if (Length(wk.CryptedKey)>0) then begin
  271. cbPrivateKeyToMine.Items.AddObject(auxs,TObject(i));
  272. end;
  273. end;
  274. cbPrivateKeyToMine.Sorted := true;
  275. if Assigned(FAppParams) then begin
  276. raw := FAppParams.ParamByName[CT_PARAM_MinerPrivateKeySelectedPublicKey].GetAsTBytes(Nil);
  277. iselected := FWalletKeys.IndexOfAccountKey(TAccountComp.RawString2Accountkey(raw));
  278. if iselected>=0 then begin
  279. iselected := cbPrivateKeyToMine.Items.IndexOfObject(TObject(iselected));
  280. cbPrivateKeyToMine.ItemIndex := iselected;
  281. end;
  282. end;
  283. end else bbUpdatePassword.Caption := '(Wallet password)';
  284. bbUpdatePassword.Enabled := Assigned(FWAlletKeys);
  285. ebMinFutureBlocksToDownloadNewSafebox.Enabled:=cbDownloadNewCheckpoint.Checked;
  286. end;
  287. end.