Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AccountList.cpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * AccountList.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 "AccountList.hpp"
136 #include "OTLog.hpp"
137 #include "OTMessage.hpp"
138 #include "OTStorage.hpp"
139 
140 #include <irrxml/irrXML.hpp>
141 
142 #include <string>
143 #include <utility>
144 
145 #ifndef _WIN32
146 #include <unistd.h>
147 #include <sys/time.h>
148 #endif
149 
150 using namespace irr;
151 using namespace io;
152 
153 namespace opentxs
154 {
155 
156 void TranslateAccountTypeToString(OTAccount::AccountType type,
157  OTString& acctType);
158 
159 OTAccount::AccountType TranslateAccountTypeStringToEnum(
160  const OTString& acctType);
161 
162 AccountList::AccountList()
163  : acctType_(OTAccount::voucher)
164 {
165 }
166 
168  : acctType_(acctType)
169 {
170 }
171 
173 {
175 }
176 
177 void AccountList::Serialize(OTString& append) const
178 {
179  OTString acctType;
180  TranslateAccountTypeToString(acctType_, acctType);
181 
182  append.Concatenate("<accountList type=\"%s\" count=\"%d\" >\n\n",
183  acctType.Get(), mapAcctIDs_.size());
184 
185  for (auto& it : mapAcctIDs_) {
186  std::string assetTypeId = it.first;
187  std::string accountId = it.second;
188  OT_ASSERT((assetTypeId.size() > 0) && (accountId.size() > 0));
189 
190  append.Concatenate(
191  "<accountEntry assetTypeID=\"%s\" accountID=\"%s\" />\n\n",
192  assetTypeId.c_str(), accountId.c_str());
193  }
194 
195  append.Concatenate("</accountList>\n\n");
196 }
197 
199  const OTString& acctType,
200  const OTString& acctCount)
201 {
202  if (!acctType.Exists()) {
203  otErr << "AccountList::ReadFromXMLNode: Failed: Empty accountList "
204  "'type' attribute.\n";
205  return -1;
206  }
207 
208  acctType_ = TranslateAccountTypeStringToEnum(acctType);
209 
210  if (OTAccount::err_acct == acctType_) {
211  otErr << "AccountList::ReadFromXMLNode: Failed: accountList 'type' "
212  "attribute contains unknown value.\n";
213  return -1;
214  }
215 
216  // Load up the account IDs.
217  int32_t count = acctCount.Exists() ? atoi(acctCount.Get()) : 0;
218  if (count > 0) {
219  while (count-- > 0) {
220  if (!OTContract::SkipToElement(xml)) {
221  otOut
222  << "AccountList::ReadFromXMLNode: Failure: Unable to find "
223  "expected element.\n";
224  return -1;
225  }
226 
227  if ((xml->getNodeType() == EXN_ELEMENT) &&
228  (!strcmp("accountEntry", xml->getNodeName()))) {
229  OTString assetTypeID = xml->getAttributeValue(
230  "assetTypeID"); // Asset Type ID of this account.
231  OTString accountID = xml->getAttributeValue(
232  "accountID"); // Account ID for this account.
233 
234  if (!assetTypeID.Exists() || !accountID.Exists()) {
235  otErr << "Error loading accountEntry: Either the "
236  "assetTypeID (" << assetTypeID
237  << "), or the accountID (" << accountID
238  << ") was EMPTY.\n";
239  return -1;
240  }
241 
242  mapAcctIDs_.insert(
243  std::make_pair(assetTypeID.Get(), accountID.Get()));
244  }
245  else {
246  otErr << "Expected accountEntry element in accountList.\n";
247  return -1;
248  }
249  }
250  }
251 
252  if (!OTContract::SkipAfterLoadingField(xml)) // </accountList>
253  {
254  otOut << "*** AccountList::ReadFromXMLNode: Bad data? Expected "
255  "EXN_ELEMENT_END here, but "
256  "didn't get it. Returning false.\n";
257  return -1;
258  }
259 
260  return 1;
261 }
262 
264 {
265  mapAcctIDs_.clear();
266  mapWeakAccts_.clear();
267 }
268 
270 {
272 }
273 
274 std::shared_ptr<OTAccount> AccountList::GetOrCreateAccount(
275  OTPseudonym& serverNym, const OTIdentifier& accountOwnerId,
276  const OTIdentifier& assetTypeId, const OTIdentifier& serverId,
277  // this will be set to true if the acct is created here.
278  // Otherwise set to false;
279  bool& wasAcctCreated, int64_t stashTransNum)
280 {
281  std::shared_ptr<OTAccount> account;
282  wasAcctCreated = false;
283 
284  if (OTAccount::stash == acctType_) {
285  if (stashTransNum <= 0) {
286  otErr
287  << "AccountList::GetOrCreateAccount: Failed attempt to create "
288  "stash account without cron item #.\n";
289  return account;
290  }
291  }
292 
293  // First, we'll see if there's already an account ID available for the
294  // requested asset type ID.
295  std::string assetTypeIdString = OTString(assetTypeId).Get();
296 
297  OTString acctTypeString;
298  TranslateAccountTypeToString(acctType_, acctTypeString);
299 
300  auto acctIDsIt = mapAcctIDs_.find(assetTypeIdString);
301  // Account ID *IS* already there for this asset type
302  if (mapAcctIDs_.end() != acctIDsIt) {
303  // grab account ID
304  std::string accountIdString = acctIDsIt->second;
305  auto weakIt = mapWeakAccts_.find(accountIdString);
306 
307  // FOUND the weak ptr to the account! Maybe it's already loaded
308  if (mapWeakAccts_.end() != weakIt) {
309  try {
310  std::shared_ptr<OTAccount> weakAccount(weakIt->second);
311 
312  // If success, then we have a shared pointer. But it's worrying
313  // (TODO) because this should have
314  // gone out of scope and been destroyed by whoever ELSE was
315  // using it. The fact that it's still here...
316  // well I'm glad not to double-load it, but I wonder why it's
317  // still here? And we aren't walking on anyone's
318  // toes, right? If this were multi-threaded, then I'd explicitly
319  // lock a mutex here, honestly. But since things
320  // happen one at a time on OT, I'll settle for a warning for
321  // now. I'm assuming that if the account's loaded
322  // already somewhere, it's just a pointer sitting there, and
323  // we're not walking on each other's toes.
324  if (weakAccount) {
325  otOut
326  << "AccountList::GetOrCreateAccount: Warning: account ("
327  << accountIdString
328  << ") was already in memory so I gave you a "
329  "pointer to the existing one. (But who else has a "
330  "copy of it?) \n";
331  return weakAccount;
332  }
333  }
334  catch (...) {
335  }
336 
337  // Though the weak pointer was there, the resource must have since
338  // been destroyed, because I cannot lock a new shared ptr onto it.
339  // Therefore remove it from the map, and RE-LOAD IT.
340  mapWeakAccts_.erase(weakIt);
341  }
342 
343  // DIDN'T find the acct pointer, even though we had the ID.
344  // (Or it was there, but we couldn't lock a shared_ptr onto it, so we
345  // erased it...)
346  // So let's load it now. After all, the Account ID *does* exist...
347  OTString acctIDString(accountIdString.c_str());
348  OTIdentifier accountID(acctIDString);
349 
350  // The Account ID exists, but we don't have the pointer to a loaded
351  // account for it. So, let's load it.
352  OTAccount* loadedAccount =
353  OTAccount::LoadExistingAccount(accountID, serverId);
354 
355  if (!loadedAccount) {
356  otErr << "Failed trying to load " << acctTypeString
357  << " account with account ID: " << acctIDString << '\n';
358  }
359  else if (!loadedAccount->VerifySignature(serverNym)) {
360  otErr << "Failed verifying server's signature on " << acctTypeString
361  << " account with account ID: " << acctIDString << '\n';
362  }
363  else if (!loadedAccount->VerifyOwnerByID(accountOwnerId)) {
364  OTString strOwnerID(accountOwnerId);
365  otErr << "Failed verifying owner ID (" << strOwnerID << ") on "
366  << acctTypeString << " account ID: " << acctIDString << '\n';
367  }
368  else {
369  otLog3 << "Successfully loaded " << acctTypeString
370  << " account ID: " << acctIDString
371  << " Asset Type ID: " << assetTypeIdString << "\n";
372 
373  account = std::shared_ptr<OTAccount>(loadedAccount);
374  // save a weak pointer to the acct, so we'll never load it twice,
375  // but we'll also know if it's been deleted.
376  mapWeakAccts_[acctIDString.Get()] =
377  std::weak_ptr<OTAccount>(account);
378  }
379  return account;
380  }
381 
382  // Not found. There's no account ID yet for that asset type ID. That means
383  // we can create it.
384  OTMessage message;
385  accountOwnerId.GetString(message.m_strNymID);
386  assetTypeId.GetString(message.m_strAssetID);
387  serverId.GetString(message.m_strServerID);
388 
389  OTAccount* createdAccount = OTAccount::GenerateNewAccount(
390  accountOwnerId, serverId, serverNym, message, acctType_, stashTransNum);
391 
392  if (!createdAccount) {
393  otErr << " AccountList::GetOrCreateAccount: Failed trying to generate"
394  << acctTypeString
395  << " account with asset type ID: " << assetTypeIdString << "\n";
396  }
397  else {
398  OTString acctIDString;
399  createdAccount->GetIdentifier(acctIDString);
400 
401  otOut << "Successfully created " << acctTypeString
402  << " account ID: " << acctIDString
403  << " Asset Type ID: " << assetTypeIdString << "\n";
404 
405  account = std::shared_ptr<OTAccount>(createdAccount);
406 
407  // save a weak pointer to the acct, so we'll never load it twice,
408  // but we'll also know if it's been deleted.
409  mapWeakAccts_[acctIDString.Get()] = std::weak_ptr<OTAccount>(account);
410  // Save the new acct ID in a map, keyed by asset type ID.
411  mapAcctIDs_[message.m_strAssetID.Get()] = acctIDString.Get();
412 
413  wasAcctCreated = true;
414  }
415 
416  return account;
417 }
418 
419 } // namespace opentxs
EXPORT bool VerifyOwnerByID(const OTIdentifier &nymId) const
Definition: OTAccount.cpp:472
static EXPORT OTAccount * GenerateNewAccount(const OTIdentifier &userId, const OTIdentifier &serverId, const OTPseudonym &serverNym, const OTMessage &message, AccountType acctType=simple, int64_t stashTransNum=0)
Definition: OTAccount.cpp:531
OTLOG_IMPORT OTLogStream otOut
OTString m_strAssetID
Definition: OTMessage.hpp:203
OTLOG_IMPORT OTLogStream otLog3
EXPORT std::shared_ptr< OTAccount > GetOrCreateAccount(OTPseudonym &serverNym, const OTIdentifier &ACCOUNT_OWNER_ID, const OTIdentifier &ASSET_TYPE_ID, const OTIdentifier &SERVER_ID, bool &wasAcctCreated, int64_t stashTransNum=0)
EXPORT int32_t ReadFromXMLNode(irr::io::IrrXMLReader *&xml, const OTString &acctType, const OTString &acctCount)
EXPORT void Concatenate(const char *arg,...)
Definition: OTString.cpp:1334
void TranslateAccountTypeToString(OTAccount::AccountType type, OTString &acctType)
Definition: OTAccount.cpp:697
EXPORT bool Exists() const
Definition: OTString.cpp:1035
OTAccount::AccountType TranslateAccountTypeStringToEnum(const OTString &acctType)
Definition: OTAccount.cpp:672
#define OT_ASSERT(x)
Definition: Assert.hpp:150
virtual EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
Definition: OTContract.cpp:317
OTString m_strServerID
Definition: OTMessage.hpp:191
EXPORT void Release()
EXPORT const char * Get() const
Definition: OTString.cpp:1045
static bool SkipAfterLoadingField(irr::io::IrrXMLReader *&xml)
static bool SkipToElement(irr::io::IrrXMLReader *&xml)
OTLOG_IMPORT OTLogStream otErr
EXPORT void Serialize(OTString &append) const
EXPORT void GetString(OTString &theStr) const
virtual EXPORT bool VerifySignature(const OTPseudonym &theNym, const OTPasswordData *pPWData=nullptr) const
Definition: OTContract.cpp:818
EXPORT void Release_AcctList()
static EXPORT OTAccount * LoadExistingAccount(const OTIdentifier &accountId, const OTIdentifier &serverId)
Definition: OTAccount.cpp:480