OTLedger * OTLedger::GenerateLedger(const OTIdentifier & theUserID, const OTIdentifier & theAcctID, // AcctID should be "OwnerID" since could be acct OR Nym (with nymbox) const OTIdentifier & theServerID, const ledgerType theType, bool bCreateFile/*=false*/) { OTLedger * pLedger = new OTLedger(theUserID, theAcctID, theServerID); OT_ASSERT(NULL != pLedger); pLedger->GenerateLedger(theAcctID, theServerID, theType, bCreateFile); pLedger->SetUserID(theUserID); return pLedger; }
OTLedger * OTLedger::GenerateLedger(const OTIdentifier & theUserID, const OTIdentifier & theAcctID, const OTIdentifier & theServerID, const ledgerType theType, bool bCreateFile/*=false*/) { OTLedger * pLedger = new OTLedger(theUserID, theAcctID, theServerID); OT_ASSERT(NULL != pLedger); pLedger->GenerateLedger(theAcctID, theServerID, theType, bCreateFile); pLedger->SetUserID(theUserID); return pLedger; }
// Caller responsible to delete. OTLedger * OTAccount::LoadOutbox(OTPseudonym & theNym) { OTLedger * pBox = new OTLedger(GetUserID(), GetRealAccountID(), GetRealServerID()); OT_ASSERT(NULL != pBox); if (pBox->LoadOutbox() && pBox->VerifyAccount(theNym)) { return pBox; } else { OTString strUserID(GetUserID()), strAcctID(GetRealAccountID()); OTLog::vOutput(2, "Unable to load or verify outbox:\n%s\n For user:\n%s\n", strAcctID.Get(), strUserID.Get()); } return NULL; }
// pHash is optional, the account will update its internal copy of the hash anyway. // bool OTAccount::SaveOutbox(OTLedger &theBox, OTIdentifier * pHash/*=NULL*/) // If you pass the identifier in, the hash is recorded there { if (!IsSameAccount(theBox)) { const OTString strAcctID(GetRealAccountID()), strServerID(GetRealServerID()), strBoxAcctID(theBox.GetRealAccountID()), strBoxSvrID(theBox.GetRealServerID()); OTLog::vError("OTAccount::SaveOutbox: ERROR: The ledger passed in, isn't even for this account!\n" " Acct ID: %s\n Other ID: %s\n Server ID: %s\n Other ID: %s\n", strAcctID.Get(), strBoxAcctID.Get(), strServerID.Get(), strBoxSvrID.Get()); return false; } OTIdentifier theHash; // Use pHash. if (NULL == pHash) pHash = &theHash; // ------------------------------------------ bool bSuccess = theBox.SaveOutbox(pHash); if (bSuccess) this->SetOutboxHash(*pHash); return bSuccess; }
// --------------------------------------- // For incoming, pending (not-yet-accepted) instruments. // bool MTRecord::DiscardIncoming() { if (!this->CanDiscardIncoming()) return false; switch (this->GetRecordType()) { case MTRecord::Instrument: { if (m_str_server_id.empty() || m_str_nym_id.empty()) { OTLog::vError("%s: Error: missing server id (%s) or nym id (%s)\n", __FUNCTION__, m_str_server_id.c_str(), m_str_nym_id.c_str()); return false; } // ------------------------------ if (0 == m_lTransactionNum) { OTLog::vError("%s: Error: Transaction number is 0, in payment inbox for server id (%s), nym id (%s)\n", __FUNCTION__, m_str_server_id.c_str(), m_str_nym_id.c_str()); return false; } // ------------------------------ const OTIdentifier theServerID(m_str_server_id), theNymID(m_str_nym_id); // Open the Nym's payments inbox. OTLedger * pInbox = OTAPI_Wrap::OTAPI()->LoadPaymentInbox(theServerID, theNymID); OTCleanup<OTLedger> theInboxAngel(pInbox); // ------------------------------------------ if (NULL == pInbox) { OTLog::vError("%s: Error: Unable to load payment inbox for server id (%s), nym id (%s)\n", __FUNCTION__, m_str_server_id.c_str(), m_str_nym_id.c_str()); return false; } // ------------------------------------------ // Find the payment therein that correlates to this MTRecord. // int nIndex = pInbox->GetTransactionIndex(m_lTransactionNum); if ((-1) == nIndex) { OTLog::vError("%s: Error: Unable to find transaction %ld in payment inbox for server id (%s), nym id (%s)\n", __FUNCTION__, m_lTransactionNum, m_str_server_id.c_str(), m_str_nym_id.c_str()); return false; } // ------------------------------------------ // Accept it. // OTString strIndices; strIndices.Format("%d", nIndex); const std::string str_indices(strIndices.Get()); OT_ME madeEasy; return madeEasy.discard_incoming_payments(m_str_server_id, m_str_nym_id, str_indices); } // case: instrument break; // -------------------------------------------- default: OTLog::vError("%s: Unexpected type: %s\n", __FUNCTION__, this->GetInstrumentType().c_str()); return false; } return true; }
// --------------------------------------- // For incoming, pending (not-yet-accepted) instruments. // bool MTRecord::AcceptIncomingInstrument(const std::string & str_into_acct) { if (!this->CanAcceptIncoming()) return false; switch (this->GetRecordType()) { // -------------------------------------------- // Accept from Nym's payments inbox. // case MTRecord::Instrument: { if (m_str_server_id.empty() || m_str_nym_id.empty()) { OTLog::vError("%s: Error: missing server id (%s) or nym id (%s)\n", __FUNCTION__, m_str_server_id.c_str(), m_str_nym_id.c_str()); return false; } // ------------------------------ if (0 == m_lTransactionNum) { OTLog::vError("%s: Error: Transaction number is 0, in payment inbox for server id (%s), nym id (%s)\n", __FUNCTION__, m_str_server_id.c_str(), m_str_nym_id.c_str()); return false; } // ------------------------------ const OTIdentifier theServerID(m_str_server_id), theNymID(m_str_nym_id); // Open the Nym's payments inbox. OTLedger * pInbox = OTAPI_Wrap::OTAPI()->LoadPaymentInbox(theServerID, theNymID); OTCleanup<OTLedger> theInboxAngel(pInbox); // ------------------------------------------ if (NULL == pInbox) { OTLog::vError("%s: Error: Unable to load payment inbox for server id (%s), nym id (%s)\n", __FUNCTION__, m_str_server_id.c_str(), m_str_nym_id.c_str()); return false; } // ------------------------------------------ // Find the payment therein that correlates to this MTRecord. // int nIndex = pInbox->GetTransactionIndex(m_lTransactionNum); if ((-1) == nIndex) { OTLog::vError("%s: Error: Unable to find transaction %ld in payment inbox for server id (%s), nym id (%s)\n", __FUNCTION__, m_lTransactionNum, m_str_server_id.c_str(), m_str_nym_id.c_str()); return false; } // ------------------------------------------ // Accept it. // OTString strIndices; strIndices.Format("%d", nIndex); const std::string str_indices(strIndices.Get()); OT_ME madeEasy; int32_t nReturn = madeEasy.accept_from_paymentbox(str_into_acct, str_indices, "ANY"); switch (nReturn) { case 0: OTLog::vOutput(0, "%s: This instrument was expired, so it was moved to the record box.\n", __FUNCTION__); return true; case 1: // success break; // ---------------------------------- default: OTLog::vError("%s: Error while trying to accept this instrument.\n", __FUNCTION__); return false; } // switch } // case: instrument break; // -------------------------------------------- default: OTLog::vError("%s: Unexpected type: %s\n", __FUNCTION__, this->GetInstrumentType().c_str()); return false; } return true; }
// For completed records (not pending.) // bool MTRecord::DeleteRecord() { if (!this->CanDeleteRecord()) return false; // ----------------------------- if (m_str_server_id.empty() || m_str_nym_id.empty()) { OTLog::vError("%s: Error: missing server id (%s) or nym id (%s)\n", __FUNCTION__, m_str_server_id.c_str(), m_str_nym_id.c_str()); return false; } // ------------------------------ std::string str_using_account; if ((MTRecord::Transfer == this->GetRecordType()) || (MTRecord::Receipt == this->GetRecordType())) { if (m_str_account_id.empty()) { OTLog::vError("%s: Error: missing account id for transfer or receipt.\n", __FUNCTION__); return false; } str_using_account = m_str_account_id; } else str_using_account = m_str_nym_id; // For instruments. // ------------------------------ switch (this->GetRecordType()) { // Delete from in-mail or out-mail. // case MTRecord::Mail: { if (m_bIsOutgoing) // outgoing mail { int32_t nCount = OTAPI_Wrap::GetNym_OutmailCount(m_str_nym_id); for (int32_t nIndex = 0; nIndex < nCount; ++nIndex) { const std::string str_contents(OTAPI_Wrap::GetNym_OutmailContentsByIndex(m_str_nym_id, nIndex)); if (str_contents == m_str_contents) // found it. { return OTAPI_Wrap::Nym_RemoveOutmailByIndex(m_str_nym_id, nIndex); } } } else // incoming mail { int32_t nCount = OTAPI_Wrap::GetNym_MailCount(m_str_nym_id); for (int32_t nIndex = 0; nIndex < nCount; ++nIndex) { const std::string str_contents(OTAPI_Wrap::GetNym_MailContentsByIndex(m_str_nym_id, nIndex)); if (str_contents == m_str_contents) // found it. { return OTAPI_Wrap::Nym_RemoveMailByIndex(m_str_nym_id, nIndex); } } } return false; } break; // -------------------------------------------- case MTRecord::Transfer: // Delete from asset account recordbox. case MTRecord::Receipt: // Delete from asset account recordbox. case MTRecord::Instrument: // Delete from Nym's recordbox. break; // -------------------------------------------- default: OTLog::vError("%s: Unexpected type: %s\n", __FUNCTION__, this->GetInstrumentType().c_str()); return false; } // -------------------------------------------- // The below section handles both the Nym's recordbox AND the Asset Account recordbox. // // ------------------------------ if (0 == m_lTransactionNum) { OTLog::vError("%s: Error: Transaction number is 0, in recordbox for server id (%s), nym id (%s), acct id ()\n", __FUNCTION__, m_str_server_id.c_str(), m_str_nym_id.c_str(), str_using_account.c_str()); return false; } // ------------------------------ const OTIdentifier theServerID(m_str_server_id), theNymID(m_str_nym_id), theAcctID(str_using_account); // this last one sometimes contains NymID (see above.) OTLedger * pRecordbox = OTAPI_Wrap::OTAPI()->LoadRecordBox(theServerID, theNymID, theAcctID); OTCleanup<OTLedger> theRecordBoxAngel(pRecordbox); // -------------------------------------------- if (NULL == pRecordbox) { OTLog::vError("%s: Failed loading record box for server ID (%s) nymID (%s) accountID (%s)\n", __FUNCTION__, m_str_server_id.c_str(), m_str_nym_id.c_str(), str_using_account.c_str()); return false; } // ------------------------------------------ // Find the receipt in the recordbox that correlates to this MTRecord. // int nIndex = pRecordbox->GetTransactionIndex(m_lTransactionNum); if ((-1) == nIndex) { OTLog::vError("%s: Error: Unable to find transaction %ld in recordbox for server id (%s), nym id (%s), acct id (%s)\n", __FUNCTION__, m_lTransactionNum, m_str_server_id.c_str(), m_str_nym_id.c_str(), str_using_account.c_str()); return false; } // ------------------------------------------ // Accept it. // return OTAPI_Wrap::ClearRecord(m_str_server_id, m_str_nym_id, str_using_account, nIndex, false); //clear all = false. We're only clearing one record. }