Update License in File Headers
[novacoin.git] / src / ui.cpp
index 52315e1..1fcfff6 100644 (file)
@@ -1,6 +1,7 @@
 // Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (c) 2011 The Bitcoin developers
 // Distributed under the MIT/X11 software license, see the accompanying
-// file license.txt or http://www.opensource.org/licenses/mit-license.php.
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
 
 #include "headers.h"
 #include "db.h"
@@ -235,12 +236,13 @@ void SetDefaultReceivingAddress(const string& strAddress)
         return;
     if (strAddress != pframeMain->m_textCtrlAddress->GetValue())
     {
-        uint160 hash160;
-        if (!AddressToHash160(strAddress, hash160))
+        CBitcoinAddress address(strAddress);
+        if (!address.IsValid())
             return;
-        if (!mapPubKeys.count(hash160))
+        vector<unsigned char> vchPubKey;
+        if (!pwalletMain->GetPubKey(address, vchPubKey))
             return;
-        pwalletMain->SetDefaultKey(mapPubKeys[hash160]);
+        pwalletMain->SetDefaultKey(vchPubKey);
         pframeMain->m_textCtrlAddress->SetValue(strAddress);
     }
 }
@@ -366,7 +368,12 @@ CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent)
     // Fill your address text box
     vector<unsigned char> vchPubKey;
     if (CWalletDB(pwalletMain->strWalletFile,"r").ReadDefaultKey(vchPubKey))
-        m_textCtrlAddress->SetValue(PubKeyToAddress(vchPubKey));
+        m_textCtrlAddress->SetValue(CBitcoinAddress(vchPubKey).ToString());
+
+    if (pwalletMain->IsCrypted())
+        m_menuOptions->Remove(m_menuOptionsEncryptWallet);
+    else
+        m_menuOptions->Remove(m_menuOptionsChangeWalletPassphrase);
 
     // Fill listctrl with wallet transactions
     RefreshListCtrl();
@@ -557,7 +564,7 @@ string FormatTxStatus(const CWalletTx& wtx)
     // Status
     if (!wtx.IsFinal())
     {
-        if (wtx.nLockTime < 500000000)
+        if (wtx.nLockTime < LOCKTIME_THRESHOLD)
             return strprintf(_("Open for %d blocks"), nBestHeight - wtx.nLockTime);
         else
             return strprintf(_("Open until %s"), DateTimeStr(wtx.nLockTime).c_str());
@@ -698,24 +705,23 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex)
             {
                 if (pwalletMain->IsMine(txout))
                 {
-                    vector<unsigned char> vchPubKey;
-                    if (ExtractPubKey(txout.scriptPubKey, pwalletMain, vchPubKey))
+                    CBitcoinAddress address;
+                    if (ExtractAddress(txout.scriptPubKey, pwalletMain, address))
                     {
-                        CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
+                        CRITICAL_BLOCK(pwalletMain->cs_wallet)
                         {
                             //strDescription += _("Received payment to ");
                             //strDescription += _("Received with address ");
                             strDescription += _("Received with: ");
-                            string strAddress = PubKeyToAddress(vchPubKey);
-                            map<string, string>::iterator mi = pwalletMain->mapAddressBook.find(strAddress);
+                            map<CBitcoinAddress, string>::iterator mi = pwalletMain->mapAddressBook.find(address);
                             if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty())
                             {
                                 string strLabel = (*mi).second;
-                                strDescription += strAddress.substr(0,12) + "... ";
+                                strDescription += address.ToString().substr(0,12) + "... ";
                                 strDescription += "(" + strLabel + ")";
                             }
                             else
-                                strDescription += strAddress;
+                                strDescription += address.ToString();
                         }
                     }
                     break;
@@ -771,6 +777,7 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex)
                 if (pwalletMain->IsMine(txout))
                     continue;
 
+                CBitcoinAddress address;
                 string strAddress;
                 if (!mapValue["to"].empty())
                 {
@@ -780,15 +787,14 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex)
                 else
                 {
                     // Sent to Bitcoin Address
-                    uint160 hash160;
-                    if (ExtractHash160(txout.scriptPubKey, hash160))
-                        strAddress = Hash160ToAddress(hash160);
+                    if (ExtractAddress(txout.scriptPubKey, NULL, address))
+                        strAddress = address.ToString();
                 }
 
                 string strDescription = _("To: ");
