1 #include "walletmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
7 #include "ui_interface.h"
9 #include "walletdb.h" // for BackupWallet
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)
19 addressTableModel = new AddressTableModel(wallet, this);
20 transactionTableModel = new TransactionTableModel(wallet, this);
23 qint64 WalletModel::getBalance() const
25 return wallet->GetBalance();
28 qint64 WalletModel::getUnconfirmedBalance() const
30 return wallet->GetUnconfirmedBalance();
33 int WalletModel::getNumTransactions() const
35 int numTransactions = 0;
37 LOCK(wallet->cs_wallet);
38 numTransactions = wallet->mapWallet.size();
40 return numTransactions;
43 void WalletModel::update()
45 qint64 newBalance = getBalance();
46 qint64 newUnconfirmedBalance = getUnconfirmedBalance();
47 int newNumTransactions = getNumTransactions();
48 EncryptionStatus newEncryptionStatus = getEncryptionStatus();
50 if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance)
51 emit balanceChanged(newBalance, newUnconfirmedBalance);
53 if(cachedNumTransactions != newNumTransactions)
54 emit numTransactionsChanged(newNumTransactions);
56 if(cachedEncryptionStatus != newEncryptionStatus)
57 emit encryptionStatusChanged(newEncryptionStatus);
59 cachedBalance = newBalance;
60 cachedUnconfirmedBalance = newUnconfirmedBalance;
61 cachedNumTransactions = newNumTransactions;
64 void WalletModel::updateAddressList()
66 addressTableModel->update();
69 bool WalletModel::validateAddress(const QString &address)
71 CBitcoinAddress addressParsed(address.toStdString());
72 return addressParsed.IsValid();
75 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
78 QSet<QString> setAddress;
81 if(recipients.empty())
86 // Pre-check input data for validity
87 foreach(const SendCoinsRecipient &rcp, recipients)
89 if(!validateAddress(rcp.address))
91 return InvalidAddress;
93 setAddress.insert(rcp.address);
102 if(recipients.size() > setAddress.size())
104 return DuplicateAddress;
107 if(total > getBalance())
109 return AmountExceedsBalance;
112 if((total + nTransactionFee) > getBalance())
114 return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
118 LOCK2(cs_main, wallet->cs_wallet);
121 std::vector<std::pair<CScript, int64> > vecSend;
122 foreach(const SendCoinsRecipient &rcp, recipients)
124 CScript scriptPubKey;
125 scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
126 vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
130 CReserveKey keyChange(wallet);
131 int64 nFeeRequired = 0;
132 bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
136 if((total + nFeeRequired) > wallet->GetBalance())
138 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
140 return TransactionCreationFailed;
142 if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString()))
146 if(!wallet->CommitTransaction(wtx, keyChange))
148 return TransactionCommitFailed;
150 hex = QString::fromStdString(wtx.GetHash().GetHex());
153 // Add addresses / update labels that we've sent to to the address book
154 foreach(const SendCoinsRecipient &rcp, recipients)
156 std::string strAddress = rcp.address.toStdString();
157 std::string strLabel = rcp.label.toStdString();
159 LOCK(wallet->cs_wallet);
161 std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(strAddress);
163 // Check if we have a new address or an updated label
164 if (mi == wallet->mapAddressBook.end() || mi->second != strLabel)
166 wallet->SetAddressBookName(strAddress, strLabel);
171 return SendCoinsReturn(OK, 0, hex);
174 OptionsModel *WalletModel::getOptionsModel()
179 AddressTableModel *WalletModel::getAddressTableModel()
181 return addressTableModel;
184 TransactionTableModel *WalletModel::getTransactionTableModel()
186 return transactionTableModel;
189 WalletModel::EncryptionStatus WalletModel::getEncryptionStatus() const
191 if(!wallet->IsCrypted())
195 else if(wallet->IsLocked())
205 bool WalletModel::setWalletEncrypted(bool encrypted, const SecureString &passphrase)
210 return wallet->EncryptWallet(passphrase);
214 // Decrypt -- TODO; not supported yet
219 bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
224 return wallet->Lock();
229 return wallet->Unlock(passPhrase);
233 bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureString &newPass)
237 LOCK(wallet->cs_wallet);
238 wallet->Lock(); // Make sure wallet is locked before attempting pass change
239 retval = wallet->ChangeWalletPassphrase(oldPass, newPass);
244 bool WalletModel::backupWallet(const QString &filename)
246 return BackupWallet(*wallet, filename.toLocal8Bit().data());
249 // WalletModel::UnlockContext implementation
250 WalletModel::UnlockContext WalletModel::requestUnlock()
252 bool was_locked = getEncryptionStatus() == Locked;
255 // Request UI to unlock wallet
256 emit requireUnlock();
258 // If wallet is still locked, unlock was failed or cancelled, mark context as invalid
259 bool valid = getEncryptionStatus() != Locked;
261 return UnlockContext(this, valid, was_locked);
264 WalletModel::UnlockContext::UnlockContext(WalletModel *wallet, bool valid, bool relock):
271 WalletModel::UnlockContext::~UnlockContext()
275 wallet->setWalletLocked(true);
279 void WalletModel::UnlockContext::CopyFrom(const UnlockContext& rhs)
281 // Transfer context; old object no longer relocks wallet