diff --git a/html/js/controllers/import_wallet.js b/html/js/controllers/import_wallet.js index 6e64eeb..f21038a 100755 --- a/html/js/controllers/import_wallet.js +++ b/html/js/controllers/import_wallet.js @@ -89,7 +89,10 @@ thinwalletCtrls.controller("ImportWalletCtrl", function($scope, $location, $http $timeout(function(){ModalService.hide('import-wallet')}, 5000); } },function(response) { - $scope.error = "Error connecting to the backend. Can't get payment import data."; + var data = response.data; + $scope.error = data && data.Error + ? data.Error + : "Something went wrong getting payment details"; }); } diff --git a/src/YourMoneroRequests.cpp b/src/YourMoneroRequests.cpp index 7786987..d28f856 100755 --- a/src/YourMoneroRequests.cpp +++ b/src/YourMoneroRequests.cpp @@ -985,11 +985,36 @@ void YourMoneroRequests::import_wallet_request( const shared_ptr< Session > session, const Bytes & body) { - json j_request = body_to_json(body); - - string xmr_address = j_request["address"]; json j_response; + json j_request; + + vector requested_values {"address" , "view_key"}; + + if (!parse_request(body, requested_values, + j_request, j_response)) + { + session_close(session, j_response, UNPROCESSABLE_ENTITY, + "Cant parse json body!"); + return; + } + + string xmr_address; + string view_key; + + try + { + xmr_address = j_request["address"]; + view_key = j_request["view_key"]; + } + catch (json::exception const& e) + { + OMERROR << "json exception: " << e.what(); + session_close(session, j_response, UNPROCESSABLE_ENTITY, + e.what()); + return; + } + j_response["request_fulfilled"] = false; j_response["import_fee"] = std::to_string( @@ -1035,16 +1060,21 @@ YourMoneroRequests::import_wallet_request( return; } + // payment fee is not zero, so we need to + // ask for the payment. So we first get payment details + // associated with the given account. auto xmr_payment = select_payment(*xmr_account); + // something went wrong. if (!xmr_payment) { session_close(session, j_response, UNPROCESSABLE_ENTITY, - "Selecting payment details faild!"); + "Selecting payment details failed!"); return; } + // payment id is null if (xmr_payment->id == mysqlpp::null) { // no current payment record exist, @@ -1095,13 +1125,43 @@ YourMoneroRequests::import_wallet_request( return; } // if (xmr_payment->id == mysqlpp::null) + // payment id is not null, so it means that + // we have already payment record in our db for that + // account. bool request_fulfilled = bool {xmr_payment->request_fulfilled}; + if (request_fulfilled) + { + // if payment has been made, and we get new request to import txs + // indicate that this is new requeest, but request was fulfiled. + // front end should give proper message in this case + + j_response["request_fulfilled"] = request_fulfilled; + j_response["status"] = "Wallet already imported or " + "in the progress."; + j_response["new_request"] = false; + j_response["error"] = ""; + + session_close(session, j_response); + return; + } + + // payment has not been yet done, so we are going + // to check if it has just been done and update + // db accordingly + string integrated_address = current_bc_status->get_account_integrated_address_as_str( xmr_payment->payment_id); + if (integrated_address.empty()) + { + session_close(session, j_response, UNPROCESSABLE_ENTITY, + "get_account_integrated_address_as_str failed!"); + return; + } + j_response["payment_id"] = xmr_payment->payment_id; j_response["import_fee"] = std::to_string(xmr_payment->import_fee); j_response["new_request"] = false; @@ -1112,91 +1172,70 @@ YourMoneroRequests::import_wallet_request( string tx_hash_with_payment; // if payment has not yet been done - if (!request_fulfilled) - { - // check if it has just been done now - // if yes, mark it in mysql - if(current_bc_status->search_if_payment_made( - xmr_payment->payment_id, - xmr_payment->import_fee, - tx_hash_with_payment)) - { - XmrPayment updated_xmr_payment = *xmr_payment; + // check if it has just been done now + // if yes, mark it in mysql - // updated values - updated_xmr_payment.request_fulfilled = true; - updated_xmr_payment.tx_hash = tx_hash_with_payment; + if(current_bc_status->search_if_payment_made( + xmr_payment->payment_id, + xmr_payment->import_fee, + tx_hash_with_payment)) + { + XmrPayment updated_xmr_payment = *xmr_payment; - // save to mysql - if (xmr_accounts->update(*xmr_payment, updated_xmr_payment)) - { + // updated values + updated_xmr_payment.request_fulfilled = true; + updated_xmr_payment.tx_hash = tx_hash_with_payment; - // set scanned_block_height to 0 to begin - // scanning entire blockchain + // save to mysql + if (!xmr_accounts->update(*xmr_payment, updated_xmr_payment)) + { - XmrAccount acc; + OMERROR << xmr_address.substr(0,6) + + "Updating payment db failed!\n"; - if (xmr_accounts->select(xmr_address, acc)) - { - XmrAccount updated_acc = acc; + session_close(session, j_response, UNPROCESSABLE_ENTITY, + "Updating payment db failed!"); + return; + } - updated_acc.scanned_block_height = 0; + XmrAccount updated_acc = *xmr_account; - if (xmr_accounts->update(acc, updated_acc)) - { - // if success, set acc to updated_acc; - request_fulfilled = true; + updated_acc.scanned_block_height = 0; - // change search blk number in the search thread - if (!current_bc_status - ->set_new_searched_blk_no(xmr_address, 0)) - { - OMERROR << xmr_address.substr(0,6) + - ": updating searched_blk_no failed!\n"; - j_response["error"] = "Updating searched_blk_no" - " failed!"; - } + // set scanned_block_height to 0 to begin + // scanning entire blockchain - j_response["request_fulfilled"] - = request_fulfilled; - j_response["status"] - = "Payment received. Thank you."; - j_response["new_request"] = true; - j_response["error"] = ""; - } - } - else - { - OMERROR << xmr_address.substr(0,6) + - ": updating accounts " - "payment db failed! \n"; - j_response["error"] - = "Updating accounts " - "payment db failed!"; - } - } - else - { - OMERROR << xmr_address.substr(0,6) + - "Updating payment db failed!\n"; - j_response["error"] = "Updating payment mysql failed!"; - } + if (!xmr_accounts->update(*xmr_account, updated_acc)) + { + OMERROR << xmr_address.substr(0,6) + + "Updating scanned_block_height failed!\n"; - } // if(current_bc_status->search_if_payment_made( + session_close(session, j_response, UNPROCESSABLE_ENTITY, + "Updating scanned_block_height failed!"); + return; + } - } // if (!request_fulfilled) - else - { - // if payment has been made, and we get new request to import txs - // indicate that this is new requeest, but request was fulfiled. - // front end should give proper message in this case + // if success, set acc to updated_acc; + request_fulfilled = true; - j_response["request_fulfilled"] = request_fulfilled; - j_response["status"] = "Wallet already imported or " - "in the progress."; - j_response["new_request"] = false; + // change search blk number in the search thread + if (!current_bc_status + ->set_new_searched_blk_no(xmr_address, 0)) + { + OMERROR << xmr_address.substr(0,6) + + ": updating searched_blk_no failed!\n"; + j_response["error"] = "Updating searched_blk_no" + " failed!"; + } + + j_response["request_fulfilled"] + = request_fulfilled; + j_response["status"] + = "Payment received. Thank you."; + j_response["new_request"] = true; j_response["error"] = ""; - } + + } // if(current_bc_status->search_if_payment_made( session_close(session, j_response); @@ -1218,10 +1257,11 @@ YourMoneroRequests::import_recent_wallet_request( vector requested_values {"address" , "view_key", "no_blocks_to_import"}; - if (!parse_request(body, requested_values, j_request, j_response)) + if (!parse_request(body, requested_values, + j_request, j_response)) { - j_response["Error"] = "Cant parse json body"; - session_close(session, j_response); + session_close(session, j_response, UNPROCESSABLE_ENTITY, + "Cant parse json body!"); return; } @@ -1235,8 +1275,9 @@ YourMoneroRequests::import_recent_wallet_request( } catch (json::exception const& e) { - cerr << "json exception: " << e.what() << '\n'; - session_close(session, j_response); + OMERROR << "json exception: " << e.what(); + session_close(session, j_response, UNPROCESSABLE_ENTITY, + e.what()); return; } @@ -1954,7 +1995,7 @@ boost::optional YourMoneroRequests::select_account( string const& xmr_address) const { - boost::optional acc; + boost::optional acc = XmrAccount{}; if (!xmr_accounts->select(xmr_address, *acc)) { @@ -1964,7 +2005,7 @@ YourMoneroRequests::select_account( return acc; } - return {}; + return acc; } boost::optional @@ -1976,9 +2017,15 @@ YourMoneroRequests::select_payment( if (!xmr_accounts->select(xmr_account.id.data, xmr_payments)) { - OMERROR << xmr_account.address.substr(0,6) + - ": address does not exists!"; - return {}; + OMINFO << xmr_account.address.substr(0,6) + + ": no payment record found!"; + + // so create empty record to be inserted into + // db after. + XmrPayment xmr_payment; + xmr_payment.id = mysqlpp::null; + + return xmr_payment; } if (xmr_payments.size() > 1) @@ -1993,13 +2040,19 @@ YourMoneroRequests::select_payment( // paymnet record created. so new // paymnet will be created if (xmr_payments.empty()) - { + { OMINFO << xmr_account.address.substr(0,6) + ": no payment record found!"; - return {XmrPayment{}}; + + // so create empty record to be inserted into + // db after. + XmrPayment xmr_payment; + xmr_payment.id = mysqlpp::null; + + return xmr_payment; } - return {xmr_payments.at(1)}; + return xmr_payments.at(0); } void @@ -2011,7 +2064,7 @@ YourMoneroRequests::session_close( { if (return_code != OK) { - j_response["error"] = error_msg; + j_response["Error"] = error_msg; } string response_body = j_response.dump(); diff --git a/src/db/MySqlAccounts.cpp b/src/db/MySqlAccounts.cpp index 1b28586..1adc98a 100755 --- a/src/db/MySqlAccounts.cpp +++ b/src/db/MySqlAccounts.cpp @@ -19,7 +19,8 @@ MysqlInputs::MysqlInputs(shared_ptr _conn) {} bool -MysqlInputs::select_for_out(const uint64_t& output_id, vector& ins) +MysqlInputs::select_for_out(const uint64_t& output_id, + vector& ins) { try { @@ -77,7 +78,8 @@ MysqlOutpus::exist(const string& output_public_key_str, XmrOutput& out) } -MysqlTransactions::MysqlTransactions(shared_ptr _conn): conn {_conn} +MysqlTransactions::MysqlTransactions(shared_ptr _conn) + : conn {_conn} {} uint64_t @@ -87,9 +89,10 @@ MysqlTransactions::mark_spendable(const uint64_t& tx_id_no, bool spendable) { conn->check_if_connected(); - Query query = conn->query(spendable ? - XmrTransaction::MARK_AS_SPENDABLE_STMT - : XmrTransaction::MARK_AS_NONSPENDABLE_STMT); + Query query = conn->query( + spendable ? + XmrTransaction::MARK_AS_SPENDABLE_STMT + : XmrTransaction::MARK_AS_NONSPENDABLE_STMT); query.parse(); @@ -131,7 +134,8 @@ MysqlTransactions::delete_tx(const uint64_t& tx_id_no) bool -MysqlTransactions::exist(const uint64_t& account_id, const string& tx_hash_str, XmrTransaction& tx) +MysqlTransactions::exist(const uint64_t& account_id, + const string& tx_hash_str, XmrTransaction& tx) { try { @@ -161,7 +165,8 @@ MysqlTransactions::exist(const uint64_t& account_id, const string& tx_hash_str, bool -MysqlTransactions::get_total_recieved(const uint64_t& account_id, uint64_t& amount) +MysqlTransactions::get_total_recieved(const uint64_t& account_id, + uint64_t& amount) { try { @@ -190,7 +195,8 @@ MysqlPayments::MysqlPayments(shared_ptr _conn): conn {_conn} {} bool -MysqlPayments::select_by_payment_id(const string& payment_id, vector& payments) +MysqlPayments::select_by_payment_id(const string& payment_id, + vector& payments) { try @@ -214,7 +220,8 @@ MysqlPayments::select_by_payment_id(const string& payment_id, vector return false; } -MySqlAccounts::MySqlAccounts(shared_ptr _current_bc_status) +MySqlAccounts::MySqlAccounts( + shared_ptr _current_bc_status) : current_bc_status {_current_bc_status} { // create connection to the mysql @@ -223,8 +230,9 @@ MySqlAccounts::MySqlAccounts(shared_ptr _current_bc_sta _init(); } -MySqlAccounts::MySqlAccounts(shared_ptr _current_bc_status, - shared_ptr _conn) +MySqlAccounts::MySqlAccounts( + shared_ptr _current_bc_status, + shared_ptr _conn) : current_bc_status {_current_bc_status} { conn = _conn; @@ -292,7 +300,8 @@ MySqlAccounts::insert(const T& data_to_insert) template uint64_t MySqlAccounts::insert(const XmrAccount& data_to_insert); template -uint64_t MySqlAccounts::insert(const XmrTransaction& data_to_insert); +uint64_t MySqlAccounts::insert( + const XmrTransaction& data_to_insert); template uint64_t MySqlAccounts::insert(const XmrOutput& data_to_insert); template @@ -327,9 +336,12 @@ MySqlAccounts::insert(const vector& data_to_insert) // Explicitly instantiate insert template for our tables template -uint64_t MySqlAccounts::insert(const vector& data_to_insert); +uint64_t MySqlAccounts::insert( + const vector& data_to_insert); + template -uint64_t MySqlAccounts::insert(const vector& data_to_insert); +uint64_t MySqlAccounts::insert( + const vector& data_to_insert); template bool @@ -339,14 +351,18 @@ MySqlAccounts::select(uint64_t account_id, vector& selected_data) { conn->check_if_connected(); - Query query = conn->query((query_no == 1 ? T::SELECT_STMT : T::SELECT_STMT2)); + Query query = conn->query((query_no == 1 + ? T::SELECT_STMT : T::SELECT_STMT2)); query.parse(); selected_data.clear(); query.storein(selected_data, account_id); + // this is confusing. So I get false from this method + // when this is empty and when there is some exception! return !selected_data.empty(); + //return true; } catch (std::exception const& e) { @@ -357,25 +373,34 @@ MySqlAccounts::select(uint64_t account_id, vector& selected_data) } template -bool MySqlAccounts::select(uint64_t account_id, vector& selected_data); +bool MySqlAccounts::select(uint64_t account_id, + vector& selected_data); template -bool MySqlAccounts::select(uint64_t account_id, vector& selected_data); +bool MySqlAccounts::select(uint64_t account_id, + vector& selected_data); template -bool MySqlAccounts::select(uint64_t account_id, vector& selected_data); +bool MySqlAccounts::select(uint64_t account_id, + vector& selected_data); -template // this will use SELECT_STMT2 which selectes based on transaction id, not account_id, -bool MySqlAccounts::select(uint64_t tx_id, vector& selected_data); +template // this will use SELECT_STMT2 which selectes + // based on transaction id, not account_id, +bool MySqlAccounts::select(uint64_t tx_id, + vector& selected_data); template -bool MySqlAccounts::select(uint64_t account_id, vector& selected_data); +bool MySqlAccounts::select(uint64_t account_id, + vector& selected_data); template -bool MySqlAccounts::select(uint64_t account_id, vector& selected_data); +bool MySqlAccounts::select(uint64_t account_id, + vector& selected_data); -template // this will use SELECT_STMT2 which selectes based on transaction id, not account_id, -bool MySqlAccounts::select(uint64_t tx_id, vector& selected_data); +template // this will use SELECT_STMT2 which selectes + // based on transaction id, not account_id, +bool MySqlAccounts::select(uint64_t tx_id, + vector& selected_data); template @@ -403,10 +428,12 @@ MySqlAccounts::update(T const& orginal_row, T const& new_row) } template -bool MySqlAccounts::update(XmrAccount const& orginal_row, XmrAccount const& new_row); +bool MySqlAccounts::update( + XmrAccount const& orginal_row, XmrAccount const& new_row); template -bool MySqlAccounts::update(XmrPayment const& orginal_row, XmrPayment const& new_row); +bool MySqlAccounts::update( + XmrPayment const& orginal_row, XmrPayment const& new_row); template bool @@ -415,12 +442,16 @@ MySqlAccounts::select_for_tx(uint64_t tx_id, vector& selected_data) return select(tx_id, selected_data); } -template // this will use SELECT_STMT2 which selectes based on transaction id, not account_id, -bool MySqlAccounts::select_for_tx(uint64_t tx_id, vector& selected_data); +template // this will use SELECT_STMT2 which selectes based on + // transaction id, not account_id, +bool MySqlAccounts::select_for_tx(uint64_t tx_id, + vector& selected_data); -template // this will use SELECT_STMT2 which selectes based on transaction id, not account_id, -bool MySqlAccounts::select_for_tx(uint64_t tx_id, vector& selected_data); +template // this will use SELECT_STMT2 which selectes + //based on transaction id, not account_id, +bool MySqlAccounts::select_for_tx(uint64_t tx_id, + vector& selected_data); template bool @@ -453,16 +484,20 @@ MySqlAccounts::select_by_primary_id(uint64_t id, T& selected_data) } //template -//bool MySqlAccounts::select_by_primary_id(uint64_t id, XmrTransaction& selected_data); +//bool MySqlAccounts::select_by_primary_id( +// uint64_t id, XmrTransaction& selected_data); template -bool MySqlAccounts::select_by_primary_id(uint64_t id, XmrInput& selected_data); +bool MySqlAccounts::select_by_primary_id( + uint64_t id, XmrInput& selected_data); template -bool MySqlAccounts::select_by_primary_id(uint64_t id, XmrOutput& selected_data); +bool MySqlAccounts::select_by_primary_id( + uint64_t id, XmrOutput& selected_data); template -bool MySqlAccounts::select_by_primary_id(uint64_t id, XmrPayment& selected_data); +bool MySqlAccounts::select_by_primary_id( + uint64_t id, XmrPayment& selected_data); bool MySqlAccounts::select_txs_for_account_spendability_check( @@ -492,7 +527,8 @@ MySqlAccounts::select_txs_for_account_spendability_check( if (no_row_updated != 1) { - cerr << "no_row_updated != 1 due to xmr_accounts->mark_tx_spendable(tx.id)\n"; + cerr << "no_row_updated != 1 due to " + "xmr_accounts->mark_tx_spendable(tx.id)\n"; return false; } @@ -512,14 +548,16 @@ MySqlAccounts::select_txs_for_account_spendability_check( if (blockchain_tx_id != tx.blockchain_tx_id) { - // tx does not exist in blockchain, or its blockchain_id changed + // tx does not exist in blockchain, or its blockchain_id + // changed // for example, it was orhpaned, and then readded. uint64_t no_row_updated = delete_tx(tx.id.data); if (no_row_updated != 1) { - cerr << "no_row_updated != 1 due to xmr_accounts->delete_tx(tx.id)\n"; + cerr << "no_row_updated != 1 due to " + "xmr_accounts->delete_tx(tx.id)\n"; return false; } @@ -539,7 +577,8 @@ MySqlAccounts::select_txs_for_account_spendability_check( // be spent anyway. if (tx.unlock_time == 0) - tx.unlock_time = tx.height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE; + tx.unlock_time = tx.height + + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE; } // else @@ -555,19 +594,22 @@ MySqlAccounts::select_txs_for_account_spendability_check( bool -MySqlAccounts::select_inputs_for_out(const uint64_t& output_id, vector& ins) +MySqlAccounts::select_inputs_for_out(const uint64_t& output_id, + vector& ins) { return mysql_in->select_for_out(output_id, ins); } bool -MySqlAccounts::output_exists(const string& output_public_key_str, XmrOutput& out) +MySqlAccounts::output_exists(const string& output_public_key_str, + XmrOutput& out) { return mysql_out->exist(output_public_key_str, out); } bool -MySqlAccounts::tx_exists(const uint64_t& account_id, const string& tx_hash_str, XmrTransaction& tx) +MySqlAccounts::tx_exists(const uint64_t& account_id, + const string& tx_hash_str, XmrTransaction& tx) { return mysql_tx->exist(account_id, tx_hash_str, tx); } @@ -591,13 +633,15 @@ MySqlAccounts::delete_tx(const uint64_t& tx_id_no) } bool -MySqlAccounts::select_payment_by_id(const string& payment_id, vector& payments) +MySqlAccounts::select_payment_by_id(const string& payment_id, + vector& payments) { return mysql_payment->select_by_payment_id(payment_id, payments); } bool -MySqlAccounts::get_total_recieved(const uint64_t& account_id, uint64_t& amount) +MySqlAccounts::get_total_recieved(const uint64_t& account_id, + uint64_t& amount) { return mysql_tx->get_total_recieved(account_id, amount); } @@ -617,7 +661,8 @@ MySqlAccounts::get_connection() void -MySqlAccounts::set_bc_status_provider(shared_ptr bc_status_provider) +MySqlAccounts::set_bc_status_provider( + shared_ptr bc_status_provider) { current_bc_status = bc_status_provider; }