Skip to content

Commit

Permalink
split up parse_tx_from_str() so accept_func() can be applied outside …
Browse files Browse the repository at this point in the history
…the Wallet API
  • Loading branch information
SNeedlewoods committed Oct 28, 2024
1 parent 68dbcaf commit f9e2bcf
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/wallet/api/pending_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class PendingTransactionImpl : public PendingTransaction
std::unordered_set<crypto::public_key> m_signers;
std::vector<std::string> m_tx_device_aux;
std::vector<crypto::key_image> m_key_images;
// wallet2 m_cold_key_images
std::unordered_map<crypto::public_key, crypto::key_image> m_tx_key_images;
};


Expand Down
15 changes: 11 additions & 4 deletions src/wallet/api/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3188,7 +3188,16 @@ bool WalletImpl::parseUnsignedTxFromStr(const std::string &unsigned_tx_str, Unsi
bool WalletImpl::parseTxFromStr(const std::string &signed_tx_str, PendingTransaction &ptx) const
{
PendingTransactionImpl *ptx_impl = dynamic_cast<PendingTransactionImpl*>(&ptx);
return m_wallet->parse_tx_from_str(signed_tx_str, ptx_impl->m_pending_tx, NULL);
std::shared_ptr<tools::wallet2::signed_tx_set> signed_tx_set_out = std::make_shared<tools::wallet2::signed_tx_set>();
bool r = m_wallet->parse_tx_from_str(signed_tx_str, ptx_impl->m_pending_tx, /* accept_func = */ NULL, signed_tx_set_out.get(), /* do_handle_key_images = */ false);
if (r)
ptx_impl->m_tx_key_images = signed_tx_set_out->tx_key_images;
return r;
}
//-------------------------------------------------------------------------------------------------------------------
void WalletImpl::insertColdKeyImages(PendingTransaction &ptx)
{
m_wallet->insert_cold_key_images(dynamic_cast<PendingTransactionImpl*>(&ptx)->m_tx_key_images);
}
//-------------------------------------------------------------------------------------------------------------------
bool WalletImpl::parseMultisigTxFromStr(const std::string &multisig_tx_str, PendingTransaction &exported_txs) const
Expand Down Expand Up @@ -3272,9 +3281,7 @@ void WalletImpl::coldSignTx(const PendingTransaction &ptx_in, PendingTransaction
m_wallet->cold_sign_tx(ptx_impl_in->m_pending_tx, signed_txs, dsts_info, ptx_impl_out->m_tx_device_aux);
ptx_impl_out->m_key_images = signed_txs.key_images;
ptx_impl_out->m_pending_tx = signed_txs.ptx;
// TODO : figure out if we need signed_txs.tx_key_images here, afaik they're used for selfsend/change enotes
// if needed we can probably add a member like `m_selfsend_key_images` to `PendingTransaction`
// guess then `PendingTransaction` would be a proper replacement for `wallet2::signed_tx_set`
ptx_impl_out->m_tx_key_images = signed_txs.tx_key_images;
}
catch (const std::exception &e)
{
Expand Down
1 change: 1 addition & 0 deletions src/wallet/api/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ class WalletImpl : public Wallet
std::string convertTxToStr(const PendingTransaction &ptxs) const override;
bool parseUnsignedTxFromStr(const std::string &unsigned_tx_str, UnsignedTransaction &exported_txs) const override;
bool parseTxFromStr(const std::string &signed_tx_str, PendingTransaction &ptx) const override;
void insertColdKeyImages(PendingTransaction &ptx) override;
bool parseMultisigTxFromStr(const std::string &multisig_tx_str, PendingTransaction &exported_txs) const override;
std::uint64_t getFeeMultiplier(std::uint32_t priority, int fee_algorithm) const override;
std::uint64_t getBaseFee() const override;
Expand Down
10 changes: 10 additions & 0 deletions src/wallet/api/wallet2_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,16 @@ struct Wallet
*/
virtual bool parseTxFromStr(const std::string &signed_tx_str, PendingTransaction &ptx) const = 0;
/**
* brief: insertColdKeyImages - remember cold key images for parsed tx, for when we get those txes from the blockchain
* Call:
* - parseTxFromStr()
* - accept_func() (optional)
* - importKeyImages()
* - insertColdKeyImages()
* param: ptx - PendingTransaction obtained from parseTxFromStr() outparam ptx
*/
virtual void insertColdKeyImages(PendingTransaction &ptx) = 0;
/**
* brief: parseMultisigTxFromStr - get pending multisig transaction from encrypted unsigned multisig transaction as hex string
* param: multisig_tx_str -
* outparam: exported_txs -
Expand Down
26 changes: 19 additions & 7 deletions src/wallet/wallet2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7979,7 +7979,7 @@ bool wallet2::load_tx(const std::string &signed_filename, std::vector<tools::wal
return parse_tx_from_str(s, ptx, accept_func);
}
//----------------------------------------------------------------------------------------------------
bool wallet2::parse_tx_from_str(const std::string &signed_tx_st, std::vector<tools::wallet2::pending_tx> &ptx, std::function<bool(const signed_tx_set &)> accept_func)
bool wallet2::parse_tx_from_str(const std::string &signed_tx_st, std::vector<tools::wallet2::pending_tx> &ptx, std::function<bool(const signed_tx_set &)> accept_func, tools::wallet2::signed_tx_set *signed_txs_out /* = nullptr */, bool do_handle_key_images /* = true */)
{
std::string s = signed_tx_st;
signed_tx_set signed_txs;
Expand Down Expand Up @@ -8073,19 +8073,31 @@ bool wallet2::parse_tx_from_str(const std::string &signed_tx_st, std::vector<too
return false;
}

// import key images
bool r = import_key_images(signed_txs.key_images);
if (!r) return false;
// Make signed_tx_set available to caller
if (signed_txs_out)
*signed_txs_out = std::move(signed_txs);

// remember key images for this tx, for when we get those txes from the blockchain
for (const auto &e: signed_txs.tx_key_images)
m_cold_key_images.insert(e);
// `do_handle_key_images = true` was (and is) the default behavior, but for more flexibility in the Wallet API it can be turned off now
if (do_handle_key_images)
{
// import key images
bool r = import_key_images(signed_txs.key_images);
if (!r) return false;

// remember key images for this tx, for when we get those txes from the blockchain
insert_cold_key_images(signed_txs.tx_key_images);
}
ptx = signed_txs.ptx;

return true;
}
//----------------------------------------------------------------------------------------------------
void wallet2::insert_cold_key_images(std::unordered_map<crypto::public_key, crypto::key_image> &cold_key_images)
{
for (const auto &ki: cold_key_images)
m_cold_key_images.insert(ki);
}
//----------------------------------------------------------------------------------------------------
std::string wallet2::save_multisig_tx(multisig_tx_set txs)
{
LOG_PRINT_L0("saving " << txs.m_ptx.size() << " multisig transactions");
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/wallet2.h
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,8 @@ namespace tools
bool load_unsigned_tx(const std::string &unsigned_filename, unsigned_tx_set &exported_txs) const;
bool parse_unsigned_tx_from_str(const std::string &unsigned_tx_st, unsigned_tx_set &exported_txs) const;
bool load_tx(const std::string &signed_filename, std::vector<tools::wallet2::pending_tx> &ptx, std::function<bool(const signed_tx_set&)> accept_func = NULL);
bool parse_tx_from_str(const std::string &signed_tx_st, std::vector<tools::wallet2::pending_tx> &ptx, std::function<bool(const signed_tx_set &)> accept_func);
bool parse_tx_from_str(const std::string &signed_tx_st, std::vector<tools::wallet2::pending_tx> &ptx, std::function<bool(const signed_tx_set &)> accept_func, signed_tx_set *signed_tx_set_out = nullptr, bool do_handle_key_images = true);
void insert_cold_key_images(std::unordered_map<crypto::public_key, crypto::key_image> &cold_key_images);
std::vector<wallet2::pending_tx> create_transactions_2(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, const unique_index_container& subtract_fee_from_outputs = {}); // pass subaddr_indices by value on purpose
std::vector<wallet2::pending_tx> create_transactions_all(uint64_t below, const cryptonote::account_public_address &address, bool is_subaddress, const size_t outputs, const size_t fake_outs_count, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices);
std::vector<wallet2::pending_tx> create_transactions_single(const crypto::key_image &ki, const cryptonote::account_public_address &address, bool is_subaddress, const size_t outputs, const size_t fake_outs_count, uint32_t priority, const std::vector<uint8_t>& extra);
Expand Down

0 comments on commit f9e2bcf

Please sign in to comment.