Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTTransactionType.cpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTTransactionType.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 "OTTransactionType.hpp"
136 
137 #include "OTAccount.hpp"
138 #include "OTLedger.hpp"
139 #include "OTLog.hpp"
140 
141 // static -- class factory.
142 //
143 
144 namespace opentxs
145 {
146 
148 {
149  OTString strContract, strFirstLine; // output for the below function.
150  const bool bProcessed =
151  OTContract::DearmorAndTrim(strInput, strContract, strFirstLine);
152 
153  if (bProcessed) {
154  OTTransactionType* pContract = nullptr;
155 
156  if (strFirstLine.Contains(
157  "-----BEGIN SIGNED TRANSACTION-----")) // this string is 34
158  // chars long.
159  {
160  pContract = new OTTransaction();
161  OT_ASSERT(nullptr != pContract);
162  }
163  else if (strFirstLine.Contains(
164  "-----BEGIN SIGNED TRANSACTION ITEM-----")) // this
165  // string is
166  // 39 chars
167  // long.
168  {
169  pContract = new OTItem();
170  OT_ASSERT(nullptr != pContract);
171  }
172  else if (strFirstLine.Contains(
173  "-----BEGIN SIGNED LEDGER-----")) // this string is 29
174  // chars long.
175  {
176  pContract = new OTLedger();
177  OT_ASSERT(nullptr != pContract);
178  }
179  else if (strFirstLine.Contains(
180  "-----BEGIN SIGNED ACCOUNT-----")) // this string is 30
181  // chars long.
182  {
183  pContract = new OTAccount();
184  OT_ASSERT(nullptr != pContract);
185  }
186 
187  // The string didn't match any of the options in the factory.
188  //
189 
190  const char* szFunc = "OTTransactionType::TransactionFactory";
191  // The string didn't match any of the options in the factory.
192  if (nullptr == pContract) {
193  otOut << szFunc
194  << ": Object type not yet supported by class factory: "
195  << strFirstLine << "\n";
196  return nullptr;
197  }
198 
199  // This causes pItem to load ASSUMING that the PurportedAcctID and
200  // PurportedServerID are correct.
201  // The object is still expected to be internally consistent with its
202  // sub-items, regarding those IDs,
203  // but the big difference is that it will SET the Real Acct and Real
204  // Server IDs based on the purported
205  // values. This way you can load a transaction without knowing the
206  // account in advance.
207  //
208  pContract->m_bLoadSecurely = false;
209 
210  // Does the contract successfully load from the string passed in?
211  if (pContract->LoadContractFromString(strContract)) {
212  // NOTE: this already happens in OTTransaction::ProcessXMLNode and
213  // OTLedger::ProcessXMLNode.
214  // Specifically, it happens when m_bLoadSecurely is set to false.
215  //
216  // pContract->SetRealServerID(pItem->GetPurportedServerID());
217  // pContract->SetRealAccountID(pItem->GetPurportedAccountID());
218  //
219  return pContract;
220  }
221  else {
222  otOut << szFunc
223  << ": Failed loading contract from string (first line): "
224  << strFirstLine << "\n";
225  delete pContract;
226  pContract = nullptr;
227  }
228  }
229 
230  return nullptr;
231 }
232 
234 {
235  theOutput.Release();
236  theOutput.Add(m_Numlist);
237 }
238 
239 // Allows you to string-search the raw contract.
240 bool OTTransactionType::Contains(const OTString& strContains)
241 {
242  return m_strRawFile.Contains(strContains);
243 }
244 
245 // Allows you to string-search the raw contract.
246 bool OTTransactionType::Contains(const char* szContains)
247 {
248  return m_strRawFile.Contains(szContains);
249 }
250 
251 // keeping constructor private in order to force people to use the other
252 // constructors and
253 // therefore provide the requisite IDs.
255  : OTContract()
256  , m_lTransactionNum(0)
257  , m_lInReferenceToTransaction(0)
258  , m_lNumberOfOrigin(0)
259  , m_bLoadSecurely(true)
260 {
261  // this function is private to prevent people from using it.
262  // Should never actually get called.
263 
264  // InitTransactionType(); // Just in case.
265 }
266 
268  const OTIdentifier& theAccountID,
269  const OTIdentifier& theServerID)
270  : OTContract(theAccountID)
271  , m_ServerID(theServerID)
272  , m_AcctUserID(theUserID)
273  , m_lTransactionNum(0)
274  , m_lInReferenceToTransaction(0)
275  , m_lNumberOfOrigin(0)
276  , m_bLoadSecurely(true)
277 {
278  // InitTransactionType();
279 
280  // m_ID = theAccountID -- This happens in OTContract, no need
281  // to do it twice.
282 
283  // do NOT set m_AcctID and m_AcctServerID here. Let the child classes LOAD
284  // them or GENERATE them.
285 }
286 
288  const OTIdentifier& theAccountID,
289  const OTIdentifier& theServerID,
290  int64_t lTransactionNum)
291  : OTContract(theAccountID)
292  , m_ServerID(theServerID)
293  , m_AcctUserID(theUserID)
294  , m_lTransactionNum(lTransactionNum)
295  , m_lInReferenceToTransaction(0)
296  , m_lNumberOfOrigin(0)
297  , m_bLoadSecurely(true)
298 {
299  // This initializes m_lTransactionNum, so it must come FIRST.
300  // In fact, that's the general rule with this function.
301  // InitTransactionType();
302 
303  // m_ID = theAccountID -- This happens in OTContract, no
304  // need to do it twice.
305 
306  // do NOT set m_AcctID and m_AcctServerID here. Let the child classes LOAD
307  // them or GENERATE them.
308 }
309 
310 // Note: can probably remove this function entirely...
312 {
313  m_lTransactionNum = 0;
315  m_lNumberOfOrigin = 0;
316 }
317 
319 {
321 }
322 
323 // We'll see if any new bugs pop up after adding this...
324 //
326 {
327  // If there were any dynamically allocated objects, clean them up here.
328 
329  // m_ID.Release();
330  m_AcctID.Release(); // Compare m_AcctID to m_ID after loading it from string
331  // or file. They should match, and signature should
332  // verify.
333 
334  // m_ServerID.Release(); // Server ID as used to instantiate the
335  // transaction, based on expected ServerID.
336  m_AcctServerID.Release(); // Actual ServerID within the signed portion.
337  // (Compare to m_ServerID upon loading.)
338 
339  // m_AcctUserID.Release();
340 
341  m_lTransactionNum = 0;
343  m_lNumberOfOrigin = 0;
344 
345  m_ascInReferenceTo.Release(); // This item may be in reference to a
346  // different item
347 
348  // This was causing OTLedger to fail loading. Can't set this to true until
349  // the END
350  // of loading. Todo: Starting reading the END TAGS during load. For example,
351  // the OTLedger
352  // END TAG could set this back to true...
353  //
354  // m_bLoadSecurely = true; // defaults to true.
355 
356  m_Numlist.Release();
357 }
358 
360 {
362 
363  ot_super::Release(); // since I've overridden the base class, I call it
364  // now...
365 }
366 
367 // OTAccount, OTTransaction, OTItem, and OTLedger are all derived from
368 // this class (OTTransactionType). Therefore they can all quickly identify
369 // whether one of the other components belongs to the same account, using
370 // this method.
371 //
373 {
374  if ((GetUserID() != rhs.GetUserID()) ||
375  (GetRealAccountID() != rhs.GetRealAccountID()) ||
376  (GetRealServerID() != rhs.GetRealServerID()))
377  return false;
378  return true;
379 }
380 
382 {
384 }
385 
387 {
389 }
390 
391 bool OTTransactionType::SaveContractWallet(std::ofstream&) const
392 {
393  return true;
394 }
395 
396 // Make sure this contract checks out. Very high level.
397 // Verifies ID and signature.
398 // I do NOT call VerifyOwner() here, because the server may
399 // wish to verify its signature on this account, even though
400 // the server may not be the actual owner.
401 // So if you wish to VerifyOwner(), then call it.
403 {
404  // Make sure that the supposed AcctID matches the one read from the file.
405  //
406  if (!VerifyContractID()) {
407  otErr << "Error verifying account ID in "
408  "OTTransactionType::VerifyAccount\n";
409  return false;
410  }
411  else if (!VerifySignature(theNym)) {
412  otErr << "Error verifying signature in "
413  "OTTransactionType::VerifyAccount.\n";
414  return false;
415  }
416 
417  otLog4 << "\nWe now know that...\n"
418  "1) The expected Account ID matches the ID that was found on the "
419  "object.\n"
420  "2) The SIGNATURE VERIFIED on the object.\n\n";
421  return true;
422 }
423 
425 {
426  // m_AcctID contains the number we read from the xml file
427  // we can compare it to the existing and actual identifier.
428 
429  // m_AcctID contains the "IDENTIFIER" of the object, according to the xml
430  // file.
431  //
432  // Meanwhile m_ID contains the same identifier, except it was generated.
433  //
434  // Now let's compare the two and make sure they match...
435 
436  // Also, for this class, we compare ServerID as well. They go hand in hand.
437 
438  if ((m_ID != m_AcctID) || (m_ServerID != m_AcctServerID)) {
439  OTString str1(m_ID), str2(m_AcctID), str3(m_ServerID),
440  str4(m_AcctServerID);
441  otErr << "Identifiers do NOT match in "
442  "OTTransactionType::VerifyContractID.\n"
443  "m_ID: " << str1 << "\n m_AcctID: " << str2
444  << "\n m_ServerID: " << str3 << "\n m_AcctServerID: " << str4
445  << "\n";
446 
447  // OT_FAIL; // I was debugging.
448 
449  return false;
450  }
451  else {
452  // OTString str1(m_AcctID), str2(m_AcctServerID);
453  // otErr << "Expected Account ID and Server ID both *SUCCESSFUL*
454  // match to "
455  // "IDs in the xml:\n Account ID:\n%s\n ServerID:\n%s\n"
456  // "-----------------------------------------------------------------------------\n",
457  // str1.Get(), str2.Get());
458  return true;
459  }
460 }
461 
462 // Need to know the transaction number of this transaction? Call this.
464 {
465  return m_lTransactionNum;
466 }
467 
468 void OTTransactionType::SetTransactionNum(int64_t lTransactionNum)
469 {
470  m_lTransactionNum = lTransactionNum;
471 }
472 
473 // virtual
475 {
477 }
478 
479 // Need to know the transaction number of the ORIGINAL transaction? Call this.
480 // virtual
482 {
484 
485  return m_lNumberOfOrigin;
486 }
487 
488 // Gets WITHOUT calculating.
490 {
491  return m_lNumberOfOrigin;
492 }
493 
494 void OTTransactionType::SetNumberOfOrigin(int64_t lTransactionNum)
495 {
496  m_lNumberOfOrigin = lTransactionNum;
497 }
498 
500 {
502 }
503 
504 // Allows you to compare any OTTransaction or OTItem to any other OTTransaction
505 // or OTItem,
506 // and see if they share the same origin number.
507 //
508 // Let's say Alice sends a transfer #100 to Bob.
509 // Then Bob receives a pending in his inbox, #800, which is in reference to
510 // #100.
511 // Then Bob accepts the pending with processInbox #45, which is in reference to
512 // #800.
513 // Then Alice receives a transferReceipt #64, which is in reference to #45.
514 // Then Alice accepts the transferReceipt with processInbox #91, in reference to
515 // #64.
516 //
517 // ALL OF THOSE transactions and receipts will have origin #100 attached to
518 // them.
519 //
521 {
522  // Have to use the function here, NOT the internal variable.
523  // (Because subclasses may override the function.)
524  //
525  return (GetNumberOfOrigin() == compareTo.GetNumberOfOrigin());
526 }
527 
528 // Need to know the transaction number that this is in reference to? Call this.
530 {
532 }
533 
534 void OTTransactionType::SetReferenceToNum(int64_t lTransactionNum)
535 {
536  m_lInReferenceToTransaction = lTransactionNum;
537 }
538 
539 } // namespace opentxs
EXPORT void GetReferenceString(OTString &theStr) const
OTLOG_IMPORT OTLogStream otLog4
virtual EXPORT void CalculateNumberOfOrigin()
EXPORT void SetNumberOfOrigin(int64_t lTransactionNum)
static EXPORT bool DearmorAndTrim(const OTString &strInput, OTString &strOutput, OTString &strFirstLine)
Definition: OTContract.cpp:158
void SetTransactionNum(int64_t lTransactionNum)
EXPORT void Release()
Definition: OTNumList.cpp:465
static EXPORT OTTransactionType * TransactionFactory(OTString strInput)
bool IsSameAccount(const OTTransactionType &rhs) const
OTLOG_IMPORT OTLogStream otOut
EXPORT void GetNumList(OTNumList &theOutput)
virtual bool SaveContractWallet(std::ofstream &ofs) const
EXPORT bool SetString(const OTString &theData, bool bLineBreaks=true)
virtual EXPORT int64_t GetNumberOfOrigin()
const OTIdentifier & GetRealAccountID() const
EXPORT void SetReferenceString(const OTString &theStr)
#define OT_ASSERT(x)
Definition: Assert.hpp:150
OTIdentifier m_ID
Definition: OTContract.hpp:172
EXPORT int64_t GetReferenceToNum() const
OTLOG_IMPORT OTLogStream otErr
virtual bool VerifyAccount(const OTPseudonym &theNym)
EXPORT int64_t GetTransactionNum() const
EXPORT void SetReferenceToNum(int64_t lTransactionNum)
virtual EXPORT void Release()
Definition: OTContract.cpp:277
virtual EXPORT bool VerifySignature(const OTPseudonym &theNym, const OTPasswordData *pPWData=nullptr) const
Definition: OTContract.cpp:818
virtual EXPORT bool VerifyContractID() const
EXPORT bool Contains(const char *compare) const
Definition: OTString.cpp:1137
const OTIdentifier & GetUserID() const
bool Contains(const OTString &strContains)
EXPORT bool GetString(OTString &theData, bool bLineBreaks=true) const
EXPORT bool LoadContractFromString(const OTString &theStr)
virtual EXPORT void Release()
Definition: OTData.cpp:257
virtual EXPORT void Release()
Definition: OTString.cpp:765
const OTIdentifier & GetRealServerID() const
EXPORT bool VerifyNumberOfOrigin(OTTransactionType &compareTo)
EXPORT int64_t GetRawNumberOfOrigin() const