Update Bugreport Links
[bitcoinplatinum.git] / src / rpcdump.cpp
blob30e504a095409ea77b85c3cf746f2d59e94ce5d0
1 // Copyright (c) 2009-2012 Bitcoin Developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "init.h" // for pwalletMain
6 #include "bitcoinrpc.h"
7 #include "ui_interface.h"
8 #include "base58.h"
10 #include <boost/lexical_cast.hpp>
12 #define printf OutputDebugStringF
14 using namespace json_spirit;
15 using namespace std;
17 class CTxDump
19 public:
20 CBlockIndex *pindex;
21 int64 nValue;
22 bool fSpent;
23 CWalletTx* ptx;
24 int nOut;
25 CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
27 pindex = NULL;
28 nValue = 0;
29 fSpent = false;
30 this->ptx = ptx;
31 this->nOut = nOut;
35 Value importprivkey(const Array& params, bool fHelp)
37 if (fHelp || params.size() < 1 || params.size() > 2)
38 throw runtime_error(
39 "importprivkey <bitcoinprivkey> [label]\n"
40 "Adds a private key (as returned by dumpprivkey) to your wallet.");
42 string strSecret = params[0].get_str();
43 string strLabel = "";
44 if (params.size() > 1)
45 strLabel = params[1].get_str();
46 CBitcoinSecret vchSecret;
47 bool fGood = vchSecret.SetString(strSecret);
49 if (!fGood) throw JSONRPCError(-5,"Invalid private key");
51 CKey key;
52 bool fCompressed;
53 CSecret secret = vchSecret.GetSecret(fCompressed);
54 key.SetSecret(secret, fCompressed);
55 CKeyID vchAddress = key.GetPubKey().GetID();
57 LOCK2(cs_main, pwalletMain->cs_wallet);
59 pwalletMain->MarkDirty();
60 pwalletMain->SetAddressBookName(vchAddress, strLabel);
62 if (!pwalletMain->AddKey(key))
63 throw JSONRPCError(-4,"Error adding key to wallet");
65 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
66 pwalletMain->ReacceptWalletTransactions();
69 return Value::null;
72 Value dumpprivkey(const Array& params, bool fHelp)
74 if (fHelp || params.size() != 1)
75 throw runtime_error(
76 "dumpprivkey <bitcoinaddress>\n"
77 "Reveals the private key corresponding to <bitcoinaddress>.");
79 string strAddress = params[0].get_str();
80 CBitcoinAddress address;
81 if (!address.SetString(strAddress))
82 throw JSONRPCError(-5, "Invalid Bitcoin address");
83 CKeyID keyID;
84 if (!address.GetKeyID(keyID))
85 throw JSONRPCError(-3, "Address does not refer to a key");
86 CSecret vchSecret;
87 bool fCompressed;
88 if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
89 throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
90 return CBitcoinSecret(vchSecret, fCompressed).ToString();