Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
rpcdump.cpp
Go to the documentation of this file.
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.
4 
5 #include "init.h" // for pwalletMain
6 #include "bitcoinrpc.h"
7 #include "ui_interface.h"
8 #include "base58.h"
9 
10 #include <boost/lexical_cast.hpp>
11 
12 #define printf OutputDebugStringF
13 
14 using namespace json_spirit;
15 using namespace std;
16 
17 class CTxDump
18 {
19 public:
22  bool fSpent;
24  int nOut;
25  CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
26  {
27  pindex = NULL;
28  nValue = 0;
29  fSpent = false;
30  this->ptx = ptx;
31  this->nOut = nOut;
32  }
33 };
34 
35 Value importprivkey(const Array& params, bool fHelp)
36 {
37  if (fHelp || params.size() < 1 || params.size() > 3)
38  throw runtime_error(
39  "importprivkey <feathercoinprivkey> [label] [rescan=true]\n"
40  "Adds a private key (as returned by dumpprivkey) to your wallet.");
41 
42  string strSecret = params[0].get_str();
43  string strLabel = "";
44  if (params.size() > 1)
45  strLabel = params[1].get_str();
46 
47  // Whether to perform rescan after import
48  bool fRescan = true;
49  if (params.size() > 2)
50  fRescan = params[2].get_bool();
51 
52  CBitcoinSecret vchSecret;
53  bool fGood = vchSecret.SetString(strSecret);
54 
55  if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
56 
57  CKey key = vchSecret.GetKey();
58  CPubKey pubkey = key.GetPubKey();
59  CKeyID vchAddress = pubkey.GetID();
60  {
62 
64  pwalletMain->SetAddressBookName(vchAddress, strLabel);
65 
66  if (!pwalletMain->AddKeyPubKey(key, pubkey))
67  throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
68 
69  if (fRescan) {
72  }
73  }
74 
75  return Value::null;
76 }
77 
78 Value dumpprivkey(const Array& params, bool fHelp)
79 {
80  if (fHelp || params.size() != 1)
81  throw runtime_error(
82  "dumpprivkey <feathercoinaddress>\n"
83  "Reveals the private key corresponding to <feathercoinaddress>.");
84 
85  string strAddress = params[0].get_str();
86  CBitcoinAddress address;
87  if (!address.SetString(strAddress))
88  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Feathercoin address");
89  CKeyID keyID;
90  if (!address.GetKeyID(keyID))
91  throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
92  CKey vchSecret;
93  if (!pwalletMain->GetKey(keyID, vchSecret))
94  throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
95  return CBitcoinSecret(vchSecret).ToString();
96 }
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: wallet.cpp:49
CKey GetKey()
Definition: base58.h:415
CCriticalSection cs_wallet
Definition: wallet.h:83
int64 nValue
Definition: rpcdump.cpp:21
bool fSpent
Definition: rpcdump.cpp:22
CCriticalSection cs_main
Definition: main.cpp:32
Object JSONRPCError(int code, const string &message)
Definition: bitcoinrpc.cpp:46
void ReacceptWalletTransactions()
Definition: wallet.cpp:796
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: keystore.cpp:142
Value importprivkey(const Array &params, bool fHelp)
Definition: rpcdump.cpp:35
bool SetAddressBookName(const CTxDestination &address, const std::string &strName)
Definition: wallet.cpp:1463
bool GetKeyID(CKeyID &keyID) const
Definition: base58.h:365
void MarkDirty()
Definition: wallet.cpp:365
bool SetString(const char *pszSecret)
Definition: base58.h:440
#define LOCK2(cs1, cs2)
Definition: sync.h:109
int nOut
Definition: rpcdump.cpp:24
CPubKey GetPubKey() const
Definition: key.cpp:312
A base58-encoded secret key.
Definition: base58.h:398
An encapsulated public key.
Definition: key.h:40
static const Value_impl null
CWallet * pwalletMain
Definition: init.cpp:31
std::string ToString() const
Definition: base58.h:229
Config::Array_type Array
const String_type & get_str() const
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:367
CBlockIndex * pindex
Definition: rpcdump.cpp:20
CBlockIndex * pindexGenesisBlock
Definition: main.cpp:40
int ScanForWalletTransactions(CBlockIndex *pindexStart, bool fUpdate=false)
Definition: wallet.cpp:774
CTxDump(CWalletTx *ptx=NULL, int nOut=-1)
Definition: rpcdump.cpp:25
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:1626
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:24
bool SetString(const char *psz)
Definition: base58.h:206
An encapsulated private key.
Definition: key.h:172
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)
CKeyID GetID() const
Definition: key.h:129
CWalletTx * ptx
Definition: rpcdump.cpp:23
Value dumpprivkey(const Array &params, bool fHelp)
Definition: rpcdump.cpp:78
long long int64
Definition: serialize.h:25