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;
63 addressTableModel->update();
66 void WalletModel::updateAddressList()
68 addressTableModel->update();
71 bool WalletModel::validateAddress(const QString &address)
73 CBitcoinAddress addressParsed(address.toStdString());
74 return addressParsed.IsValid();
77 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
80 QSet<QString> setAddress;
83 if(recipients.empty())
88 // Pre-check input data for validity
89 foreach(const SendCoinsRecipient &rcp, recipients)
91 if(!validateAddress(rcp.address))
93 return InvalidAddress;
95 setAddress.insert(rcp.address);
104 if(recipients.size() > setAddress.size())
106 return DuplicateAddress;
109 if(total > getBalance())
111 return AmountExceedsBalance;
114 if((total + nTransactionFee) > getBalance())
116 return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
120 LOCK2(cs_main, wallet->cs_wallet);
123 std::vector<std::pair<CScript, int64> > vecSend;
124 foreach(const SendCoinsRecipient &rcp, recipients)
126 CScript scriptPubKey;
127 scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
128 vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
132 CReserveKey keyChange(wallet);
133 int64 nFeeRequired = 0;
134 bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
138 if((total + nFeeRequired) > wallet->GetBalance())
140 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
142 return TransactionCreationFailed;
144 if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString()))
148 if(!wallet->CommitTransaction(wtx, keyChange))
150 return TransactionCommitFailed;
152 hex = QString::fromStdString(wtx.GetHash().GetHex());
155 // Add addresses that we've sent to to the address book
156 foreach(const SendCoinsRecipient &rcp, recipients)
158 std::string strAddress = rcp.address.toStdString();
160 LOCK(wallet->cs_wallet);
161 if (!wallet->mapAddressBook.count(strAddress))
162 wallet->SetAddressBookName(strAddress, rcp.label.toStdString());
166 return SendCoinsReturn(OK, 0, hex);
169 OptionsModel *WalletModel::getOptionsModel()
174 AddressTableModel *WalletModel::getAddressTableModel()
176 return addressTableModel;
179 TransactionTableModel *WalletModel::getTransactionTableModel()
181 return transactionTableModel;
184 WalletModel::EncryptionStatus WalletModel::getEncryptionStatus() const
186 if(!wallet->IsCrypted())
190 else if(wallet->IsLocked())
200 bool WalletModel::setWalletEncrypted(bool encrypted, const SecureString &passphrase)
205 return wallet->EncryptWallet(passphrase);
209 // Decrypt -- TODO; not supported yet
214 bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
219 return wallet->Lock();
224 return wallet->Unlock(passPhrase);
228 bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureString &newPass)
232 LOCK(wallet->cs_wallet);
233 wallet->Lock(); // Make sure wallet is locked before attempting pass change
234 retval = wallet->ChangeWalletPassphrase(oldPass, newPass);
239 bool WalletModel::backupWallet(const QString &filename)
241 return BackupWallet(*wallet, filename.toLocal8Bit().data());
244 // WalletModel::UnlockContext implementation
245 WalletModel::UnlockContext WalletModel::requestUnlock()
247 bool was_locked = getEncryptionStatus() == Locked;
250 // Request UI to unlock wallet
251 emit requireUnlock();
253 // If wallet is still locked, unlock was failed or cancelled, mark context as invalid
254 bool valid = getEncryptionStatus() != Locked;
256 return UnlockContext(this, valid, was_locked);
259 WalletModel::UnlockContext::UnlockContext(WalletModel *wallet, bool valid, bool relock):
266 WalletModel::UnlockContext::~UnlockContext()
270 wallet->setWalletLocked(true);
274 void WalletModel::UnlockContext::CopyFrom(const UnlockContext& rhs)
276 // Transfer context; old object no longer relocks wallet