b9ccb06c09e0a280d10bef6466df4aed5aa92cf0
[novacoin.git] / src / qt / walletmodel.cpp
1 #include "walletmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
6
7 #include "ui_interface.h"
8 #include "wallet.h"
9 #include "walletdb.h" // for BackupWallet
10
11 #include <QSet>
12
13 WalletModel::WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent) :
14     QObject(parent), wallet(wallet), optionsModel(optionsModel), addressTableModel(0),
15     transactionTableModel(0),
16     cachedBalance(0), cachedUnconfirmedBalance(0), cachedNumTransactions(0),
17     cachedEncryptionStatus(Unencrypted)
18 {
19     addressTableModel = new AddressTableModel(wallet, this);
20     transactionTableModel = new TransactionTableModel(wallet, this);
21 }
22
23 qint64 WalletModel::getBalance() const
24 {
25     return wallet->GetBalance();
26 }
27
28 qint64 WalletModel::getUnconfirmedBalance() const
29 {
30     return wallet->GetUnconfirmedBalance();
31 }
32
33 int WalletModel::getNumTransactions() const
34 {
35     int numTransactions = 0;
36     {
37         LOCK(wallet->cs_wallet);
38         numTransactions = wallet->mapWallet.size();
39     }
40     return numTransactions;
41 }
42
43 void WalletModel::update()
44 {
45     qint64 newBalance = getBalance();
46     qint64 newUnconfirmedBalance = getUnconfirmedBalance();
47     int newNumTransactions = getNumTransactions();
48     EncryptionStatus newEncryptionStatus = getEncryptionStatus();
49
50     if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance)
51         emit balanceChanged(newBalance, newUnconfirmedBalance);
52
53     if(cachedNumTransactions != newNumTransactions)
54         emit numTransactionsChanged(newNumTransactions);
55
56     if(cachedEncryptionStatus != newEncryptionStatus)
57         emit encryptionStatusChanged(newEncryptionStatus);
58
59     cachedBalance = newBalance;
60     cachedUnconfirmedBalance = newUnconfirmedBalance;
61     cachedNumTransactions = newNumTransactions;
62 }
63
64 void WalletModel::updateAddressList()
65 {
66     addressTableModel->update();
67 }
68
69 bool WalletModel::validateAddress(const QString &address)
70 {
71     CBitcoinAddress addressParsed(address.toStdString());
72     return addressParsed.IsValid();
73 }
74
75 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
76 {
77     qint64 total = 0;
78     QSet<QString> setAddress;
79     QString hex;
80
81     if(recipients.empty())
82     {
83         return OK;
84     }
85
86     // Pre-check input data for validity
87     foreach(const SendCoinsRecipient &rcp, recipients)
88     {
89         if(!validateAddress(rcp.address))
90         {
91             return InvalidAddress;
92         }
93         setAddress.insert(rcp.address);
94
95         if(rcp.amount <= 0)
96         {
97             return InvalidAmount;
98         }
99         total += rcp.amount;
100     }
101
102     if(recipients.size() > setAddress.size())
103     {
104         return DuplicateAddress;
105     }
106
107     if(total > getBalance())
108     {
109         return AmountExceedsBalance;
110     }
111
112     if((total + nTransactionFee) > getBalance())
113     {
114         return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
115     }
116
117     {
118         LOCK2(cs_main, wallet->cs_wallet);
119
120         // Sendmany
121         std::vector<std::pair<CScript, int64> > vecSend;
122         foreach(const SendCoinsRecipient &rcp, recipients)
123         {
124             CScript scriptPubKey;
125             scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
126             vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
127         }
128
129         CWalletTx wtx;
130         CReserveKey keyChange(wallet);
131         int64 nFeeRequired = 0;
132         bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
133
134         if(!fCreated)
135         {
136             if((total + nFeeRequired) > wallet->GetBalance())
137             {
138                 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
139             }
140             return TransactionCreationFailed;
141         }
142         if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString()))
143         {
144             return Aborted;
145         }
146         if(!wallet->CommitTransaction(wtx, keyChange))
147         {
148             return TransactionCommitFailed;
149         }
150         hex = QString::fromStdString(wtx.GetHash().GetHex());
151     }
152
153     // Add addresses / update labels that we've sent to to the address book
154     foreach(const SendCoinsRecipient &rcp, recipients)
155     {
156         std::string strAddress = rcp.address.toStdString();
157         std::string strLabel = rcp.label.toStdString();
158         {
159             LOCK(wallet->cs_wallet);
160
161             std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(strAddress);
162
163             // Check if we have a new address or an updated label
164             if (mi == wallet->mapAddressBook.end() || mi->second != strLabel)
165             {
166                 wallet->SetAddressBookName(strAddress, strLabel);
167             }
168         }
169     }
170
171     return SendCoinsReturn(OK, 0, hex);
172 }
173
174 OptionsModel *WalletModel::getOptionsModel()
175 {
176     return optionsModel;
177 }
178
179 AddressTableModel *WalletModel::getAddressTableModel()
180 {
181     return addressTableModel;
182 }
183
184 TransactionTableModel *WalletModel::getTransactionTableModel()
185 {
186     return transactionTableModel;
187 }
188
189 WalletModel::EncryptionStatus WalletModel::getEncryptionStatus() const
190 {
191     if(!wallet->IsCrypted())
192     {
193         return Unencrypted;
194     }
195     else if(wallet->IsLocked())
196     {
197         return Locked;
198     }
199     else
200     {
201         return Unlocked;
202     }
203 }
204
205 bool WalletModel::setWalletEncrypted(bool encrypted, const SecureString &passphrase)
206 {
207     if(encrypted)
208     {
209         // Encrypt
210         return wallet->EncryptWallet(passphrase);
211     }
212     else
213     {
214         // Decrypt -- TODO; not supported yet
215         return false;
216     }
217 }
218
219 bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
220 {
221     if(locked)
222     {
223         // Lock
224         return wallet->Lock();
225     }
226     else
227     {
228         // Unlock
229         return wallet->Unlock(passPhrase);
230     }
231 }
232
233 bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureString &newPass)
234 {
235     bool retval;
236     {
237         LOCK(wallet->cs_wallet);
238         wallet->Lock(); // Make sure wallet is locked before attempting pass change
239         retval = wallet->ChangeWalletPassphrase(oldPass, newPass);
240     }
241     return retval;
242 }
243
244 bool WalletModel::backupWallet(const QString &filename)
245 {
246     return BackupWallet(*wallet, filename.toLocal8Bit().data());
247 }
248
249 // WalletModel::UnlockContext implementation
250 WalletModel::UnlockContext WalletModel::requestUnlock()
251 {
252     bool was_locked = getEncryptionStatus() == Locked;
253     if(was_locked)
254     {
255         // Request UI to unlock wallet
256         emit requireUnlock();
257     }
258     // If wallet is still locked, unlock was failed or cancelled, mark context as invalid
259     bool valid = getEncryptionStatus() != Locked;
260
261     return UnlockContext(this, valid, was_locked);
262 }
263
264 WalletModel::UnlockContext::UnlockContext(WalletModel *wallet, bool valid, bool relock):
265         wallet(wallet),
266         valid(valid),
267         relock(relock)
268 {
269 }
270
271 WalletModel::UnlockContext::~UnlockContext()
272 {
273     if(valid && relock)
274     {
275         wallet->setWalletLocked(true);
276     }
277 }
278
279 void WalletModel::UnlockContext::CopyFrom(const UnlockContext& rhs)
280 {
281     // Transfer context; old object no longer relocks wallet
282     *this = rhs;
283     rhs.relock = false;
284 }