From 1c42569648f41fca2785468bead9e052b930ccb2 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Thu, 25 Jul 2013 08:47:35 -0300 Subject: [PATCH] wallet rpc with username --- src/rpcdump.cpp | 67 +++++++++++++++++++++++++------------------------ src/wallet.cpp | 11 ++++++++ src/wallet.h | 2 ++ 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/rpcdump.cpp b/src/rpcdump.cpp index dcfb023f..37e427d9 100644 --- a/src/rpcdump.cpp +++ b/src/rpcdump.cpp @@ -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 [label] [rescan=true]\n" + "importprivkey [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) 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) 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) 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...\n", CBitcoinAddress(keyid).ToString().c_str()); + printf("Importing %s (username=%s)...\n", CBitcoinAddress(keyid).ToString().c_str(), + strUsername.c_str()); + if (!fUsername) { + printf("Missing username, skipping.\n"); + fGood = false; + continue; + } 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) { if (fHelp || params.size() != 1) throw runtime_error( - "dumpprivkey \n" - "Reveals the private key corresponding to ."); + "dumpprivkey \n" + "Reveals the private key corresponding to ."); 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) 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) 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()); } } } diff --git a/src/wallet.cpp b/src/wallet.cpp index 507a37c9..8a843305 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -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::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 &vchCryptedSecret) { return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); diff --git a/src/wallet.h b/src/wallet.h index 9053422b..11d23061 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -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; }