bool OTEnvelope::GetAsBookendedString( String& strArmorWithBookends, // output (if successful.) bool bEscaped) const { OTASCIIArmor theArmoredText; // This function will base64 ENCODE m_dataContents, and then // Set() that as the string contents on theArmoredText. const bool bSetData = theArmoredText.SetData( m_dataContents, true); // bLineBreaks=true (by default anyway.) if (bSetData) { const bool bWritten = theArmoredText.WriteArmoredString( strArmorWithBookends, "ENVELOPE", // todo hardcoded bEscaped); if (!bWritten) otErr << __FUNCTION__ << ": Failed while calling: " "theArmoredText.WriteArmoredString\n"; else return true; } else otErr << __FUNCTION__ << ": Failed while calling: " "theArmoredText.SetData(m_dataContents, true)\n"; return false; }
bool OTCron::GetMarketList (OTASCIIArmor & ascOutput, int & nMarketCount) { nMarketCount = 0; // This parameter is set to zero here, and incremented in the loop below. // ------------------------ OTMarket * pMarket = NULL; OTDB::MarketList * pMarketList = dynamic_cast<OTDB::MarketList*>(OTDB::CreateObject(OTDB::STORED_OBJ_MARKET_LIST)); OTCleanup<OTDB::MarketList> theListAngel(*pMarketList); // ----------------------------------------------------------- for (mapOfMarkets::iterator ii = m_mapMarkets.begin(); ii != m_mapMarkets.end(); ++ii) { pMarket = (*ii).second; OT_ASSERT(NULL != pMarket); OTDB::MarketData * pMarketData = dynamic_cast<OTDB::MarketData *>(OTDB::CreateObject(OTDB::STORED_OBJ_MARKET_DATA)); OTCleanup<OTDB::MarketData> theDataAngel(*pMarketData); // -------------------------------------------- const OTIdentifier MARKET_ID(*pMarket); const OTString str_MARKET_ID(MARKET_ID); const OTString str_ServerID(pMarket->GetServerID()); const OTString str_ASSET_ID(pMarket->GetAssetID()); const OTString str_CURRENCY_ID(pMarket->GetCurrencyID()); pMarketData->server_id = str_ServerID.Get(); pMarketData->market_id = str_MARKET_ID.Get(); pMarketData->asset_type_id = str_ASSET_ID.Get(); pMarketData->currency_type_id = str_CURRENCY_ID.Get(); // -------------------------------------------- const long & lScale = pMarket->GetScale(); pMarketData->scale = to_string<long>(lScale); // -------------------------------------------- const uint64_t theCurrentBid = pMarket->GetHighestBidPrice(); const uint64_t theCurrentAsk = pMarket->GetLowestAskPrice(); pMarketData->current_bid = to_string<uint64_t>(theCurrentBid); pMarketData->current_ask = to_string<uint64_t>(theCurrentAsk); // --------------------------------------------- const long & lLastSalePrice = pMarket->GetLastSalePrice(); const long & lTotalAvailableAssets = pMarket->GetTotalAvailableAssets(); pMarketData->total_assets = to_string<long>(lTotalAvailableAssets); pMarketData->last_sale_price = to_string<long>(lLastSalePrice); // --------------------------------------------- const mapOfOffers::size_type theBidCount = pMarket->GetBidCount(); const mapOfOffers::size_type theAskCount = pMarket->GetAskCount(); pMarketData->number_bids = to_string<mapOfOffers::size_type>(theBidCount); pMarketData->number_asks = to_string<mapOfOffers::size_type>(theAskCount); // --------------------------------------------- // In the past 24 hours. // (I'm not collecting this data yet, (maybe never), so these values aren't set at all.) // // pMarketData->volume_trades = ???; // pMarketData->volume_assets = ???; // pMarketData->volume_currency = ???; // // pMarketData->recent_highest_bid = ???; // pMarketData->recent_lowest_ask = ???; // --------------------------------------------- // *pMarketData is CLONED at this time (I'm still responsible to delete.) // That's also why I add it here, below: So the data is set right before the cloning occurs. // pMarketList->AddMarketData(*pMarketData); nMarketCount++; } // ------------------------------------------------------------- // Now pack the list into strOutput... if (nMarketCount == 0) return true; // Success, but the list contains 0 markets. else if (nMarketCount > 0) { OTDB::Storage * pStorage = OTDB::GetDefaultStorage(); OT_ASSERT(NULL != pStorage); OTDB::OTPacker * pPacker = pStorage->GetPacker(); // No need to check for failure, since this already ASSERTS. No need to cleanup either. // ----------------------------- OTDB::PackedBuffer * pBuffer = pPacker->Pack(*pMarketList); // Now we PACK our market list. if (NULL == pBuffer) { OTLog::Error("Failed packing pMarketList in OTCron::GetMarketList. \n"); return false; } OTCleanup<OTDB::PackedBuffer> theBufferAngel(*pBuffer); // make sure memory is cleaned up. // -------------------------------------------------------- // Now we need to translate pBuffer into strOutput. const uint8_t* pUint = static_cast<const uint8_t*>(pBuffer->GetData()); const size_t theSize = pBuffer->GetSize(); if ((theSize > 0) && (NULL != pUint)) { OTData theData(pUint, theSize); // This function will base64 ENCODE theData, // and then Set() that as the string contents. ascOutput.SetData(theData); // bool bSuccessSetData = false; // bSuccessSetData = ascOutput.SetData(theData); return true; } else OTLog::Error("OTCron::GetMarketList: 0 size, or null return value, while getting raw data from packed buffer.\n"); } else OTLog::vError("OTCron::GetMarketList: nMarketCount is less than zero: %d.\n", nMarketCount); return false; }
// Loops through ALL markets, and calls pMarket->GetNym_OfferList(NYM_ID, *pOfferList) for each. // Returns a list of all the offers that a specific Nym has on all the markets. // bool OTCron::GetNym_OfferList(OTASCIIArmor & ascOutput, const OTIdentifier & NYM_ID, int & nOfferCount) { nOfferCount = 0; // Outputs the number of offers on this nym. // --------------------------- OTDB::OfferListNym * pOfferList = dynamic_cast<OTDB::OfferListNym*>(OTDB::CreateObject(OTDB::STORED_OBJ_OFFER_LIST_NYM)); OTCleanup<OTDB::OfferListNym> theListAngel(*pOfferList); // ----------------------------------------------------------- for (mapOfMarkets::iterator ii = m_mapMarkets.begin(); ii != m_mapMarkets.end(); ++ii) { OTMarket * pMarket = (*ii).second; OT_ASSERT(NULL != pMarket); int nNymOfferCount = 0; if (false == pMarket->GetNym_OfferList(NYM_ID, *pOfferList, nNymOfferCount)) // appends to *pOfferList, each iteration. { // may wish to add a log later. Anyway, keep iterationg and appending, then send back whatever we have. } else // Success! nOfferCount += nNymOfferCount; } // ------------------------------------------------------------- // Now pack the list into strOutput... if (nOfferCount == 0) return true; // Success, but 0 offers being returned. (List is empty.) else if (nOfferCount > 0) { OTDB::Storage * pStorage = OTDB::GetDefaultStorage(); OT_ASSERT(NULL != pStorage); OTDB::OTPacker * pPacker = pStorage->GetPacker(); // No need to check for failure, since this already ASSERTS. No need to cleanup either. // ----------------------------- OTDB::PackedBuffer * pBuffer = pPacker->Pack(*pOfferList); // Now we PACK our nym's offer list. if (NULL == pBuffer) { OTLog::Error("Failed packing pOfferList in OTCron::GetNym_OfferList. \n"); return false; } OTCleanup<OTDB::PackedBuffer> theBufferAngel(*pBuffer); // make sure memory is cleaned up. // -------------------------------------------------------- // Now we need to translate pBuffer into strOutput. const uint8_t* pUint = static_cast<const uint8_t*>(pBuffer->GetData()); const size_t theSize = pBuffer->GetSize(); if ((NULL != pUint) || (theSize < 2)) { OTData theData(pUint, theSize); // This function will base64 ENCODE theData, // and then Set() that as the string contents. ascOutput.SetData(theData); return true; } else OTLog::Error("Null returned, or bad size, while getting buffer data in OTCron::GetNym_OfferList.\n"); } else OTLog::vError("Error: Less-than-zero nOfferCount in OTCron::GetNym_OfferList: %d.\n", nOfferCount); return false; }
// Presumably this Envelope contains encrypted data (in binary form.) // If you would like an ASCII-armored version of that data, just call this // function. // Should be called "Get Binary Envelope Encrypted Contents Into Ascii-Armored // Form" // bool OTEnvelope::GetAsciiArmoredData(OTASCIIArmor& theArmoredText, bool bLineBreaks) const { return theArmoredText.SetData(m_dataContents, bLineBreaks); }
// Presumably this Envelope contains encrypted data (in binary form.) // If you would like an ASCII-armored version of that data, just call this // function. // Should be called "Get Binary Envelope Encrypted Contents Into Ascii-Armored Form" bool OTEnvelope::GetAsciiArmoredData(OTASCIIArmor & theArmoredText) const { return theArmoredText.SetData(m_dataContents); }
//static bool OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::ArmorPrivateKey(EVP_PKEY & theKey, OTASCIIArmor & ascKey, Timer & theTimer, OTPasswordData * pPWData/*=NULL*/, OTPassword * pImportPassword/*=NULL*/) { bool bReturnVal = false; ascKey.Release(); // ---------------------------------------- // Create a new memory buffer on the OpenSSL side OpenSSL_BIO bmem = BIO_new(BIO_s_mem()); OT_ASSERT(NULL != bmem); int64_t lSize = 0; // ---------------------------------------- // write a private key to that buffer, from theKey // OTPasswordData thePWData("OTAsymmetricKey_OpenSSL::ArmorPrivateKey is calling PEM_write_bio_PrivateKey..."); if (NULL == pPWData) pPWData = &thePWData; int32_t nWriteBio = 0; if (NULL == pImportPassword) nWriteBio = PEM_write_bio_PrivateKey(bmem, &theKey, EVP_des_ede3_cbc(), // todo should this algorithm be hardcoded? NULL, 0, OTAsymmetricKey::GetPasswordCallback(), pPWData); else nWriteBio = PEM_write_bio_PrivateKey(bmem, &theKey, EVP_des_ede3_cbc(), // todo should this algorithm be hardcoded? NULL, 0, 0, const_cast<void*>(reinterpret_cast<const void*>(pImportPassword->getPassword()))); if (0 == nWriteBio) { OTLog::vError("%s: Failed writing EVP_PKEY to memory buffer.\n", __FUNCTION__); } else { // TODO (remove theTimer entirely. OTCachedKey replaces already.) // I set this timer because the above required a password. But now that master key is working, // the above would flow through even WITHOUT the user typing his passphrase (since master key still // not timed out.) Resulting in THIS timer being reset! Todo: I already shortened this timer to 30 // seconds, but need to phase it down to 0 and then remove it entirely! Master key takes over now! // theTimer.start(); // Note: this isn't the ultimate timer solution. See notes in ReleaseKeyLowLevel. // -------------------- OTLog::vOutput(5, "%s: Success writing EVP_PKEY to memory buffer.\n", __FUNCTION__); OTPayload theData; char * pChar = NULL; // After the below call, pChar will point to the memory buffer where the private key supposedly is, // and lSize will contain the size of that memory. // lSize = BIO_get_mem_data(bmem, &pChar); uint32_t nSize = static_cast<uint32_t>(lSize); if (nSize > 0) { // Set the buffer size in our own memory. theData.SetPayloadSize(nSize); // void * pv = OTPassword::safe_memcpy((static_cast<char*>(const_cast<void*>(theData.GetPayloadPointer()))), // destination theData.GetSize(), // size of destination buffer. pChar, // source nSize); // length of source. // bool bZeroSource=false); // if true, sets the source buffer to zero after copying is done. // ------------------------------------------------ // This base64 encodes the private key data, which // is already encrypted to its passphase as well. // ascKey.SetData(theData); OTLog::vOutput(5, "%s: Success copying private key into memory.\n", __FUNCTION__); bReturnVal = true; } else { OTLog::vError("%s: Failed copying private key into memory.\n", __FUNCTION__); } } return bReturnVal; }
// Take a public key, theKey (input), and create an armored version of // it into ascKey (output.) // // OpenSSL loaded key ===> ASCII-Armored export of same key. // //static // bool OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::ArmorPublicKey(EVP_PKEY & theKey, OTASCIIArmor & ascKey) { bool bReturnVal = false; const char * szFunc = "OTAsymmetricKey_OpenSSL::ArmorPublicKey"; ascKey.Release(); // ---------------------------------------- // Create a new memory buffer on the OpenSSL side OpenSSL_BIO bmem = BIO_new(BIO_s_mem()); OT_ASSERT_MSG(NULL != bmem, "OTAsymmetricKey_OpenSSL::ArmorPublicKey: ASSERT: NULL != bmem"); int64_t lSize = 0; // ---------------------------------------- // write a public key to that buffer, from theKey (parameter.) // int32_t nWriteBio = PEM_write_bio_PUBKEY(bmem, &theKey); if (0 == nWriteBio) { OTLog::vError("%s: Error: Failed writing EVP_PKEY to memory buffer.\n", szFunc); } else { OTLog::vOutput(5, "%s: Success writing EVP_PKEY to memory buffer.\n", szFunc); OTPayload theData; char * pChar = NULL; // After the below call, pChar will point to the memory buffer where the public key // supposedly is, and lSize will contain the size of that memory. // lSize = BIO_get_mem_data(bmem, &pChar); uint32_t nSize = static_cast<uint32_t>(lSize); // todo security, etc. Fix this assumed type conversion. if (nSize > 0) { // Set the buffer size in our own memory. theData.SetPayloadSize(nSize); // void * pv = OTPassword::safe_memcpy((static_cast<char*>(const_cast<void*>(theData.GetPayloadPointer()))), // destination theData.GetSize(), // size of destination buffer. pChar, // source nSize); // length of source. // bool bZeroSource=false); // if true, sets the source buffer to zero after copying is done. // ------------------------------------------------ // This base64 encodes the public key data // ascKey.SetData(theData); OTLog::vOutput(5, "%s: Success copying public key into memory.\n", szFunc); bReturnVal = true; } else { OTLog::vError("%s: Failed copying public key into memory.\n", szFunc); } } return bReturnVal; }