Browse Source

wallet rpc with username

miguelfreitas
Miguel Freitas 12 years ago
parent
commit
1c42569648
  1. 67
      src/rpcdump.cpp
  2. 11
      src/wallet.cpp
  3. 2
      src/wallet.h

67
src/rpcdump.cpp

@ -67,15 +67,13 @@ std::string DecodeDumpString(const std::string &str) { @@ -67,15 +67,13 @@ std::string DecodeDumpString(const std::string &str) {
Value importprivkey(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 3)
if (fHelp || params.size() < 2 || params.size() > 3)
throw runtime_error(
"importprivkey <bitcoinprivkey> [label] [rescan=true]\n"
"importprivkey <bitcoinprivkey> <username> [rescan=true]\n"
"Adds a private key (as returned by dumpprivkey) to your wallet.");
string strSecret = params[0].get_str();
string strLabel = "";
if (params.size() > 1)
strLabel = params[1].get_str();
string strUsername = params[1].get_str();
// Whether to perform rescan after import
bool fRescan = true;
@ -94,7 +92,6 @@ Value importprivkey(const Array& params, bool fHelp) @@ -94,7 +92,6 @@ Value importprivkey(const Array& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
pwalletMain->MarkDirty();
pwalletMain->SetAddressBookName(vchAddress, strLabel);
// Don't throw error in case a key is already there
if (pwalletMain->HaveKey(vchAddress))
@ -103,6 +100,9 @@ Value importprivkey(const Array& params, bool fHelp) @@ -103,6 +100,9 @@ Value importprivkey(const Array& params, bool fHelp)
if (!pwalletMain->AddKeyPubKey(key, pubkey))
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = GetTime();
pwalletMain->mapKeyMetadata[vchAddress].username = strUsername;
if (fRescan) {
pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
pwalletMain->ReacceptWalletTransactions();
@ -151,28 +151,29 @@ Value importwallet(const Array& params, bool fHelp) @@ -151,28 +151,29 @@ Value importwallet(const Array& params, bool fHelp)
continue;
}
int64 nTime = DecodeDumpTime(vstr[1]);
std::string strLabel;
bool fLabel = true;
std::string strUsername;
bool fUsername = false;
for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
if (boost::algorithm::starts_with(vstr[nStr], "#"))
break;
if (vstr[nStr] == "change=1")
fLabel = false;
if (vstr[nStr] == "reserve=1")
fLabel = false;
if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
strLabel = DecodeDumpString(vstr[nStr].substr(6));
fLabel = true;
if (boost::algorithm::starts_with(vstr[nStr], "username=")) {
strUsername = DecodeDumpString(vstr[nStr].substr(9));
fUsername = true;
}
}
printf("Importing %s (username=%s)...\n", CBitcoinAddress(keyid).ToString().c_str(),
strUsername.c_str());
if (!fUsername) {
printf("Missing username, skipping.\n");
fGood = false;
continue;
}
printf("Importing %s...\n", CBitcoinAddress(keyid).ToString().c_str());
if (!pwalletMain->AddKeyPubKey(key, pubkey)) {
fGood = false;
continue;
}
pwalletMain->mapKeyMetadata[keyid].nCreateTime = nTime;
if (fLabel)
pwalletMain->SetAddressBookName(keyid, strLabel);
pwalletMain->mapKeyMetadata[keyid].username = strUsername;
nTimeBegin = std::min(nTimeBegin, nTime);
}
file.close();
@ -196,21 +197,18 @@ Value dumpprivkey(const Array& params, bool fHelp) @@ -196,21 +197,18 @@ Value dumpprivkey(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"dumpprivkey <bitcoinaddress>\n"
"Reveals the private key corresponding to <bitcoinaddress>.");
"dumpprivkey <username>\n"
"Reveals the private key corresponding to <username>.");
EnsureWalletIsUnlocked();
string strAddress = params[0].get_str();
CBitcoinAddress address;
if (!address.SetString(strAddress))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
string strUsername = params[0].get_str();
CKeyID keyID;
if (!address.GetKeyID(keyID))
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
if (!pwalletMain->GetKeyIdFromUsername(strUsername, keyID))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Username not found");
CKey vchSecret;
if (!pwalletMain->GetKey(keyID, vchSecret))
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for username " + strUsername + " is not known");
return CBitcoinSecret(vchSecret).ToString();
}
@ -243,7 +241,7 @@ Value dumpwallet(const Array& params, bool fHelp) @@ -243,7 +241,7 @@ Value dumpwallet(const Array& params, bool fHelp)
std::sort(vKeyBirth.begin(), vKeyBirth.end());
// produce output
file << strprintf("# Wallet dump created by Bitcoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
file << strprintf("# Wallet dump created by Twister %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
file << strprintf("# * Best block at time of backup was %i (%s),\n", nBestHeight, hashBestChain.ToString().c_str());
file << strprintf("# mined on %s\n", EncodeDumpTime(pindexBest->nTime).c_str());
@ -254,12 +252,15 @@ Value dumpwallet(const Array& params, bool fHelp) @@ -254,12 +252,15 @@ Value dumpwallet(const Array& params, bool fHelp)
std::string strAddr = CBitcoinAddress(keyid).ToString();
CKey key;
if (pwalletMain->GetKey(keyid, key)) {
if (pwalletMain->mapAddressBook.count(keyid)) {
file << strprintf("%s %s label=%s # addr=%s\n", CBitcoinSecret(key).ToString().c_str(), strTime.c_str(), EncodeDumpString(pwalletMain->mapAddressBook[keyid]).c_str(), strAddr.c_str());
} else if (setKeyPool.count(keyid)) {
file << strprintf("%s %s reserve=1 # addr=%s\n", CBitcoinSecret(key).ToString().c_str(), strTime.c_str(), strAddr.c_str());
if (pwalletMain->mapKeyMetadata.count(keyid)) {
file << strprintf("%s %s username=%s # addr=%s\n",
CBitcoinSecret(key).ToString().c_str(),
strTime.c_str(),
EncodeDumpString(pwalletMain->mapKeyMetadata[keyid].username).c_str(), strAddr.c_str());
} else {
file << strprintf("%s %s change=1 # addr=%s\n", CBitcoinSecret(key).ToString().c_str(), strTime.c_str(), strAddr.c_str());
file << strprintf("%s %s noname=1 # addr=%s\n",
CBitcoinSecret(key).ToString().c_str(),
strTime.c_str(), strAddr.c_str());
}
}
}

11
src/wallet.cpp

@ -96,6 +96,17 @@ bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta) @@ -96,6 +96,17 @@ bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
return true;
}
bool CWallet::GetKeyIdFromUsername(std::string username, CKeyID &keyid)
{
for (std::map<CKeyID, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++) {
if (it->second.username == username) {
keyid = it->first;
return true;
}
}
return false;
}
bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
{
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);

2
src/wallet.h

@ -132,6 +132,8 @@ public: @@ -132,6 +132,8 @@ public:
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
// Load metadata (used by LoadWallet)
bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
// Search metadata for a given username
bool GetKeyIdFromUsername(std::string username, CKeyID &keyid);
bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }

Loading…
Cancel
Save