Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MainFile.cpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * MainFile.cpp
4  *
5  */
6 
7 /************************************************************
8  -----BEGIN PGP SIGNED MESSAGE-----
9  Hash: SHA1
10 
11  * OPEN TRANSACTIONS
12  *
13  * Financial Cryptography and Digital Cash
14  * Library, Protocol, API, Server, CLI, GUI
15  *
16  * -- Anonymous Numbered Accounts.
17  * -- Untraceable Digital Cash.
18  * -- Triple-Signed Receipts.
19  * -- Cheques, Vouchers, Transfers, Inboxes.
20  * -- Basket Currencies, Markets, Payment Plans.
21  * -- Signed, XML, Ricardian-style Contracts.
22  * -- Scripted smart contracts.
23  *
24  * Copyright (C) 2010-2013 by "Fellow Traveler" (A pseudonym)
25  *
26  * EMAIL:
28  *
29  * BITCOIN: 1NtTPVVjDsUfDWybS4BwvHpG2pdS9RnYyQ
30  *
31  * KEY FINGERPRINT (PGP Key in license file):
32  * 9DD5 90EB 9292 4B48 0484 7910 0308 00ED F951 BB8E
33  *
34  * OFFICIAL PROJECT WIKI(s):
35  * https://github.com/FellowTraveler/Moneychanger
36  * https://github.com/FellowTraveler/Open-Transactions/wiki
37  *
38  * WEBSITE:
39  * http://www.OpenTransactions.org/
40  *
41  * Components and licensing:
42  * -- Moneychanger..A Java client GUI.....LICENSE:.....GPLv3
43  * -- otlib.........A class library.......LICENSE:...LAGPLv3
44  * -- otapi.........A client API..........LICENSE:...LAGPLv3
45  * -- opentxs/ot....Command-line client...LICENSE:...LAGPLv3
46  * -- otserver......Server Application....LICENSE:....AGPLv3
47  * Github.com/FellowTraveler/Open-Transactions/wiki/Components
48  *
49  * All of the above OT components were designed and written by
50  * Fellow Traveler, with the exception of Moneychanger, which
51  * was contracted out to Vicky C ([email protected]).
52  * The open-source community has since actively contributed.
53  *
54  * -----------------------------------------------------
55  *
56  * LICENSE:
57  * This program is free software: you can redistribute it
58  * and/or modify it under the terms of the GNU Affero
59  * General Public License as published by the Free Software
60  * Foundation, either version 3 of the License, or (at your
61  * option) any later version.
62  *
63  * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
64  * section 7: (This paragraph applies only to the LAGPLv3
65  * components listed above.) If you modify this Program, or
66  * any covered work, by linking or combining it with other
67  * code, such other code is not for that reason alone subject
68  * to any of the requirements of the GNU Affero GPL version 3.
69  * (==> This means if you are only using the OT API, then you
70  * don't have to open-source your code--only your changes to
71  * Open-Transactions itself must be open source. Similar to
72  * LGPLv3, except it applies to software-as-a-service, not
73  * just to distributing binaries.)
74  *
75  * Extra WAIVER for OpenSSL, Lucre, and all other libraries
76  * used by Open Transactions: This program is released under
77  * the AGPL with the additional exemption that compiling,
78  * linking, and/or using OpenSSL is allowed. The same is true
79  * for any other open source libraries included in this
80  * project: complete waiver from the AGPL is hereby granted to
81  * compile, link, and/or use them with Open-Transactions,
82  * according to their own terms, as long as the rest of the
83  * Open-Transactions terms remain respected, with regard to
84  * the Open-Transactions code itself.
85  *
86  * Lucre License:
87  * This code is also "dual-license", meaning that Ben Lau-
88  * rie's license must also be included and respected, since
89  * the code for Lucre is also included with Open Transactions.
90  * See Open-Transactions/src/otlib/lucre/LUCRE_LICENSE.txt
91  * The Laurie requirements are light, but if there is any
92  * problem with his license, simply remove the Lucre code.
93  * Although there are no other blind token algorithms in Open
94  * Transactions (yet. credlib is coming), the other functions
95  * will continue to operate.
96  * See Lucre on Github: https://github.com/benlaurie/lucre
97  * -----------------------------------------------------
98  * You should have received a copy of the GNU Affero General
99  * Public License along with this program. If not, see:
100  * http://www.gnu.org/licenses/
101  *
102  * If you would like to use this software outside of the free
103  * software license, please contact FellowTraveler.
104  * (Unfortunately many will run anonymously and untraceably,
105  * so who could really stop them?)
106  *
107  * DISCLAIMER:
108  * This program is distributed in the hope that it will be
109  * useful, but WITHOUT ANY WARRANTY; without even the implied
110  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
111  * PURPOSE. See the GNU Affero General Public License for
112  * more details.
113 
114  -----BEGIN PGP SIGNATURE-----
115  Version: GnuPG v1.4.9 (Darwin)
116 
117  iQIcBAEBAgAGBQJRSsfJAAoJEAMIAO35UbuOQT8P/RJbka8etf7wbxdHQNAY+2cC
118  vDf8J3X8VI+pwMqv6wgTVy17venMZJa4I4ikXD/MRyWV1XbTG0mBXk/7AZk7Rexk
119  KTvL/U1kWiez6+8XXLye+k2JNM6v7eej8xMrqEcO0ZArh/DsLoIn1y8p8qjBI7+m
120  aE7lhstDiD0z8mwRRLKFLN2IH5rAFaZZUvj5ERJaoYUKdn4c+RcQVei2YOl4T0FU
121  LWND3YLoH8naqJXkaOKEN4UfJINCwxhe5Ke9wyfLWLUO7NamRkWD2T7CJ0xocnD1
122  sjAzlVGNgaFDRflfIF4QhBx1Ddl6wwhJfw+d08bjqblSq8aXDkmFA7HeunSFKkdn
123  oIEOEgyj+veuOMRJC5pnBJ9vV+7qRdDKQWaCKotynt4sWJDGQ9kWGWm74SsNaduN
124  TPMyr9kNmGsfR69Q2Zq/FLcLX/j8ESxU+HYUB4vaARw2xEOu2xwDDv6jt0j3Vqsg
125  x7rWv4S/Eh18FDNDkVRChiNoOIilLYLL6c38uMf1pnItBuxP3uhgY6COm59kVaRh
126  nyGTYCDYD2TK+fI9o89F1297uDCwEJ62U0Q7iTDp5QuXCoxkPfv8/kX6lS6T3y9G
127  M9mqIoLbIQ1EDntFv7/t6fUTS2+46uCrdZWbQ5RjYXdrzjij02nDmJAm2BngnZvd
128  kamH0Y/n11lCvo1oQxM+
129  =uSzz
130  -----END PGP SIGNATURE-----
131 **************************************************************/
132 
133 #include "MainFile.hpp"
134 #include "OTServer.hpp"
135 #include "Helpers.hpp"
136 #include <opentxs/core/OTString.hpp>
139 #include <opentxs/core/OTLog.hpp>
147 #include <irrxml/irrXML.hpp>
148 #include <string>
149 #include <memory>
150 
151 namespace opentxs
152 {
153 
155  : m_strVersion()
156  , server_(server)
157 {
158 }
159 
161 {
162  const char* szFunc = "MainFile::SaveMainFileToString";
163 
164  strMainFile.Format(
165  "<?xml version=\"1.0\"?>\n"
166  "<notaryServer version=\"%s\"\n"
167  " serverID=\"%s\"\n"
168  " serverUserID=\"%s\"\n"
169  " transactionNum=\"%ld\" >\n\n",
170  OTCachedKey::It()->IsGenerated() ? "2.0" : m_strVersion.Get(),
171  server_->m_strServerID.Get(), server_->m_strServerUserID.Get(),
172  server_->transactor_.transactionNumber());
173 
174  if (OTCachedKey::It()->IsGenerated()) // If it exists, then serialize it.
175  {
176  OTASCIIArmor ascMasterContents;
177 
178  if (OTCachedKey::It()->SerializeTo(ascMasterContents)) {
179  strMainFile.Concatenate("<cachedKey>\n%s</cachedKey>\n\n",
180  ascMasterContents.Get());
181  }
182  else
184  "%s: Failed trying to write master key to notary file.\n",
185  szFunc);
186  }
187 
188  // ContractsMap contractsMap_; // If the server needs to store
189  // copies of the asset contracts, then here they are.
190  // MintsMap m_mapMints; // Mints for each of those.
191 
192  for (auto& it : server_->transactor_.contractsMap_) {
193  OTContract* pContract = it.second;
194  OT_ASSERT_MSG(nullptr != pContract,
195  "nullptr contract pointer in MainFile::SaveMainFile.\n");
196 
197  // This is like the Server's wallet.
198  pContract->SaveContractWallet(strMainFile);
199  }
200 
201  // Save the basket account information
202 
203  for (auto& it : server_->transactor_.idToBasketMap_) {
204  OTString strBasketID = it.first.c_str();
205  OTString strBasketAcctID = it.second.c_str();
206 
207  const OTIdentifier BASKET_ACCOUNT_ID(strBasketAcctID);
208  OTIdentifier BASKET_CONTRACT_ID;
209 
210  bool bContractID =
211  server_->transactor_.lookupBasketContractIDByAccountID(
212  BASKET_ACCOUNT_ID, BASKET_CONTRACT_ID);
213 
214  if (!bContractID) {
215  OTLog::vError("%s: Error: Missing Contract ID for basket ID %s\n",
216  szFunc, strBasketID.Get());
217  break;
218  }
219 
220  OTString strBasketContractID(BASKET_CONTRACT_ID);
221 
222  strMainFile.Concatenate("<basketInfo basketID=\"%s\"\n"
223  " basketAcctID=\"%s\"\n"
224  " basketContractID=\"%s\" />\n\n",
225  strBasketID.Get(), strBasketAcctID.Get(),
226  strBasketContractID.Get());
227  }
228 
229  server_->transactor_.voucherAccounts_.Serialize(strMainFile);
230 
231  strMainFile.Concatenate("</notaryServer>\n");
232 
233  return true;
234 }
235 
236 // Setup the default location for the Sever Main File...
237 // maybe this should be set differently...
238 // should be set in the servers configuration.
239 //
241 {
242  // Get the loaded (or new) version of the Server's Main File.
243  //
244  OTString strMainFile;
245 
246  if (!SaveMainFileToString(strMainFile)) {
248  "%s: Error saving to string. (Giving up on save attempt.)\n",
249  __FUNCTION__);
250  return false;
251  }
252  // Try to save the notary server's main datafile to local storage...
253  //
254  OTString strFinal;
255  OTASCIIArmor ascTemp(strMainFile);
256 
257  if (false ==
258  ascTemp.WriteArmoredString(strFinal, "NOTARY")) // todo hardcoding.
259  {
260 
262  "%s: Error saving notary (failed writing armored string)\n",
263  __FUNCTION__);
264  return false;
265  }
266  // Save the Main File to the Harddrive... (or DB, if other storage module is
267  // being used).
268  //
269  const bool bSaved = OTDB::StorePlainString(
270  strFinal.Get(), ".", server_->m_strWalletFilename.Get());
271 
272  if (!bSaved) {
273  OTLog::vError("%s: Error saving main file: %s\n", __FUNCTION__,
274  server_->m_strWalletFilename.Get());
275  }
276  return bSaved;
277 }
278 
279 bool MainFile::CreateMainFile(const std::string& strContract,
280  const std::string& strServerID,
281  const std::string& strCert,
282  const std::string& strNymID,
283  const std::string& strCachedKey)
284 {
285  if (!OTDB::StorePlainString(strContract, "contracts", strServerID)) {
286  OTLog::Error("Failed trying to store the server contract.\n");
287  return false;
288  }
289 
290  if (!strCert.empty() &&
291  !OTDB::StorePlainString(strCert, "certs", strNymID)) {
292  OTLog::Error(
293  "Failed trying to store the server Nym's public/private cert.\n");
294  return false;
295  }
296 
297  const char* szBlankFile = // todo hardcoding.
298  "<?xml version=\"1.0\"?>\n"
299  "<notaryServer version=\"2.0\"\n"
300  " serverID=\"%s\"\n"
301  " serverUserID=\"%s\"\n"
302  " transactionNum=\"%ld\" >\n"
303  "\n"
304  "<cachedKey>\n"
305  "%s</cachedKey>\n"
306  "\n"
307  "<accountList type=\"voucher\" count=\"0\" >\n"
308  "\n"
309  "</accountList>\n"
310  "\n"
311  "</notaryServer>\n\n";
312 
313  int64_t lTransNum = 5; // a starting point, for the new server.
314 
315  OTString strNotaryFile;
316  strNotaryFile.Format(szBlankFile, strServerID.c_str(), strNymID.c_str(),
317  lTransNum, strCachedKey.c_str());
318 
319  std::string str_Notary(strNotaryFile.Get());
320 
321  if (!OTDB::StorePlainString(str_Notary, ".",
322  "notaryServer.xml")) // todo hardcoding.
323  {
324  OTLog::Error("Failed trying to store the new notaryServer.xml file.\n");
325  return false;
326  }
327  OTASCIIArmor ascCachedKey;
328  ascCachedKey.Set(strCachedKey.c_str());
329  OTCachedKey::It()->SetCachedKey(ascCachedKey);
330 
331  if (!OTCachedKey::It()->HasHashCheck()) {
332  OTPassword tempPassword;
333  tempPassword.zeroMemory();
334  std::shared_ptr<OTCachedKey> sharedPtr(OTCachedKey::It());
335  sharedPtr->GetMasterPassword(
336  sharedPtr, tempPassword,
337  "We do not have a check hash yet for this password, "
338  "please enter your password",
339  true);
340  if (!SaveMainFile()) {
341  OT_FAIL;
342  }
343  }
344  // At this point, the contract is saved, the cert is saved, and the
345  // notaryServer.xml file
346  // is saved. All we have left is the Nymfile, which we'll create.
347 
348  const OTString strServerUserID(strNymID.c_str());
349 
350  server_->m_nymServer.SetIdentifier(strServerUserID);
351 
352  if (!server_->m_nymServer.Loadx509CertAndPrivateKey()) {
353  OTLog::vOutput(0, "%s: Error loading server credentials, or "
354  "certificate and private key.\n",
355  __FUNCTION__);
356  }
357  else if (!server_->m_nymServer.VerifyPseudonym()) {
358  OTLog::vOutput(0, "%s: Error verifying server nym. Are you sure you "
359  "have the right ID?\n",
360  __FUNCTION__);
361  }
362  else if (!server_->m_nymServer.SaveSignedNymfile(server_->m_nymServer)) {
363  OTLog::vOutput(0, "%s: Error saving new nymfile for server nym.\n",
364  __FUNCTION__);
365  }
366  else {
367  OTLog::vOutput(0, "%s: OKAY, we have apparently created the new "
368  "server. Remember to erase the contents "
369  "of your ~/.ot/client_data folder, since we used a "
370  "temporary wallet to generate the server "
371  "nym and its master key.\n"
372  "Let's try to load up your new server contract...\n",
373  __FUNCTION__);
374  return true;
375  }
376 
377  return false;
378 }
379 
380 bool MainFile::LoadMainFile(bool bReadOnly)
381 {
382  if (!OTDB::Exists(".", server_->m_strWalletFilename.Get())) {
383  OTLog::vError("%s: Error finding file: %s\n", __FUNCTION__,
384  server_->m_strWalletFilename.Get());
385  return false;
386  }
387  OTString strFileContents(OTDB::QueryPlainString(
388  ".",
389  server_->m_strWalletFilename.Get())); // <=== LOADING FROM DATA STORE.
390 
391  if (!strFileContents.Exists()) {
392  OTLog::vError("%s: Unable to read main file: %s\n", __FUNCTION__,
393  server_->m_strWalletFilename.Get());
394  return false;
395  }
396 
397  // If, for example, the server user Nym is in old format (no master key)
398  // then we will set this to true while loading. Then at the BOTTOM of this
399  // function, we'll convert the Nym to the new format and re-save the notary
400  // file.
401  //
402  bool bNeedToConvertUser = false;
403  bool bNeedToSaveAgain = false;
404 
405  bool bFailure = false;
406 
407  {
408  OTStringXML xmlFileContents(strFileContents);
409 
410  if (false ==
411  xmlFileContents.DecodeIfArmored()) // bEscapedIsAllowed=true by
412  // default.
413  {
414  OTLog::vError("%s: Notary server file apparently was encoded and "
415  "then failed decoding. Filename: %s \n"
416  "Contents: \n%s\n",
417  __FUNCTION__, server_->m_strWalletFilename.Get(),
418  strFileContents.Get());
419  return false;
420  }
421  irr::io::IrrXMLReader* xml =
422  irr::io::createIrrXMLReader(xmlFileContents);
423  // cppcheck-suppress unreadVariable
424  std::unique_ptr<irr::io::IrrXMLReader> theXMLGuardian(xml);
425  while (xml && xml->read()) {
426  // strings for storing the data that we want to read out of the file
427 
428  OTString AssetName;
429  OTString AssetContract;
430  OTString AssetID;
431 
432  const OTString strNodeName(xml->getNodeName());
433 
434  switch (xml->getNodeType()) {
435  case irr::io::EXN_TEXT:
436  // in this xml file, the only text which occurs is the
437  // messageText
438  // messageText = xml->getNodeData();
439  break;
440  case irr::io::EXN_ELEMENT: {
441  if (strNodeName.Compare("notaryServer")) {
442  m_strVersion = xml->getAttributeValue("version");
443  server_->m_strServerID = xml->getAttributeValue("serverID");
444  server_->m_strServerUserID =
445  xml->getAttributeValue("serverUserID");
446 
447  OTString strTransactionNumber; // The server issues
448  // transaction numbers and
449  // stores the counter here
450  // for the latest one.
451  strTransactionNumber =
452  xml->getAttributeValue("transactionNum");
453  server_->transactor_.transactionNumber(
454  atol(strTransactionNumber.Get()));
455 
457  0,
458  "\nLoading Open Transactions server. File version: %s\n"
459  " Last Issued Transaction Number: %ld\n Server ID: "
460  " %s\n Server User ID: %s\n",
461  m_strVersion.Get(),
462  server_->transactor_.transactionNumber(),
463  server_->m_strServerID.Get(),
464  server_->m_strServerUserID.Get());
465 
466  if (m_strVersion.Compare("1.0")) // This means this
467  // Nym has
468  // not been converted yet
469  // to master password.
470  {
471  bNeedToConvertUser = true;
472 
473  if (!(OTCachedKey::It()->isPaused()))
474  OTCachedKey::It()->Pause();
475 
476  if (!LoadServerUserAndContract()) {
477  OTLog::vError("%s: Failed calling "
478  "LoadServerUserAndContract.\n",
479  __FUNCTION__);
480  bFailure = true;
481  }
482 
483  if (OTCachedKey::It()->isPaused())
484  OTCachedKey::It()->Unpause();
485  }
486  }
487  // todo in the future just remove masterkey. I'm leaving it for
488  // now so people's
489  // data files can get converted over. After a while just remove
490  // it.
491  //
492  else if (strNodeName.Compare("masterKey") ||
493  strNodeName.Compare("cachedKey")) {
494  OTASCIIArmor ascCachedKey;
495 
496  if (OTContract::LoadEncodedTextField(xml, ascCachedKey)) {
497  // We successfully loaded the masterKey from file, so
498  // let's SET it
499  // as the master key globally...
500  //
501  OTCachedKey::It()->SetCachedKey(ascCachedKey);
502 
503  if (!OTCachedKey::It()->HasHashCheck()) {
504  OTPassword tempPassword;
505  tempPassword.zeroMemory();
506  std::shared_ptr<OTCachedKey> sharedPtr(
507  OTCachedKey::It());
508  bNeedToSaveAgain = sharedPtr->GetMasterPassword(
509  sharedPtr, tempPassword,
510  "We do not have a check hash yet for this "
511  "password, "
512  "please enter your password",
513  true);
514  }
515  }
516 
517  OTLog::vOutput(0, "\nLoading cachedKey:\n%s\n",
518  ascCachedKey.Get());
519  //
520  // It's only here, AFTER the master key has been loaded,
521  // that we can
522  // go ahead and load the server user, the server contract,
523  // cron, etc.
524  // (It wasn't that way in version 1, before we had master
525  // keys.)
526  //
527  if (!m_strVersion.Compare("1.0")) // This is, for
528  // example, 2.0
529  {
530  if (!LoadServerUserAndContract()) {
531  OTLog::vError("%s: Failed calling "
532  "LoadServerUserAndContract.\n",
533  __FUNCTION__);
534  bFailure = true;
535  }
536  }
537  }
538  else if (strNodeName.Compare("accountList")) // the voucher
539  // reserve
540  // account IDs.
541  {
542  const OTString strAcctType = xml->getAttributeValue("type");
543  const OTString strAcctCount =
544  xml->getAttributeValue("count");
545 
546  if ((-1) ==
547  server_->transactor_.voucherAccounts_.ReadFromXMLNode(
548  xml, strAcctType, strAcctCount))
550  "%s: Error loading voucher accountList.\n",
551  __FUNCTION__);
552  }
553  else if (strNodeName.Compare("basketInfo")) {
554  OTString strBasketID = xml->getAttributeValue("basketID");
555  OTString strBasketAcctID =
556  xml->getAttributeValue("basketAcctID");
557  OTString strBasketContractID =
558  xml->getAttributeValue("basketContractID");
559 
560  const OTIdentifier BASKET_ID(strBasketID),
561  BASKET_ACCT_ID(strBasketAcctID),
562  BASKET_CONTRACT_ID(strBasketContractID);
563 
564  if (server_->transactor_.addBasketAccountID(
565  BASKET_ID, BASKET_ACCT_ID, BASKET_CONTRACT_ID))
566  OTLog::vOutput(0, "Loading basket currency info...\n "
567  "Basket ID: %s\n Basket Acct ID: "
568  "%s\n Basket Contract ID: %s\n",
569  strBasketID.Get(), strBasketAcctID.Get(),
570  strBasketContractID.Get());
571  else
572  OTLog::vError("Error adding basket currency info...\n "
573  "Basket ID: %s\n Basket Acct ID: %s\n",
574  strBasketID.Get(), strBasketAcctID.Get());
575  }
576 
577  // Create an OTAssetContract and load them from file, (for each
578  // asset type),
579  // and add them to the internal map.
580  else if (strNodeName.Compare("assetType")) {
581  OTASCIIArmor ascAssetName = xml->getAttributeValue("name");
582 
583  if (ascAssetName.Exists())
584  ascAssetName.GetString(AssetName,
585  false); // linebreaks == false
586 
587  AssetID = xml->getAttributeValue(
588  "assetTypeID"); // hash of contract itself
589 
590  OTLog::vOutput(0, "\n\n****Asset Contract**** (server "
591  "listing)\n Name: %s\n Contract ID: %s\n",
592  AssetName.Get(), AssetID.Get());
593 
594  OTString strContractPath;
595  strContractPath = OTFolders::Contract().Get();
596 
597  OTAssetContract* pContract = new OTAssetContract(
598  AssetName, strContractPath, AssetID, AssetID);
599 
600  OT_ASSERT_MSG(nullptr != pContract,
601  "ASSERT: allocating memory for Asset "
602  "Contract in MainFile::LoadMainFile\n");
603 
604  if (pContract->LoadContract()) {
605  if (pContract->VerifyContract()) {
606  OTLog::Output(0, "** Asset Contract Verified **\n");
607 
608  pContract->SetName(AssetName);
609 
610  server_->transactor_.contractsMap_[AssetID.Get()] =
611  pContract;
612  }
613  else {
614  delete pContract;
615  pContract = nullptr;
616  OTLog::Output(0,
617  "Asset Contract FAILED to verify.\n");
618  }
619  }
620  else {
621  delete pContract;
622  pContract = nullptr;
624  0, "%s: Failed reading file for Asset Contract.\n",
625  __FUNCTION__);
626  }
627  }
628  else {
629  // unknown element type
630  OTLog::vError("%s: Unknown element type: %s\n",
631  __FUNCTION__, xml->getNodeName());
632  }
633  } break;
634  default:
635  break;
636  }
637  }
638  }
639  if (!bReadOnly) {
640  {
641  OTString strReason("Converting Server Nym to master key.");
642  if (bNeedToConvertUser &&
643  server_->m_nymServer.Savex509CertAndPrivateKey(true,
644  &strReason))
645  SaveMainFile();
646  }
647  {
648  OTString strReason("Creating a Hash Check for the master key.");
649  if (bNeedToSaveAgain &&
650  server_->m_nymServer.Savex509CertAndPrivateKey(true,
651  &strReason))
652  SaveMainFile();
653  }
654  }
655  return !bFailure;
656 }
657 
659 {
660  const char* szFunc = "MainFile::LoadServerUserAndContract";
661  bool bSuccess = false;
662  OT_ASSERT(m_strVersion.Exists());
663  OT_ASSERT(server_->m_strServerID.Exists());
664  OT_ASSERT(server_->m_strServerUserID.Exists());
665 
666  server_->m_nymServer.SetIdentifier(server_->m_strServerUserID);
667 
668  if (!server_->m_nymServer.Loadx509CertAndPrivateKey(false)) {
669  OTLog::vOutput(0, "%s: Error loading server certificate and keys.\n",
670  szFunc);
671  }
672  else if (!server_->m_nymServer.VerifyPseudonym()) {
673  OTLog::vOutput(0, "%s: Error verifying server nym.\n", szFunc);
674  }
675  else {
676  // This file will be saved during the course of operation
677  // Just making sure it is loaded up first.
678  //
679  bool bLoadedSignedNymfile =
680  server_->m_nymServer.LoadSignedNymfile(server_->m_nymServer);
681  OT_ASSERT_MSG(bLoadedSignedNymfile,
682  "ASSERT: MainFile::LoadServerUserAndContract: "
683  "m_nymServer.LoadSignedNymfile(m_nymServer)\n");
684  // m_nymServer.SaveSignedNymfile(m_nymServer); // Uncomment this if
685  // you want to create the file. NORMALLY LEAVE IT OUT!!!! DANGEROUS!!!
686 
688  0,
689  "%s: Loaded server certificate and keys.\nNext, loading Cron...\n",
690  szFunc);
691  // Load Cron (now that we have the server Nym.
692  // (I WAS loading this erroneously in Server.Init(), before
693  // the Nym had actually been loaded from disk. That didn't work.)
694  //
695  const OTIdentifier SERVER_ID(server_->m_strServerID);
696 
697  // Make sure the Cron object has a pointer to the server's Nym.
698  // (For signing stuff...)
699  //
700  server_->m_Cron.SetServerID(SERVER_ID);
701  server_->m_Cron.SetServerNym(&server_->m_nymServer);
702 
703  if (!server_->m_Cron.LoadCron())
704  OTLog::vError("%s: Failed loading Cron file. (Did you just create "
705  "this server?)\n",
706  szFunc);
707  OTLog::vOutput(0, "%s: Loading the server contract...\n", szFunc);
708 
709  // We have the serverID, so let's load up the server Contract!
710  OTString strContractPath(OTFolders::Contract().Get());
711 
712  std::unique_ptr<OTServerContract> pContract(new OTServerContract(
713  server_->m_strServerID, strContractPath, server_->m_strServerID,
714  server_->m_strServerID));
715  OT_ASSERT_MSG(nullptr != pContract,
716  "ASSERT while allocating memory for main Server Contract "
717  "in MainFile::LoadServerUserAndContract\n");
718 
719  if (pContract->LoadContract()) {
720  if (pContract->VerifyContract()) {
721  OTLog::Output(0, "\n** Main Server Contract Verified **\n");
722  server_->m_pServerContract.swap(pContract);
723  bSuccess = true;
724  }
725  else {
726  OTLog::Output(0, "\nMain Server Contract FAILED to verify.\n");
727  }
728  }
729  else {
730  OTLog::vOutput(0,
731  "\n%s: Failed reading Main Server Contract:\n%s\n",
732  szFunc, strContractPath.Get());
733  }
734  }
735 
736  return bSuccess;
737 }
738 
739 } // namespace opentxs
static EXPORT void vError(const char *szError,...)
Definition: OTLog.cpp:800
EXPORT bool StorePlainString(std::string strContents, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:698
bool lookupBasketContractIDByAccountID(const OTIdentifier &basketAccountId, OTIdentifier &basketContractId)
Definition: Transactor.cpp:484
bool LoadServerUserAndContract()
Definition: MainFile.cpp:658
static EXPORT void Output(int32_t nVerbosity, const char *szOutput)
Definition: OTLog.cpp:710
void SetServerID(const OTIdentifier &SERVER_ID)
Definition: OTCron.hpp:289
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
int64_t transactionNumber() const
Definition: Transactor.hpp:171
EXPORT bool LoadSignedNymfile(OTPseudonym &SIGNER_NYM)
EXPORT int32_t ReadFromXMLNode(irr::io::IrrXMLReader *&xml, const OTString &acctType, const OTString &acctCount)
EXPORT void Concatenate(const char *arg,...)
Definition: OTString.cpp:1334
EXPORT bool WriteArmoredString(OTString &strOutput, const std::string str_type, bool bEscaped=false) const
static EXPORT const OTString & Contract()
Definition: OTFolders.cpp:303
EXPORT bool VerifyPseudonym() const
EXPORT bool DecodeIfArmored(bool escapedIsAllowed=true)
Definition: OTString.cpp:1212
EXPORT bool Exists() const
Definition: OTString.cpp:1035
static EXPORT std::shared_ptr< OTCachedKey > It(OTIdentifier *pIdentifier=nullptr)
EXPORT void SetIdentifier(const OTIdentifier &theIdentifier)
static EXPORT void Error(const char *szError)
Definition: OTLog.cpp:831
bool LoadMainFile(bool readOnly=false)
Definition: MainFile.cpp:380
EXPORT void Format(const char *fmt,...)
Definition: OTString.cpp:1319
EXPORT bool Compare(const char *compare) const
Definition: OTString.cpp:1102
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
EXPORT bool Loadx509CertAndPrivateKey(bool bChecking=false, const OTPasswordData *pPWData=nullptr, const OTPassword *pImportPassword=nullptr)
EXPORT std::string QueryPlainString(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:728
EXPORT bool LoadCron()
Definition: OTCron.cpp:165
static EXPORT bool LoadEncodedTextField(irr::io::IrrXMLReader *&xml, OTASCIIArmor &ascOutput)
#define OT_ASSERT(x)
Definition: Assert.hpp:150
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
virtual EXPORT bool LoadContract()
#define OT_FAIL
Definition: Assert.hpp:139
bool SaveMainFileToString(OTString &filename)
Definition: MainFile.cpp:160
EXPORT const char * Get() const
Definition: OTString.cpp:1045
EXPORT void Serialize(OTString &append) const
virtual EXPORT bool VerifyContract()
Definition: OTContract.cpp:330
EXPORT bool Exists(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:584
void SetServerNym(OTPseudonym *pServerNym)
Definition: OTCron.hpp:298
bool CreateMainFile(const std::string &strContract, const std::string &strServerID, const std::string &strCert, const std::string &strNymID, const std::string &strCachedKey)
Definition: MainFile.cpp:279
EXPORT bool GetString(OTString &theData, bool bLineBreaks=true) const
EXPORT bool Savex509CertAndPrivateKey(bool bCreateFile=true, const OTString *pstrReason=nullptr)
static EXPORT void vOutput(int32_t nVerbosity, const char *szOutput,...)
Definition: OTLog.cpp:768
bool addBasketAccountID(const OTIdentifier &basketId, const OTIdentifier &basketAccountId, const OTIdentifier &basketContractId)
Definition: Transactor.cpp:432
EXPORT bool SaveSignedNymfile(OTPseudonym &SIGNER_NYM)
MainFile(OTServer *server)
Definition: MainFile.cpp:154
virtual bool SaveContractWallet(std::ofstream &ofs) const =0
EXPORT void SetName(const OTString &strName)
Definition: OTContract.hpp:364