Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTLedger.hpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTLedger.hpp
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 #ifndef OPENTXS_CORE_OTLEDGER_HPP
134 #define OPENTXS_CORE_OTLEDGER_HPP
135 
136 #include "OTTransaction.hpp"
137 
138 namespace opentxs
139 {
140 
141 class OTAccount;
142 class OTCheque;
143 class OTIdentifier;
144 class OTItem;
145 class OTPseudonym;
146 class OTString;
147 
148 // transaction ID is a int64_t, assigned by the server. Each transaction has
149 // one.
150 // FIRST the server issues the ID. THEN we create the blank transaction object
151 // with the
152 // ID in it and store it in our inbox. THEN if we want to send a transaction, we
153 // use
154 // the blank to do so. If there is no blank available, we message the server and
155 // request one.
156 
157 typedef std::map<int64_t, OTTransaction*> mapOfTransactions;
158 
159 // the "inbox" and "outbox" functionality is implemented in this class
161 {
162 private: // Private prevents erroneous use by other classes.
163  typedef OTTransactionType ot_super;
164 
166  OTString strInput);
167 
168 private:
169  mapOfTransactions m_mapTransactions; // a ledger contains a map of
170  // transactions.
171 
172 protected:
173  // return -1 if error, 0 if nothing, and 1 if the node was processed.
174  virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader*& xml);
175  virtual void UpdateContents(); // Before transmission or serialization, this
176  // is where the ledger saves its contents
177 
178  OTLedger(); // Hopefully stays here.
179 
180 public:
181  enum ledgerType {
182  nymbox, // the nymbox is per user account (versus per asset account) and
183  // is used to receive new transaction numbers (and messages.)
184  inbox, // each asset account has an inbox, with pending transfers as
185  // well as receipts inside.
186  outbox, // if you SEND a pending transfer, it sits in your outbox until
187  // it's accepted, rejected, or canceled.
188  message, // used in OTMessages, to send various lists of transactions
189  // back and forth.
190  paymentInbox, // Used for client-side-only storage of incoming cheques,
191  // invoices, payment plan requests, etc. (Coming in from
192  // the Nymbox.)
193  recordBox, // Used for client-side-only storage of completed items from
194  // the inbox, and the paymentInbox.
195  expiredBox, // Used for client-side-only storage of expired items from
196  // the paymentInbox.
198  }; // If you add any types to this list, update the list of strings at the
199  // top of the .CPP file.
201 
202  bool m_bLoadedLegacyData; // So the server can tell if it just loaded a
203  // legacy box or a hashed box. (Legacy boxes
204  // stored ALL of the receipts IN the box. No
205  // more.)
206 
207 protected:
208  bool LoadGeneric(ledgerType theType, const OTString* pString = nullptr);
209  bool SaveGeneric(ledgerType theType);
210 
211 public:
212  inline ledgerType GetType() const
213  {
214  return m_Type;
215  }
216 
217  EXPORT bool LoadedLegacyData() const
218  {
219  return m_bLoadedLegacyData;
220  }
221 
222  // This function assumes that this is an INBOX.
223  // If you don't use an INBOX to call this method, then it will return
224  // nullptr
225  // immediately.
226  // If you DO use an inbox, then it will create a balanceStatement item to go
227  // onto your
228  // transaction. (Transactions require balance statements. And when you get
229  // the atBalanceStatement
230  // reply from the server, KEEP THAT RECEIPT. Well, OT will do that for you.)
231  // You only have to keep the latest receipt, unlike systems that don't store
232  // balance
233  // agreement. We also store a list of issued transactions, the new balance,
234  // and the outbox hash.
235  EXPORT OTItem* GenerateBalanceStatement(int64_t lAdjustment,
236  const OTTransaction& theOwner,
237  OTPseudonym& theNym,
238  const OTAccount& theAccount,
239  OTLedger& theOutbox);
240 
241  EXPORT void ProduceOutboxReport(OTItem& theBalanceItem);
242 
243  EXPORT bool AddTransaction(OTTransaction& theTransaction);
244  EXPORT bool RemoveTransaction(int64_t lTransactionNum,
245  bool bDeleteIt = true); // if false,
246  // transaction wasn't
247  // found.
248 
251  EXPORT OTTransaction* GetTransaction(int64_t lTransactionNum) const;
252  EXPORT OTTransaction* GetTransactionByIndex(int32_t nIndex) const;
253  EXPORT OTTransaction* GetFinalReceipt(int64_t lReferenceNum);
254  EXPORT OTTransaction* GetTransferReceipt(int64_t lNumberOfOrigin);
256  int64_t lChequeNum,
257  OTCheque** ppChequeOut = nullptr); // CALLER RESPONSIBLE
258  // TO
259  // DELETE.
260  EXPORT int32_t GetTransactionIndex(int64_t lTransactionNum); // if not
261  // found,
262  // returns -1
263  EXPORT OTTransaction* GetReplyNotice(const int64_t& lRequestNum);
264 
265  // This calls OTTransactionType::VerifyAccount(), which calls
266  // VerifyContractID() as well as VerifySignature().
267  //
268  // But first, this OTLedger version also loads the box receipts,
269  // if doing so is appropriate. (message ledger == not appropriate.)
270  //
271  // Use this method instead of OTContract::VerifyContract, which
272  // expects/uses a pubkey from inside the contract in order to verify
273  // it.
274  //
275  EXPORT virtual bool VerifyAccount(const OTPseudonym& theNym);
276  // For ALL abbreviated transactions, load the actual box receipt for each.
277  EXPORT bool LoadBoxReceipts(std::set<int64_t>* psetUnloaded =
278  nullptr); // if psetUnloaded passed in, then
279  // use it to return the #s that
280  // weren't there.
281  EXPORT bool SaveBoxReceipts(); // For all "full version" transactions, save
282  // the actual box receipt for each.
283  // Verifies the abbreviated form exists first, and then loads the
284  // full version and compares the two. Returns success / fail.
285  //
286  EXPORT bool LoadBoxReceipt(const int64_t& lTransactionNum);
287  // Saves the Box Receipt separately.
288  EXPORT bool SaveBoxReceipt(const int64_t& lTransactionNum);
289  // "Deletes" it by adding MARKED_FOR_DELETION to the bottom of the file.
290  EXPORT bool DeleteBoxReceipt(const int64_t& lTransactionNum);
291  EXPORT bool LoadInbox();
292  EXPORT bool SaveInbox(OTIdentifier* pInboxHash = nullptr); // If you pass
293  // the
294  // identifier in,
295  // the hash is
296  // recorded there
297  EXPORT bool LoadNymbox();
298  EXPORT bool SaveNymbox(OTIdentifier* pNymboxHash = nullptr); // If you pass
299  // the
300  // identifier in,
301  // the hash is
302  // recorded there.
303  EXPORT bool LoadOutbox();
304  EXPORT bool SaveOutbox(OTIdentifier* pOutboxHash = nullptr); // If you pass
305  // the
306  // identifier in,
307  // the hash is
308  // recorded there
309 
310  EXPORT bool CalculateHash(OTIdentifier& theOutput);
311  EXPORT bool CalculateInboxHash(OTIdentifier& theOutput);
312  EXPORT bool CalculateOutboxHash(OTIdentifier& theOutput);
313  EXPORT bool CalculateNymboxHash(OTIdentifier& theOutput);
314  EXPORT bool SavePaymentInbox();
315  EXPORT bool LoadPaymentInbox();
316 
317  EXPORT bool SaveRecordBox();
318  EXPORT bool LoadRecordBox();
319 
320  EXPORT bool SaveExpiredBox();
321  EXPORT bool LoadExpiredBox();
322  EXPORT bool LoadLedgerFromString(const OTString& theStr); // Auto-detects
323  // ledger type.
324  // (message/nymbox/inbox/outbox)
325  EXPORT bool LoadInboxFromString(const OTString& strBox);
326  EXPORT bool LoadOutboxFromString(const OTString& strBox);
327  EXPORT bool LoadNymboxFromString(const OTString& strBox);
328  EXPORT bool LoadPaymentInboxFromString(const OTString& strBox);
329  EXPORT bool LoadRecordBoxFromString(const OTString& strBox);
330  EXPORT bool LoadExpiredBoxFromString(const OTString& strBox);
331  // inline for the top one only.
332  inline int32_t GetTransactionCount() const
333  {
334  return static_cast<int32_t>(m_mapTransactions.size());
335  }
336  EXPORT int32_t GetTransactionCountInRefTo(int64_t lReferenceNum) const;
337  EXPORT int64_t GetTotalPendingValue(); // for inbox only, allows you to
338  // lookup the total value of pending
339  // transfers within.
340  EXPORT const mapOfTransactions& GetTransactionMap() const;
341  EXPORT OTLedger(const OTIdentifier& theUserID,
342  const OTIdentifier& theAccountID,
343  const OTIdentifier& theServerID);
344  EXPORT virtual ~OTLedger();
345 
346  EXPORT virtual void Release();
347  EXPORT void Release_Ledger();
348 
349  EXPORT void ReleaseTransactions();
350  // ONLY call this if you need to load a ledger where you don't already know
351  // the person's UserID
352  // For example, if you need to load someone ELSE's inbox in order to send
353  // them a transfer, then
354  // you only know their account number, not their user ID. So you call this
355  // function to get it
356  // loaded up, and the UserID will hopefully be loaded up with the rest of
357  // it.
358  EXPORT OTLedger(const OTIdentifier& theAccountID,
359  const OTIdentifier& theServerID);
360  EXPORT void InitLedger();
361  EXPORT static OTLedger* GenerateLedger(const OTIdentifier& theUserID,
362  const OTIdentifier& theAcctID,
363  const OTIdentifier& theServerID,
364  ledgerType theType,
365  bool bCreateFile = false);
366 
367  EXPORT bool GenerateLedger(const OTIdentifier& theAcctID,
368  const OTIdentifier& theServerID,
369  ledgerType theType, bool bCreateFile = false);
370 
371  EXPORT virtual bool SaveContractWallet(std::ofstream& ofs) const;
372  EXPORT static char const* _GetTypeString(ledgerType theType);
373  EXPORT char const* GetTypeString() const
374  {
375  return _GetTypeString(m_Type);
376  }
377 };
378 
379 } // namespace opentxs
380 
381 #endif // OPENTXS_CORE_OTLEDGER_HPP
EXPORT bool LoadInboxFromString(const OTString &strBox)
Definition: OTLedger.cpp:487
EXPORT bool RemoveTransaction(int64_t lTransactionNum, bool bDeleteIt=true)
Definition: OTLedger.cpp:1168
static EXPORT char const * _GetTypeString(ledgerType theType)
Definition: OTLedger.cpp:171
EXPORT OTItem * GenerateBalanceStatement(int64_t lAdjustment, const OTTransaction &theOwner, OTPseudonym &theNym, const OTAccount &theAccount, OTLedger &theOutbox)
Definition: OTLedger.cpp:1532
int32_t GetTransactionCount() const
Definition: OTLedger.hpp:332
virtual EXPORT bool SaveContractWallet(std::ofstream &ofs) const
Definition: OTLedger.cpp:2476
EXPORT bool CalculateNymboxHash(OTIdentifier &theOutput)
Definition: OTLedger.cpp:790
EXPORT bool LoadBoxReceipt(const int64_t &lTransactionNum)
Definition: OTLedger.cpp:393
EXPORT OTTransaction * GetTransferReceipt(int64_t lNumberOfOrigin)
Definition: OTLedger.cpp:1328
EXPORT bool SaveOutbox(OTIdentifier *pOutboxHash=nullptr)
Definition: OTLedger.cpp:881
EXPORT bool LoadRecordBox()
Definition: OTLedger.cpp:507
EXPORT bool SaveBoxReceipts()
Definition: OTLedger.cpp:251
virtual EXPORT bool VerifyAccount(const OTPseudonym &theNym)
Definition: OTLedger.cpp:187
EXPORT bool DeleteBoxReceipt(const int64_t &lTransactionNum)
Definition: OTLedger.cpp:296
static EXPORT OTTransactionType * TransactionFactory(OTString strInput)
EXPORT char const * GetTypeString() const
Definition: OTLedger.hpp:373
bool m_bLoadedLegacyData
Definition: OTLedger.hpp:202
bool LoadGeneric(ledgerType theType, const OTString *pString=nullptr)
Definition: OTLedger.cpp:539
EXPORT void Release_Ledger()
Definition: OTLedger.cpp:2463
EXPORT bool LoadInbox()
Definition: OTLedger.cpp:463
bool SaveGeneric(ledgerType theType)
Definition: OTLedger.cpp:655
EXPORT bool SaveRecordBox()
Definition: OTLedger.cpp:924
EXPORT bool SaveBoxReceipt(const int64_t &lTransactionNum)
Definition: OTLedger.cpp:278
EXPORT bool AddTransaction(OTTransaction &theTransaction)
Definition: OTLedger.cpp:1194
EXPORT bool LoadOutbox()
Definition: OTLedger.cpp:477
EXPORT void ProduceOutboxReport(OTItem &theBalanceItem)
Definition: OTLedger.cpp:1723
virtual void UpdateContents()
Definition: OTLedger.cpp:1779
EXPORT bool CalculateOutboxHash(OTIdentifier &theOutput)
Definition: OTLedger.cpp:780
EXPORT int32_t GetTransactionCountInRefTo(int64_t lReferenceNum) const
Definition: OTLedger.cpp:1272
EXPORT bool LoadNymbox()
Definition: OTLedger.cpp:482
EXPORT bool SavePaymentInbox()
Definition: OTLedger.cpp:913
virtual EXPORT void Release()
Definition: OTLedger.cpp:2468
EXPORT OTTransaction * GetReplyNotice(const int64_t &lRequestNum)
Definition: OTLedger.cpp:1312
EXPORT void ReleaseTransactions()
Definition: OTLedger.cpp:2451
EXPORT bool LoadRecordBoxFromString(const OTString &strBox)
Definition: OTLedger.cpp:522
EXPORT int32_t GetTransactionIndex(int64_t lTransactionNum)
Definition: OTLedger.cpp:1230
ledgerType m_Type
Definition: OTLedger.hpp:200
virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader *&xml)
Definition: OTLedger.cpp:1922
EXPORT bool LoadOutboxFromString(const OTString &strBox)
Definition: OTLedger.cpp:492
EXPORT bool LoadLedgerFromString(const OTString &theStr)
Definition: OTLedger.cpp:1752
EXPORT bool SaveExpiredBox()
Definition: OTLedger.cpp:935
EXPORT bool CalculateHash(OTIdentifier &theOutput)
Definition: OTLedger.cpp:756
EXPORT bool LoadNymboxFromString(const OTString &strBox)
Definition: OTLedger.cpp:497
static EXPORT OTLedger * GenerateLedger(const OTIdentifier &theUserID, const OTIdentifier &theAcctID, const OTIdentifier &theServerID, ledgerType theType, bool bCreateFile=false)
Definition: OTLedger.cpp:946
EXPORT bool LoadedLegacyData() const
Definition: OTLedger.hpp:217
ledgerType GetType() const
Definition: OTLedger.hpp:212
EXPORT OTTransaction * GetTransactionByIndex(int32_t nIndex) const
Definition: OTLedger.cpp:1288
EXPORT bool LoadBoxReceipts(std::set< int64_t > *psetUnloaded=nullptr)
Definition: OTLedger.cpp:322
EXPORT bool SaveNymbox(OTIdentifier *pNymboxHash=nullptr)
Definition: OTLedger.cpp:801
EXPORT void InitLedger()
Definition: OTLedger.cpp:1105
EXPORT int64_t GetTotalPendingValue()
Definition: OTLedger.cpp:1695
virtual EXPORT ~OTLedger()
Definition: OTLedger.cpp:2446
EXPORT bool LoadExpiredBox()
Definition: OTLedger.cpp:512
EXPORT bool LoadPaymentInboxFromString(const OTString &strBox)
Definition: OTLedger.cpp:517
EXPORT OTTransaction * GetFinalReceipt(int64_t lReferenceNum)
Definition: OTLedger.cpp:1506
EXPORT bool LoadExpiredBoxFromString(const OTString &strBox)
Definition: OTLedger.cpp:527
EXPORT bool CalculateInboxHash(OTIdentifier &theOutput)
Definition: OTLedger.cpp:770
std::map< int64_t, OTTransaction * > mapOfTransactions
Definition: OTLedger.hpp:146
EXPORT OTTransaction * GetTransaction(OTTransaction::transactionType theType)
Definition: OTLedger.cpp:1215
EXPORT const mapOfTransactions & GetTransactionMap() const
Definition: OTLedger.cpp:1160
EXPORT bool SaveInbox(OTIdentifier *pInboxHash=nullptr)
Definition: OTLedger.cpp:836
EXPORT OTTransaction * GetChequeReceipt(int64_t lChequeNum, OTCheque **ppChequeOut=nullptr)
Definition: OTLedger.cpp:1402
EXPORT bool LoadPaymentInbox()
Definition: OTLedger.cpp:502