1 #include "walletmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
12 WalletModel::WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent) :
13 QObject(parent), wallet(wallet), optionsModel(optionsModel), addressTableModel(0),
14 transactionTableModel(0),
15 cachedBalance(0), cachedUnconfirmedBalance(0), cachedNumTransactions(0),
16 cachedEncryptionStatus(Unencrypted)
18 // Until signal notifications is built into the bitcoin core,
19 // simply update everything after polling using a timer.
20 QTimer *timer = new QTimer(this);
21 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
22 timer->start(MODEL_UPDATE_DELAY);
24 addressTableModel = new AddressTableModel(wallet, this);
25 transactionTableModel = new TransactionTableModel(wallet, this);
28 qint64 WalletModel::getBalance() const
30 return wallet->GetBalance();
33 qint64 WalletModel::getUnconfirmedBalance() const
35 return wallet->GetUnconfirmedBalance();
38 int WalletModel::getNumTransactions() const
40 int numTransactions = 0;
41 CRITICAL_BLOCK(wallet->cs_wallet)
43 numTransactions = wallet->mapWallet.size();
45 return numTransactions;
48 void WalletModel::update()
50 qint64 newBalance = getBalance();
51 qint64 newUnconfirmedBalance = getUnconfirmedBalance();
52 int newNumTransactions = getNumTransactions();
53 EncryptionStatus newEncryptionStatus = getEncryptionStatus();
55 if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance)
56 emit balanceChanged(newBalance, newUnconfirmedBalance);
58 if(cachedNumTransactions != newNumTransactions)
59 emit numTransactionsChanged(newNumTransactions);
61 if(cachedEncryptionStatus != newEncryptionStatus)
62 emit encryptionStatusChanged(newEncryptionStatus);
64 cachedBalance = newBalance;
65 cachedUnconfirmedBalance = newUnconfirmedBalance;
66 cachedNumTransactions = newNumTransactions;
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);
119 CRITICAL_BLOCK(cs_main)
120 CRITICAL_BLOCK(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(), NULL))
148 if(!wallet->CommitTransaction(wtx, keyChange))
150 return TransactionCommitFailed;
152 hex = QString::fromStdString(wtx.GetHash().GetHex());
155 // Add addresses / update labels that we've sent to to the address book
156 foreach(const SendCoinsRecipient &rcp, recipients)
158 std::string strAddress = rcp.address.toStdString();
159 std::string strLabel = rcp.label.toStdString();
160 CRITICAL_BLOCK(wallet->cs_wallet)
162 std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(strAddress);
164 // Check if we have a new address or an updated label
165 if (mi == wallet->mapAddressBook.end() || mi->second != strLabel)
167 wallet->SetAddressBookName(strAddress, strLabel);
172 // Update our model of the address table
173 addressTableModel->updateList();
175 return SendCoinsReturn(OK, 0, hex);
178 OptionsModel *WalletModel::getOptionsModel()
183 AddressTableModel *WalletModel::getAddressTableModel()
185 return addressTableModel;
188 TransactionTableModel *WalletModel::getTransactionTableModel()
190 return transactionTableModel;
193 WalletModel::EncryptionStatus WalletModel::getEncryptionStatus() const
195 if(!wallet->IsCrypted())
199 else if(wallet->IsLocked())
209 bool WalletModel::setWalletEncrypted(bool encrypted, const SecureString &passphrase)
214 return wallet->EncryptWallet(passphrase);
218 // Decrypt -- TODO; not supported yet
223 bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
228 return wallet->Lock();
233 return wallet->Unlock(passPhrase);
237 bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureString &newPass)
240 CRITICAL_BLOCK(wallet->cs_wallet)
242 wallet->Lock(); // Make sure wallet is locked before attempting pass change
243 retval = wallet->ChangeWalletPassphrase(oldPass, newPass);
248 // WalletModel::UnlockContext implementation
249 WalletModel::UnlockContext WalletModel::requestUnlock()
251 bool was_locked = getEncryptionStatus() == Locked;
254 // Request UI to unlock wallet
255 emit requireUnlock();
257 // If wallet is still locked, unlock was failed or cancelled, mark context as invalid
258 bool valid = getEncryptionStatus() != Locked;
260 return UnlockContext(this, valid, was_locked);
263 WalletModel::UnlockContext::UnlockContext(WalletModel *wallet, bool valid, bool relock):
270 WalletModel::UnlockContext::~UnlockContext()
274 wallet->setWalletLocked(true);
278 void WalletModel::UnlockContext::CopyFrom(const UnlockContext& rhs)
280 // Transfer context; old object no longer relocks wallet