diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 2ac58748..511cfba9 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -243,6 +243,7 @@ static const CRPCCommand vRPCCommands[] = { "decoderawtransaction", &decoderawtransaction, false, false }, { "signrawtransaction", &signrawtransaction, false, false }, { "sendrawtransaction", &sendrawtransaction, false, false }, + { "sendnewusertransaction", &sendnewusertransaction, false, false }, { "verifychain", &verifychain, true, false }, }; diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index 72df9c33..3e48f815 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -196,6 +196,7 @@ extern json_spirit::Value createrawtransaction(const json_spirit::Array& params, extern json_spirit::Value decoderawtransaction(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value signrawtransaction(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value sendrawtransaction(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value sendnewusertransaction(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value getblockcount(const json_spirit::Array& params, bool fHelp); // in rpcblockchain.cpp extern json_spirit::Value getbestblockhash(const json_spirit::Array& params, bool fHelp); diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index 115db89c..ce320321 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -145,9 +145,9 @@ Value getrawtransaction(const Array& params, bool fHelp) Value createrawtransaction(const Array& params, bool fHelp) { - if (fHelp || params.size() < 1 || params.size() > 2) + if (fHelp || params.size() != 2) throw runtime_error( - "createrawtransaction [pubKey=generate if omited]\n" + "createrawtransaction \n" "Create a transaction registering a new user\n" "Returns hex-encoded raw transaction.\n" "it is not stored in the wallet or transmitted to the network."); @@ -159,13 +159,12 @@ Value createrawtransaction(const Array& params, bool fHelp) string username = params[0].get_str(); rawTx.userName = CScript() << vector((const unsigned char*)username.data(), (const unsigned char*)username.data() + username.size()); - if (params.size() > 1) { - vector txData(ParseHexV(params[1], "pubkey")); - rawTx.pubKey << txData; - } else { - pwalletMain->GenerateNewKey(username); - throw JSONRPCError(RPC_INTERNAL_ERROR, "pubkey generation not implemented"); - } + vector vch(ParseHexV(params[1], "pubkey")); + CPubKey pubkey(vch); + if( !pubkey.IsValid() ) + throw JSONRPCError(RPC_INTERNAL_ERROR, "pubkey is not valid"); + + rawTx.pubKey << vch; DoTxProofOfWork(rawTx); @@ -425,3 +424,36 @@ Value sendrawtransaction(const Array& params, bool fHelp) return hashTx.GetHex(); } + +Value sendnewusertransaction(const Array& params, bool fHelp) +{ + if (fHelp || params.size() != 1) + throw runtime_error( + "sendnewusertransaction \n" + "Send a transaction registering a previously created new user\n" + "using createuserkey or imported to the wallet\n" + "Submits raw transaction (serialized, hex-encoded) to local node and network."); + + if (params[0].type() != str_type) + throw JSONRPCError(RPC_INVALID_PARAMETER, "username must be string"); + string strUsername = params[0].get_str(); + + CKeyID keyID; + if( !pwalletMain->GetKeyIdFromUsername(strUsername, keyID) ) + throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: username must exist in wallet"); + + CPubKey pubkey; + if( !pwalletMain->GetPubKey(keyID, pubkey) ) + throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: no public key found"); + + Array createTxParams; + createTxParams.push_back(strUsername); + createTxParams.push_back(HexStr(pubkey)); + Value txValue = createrawtransaction(createTxParams, false); + + std::string strTxHex = txValue.get_str(); + Array sendTxParams; + sendTxParams.push_back(strTxHex); + return sendrawtransaction(sendTxParams, false); +} + diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index b593280b..b3d1a1d8 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -109,7 +109,7 @@ Value createuserkey(const Array& params, bool fHelp) 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()); + printf("createuserkey: usernamehash(%s) = %s\n", strUsername.c_str(), userhash.GetHex().c_str()); CTransaction txOut; uint256 hashBlock;