mirror of
https://github.com/twisterarmy/twister-core.git
synced 2025-01-10 23:07:52 +00:00
wallet rpc with username
This commit is contained in:
parent
76f8a901a7
commit
1c42569648
@ -67,15 +67,13 @@ std::string DecodeDumpString(const std::string &str) {
|
|||||||
|
|
||||||
Value importprivkey(const Array& params, bool fHelp)
|
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(
|
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.");
|
"Adds a private key (as returned by dumpprivkey) to your wallet.");
|
||||||
|
|
||||||
string strSecret = params[0].get_str();
|
string strSecret = params[0].get_str();
|
||||||
string strLabel = "";
|
string strUsername = params[1].get_str();
|
||||||
if (params.size() > 1)
|
|
||||||
strLabel = params[1].get_str();
|
|
||||||
|
|
||||||
// Whether to perform rescan after import
|
// Whether to perform rescan after import
|
||||||
bool fRescan = true;
|
bool fRescan = true;
|
||||||
@ -94,7 +92,6 @@ Value importprivkey(const Array& params, bool fHelp)
|
|||||||
LOCK2(cs_main, pwalletMain->cs_wallet);
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||||
|
|
||||||
pwalletMain->MarkDirty();
|
pwalletMain->MarkDirty();
|
||||||
pwalletMain->SetAddressBookName(vchAddress, strLabel);
|
|
||||||
|
|
||||||
// Don't throw error in case a key is already there
|
// Don't throw error in case a key is already there
|
||||||
if (pwalletMain->HaveKey(vchAddress))
|
if (pwalletMain->HaveKey(vchAddress))
|
||||||
@ -103,6 +100,9 @@ Value importprivkey(const Array& params, bool fHelp)
|
|||||||
if (!pwalletMain->AddKeyPubKey(key, pubkey))
|
if (!pwalletMain->AddKeyPubKey(key, pubkey))
|
||||||
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
|
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
|
||||||
|
|
||||||
|
pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = GetTime();
|
||||||
|
pwalletMain->mapKeyMetadata[vchAddress].username = strUsername;
|
||||||
|
|
||||||
if (fRescan) {
|
if (fRescan) {
|
||||||
pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
|
pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
|
||||||
pwalletMain->ReacceptWalletTransactions();
|
pwalletMain->ReacceptWalletTransactions();
|
||||||
@ -151,28 +151,29 @@ Value importwallet(const Array& params, bool fHelp)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int64 nTime = DecodeDumpTime(vstr[1]);
|
int64 nTime = DecodeDumpTime(vstr[1]);
|
||||||
std::string strLabel;
|
std::string strUsername;
|
||||||
bool fLabel = true;
|
bool fUsername = false;
|
||||||
for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
|
for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
|
||||||
if (boost::algorithm::starts_with(vstr[nStr], "#"))
|
if (boost::algorithm::starts_with(vstr[nStr], "#"))
|
||||||
break;
|
break;
|
||||||
if (vstr[nStr] == "change=1")
|
if (boost::algorithm::starts_with(vstr[nStr], "username=")) {
|
||||||
fLabel = false;
|
strUsername = DecodeDumpString(vstr[nStr].substr(9));
|
||||||
if (vstr[nStr] == "reserve=1")
|
fUsername = true;
|
||||||
fLabel = false;
|
|
||||||
if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
|
|
||||||
strLabel = DecodeDumpString(vstr[nStr].substr(6));
|
|
||||||
fLabel = 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)) {
|
if (!pwalletMain->AddKeyPubKey(key, pubkey)) {
|
||||||
fGood = false;
|
fGood = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
pwalletMain->mapKeyMetadata[keyid].nCreateTime = nTime;
|
pwalletMain->mapKeyMetadata[keyid].nCreateTime = nTime;
|
||||||
if (fLabel)
|
pwalletMain->mapKeyMetadata[keyid].username = strUsername;
|
||||||
pwalletMain->SetAddressBookName(keyid, strLabel);
|
|
||||||
nTimeBegin = std::min(nTimeBegin, nTime);
|
nTimeBegin = std::min(nTimeBegin, nTime);
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
@ -196,21 +197,18 @@ Value dumpprivkey(const Array& params, bool fHelp)
|
|||||||
{
|
{
|
||||||
if (fHelp || params.size() != 1)
|
if (fHelp || params.size() != 1)
|
||||||
throw runtime_error(
|
throw runtime_error(
|
||||||
"dumpprivkey <bitcoinaddress>\n"
|
"dumpprivkey <username>\n"
|
||||||
"Reveals the private key corresponding to <bitcoinaddress>.");
|
"Reveals the private key corresponding to <username>.");
|
||||||
|
|
||||||
EnsureWalletIsUnlocked();
|
EnsureWalletIsUnlocked();
|
||||||
|
|
||||||
string strAddress = params[0].get_str();
|
string strUsername = params[0].get_str();
|
||||||
CBitcoinAddress address;
|
|
||||||
if (!address.SetString(strAddress))
|
|
||||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
|
|
||||||
CKeyID keyID;
|
CKeyID keyID;
|
||||||
if (!address.GetKeyID(keyID))
|
if (!pwalletMain->GetKeyIdFromUsername(strUsername, keyID))
|
||||||
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Username not found");
|
||||||
CKey vchSecret;
|
CKey vchSecret;
|
||||||
if (!pwalletMain->GetKey(keyID, 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();
|
return CBitcoinSecret(vchSecret).ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,7 +241,7 @@ Value dumpwallet(const Array& params, bool fHelp)
|
|||||||
std::sort(vKeyBirth.begin(), vKeyBirth.end());
|
std::sort(vKeyBirth.begin(), vKeyBirth.end());
|
||||||
|
|
||||||
// produce output
|
// 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("# * 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("# * 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());
|
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();
|
std::string strAddr = CBitcoinAddress(keyid).ToString();
|
||||||
CKey key;
|
CKey key;
|
||||||
if (pwalletMain->GetKey(keyid, key)) {
|
if (pwalletMain->GetKey(keyid, key)) {
|
||||||
if (pwalletMain->mapAddressBook.count(keyid)) {
|
if (pwalletMain->mapKeyMetadata.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());
|
file << strprintf("%s %s username=%s # addr=%s\n",
|
||||||
} else if (setKeyPool.count(keyid)) {
|
CBitcoinSecret(key).ToString().c_str(),
|
||||||
file << strprintf("%s %s reserve=1 # addr=%s\n", CBitcoinSecret(key).ToString().c_str(), strTime.c_str(), strAddr.c_str());
|
strTime.c_str(),
|
||||||
|
EncodeDumpString(pwalletMain->mapKeyMetadata[keyid].username).c_str(), strAddr.c_str());
|
||||||
} else {
|
} 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,6 +96,17 @@ bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
|
|||||||
return true;
|
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)
|
bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
|
||||||
{
|
{
|
||||||
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
|
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
|
||||||
|
@ -132,6 +132,8 @@ public:
|
|||||||
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
|
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
|
||||||
// Load metadata (used by LoadWallet)
|
// Load metadata (used by LoadWallet)
|
||||||
bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
|
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; }
|
bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user