basic submission of signed raw tx done

This commit is contained in:
moneroexamples 2016-10-11 13:38:27 +08:00
parent 57a874c771
commit 41f29b714d
5 changed files with 100 additions and 25 deletions

View File

@ -214,7 +214,7 @@ int main(int ac, const char* av[]) {
return xmrblocks.show_rawtx();
});
CROW_ROUTE(app, "/checkrawtx").methods("POST"_method)
CROW_ROUTE(app, "/checkandpush").methods("POST"_method)
([&](const crow::request& req) {
map<std::string, std::string> post_body = xmreg::parse_crow_post_data(req.body);
@ -227,23 +227,11 @@ int main(int ac, const char* av[]) {
string raw_tx_data = post_body["rawtxdata"];
string action = post_body["action"];
return xmrblocks.show_checkrawtx(raw_tx_data, action);
});
if (action == "check")
return xmrblocks.show_checkrawtx(raw_tx_data, action);
else if (action == "push")
return xmrblocks.show_pushrawtx(raw_tx_data, action);
CROW_ROUTE(app, "/pushrawtx").methods("POST"_method)
([&](const crow::request& req) {
map<std::string, std::string> post_body = xmreg::parse_crow_post_data(req.body);
if (post_body.count("rawtxdata") == 0 || post_body.count("action") == 0)
{
return string("Raw tx data or action not provided");
}
string raw_tx_data = post_body["rawtxdata"];
string action = post_body["action"];
return xmrblocks.show_pushrawtx(raw_tx_data, action);
});

View File

@ -1511,6 +1511,9 @@ namespace xmreg {
mstch::map tx_context = construct_tx_context(ptx.tx);
// mark that we have signed tx data for use in mstch
tx_context["have_raw_tx"] = true;
// get reference to inputs array created of the tx
mstch::array& inputs = boost::get<mstch::array>(tx_context["inputs"]);
@ -1622,7 +1625,7 @@ namespace xmreg {
const size_t magiclen = strlen(SIGNED_TX_PREFIX);
if (!strncmp(decoded_raw_tx_data.c_str(), SIGNED_TX_PREFIX, magiclen) != 0)
if (strncmp(decoded_raw_tx_data.c_str(), SIGNED_TX_PREFIX, magiclen) != 0)
{
cout << "The data does not appear to be signed raw tx!" << endl;
return string( "The data does not appear to be signed raw tx!");
@ -1647,9 +1650,60 @@ namespace xmreg {
// actually commit the transactions
while (!ptx_vector.empty())
{
auto & ptx = ptx_vector.back();
//m_wallet->commit_tx(ptx);
//success_msg_writer(true) << tr("Money successfully sent, transaction: ") << get_transaction_hash(ptx.tx);
tools::wallet2::pending_tx& ptx = ptx_vector.back();
tx_details txd = get_tx_details(ptx.tx);
string tx_hash_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", txd.hash));
// check in mempool already contains tx to be submited
vector<pair<tx_info, transaction>> found_mempool_txs
= search_mempool(txd.hash);
if (!found_mempool_txs.empty())
{
cerr << "Tx already exist in the mempool: " << tx_hash_str << endl;
return string("Tx already exist in the mempool: " + tx_hash_str);
}
// check if tx to be submited already exists in the blockchain
if (core_storage->have_tx(txd.hash))
{
cerr << "Tx already exist in the blockchain: " << tx_hash_str << endl;
return string("Tx already exist in the blockchain: " + tx_hash_str);
}
// check if any key images of the tx to be submited are already spend
vector<key_image> key_images_spent;
for (const txin_to_key& tx_in: txd.input_key_imgs)
{
if (core_storage->have_tx_keyimg_as_spent(tx_in.k_image))
key_images_spent.push_back(tx_in.k_image);
}
if (!key_images_spent.empty())
{
string key_images_str("Already spent inputs's key images: <br/>");
for (key_image& k_img: key_images_spent)
{
key_images_str += REMOVE_HASH_BRAKETS(fmt::format("{:s}", k_img));
key_images_str += "</br>";
}
cerr << "submitting signed tx has already spend inputs: " << key_images_str << endl;
return string("submitting signed tx has already spend inputs: " + key_images_str);
}
string error_msg;
if (!rpc.commit_tx(ptx, error_msg))
{
cerr << "submitting signed tx to daemon failed: " << error_msg << endl;
return string("submitting signed tx to daemon failed: " + error_msg);
}
// if no exception, remove element from vector
ptx_vector.pop_back();

View File

@ -105,6 +105,38 @@ namespace xmreg
return true;
}
bool
commit_tx(tools::wallet2::pending_tx& ptx, string& error_msg)
{
COMMAND_RPC_SEND_RAW_TX::request req;
COMMAND_RPC_SEND_RAW_TX::response res;
req.tx_as_hex = epee::string_tools::buff_to_hex_nodelimer(
tx_to_blob(ptx.tx)
);
req.do_not_relay = false;
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
bool r = epee::net_utils::invoke_http_json_remote_command2(deamon_url
+ "/sendrawtransaction",
req, res,
m_http_client, 200000);;
if (!r || res.status == "Failed")
{
error_msg = res.reason;
cerr << "Error sending tx: " << res.reason << endl;
return false;
}
return true;
}
};

View File

@ -136,10 +136,9 @@
</div>
<div class="center">
<h3>Details of submitted raw tx data</h3>
{{#unsigned_tx_given}}
<h4>unsigned_tx_given: {{unsigned_tx_given}}</h4>
<h3>Details of unsigned raw tx data given</h3>
{{#txs}}
<div class="center" style="width: 100%; margin-top:10px">
<h4>Destination information</h4>
@ -182,7 +181,8 @@
{{/unsigned_tx_given}}
{{^unsigned_tx_given}}
<h4>signed_tx_given</h4>
<h3>Details of signed raw tx data given</h3>
{{#txs}}
{{>tx_details}}
{{/txs}}

View File

@ -145,6 +145,7 @@
<div class="center">
<form action="/checkandpush" method="post" style="width:100%; margin-top:15px" class="style-1">
Paste base64 encoded, signed transaction data here<br/>
(In Linux, can get base64 signed raw tx data: <i>base64 signed_monero_tx | xclip -selection clipboard</i>)<br/>
<textarea name="rawtxdata" rows="20" cols="80"></textarea>
<br/>
<input type="submit" name="action" value="check">