From d17eb5ec72b3ea15304a0558699d9f30a36ddbf4 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Fri, 14 Jan 2022 20:10:16 -0600 Subject: [PATCH] Add wallet command for custom_operation Create a new cli_wallet command, run_custom_operation, which makes it convenient to run custom_operation transactions which invoke third party contracts (i.e., dapps) --- .../wallet/include/graphene/wallet/wallet.hpp | 4 +++ libraries/wallet/wallet.cpp | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index ac7d8996..b7203ed7 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -2386,6 +2386,9 @@ class wallet_api bool broadcast); vector get_account_roles_by_owner(string owner_account_id_or_name) const; + signed_transaction run_custom_operation(string payer_id_or_name, std::vector required_auths, + string data, uint16_t id=0, bool broadcast=false); + void dbg_make_uia(string creator, string symbol); void dbg_make_mia(string creator, string symbol); void dbg_push_blocks( std::string src_filename, uint32_t count ); @@ -2709,4 +2712,5 @@ FC_API( graphene::wallet::wallet_api, (get_custom_account_authorities_by_permission_id) (get_custom_account_authorities_by_permission_name) (get_active_custom_account_authorities_by_operation) + (run_custom_operation) ) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 8ecee060..91fa3f9d 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -7340,6 +7340,31 @@ vector wallet_api::get_account_roles_by_owner(string owner_ account_object owner_account = my->get_account(owner_account_id_or_name); return my->_remote_db->get_account_roles_by_owner(owner_account.id); } + +signed_transaction wallet_api::run_custom_operation(string payer_id_or_name, std::vector required_auths, string data, uint16_t id, bool broadcast) +{ + account_object payer = my->get_account(payer_id_or_name); + custom_operation op; + + // FC offers no way to have quotes in the data string. Add a simple escape option. + boost::replace_all(data, "\\\"", "\""); + boost::replace_all(data, "\\\\", "\\"); + + op.payer = payer.get_id(); + if (!required_auths.empty()) + std::transform(required_auths.begin(), required_auths.end(), std::inserter(op.required_auths, op.required_auths.begin()), + [&my=my](const string& name_or_id) { return my->get_account(name_or_id).get_id(); }); + op.id = id; + op.data.reserve(data.size()); + op.data.assign(data.begin(), data.end()); + + signed_transaction trx; + trx.operations = {std::move(op)}; + my->set_operation_fees(trx, my->_remote_db->get_global_properties().parameters.current_fees); + trx.validate(); + + return my->sign_transaction(std::move(trx), broadcast); +} // default ctor necessary for FC_REFLECT signed_block_with_info::signed_block_with_info() {