#include "addresstablemodel.h"
#include "guiutil.h"
+#include "walletmodel.h"
#include "headers.h"
{
cachedAddressTable.clear();
- CRITICAL_BLOCK(wallet->cs_mapKeys)
+ CRITICAL_BLOCK(wallet->cs_KeyStore)
CRITICAL_BLOCK(wallet->cs_mapAddressBook)
{
- BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
+ BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, std::string)& item, wallet->mapAddressBook)
{
- std::string strAddress = item.first;
- std::string strName = item.second;
- uint160 hash160;
- bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
+ const CBitcoinAddress& address = item.first;
+ const std::string& strName = item.second;
+ bool fMine = wallet->HaveKey(address);
cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
QString::fromStdString(strName),
- QString::fromStdString(strAddress)));
+ QString::fromStdString(address.ToString())));
}
}
}
}
};
-AddressTableModel::AddressTableModel(CWallet *wallet, QObject *parent) :
- QAbstractTableModel(parent),wallet(wallet),priv(0)
+AddressTableModel::AddressTableModel(CWallet *wallet, WalletModel *parent) :
+ QAbstractTableModel(parent),walletModel(parent),wallet(wallet),priv(0)
{
columns << tr("Label") << tr("Address");
priv = new AddressTablePriv(wallet);
return false;
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
+ editStatus = OK;
+
if(role == Qt::EditRole)
{
switch(index.column())
rec->label = value.toString();
break;
case Address:
- // Refuse to set invalid address
- if(!validateAddress(value.toString()))
+ // Refuse to set invalid address, set error status and return false
+ if(!walletModel->validateAddress(value.toString()))
+ {
+ editStatus = INVALID_ADDRESS;
return false;
- // Double-check that we're not overwriting receiving address
+ }
+ // Double-check that we're not overwriting a receiving address
if(rec->type == AddressTableEntry::Sending)
{
CRITICAL_BLOCK(wallet->cs_mapAddressBook)
void AddressTableModel::updateList()
{
- // Update internal model from Bitcoin core
+ // Update address book model from Bitcoin core
beginResetModel();
priv->refreshAddressTable();
endResetModel();
std::string strLabel = label.toStdString();
std::string strAddress = address.toStdString();
+ editStatus = OK;
+
if(type == Send)
{
- // Check for duplicate
+ if(!walletModel->validateAddress(address))
+ {
+ editStatus = INVALID_ADDRESS;
+ return QString();
+ }
+ // Check for duplicate addresses
CRITICAL_BLOCK(wallet->cs_mapAddressBook)
{
if(wallet->mapAddressBook.count(strAddress))
{
+ editStatus = DUPLICATE_ADDRESS;
return QString();
}
}
}
else if(type == Receive)
{
- // Generate a new address to associate with given label, optionally
- // set as default receiving address.
- strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
+ // Generate a new address to associate with given label
+ WalletModel::UnlockContext ctx(walletModel->requestUnlock());
+ if(!ctx.isValid())
+ {
+ // Unlock wallet failed or was cancelled
+ editStatus = WALLET_UNLOCK_FAILURE;
+ return QString();
+ }
+
+ strAddress = CBitcoinAddress(wallet->GetOrReuseKeyFromPool()).ToString();
}
else
{
return QString();
}
// Add entry and update list
- wallet->SetAddressBookName(strAddress, strLabel);
+ CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+ wallet->SetAddressBookName(strAddress, strLabel);
updateList();
return QString::fromStdString(strAddress);
}
}
-bool AddressTableModel::validateAddress(const QString &address)
-{
- uint160 hash160 = 0;
-
- return AddressToHash160(address.toStdString(), hash160);
-}
-
/* Look up label for address in address book, if not found return empty string.
*/
QString AddressTableModel::labelForAddress(const QString &address) const
{
CRITICAL_BLOCK(wallet->cs_mapAddressBook)
{
- std::map<std::string, std::string>::iterator mi = wallet->mapAddressBook.find(address.toStdString());
+ CBitcoinAddress address_parsed(address.toStdString());
+ std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed);
if (mi != wallet->mapAddressBook.end())
{
return QString::fromStdString(mi->second);