mirror of
https://github.com/twisterarmy/twister-core.git
synced 2025-01-10 23:07:52 +00:00
create/list users in wallet
This commit is contained in:
parent
1c42569648
commit
f764674865
@ -206,11 +206,8 @@ static const CRPCCommand vRPCCommands[] =
|
||||
{ "gethashespersec", &gethashespersec, true, false },
|
||||
{ "getinfo", &getinfo, true, false },
|
||||
{ "getmininginfo", &getmininginfo, true, false },
|
||||
{ "getnewaddress", &getnewaddress, true, false },
|
||||
{ "getaccountaddress", &getaccountaddress, true, false },
|
||||
{ "setaccount", &setaccount, true, false },
|
||||
{ "getaccount", &getaccount, false, false },
|
||||
{ "getaddressesbyaccount", &getaddressesbyaccount, true, false },
|
||||
{ "createuserkey", &createuserkey, true, false },
|
||||
{ "listusernames", &listusernames, true, false },
|
||||
{ "sendtoaddress", &sendtoaddress, false, false },
|
||||
{ "getreceivedbyaddress", &getreceivedbyaddress, false, false },
|
||||
{ "getreceivedbyaccount", &getreceivedbyaccount, false, false },
|
||||
|
@ -159,11 +159,8 @@ extern json_spirit::Value getwork(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp);
|
||||
|
||||
extern json_spirit::Value getnewaddress(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
|
||||
extern json_spirit::Value getaccountaddress(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value setaccount(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getaccount(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getaddressesbyaccount(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value createuserkey(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
|
||||
extern json_spirit::Value listusernames(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value sendtoaddress(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value signmessage(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value verifymessage(const json_spirit::Array& params, bool fHelp);
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "bitcoinrpc.h"
|
||||
#include "init.h"
|
||||
#include "base58.h"
|
||||
#include "main.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
@ -90,155 +91,51 @@ Value getinfo(const Array& params, bool fHelp)
|
||||
|
||||
|
||||
|
||||
Value getnewaddress(const Array& params, bool fHelp)
|
||||
Value createuserkey(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() > 1)
|
||||
if (fHelp || params.size() != 1)
|
||||
throw runtime_error(
|
||||
"getnewaddress [account]\n"
|
||||
"createuserkey <username>\n"
|
||||
"Returns a new Bitcoin address for receiving payments. "
|
||||
"If [account] is specified (recommended), it is added to the address book "
|
||||
"so payments received with the address will be credited to [account].");
|
||||
|
||||
// Parse the account first so we don't generate a key if there's an error
|
||||
string strAccount;
|
||||
if (params.size() > 0)
|
||||
strAccount = AccountFromValue(params[0]);
|
||||
EnsureWalletIsUnlocked();
|
||||
|
||||
string strUsername = params[0].get_str();
|
||||
|
||||
CKeyID keyID;
|
||||
if( pwalletMain->GetKeyIdFromUsername(strUsername, keyID) )
|
||||
throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: this username exists in wallet");
|
||||
|
||||
uint256 userhash = SerializeHash(strUsername);
|
||||
printf("usernamehash(%s) = %s\n", strUsername.c_str(), userhash.GetHex().c_str());
|
||||
|
||||
CTransaction txOut;
|
||||
uint256 hashBlock;
|
||||
if( GetTransaction(userhash, txOut, hashBlock) )
|
||||
throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: this username exists in tx database");
|
||||
|
||||
// Generate a new key that is added to wallet
|
||||
CPubKey newKey;
|
||||
if (!pwalletMain->GetKeyFromPool(newKey, false))
|
||||
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
|
||||
CKeyID keyID = newKey.GetID();
|
||||
|
||||
pwalletMain->SetAddressBookName(keyID, strAccount);
|
||||
CPubKey newKey = pwalletMain->GenerateNewKey(strUsername);
|
||||
keyID = newKey.GetID();
|
||||
|
||||
return CBitcoinAddress(keyID).ToString();
|
||||
}
|
||||
|
||||
|
||||
CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false)
|
||||
Value listusernames(const Array& params, bool fHelp)
|
||||
{
|
||||
CWalletDB walletdb(pwalletMain->strWalletFile);
|
||||
|
||||
CAccount account;
|
||||
walletdb.ReadAccount(strAccount, account);
|
||||
|
||||
bool bKeyUsed = false;
|
||||
/*
|
||||
// Check if the current key has been used
|
||||
if (account.vchPubKey.IsValid())
|
||||
{
|
||||
CScript scriptPubKey;
|
||||
scriptPubKey.SetDestination(account.vchPubKey.GetID());
|
||||
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin();
|
||||
it != pwalletMain->mapWallet.end() && account.vchPubKey.IsValid();
|
||||
++it)
|
||||
{
|
||||
const CWalletTx& wtx = (*it).second;
|
||||
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
|
||||
if (txout.scriptPubKey == scriptPubKey)
|
||||
bKeyUsed = true;
|
||||
}
|
||||
}
|
||||
*/
|
||||
// Generate a new key
|
||||
if (!account.vchPubKey.IsValid() || bForceNew || bKeyUsed)
|
||||
{
|
||||
if (!pwalletMain->GetKeyFromPool(account.vchPubKey, false))
|
||||
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
|
||||
|
||||
pwalletMain->SetAddressBookName(account.vchPubKey.GetID(), strAccount);
|
||||
walletdb.WriteAccount(strAccount, account);
|
||||
}
|
||||
|
||||
return CBitcoinAddress(account.vchPubKey.GetID());
|
||||
}
|
||||
|
||||
Value getaccountaddress(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() != 1)
|
||||
if (fHelp || params.size() != 0)
|
||||
throw runtime_error(
|
||||
"getaccountaddress <account>\n"
|
||||
"Returns the current Bitcoin address for receiving payments to this account.");
|
||||
|
||||
// Parse the account first so we don't generate a key if there's an error
|
||||
string strAccount = AccountFromValue(params[0]);
|
||||
|
||||
Value ret;
|
||||
|
||||
ret = GetAccountAddress(strAccount).ToString();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Value setaccount(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() < 1 || params.size() > 2)
|
||||
throw runtime_error(
|
||||
"setaccount <bitcoinaddress> <account>\n"
|
||||
"Sets the account associated with the given address.");
|
||||
|
||||
CBitcoinAddress address(params[0].get_str());
|
||||
if (!address.IsValid())
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
|
||||
|
||||
|
||||
string strAccount;
|
||||
if (params.size() > 1)
|
||||
strAccount = AccountFromValue(params[1]);
|
||||
|
||||
// Detect when changing the account of an address that is the 'unused current key' of another account:
|
||||
if (pwalletMain->mapAddressBook.count(address.Get()))
|
||||
{
|
||||
string strOldAccount = pwalletMain->mapAddressBook[address.Get()];
|
||||
if (address == GetAccountAddress(strOldAccount))
|
||||
GetAccountAddress(strOldAccount, true);
|
||||
}
|
||||
|
||||
pwalletMain->SetAddressBookName(address.Get(), strAccount);
|
||||
|
||||
return Value::null;
|
||||
}
|
||||
|
||||
|
||||
Value getaccount(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() != 1)
|
||||
throw runtime_error(
|
||||
"getaccount <bitcoinaddress>\n"
|
||||
"Returns the account associated with the given address.");
|
||||
|
||||
CBitcoinAddress address(params[0].get_str());
|
||||
if (!address.IsValid())
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
|
||||
|
||||
string strAccount;
|
||||
map<CTxDestination, string>::iterator mi = pwalletMain->mapAddressBook.find(address.Get());
|
||||
if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty())
|
||||
strAccount = (*mi).second;
|
||||
return strAccount;
|
||||
}
|
||||
|
||||
|
||||
Value getaddressesbyaccount(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() != 1)
|
||||
throw runtime_error(
|
||||
"getaddressesbyaccount <account>\n"
|
||||
"Returns the list of addresses for the given account.");
|
||||
|
||||
string strAccount = AccountFromValue(params[0]);
|
||||
"listusernames\n"
|
||||
"Returns the list of usernames.");
|
||||
|
||||
// Find all addresses that have the given account
|
||||
Array ret;
|
||||
BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
|
||||
BOOST_FOREACH(const PAIRTYPE(CKeyID, CKeyMetadata)& item, pwalletMain->mapKeyMetadata)
|
||||
{
|
||||
const CBitcoinAddress& address = item.first;
|
||||
const string& strName = item.second;
|
||||
if (strName == strAccount)
|
||||
ret.push_back(address.ToString());
|
||||
ret.push_back(item.second.username);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user