Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTPartyAccount.cpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTPartyAccount.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 "stdafx.hpp"
134 
135 #include "OTPartyAccount.hpp"
136 
137 #include "OTAccount.hpp"
138 #include "OTAgent.hpp"
139 #include "OTLog.hpp"
140 #include "OTParty.hpp"
141 #include "OTScript.hpp"
142 
143 // IDEA: Put a Nym in the Nyms folder for each entity. While it may
144 // not have a public key in the pubkey folder, or embedded within it,
145 // it can still have information about the entity or role related to it,
146 // which becomes accessible when that Nym is loaded based on the Entity ID.
147 // This also makes sure that Nyms and Entities don't ever share IDs, so the
148 // IDs become more and more interchangeable.
149 
150 namespace opentxs
151 {
152 
154  : m_pForParty(nullptr)
155  , m_pAccount(nullptr)
156  , m_lClosingTransNo(0)
157 {
158 }
159 
160 // For an account to be party to an agreement, there must be a closing
161 // transaction #
162 // provided, for the finalReceipt for that account.
163 //
164 OTPartyAccount::OTPartyAccount(std::string str_account_name,
165  const OTString& strAgentName,
166  OTAccount& theAccount, int64_t lClosingTransNo)
167  : m_pForParty(nullptr)
168  , // This gets set when this partyaccount is added to its party.
169  m_pAccount(&theAccount)
170  , m_lClosingTransNo(lClosingTransNo)
171  , m_strName(str_account_name.c_str())
172  , m_strAcctID(theAccount.GetRealAccountID())
173  , m_strAssetTypeID(theAccount.GetAssetTypeID())
174  , m_strAgentName(strAgentName)
175 {
176 }
177 
179  const OTString& strAgentName,
180  const OTString& strAcctID,
181  const OTString& strAssetTypeID,
182  int64_t lClosingTransNo)
183  : m_pForParty(nullptr)
184  , // This gets set when this partyaccount is added to its party.
185  m_pAccount(nullptr)
186  , m_lClosingTransNo(lClosingTransNo)
187  , m_strName(strName)
188  , m_strAcctID(strAcctID)
189  , m_strAssetTypeID(strAssetTypeID)
190  , m_strAgentName(strAgentName)
191 {
192 }
193 
194 // Every partyaccount has its own authorized agent's name.
195 // Use that name to look up the agent ON THE PARTY (I already
196 // have a pointer to my owner party.)
197 //
199 {
200  OT_ASSERT(nullptr != m_pForParty);
201 
202  if (!m_strAgentName.Exists()) {
203  otErr << "OTPartyAccount::GetAuthorizedAgent: Error: Authorized agent "
204  "name (for this account) is blank!\n";
205  return nullptr;
206  }
207 
208  const std::string str_agent_name = m_strAgentName.Get();
209 
210  OTAgent* pAgent = m_pForParty->GetAgent(str_agent_name);
211 
212  return pAgent;
213 }
214 
215 // This happens when the partyaccount is added to the party.
216 //
217 void OTPartyAccount::SetParty(OTParty& theOwnerParty)
218 {
219  m_pForParty = &theOwnerParty;
220 }
221 
223 {
224  // m_pForParty and m_pAccount NOT cleaned up here. pointer is only for
225  // convenience.
226  m_pForParty = nullptr;
227  m_pAccount = nullptr;
228 }
229 
230 bool OTPartyAccount::IsAccountByID(const OTIdentifier& theAcctID) const
231 {
232  if (!m_strAcctID.Exists()) {
233  return false;
234  }
235 
236  if (!m_strAssetTypeID.Exists()) {
237  return false;
238  }
239 
240  const OTIdentifier theMemberAcctID(m_strAcctID);
241  if (!(theAcctID == theMemberAcctID)) {
242  OTString strRHS(theAcctID);
243  otLog4 << "OTPartyAccount::IsAccountByID: Account IDs don't match: "
244  << m_strAcctID << " / " << strRHS << " \n";
245  // I set output to 4 because it's normal to call IsAccountByID() even
246  // when they don't match.
247  return false;
248  }
249 
250  // They match!
251 
252  return true;
253 }
254 
256 {
257  if (!m_strAcctID.Exists()) {
258  otErr << "OTPartyAccount::IsAccount: Error: Empty m_strAcctID.\n";
259  return false;
260  }
261 
262  if (!m_strAssetTypeID.Exists()) {
263  otErr << "OTPartyAccount::IsAccount: Error: Empty m_strAssetTypeID.\n";
264  return false;
265  }
266 
267  const OTIdentifier theAcctID(m_strAcctID);
268  if (!(theAccount.GetRealAccountID() == theAcctID)) {
269  OTString strRHS(theAccount.GetRealAccountID());
270  otLog4 << "OTPartyAccount::IsAccount: Account IDs don't match: "
271  << m_strAcctID << " / " << strRHS
272  << " \n"; // I set output to 4 because it's normal to call
273  // IsAccount() even when they don't match.
274  return false;
275  }
276 
277  const OTIdentifier theAssetTypeID(m_strAssetTypeID);
278  if (!(theAccount.GetAssetTypeID() == theAssetTypeID)) {
279  OTString strRHS(theAccount.GetAssetTypeID());
280  otOut << "OTPartyAccount::IsAccount: Asset Type IDs don't match ( "
281  << m_strAssetTypeID << " / " << strRHS
282  << " ) for Acct ID: " << m_strAcctID << " \n";
283  return false;
284  }
285 
286  m_pAccount = &theAccount;
287  return true;
288 }
289 
290 // I have a ptr to my owner (party), as well as to the actual account.
291 // I will ask him to verify whether he actually owns it.
293 {
294  if (nullptr == m_pForParty) {
295  otErr << "OTPartyAccount::VerifyOwnership: Error: nullptr pointer to "
296  "owner party. \n";
297  return false;
298  }
299  if (nullptr == m_pAccount) {
300  otErr << "OTPartyAccount::VerifyOwnership: Error: nullptr pointer to "
301  "account. (This function expects account to already be "
302  "loaded.) \n";
303  return false;
304  } // todo maybe turn the above into OT_ASSERT()s.
305 
306  if (!m_pForParty->VerifyOwnershipOfAccount(*m_pAccount)) {
307  otOut << "OTPartyAccount::VerifyOwnership: Party %s doesn't verify as "
308  "the ACTUAL owner of account: " << m_strName << " \n";
309  return false;
310  }
311 
312  return true;
313 }
314 
315 // I can get a ptr to my agent, and I have one to the actual account.
316 // I will ask him to verify whether he actually has agency over it.
318 {
319  if (nullptr == m_pAccount) {
320  otErr << "OTPartyAccount::VerifyAgency: Error: nullptr pointer to "
321  "account. (This function expects account to already be "
322  "loaded.) \n";
323  return false;
324  } // todo maybe turn the above into OT_ASSERT()s.
325 
326  OTAgent* pAgent = GetAuthorizedAgent();
327 
328  if (nullptr == pAgent) {
329  otOut
330  << "OTPartyAccount::VerifyAgency: Unable to find authorized agent ("
331  << GetAgentName() << ") for this account: " << GetName() << " \n";
332  return false;
333  }
334 
335  if (!pAgent->VerifyAgencyOfAccount(*m_pAccount)) {
336  otOut << "OTPartyAccount::VerifyAgency: Agent " << GetAgentName()
337  << " doesn't verify as ACTUALLY having rights over account "
338  << GetName() << " with ID: " << GetAcctID() << " \n";
339  return false;
340  }
341 
342  return true;
343 }
344 
346  mapOfNyms* pNymMap, const OTString& strServerID, OTPseudonym& theServerNym,
347  OTSmartContract& theSmartContract, const int64_t& lNewTransactionNumber,
348  const OTString& strOrigCronItem, OTString* pstrNote,
349  OTString* pstrAttachment)
350 {
351  const char* szFunc = "OTPartyAccount::DropFinalReceiptToInbox";
352 
353  if (nullptr == m_pForParty) {
354  otErr << szFunc << ": nullptr m_pForParty.\n";
355  return false;
356  }
357  else if (!m_strAcctID.Exists()) {
358  otErr << szFunc << ": Empty Acct ID.\n";
359  return false;
360  }
361  else if (!m_strAgentName.Exists()) {
362  otErr << szFunc << ": No agent named for this account.\n";
363  return false;
364  }
365 
366  // TODO: When entites and roles are added, this function may change a bit to
367  // accommodate them.
368 
369  const std::string str_agent_name(m_strAgentName.Get());
370 
371  OTAgent* pAgent = m_pForParty->GetAgent(str_agent_name);
372 
373  if (nullptr == pAgent)
374  otErr << szFunc << ": named agent wasn't found on party.\n";
375  else {
376  const OTIdentifier theAccountID(m_strAcctID);
377 
378  return pAgent->DropFinalReceiptToInbox(
379  pNymMap, strServerID, theServerNym, theSmartContract,
380  theAccountID, // acct ID from this.
381  lNewTransactionNumber, m_lClosingTransNo, // closing_no from this.
382  strOrigCronItem, pstrNote, pstrAttachment);
383  }
384 
385  return false;
386 }
387 
388 // CALLER IS RESPONSIBLE TO DELETE.
389 // This is very low-level. (It's better to use OTPartyAccount through it's
390 // interface, than to
391 // just load up its account directly.) But this is here because it is
392 // appropriate in certain cases.
393 //
395  const OTString& strServerID)
396 {
397  if (!m_strAcctID.Exists()) {
398  otOut << "OTPartyAccount::LoadAccount: Bad: Acct ID is blank for "
399  "account: " << m_strName << " \n";
400  return nullptr;
401  }
402 
403  const OTIdentifier theAcctID(m_strAcctID), theServerID(strServerID);
404 
405  OTAccount* pAccount =
406  OTAccount::LoadExistingAccount(theAcctID, theServerID);
407 
408  if (nullptr == pAccount) {
409  otOut << "OTPartyAccount::LoadAccount: Failed trying to load account: "
410  << m_strName << ", with AcctID: " << m_strAcctID << " \n";
411  return nullptr;
412  }
413  // BELOW THIS POINT, You must delete pAccount if you don't return it!!
414  //
415  else if (!pAccount->VerifyAccount(theSignerNym)) {
416  otOut
417  << "OTPartyAccount::LoadAccount: Failed trying to verify account: "
418  << m_strName << ", with AcctID: " << m_strAcctID << " \n";
419  delete pAccount;
420  return nullptr;
421  }
422 
423  // This compares asset type ID, AND account ID on the actual loaded account,
424  // to what is expected.
425  else if (!IsAccount(*pAccount)) // It also sets the internal pointer
426  // m_pAccount... FYI.
427  {
428  // IsAccount has plenty of logging already.
429  delete pAccount;
430  return nullptr;
431  }
432  // BELOW THIS POINT, pAccount is loaded and validated, in-and-of-itself, and
433  // against the PartyAcct.
434  // (But not against the party ownership and agent rights.)
435  // It must be deleted or will leak.
436 
437  // (No need to set m_pAccount, as that happened already in IsAccount().)
438 
439  return pAccount;
440 }
441 
442 void OTPartyAccount::Serialize(OTString& strAppend, bool bCalculatingID,
443  bool bSpecifyAssetID) const
444 {
445  strAppend.Concatenate(
446  "<assetAccount\n name=\"%s\"\n"
447  " acctID=\"%s\"\n"
448  " assetTypeID=\"%s\"\n"
449  " agentName=\"%s\"\n"
450  " closingTransNo=\"%lld\" />\n\n",
451  m_strName.Get(), bCalculatingID ? "" : m_strAcctID.Get(),
452  (bCalculatingID && !bSpecifyAssetID) ? "" : m_strAssetTypeID.Get(),
453  bCalculatingID ? "" : m_strAgentName.Get(),
454  bCalculatingID ? 0 : m_lClosingTransNo);
455 }
456 
458 {
459  const std::string str_acct_name = m_strName.Get();
460  theScript.AddAccount(str_acct_name, *this);
461 }
462 
463 // Done
465 {
466  if (!(GetName().Compare(rhs.GetName()))) {
467  otOut << "OTPartyAccount::Compare: Names don't match: " << GetName()
468  << " / " << rhs.GetName() << " \n";
469  return false;
470  }
471 
472  if ((GetClosingTransNo() > 0) && (rhs.GetClosingTransNo() > 0) &&
473  (GetClosingTransNo() != rhs.GetClosingTransNo())) {
474  otOut << "OTPartyAccount::Compare: Closing transaction numbers don't "
475  "match: " << GetName() << " \n";
476  return false;
477  }
478 
479  if ((GetAcctID().Exists()) && (rhs.GetAcctID().Exists()) &&
480  (!GetAcctID().Compare(rhs.GetAcctID()))) {
481  otOut << "OTPartyAccount::Compare: Asset account numbers don't match "
482  "for party account " << GetName() << ".\n( " << GetAcctID()
483  << " / " << rhs.GetAcctID() << " ) \n";
484  return false;
485  }
486 
487  if ((GetAgentName().Exists()) && (rhs.GetAgentName().Exists()) &&
488  (!GetAgentName().Compare(rhs.GetAgentName()))) {
489  otOut << "OTPartyAccount::Compare: Agent names don't match for party "
490  "account " << GetName() << ".\n( " << GetAgentName() << " / "
491  << rhs.GetAgentName() << " ) \n";
492  return false;
493  }
494 
495  if (!(GetAssetTypeID().Exists()) || !(rhs.GetAssetTypeID().Exists()) ||
496  !(GetAssetTypeID().Compare(rhs.GetAssetTypeID()))) {
497  otOut << "OTPartyAccount::Compare: Asset Type IDs don't exist, or "
498  "don't match ( " << GetAssetTypeID() << " / "
499  << rhs.GetAssetTypeID() << " ) for party's account: " << GetName()
500  << " \n";
501  return false;
502  }
503 
504  return true;
505 }
506 
507 } // namespace opentxs
OTLOG_IMPORT OTLogStream otLog4
void Serialize(OTString &strAppend, bool bCalculatingID=false, bool bSpecifyAssetID=false) const
EXPORT OTAgent * GetAgent(const std::string &str_agent_name) const
Definition: OTParty.cpp:609
EXPORT const OTIdentifier & GetAssetTypeID() const
Definition: OTAccount.cpp:449
EXPORT const OTString & GetName() const
OTLOG_IMPORT OTLogStream otOut
std::map< std::string, OTPseudonym * > mapOfNyms
Definition: OTWallet.hpp:161
bool VerifyOwnershipOfAccount(const OTAccount &theAccount) const
Definition: OTParty.cpp:934
EXPORT void Concatenate(const char *arg,...)
Definition: OTString.cpp:1334
EXPORT OTAgent * GetAuthorizedAgent()
EXPORT bool Exists() const
Definition: OTString.cpp:1035
const OTIdentifier & GetRealAccountID() const
EXPORT bool Compare(const char *compare) const
Definition: OTString.cpp:1102
void AddAccount(std::string str_acct_name, OTPartyAccount &theAcct)
Definition: OTScript.cpp:363
bool Compare(const OTPartyAccount &rhs) const
#define OT_ASSERT(x)
Definition: Assert.hpp:150
const OTString & GetAgentName() const
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
virtual bool VerifyAccount(const OTPseudonym &theNym)
const OTString & GetAcctID() const
EXPORT void RegisterForExecution(OTScript &theScript)
bool VerifyAgencyOfAccount(const OTAccount &theAccount) const
Definition: OTAgent.cpp:699
bool DropFinalReceiptToInbox(mapOfNyms *pNymMap, const OTString &strServerID, OTPseudonym &theServerNym, OTSmartContract &theSmartContract, const int64_t &lNewTransactionNumber, const OTString &strOrigCronItem, OTString *pstrNote=nullptr, OTString *pstrAttachment=nullptr)
const OTString & GetAssetTypeID() const
bool IsAccountByID(const OTIdentifier &theAcctID) const
EXPORT bool Exists(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:584
int64_t GetClosingTransNo() const
bool IsAccount(OTAccount &theAccount)
void SetParty(OTParty &theOwnerParty)
OTAccount * LoadAccount(OTPseudonym &theSignerNym, const OTString &strServerID)
static EXPORT OTAccount * LoadExistingAccount(const OTIdentifier &accountId, const OTIdentifier &serverId)
Definition: OTAccount.cpp:480