Skip to content
This repository was archived by the owner on Jul 23, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,81 @@ Value dumpwallet(const Array& params, bool fHelp)
file.close();
return Value::null;
}

Value dumpwalletjson(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"dumpwallet <filename>\n"
"Dumps all wallet keys in a JSON format.");

EnsureWalletIsUnlocked();

ofstream file;
file.open(params[0].get_str().c_str());
if (!file.is_open())
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");

std::map<CKeyID, int64_t> mapKeyBirth;

std::set<CKeyID> setKeyPool;

pwalletMain->GetKeyBirthTimes(mapKeyBirth);

pwalletMain->GetAllReserveKeys(setKeyPool);

// sort time/key pairs
std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
for (std::map<CKeyID, int64_t>::const_iterator it = mapKeyBirth.begin(); it != mapKeyBirth.end(); it++) {
vKeyBirth.push_back(std::make_pair(it->second, it->first));
}
mapKeyBirth.clear();
std::sort(vKeyBirth.begin(), vKeyBirth.end());

Object payload;
Array privateKeys;
for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
const CKeyID &keyid = it->second;
std::string strAddr = CDigitalNoteAddress(keyid).ToString();

CKey key;
if (pwalletMain->GetKey(keyid, key)) {
Object privateKeyAndAddress;
privateKeyAndAddress.push_back(Pair("privateKey", CDigitalNoteSecret(key).ToString()));
privateKeyAndAddress.push_back(Pair("address", strAddr));
privateKeys.push_back(privateKeyAndAddress);
}
}
payload.push_back(Pair("privateKeys", privateKeys));
file << write_string(Value(payload), true);
file.close();
return Value::null;
}

Value getaddressfromprivkey(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 3)
throw runtime_error(
"getaddressfromprivkey <DigitalNoteprivkey>\n"
"Returns DigitalNote address from private key.");

string strSecret = params[0].get_str();
string strLabel = "";
if (params.size() > 1)
strLabel = params[1].get_str();

CDigitalNoteSecret vchSecret;
bool fGood = vchSecret.SetString(strSecret);

if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");

CKey key = vchSecret.GetKey();
CPubKey pubkey = key.GetPubKey();
assert(key.VerifyPubKey(pubkey));
CKeyID vchAddress = pubkey.GetID();

Object payload;
payload.push_back(Pair("address", CDigitalNoteAddress(vchAddress).ToString()));
return Value(payload);
}

2 changes: 2 additions & 0 deletions src/rpcserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ static const CRPCCommand vRPCCommands[] =
{ "listsinceblock", &listsinceblock, false, false, true },
{ "dumpprivkey", &dumpprivkey, false, false, true },
{ "dumpwallet", &dumpwallet, true, false, true },
{ "dumpwalletjson", &dumpwalletjson, true, false, true },
{ "getaddressfromprivkey", &getaddressfromprivkey, true, false, true },
{ "importprivkey", &importprivkey, false, false, true },
{ "importwallet", &importwallet, false, false, true },
{ "importaddress", &importaddress, false, false, true },
Expand Down
2 changes: 2 additions & 0 deletions src/rpcserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ extern json_spirit::Value importwallet(const json_spirit::Array& params, bool fH
extern json_spirit::Value importaddress(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value dumpwalletjson(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getaddressfromprivkey(const json_spirit::Array& params, bool fHelp);

extern json_spirit::Value sendalert(const json_spirit::Array& params, bool fHelp);

Expand Down