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::getStake() const
30 return wallet->GetStake();
33 qint64 WalletModel::getUnconfirmedBalance() const
35 return wallet->GetUnconfirmedBalance();
38 int WalletModel::getNumTransactions() const
40 int numTransactions = 0;
42 LOCK(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, getStake(), 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;
69 void WalletModel::updateAddressList()
71 addressTableModel->update();
74 bool WalletModel::validateAddress(const QString &address)
76 CBitcoinAddress addressParsed(address.toStdString());
77 return addressParsed.IsValid();
80 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
83 QSet<QString> setAddress;
86 if(recipients.empty())
91 // Pre-check input data for validity
92 foreach(const SendCoinsRecipient &rcp, recipients)
94 if(!validateAddress(rcp.address))
96 return InvalidAddress;
98 setAddress.insert(rcp.address);
102 return InvalidAmount;
107 if(recipients.size() > setAddress.size())
109 return DuplicateAddress;
112 if(total > getBalance())
114 return AmountExceedsBalance;
117 if((total + nTransactionFee) > getBalance())
119 return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
123 LOCK2(cs_main, wallet->cs_wallet);
126 std::vector<std::pair<CScript, int64> > vecSend;
127 foreach(const SendCoinsRecipient &rcp, recipients)
129 CScript scriptPubKey;
130 scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
131 vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
135 CReserveKey keyChange(wallet);
136 int64 nFeeRequired = 0;
137 bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
141 if((total + nFeeRequired) > wallet->GetBalance())
143 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
145 return TransactionCreationFailed;
147 if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString()))
151 if(!wallet->CommitTransaction(wtx, keyChange))
153 return TransactionCommitFailed;
155 hex = QString::fromStdString(wtx.GetHash().GetHex());
158 // Add addresses / update labels that we've sent to to the address book
159 foreach(const SendCoinsRecipient &rcp, recipients)
161 std::string strAddress = rcp.address.toStdString();
162 std::string strLabel = rcp.label.toStdString();
164 LOCK(wallet->cs_wallet);
166 std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(strAddress);
168 // Check if we have a new address or an updated label
169 if (mi == wallet->mapAddressBook.end() || mi->second != strLabel)
171 wallet->SetAddressBookName(strAddress, strLabel);
176 return SendCoinsReturn(OK, 0, hex);
179 OptionsModel *WalletModel::getOptionsModel()
184 AddressTableModel *WalletModel::getAddressTableModel()
186 return addressTableModel;
189 TransactionTableModel *WalletModel::getTransactionTableModel()
191 return transactionTableModel;
194 WalletModel::EncryptionStatus WalletModel::getEncryptionStatus() const
196 if(!wallet->IsCrypted())
200 else if(wallet->IsLocked())
210 bool WalletModel::setWalletEncrypted(bool encrypted, const SecureString &passphrase)
215 return wallet->EncryptWallet(passphrase);
219 // Decrypt -- TODO; not supported yet
224 bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
229 return wallet->Lock();
234 return wallet->Unlock(passPhrase);
238 bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureString &newPass)
242 LOCK(wallet->cs_wallet);
243 wallet->Lock(); // Make sure wallet is locked before attempting pass change
244 retval = wallet->ChangeWalletPassphrase(oldPass, newPass);
249 bool WalletModel::backupWallet(const QString &filename)
251 return BackupWallet(*wallet, filename.toLocal8Bit().data());
254 // WalletModel::UnlockContext implementation
255 WalletModel::UnlockContext WalletModel::requestUnlock()
257 bool was_locked = getEncryptionStatus() == Locked;
260 // Request UI to unlock wallet
261 emit requireUnlock();
263 // If wallet is still locked, unlock was failed or cancelled, mark context as invalid
264 bool valid = getEncryptionStatus() != Locked;
266 return UnlockContext(this, valid, was_locked);
269 WalletModel::UnlockContext::UnlockContext(WalletModel *wallet, bool valid, bool relock):
276 WalletModel::UnlockContext::~UnlockContext()
280 wallet->setWalletLocked(true);
284 void WalletModel::UnlockContext::CopyFrom(const UnlockContext& rhs)
286 // Transfer context; old object no longer relocks wallet