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"
10 #include <boost/lexical_cast.hpp>
12 #define printf OutputDebugStringF
14 using namespace json_spirit
;
25 CTxDump(CWalletTx
* ptx
= NULL
, int nOut
= -1)
35 Value
importprivkey(const Array
& params
, bool fHelp
)
37 if (fHelp
|| params
.size() < 1 || params
.size() > 2)
39 "importprivkey <bitcoinprivkey> [label]\n"
40 "Adds a private key (as returned by dumpprivkey) to your wallet.");
42 string strSecret
= params
[0].get_str();
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");
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();
72 Value
dumpprivkey(const Array
& params
, bool fHelp
)
74 if (fHelp
|| params
.size() != 1)
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");
84 if (!address
.GetKeyID(keyID
))
85 throw JSONRPCError(-3, "Address does not refer to a key");
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();