135 #include "Helpers.hpp"
147 #include <irrxml/irrXML.hpp>
162 const char* szFunc =
"MainFile::SaveMainFileToString";
165 "<?xml version=\"1.0\"?>\n"
166 "<notaryServer version=\"%s\"\n"
168 " serverUserID=\"%s\"\n"
169 " transactionNum=\"%ld\" >\n\n",
171 server_->m_strServerID.
Get(), server_->m_strServerUserID.
Get(),
179 strMainFile.
Concatenate(
"<cachedKey>\n%s</cachedKey>\n\n",
180 ascMasterContents.
Get());
184 "%s: Failed trying to write master key to notary file.\n",
192 for (
auto& it : server_->transactor_.contractsMap_) {
195 "nullptr contract pointer in MainFile::SaveMainFile.\n");
203 for (
auto& it : server_->transactor_.idToBasketMap_) {
204 OTString strBasketID = it.first.c_str();
205 OTString strBasketAcctID = it.second.c_str();
212 BASKET_ACCOUNT_ID, BASKET_CONTRACT_ID);
215 OTLog::vError(
"%s: Error: Missing Contract ID for basket ID %s\n",
216 szFunc, strBasketID.
Get());
220 OTString strBasketContractID(BASKET_CONTRACT_ID);
222 strMainFile.
Concatenate(
"<basketInfo basketID=\"%s\"\n"
223 " basketAcctID=\"%s\"\n"
224 " basketContractID=\"%s\" />\n\n",
225 strBasketID.
Get(), strBasketAcctID.
Get(),
226 strBasketContractID.
Get());
229 server_->transactor_.voucherAccounts_.
Serialize(strMainFile);
248 "%s: Error saving to string. (Giving up on save attempt.)\n",
262 "%s: Error saving notary (failed writing armored string)\n",
270 strFinal.
Get(),
".", server_->m_strWalletFilename.
Get());
273 OTLog::vError(
"%s: Error saving main file: %s\n", __FUNCTION__,
274 server_->m_strWalletFilename.
Get());
280 const std::string& strServerID,
281 const std::string& strCert,
282 const std::string& strNymID,
283 const std::string& strCachedKey)
286 OTLog::Error(
"Failed trying to store the server contract.\n");
290 if (!strCert.empty() &&
293 "Failed trying to store the server Nym's public/private cert.\n");
297 const char* szBlankFile =
298 "<?xml version=\"1.0\"?>\n"
299 "<notaryServer version=\"2.0\"\n"
301 " serverUserID=\"%s\"\n"
302 " transactionNum=\"%ld\" >\n"
307 "<accountList type=\"voucher\" count=\"0\" >\n"
311 "</notaryServer>\n\n";
313 int64_t lTransNum = 5;
316 strNotaryFile.
Format(szBlankFile, strServerID.c_str(), strNymID.c_str(),
317 lTransNum, strCachedKey.c_str());
319 std::string str_Notary(strNotaryFile.
Get());
324 OTLog::Error(
"Failed trying to store the new notaryServer.xml file.\n");
328 ascCachedKey.
Set(strCachedKey.c_str());
335 sharedPtr->GetMasterPassword(
336 sharedPtr, tempPassword,
337 "We do not have a check hash yet for this password, "
338 "please enter your password",
348 const OTString strServerUserID(strNymID.c_str());
354 "certificate and private key.\n",
358 OTLog::vOutput(0,
"%s: Error verifying server nym. Are you sure you "
359 "have the right ID?\n",
363 OTLog::vOutput(0,
"%s: Error saving new nymfile for server nym.\n",
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",
384 server_->m_strWalletFilename.
Get());
389 server_->m_strWalletFilename.
Get()));
391 if (!strFileContents.
Exists()) {
392 OTLog::vError(
"%s: Unable to read main file: %s\n", __FUNCTION__,
393 server_->m_strWalletFilename.
Get());
402 bool bNeedToConvertUser =
false;
403 bool bNeedToSaveAgain =
false;
405 bool bFailure =
false;
414 OTLog::vError(
"%s: Notary server file apparently was encoded and "
415 "then failed decoding. Filename: %s \n"
417 __FUNCTION__, server_->m_strWalletFilename.
Get(),
418 strFileContents.
Get());
422 irr::io::createIrrXMLReader(xmlFileContents);
424 std::unique_ptr<irr::io::IrrXMLReader> theXMLGuardian(xml);
425 while (xml && xml->read()) {
432 const OTString strNodeName(xml->getNodeName());
434 switch (xml->getNodeType()) {
435 case irr::io::EXN_TEXT:
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");
451 strTransactionNumber =
452 xml->getAttributeValue(
"transactionNum");
454 atol(strTransactionNumber.
Get()));
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",
463 server_->m_strServerID.
Get(),
464 server_->m_strServerUserID.
Get());
466 if (m_strVersion.
Compare(
"1.0"))
471 bNeedToConvertUser =
true;
478 "LoadServerUserAndContract.\n",
492 else if (strNodeName.Compare(
"masterKey") ||
493 strNodeName.Compare(
"cachedKey")) {
506 std::shared_ptr<OTCachedKey> sharedPtr(
508 bNeedToSaveAgain = sharedPtr->GetMasterPassword(
509 sharedPtr, tempPassword,
510 "We do not have a check hash yet for this "
512 "please enter your password",
527 if (!m_strVersion.
Compare(
"1.0"))
532 "LoadServerUserAndContract.\n",
538 else if (strNodeName.Compare(
"accountList"))
542 const OTString strAcctType = xml->getAttributeValue(
"type");
544 xml->getAttributeValue(
"count");
548 xml, strAcctType, strAcctCount))
550 "%s: Error loading voucher accountList.\n",
553 else if (strNodeName.Compare(
"basketInfo")) {
554 OTString strBasketID = xml->getAttributeValue(
"basketID");
556 xml->getAttributeValue(
"basketAcctID");
558 xml->getAttributeValue(
"basketContractID");
561 BASKET_ACCT_ID(strBasketAcctID),
562 BASKET_CONTRACT_ID(strBasketContractID);
565 BASKET_ID, BASKET_ACCT_ID, BASKET_CONTRACT_ID))
567 "Basket ID: %s\n Basket Acct ID: "
568 "%s\n Basket Contract ID: %s\n",
569 strBasketID.
Get(), strBasketAcctID.
Get(),
570 strBasketContractID.
Get());
573 "Basket ID: %s\n Basket Acct ID: %s\n",
574 strBasketID.
Get(), strBasketAcctID.
Get());
580 else if (strNodeName.Compare(
"assetType")) {
581 OTASCIIArmor ascAssetName = xml->getAttributeValue(
"name");
583 if (ascAssetName.
Exists())
587 AssetID = xml->getAttributeValue(
591 "listing)\n Name: %s\n Contract ID: %s\n",
592 AssetName.
Get(), AssetID.
Get());
598 AssetName, strContractPath, AssetID, AssetID);
601 "ASSERT: allocating memory for Asset "
602 "Contract in MainFile::LoadMainFile\n");
610 server_->transactor_.contractsMap_[AssetID.
Get()] =
617 "Asset Contract FAILED to verify.\n");
624 0,
"%s: Failed reading file for Asset Contract.\n",
631 __FUNCTION__, xml->getNodeName());
641 OTString strReason(
"Converting Server Nym to master key.");
642 if (bNeedToConvertUser &&
648 OTString strReason(
"Creating a Hash Check for the master key.");
649 if (bNeedToSaveAgain &&
660 const char* szFunc =
"MainFile::LoadServerUserAndContract";
661 bool bSuccess =
false;
666 server_->m_nymServer.
SetIdentifier(server_->m_strServerUserID);
669 OTLog::vOutput(0,
"%s: Error loading server certificate and keys.\n",
679 bool bLoadedSignedNymfile =
682 "ASSERT: MainFile::LoadServerUserAndContract: "
683 "m_nymServer.LoadSignedNymfile(m_nymServer)\n");
689 "%s: Loaded server certificate and keys.\nNext, loading Cron...\n",
704 OTLog::vError(
"%s: Failed loading Cron file. (Did you just create "
707 OTLog::vOutput(0,
"%s: Loading the server contract...\n", szFunc);
713 server_->m_strServerID, strContractPath, server_->m_strServerID,
714 server_->m_strServerID));
716 "ASSERT while allocating memory for main Server Contract "
717 "in MainFile::LoadServerUserAndContract\n");
719 if (pContract->LoadContract()) {
720 if (pContract->VerifyContract()) {
722 server_->m_pServerContract.swap(pContract);
726 OTLog::Output(0,
"\nMain Server Contract FAILED to verify.\n");
731 "\n%s: Failed reading Main Server Contract:\n%s\n",
732 szFunc, strContractPath.
Get());
static EXPORT void vError(const char *szError,...)
EXPORT bool StorePlainString(std::string strContents, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
bool lookupBasketContractIDByAccountID(const OTIdentifier &basketAccountId, OTIdentifier &basketContractId)
bool LoadServerUserAndContract()
static EXPORT void Output(int32_t nVerbosity, const char *szOutput)
void SetServerID(const OTIdentifier &SERVER_ID)
int64_t transactionNumber() const
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,...)
EXPORT bool WriteArmoredString(OTString &strOutput, const std::string str_type, bool bEscaped=false) const
static EXPORT const OTString & Contract()
EXPORT bool VerifyPseudonym() const
EXPORT bool DecodeIfArmored(bool escapedIsAllowed=true)
EXPORT bool Exists() const
static EXPORT std::shared_ptr< OTCachedKey > It(OTIdentifier *pIdentifier=nullptr)
EXPORT void SetIdentifier(const OTIdentifier &theIdentifier)
static EXPORT void Error(const char *szError)
bool LoadMainFile(bool readOnly=false)
EXPORT void Format(const char *fmt,...)
EXPORT bool Compare(const char *compare) const
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
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="")
static EXPORT bool LoadEncodedTextField(irr::io::IrrXMLReader *&xml, OTASCIIArmor &ascOutput)
#define OT_ASSERT_MSG(x, s)
virtual EXPORT bool LoadContract()
bool SaveMainFileToString(OTString &filename)
EXPORT const char * Get() const
EXPORT void Serialize(OTString &append) const
virtual EXPORT bool VerifyContract()
EXPORT bool Exists(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
void SetServerNym(OTPseudonym *pServerNym)
bool CreateMainFile(const std::string &strContract, const std::string &strServerID, const std::string &strCert, const std::string &strNymID, const std::string &strCachedKey)
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,...)
bool addBasketAccountID(const OTIdentifier &basketId, const OTIdentifier &basketAccountId, const OTIdentifier &basketContractId)
EXPORT bool SaveSignedNymfile(OTPseudonym &SIGNER_NYM)
MainFile(OTServer *server)
virtual bool SaveContractWallet(std::ofstream &ofs) const =0
EXPORT void SetName(const OTString &strName)