-                CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
-                    if (pwalletMain->mapAddressBook.count(strAddress) && !pwalletMain->mapAddressBook[strAddress].empty())
-                        strDescription += pwalletMain->mapAddressBook[strAddress] + " ";
+                CRITICAL_BLOCK(pwalletMain->cs_wallet)
+                    if (pwalletMain->mapAddressBook.count(address) && !pwalletMain->mapAddressBook[address].empty())
+                        strDescription += pwalletMain->mapAddressBook[address] + " ";
                 strDescription += strAddress;
                 if (!mapValue["message"].empty())
                 {
@@ -856,7 +862,7 @@ void CMainFrame::OnIdle(wxIdleEvent& event)
         // Collect list of wallet transactions and sort newest first
         bool fEntered = false;
         vector<pair<unsigned int, uint256> > vSorted;
-        TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
+        TRY_CRITICAL_BLOCK(pwalletMain->cs_wallet)
         {
             printf("RefreshListCtrl starting\n");
             fEntered = true;
@@ -884,7 +890,7 @@ void CMainFrame::OnIdle(wxIdleEvent& event)
             if (fShutdown)
                 return;
             bool fEntered = false;
-            TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
+            TRY_CRITICAL_BLOCK(pwalletMain->cs_wallet)
             {
                 fEntered = true;
                 uint256& hash = vSorted[i++].second;
@@ -907,7 +913,7 @@ void CMainFrame::OnIdle(wxIdleEvent& event)
         static int64 nLastTime;
         if (GetTime() > nLastTime + 30)
         {
-            TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
+            TRY_CRITICAL_BLOCK(pwalletMain->cs_wallet)
             {
                 nLastTime = GetTime();
                 for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
@@ -931,7 +937,7 @@ void CMainFrame::RefreshStatusColumn()
     if (nTop == nLastTop && pindexLastBest == pindexBest)
         return;
 
-    TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
+    TRY_CRITICAL_BLOCK(pwalletMain->cs_wallet)
     {
         int nStart = nTop;
         int nEnd = min(nStart + 100, m_listCtrl->GetItemCount());
@@ -1051,7 +1057,7 @@ void CMainFrame::OnPaintListCtrl(wxPaintEvent& event)
         // Update listctrl contents
         if (!pwalletMain->vWalletUpdated.empty())
         {
-            TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
+            TRY_CRITICAL_BLOCK(pwalletMain->cs_wallet)
             {
                 string strTop;
                 if (m_listCtrl->GetItemCount())
@@ -1069,7 +1075,7 @@ void CMainFrame::OnPaintListCtrl(wxPaintEvent& event)
         }
 
         // Balance total
-        TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
+        TRY_CRITICAL_BLOCK(pwalletMain->cs_wallet)
         {
             fPaintedBalance = true;
             m_staticTextBalance->SetLabel(FormatMoney(pwalletMain->GetBalance()) + "  ");
@@ -1109,7 +1115,7 @@ void CMainFrame::OnPaintListCtrl(wxPaintEvent& event)
     m_statusBar->SetStatusText(strStatus, 2);
 
     // Update receiving address
-    string strDefaultAddress = PubKeyToAddress(pwalletMain->vchDefaultKey);
+    string strDefaultAddress = CBitcoinAddress(pwalletMain->vchDefaultKey).ToString();
     if (m_textCtrlAddress->GetValue() != strDefaultAddress)
         m_textCtrlAddress->SetValue(strDefaultAddress);
 }
@@ -1216,7 +1222,9 @@ void CMainFrame::OnMenuOptionsEncryptWallet(wxCommandEvent& event)
     fill(strWalletPassTest.begin(), strWalletPassTest.end(), '\0');
     munlock(&strWalletPass[0], strWalletPass.capacity());
     munlock(&strWalletPassTest[0], strWalletPassTest.capacity());
-    wxMessageBox(_("Wallet Encrypted.\nRemember that encrypting your wallet cannot fully protect your bitcoins from being stolen by malware infecting your computer."), "Bitcoin");
+    wxMessageBox(_("Wallet Encrypted.\nBitcoin will close now to finish the encryption process. Remember that encrypting your wallet cannot fully protect your bitcoins from being stolen by malware infecting your computer."), "Bitcoin");
+
+    Close(true);
 }
 
 void CMainFrame::OnMenuOptionsChangeWalletPassphrase(wxCommandEvent& event)
@@ -1238,83 +1246,80 @@ void CMainFrame::OnMenuOptionsChangeWalletPassphrase(wxCommandEvent& event)
     strOldWalletPass = wxGetPasswordFromUser(_("Enter the current passphrase to the wallet."),
                                              _("Passphrase")).ToStdString();
 
-    CRITICAL_BLOCK(pwalletMain->cs_vMasterKey)
-    {
-        bool fWasLocked = pwalletMain->IsLocked();
-        pwalletMain->Lock();
+    bool fWasLocked = pwalletMain->IsLocked();
+    pwalletMain->Lock();
 
-        if (!strOldWalletPass.size() || !pwalletMain->Unlock(strOldWalletPass))
-        {
-            fill(strOldWalletPass.begin(), strOldWalletPass.end(), '\0');
-            munlock(&strOldWalletPass[0], strOldWalletPass.capacity());
-            wxMessageBox(_("The passphrase entered for the wallet decryption was incorrect."), "Bitcoin", wxOK | wxICON_ERROR);
-            return;
-        }
+    if (!strOldWalletPass.size() || !pwalletMain->Unlock(strOldWalletPass))
+    {
+        fill(strOldWalletPass.begin(), strOldWalletPass.end(), '\0');
+        munlock(&strOldWalletPass[0], strOldWalletPass.capacity());
+        wxMessageBox(_("The passphrase entered for the wallet decryption was incorrect."), "Bitcoin", wxOK | wxICON_ERROR);
+        return;
+    }
 
-        if (fWasLocked)
-            pwalletMain->Lock();
+    if (fWasLocked)
+        pwalletMain->Lock();
 
-        string strNewWalletPass;
-        strNewWalletPass.reserve(100);
-        mlock(&strNewWalletPass[0], strNewWalletPass.capacity());
+    string strNewWalletPass;
+    strNewWalletPass.reserve(100);
+    mlock(&strNewWalletPass[0], strNewWalletPass.capacity());
 
-        // obtain new wallet encrypt/decrypt key, from passphrase
-        // Note that the passphrase is not mlock()d during this entry and could potentially
-        // be obtained from disk long after bitcoin has run.
-        strNewWalletPass = wxGetPasswordFromUser(_("Enter the new passphrase for the wallet."),
-                                                 _("Passphrase")).ToStdString();
+    // obtain new wallet encrypt/decrypt key, from passphrase
+    // Note that the passphrase is not mlock()d during this entry and could potentially
+    // be obtained from disk long after bitcoin has run.
+    strNewWalletPass = wxGetPasswordFromUser(_("Enter the new passphrase for the wallet."),
+                                             _("Passphrase")).ToStdString();
 
-        if (!strNewWalletPass.size())
-        {
-            fill(strOldWalletPass.begin(), strOldWalletPass.end(), '\0');
-            fill(strNewWalletPass.begin(), strNewWalletPass.end(), '\0');
-            munlock(&strOldWalletPass[0], strOldWalletPass.capacity());
-            munlock(&strNewWalletPass[0], strNewWalletPass.capacity());
-            wxMessageBox(_("Error: The supplied passphrase was too short."), "Bitcoin", wxOK | wxICON_ERROR);
-            return;
-        }
+    if (!strNewWalletPass.size())
+    {
+        fill(strOldWalletPass.begin(), strOldWalletPass.end(), '\0');
+        fill(strNewWalletPass.begin(), strNewWalletPass.end(), '\0');
+        munlock(&strOldWalletPass[0], strOldWalletPass.capacity());
+        munlock(&strNewWalletPass[0], strNewWalletPass.capacity());
+        wxMessageBox(_("Error: The supplied passphrase was too short."), "Bitcoin", wxOK | wxICON_ERROR);
+        return;
+    }
 
-        string strNewWalletPassTest;
-        strNewWalletPassTest.reserve(100);
-        mlock(&strNewWalletPassTest[0], strNewWalletPassTest.capacity());
+    string strNewWalletPassTest;
+    strNewWalletPassTest.reserve(100);
+    mlock(&strNewWalletPassTest[0], strNewWalletPassTest.capacity());
 
-        // obtain new wallet encrypt/decrypt key, from passphrase
-        // Note that the passphrase is not mlock()d during this entry and could potentially
-        // be obtained from disk long after bitcoin has run.
-        strNewWalletPassTest = wxGetPasswordFromUser(_("Re-enter the new passphrase for the wallet."),
-                                                     _("Passphrase")).ToStdString();
+    // obtain new wallet encrypt/decrypt key, from passphrase
+    // Note that the passphrase is not mlock()d during this entry and could potentially
+    // be obtained from disk long after bitcoin has run.
+    strNewWalletPassTest = wxGetPasswordFromUser(_("Re-enter the new passphrase for the wallet."),
+                                                 _("Passphrase")).ToStdString();
 
-        if (strNewWalletPassTest != strNewWalletPass)
-        {
-            fill(strOldWalletPass.begin(), strOldWalletPass.end(), '\0');
-            fill(strNewWalletPass.begin(), strNewWalletPass.end(), '\0');
-            fill(strNewWalletPassTest.begin(), strNewWalletPassTest.end(), '\0');
-            munlock(&strOldWalletPass[0], strOldWalletPass.capacity());
-            munlock(&strNewWalletPass[0], strNewWalletPass.capacity());
-            munlock(&strNewWalletPassTest[0], strNewWalletPassTest.capacity());
-            wxMessageBox(_("Error: the supplied passphrases didn't match."), "Bitcoin", wxOK | wxICON_ERROR);
-            return;
-        }
+    if (strNewWalletPassTest != strNewWalletPass)
+    {
+        fill(strOldWalletPass.begin(), strOldWalletPass.end(), '\0');
+        fill(strNewWalletPass.begin(), strNewWalletPass.end(), '\0');
+        fill(strNewWalletPassTest.begin(), strNewWalletPassTest.end(), '\0');
+        munlock(&strOldWalletPass[0], strOldWalletPass.capacity());
+        munlock(&strNewWalletPass[0], strNewWalletPass.capacity());
+        munlock(&strNewWalletPassTest[0], strNewWalletPassTest.capacity());
+        wxMessageBox(_("Error: the supplied passphrases didn't match."), "Bitcoin", wxOK | wxICON_ERROR);
+        return;
+    }
 
-        if (!pwalletMain->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass))
-        {
-            fill(strOldWalletPass.begin(), strOldWalletPass.end(), '\0');
-            fill(strNewWalletPass.begin(), strNewWalletPass.end(), '\0');
-            fill(strNewWalletPassTest.begin(), strNewWalletPassTest.end(), '\0');
-            munlock(&strOldWalletPass[0], strOldWalletPass.capacity());
-            munlock(&strNewWalletPass[0], strNewWalletPass.capacity());
-            munlock(&strNewWalletPassTest[0], strNewWalletPassTest.capacity());
-            wxMessageBox(_("The passphrase entered for the wallet decryption was incorrect."), "Bitcoin", wxOK | wxICON_ERROR);
-            return;
-        }
+    if (!pwalletMain->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass))
+    {
         fill(strOldWalletPass.begin(), strOldWalletPass.end(), '\0');
         fill(strNewWalletPass.begin(), strNewWalletPass.end(), '\0');
         fill(strNewWalletPassTest.begin(), strNewWalletPassTest.end(), '\0');
         munlock(&strOldWalletPass[0], strOldWalletPass.capacity());
         munlock(&strNewWalletPass[0], strNewWalletPass.capacity());
         munlock(&strNewWalletPassTest[0], strNewWalletPassTest.capacity());
-        wxMessageBox(_("Wallet Passphrase Changed."), "Bitcoin");
+        wxMessageBox(_("The passphrase entered for the wallet decryption was incorrect."), "Bitcoin", wxOK | wxICON_ERROR);
+        return;
     }
+    fill(strOldWalletPass.begin(), strOldWalletPass.end(), '\0');
+    fill(strNewWalletPass.begin(), strNewWalletPass.end(), '\0');
+    fill(strNewWalletPassTest.begin(), strNewWalletPassTest.end(), '\0');
+    munlock(&strOldWalletPass[0], strOldWalletPass.capacity());
+    munlock(&strNewWalletPass[0], strNewWalletPass.capacity());
+    munlock(&strNewWalletPassTest[0], strNewWalletPassTest.capacity());
+    wxMessageBox(_("Wallet Passphrase Changed."), "Bitcoin");
 }
 
 void CMainFrame::OnMenuOptionsOptions(wxCommandEvent& event)
@@ -1378,21 +1383,21 @@ void CMainFrame::OnButtonNew(wxCommandEvent& event)
     string strName = dialog.GetValue();
 
     string strAddress;
-    CRITICAL_BLOCK(pwalletMain->cs_vMasterKey)
-    {
-        bool fWasLocked = pwalletMain->IsLocked();
-        if (!GetWalletPassphrase())
-            return;
 
-        // Generate new key
-        strAddress = PubKeyToAddress(pwalletMain->GetOrReuseKeyFromPool());
+    bool fWasLocked = pwalletMain->IsLocked();
+    if (!GetWalletPassphrase())
+        return;
 
-        if (fWasLocked)
-            pwalletMain->Lock();
-    }
+    // Generate new key
+    std::vector<unsigned char> newKey;
+    pwalletMain->GetKeyFromPool(newKey, true);
+    strAddress = CBitcoinAddress(newKey).ToString();
+
+    if (fWasLocked)
+        pwalletMain->Lock();
 
     // Save
-    CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
+    CRITICAL_BLOCK(pwalletMain->cs_wallet)
         pwalletMain->SetAddressBookName(strAddress, strName);
     SetDefaultReceivingAddress(strAddress);
 }
@@ -1411,7 +1416,7 @@ void CMainFrame::OnListItemActivated(wxListEvent& event)
 {
     uint256 hash((string)GetItemText(m_listCtrl, event.GetIndex(), 1));
     CWalletTx wtx;
-    CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
+    CRITICAL_BLOCK(pwalletMain->cs_wallet)
     {
         map<uint256, CWalletTx>::iterator mi = pwalletMain->mapWallet.find(hash);
         if (mi == pwalletMain->mapWallet.end())
@@ -1442,7 +1447,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails
 #ifdef __WXMSW__
     SetSize(nScaleX * GetSize().GetWidth(), nScaleY * GetSize().GetHeight());
 #endif
-    CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
+    CRITICAL_BLOCK(pwalletMain->cs_wallet)
     {
         string strHTML;
         strHTML.reserve(4000);
@@ -1494,17 +1499,16 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails
                 {
                     if (pwalletMain->IsMine(txout))
                     {
-                        vector<unsigned char> vchPubKey;
-                        if (ExtractPubKey(txout.scriptPubKey, pwalletMain, vchPubKey))
+                        CBitcoinAddress address;
+                        if (ExtractAddress(txout.scriptPubKey, pwalletMain, address))
                         {
-                            string strAddress = PubKeyToAddress(vchPubKey);
-                            if (pwalletMain->mapAddressBook.count(strAddress))
+                            if (pwalletMain->mapAddressBook.count(address))
                             {
                                 strHTML += string() + _("<b>From:</b> ") + _("unknown") + "<br>";
                                 strHTML += _("<b>To:</b> ");
-                                strHTML += HtmlEscape(strAddress);
-                                if (!pwalletMain->mapAddressBook[strAddress].empty())
-                                    strHTML += _(" (yours, label: ") + pwalletMain->mapAddressBook[strAddress] + ")";
+                                strHTML += HtmlEscape(address.ToString());
+                                if (!pwalletMain->mapAddressBook[address].empty())
+                                    strHTML += _(" (yours, label: ") + pwalletMain->mapAddressBook[address] + ")";
                                 else
                                     strHTML += _(" (yours)");
                                 strHTML += "<br>";
@@ -1580,13 +1584,13 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails
                     if (wtx.mapValue["to"].empty())
                     {
                         // Offline transaction
-                        uint160 hash160;
-                        if (ExtractHash160(txout.scriptPubKey, hash160))
+                        CBitcoinAddress address;
+                        if (ExtractAddress(txout.scriptPubKey, pwalletMain, address))
                         {
-                            string strAddress = Hash160ToAddress(hash160);
+                            string strAddress = address.ToString();
                             strHTML += _("<b>To:</b> ");
-                            if (pwalletMain->mapAddressBook.count(strAddress) && !pwalletMain->mapAddressBook[strAddress].empty())
-                                strHTML += pwalletMain->mapAddressBook[strAddress] + " ";
+                            if (pwalletMain->mapAddressBook.count(address) && !pwalletMain->mapAddressBook[address].empty())
+                                strHTML += pwalletMain->mapAddressBook[address] + " ";
                             strHTML += strAddress;
                             strHTML += "<br>";
                         }
@@ -1654,7 +1658,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails
             strHTML += HtmlEscape(wtx.ToString(), true);
 
             strHTML += "<br><b>Inputs:</b><br>";
-            CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
+            CRITICAL_BLOCK(pwalletMain->cs_wallet)
             {
                 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
                 {
@@ -1800,7 +1804,11 @@ void SetStartOnSystemStartup(bool fAutoStart)
 {
     if (!fAutoStart)
     {
+#if defined(BOOST_FILESYSTEM_VERSION) && BOOST_FILESYSTEM_VERSION >= 3
+        unlink(GetAutostartFilePath().string().c_str());
+#else
         unlink(GetAutostartFilePath().native_file_string().c_str());
+#endif
     }
     else
     {
@@ -2147,43 +2155,47 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event)
         }
 
         // Parse bitcoin address
-        uint160 hash160;
-        bool fBitcoinAddress = AddressToHash160(strAddress, hash160);
+        CBitcoinAddress address(strAddress);
+        bool fBitcoinAddress = address.IsValid();
 
         if (fBitcoinAddress)
         {
+            bool fWasLocked = pwalletMain->IsLocked();
+            if (!GetWalletPassphrase())
+                return;
+
+            string strError;
            CRITICAL_BLOCK(cs_main)
-            CRITICAL_BLOCK(pwalletMain->cs_vMasterKey)
+            CRITICAL_BLOCK(pwalletMain->cs_wallet)
            {
-                bool fWasLocked = pwalletMain->IsLocked();
-                if (!GetWalletPassphrase())
-                    return;
-
                 // Send to bitcoin address
                 CScript scriptPubKey;
-                scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
-
-                string strError = pwalletMain->SendMoney(scriptPubKey, nValue, wtx, true);
-                if (strError == "")
-                    wxMessageBox(_("Payment sent  "), _("Sending..."));
-                else if (strError == "ABORTED")
-                {
-                    if (fWasLocked)
-                        pwalletMain->Lock();
-                    return; // leave send dialog open
-                }
-                else
-                {
-                    wxMessageBox(strError + "  ", _("Sending..."));
-                    EndModal(false);
-                    if (fWasLocked)
-                        pwalletMain->Lock();
-                    return;
-                }
+                scriptPubKey.SetBitcoinAddress(address);
 
+                strError = pwalletMain->SendMoney(scriptPubKey, nValue, wtx, true);
+            }
+            if (strError == "")
+            {
+                pframeMain->RefreshListCtrl();
+                wxMessageBox(_("Payment sent  "), _("Sending..."));
+            }
+            else if (strError == "ABORTED")
+            {
                 if (fWasLocked)
                     pwalletMain->Lock();
-           }
+                return; // leave send dialog open
+            }
+            else
+            {
+                wxMessageBox(strError + "  ", _("Sending..."));
+                EndModal(false);
+                if (fWasLocked)
+                    pwalletMain->Lock();
+                return;
+            }
+
+            if (fWasLocked)
+                pwalletMain->Lock();
         }
         else
         {
@@ -2204,8 +2216,8 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event)
                 return;
         }
 
-        CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
-            if (!pwalletMain->mapAddressBook.count(strAddress))
+        CRITICAL_BLOCK(pwalletMain->cs_wallet)
+            if (!pwalletMain->mapAddressBook.count(address))
                 pwalletMain->SetAddressBookName(strAddress, "");
 
         EndModal(true);
@@ -2456,83 +2468,89 @@ void CSendingDialog::OnReply2(CDataStream& vRecv)
             return;
     }
 
-    CRITICAL_BLOCK(cs_main)
+    // Pay
+    if (!Status(_("Creating transaction...")))
+        return;
+    if (nPrice + nTransactionFee > pwalletMain->GetBalance())
     {
-        // Pay
-        if (!Status(_("Creating transaction...")))
-            return;
-        if (nPrice + nTransactionFee > pwalletMain->GetBalance())
-        {
-            Error(_("Insufficient funds"));
-            return;
-        }
+        Error(_("Insufficient funds"));
+        return;
+    }
 
-        CReserveKey reservekey(pwalletMain);
-        int64 nFeeRequired;
-        CRITICAL_BLOCK(pwalletMain->cs_vMasterKey)
-        {
-            bool fWasLocked = pwalletMain->IsLocked();
-            if (!GetWalletPassphrase())
-                return;
+    CReserveKey reservekey(pwalletMain);
+    int64 nFeeRequired;
+    bool fWasLocked = pwalletMain->IsLocked();
+    if (!GetWalletPassphrase())
+        return;
 
-            if (!pwalletMain->CreateTransaction(scriptPubKey, nPrice, wtx, reservekey, nFeeRequired))
-            {
-                if (nPrice + nFeeRequired > pwalletMain->GetBalance())
-                    Error(strprintf(_("This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds"), FormatMoney(nFeeRequired).c_str()));
-                else
-                    Error(_("Transaction creation failed"));
-                return;
-            }
+    bool fTxCreated = false;
+    CRITICAL_BLOCK(cs_main)
+    CRITICAL_BLOCK(pwalletMain->cs_wallet)
+    {
+        fTxCreated = pwalletMain->CreateTransaction(scriptPubKey, nPrice, wtx, reservekey, nFeeRequired);
+    }
+    if (!fTxCreated)
+    {
+        if (nPrice + nFeeRequired > pwalletMain->GetBalance())
+            Error(strprintf(_("This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds"), FormatMoney(nFeeRequired).c_str()));
+        else
+            Error(_("Transaction creation failed"));
+        return;
+    }
 
-            if (fWasLocked)
-                pwalletMain->Lock();
-       }
+    if (fWasLocked)
+        pwalletMain->Lock();
 
-        // Transaction fee
-        if (!ThreadSafeAskFee(nFeeRequired, _("Sending..."), this))
-        {
-            Error(_("Transaction aborted"));
-            return;
-        }
+    // Transaction fee
+    if (!ThreadSafeAskFee(nFeeRequired, _("Sending..."), this))
+    {
+        Error(_("Transaction aborted"));
+        return;
+    }
 
-        // Make sure we're still connected
-        CNode* pnode = ConnectNode(addr, 2 * 60 * 60);
-        if (!pnode)
-        {
-            Error(_("Lost connection, transaction cancelled"));
-            return;
-        }
+    // Make sure we're still connected
+    CNode* pnode = ConnectNode(addr, 2 * 60 * 60);
+    if (!pnode)
+    {
+        Error(_("Lost connection, transaction cancelled"));
+        return;
+    }
 
-        // Last chance to cancel
-        Sleep(50);
+    // Last chance to cancel
+    Sleep(50);
+    if (!Status())
+        return;
+    fCanCancel = false;
+    if (fAbort)
+    {
+        fCanCancel = true;
         if (!Status())
             return;
         fCanCancel = false;
-        if (fAbort)
-        {
-            fCanCancel = true;
-            if (!Status())
-                return;
-            fCanCancel = false;
-        }
-        if (!Status(_("Sending payment...")))
-            return;
+    }
+    if (!Status(_("Sending payment...")))
+        return;
 
-        // Commit
-        if (!pwalletMain->CommitTransaction(wtx, reservekey))
-        {
-            Error(_("The transaction was rejected.  This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here."));
-            return;
-        }
+    // Commit
+    bool fTxCommitted = false;
+    CRITICAL_BLOCK(cs_main)
+    CRITICAL_BLOCK(pwalletMain->cs_wallet)
+    {
+        fTxCommitted = pwalletMain->CommitTransaction(wtx, reservekey);
+    }
+    if (!fTxCommitted)
+    {
+        Error(_("The transaction was rejected.  This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here."));
+        return;
+    }
 
-        // Send payment tx to seller, with response going to OnReply3 via event handler
-        CWalletTx wtxSend = wtx;
-        wtxSend.fFromMe = false;
-        pnode->PushRequest("submitorder", wtxSend, SendingDialogOnReply3, this);
+    // Send payment tx to seller, with response going to OnReply3 via event handler
+    CWalletTx wtxSend = wtx;
+    wtxSend.fFromMe = false;
+    pnode->PushRequest("submitorder", wtxSend, SendingDialogOnReply3, this);
 
-        Status(_("Waiting for confirmation..."));
-        MainFrameRepaint();
-    }
+    Status(_("Waiting for confirmation..."));
+    MainFrameRepaint();
 }
 
 void SendingDialogOnReply3(void* parg, CDataStream& vRecv)
@@ -2613,19 +2631,17 @@ CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInit
     m_listCtrlReceiving->SetFocus();
 
     // Fill listctrl with address book data
-    CRITICAL_BLOCK(pwalletMain->cs_KeyStore)
-    CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
+    CRITICAL_BLOCK(pwalletMain->cs_wallet)
     {
         string strDefaultReceiving = (string)pframeMain->m_textCtrlAddress->GetValue();
-        BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook)
+        BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
         {
-            string strAddress = item.first;
+            const CBitcoinAddress& address = item.first;
             string strName = item.second;
-            uint160 hash160;
-            bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
+            bool fMine = pwalletMain->HaveKey(address);
             wxListCtrl* plistCtrl = fMine ? m_listCtrlReceiving : m_listCtrlSending;
-            int nIndex = InsertLine(plistCtrl, strName, strAddress);
-            if (strAddress == (fMine ? strDefaultReceiving : string(strInitSelected)))
+            int nIndex = InsertLine(plistCtrl, strName, address.ToString());
+            if (address.ToString() == (fMine ? strDefaultReceiving : string(strInitSelected)))
                 plistCtrl->SetItemState(nIndex, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED);
         }
     }
@@ -2676,7 +2692,7 @@ void CAddressBookDialog::OnListEndLabelEdit(wxListEvent& event)
     if (event.IsEditCancelled())
         return;
     string strAddress = (string)GetItemText(m_listCtrl, event.GetIndex(), 1);
-    CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
+    CRITICAL_BLOCK(pwalletMain->cs_wallet)
         pwalletMain->SetAddressBookName(strAddress, string(event.GetText()));
     pframeMain->RefreshListCtrl();
 }
@@ -2712,7 +2728,7 @@ void CAddressBookDialog::OnButtonDelete(wxCommandEvent& event)
         if (m_listCtrl->GetItemState(nIndex, wxLIST_STATE_SELECTED))
         {
             string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1);
-            CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
+            CRITICAL_BLOCK(pwalletMain->cs_wallet)
                 pwalletMain->DelAddressBookName(strAddress);
             m_listCtrl->DeleteItem(nIndex);
         }
@@ -2732,8 +2748,8 @@ void CAddressBookDialog::OnButtonCopy(wxCommandEvent& event)
 
 bool CAddressBookDialog::CheckIfMine(const string& strAddress, const string& strTitle)
 {
-    uint160 hash160;
-    bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
+    CBitcoinAddress address(strAddress);
+    bool fMine = address.IsValid() && pwalletMain->HaveKey(address);
     if (fMine)
         wxMessageBox(_("This is one of your own addresses for receiving payments and cannot be entered in the address book.  "), strTitle);
     return fMine;
@@ -2772,7 +2788,7 @@ void CAddressBookDialog::OnButtonEdit(wxCommandEvent& event)
     }
 
     // Write back
-    CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
+    CRITICAL_BLOCK(pwalletMain->cs_wallet)
     {
         if (strAddress != strAddressOrg)
             pwalletMain->DelAddressBookName(strAddressOrg);
@@ -2812,22 +2828,21 @@ void CAddressBookDialog::OnButtonNew(wxCommandEvent& event)
             return;
         strName = dialog.GetValue();
 
-        CRITICAL_BLOCK(pwalletMain->cs_vMasterKey)
-        {
-            bool fWasLocked = pwalletMain->IsLocked();
-            if (!GetWalletPassphrase())
-                return;
+        bool fWasLocked = pwalletMain->IsLocked();
+        if (!GetWalletPassphrase())
+            return;
 
-            // Generate new key
-            strAddress = PubKeyToAddress(pwalletMain->GetOrReuseKeyFromPool());
+        // Generate new key
+        std::vector<unsigned char> newKey;
+        pwalletMain->GetKeyFromPool(newKey, true);
+        strAddress = CBitcoinAddress(newKey).ToString();
 
-            if (fWasLocked)
-                pwalletMain->Lock();
-        }
+        if (fWasLocked)
+            pwalletMain->Lock();
     }
 
     // Add to list and select it
-    CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
+    CRITICAL_BLOCK(pwalletMain->cs_wallet)
         pwalletMain->SetAddressBookName(strAddress, strName);
     int nIndex = InsertLine(m_listCtrl, strName, strAddress);
     SetSelection(m_listCtrl, nIndex);