Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Purse.hpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * Purse.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_CASH_PURSE_HPP
134 #define OPENTXS_CASH_PURSE_HPP
135 
137 
138 #include <deque>
139 
140 namespace opentxs
141 {
142 
143 class OTASCIIArmor;
144 class OTNym_or_SymmetricKey;
145 class OTPassword;
146 class OTPseudonym;
147 class Token;
148 
149 // A token has no User ID, or Account ID, or even a traceable TokenID (the
150 // tokenID only becomes relevant
151 // after it is spent.)
152 // But a purse can be stuffed full of tokens, and can be saved by accountID as
153 // filename, and can have its
154 // contents encrypted to the public key of a specific user.
155 //
156 // I will add an optional UserID field, so it's obvious whose public key to use
157 // for opening the tokens.
158 // This may seem odd, but the field is entirely optional because it's not
159 // necessary for the actual operation.
160 // The recipient will already know to use his own private key to open the purse,
161 // and then he will immediately
162 // open it, redeem the coins, and store the replacements again encrypted to his
163 // own key, until he spends them
164 // again to someone else, when he will also know to encrypt the purse to THEIR
165 // public key, and so on.
166 //
167 // The interface of this class is that of a simple stack.
168 // Imagine a stack of poker chips.
169 
170 typedef std::deque<OTASCIIArmor*> dequeOfTokens;
171 
172 class Purse : public OTContract
173 {
174 private: // Private prevents erroneous use by other classes.
175  typedef OTContract ot_super;
176 
177 protected:
178  virtual void UpdateContents(); // Before transmission or serialization, this
179  // is where the Purse saves its contents
180 
182 
183  // Todo: Add a boolean value, so that the UserID is either for a real user,
184  // or is for a temp Nym
185  // which must be ATTACHED to the purse, if that boolean is set to true.
186 
187  OTIdentifier m_UserID; // Optional
188  OTIdentifier m_ServerID; // Mandatory
189  OTIdentifier m_AssetID; // Mandatory
190  int64_t m_lTotalValue; // Push increments this by denomination, and Pop
191  // decrements it by denomination.
192  bool m_bPasswordProtected; // this purse might be encrypted to a passphrase,
193  // instead of a Nym.
194  // If that's the case, BTW, then there will be a Symmetric Key and a Master
195  // Key.
196  // The symmetric key is used to store the actual key for
197  // encrypting/decrypting the tokens in this purse.
198  // Whereas the master key is used for retrieving the passphrase to use for
199  // unlocking the symmetric key.
200  // The passphrase in question is actually a random number stored inside the
201  // master key, inside its own
202  // internal symmetric key. In order to unlock it, OTCachedKey may
203  // occasionally ask the user to enter a
204  // passphrase, which is used to derived a key to unlock it. This key may
205  // then be cached in memory by
206  // OTCachedKey until a timeout, and later be zapped by a thread for that
207  // purpose.
208  bool m_bIsNymIDIncluded; // It's possible to use a purse WITHOUT attaching
209  // the relevant NymID. (The holder of the purse
210  // just has to "know" what the correct NymID is, or
211  // it won't work.) This bool tells us whether the
212  // ID is attached, or not.
213  OTSymmetricKey* m_pSymmetricKey; // If this purse contains its own symmetric
214  // key (instead of using an owner Nym)...
215 
216  // ...then it will have a master key as well, for unlocking that symmetric
217  // key, and managing timeouts, etc.
218  std::shared_ptr<OTCachedKey> m_pCachedKey;
219  time64_t m_tLatestValidFrom; // The tokens in the purse may become valid on
220  // different dates. This stores the latest one.
221  time64_t m_tEarliestValidTo; // The tokens in the purse may have different
222  // expirations. This stores the earliest one.
224  Purse(); // private
225 
226 public:
227  // OTPayment needs to be able to instantiate OTPurse without knowing the
228  // server ID
229  // in advance. I decided to add a factory for OTPurse to facilitate that.
230  EXPORT static Purse* PurseFactory(OTString strInput);
231  EXPORT static Purse* PurseFactory(OTString strInput,
232  const OTIdentifier& SERVER_ID);
233  EXPORT static Purse* PurseFactory(OTString strInput,
234  const OTIdentifier& SERVER_ID,
235  const OTIdentifier& ASSET_ID);
236  EXPORT static Purse* LowLevelInstantiate(const OTString& strFirstLine);
237  EXPORT static Purse* LowLevelInstantiate(const OTString& strFirstLine,
238  const OTIdentifier& SERVER_ID);
239  EXPORT static Purse* LowLevelInstantiate(const OTString& strFirstLine,
240  const OTIdentifier& SERVER_ID,
241  const OTIdentifier& ASSET_ID);
242  virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader*& xml);
243  // What if you DON'T want to encrypt the purse to your Nym??
244  // What if you just want to use a passphrase instead?
245  // That's what these functions are for. OT just generates
246  // an internal symmetric key and stores it INSIDE THE PURSE.
247  // You set the passphrase for the internal key, and thereafter
248  // your experience is one of a password-protected purse.
249  //
250  EXPORT bool GenerateInternalKey(); // Create internal symmetric key for
251  // password-protected purse.
253  {
254  return m_pSymmetricKey;
255  } // symmetric key for this purse.
256 
257  // stores the passphrase for the symmetric key.
258  EXPORT std::shared_ptr<OTCachedKey> GetInternalMaster();
259  EXPORT bool GetPassphrase(OTPassword& theOutput,
260  const char* szDisplay = nullptr); // Retrieves the
261  // passphrase
262  // for this
263  // purse (which is cached by the master
264  // key.) Prompts the user to enter his
265  // actual passphrase, if necessary to
266  // unlock it. (May not need unlocking
267  // yet -- there is a timeout.)
268  EXPORT bool IsNymIDIncluded() const
269  {
270  return m_bIsNymIDIncluded;
271  } // NymID may be left blank, with user left guessing.
272  EXPORT bool IsPasswordProtected() const
273  {
274  return m_bPasswordProtected;
275  }
276  // This will return false every time, if IsNymIDIncluded() is false.
277  EXPORT bool GetNymID(OTIdentifier& theOutput) const;
278  // FYI: OTPurse::Push makes its own copy of theToken and does NOT take
279  // ownership of the one passed in.
280  EXPORT bool Push(OTNym_or_SymmetricKey theOwner, const Token& theToken);
281  EXPORT Token* Pop(OTNym_or_SymmetricKey theOwner); // Caller IS
282  // responsible to
283  // delete. (Peek
284  EXPORT Token* Peek(OTNym_or_SymmetricKey theOwner) const; // Caller IS
285  // responsible
286  // to delete.
287  // (Peek returns
288  // a copy of the
289  // token.)
290  EXPORT int32_t Count() const;
291  EXPORT bool IsEmpty() const;
292  inline int64_t GetTotalValue() const
293  {
294  return m_lTotalValue;
295  }
296  EXPORT time64_t GetLatestValidFrom() const;
297  EXPORT time64_t GetEarliestValidTo() const;
298  // NOTE: Keep in mind that a purse's expiration dates are based on ALL the
299  // tokens within.
300  // Therefore this will never be as accurate as individually examining those
301  // tokens...
302  //
303  EXPORT bool VerifyCurrentDate(); // Verify whether the CURRENT date is
304  // WITHIN the VALID FROM / TO dates.
305  EXPORT bool IsExpired(); // Verify whether the CURRENT date is AFTER the the
306  // "VALID TO" date.
307  EXPORT bool Merge(const OTPseudonym& theSigner,
308  OTNym_or_SymmetricKey theOldNym,
309  OTNym_or_SymmetricKey theNewNym, Purse& theNewPurse);
310  EXPORT Purse(const Purse& thePurse); // just for copy another purse's
311  // Server and Asset ID
312  EXPORT Purse(const OTIdentifier& SERVER_ID,
313  const OTIdentifier& ASSET_ID); // similar thing
314  EXPORT Purse(const OTIdentifier& SERVER_ID); // Don't use this unless you
315  // really don't know the
316  // asset type
317  // (Like if you're about to read it out of a string.)
318  // Normally you really really want to set the asset type.
319  EXPORT Purse(const OTIdentifier& SERVER_ID, const OTIdentifier& ASSET_ID,
320  const OTIdentifier& USER_ID); // UserID optional
321  EXPORT virtual ~Purse();
322  EXPORT bool LoadPurse(const char* szServerID = nullptr,
323  const char* szUserID = nullptr,
324  const char* szAssetTypeID = nullptr);
325  EXPORT bool SavePurse(const char* szServerID = nullptr,
326  const char* szUserID = nullptr,
327  const char* szAssetTypeID = nullptr);
328 
329  virtual bool LoadContract();
330 
331  inline const OTIdentifier& GetServerID() const
332  {
333  return m_ServerID;
334  }
335  inline const OTIdentifier& GetAssetID() const
336  {
337  return m_AssetID;
338  }
339  EXPORT void InitPurse();
340  virtual void Release();
341  EXPORT void Release_Purse();
342  EXPORT void ReleaseTokens();
343 
344  virtual bool SaveContractWallet(std::ofstream& ofs) const;
345 };
346 
347 } // namespace opentxs
348 
349 #endif // OPENTXS_CASH_PURSE_HPP
int64_t m_lTotalValue
Definition: Purse.hpp:190
EXPORT bool GetPassphrase(OTPassword &theOutput, const char *szDisplay=nullptr)
Definition: Purse.cpp:179
EXPORT bool IsExpired()
Definition: Purse.cpp:1387
EXPORT bool IsNymIDIncluded() const
Definition: Purse.hpp:268
EXPORT int32_t Count() const
Definition: Purse.cpp:1641
EXPORT bool IsPasswordProtected() const
Definition: Purse.hpp:272
virtual bool SaveContractWallet(std::ofstream &ofs) const
Definition: Purse.cpp:1365
EXPORT bool Push(OTNym_or_SymmetricKey theOwner, const Token &theToken)
Definition: Purse.cpp:1575
bool m_bIsNymIDIncluded
Definition: Purse.hpp:208
virtual void Release()
Definition: Purse.cpp:812
time64_t m_tLatestValidFrom
Definition: Purse.hpp:219
virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader *&xml)
Definition: Purse.cpp:1063
time64_t m_tEarliestValidTo
Definition: Purse.hpp:221
EXPORT bool VerifyCurrentDate()
Definition: Purse.cpp:1404
const OTIdentifier & GetAssetID() const
Definition: Purse.hpp:335
static EXPORT Purse * PurseFactory(OTString strInput)
Definition: Purse.cpp:671
OTIdentifier m_ServerID
Definition: Purse.hpp:188
OTSymmetricKey * m_pSymmetricKey
Definition: Purse.hpp:213
int64_t time64_t
Definition: Common.hpp:209
std::deque< OTASCIIArmor * > dequeOfTokens
Definition: Purse.hpp:147
virtual bool LoadContract()
Definition: Purse.cpp:827
EXPORT std::shared_ptr< OTCachedKey > GetInternalMaster()
Definition: Purse.cpp:207
OTIdentifier m_AssetID
Definition: Purse.hpp:189
EXPORT OTSymmetricKey * GetInternalKey()
Definition: Purse.hpp:252
EXPORT Token * Peek(OTNym_or_SymmetricKey theOwner) const
Definition: Purse.cpp:1419
EXPORT void Release_Purse()
Definition: Purse.cpp:787
int64_t GetTotalValue() const
Definition: Purse.hpp:292
std::shared_ptr< OTCachedKey > m_pCachedKey
Definition: Purse.hpp:218
bool m_bPasswordProtected
Definition: Purse.hpp:192
EXPORT time64_t GetLatestValidFrom() const
Definition: Purse.cpp:1370
EXPORT void ReleaseTokens()
Definition: Purse.cpp:1651
EXPORT bool Merge(const OTPseudonym &theSigner, OTNym_or_SymmetricKey theOldNym, OTNym_or_SymmetricKey theNewNym, Purse &theNewPurse)
Definition: Purse.cpp:349
void RecalculateExpirationDates(OTNym_or_SymmetricKey &theOwner)
Definition: Purse.cpp:1519
EXPORT void InitPurse()
Definition: Purse.cpp:772
EXPORT time64_t GetEarliestValidTo() const
Definition: Purse.cpp:1375
EXPORT bool SavePurse(const char *szServerID=nullptr, const char *szUserID=nullptr, const char *szAssetTypeID=nullptr)
Definition: Purse.cpp:889
virtual EXPORT ~Purse()
Definition: Purse.cpp:782
EXPORT bool LoadPurse(const char *szServerID=nullptr, const char *szUserID=nullptr, const char *szAssetTypeID=nullptr)
Definition: Purse.cpp:832
static EXPORT Purse * LowLevelInstantiate(const OTString &strFirstLine)
Definition: Purse.cpp:566
OTIdentifier m_UserID
Definition: Purse.hpp:187
EXPORT bool GetNymID(OTIdentifier &theOutput) const
Definition: Purse.cpp:154
EXPORT bool GenerateInternalKey()
Definition: Purse.cpp:257
dequeOfTokens m_dequeTokens
Definition: Purse.hpp:181
EXPORT bool IsEmpty() const
Definition: Purse.cpp:1646
virtual void UpdateContents()
Definition: Purse.cpp:952
const OTIdentifier & GetServerID() const
Definition: Purse.hpp:331
EXPORT Token * Pop(OTNym_or_SymmetricKey theOwner)
Definition: Purse.cpp:1473