diff options
author | kenshi84 <kenshi84@protonmail.ch> | 2017-02-19 11:42:10 +0900 |
---|---|---|
committer | kenshi84 <kenshi84@protonmail.ch> | 2017-10-07 13:06:21 +0900 |
commit | 53ad5a0f42174bca57e24485ef3d40e4b9cf5599 (patch) | |
tree | afc13a3ee6a049ec78ac234e2d55ff46e992b457 /src/wallet | |
parent | Merge pull request #2548 (diff) | |
download | monero-53ad5a0f42174bca57e24485ef3d40e4b9cf5599.tar.xz |
Subaddresses
Diffstat (limited to 'src/wallet')
23 files changed, 1798 insertions, 388 deletions
diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index b62c084be..24399790c 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -41,6 +41,8 @@ set(wallet_sources api/pending_transaction.cpp api/utils.cpp api/address_book.cpp + api/subaddress.cpp + api/subaddress_account.cpp api/unsigned_transaction.cpp) set(wallet_api_headers @@ -62,6 +64,8 @@ set(wallet_private_headers api/pending_transaction.h api/common_defines.h api/address_book.h + api/subaddress.h + api/subaddress_account.h api/unsigned_transaction.h) monero_private_headers(wallet diff --git a/src/wallet/api/address_book.cpp b/src/wallet/api/address_book.cpp index 9605047b7..da412cd4b 100644 --- a/src/wallet/api/address_book.cpp +++ b/src/wallet/api/address_book.cpp @@ -48,10 +48,8 @@ bool AddressBookImpl::addRow(const std::string &dst_addr , const std::string &pa { clearStatus(); - cryptonote::account_public_address addr; - bool has_short_pid; - crypto::hash8 payment_id_short; - if(!cryptonote::get_account_integrated_address_from_str(addr, has_short_pid, payment_id_short, m_wallet->m_wallet->testnet(), dst_addr)) { + cryptonote::address_parse_info info; + if(!cryptonote::get_account_address_from_str(info, m_wallet->m_wallet->testnet(), dst_addr)) { m_errorString = tr("Invalid destination address"); m_errorCode = Invalid_Address; return false; @@ -75,19 +73,19 @@ bool AddressBookImpl::addRow(const std::string &dst_addr , const std::string &pa } // integrated + long payment id provided - if(has_long_pid && has_short_pid) { + if(has_long_pid && info.has_payment_id) { m_errorString = tr("Integrated address and long payment id can't be used at the same time"); m_errorCode = Invalid_Payment_Id; return false; } // Pad short pid with zeros - if (has_short_pid) + if (info.has_payment_id) { - memcpy(payment_id.data, payment_id_short.data, 8); + memcpy(payment_id.data, info.payment_id.data, 8); } - bool r = m_wallet->m_wallet->add_address_book_row(addr,payment_id,description); + bool r = m_wallet->m_wallet->add_address_book_row(info.address,payment_id,description,info.is_subaddress); if (r) refresh(); else @@ -107,9 +105,9 @@ void AddressBookImpl::refresh() tools::wallet2::address_book_row * row = &rows.at(i); std::string payment_id = (row->m_payment_id == crypto::null_hash)? "" : epee::string_tools::pod_to_hex(row->m_payment_id); - std::string address = cryptonote::get_account_address_as_str(m_wallet->m_wallet->testnet(),row->m_address); + std::string address = cryptonote::get_account_address_as_str(m_wallet->m_wallet->testnet(), row->m_is_subaddress, row->m_address); // convert the zero padded short payment id to integrated address - if (payment_id.length() > 16 && payment_id.substr(16).find_first_not_of('0') == std::string::npos) { + if (!row->m_is_subaddress && payment_id.length() > 16 && payment_id.substr(16).find_first_not_of('0') == std::string::npos) { payment_id = payment_id.substr(0,16); crypto::hash8 payment_id_short; if(tools::wallet2::parse_short_payment_id(payment_id, payment_id_short)) { diff --git a/src/wallet/api/pending_transaction.cpp b/src/wallet/api/pending_transaction.cpp index c98a599e7..e17931de1 100644 --- a/src/wallet/api/pending_transaction.cpp +++ b/src/wallet/api/pending_transaction.cpp @@ -172,6 +172,22 @@ uint64_t PendingTransactionImpl::txCount() const return m_pending_tx.size(); } +std::vector<uint32_t> PendingTransactionImpl::subaddrAccount() const +{ + std::vector<uint32_t> result; + for (const auto& ptx : m_pending_tx) + result.push_back(ptx.construction_data.subaddr_account); + return result; +} + +std::vector<std::set<uint32_t>> PendingTransactionImpl::subaddrIndices() const +{ + std::vector<std::set<uint32_t>> result; + for (const auto& ptx : m_pending_tx) + result.push_back(ptx.construction_data.subaddr_indices); + return result; +} + } namespace Bitmonero = Monero; diff --git a/src/wallet/api/pending_transaction.h b/src/wallet/api/pending_transaction.h index 0c3e95a85..e5b33ac01 100644 --- a/src/wallet/api/pending_transaction.h +++ b/src/wallet/api/pending_transaction.h @@ -51,6 +51,8 @@ public: uint64_t fee() const; std::vector<std::string> txid() const; uint64_t txCount() const; + std::vector<uint32_t> subaddrAccount() const; + std::vector<std::set<uint32_t>> subaddrIndices() const; // TODO: continue with interface; private: diff --git a/src/wallet/api/subaddress.cpp b/src/wallet/api/subaddress.cpp new file mode 100644 index 000000000..ceda9a9da --- /dev/null +++ b/src/wallet/api/subaddress.cpp @@ -0,0 +1,91 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "subaddress.h" +#include "wallet.h" +#include "crypto/hash.h" +#include "wallet/wallet2.h" +#include "common_defines.h" + +#include <vector> + +namespace Monero { + +Subaddress::~Subaddress() {} + +SubaddressImpl::SubaddressImpl(WalletImpl *wallet) + : m_wallet(wallet) {} + +void SubaddressImpl::addRow(uint32_t accountIndex, const std::string &label) +{ + m_wallet->m_wallet->add_subaddress(accountIndex, label); + refresh(accountIndex); +} + +void SubaddressImpl::setLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label) +{ + try + { + m_wallet->m_wallet->set_subaddress_label({accountIndex, addressIndex}, label); + refresh(accountIndex); + } + catch (const std::exception& e) + { + LOG_ERROR("setLabel: " << e.what()); + } +} + +void SubaddressImpl::refresh(uint32_t accountIndex) +{ + LOG_PRINT_L2("Refreshing subaddress"); + + clearRows(); + for (size_t i = 0; i < m_wallet->m_wallet->get_num_subaddresses(accountIndex); ++i) + { + m_rows.push_back(new SubaddressRow(i, m_wallet->m_wallet->get_subaddress_as_str({accountIndex, (uint32_t)i}), m_wallet->m_wallet->get_subaddress_label({accountIndex, (uint32_t)i}))); + } +} + +void SubaddressImpl::clearRows() { + for (auto r : m_rows) { + delete r; + } + m_rows.clear(); +} + +std::vector<SubaddressRow*> SubaddressImpl::getAll() const +{ + return m_rows; +} + +SubaddressImpl::~SubaddressImpl() +{ + clearRows(); +} + +} // namespace diff --git a/src/wallet/api/subaddress.h b/src/wallet/api/subaddress.h new file mode 100644 index 000000000..e3e28eba1 --- /dev/null +++ b/src/wallet/api/subaddress.h @@ -0,0 +1,56 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "wallet/wallet2_api.h" +#include "wallet/wallet2.h" + +namespace Monero { + +class WalletImpl; + +class SubaddressImpl : public Subaddress +{ +public: + SubaddressImpl(WalletImpl * wallet); + ~SubaddressImpl(); + + // Fetches addresses from Wallet2 + void refresh(uint32_t accountIndex); + std::vector<SubaddressRow*> getAll() const; + void addRow(uint32_t accountIndex, const std::string &label); + void setLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label); + +private: + void clearRows(); + +private: + WalletImpl *m_wallet; + std::vector<SubaddressRow*> m_rows; +}; + +} diff --git a/src/wallet/api/subaddress_account.cpp b/src/wallet/api/subaddress_account.cpp new file mode 100644 index 000000000..736ef874e --- /dev/null +++ b/src/wallet/api/subaddress_account.cpp @@ -0,0 +1,90 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "subaddress_account.h" +#include "wallet.h" +#include "crypto/hash.h" +#include "wallet/wallet2.h" +#include "common_defines.h" + +#include <vector> + +namespace Monero { + +SubaddressAccount::~SubaddressAccount() {} + +SubaddressAccountImpl::SubaddressAccountImpl(WalletImpl *wallet) + : m_wallet(wallet) {} + +void SubaddressAccountImpl::addRow(const std::string &label) +{ + m_wallet->m_wallet->add_subaddress_account(label); + refresh(); +} + +void SubaddressAccountImpl::setLabel(uint32_t accountIndex, const std::string &label) +{ + m_wallet->m_wallet->set_subaddress_label({accountIndex, 0}, label); + refresh(); +} + +void SubaddressAccountImpl::refresh() +{ + LOG_PRINT_L2("Refreshing subaddress account"); + + clearRows(); + for (uint32_t i = 0; i < m_wallet->m_wallet->get_num_subaddress_accounts(); ++i) + { + m_rows.push_back(new SubaddressAccountRow( + i, + m_wallet->m_wallet->get_subaddress_as_str({i,0}).substr(0,6), + m_wallet->m_wallet->get_subaddress_label({i,0}), + cryptonote::print_money(m_wallet->m_wallet->balance(i)), + cryptonote::print_money(m_wallet->m_wallet->unlocked_balance(i)) + )); + } +} + +void SubaddressAccountImpl::clearRows() { + for (auto r : m_rows) { + delete r; + } + m_rows.clear(); +} + +std::vector<SubaddressAccountRow*> SubaddressAccountImpl::getAll() const +{ + return m_rows; +} + +SubaddressAccountImpl::~SubaddressAccountImpl() +{ + clearRows(); +} + +} // namespace diff --git a/src/wallet/api/subaddress_account.h b/src/wallet/api/subaddress_account.h new file mode 100644 index 000000000..107d7f87f --- /dev/null +++ b/src/wallet/api/subaddress_account.h @@ -0,0 +1,56 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "wallet/wallet2_api.h" +#include "wallet/wallet2.h" + +namespace Monero { + +class WalletImpl; + +class SubaddressAccountImpl : public SubaddressAccount +{ +public: + SubaddressAccountImpl(WalletImpl * wallet); + ~SubaddressAccountImpl(); + + // Fetches addresses from Wallet2 + void refresh(); + std::vector<SubaddressAccountRow*> getAll() const; + void addRow(const std::string &label); + void setLabel(uint32_t accountIndex, const std::string &label); + +private: + void clearRows(); + +private: + WalletImpl *m_wallet; + std::vector<SubaddressAccountRow*> m_rows; +}; + +} diff --git a/src/wallet/api/transaction_history.cpp b/src/wallet/api/transaction_history.cpp index 23d3905b2..b6ba8c359 100644 --- a/src/wallet/api/transaction_history.cpp +++ b/src/wallet/api/transaction_history.cpp @@ -130,6 +130,9 @@ void TransactionHistoryImpl::refresh() ti->m_direction = TransactionInfo::Direction_In; ti->m_hash = string_tools::pod_to_hex(pd.m_tx_hash); ti->m_blockheight = pd.m_block_height; + ti->m_subaddrIndex = { pd.m_subaddr_index.minor }; + ti->m_subaddrAccount = pd.m_subaddr_index.major; + ti->m_label = m_wallet->m_wallet->get_subaddress_label(pd.m_subaddr_index); ti->m_timestamp = pd.m_timestamp; ti->m_confirmations = wallet_height - pd.m_block_height; ti->m_unlock_time = pd.m_unlock_time; @@ -174,12 +177,15 @@ void TransactionHistoryImpl::refresh() ti->m_direction = TransactionInfo::Direction_Out; ti->m_hash = string_tools::pod_to_hex(hash); ti->m_blockheight = pd.m_block_height; + ti->m_subaddrIndex = pd.m_subaddr_indices; + ti->m_subaddrAccount = pd.m_subaddr_account; + ti->m_label = pd.m_subaddr_indices.size() == 1 ? m_wallet->m_wallet->get_subaddress_label({pd.m_subaddr_account, *pd.m_subaddr_indices.begin()}) : ""; ti->m_timestamp = pd.m_timestamp; ti->m_confirmations = wallet_height - pd.m_block_height; // single output transaction might contain multiple transfers for (const auto &d: pd.m_dests) { - ti->m_transfers.push_back({d.amount, get_account_address_as_str(m_wallet->m_wallet->testnet(), d.addr)}); + ti->m_transfers.push_back({d.amount, get_account_address_as_str(m_wallet->m_wallet->testnet(), d.is_subaddress, d.addr)}); } m_history.push_back(ti); } @@ -199,12 +205,15 @@ void TransactionHistoryImpl::refresh() TransactionInfoImpl * ti = new TransactionInfoImpl(); ti->m_paymentid = payment_id; - ti->m_amount = amount - pd.m_change; + ti->m_amount = amount - pd.m_change - fee; ti->m_fee = fee; ti->m_direction = TransactionInfo::Direction_Out; ti->m_failed = is_failed; ti->m_pending = true; ti->m_hash = string_tools::pod_to_hex(hash); + ti->m_subaddrIndex = pd.m_subaddr_indices; + ti->m_subaddrAccount = pd.m_subaddr_account; + ti->m_label = pd.m_subaddr_indices.size() == 1 ? m_wallet->m_wallet->get_subaddress_label({pd.m_subaddr_account, *pd.m_subaddr_indices.begin()}) : ""; ti->m_timestamp = pd.m_timestamp; ti->m_confirmations = 0; m_history.push_back(ti); @@ -226,6 +235,9 @@ void TransactionHistoryImpl::refresh() ti->m_hash = string_tools::pod_to_hex(pd.m_tx_hash); ti->m_blockheight = pd.m_block_height; ti->m_pending = true; + ti->m_subaddrIndex = { pd.m_subaddr_index.minor }; + ti->m_subaddrAccount = pd.m_subaddr_index.major; + ti->m_label = m_wallet->m_wallet->get_subaddress_label(pd.m_subaddr_index); ti->m_timestamp = pd.m_timestamp; ti->m_confirmations = 0; m_history.push_back(ti); diff --git a/src/wallet/api/transaction_info.cpp b/src/wallet/api/transaction_info.cpp index 171272265..1a5df454c 100644 --- a/src/wallet/api/transaction_info.cpp +++ b/src/wallet/api/transaction_info.cpp @@ -48,6 +48,7 @@ TransactionInfoImpl::TransactionInfoImpl() , m_amount(0) , m_fee(0) , m_blockheight(0) + , m_subaddrAccount(0) , m_timestamp(0) , m_confirmations(0) , m_unlock_time(0) @@ -91,6 +92,22 @@ uint64_t TransactionInfoImpl::blockHeight() const return m_blockheight; } +std::set<uint32_t> TransactionInfoImpl::subaddrIndex() const +{ + return m_subaddrIndex; +} + +uint32_t TransactionInfoImpl::subaddrAccount() const +{ + return m_subaddrAccount; +} + +string TransactionInfoImpl::label() const +{ + return m_label; +} + + string TransactionInfoImpl::hash() const { return m_hash; diff --git a/src/wallet/api/transaction_info.h b/src/wallet/api/transaction_info.h index ee56b859f..d19ef8899 100644 --- a/src/wallet/api/transaction_info.h +++ b/src/wallet/api/transaction_info.h @@ -50,6 +50,9 @@ public: //! always 0 for incoming txes virtual uint64_t fee() const; virtual uint64_t blockHeight() const; + virtual std::set<uint32_t> subaddrIndex() const; + virtual uint32_t subaddrAccount() const; + virtual std::string label() const; virtual std::string hash() const; virtual std::time_t timestamp() const; @@ -65,6 +68,9 @@ private: uint64_t m_amount; uint64_t m_fee; uint64_t m_blockheight; + std::set<uint32_t> m_subaddrIndex; // always unique index for incoming transfers; can be multiple indices for outgoing transfers + uint32_t m_subaddrAccount; + std::string m_label; std::string m_hash; std::time_t m_timestamp; std::string m_paymentid; diff --git a/src/wallet/api/unsigned_transaction.cpp b/src/wallet/api/unsigned_transaction.cpp index 5105278e4..4c8c5ade2 100644 --- a/src/wallet/api/unsigned_transaction.cpp +++ b/src/wallet/api/unsigned_transaction.cpp @@ -102,12 +102,38 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu // gather info to ask the user uint64_t amount = 0, amount_to_dests = 0, change = 0; size_t min_ring_size = ~0; - std::unordered_map<std::string, uint64_t> dests; - const std::string wallet_address = m_wallet.m_wallet->get_account().get_public_address_str(m_wallet.m_wallet->testnet()); + std::unordered_map<cryptonote::account_public_address, std::pair<std::string, uint64_t>> dests; int first_known_non_zero_change_index = -1; + std::string payment_id_string = ""; for (size_t n = 0; n < get_num_txes(); ++n) { const tools::wallet2::tx_construction_data &cd = get_tx(n); + + std::vector<cryptonote::tx_extra_field> tx_extra_fields; + bool has_encrypted_payment_id = false; + crypto::hash8 payment_id8 = crypto::null_hash8; + if (cryptonote::parse_tx_extra(cd.extra, tx_extra_fields)) + { + cryptonote::tx_extra_nonce extra_nonce; + if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) + { + crypto::hash payment_id; + if(cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) + { + if (!payment_id_string.empty()) + payment_id_string += ", "; + payment_id_string = std::string("encrypted payment ID ") + epee::string_tools::pod_to_hex(payment_id8); + has_encrypted_payment_id = true; + } + else if (cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) + { + if (!payment_id_string.empty()) + payment_id_string += ", "; + payment_id_string = std::string("unencrypted payment ID ") + epee::string_tools::pod_to_hex(payment_id); + } + } + } + for (size_t s = 0; s < cd.sources.size(); ++s) { amount += cd.sources[s].amount; @@ -118,24 +144,31 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu for (size_t d = 0; d < cd.splitted_dsts.size(); ++d) { const cryptonote::tx_destination_entry &entry = cd.splitted_dsts[d]; - std::string address = get_account_address_as_str(m_wallet.m_wallet->testnet(), entry.addr); - std::unordered_map<std::string,uint64_t>::iterator i = dests.find(address); + std::string address, standard_address = get_account_address_as_str(m_wallet.testnet(), entry.is_subaddress, entry.addr); + if (has_encrypted_payment_id && !entry.is_subaddress) + { + address = get_account_integrated_address_as_str(m_wallet.testnet(), entry.addr, payment_id8); + address += std::string(" (" + standard_address + " with encrypted payment id " + epee::string_tools::pod_to_hex(payment_id8) + ")"); + } + else + address = standard_address; + auto i = dests.find(entry.addr); if (i == dests.end()) - dests.insert(std::make_pair(address, entry.amount)); + dests.insert(std::make_pair(entry.addr, std::make_pair(address, entry.amount))); else - i->second += entry.amount; + i->second.second += entry.amount; amount_to_dests += entry.amount; } if (cd.change_dts.amount > 0) { - std::unordered_map<std::string, uint64_t>::iterator it = dests.find(get_account_address_as_str(m_wallet.m_wallet->testnet(), cd.change_dts.addr)); + auto it = dests.find(cd.change_dts.addr); if (it == dests.end()) { m_status = Status_Error; m_errorString = tr("Claimed change does not go to a paid address"); return false; } - if (it->second < cd.change_dts.amount) + if (it->second.second < cd.change_dts.amount) { m_status = Status_Error; m_errorString = tr("Claimed change is larger than payment to the change address"); @@ -153,15 +186,15 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu } } change += cd.change_dts.amount; - it->second -= cd.change_dts.amount; - if (it->second == 0) - dests.erase(get_account_address_as_str(m_wallet.m_wallet->testnet(), cd.change_dts.addr)); + it->second.second -= cd.change_dts.amount; + if (it->second.second == 0) + dests.erase(cd.change_dts.addr); } } std::string dest_string; - for (std::unordered_map<std::string, uint64_t>::const_iterator i = dests.begin(); i != dests.end(); ) + for (auto i = dests.begin(); i != dests.end(); ) { - dest_string += (boost::format(tr("sending %s to %s")) % cryptonote::print_money(i->second) % i->first).str(); + dest_string += (boost::format(tr("sending %s to %s")) % cryptonote::print_money(i->second.second) % i->second.first).str(); ++i; if (i != dests.end()) dest_string += ", "; @@ -172,7 +205,7 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu std::string change_string; if (change > 0) { - std::string address = get_account_address_as_str(m_wallet.m_wallet->testnet(), get_tx(0).change_dts.addr); + std::string address = get_account_address_as_str(m_wallet.m_wallet->testnet(), get_tx(0).subaddr_account > 0, get_tx(0).change_dts.addr); change_string += (boost::format(tr("%s change to %s")) % cryptonote::print_money(change) % address).str(); } else @@ -260,7 +293,7 @@ std::vector<std::string> UnsignedTransactionImpl::recipientAddress() const // TODO: return integrated address if short payment ID exists std::vector<string> result; for (const auto &utx: m_unsigned_tx_set.txes) { - result.push_back(cryptonote::get_account_address_as_str(m_wallet.m_wallet->testnet(), utx.dests[0].addr)); + result.push_back(cryptonote::get_account_address_as_str(m_wallet.m_wallet->testnet(), utx.dests[0].is_subaddress, utx.dests[0].addr)); } return result; } diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 9cd72b543..a932d9d6f 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -34,6 +34,8 @@ #include "unsigned_transaction.h" #include "transaction_history.h" #include "address_book.h" +#include "subaddress.h" +#include "subaddress_account.h" #include "common_defines.h" #include "common/util.h" @@ -100,14 +102,15 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback } } - virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) + virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) { std::string tx_hash = epee::string_tools::pod_to_hex(txid); LOG_PRINT_L3(__FUNCTION__ << ": money received. height: " << height << ", tx: " << tx_hash - << ", amount: " << print_money(amount)); + << ", amount: " << print_money(amount) + << ", idx: " << subaddr_index); // do not signal on received tx if wallet is not syncronized completely if (m_listener && m_wallet->synchronized()) { m_listener->moneyReceived(tx_hash, amount); @@ -115,14 +118,15 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback } } - virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) + virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) { std::string tx_hash = epee::string_tools::pod_to_hex(txid); LOG_PRINT_L3(__FUNCTION__ << ": unconfirmed money received. height: " << height << ", tx: " << tx_hash - << ", amount: " << print_money(amount)); + << ", amount: " << print_money(amount) + << ", idx: " << subaddr_index); // do not signal on received tx if wallet is not syncronized completely if (m_listener && m_wallet->synchronized()) { m_listener->unconfirmedMoneyReceived(tx_hash, amount); @@ -131,13 +135,14 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback } virtual void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, - uint64_t amount, const cryptonote::transaction& spend_tx) + uint64_t amount, const cryptonote::transaction& spend_tx, const cryptonote::subaddress_index& subaddr_index) { // TODO; std::string tx_hash = epee::string_tools::pod_to_hex(txid); LOG_PRINT_L3(__FUNCTION__ << ": money spent. height: " << height << ", tx: " << tx_hash - << ", amount: " << print_money(amount)); + << ", amount: " << print_money(amount) + << ", idx: " << subaddr_index); // do not signal on sent tx if wallet is not syncronized completely if (m_listener && m_wallet->synchronized()) { m_listener->moneySpent(tx_hash, amount); @@ -198,18 +203,14 @@ bool Wallet::paymentIdValid(const string &paiment_id) bool Wallet::addressValid(const std::string &str, bool testnet) { - bool has_payment_id; - cryptonote::account_public_address address; - crypto::hash8 pid; - return get_account_integrated_address_from_str(address, has_payment_id, pid, testnet, str); + cryptonote::address_parse_info info; + return get_account_address_from_str(info, testnet, str); } bool Wallet::keyValid(const std::string &secret_key_string, const std::string &address_string, bool isViewKey, bool testnet, std::string &error) { - bool has_payment_id; - cryptonote::account_public_address address; - crypto::hash8 pid; - if(!get_account_integrated_address_from_str(address, has_payment_id, pid, testnet, address_string)) { + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet, address_string)) { error = tr("Failed to parse address"); return false; } @@ -230,9 +231,9 @@ bool Wallet::keyValid(const std::string &secret_key_string, const std::string &a } bool matchAddress = false; if(isViewKey) - matchAddress = address.m_view_public_key == pkey; + matchAddress = info.address.m_view_public_key == pkey; else - matchAddress = address.m_spend_public_key == pkey; + matchAddress = info.address.m_spend_public_key == pkey; if(!matchAddress) { error = tr("key does not match address"); @@ -244,14 +245,12 @@ bool Wallet::keyValid(const std::string &secret_key_string, const std::string &a std::string Wallet::paymentIdFromAddress(const std::string &str, bool testnet) { - bool has_payment_id; - cryptonote::account_public_address address; - crypto::hash8 pid; - if (!get_account_integrated_address_from_str(address, has_payment_id, pid, testnet, str)) + cryptonote::address_parse_info info; + if (!get_account_address_from_str(info, testnet, str)) return ""; - if (!has_payment_id) + if (!info.has_payment_id) return ""; - return epee::string_tools::pod_to_hex(pid); + return epee::string_tools::pod_to_hex(info.payment_id); } uint64_t Wallet::maximumAllowedAmount() @@ -286,6 +285,8 @@ WalletImpl::WalletImpl(bool testnet) m_refreshThreadDone = false; m_refreshEnabled = false; m_addressBook = new AddressBookImpl(this); + m_subaddress = new SubaddressImpl(this); + m_subaddressAccount = new SubaddressAccountImpl(this); m_refreshIntervalMillis = DEFAULT_REFRESH_INTERVAL_MILLIS; @@ -309,6 +310,8 @@ WalletImpl::~WalletImpl() delete m_wallet2Callback; delete m_history; delete m_addressBook; + delete m_subaddress; + delete m_subaddressAccount; delete m_wallet; LOG_PRINT_L1(__FUNCTION__ << " finished"); } @@ -423,10 +426,8 @@ bool WalletImpl::recoverFromKeys(const std::string &path, const std::string &viewkey_string, const std::string &spendkey_string) { - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, m_wallet->testnet(), address_string)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, m_wallet->testnet(), address_string)) { m_errorString = tr("failed to parse address"); m_status = Status_Error; @@ -471,7 +472,7 @@ bool WalletImpl::recoverFromKeys(const std::string &path, m_status = Status_Error; return false; } - if (address.m_spend_public_key != pkey) { + if (info.address.m_spend_public_key != pkey) { m_errorString = tr("spend key does not match address"); m_status = Status_Error; return false; @@ -482,7 +483,7 @@ bool WalletImpl::recoverFromKeys(const std::string &path, m_status = Status_Error; return false; } - if (address.m_view_public_key != pkey) { + if (info.address.m_view_public_key != pkey) { m_errorString = tr("view key does not match address"); m_status = Status_Error; return false; @@ -491,12 +492,12 @@ bool WalletImpl::recoverFromKeys(const std::string &path, try { if (has_spendkey) { - m_wallet->generate(path, "", address, spendkey, viewkey); + m_wallet->generate(path, "", info.address, spendkey, viewkey); setSeedLanguage(language); LOG_PRINT_L1("Generated new wallet from keys with seed language: " + language); } else { - m_wallet->generate(path, "", address, viewkey); + m_wallet->generate(path, "", info.address, viewkey); LOG_PRINT_L1("Generated new view only wallet from keys"); } @@ -635,9 +636,9 @@ bool WalletImpl::setPassword(const std::string &password) return m_status == Status_Ok; } -std::string WalletImpl::address() const +std::string WalletImpl::address(uint32_t accountIndex, uint32_t addressIndex) const { - return m_wallet->get_account().get_public_address_str(m_wallet->testnet()); + return m_wallet->get_subaddress_as_str({accountIndex, addressIndex}); } std::string WalletImpl::integratedAddress(const std::string &payment_id) const @@ -646,7 +647,7 @@ std::string WalletImpl::integratedAddress(const std::string &payment_id) const if (!tools::wallet2::parse_short_payment_id(payment_id, pid)) { return ""; } - return m_wallet->get_account().get_public_integrated_address_str(pid, m_wallet->testnet()); + return m_wallet->get_integrated_address_as_str(pid); } std::string WalletImpl::secretViewKey() const @@ -720,14 +721,14 @@ void WalletImpl::setRecoveringFromSeed(bool recoveringFromSeed) m_recoveringFromSeed = recoveringFromSeed; } -uint64_t WalletImpl::balance() const +uint64_t WalletImpl::balance(uint32_t accountIndex) const { - return m_wallet->balance(); + return m_wallet->balance(accountIndex); } -uint64_t WalletImpl::unlockedBalance() const +uint64_t WalletImpl::unlockedBalance(uint32_t accountIndex) const { - return m_wallet->unlocked_balance(); + return m_wallet->unlocked_balance(accountIndex); } uint64_t WalletImpl::blockChainHeight() const @@ -914,6 +915,50 @@ bool WalletImpl::importKeyImages(const string &filename) return true; } +void WalletImpl::addSubaddressAccount(const std::string& label) +{ + m_wallet->add_subaddress_account(label); +} +size_t WalletImpl::numSubaddressAccounts() const +{ + return m_wallet->get_num_subaddress_accounts(); +} +size_t WalletImpl::numSubaddresses(uint32_t accountIndex) const +{ + return m_wallet->get_num_subaddresses(accountIndex); +} +void WalletImpl::addSubaddress(uint32_t accountIndex, const std::string& label) +{ + m_wallet->add_subaddress(accountIndex, label); +} +std::string WalletImpl::getSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex) const +{ + try + { + return m_wallet->get_subaddress_label({accountIndex, addressIndex}); + } + catch (const std::exception &e) + { + LOG_ERROR("Error getting subaddress label: ") << e.what(); + m_errorString = string(tr("Failed to get subaddress label: ")) + e.what(); + m_status = Status_Error; + return ""; + } +} +void WalletImpl::setSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label) +{ + try + { + return m_wallet->set_subaddress_label({accountIndex, addressIndex}, label); + } + catch (const std::exception &e) + { + LOG_ERROR("Error setting subaddress label: ") << e.what(); + m_errorString = string(tr("Failed to set subaddress label: ")) + e.what(); + m_status = Status_Error; + } +} + // TODO: // 1 - properly handle payment id (add another menthod with explicit 'payment_id' param) // 2 - check / design how "Transaction" can be single interface @@ -925,6 +970,7 @@ bool WalletImpl::importKeyImages(const string &filename) // - confirmed_transfer_details) PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const string &payment_id, optional<uint64_t> amount, uint32_t mixin_count, + uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, PendingTransaction::Priority priority) { @@ -932,11 +978,9 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const // Pause refresh thread while creating transaction pauseRefresh(); - cryptonote::account_public_address addr; + cryptonote::address_parse_info info; // indicates if dst_addr is integrated address (address + payment_id) - bool has_payment_id; - crypto::hash8 payment_id_short; // TODO: (https://bitcointalk.org/index.php?topic=753252.msg9985441#msg9985441) size_t fake_outs_count = mixin_count > 0 ? mixin_count : m_wallet->default_mixin(); if (fake_outs_count == 0) @@ -945,7 +989,7 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const PendingTransactionImpl * transaction = new PendingTransactionImpl(*this); do { - if(!cryptonote::get_account_integrated_address_from_str(addr, has_payment_id, payment_id_short, m_wallet->testnet(), dst_addr)) { + if(!cryptonote::get_account_address_from_str(info, m_wallet->testnet(), dst_addr)) { // TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982 m_status = Status_Error; m_errorString = "Invalid destination address"; @@ -955,7 +999,7 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const std::vector<uint8_t> extra; // if dst_addr is not an integrated address, parse payment_id - if (!has_payment_id && !payment_id.empty()) { + if (!info.has_payment_id && !payment_id.empty()) { // copy-pasted from simplewallet.cpp:2212 crypto::hash payment_id_long; bool r = tools::wallet2::parse_long_payment_id(payment_id, payment_id_long); @@ -964,10 +1008,10 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const cryptonote::set_payment_id_to_tx_extra_nonce(extra_nonce, payment_id_long); r = add_extra_nonce_to_tx_extra(extra, extra_nonce); } else { - r = tools::wallet2::parse_short_payment_id(payment_id, payment_id_short); + r = tools::wallet2::parse_short_payment_id(payment_id, info.payment_id); if (r) { std::string extra_nonce; - set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, payment_id_short); + set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, info.payment_id); r = add_extra_nonce_to_tx_extra(extra, extra_nonce); } } @@ -978,13 +1022,13 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const break; } } - else if (has_payment_id) { + else if (info.has_payment_id) { std::string extra_nonce; - set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, payment_id_short); + set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, info.payment_id); bool r = add_extra_nonce_to_tx_extra(extra, extra_nonce); if (!r) { m_status = Status_Error; - m_errorString = tr("Failed to add short payment id: ") + epee::string_tools::pod_to_hex(payment_id_short); + m_errorString = tr("Failed to add short payment id: ") + epee::string_tools::pod_to_hex(info.payment_id); break; } } @@ -996,16 +1040,23 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const if (amount) { vector<cryptonote::tx_destination_entry> dsts; cryptonote::tx_destination_entry de; - de.addr = addr; + de.addr = info.address; de.amount = *amount; + de.is_subaddress = info.is_subaddress; dsts.push_back(de); transaction->m_pending_tx = m_wallet->create_transactions_2(dsts, fake_outs_count, 0 /* unlock_time */, static_cast<uint32_t>(priority), - extra, m_trustedDaemon); + extra, subaddr_account, subaddr_indices, m_trustedDaemon); } else { - transaction->m_pending_tx = m_wallet->create_transactions_all(0, addr, fake_outs_count, 0 /* unlock_time */, + // for the GUI, sweep_all (i.e. amount set as "(all)") will always sweep all the funds in all the addresses + if (subaddr_indices.empty()) + { + for (uint32_t index = 0; index < m_wallet->get_num_subaddresses(subaddr_account); ++index) + subaddr_indices.insert(index); + } + transaction->m_pending_tx = m_wallet->create_transactions_all(0, info.address, info.is_subaddress, fake_outs_count, 0 /* unlock_time */, static_cast<uint32_t>(priority), - extra, m_trustedDaemon); + extra, subaddr_account, subaddr_indices, m_trustedDaemon); } } catch (const tools::error::daemon_busy&) { @@ -1186,16 +1237,26 @@ void WalletImpl::disposeTransaction(PendingTransaction *t) delete t; } -TransactionHistory *WalletImpl::history() const +TransactionHistory *WalletImpl::history() { return m_history; } -AddressBook *WalletImpl::addressBook() const +AddressBook *WalletImpl::addressBook() { return m_addressBook; } +Subaddress *WalletImpl::subaddress() +{ + return m_subaddress; +} + +SubaddressAccount *WalletImpl::subaddressAccount() +{ + return m_subaddressAccount; +} + void WalletImpl::setListener(WalletListener *l) { // TODO thread synchronization; @@ -1243,7 +1304,8 @@ std::string WalletImpl::getTxKey(const std::string &txid) const const crypto::hash htxid = *reinterpret_cast<const crypto::hash*>(txid_data.data()); crypto::secret_key tx_key; - if (m_wallet->get_tx_key(htxid, tx_key)) + std::vector<crypto::secret_key> additional_tx_keys; + if (m_wallet->get_tx_key(htxid, tx_key, additional_tx_keys)) { return epee::string_tools::pod_to_hex(tx_key); } @@ -1260,14 +1322,12 @@ std::string WalletImpl::signMessage(const std::string &message) bool WalletImpl::verifySignedMessage(const std::string &message, const std::string &address, const std::string &signature) const { - cryptonote::account_public_address addr; - bool has_payment_id; - crypto::hash8 payment_id; + cryptonote::address_parse_info info; - if (!cryptonote::get_account_integrated_address_from_str(addr, has_payment_id, payment_id, m_wallet->testnet(), address)) + if (!cryptonote::get_account_address_from_str(info, m_wallet->testnet(), address)) return false; - return m_wallet->verify(message, addr, signature); + return m_wallet->verify(message, info.address, signature); } bool WalletImpl::connectToDaemon() diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 1e3d1e600..020c5e46a 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -45,6 +45,8 @@ class TransactionHistoryImpl; class PendingTransactionImpl; class UnsignedTransactionImpl; class AddressBookImpl; +class SubaddressImpl; +class SubaddressAccountImpl; struct Wallet2CallbackImpl; class WalletImpl : public Wallet @@ -71,7 +73,7 @@ public: int status() const; std::string errorString() const; bool setPassword(const std::string &password); - std::string address() const; + std::string address(uint32_t accountIndex, uint32_t addressIndex) const; std::string integratedAddress(const std::string &payment_id) const; std::string secretViewKey() const; std::string publicViewKey() const; @@ -86,8 +88,8 @@ public: ConnectionStatus connected() const; void setTrustedDaemon(bool arg); bool trustedDaemon() const; - uint64_t balance() const; - uint64_t unlockedBalance() const; + uint64_t balance(uint32_t accountIndex) const; + uint64_t unlockedBalance(uint32_t accountIndex) const; uint64_t blockChainHeight() const; uint64_t approximateBlockChainHeight() const; uint64_t daemonBlockChainHeight() const; @@ -106,8 +108,17 @@ public: void hardForkInfo(uint8_t &version, uint64_t &earliest_height) const; bool useForkRules(uint8_t version, int64_t early_blocks) const; + void addSubaddressAccount(const std::string& label); + size_t numSubaddressAccounts() const; + size_t numSubaddresses(uint32_t accountIndex) const; + void addSubaddress(uint32_t accountIndex, const std::string& label); + std::string getSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex) const; + void setSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label); + PendingTransaction * createTransaction(const std::string &dst_addr, const std::string &payment_id, optional<uint64_t> amount, uint32_t mixin_count, + uint32_t subaddr_account, + std::set<uint32_t> subaddr_indices, PendingTransaction::Priority priority = PendingTransaction::Priority_Low); virtual PendingTransaction * createSweepUnmixableTransaction(); bool submitTransaction(const std::string &fileName); @@ -116,8 +127,10 @@ public: bool importKeyImages(const std::string &filename); virtual void disposeTransaction(PendingTransaction * t); - virtual TransactionHistory * history() const; - virtual AddressBook * addressBook() const; + virtual TransactionHistory * history(); + virtual AddressBook * addressBook(); + virtual Subaddress * subaddress(); + virtual SubaddressAccount * subaddressAccount(); virtual void setListener(WalletListener * l); virtual uint32_t defaultMixin() const; virtual void setDefaultMixin(uint32_t arg); @@ -146,6 +159,8 @@ private: friend class TransactionHistoryImpl; friend struct Wallet2CallbackImpl; friend class AddressBookImpl; + friend class SubaddressImpl; + friend class SubaddressAccountImpl; tools::wallet2 * m_wallet; mutable std::atomic<int> m_status; @@ -155,6 +170,8 @@ private: bool m_trustedDaemon; Wallet2CallbackImpl * m_wallet2Callback; AddressBookImpl * m_addressBook; + SubaddressImpl * m_subaddress; + SubaddressAccountImpl * m_subaddressAccount; // multi-threaded refresh stuff std::atomic<bool> m_refreshEnabled; diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index 897137d35..a64766c84 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -215,10 +215,8 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: tx_key = *reinterpret_cast<const crypto::secret_key*>(tx_key_data.data()); bool testnet = address_text[0] != '4'; - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id; - if(!cryptonote::get_account_integrated_address_from_str(address, has_payment_id, payment_id, testnet, address_text)) + cryptonote::address_parse_info info; + if(!cryptonote::get_account_address_from_str(info, testnet, address_text)) { error = tr("failed to parse address"); return false; @@ -258,7 +256,7 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: } crypto::key_derivation derivation; - if (!crypto::generate_key_derivation(address.m_view_public_key, tx_key, derivation)) + if (!crypto::generate_key_derivation(info.address.m_view_public_key, tx_key, derivation)) { error = tr("failed to generate key derivation from supplied parameters"); return false; @@ -272,7 +270,7 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: continue; const cryptonote::txout_to_key tx_out_to_key = boost::get<cryptonote::txout_to_key>(tx.vout[n].target); crypto::public_key pubkey; - derive_public_key(derivation, n, address.m_spend_public_key, pubkey); + derive_public_key(derivation, n, info.address.m_spend_public_key, pubkey); if (pubkey == tx_out_to_key.key) { uint64_t amount; @@ -287,7 +285,7 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: rct::key Ctmp; //rct::key amount_key = rct::hash_to_scalar(rct::scalarmultKey(rct::pk2rct(address.m_view_public_key), rct::sk2rct(tx_key))); crypto::key_derivation derivation; - bool r = crypto::generate_key_derivation(address.m_view_public_key, tx_key, derivation); + bool r = crypto::generate_key_derivation(info.address.m_view_public_key, tx_key, derivation); if (!r) { LOG_ERROR("Failed to generate key derivation to decode rct output " << n); @@ -322,11 +320,11 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: if (received > 0) { - LOG_PRINT_L1(get_account_address_as_str(testnet, address) << " " << tr("received") << " " << cryptonote::print_money(received) << " " << tr("in txid") << " " << txid); + LOG_PRINT_L1(get_account_address_as_str(testnet, info.is_subaddress, info.address) << " " << tr("received") << " " << cryptonote::print_money(received) << " " << tr("in txid") << " " << txid); } else { - LOG_PRINT_L1(get_account_address_as_str(testnet, address) << " " << tr("received nothing in txid") << " " << txid); + LOG_PRINT_L1(get_account_address_as_str(testnet, info.is_subaddress, info.address) << " " << tr("received nothing in txid") << " " << txid); } if (res.txs.front().in_pool) { diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index dc4f4879b..d451baa09 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -91,6 +91,9 @@ using namespace cryptonote; #define SECOND_OUTPUT_RELATEDNESS_THRESHOLD 0.0f +#define SUBADDRESS_LOOKAHEAD_MAJOR 50 +#define SUBADDRESS_LOOKAHEAD_MINOR 200 + #define KEY_IMAGE_EXPORT_FILE_MAGIC "Monero key image export\002" namespace @@ -317,10 +320,8 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, // public key if it was not given if (field_address_found) { - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, testnet, field_address)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet, field_address)) { tools::fail_msg_writer() << tools::wallet2::tr("invalid address"); return false; @@ -332,7 +333,7 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, tools::fail_msg_writer() << tools::wallet2::tr("failed to verify view key secret key"); return false; } - if (address.m_view_public_key != pkey) { + if (info.address.m_view_public_key != pkey) { tools::fail_msg_writer() << tools::wallet2::tr("view key does not match standard address"); return false; } @@ -344,7 +345,7 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, tools::fail_msg_writer() << tools::wallet2::tr("failed to verify spend key secret key"); return false; } - if (address.m_spend_public_key != pkey) { + if (info.address.m_spend_public_key != pkey) { tools::fail_msg_writer() << tools::wallet2::tr("spend key does not match standard address"); return false; } @@ -381,15 +382,13 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, // from the address if (field_address_found) { - cryptonote::account_public_address address2; - bool has_payment_id; - crypto::hash8 new_payment_id; - if (!get_account_integrated_address_from_str(address2, has_payment_id, new_payment_id, testnet, field_address)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet, field_address)) { tools::fail_msg_writer() << tools::wallet2::tr("failed to parse address: ") << field_address; return false; } - address.m_spend_public_key = address2.m_spend_public_key; + address.m_spend_public_key = info.address.m_spend_public_key; } wallet->generate(field_filename, field_password, address, viewkey); } @@ -583,6 +582,129 @@ void wallet2::set_seed_language(const std::string &language) { seed_language = language; } +//---------------------------------------------------------------------------------------------------- +cryptonote::account_public_address wallet2::get_subaddress(const cryptonote::subaddress_index& index) const +{ + const cryptonote::account_keys& keys = m_account.get_keys(); + if (index.is_zero()) + return keys.m_account_address; + + crypto::public_key D = get_subaddress_spend_public_key(index); + + // C = a*D + crypto::public_key C = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(D), rct::sk2rct(keys.m_view_secret_key))); // could have defined secret_key_mult_public_key() under src/crypto + + // result: (C, D) + cryptonote::account_public_address address; + address.m_view_public_key = C; + address.m_spend_public_key = D; + return address; +} +//---------------------------------------------------------------------------------------------------- +crypto::public_key wallet2::get_subaddress_spend_public_key(const cryptonote::subaddress_index& index) const +{ + const cryptonote::account_keys& keys = m_account.get_keys(); + if (index.is_zero()) + return keys.m_account_address.m_spend_public_key; + + // m = Hs(a || index_major || index_minor) + crypto::secret_key m = cryptonote::get_subaddress_secret_key(keys.m_view_secret_key, index); + + // M = m*G + crypto::public_key M; + crypto::secret_key_to_public_key(m, M); + + // D = B + M + rct::key D_rct; + rct::addKeys(D_rct, rct::pk2rct(keys.m_account_address.m_spend_public_key), rct::pk2rct(M)); // could have defined add_public_key() under src/crypto + crypto::public_key D = rct::rct2pk(D_rct); + + return D; +} +//---------------------------------------------------------------------------------------------------- +std::string wallet2::get_subaddress_as_str(const cryptonote::subaddress_index& index) const +{ + cryptonote::account_public_address address = get_subaddress(index); + return cryptonote::get_account_address_as_str(m_testnet, !index.is_zero(), address); +} +//---------------------------------------------------------------------------------------------------- +std::string wallet2::get_integrated_address_as_str(const crypto::hash8& payment_id) const +{ + return cryptonote::get_account_integrated_address_as_str(m_testnet, get_address(), payment_id); +} +//---------------------------------------------------------------------------------------------------- +void wallet2::add_subaddress_account(const std::string& label) +{ + uint32_t index_major = (uint32_t)get_num_subaddress_accounts(); + expand_subaddresses({index_major, 0}); + m_subaddress_labels[index_major][0] = label; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::add_subaddress(uint32_t index_major, const std::string& label) +{ + if (index_major >= m_subaddress_labels.size()) + throw std::runtime_error("index_major is out of bound"); + uint32_t index_minor = (uint32_t)get_num_subaddresses(index_major); + expand_subaddresses({index_major, index_minor}); + m_subaddress_labels[index_major][index_minor] = label; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::expand_subaddresses(const cryptonote::subaddress_index& index) +{ + if (m_subaddress_labels.size() <= index.major) + { + // add new accounts + cryptonote::subaddress_index index2; + for (index2.major = m_subaddress_labels.size(); index2.major < index.major + SUBADDRESS_LOOKAHEAD_MAJOR; ++index2.major) + { + for (index2.minor = 0; index2.minor < (index2.major == index.major ? index.minor : 0) + SUBADDRESS_LOOKAHEAD_MINOR; ++index2.minor) + { + if (m_subaddresses_inv.count(index2) == 0) + { + crypto::public_key D = get_subaddress_spend_public_key(index2); + m_subaddresses[D] = index2; + m_subaddresses_inv[index2] = D; + } + } + } + m_subaddress_labels.resize(index.major + 1, {"Untitled account"}); + m_subaddress_labels[index.major].resize(index.minor + 1); + } + else if (m_subaddress_labels[index.major].size() <= index.minor) + { + // add new subaddresses + cryptonote::subaddress_index index2 = index; + for (index2.minor = m_subaddress_labels[index.major].size(); index2.minor < index.minor + SUBADDRESS_LOOKAHEAD_MINOR; ++index2.minor) + { + if (m_subaddresses_inv.count(index2) == 0) + { + crypto::public_key D = get_subaddress_spend_public_key(index2); + m_subaddresses[D] = index2; + m_subaddresses_inv[index2] = D; + } + } + m_subaddress_labels[index.major].resize(index.minor + 1); + } +} +//---------------------------------------------------------------------------------------------------- +std::string wallet2::get_subaddress_label(const cryptonote::subaddress_index& index) const +{ + if (index.major >= m_subaddress_labels.size()) + throw std::runtime_error("index.major is out of bound"); + if (index.minor >= m_subaddress_labels[index.major].size()) + throw std::runtime_error("index.minor is out of bound"); + return m_subaddress_labels[index.major][index.minor]; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::set_subaddress_label(const cryptonote::subaddress_index& index, const std::string &label) +{ + if (index.major >= m_subaddress_labels.size()) + throw std::runtime_error("index.major is out of bound"); + if (index.minor >= m_subaddress_labels[index.major].size()) + throw std::runtime_error("index.minor is out of bound"); + m_subaddress_labels[index.major][index.minor] = label; +} +//---------------------------------------------------------------------------------------------------- /*! * \brief Tells if the wallet file is deprecated. */ @@ -607,7 +729,7 @@ void wallet2::set_unspent(size_t idx) td.m_spent_height = 0; } //---------------------------------------------------------------------------------------------------- -void wallet2::check_acc_out_precomp(const crypto::public_key &spend_public_key, const tx_out &o, const crypto::key_derivation &derivation, size_t i, tx_scan_info_t &tx_scan_info) const +void wallet2::check_acc_out_precomp(const tx_out &o, const crypto::key_derivation &derivation, const std::vector<crypto::key_derivation> &additional_derivations, size_t i, tx_scan_info_t &tx_scan_info) const { if (o.target.type() != typeid(txout_to_key)) { @@ -615,7 +737,7 @@ void wallet2::check_acc_out_precomp(const crypto::public_key &spend_public_key, LOG_ERROR("wrong type id in transaction out"); return; } - tx_scan_info.received = is_out_to_acc_precomp(spend_public_key, boost::get<txout_to_key>(o.target), derivation, i); + tx_scan_info.received = is_out_to_acc_precomp(m_subaddresses, boost::get<txout_to_key>(o.target).key, derivation, additional_derivations, i); if(tx_scan_info.received) { tx_scan_info.money_transfered = o.amount; // may be 0 for ringct outputs @@ -627,15 +749,8 @@ void wallet2::check_acc_out_precomp(const crypto::public_key &spend_public_key, tx_scan_info.error = false; } //---------------------------------------------------------------------------------------------------- -static uint64_t decodeRct(const rct::rctSig & rv, const crypto::public_key &pub, const crypto::secret_key &sec, unsigned int i, rct::key & mask) +static uint64_t decodeRct(const rct::rctSig & rv, const crypto::key_derivation &derivation, unsigned int i, rct::key & mask) { - crypto::key_derivation derivation; - bool r = crypto::generate_key_derivation(pub, sec, derivation); - if (!r) - { - LOG_ERROR("Failed to generate key derivation to decode rct output " << i); - return 0; - } crypto::secret_key scalar1; crypto::derivation_to_scalar(derivation, i, scalar1); try @@ -658,9 +773,9 @@ static uint64_t decodeRct(const rct::rctSig & rv, const crypto::public_key &pub, } } //---------------------------------------------------------------------------------------------------- -void wallet2::scan_output(const cryptonote::account_keys &keys, const cryptonote::transaction &tx, const crypto::public_key &tx_pub_key, size_t i, tx_scan_info_t &tx_scan_info, int &num_vouts_received, uint64_t &tx_money_got_in_outs, std::vector<size_t> &outs) +void wallet2::scan_output(const cryptonote::account_keys &keys, const cryptonote::transaction &tx, const crypto::public_key &tx_pub_key, size_t i, tx_scan_info_t &tx_scan_info, int &num_vouts_received, std::unordered_map<cryptonote::subaddress_index, uint64_t> &tx_money_got_in_outs, std::vector<size_t> &outs) { - bool r = cryptonote::generate_key_image_helper(keys, tx_pub_key, i, tx_scan_info.in_ephemeral, tx_scan_info.ki); + bool r = cryptonote::generate_key_image_helper_precomp(keys, boost::get<cryptonote::txout_to_key>(tx.vout[i].target).key, tx_scan_info.received->derivation, i, tx_scan_info.received->index, tx_scan_info.in_ephemeral, tx_scan_info.ki); THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "Failed to generate key image"); THROW_WALLET_EXCEPTION_IF(tx_scan_info.in_ephemeral.pub != boost::get<cryptonote::txout_to_key>(tx.vout[i].target).key, error::wallet_internal_error, "key_image generated ephemeral public key not matched with output_key"); @@ -668,9 +783,9 @@ void wallet2::scan_output(const cryptonote::account_keys &keys, const cryptonote outs.push_back(i); if (tx_scan_info.money_transfered == 0) { - tx_scan_info.money_transfered = tools::decodeRct(tx.rct_signatures, tx_pub_key, keys.m_view_secret_key, i, tx_scan_info.mask); + tx_scan_info.money_transfered = tools::decodeRct(tx.rct_signatures, tx_scan_info.received->derivation, i, tx_scan_info.mask); } - tx_money_got_in_outs += tx_scan_info.money_transfered; + tx_money_got_in_outs[tx_scan_info.received->index] = tx_scan_info.money_transfered; tx_scan_info.amount = tx_scan_info.money_transfered; ++num_vouts_received; } @@ -680,10 +795,10 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote // In this function, tx (probably) only contains the base information // (that is, the prunable stuff may or may not be included) - if (!miner_tx) + if (!miner_tx && !pool) process_unconfirmed(txid, tx, height); std::vector<size_t> outs; - uint64_t tx_money_got_in_outs = 0; + std::unordered_map<cryptonote::subaddress_index, uint64_t> tx_money_got_in_outs; // per receiving subaddress index crypto::public_key tx_pub_key = null_pkey; std::vector<tx_extra_field> tx_extra_fields; @@ -695,6 +810,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote // Don't try to extract tx public key if tx has no ouputs size_t pk_index = 0; + std::vector<tx_scan_info_t> tx_scan_info(tx.vout.size()); while (!tx.vout.empty()) { // if tx.vout is not empty, we loop through all tx pubkeys @@ -715,17 +831,26 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote bool r = true; tools::threadpool& tpool = tools::threadpool::getInstance(); tools::threadpool::waiter waiter; - std::unique_ptr<tx_scan_info_t[]> tx_scan_info{new tx_scan_info_t[tx.vout.size()]}; const cryptonote::account_keys& keys = m_account.get_keys(); crypto::key_derivation derivation; generate_key_derivation(tx_pub_key, keys.m_view_secret_key, derivation); + + // additional tx pubkeys and derivations for multi-destination transfers involving one or more subaddresses + std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(tx); + std::vector<crypto::key_derivation> additional_derivations; + for (size_t i = 0; i < additional_tx_pub_keys.size(); ++i) + { + additional_derivations.push_back({}); + generate_key_derivation(additional_tx_pub_keys[i], keys.m_view_secret_key, additional_derivations.back()); + } + if (miner_tx && m_refresh_type == RefreshNoCoinbase) { // assume coinbase isn't for us } else if (miner_tx && m_refresh_type == RefreshOptimizeCoinbase) { - check_acc_out_precomp(keys.m_account_address.m_spend_public_key, tx.vout[0], derivation, 0, tx_scan_info[0]); + check_acc_out_precomp(tx.vout[0], derivation, additional_derivations, 0, tx_scan_info[0]); if (tx_scan_info[0].error) { r = false; @@ -741,7 +866,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote // the first one was already checked for (size_t i = 1; i < tx.vout.size(); ++i) { - tpool.submit(&waiter, boost::bind(&wallet2::check_acc_out_precomp, this, std::cref(keys.m_account_address.m_spend_public_key), std::cref(tx.vout[i]), std::cref(derivation), i, + tpool.submit(&waiter, boost::bind(&wallet2::check_acc_out_precomp, this, std::cref(tx.vout[i]), std::cref(derivation), std::cref(additional_derivations), i, std::ref(tx_scan_info[i]))); } waiter.wait(); @@ -768,11 +893,10 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote for (size_t i = 0; i < tx.vout.size(); ++i) { - tpool.submit(&waiter, boost::bind(&wallet2::check_acc_out_precomp, this, std::cref(keys.m_account_address.m_spend_public_key), - std::cref(tx.vout[i]), std::cref(derivation), i, std::ref(tx_scan_info[i]))); + tpool.submit(&waiter, boost::bind(&wallet2::check_acc_out_precomp, this, std::cref(tx.vout[i]), std::cref(derivation), std::cref(additional_derivations), i, + std::ref(tx_scan_info[i]))); } waiter.wait(); - for (size_t i = 0; i < tx.vout.size(); ++i) { if (tx_scan_info[i].error) @@ -790,7 +914,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote { for (size_t i = 0; i < tx.vout.size(); ++i) { - check_acc_out_precomp(keys.m_account_address.m_spend_public_key, tx.vout[i], derivation, i, tx_scan_info[i]); + check_acc_out_precomp(tx.vout[i], derivation, additional_derivations, i, tx_scan_info[i]); if (tx_scan_info[i].error) { r = false; @@ -840,6 +964,8 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote td.m_key_image_known = !m_watch_only; td.m_amount = tx.vout[o].amount; td.m_pk_index = pk_index - 1; + td.m_subaddr_index = tx_scan_info[o].received->index; + expand_subaddresses(tx_scan_info[o].received->index); if (td.m_amount == 0) { td.m_mask = tx_scan_info[o].mask; @@ -861,7 +987,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote m_pub_keys[tx_scan_info[o].in_ephemeral.pub] = m_transfers.size()-1; LOG_PRINT_L0("Received money: " << print_money(td.amount()) << ", with tx: " << txid); if (0 != m_callback) - m_callback->on_money_received(height, txid, tx, td.m_amount); + m_callback->on_money_received(height, txid, tx, td.m_amount, td.m_subaddr_index); } } else if (m_transfers[kit->second].m_spent || m_transfers[kit->second].amount() >= tx.vout[o].amount) @@ -877,7 +1003,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote << " from received " << print_money(tx.vout[o].amount) << " output already exists with " << print_money(m_transfers[kit->second].amount()) << ", replacing with new output"); // The new larger output replaced a previous smaller one - tx_money_got_in_outs -= tx.vout[o].amount; + tx_money_got_in_outs[tx_scan_info[o].received->index] -= tx.vout[o].amount; if (!pool) { @@ -889,6 +1015,8 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote td.m_txid = txid; td.m_amount = tx.vout[o].amount; td.m_pk_index = pk_index - 1; + td.m_subaddr_index = tx_scan_info[o].received->index; + expand_subaddresses(tx_scan_info[o].received->index); if (td.m_amount == 0) { td.m_mask = tx_scan_info[o].mask; @@ -910,7 +1038,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote LOG_PRINT_L0("Received money: " << print_money(td.amount()) << ", with tx: " << txid); if (0 != m_callback) - m_callback->on_money_received(height, txid, tx, td.m_amount); + m_callback->on_money_received(height, txid, tx, td.m_amount, td.m_subaddr_index); } } } @@ -918,6 +1046,8 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote } uint64_t tx_money_spent_in_ins = 0; + boost::optional<uint32_t> subaddr_account; + std::set<uint32_t> subaddr_indices; // check all outputs for spending (compare key images) for(auto& in: tx.vin) { @@ -936,23 +1066,50 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote } amount = td.amount(); tx_money_spent_in_ins += amount; + if (subaddr_account && *subaddr_account != td.m_subaddr_index.major) + LOG_ERROR("spent funds are from different subaddress accounts; count of incoming/outgoing payments will be incorrect"); + subaddr_account = td.m_subaddr_index.major; + subaddr_indices.insert(td.m_subaddr_index.minor); if (!pool) { LOG_PRINT_L0("Spent money: " << print_money(amount) << ", with tx: " << txid); set_spent(it->second, height); if (0 != m_callback) - m_callback->on_money_spent(height, txid, tx, amount, tx); + m_callback->on_money_spent(height, txid, tx, amount, tx, td.m_subaddr_index); } } } - if (tx_money_spent_in_ins > 0) + if (tx_money_spent_in_ins > 0 && !pool) { - process_outgoing(txid, tx, height, ts, tx_money_spent_in_ins, tx_money_got_in_outs); + uint64_t self_received = std::accumulate<decltype(tx_money_got_in_outs.begin()), uint64_t>(tx_money_got_in_outs.begin(), tx_money_got_in_outs.end(), 0, + [&subaddr_account] (uint64_t acc, const std::pair<cryptonote::subaddress_index, uint64_t>& p) + { + return acc + (p.first.major == *subaddr_account ? p.second : 0); + }); + process_outgoing(txid, tx, height, ts, tx_money_spent_in_ins, self_received, *subaddr_account, subaddr_indices); + // if sending to yourself at the same subaddress account, set the outgoing payment amount to 0 so that it's less confusing + uint64_t fee = tx.version == 1 ? tx_money_spent_in_ins - get_outs_money_amount(tx) : tx.rct_signatures.txnFee; + if (tx_money_spent_in_ins == self_received + fee) + { + auto i = m_confirmed_txs.find(txid); + THROW_WALLET_EXCEPTION_IF(i == m_confirmed_txs.end(), error::wallet_internal_error, + "confirmed tx wasn't found: " + string_tools::pod_to_hex(txid)); + i->second.m_change = self_received; + } + } + + // remove change sent to the spending subaddress account from the list of received funds + for (auto i = tx_money_got_in_outs.begin(); i != tx_money_got_in_outs.end();) + { + if (subaddr_account && i->first.major == *subaddr_account) + i = tx_money_got_in_outs.erase(i); + else + ++i; } - uint64_t received = (tx_money_spent_in_ins < tx_money_got_in_outs) ? tx_money_got_in_outs - tx_money_spent_in_ins : 0; - if (0 < received) + // create payment_details for each incoming transfer to a subaddress index + if (tx_money_got_in_outs.size() > 0) { tx_extra_nonce extra_nonce; crypto::hash payment_id = null_hash; @@ -993,20 +1150,24 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote LOG_PRINT_L2("Found unencrypted payment ID: " << payment_id); } - payment_details payment; - payment.m_tx_hash = txid; - payment.m_amount = received; - payment.m_block_height = height; - payment.m_unlock_time = tx.unlock_time; - payment.m_timestamp = ts; - if (pool) { - m_unconfirmed_payments.emplace(payment_id, payment); - if (0 != m_callback) - m_callback->on_unconfirmed_money_received(height, txid, tx, payment.m_amount); + for (const auto& i : tx_money_got_in_outs) + { + payment_details payment; + payment.m_tx_hash = txid; + payment.m_amount = i.second; + payment.m_block_height = height; + payment.m_unlock_time = tx.unlock_time; + payment.m_timestamp = ts; + payment.m_subaddr_index = i.first; + if (pool) { + m_unconfirmed_payments.emplace(payment_id, payment); + if (0 != m_callback) + m_callback->on_unconfirmed_money_received(height, txid, tx, payment.m_amount, payment.m_subaddr_index); + } + else + m_payments.emplace(payment_id, payment); + LOG_PRINT_L2("Payment found in " << (pool ? "pool" : "block") << ": " << payment_id << " / " << payment.m_tx_hash << " / " << payment.m_amount); } - else - m_payments.emplace(payment_id, payment); - LOG_PRINT_L2("Payment found in " << (pool ? "pool" : "block") << ": " << payment_id << " / " << payment.m_tx_hash << " / " << payment.m_amount); } } //---------------------------------------------------------------------------------------------------- @@ -1030,7 +1191,7 @@ void wallet2::process_unconfirmed(const crypto::hash &txid, const cryptonote::tr } } //---------------------------------------------------------------------------------------------------- -void wallet2::process_outgoing(const crypto::hash &txid, const cryptonote::transaction &tx, uint64_t height, uint64_t ts, uint64_t spent, uint64_t received) +void wallet2::process_outgoing(const crypto::hash &txid, const cryptonote::transaction &tx, uint64_t height, uint64_t ts, uint64_t spent, uint64_t received, uint32_t subaddr_account, const std::set<uint32_t>& subaddr_indices) { std::pair<std::unordered_map<crypto::hash, confirmed_transfer_details>::iterator, bool> entry = m_confirmed_txs.insert(std::make_pair(txid, confirmed_transfer_details())); // fill with the info we know, some info might already be there @@ -1056,6 +1217,8 @@ void wallet2::process_outgoing(const crypto::hash &txid, const cryptonote::trans get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, entry.first->second.m_payment_id); } } + entry.first->second.m_subaddr_account = subaddr_account; + entry.first->second.m_subaddr_indices = subaddr_indices; } entry.first->second.m_block_height = height; entry.first->second.m_timestamp = ts; @@ -1478,6 +1641,18 @@ void wallet2::update_pool_state(bool refreshed) if (i.first == txid) { found = true; + // if this is a payment to yourself at a different subaddress account, don't skip it + // so that you can see the incoming pool tx with 'show_transfers' on that receiving subaddress account + const unconfirmed_transfer_details& utd = i.second; + for (const auto& dst : utd.m_dests) + { + auto subaddr_index = m_subaddresses.find(dst.addr.m_spend_public_key); + if (subaddr_index != m_subaddresses.end() && subaddr_index->second.major != utd.m_subaddr_account) + { + found = false; + break; + } + } break; } } @@ -1626,12 +1801,13 @@ void wallet2::fast_refresh(uint64_t stop_height, uint64_t &blocks_start_height, } -bool wallet2::add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description) +bool wallet2::add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description, bool is_subaddress) { wallet2::address_book_row a; a.m_address = address; a.m_payment_id = payment_id; a.m_description = description; + a.m_is_subaddress = is_subaddress; auto old_size = m_address_book.size(); m_address_book.push_back(a); @@ -1750,7 +1926,7 @@ void wallet2::refresh(uint64_t start_height, uint64_t & blocks_fetched, bool& re LOG_PRINT_L1("Failed to check pending transactions"); } - LOG_PRINT_L1("Refresh done, blocks received: " << blocks_fetched << ", balance: " << print_money(balance()) << ", unlocked: " << print_money(unlocked_balance())); + LOG_PRINT_L1("Refresh done, blocks received: " << blocks_fetched << ", balance (all accounts): " << print_money(balance_all()) << ", unlocked: " << print_money(unlocked_balance_all())); } //---------------------------------------------------------------------------------------------------- bool wallet2::refresh(uint64_t & blocks_fetched, bool& received_money, bool& ok) @@ -1845,12 +2021,16 @@ bool wallet2::clear() m_unconfirmed_txs.clear(); m_payments.clear(); m_tx_keys.clear(); + m_additional_tx_keys.clear(); m_confirmed_txs.clear(); m_unconfirmed_payments.clear(); m_scanned_pool_txs[0].clear(); m_scanned_pool_txs[1].clear(); m_address_book.clear(); m_local_bc_height = 1; + m_subaddresses.clear(); + m_subaddresses_inv.clear(); + m_subaddress_labels.clear(); return true; } @@ -2240,6 +2420,7 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri cryptonote::block b; generate_genesis(b); m_blockchain.push_back(get_block_hash(b)); + add_subaddress_account(tr("Primary account")); store(); return retval; @@ -2275,6 +2456,7 @@ void wallet2::generate(const std::string& wallet_, const std::string& password, cryptonote::block b; generate_genesis(b); m_blockchain.push_back(get_block_hash(b)); + add_subaddress_account(tr("Primary account")); store(); } @@ -2310,6 +2492,7 @@ void wallet2::generate(const std::string& wallet_, const std::string& password, cryptonote::block b; generate_genesis(b); m_blockchain.push_back(get_block_hash(b)); + add_subaddress_account(tr("Primary account")); store(); } @@ -2560,6 +2743,9 @@ void wallet2::load(const std::string& wallet_, const std::string& password) trim_hashchain(); + if (get_num_subaddress_accounts() == 0) + add_subaddress_account(tr("Primary account")); + m_local_bc_height = m_blockchain.size(); } //---------------------------------------------------------------------------------------------------- @@ -2707,52 +2893,109 @@ void wallet2::store_to(const std::string &path, const std::string &password) } } //---------------------------------------------------------------------------------------------------- -uint64_t wallet2::unlocked_balance() const +uint64_t wallet2::balance(uint32_t index_major) const { uint64_t amount = 0; - for(const transfer_details& td: m_transfers) - if(!td.m_spent && is_transfer_unlocked(td)) - amount += td.amount(); - + for (const auto& i : balance_per_subaddress(index_major)) + amount += i.second; return amount; } //---------------------------------------------------------------------------------------------------- -uint64_t wallet2::balance() const +uint64_t wallet2::unlocked_balance(uint32_t index_major) const { uint64_t amount = 0; - for(auto& td: m_transfers) - if(!td.m_spent) - amount += td.amount(); - - - for(auto& utx: m_unconfirmed_txs) - if (utx.second.m_state != wallet2::unconfirmed_transfer_details::failed) - amount+= utx.second.m_change; - + for (const auto& i : unlocked_balance_per_subaddress(index_major)) + amount += i.second; return amount; } //---------------------------------------------------------------------------------------------------- +std::map<uint32_t, uint64_t> wallet2::balance_per_subaddress(uint32_t index_major) const +{ + std::map<uint32_t, uint64_t> amount_per_subaddr; + for (const auto& td: m_transfers) + { + if (td.m_subaddr_index.major == index_major && !td.m_spent) + { + auto found = amount_per_subaddr.find(td.m_subaddr_index.minor); + if (found == amount_per_subaddr.end()) + amount_per_subaddr[td.m_subaddr_index.minor] = td.amount(); + else + found->second += td.amount(); + } + } + for (const auto& utx: m_unconfirmed_txs) + { + if (utx.second.m_subaddr_account == index_major && utx.second.m_state != wallet2::unconfirmed_transfer_details::failed) + { + // all changes go to 0-th subaddress (in the current subaddress account) + auto found = amount_per_subaddr.find(0); + if (found == amount_per_subaddr.end()) + amount_per_subaddr[0] = utx.second.m_change; + else + found->second += utx.second.m_change; + } + } + return amount_per_subaddr; +} +//---------------------------------------------------------------------------------------------------- +std::map<uint32_t, uint64_t> wallet2::unlocked_balance_per_subaddress(uint32_t index_major) const +{ + std::map<uint32_t, uint64_t> amount_per_subaddr; + for(const transfer_details& td: m_transfers) + { + if(td.m_subaddr_index.major == index_major && !td.m_spent && is_transfer_unlocked(td)) + { + auto found = amount_per_subaddr.find(td.m_subaddr_index.minor); + if (found == amount_per_subaddr.end()) + amount_per_subaddr[td.m_subaddr_index.minor] = td.amount(); + else + found->second += td.amount(); + } + } + return amount_per_subaddr; +} +//---------------------------------------------------------------------------------------------------- +uint64_t wallet2::balance_all() const +{ + uint64_t r = 0; + for (uint32_t index_major = 0; index_major < get_num_subaddress_accounts(); ++index_major) + r += balance(index_major); + return r; +} +//---------------------------------------------------------------------------------------------------- +uint64_t wallet2::unlocked_balance_all() const +{ + uint64_t r = 0; + for (uint32_t index_major = 0; index_major < get_num_subaddress_accounts(); ++index_major) + r += unlocked_balance(index_major); + return r; +} +//---------------------------------------------------------------------------------------------------- void wallet2::get_transfers(wallet2::transfer_container& incoming_transfers) const { incoming_transfers = m_transfers; } //---------------------------------------------------------------------------------------------------- -void wallet2::get_payments(const crypto::hash& payment_id, std::list<wallet2::payment_details>& payments, uint64_t min_height) const +void wallet2::get_payments(const crypto::hash& payment_id, std::list<wallet2::payment_details>& payments, uint64_t min_height, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { auto range = m_payments.equal_range(payment_id); - std::for_each(range.first, range.second, [&payments, &min_height](const payment_container::value_type& x) { - if (min_height < x.second.m_block_height) + std::for_each(range.first, range.second, [&payments, &min_height, &subaddr_account, &subaddr_indices](const payment_container::value_type& x) { + if (min_height < x.second.m_block_height && + (!subaddr_account || *subaddr_account == x.second.m_subaddr_index.major) && + (subaddr_indices.empty() || subaddr_indices.count(x.second.m_subaddr_index.minor) == 1)) { payments.push_back(x.second); } }); } //---------------------------------------------------------------------------------------------------- -void wallet2::get_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& payments, uint64_t min_height, uint64_t max_height) const +void wallet2::get_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& payments, uint64_t min_height, uint64_t max_height, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { auto range = std::make_pair(m_payments.begin(), m_payments.end()); - std::for_each(range.first, range.second, [&payments, &min_height, &max_height](const payment_container::value_type& x) { - if (min_height < x.second.m_block_height && max_height >= x.second.m_block_height) + std::for_each(range.first, range.second, [&payments, &min_height, &max_height, &subaddr_account, &subaddr_indices](const payment_container::value_type& x) { + if (min_height < x.second.m_block_height && max_height >= x.second.m_block_height && + (!subaddr_account || *subaddr_account == x.second.m_subaddr_index.major) && + (subaddr_indices.empty() || subaddr_indices.count(x.second.m_subaddr_index.minor) == 1)) { payments.push_back(x); } @@ -2760,25 +3003,35 @@ void wallet2::get_payments(std::list<std::pair<crypto::hash,wallet2::payment_det } //---------------------------------------------------------------------------------------------------- void wallet2::get_payments_out(std::list<std::pair<crypto::hash,wallet2::confirmed_transfer_details>>& confirmed_payments, - uint64_t min_height, uint64_t max_height) const + uint64_t min_height, uint64_t max_height, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { for (auto i = m_confirmed_txs.begin(); i != m_confirmed_txs.end(); ++i) { - if (i->second.m_block_height > min_height && i->second.m_block_height <= max_height) { - confirmed_payments.push_back(*i); - } + if (i->second.m_block_height <= min_height || i->second.m_block_height > max_height) + continue; + if (subaddr_account && *subaddr_account != i->second.m_subaddr_account) + continue; + if (!subaddr_indices.empty() && std::count_if(i->second.m_subaddr_indices.begin(), i->second.m_subaddr_indices.end(), [&subaddr_indices](uint32_t index) { return subaddr_indices.count(index) == 1; }) == 0) + continue; + confirmed_payments.push_back(*i); } } //---------------------------------------------------------------------------------------------------- -void wallet2::get_unconfirmed_payments_out(std::list<std::pair<crypto::hash,wallet2::unconfirmed_transfer_details>>& unconfirmed_payments) const +void wallet2::get_unconfirmed_payments_out(std::list<std::pair<crypto::hash,wallet2::unconfirmed_transfer_details>>& unconfirmed_payments, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { for (auto i = m_unconfirmed_txs.begin(); i != m_unconfirmed_txs.end(); ++i) { + if (subaddr_account && *subaddr_account != i->second.m_subaddr_account) + continue; + if (!subaddr_indices.empty() && std::count_if(i->second.m_subaddr_indices.begin(), i->second.m_subaddr_indices.end(), [&subaddr_indices](uint32_t index) { return subaddr_indices.count(index) == 1; }) == 0) + continue; unconfirmed_payments.push_back(*i); } } //---------------------------------------------------------------------------------------------------- -void wallet2::get_unconfirmed_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& unconfirmed_payments) const +void wallet2::get_unconfirmed_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& unconfirmed_payments, const boost::optional<uint32_t>& subaddr_account, const std::set<uint32_t>& subaddr_indices) const { for (auto i = m_unconfirmed_payments.begin(); i != m_unconfirmed_payments.end(); ++i) { + if ((!subaddr_account || *subaddr_account == i->second.m_subaddr_index.major) && + (subaddr_indices.empty() || subaddr_indices.count(i->second.m_subaddr_index.minor) == 1)) unconfirmed_payments.push_back(*i); } } @@ -2843,6 +3096,7 @@ void wallet2::rescan_blockchain(bool refresh) generate_genesis(genesis); crypto::hash genesis_hash = get_block_hash(genesis); m_blockchain.push_back(genesis_hash); + add_subaddress_account(tr("Primary account")); m_local_bc_height = 1; if (refresh) @@ -3040,7 +3294,7 @@ uint64_t wallet2::select_transfers(uint64_t needed_money, std::vector<size_t> un return found_money; } //---------------------------------------------------------------------------------------------------- -void wallet2::add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amount_in, const std::vector<cryptonote::tx_destination_entry> &dests, const crypto::hash &payment_id, uint64_t change_amount) +void wallet2::add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amount_in, const std::vector<cryptonote::tx_destination_entry> &dests, const crypto::hash &payment_id, uint64_t change_amount, uint32_t subaddr_account, const std::set<uint32_t>& subaddr_indices) { unconfirmed_transfer_details& utd = m_unconfirmed_txs[cryptonote::get_transaction_hash(tx)]; utd.m_amount_in = amount_in; @@ -3055,6 +3309,8 @@ void wallet2::add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amo utd.m_payment_id = payment_id; utd.m_state = wallet2::unconfirmed_transfer_details::pending; utd.m_timestamp = time(NULL); + utd.m_subaddr_account = subaddr_account; + utd.m_subaddr_indices = subaddr_indices; } //---------------------------------------------------------------------------------------------------- @@ -3199,10 +3455,11 @@ void wallet2::commit_tx(pending_tx& ptx) for(size_t idx: ptx.selected_transfers) amount_in += m_transfers[idx].amount(); } - add_unconfirmed_tx(ptx.tx, amount_in, dests, payment_id, ptx.change_dts.amount); + add_unconfirmed_tx(ptx.tx, amount_in, dests, payment_id, ptx.change_dts.amount, ptx.construction_data.subaddr_account, ptx.construction_data.subaddr_indices); if (store_tx_info()) { m_tx_keys.insert(std::make_pair(txid, ptx.tx_key)); + m_additional_tx_keys.insert(std::make_pair(txid, ptx.additional_tx_keys)); } LOG_PRINT_L2("transaction " << txid << " generated ok and sent to daemon, key_images: [" << ptx.key_images << "]"); @@ -3215,8 +3472,8 @@ void wallet2::commit_tx(pending_tx& ptx) //fee includes dust if dust policy specified it. LOG_PRINT_L1("Transaction successfully sent. <" << txid << ">" << ENDL << "Commission: " << print_money(ptx.fee) << " (dust sent to dust addr: " << print_money((ptx.dust_added_to_fee ? 0 : ptx.dust)) << ")" << ENDL - << "Balance: " << print_money(balance()) << ENDL - << "Unlocked: " << print_money(unlocked_balance()) << ENDL + << "Balance: " << print_money(balance(ptx.construction_data.subaddr_account)) << ENDL + << "Unlocked: " << print_money(unlocked_balance(ptx.construction_data.subaddr_account)) << ENDL << "Please, wait for confirmation for your balance to be unlocked."); } @@ -3339,7 +3596,8 @@ bool wallet2::sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_f signed_txes.ptx.push_back(pending_tx()); tools::wallet2::pending_tx &ptx = signed_txes.ptx.back(); crypto::secret_key tx_key; - bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), sd.sources, sd.splitted_dsts, sd.extra, ptx.tx, sd.unlock_time, tx_key, sd.use_rct); + std::vector<crypto::secret_key> additional_tx_keys; + bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), m_subaddresses, sd.sources, sd.splitted_dsts, sd.change_dts.addr, sd.extra, ptx.tx, sd.unlock_time, tx_key, additional_tx_keys, sd.use_rct); THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sd.sources, sd.splitted_dsts, sd.unlock_time, m_testnet); // we don't test tx size, because we don't know the current limit, due to not having a blockchain, // and it's a bit pointless to fail there anyway, since it'd be a (good) guess only. We sign anyway, @@ -3353,6 +3611,7 @@ bool wallet2::sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_f { const crypto::hash txid = get_transaction_hash(ptx.tx); m_tx_keys.insert(std::make_pair(txid, tx_key)); + m_additional_tx_keys.insert(std::make_pair(txid, additional_tx_keys)); } std::string key_images; @@ -3539,7 +3798,7 @@ int wallet2::get_fee_algorithm() // // this function will make multiple calls to wallet2::transfer if multiple // transactions will be required -std::vector<wallet2::pending_tx> wallet2::create_transactions(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon) +std::vector<wallet2::pending_tx> wallet2::create_transactions(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, bool trusted_daemon) { const std::vector<size_t> unused_transfers_indices = select_available_outputs_from_histogram(fake_outs_count + 1, true, true, true, trusted_daemon); @@ -3879,7 +4138,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>> } template<typename T> -void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_entry>& dsts, const std::list<size_t> selected_transfers, size_t fake_outputs_count, +void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_entry>& dsts, const std::list<size_t>& selected_transfers, size_t fake_outputs_count, std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction& tx, pending_tx &ptx) { @@ -3910,6 +4169,10 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent LOG_PRINT_L2("wanted " << print_money(needed_money) << ", found " << print_money(found_money) << ", fee " << print_money(fee)); THROW_WALLET_EXCEPTION_IF(found_money < needed_money, error::not_enough_money, found_money, needed_money - fee, fee); + uint32_t subaddr_account = m_transfers[*selected_transfers.begin()].m_subaddr_index.major; + for (auto i = ++selected_transfers.begin(); i != selected_transfers.end(); ++i) + THROW_WALLET_EXCEPTION_IF(subaddr_account != m_transfers[*i].m_subaddr_index.major, error::wallet_internal_error, "the tx uses funds from multiple accounts"); + if (outs.empty()) get_outs(outs, selected_transfers, fake_outputs_count); // may throw @@ -3952,6 +4215,7 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent real_oe.second.mask = rct::commit(td.amount(), td.m_mask); *it_to_replace = real_oe; src.real_out_tx_key = get_tx_pub_key_from_extra(td.m_tx, td.m_pk_index); + src.real_out_additional_tx_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); src.real_output = it_to_replace - src.outputs.begin(); src.real_output_in_tx_index = td.m_internal_output_index; detail::print_source_entry(src); @@ -3962,7 +4226,7 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent cryptonote::tx_destination_entry change_dts = AUTO_VAL_INIT(change_dts); if (needed_money < found_money) { - change_dts.addr = m_account.get_keys().m_account_address; + change_dts.addr = get_subaddress({subaddr_account, 0}); change_dts.amount = found_money - needed_money; } @@ -3975,13 +4239,14 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent } for(auto& d: dust_dsts) { if (!dust_policy.add_to_fee) - splitted_dsts.push_back(cryptonote::tx_destination_entry(d.amount, dust_policy.addr_for_dust)); + splitted_dsts.push_back(cryptonote::tx_destination_entry(d.amount, dust_policy.addr_for_dust, d.is_subaddress)); dust += d.amount; } crypto::secret_key tx_key; + std::vector<crypto::secret_key> additional_tx_keys; LOG_PRINT_L2("constructing tx"); - bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time, tx_key); + bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), m_subaddresses, sources, splitted_dsts, change_dts.addr, extra, tx, unlock_time, tx_key, additional_tx_keys); LOG_PRINT_L2("constructed tx, r="<<r); THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time, m_testnet); THROW_WALLET_EXCEPTION_IF(upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, upper_transaction_size_limit); @@ -4009,6 +4274,7 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent ptx.change_dts = change_dts; ptx.selected_transfers = selected_transfers; ptx.tx_key = tx_key; + ptx.additional_tx_keys = additional_tx_keys; ptx.dests = dsts; ptx.construction_data.sources = sources; ptx.construction_data.change_dts = change_dts; @@ -4018,10 +4284,15 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent ptx.construction_data.unlock_time = unlock_time; ptx.construction_data.use_rct = false; ptx.construction_data.dests = dsts; + // record which subaddress indices are being used as inputs + ptx.construction_data.subaddr_account = subaddr_account; + ptx.construction_data.subaddr_indices.clear(); + for (size_t idx: selected_transfers) + ptx.construction_data.subaddr_indices.insert(m_transfers[idx].m_subaddr_index.minor); LOG_PRINT_L2("transfer_selected done"); } -void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry> dsts, const std::list<size_t> selected_transfers, size_t fake_outputs_count, +void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry> dsts, const std::list<size_t>& selected_transfers, size_t fake_outputs_count, std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, cryptonote::transaction& tx, pending_tx &ptx) { @@ -4055,6 +4326,10 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry LOG_PRINT_L2("wanted " << print_money(needed_money) << ", found " << print_money(found_money) << ", fee " << print_money(fee)); THROW_WALLET_EXCEPTION_IF(found_money < needed_money, error::not_enough_money, found_money, needed_money - fee, fee); + uint32_t subaddr_account = m_transfers[*selected_transfers.begin()].m_subaddr_index.major; + for (auto i = ++selected_transfers.begin(); i != selected_transfers.end(); ++i) + THROW_WALLET_EXCEPTION_IF(subaddr_account != m_transfers[*i].m_subaddr_index.major, error::wallet_internal_error, "the tx uses funds from multiple accounts"); + if (outs.empty()) get_outs(outs, selected_transfers, fake_outputs_count); // may throw @@ -4096,6 +4371,7 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry real_oe.second.mask = rct::commit(td.amount(), td.m_mask); *it_to_replace = real_oe; src.real_out_tx_key = get_tx_pub_key_from_extra(td.m_tx, td.m_pk_index); + src.real_out_additional_tx_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); src.real_output = it_to_replace - src.outputs.begin(); src.real_output_in_tx_index = td.m_internal_output_index; src.mask = td.m_mask; @@ -4122,13 +4398,14 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry } else { - change_dts.addr = m_account.get_keys().m_account_address; + change_dts.addr = get_subaddress({subaddr_account, 0}); } splitted_dsts.push_back(change_dts); crypto::secret_key tx_key; + std::vector<crypto::secret_key> additional_tx_keys; LOG_PRINT_L2("constructing tx"); - bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time, tx_key, true); + bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), m_subaddresses, sources, splitted_dsts, change_dts.addr, extra, tx, unlock_time, tx_key, additional_tx_keys, true); LOG_PRINT_L2("constructed tx, r="<<r); THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, dsts, unlock_time, m_testnet); THROW_WALLET_EXCEPTION_IF(upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, upper_transaction_size_limit); @@ -4152,6 +4429,7 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry ptx.change_dts = change_dts; ptx.selected_transfers = selected_transfers; ptx.tx_key = tx_key; + ptx.additional_tx_keys = additional_tx_keys; ptx.dests = dsts; ptx.construction_data.sources = sources; ptx.construction_data.change_dts = change_dts; @@ -4161,6 +4439,11 @@ void wallet2::transfer_selected_rct(std::vector<cryptonote::tx_destination_entry ptx.construction_data.unlock_time = unlock_time; ptx.construction_data.use_rct = true; ptx.construction_data.dests = dsts; + // record which subaddress indices are being used as inputs + ptx.construction_data.subaddr_account = subaddr_account; + ptx.construction_data.subaddr_indices.clear(); + for (size_t idx: selected_transfers) + ptx.construction_data.subaddr_indices.insert(m_transfers[idx].m_subaddr_index.minor); LOG_PRINT_L2("transfer_selected_rct done"); } @@ -4217,7 +4500,7 @@ static size_t estimate_tx_size(bool use_rct, int n_inputs, int mixin, int n_outp return n_inputs * (mixin+1) * APPROXIMATE_INPUT_BYTES; } -std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money) const +std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money, uint32_t subaddr_account, const std::set<uint32_t> &subaddr_indices) const { std::vector<size_t> picks; float current_output_relatdness = 1.0f; @@ -4228,7 +4511,7 @@ std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money) co for (size_t i = 0; i < m_transfers.size(); ++i) { const transfer_details& td = m_transfers[i]; - if (!td.m_spent && td.is_rct() && td.amount() >= needed_money && is_transfer_unlocked(td)) + if (!td.m_spent && td.is_rct() && td.amount() >= needed_money && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1) { LOG_PRINT_L2("We can use " << i << " alone: " << print_money(td.amount())); picks.push_back(i); @@ -4243,13 +4526,13 @@ std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money) co for (size_t i = 0; i < m_transfers.size(); ++i) { const transfer_details& td = m_transfers[i]; - if (!td.m_spent && td.is_rct() && is_transfer_unlocked(td)) + if (!td.m_spent && td.is_rct() && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1) { LOG_PRINT_L2("Considering input " << i << ", " << print_money(td.amount())); for (size_t j = i + 1; j < m_transfers.size(); ++j) { const transfer_details& td2 = m_transfers[j]; - if (!td2.m_spent && td2.is_rct() && td.amount() + td2.amount() >= needed_money && is_transfer_unlocked(td2)) + if (!td2.m_spent && td2.is_rct() && td.amount() + td2.amount() >= needed_money && is_transfer_unlocked(td2) && td2.m_subaddr_index == td.m_subaddr_index) { // update our picks if those outputs are less related than any we // already found. If the same, don't update, and oldest suitable outputs @@ -4346,10 +4629,10 @@ static uint32_t get_count_above(const std::vector<wallet2::transfer_details> &tr // This system allows for sending (almost) the entire balance, since it does // not generate spurious change in all txes, thus decreasing the instantaneous // usable balance. -std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon) +std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, bool trusted_daemon) { - std::vector<size_t> unused_transfers_indices; - std::vector<size_t> unused_dust_indices; + std::vector<std::pair<uint32_t, std::vector<size_t>>> unused_transfers_indices_per_subaddr; + std::vector<std::pair<uint32_t, std::vector<size_t>>> unused_dust_indices_per_subaddr; uint64_t needed_money; uint64_t accumulated_fee, accumulated_outputs, accumulated_change; struct TX { @@ -4359,14 +4642,14 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp pending_tx ptx; size_t bytes; - void add(const account_public_address &addr, uint64_t amount, unsigned int original_output_index, bool merge_destinations) { + void add(const account_public_address &addr, bool is_subaddress, uint64_t amount, unsigned int original_output_index, bool merge_destinations) { if (merge_destinations) { std::vector<cryptonote::tx_destination_entry>::iterator i; i = std::find_if(dsts.begin(), dsts.end(), [&](const cryptonote::tx_destination_entry &d) { return !memcmp (&d.addr, &addr, sizeof(addr)); }); if (i == dsts.end()) { - dsts.push_back(tx_destination_entry(0,addr)); + dsts.push_back(tx_destination_entry(0,addr,is_subaddress)); i = dsts.end() - 1; } i->amount += amount; @@ -4376,7 +4659,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp THROW_WALLET_EXCEPTION_IF(original_output_index > dsts.size(), error::wallet_internal_error, std::string("original_output_index too large: ") + std::to_string(original_output_index) + " > " + std::to_string(dsts.size())); if (original_output_index == dsts.size()) - dsts.push_back(tx_destination_entry(0,addr)); + dsts.push_back(tx_destination_entry(0,addr,is_subaddress)); THROW_WALLET_EXCEPTION_IF(memcmp(&dsts[original_output_index].addr, &addr, sizeof(addr)), error::wallet_internal_error, "Mismatched destination address"); dsts[original_output_index].amount += amount; } @@ -4408,29 +4691,90 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp // throw if attempting a transaction with no money THROW_WALLET_EXCEPTION_IF(needed_money == 0, error::zero_destination); - // gather all our dust and non dust outputs + std::map<uint32_t, uint64_t> balance_per_subaddr = unlocked_balance_per_subaddress(subaddr_account); + + if (subaddr_indices.empty()) // "index=<N1>[,<N2>,...]" wasn't specified -> use all the indices with non-zero unlocked bakance + { + for (const auto& i : balance_per_subaddr) + subaddr_indices.insert(i.first); + } + + // early out if we know we can't make it anyway + // we could also check for being within FEE_PER_KB, but if the fee calculation + // ever changes, this might be missed, so let this go through + uint64_t balance_subtotal = 0; + for (uint32_t index_minor : subaddr_indices) + balance_subtotal += balance_per_subaddr[index_minor]; + THROW_WALLET_EXCEPTION_IF(needed_money > balance_subtotal, error::not_enough_money, + balance_subtotal, needed_money, 0); + + for (uint32_t i : subaddr_indices) + LOG_PRINT_L2("Candidate subaddress index for spending: " << i); + + // gather all dust and non-dust outputs belonging to specified subaddresses + size_t num_nondust_outputs = 0; + size_t num_dust_outputs = 0; for (size_t i = 0; i < m_transfers.size(); ++i) { const transfer_details& td = m_transfers[i]; - if (!td.m_spent && (use_rct ? true : !td.is_rct()) && is_transfer_unlocked(td)) + if (!td.m_spent && (use_rct ? true : !td.is_rct()) && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1) { + const uint32_t index_minor = td.m_subaddr_index.minor; + auto find_predicate = [&index_minor](const std::pair<uint32_t, std::vector<size_t>>& x) { return x.first == index_minor; }; if ((td.is_rct()) || is_valid_decomposed_amount(td.amount())) - unused_transfers_indices.push_back(i); + { + auto found = std::find_if(unused_transfers_indices_per_subaddr.begin(), unused_transfers_indices_per_subaddr.end(), find_predicate); + if (found == unused_transfers_indices_per_subaddr.end()) + { + unused_transfers_indices_per_subaddr.push_back({index_minor, {i}}); + } + else + { + found->second.push_back(i); + } + ++num_nondust_outputs; + } else - unused_dust_indices.push_back(i); + { + auto found = std::find_if(unused_dust_indices_per_subaddr.begin(), unused_dust_indices_per_subaddr.end(), find_predicate); + if (found == unused_dust_indices_per_subaddr.end()) + { + unused_dust_indices_per_subaddr.push_back({index_minor, {i}}); + } + else + { + found->second.push_back(i); + } + ++num_dust_outputs; + } } } - LOG_PRINT_L2("Starting with " << unused_transfers_indices.size() << " non-dust outputs and " << unused_dust_indices.size() << " dust outputs"); - // early out if we know we can't make it anyway - // we could also check for being within FEE_PER_KB, but if the fee calculation - // ever changes, this might be missed, so let this go through - THROW_WALLET_EXCEPTION_IF(needed_money > unlocked_balance(), error::not_enough_money, - unlocked_balance(), needed_money, 0); + // shuffle & sort output indices + { + std::random_device rd; + std::mt19937 g(rd()); + std::shuffle(unused_transfers_indices_per_subaddr.begin(), unused_transfers_indices_per_subaddr.end(), g); + std::shuffle(unused_dust_indices_per_subaddr.begin(), unused_dust_indices_per_subaddr.end(), g); + auto sort_predicate = [&balance_per_subaddr] (const std::pair<uint32_t, std::vector<size_t>>& x, const std::pair<uint32_t, std::vector<size_t>>& y) + { + return balance_per_subaddr[x.first] > balance_per_subaddr[y.first]; + }; + std::sort(unused_transfers_indices_per_subaddr.begin(), unused_transfers_indices_per_subaddr.end(), sort_predicate); + std::sort(unused_dust_indices_per_subaddr.begin(), unused_dust_indices_per_subaddr.end(), sort_predicate); + } - if (unused_dust_indices.empty() && unused_transfers_indices.empty()) + LOG_PRINT_L2("Starting with " << num_nondust_outputs << " non-dust outputs and " << num_dust_outputs << " dust outputs"); + + if (unused_dust_indices_per_subaddr.empty() && unused_transfers_indices_per_subaddr.empty()) return std::vector<wallet2::pending_tx>(); + // if empty, put dummy entry so that the front can be referenced later in the loop + if (unused_dust_indices_per_subaddr.empty()) + unused_dust_indices_per_subaddr.push_back({}); + if (unused_transfers_indices_per_subaddr.empty()) + unused_transfers_indices_per_subaddr.push_back({}); + // start with an empty tx txes.push_back(TX()); accumulated_fee = 0; @@ -4454,12 +4798,31 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp // this is used to build a tx that's 1 or 2 inputs, and 2 outputs, which // will get us a known fee. uint64_t estimated_fee = calculate_fee(fee_per_kb, estimate_rct_tx_size(2, fake_outs_count + 1, 2), fee_multiplier); - preferred_inputs = pick_preferred_rct_inputs(needed_money + estimated_fee); + preferred_inputs = pick_preferred_rct_inputs(needed_money + estimated_fee, subaddr_account, subaddr_indices); if (!preferred_inputs.empty()) { string s; for (auto i: preferred_inputs) s += boost::lexical_cast<std::string>(i) + "(" + print_money(m_transfers[i].amount()) + ") "; - LOG_PRINT_L1("Found preferred rct inputs for rct tx: " << s); + LOG_PRINT_L1("Found prefered rct inputs for rct tx: " << s); + + // bring the list of available outputs stored by the same subaddress index to the front of the list + uint32_t index_minor = m_transfers[preferred_inputs[0]].m_subaddr_index.minor; + for (size_t i = 1; i < unused_transfers_indices_per_subaddr.size(); ++i) + { + if (unused_transfers_indices_per_subaddr[i].first == index_minor) + { + std::swap(unused_transfers_indices_per_subaddr[0], unused_transfers_indices_per_subaddr[i]); + break; + } + } + for (size_t i = 1; i < unused_dust_indices_per_subaddr.size(); ++i) + { + if (unused_dust_indices_per_subaddr[i].first == index_minor) + { + std::swap(unused_dust_indices_per_subaddr[0], unused_dust_indices_per_subaddr[i]); + break; + } + } } } LOG_PRINT_L2("done checking preferred"); @@ -4469,19 +4832,22 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp // - or we need to gather more fee // - or we have just one input in that tx, which is rct (to try and make all/most rct txes 2/2) unsigned int original_output_index = 0; - while ((!dsts.empty() && dsts[0].amount > 0) || adding_fee || should_pick_a_second_output(use_rct, txes.back().selected_transfers.size(), unused_transfers_indices, unused_dust_indices)) { + std::vector<size_t>* unused_transfers_indices = &unused_transfers_indices_per_subaddr[0].second; + std::vector<size_t>* unused_dust_indices = &unused_dust_indices_per_subaddr[0].second; + while ((!dsts.empty() && dsts[0].amount > 0) || adding_fee || should_pick_a_second_output(use_rct, txes.back().selected_transfers.size(), *unused_transfers_indices, *unused_dust_indices)) { TX &tx = txes.back(); - LOG_PRINT_L2("Start of loop with " << unused_transfers_indices.size() << " " << unused_dust_indices.size()); - LOG_PRINT_L2("unused_transfers_indices: " << strjoin(unused_transfers_indices, " ")); - LOG_PRINT_L2("unused_dust_indices:" << strjoin(unused_dust_indices, " ")); + LOG_PRINT_L2("Start of loop with " << unused_transfers_indices->size() << " " << unused_dust_indices->size()); + LOG_PRINT_L2("Start of loop with " << unused_transfers_indices->size() << " " << unused_dust_indices->size()); + LOG_PRINT_L2("unused_transfers_indices: " << strjoin(*unused_transfers_indices, " ")); + LOG_PRINT_L2("unused_dust_indices:" << strjoin(*unused_dust_indices, " ")); LOG_PRINT_L2("dsts size " << dsts.size() << ", first " << (dsts.empty() ? -1 : dsts[0].amount)); LOG_PRINT_L2("adding_fee " << adding_fee << ", use_rct " << use_rct); // if we need to spend money and don't have any left, we fail - if (unused_dust_indices.empty() && unused_transfers_indices.empty()) { + if (unused_dust_indices->empty() && unused_transfers_indices->empty()) { LOG_PRINT_L2("No more outputs to choose from"); - THROW_WALLET_EXCEPTION_IF(1, error::tx_not_possible, unlocked_balance(), needed_money, accumulated_fee + needed_fee); + THROW_WALLET_EXCEPTION_IF(1, error::tx_not_possible, unlocked_balance(subaddr_account), needed_money, accumulated_fee + needed_fee); } // get a random unspent output and use it to pay part (or all) of the current destination (and maybe next one, etc) @@ -4489,12 +4855,12 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp size_t idx; if ((dsts.empty() || dsts[0].amount == 0) && !adding_fee) { // the "make rct txes 2/2" case - we pick a small value output to "clean up" the wallet too - std::vector<size_t> indices = get_only_rct(unused_dust_indices, unused_transfers_indices); + std::vector<size_t> indices = get_only_rct(*unused_dust_indices, *unused_transfers_indices); idx = pop_best_value(indices, tx.selected_transfers, true); // we might not want to add it if it's a large output and we don't have many left if (m_transfers[idx].amount() >= m_min_output_value) { - if (get_count_above(m_transfers, unused_transfers_indices, m_min_output_value) < m_min_output_count) { + if (get_count_above(m_transfers, *unused_transfers_indices, m_min_output_value) < m_min_output_count) { LOG_PRINT_L2("Second output was not strictly needed, and we're running out of outputs above " << print_money(m_min_output_value) << ", not adding"); break; } @@ -4508,14 +4874,14 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp LOG_PRINT_L2("Second output was not strictly needed, and relatedness " << relatedness << ", not adding"); break; } - pop_if_present(unused_transfers_indices, idx); - pop_if_present(unused_dust_indices, idx); + pop_if_present(*unused_transfers_indices, idx); + pop_if_present(*unused_dust_indices, idx); } else if (!preferred_inputs.empty()) { idx = pop_back(preferred_inputs); - pop_if_present(unused_transfers_indices, idx); - pop_if_present(unused_dust_indices, idx); + pop_if_present(*unused_transfers_indices, idx); + pop_if_present(*unused_dust_indices, idx); } else - idx = pop_best_value(unused_transfers_indices.empty() ? unused_dust_indices : unused_transfers_indices, tx.selected_transfers); + idx = pop_best_value(unused_transfers_indices->empty() ? *unused_dust_indices : *unused_transfers_indices, tx.selected_transfers); const transfer_details &td = m_transfers[idx]; LOG_PRINT_L2("Picking output " << idx << ", amount " << print_money(td.amount()) << ", ki " << td.m_key_image); @@ -4538,9 +4904,9 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp while (!dsts.empty() && dsts[0].amount <= available_amount && estimate_tx_size(use_rct, tx.selected_transfers.size(), fake_outs_count, tx.dsts.size()) < TX_SIZE_TARGET(upper_transaction_size_limit)) { // we can fully pay that destination - LOG_PRINT_L2("We can fully pay " << get_account_address_as_str(m_testnet, dsts[0].addr) << + LOG_PRINT_L2("We can fully pay " << get_account_address_as_str(m_testnet, dsts[0].is_subaddress, dsts[0].addr) << " for " << print_money(dsts[0].amount)); - tx.add(dsts[0].addr, dsts[0].amount, original_output_index, m_merge_destinations); + tx.add(dsts[0].addr, dsts[0].is_subaddress, dsts[0].amount, original_output_index, m_merge_destinations); available_amount -= dsts[0].amount; dsts[0].amount = 0; pop_index(dsts, 0); @@ -4549,9 +4915,9 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp if (available_amount > 0 && !dsts.empty() && estimate_tx_size(use_rct, tx.selected_transfers.size(), fake_outs_count, tx.dsts.size()) < TX_SIZE_TARGET(upper_transaction_size_limit)) { // we can partially fill that destination - LOG_PRINT_L2("We can partially pay " << get_account_address_as_str(m_testnet, dsts[0].addr) << + LOG_PRINT_L2("We can partially pay " << get_account_address_as_str(m_testnet, dsts[0].is_subaddress, dsts[0].addr) << " for " << print_money(available_amount) << "/" << print_money(dsts[0].amount)); - tx.add(dsts[0].addr, available_amount, original_output_index, m_merge_destinations); + tx.add(dsts[0].addr, dsts[0].is_subaddress, available_amount, original_output_index, m_merge_destinations); dsts[0].amount -= available_amount; available_amount = 0; } @@ -4603,7 +4969,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp if (i->amount > needed_fee) { uint64_t new_paid_amount = i->amount /*+ test_ptx.fee*/ - needed_fee; - LOG_PRINT_L2("Adjusting amount paid to " << get_account_address_as_str(m_testnet, i->addr) << " from " << + LOG_PRINT_L2("Adjusting amount paid to " << get_account_address_as_str(m_testnet, i->is_subaddress, i->addr) << " from " << print_money(i->amount) << " to " << print_money(new_paid_amount) << " to accommodate " << print_money(needed_fee) << " fee"); dsts[0].amount += i->amount - new_paid_amount; @@ -4652,12 +5018,28 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp } } } + + // if unused_*_indices is empty while unused_*_indices_per_subaddr has multiple elements, and if we still have something to pay, + // pop front of unused_*_indices_per_subaddr and have unused_*_indices point to the front of unused_*_indices_per_subaddr + if ((!dsts.empty() && dsts[0].amount > 0) || adding_fee) + { + if (unused_transfers_indices->empty() && unused_transfers_indices_per_subaddr.size() > 1) + { + unused_transfers_indices_per_subaddr.erase(unused_transfers_indices_per_subaddr.begin()); + unused_transfers_indices = &unused_transfers_indices_per_subaddr[0].second; + } + if (unused_dust_indices->empty() && unused_dust_indices_per_subaddr.size() > 1) + { + unused_dust_indices_per_subaddr.erase(unused_dust_indices_per_subaddr.begin()); + unused_dust_indices = &unused_dust_indices_per_subaddr[0].second; + } + } } if (adding_fee) { LOG_PRINT_L1("We ran out of outputs while trying to gather final fee"); - THROW_WALLET_EXCEPTION_IF(1, error::tx_not_possible, unlocked_balance(), needed_money, accumulated_fee + needed_fee); + THROW_WALLET_EXCEPTION_IF(1, error::tx_not_possible, unlocked_balance(subaddr_account), needed_money, accumulated_fee + needed_fee); } LOG_PRINT_L1("Done creating " << txes.size() << " transactions, " << print_money(accumulated_fee) << @@ -4681,21 +5063,37 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp return ptx_vector; } -std::vector<wallet2::pending_tx> wallet2::create_transactions_all(uint64_t below, const cryptonote::account_public_address &address, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon) +std::vector<wallet2::pending_tx> wallet2::create_transactions_all(uint64_t below, const cryptonote::account_public_address &address, bool is_subaddress, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, bool trusted_daemon) { std::vector<size_t> unused_transfers_indices; std::vector<size_t> unused_dust_indices; const bool use_rct = use_fork_rules(4, 0); - // gather all our dust and non dust outputs + THROW_WALLET_EXCEPTION_IF(unlocked_balance(subaddr_account) == 0, error::wallet_internal_error, "No unclocked balance in the entire wallet"); + + std::map<uint32_t, uint64_t> balance_per_subaddr = unlocked_balance_per_subaddress(subaddr_account); + + if (subaddr_indices.empty()) + { + // in case subaddress index wasn't specified, choose non-empty subaddress randomly (with index=0 being chosen last) + if (balance_per_subaddr.count(0) == 1 && balance_per_subaddr.size() > 1) + balance_per_subaddr.erase(0); + auto i = balance_per_subaddr.begin(); + std::advance(i, crypto::rand<size_t>() % balance_per_subaddr.size()); + subaddr_indices.insert(i->first); + } + for (uint32_t i : subaddr_indices) + LOG_PRINT_L2("Spending from subaddress index " << i); + + // gather all dust and non-dust outputs of specified subaddress for (size_t i = 0; i < m_transfers.size(); ++i) { const transfer_details& td = m_transfers[i]; - if (!td.m_spent && (use_rct ? true : !td.is_rct()) && is_transfer_unlocked(td)) + if (!td.m_spent && (use_rct ? true : !td.is_rct()) && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1) { if (below == 0 || td.amount() < below) { - if (td.is_rct() || is_valid_decomposed_amount(td.amount())) + if ((td.is_rct()) || is_valid_decomposed_amount(td.amount())) unused_transfers_indices.push_back(i); else unused_dust_indices.push_back(i); @@ -4703,10 +5101,12 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_all(uint64_t below } } - return create_transactions_from(address, unused_transfers_indices, unused_dust_indices, fake_outs_count, unlock_time, priority, extra, trusted_daemon); + THROW_WALLET_EXCEPTION_IF(unused_transfers_indices.empty() && unused_dust_indices.empty(), error::not_enough_money, 0, 0, 0); // not sure if a new error class (something like 'cant_sweep_empty'?) should be introduced + + return create_transactions_from(address, is_subaddress, unused_transfers_indices, unused_dust_indices, fake_outs_count, unlock_time, priority, extra, trusted_daemon); } -std::vector<wallet2::pending_tx> wallet2::create_transactions_from(const cryptonote::account_public_address &address, std::vector<size_t> unused_transfers_indices, std::vector<size_t> unused_dust_indices, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon) +std::vector<wallet2::pending_tx> wallet2::create_transactions_from(const cryptonote::account_public_address &address, bool is_subaddress, std::vector<size_t> unused_transfers_indices, std::vector<size_t> unused_dust_indices, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, bool trusted_daemon) { uint64_t accumulated_fee, accumulated_outputs, accumulated_change; struct TX { @@ -4769,7 +5169,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_from(const crypton needed_fee = 0; - tx.dsts.push_back(tx_destination_entry(1, address)); + tx.dsts.push_back(tx_destination_entry(1, address, is_subaddress)); LOG_PRINT_L2("Trying to create a tx now, with " << tx.dsts.size() << " destinations and " << tx.selected_transfers.size() << " outputs"); @@ -4791,7 +5191,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_from(const crypton LOG_PRINT_L2("We made a tx, adjusting fee and saving it"); tx.dsts[0].amount = available_for_fee - needed_fee; if (use_rct) - transfer_selected_rct(tx.dsts, tx.selected_transfers, fake_outs_count, outs, unlock_time, needed_fee, extra, + transfer_selected_rct(tx.dsts, tx.selected_transfers, fake_outs_count, outs, unlock_time, needed_fee, extra, test_tx, test_ptx); else transfer_selected(tx.dsts, tx.selected_transfers, fake_outs_count, outs, unlock_time, needed_fee, extra, @@ -4838,21 +5238,6 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_from(const crypton // if we made it this far, we're OK to actually send the transactions return ptx_vector; } - -uint64_t wallet2::unlocked_dust_balance(const tx_dust_policy &dust_policy) const -{ - uint64_t money = 0; - std::list<transfer_container::iterator> selected_transfers; - for (transfer_container::const_iterator i = m_transfers.begin(); i != m_transfers.end(); ++i) - { - const transfer_details& td = *i; - if (!td.m_spent && td.amount() < dust_policy.dust_threshold && is_transfer_unlocked(td)) - { - money += td.amount(); - } - } - return money; -} //---------------------------------------------------------------------------------------------------- void wallet2::get_hard_fork_info(uint8_t version, uint64_t &earliest_height) { @@ -5027,15 +5412,19 @@ std::vector<wallet2::pending_tx> wallet2::create_unmixable_sweep_transactions(bo unmixable_transfer_outputs.push_back(n); } - return create_transactions_from(m_account_public_address, unmixable_transfer_outputs, unmixable_dust_outputs, 0 /*fake_outs_count */, 0 /* unlock_time */, 1 /*priority */, std::vector<uint8_t>(), trusted_daemon); + return create_transactions_from(m_account_public_address, false, unmixable_transfer_outputs, unmixable_dust_outputs, 0 /*fake_outs_count */, 0 /* unlock_time */, 1 /*priority */, std::vector<uint8_t>(), trusted_daemon); } -bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const +bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key, std::vector<crypto::secret_key> &additional_tx_keys) const { + additional_tx_keys.clear(); const std::unordered_map<crypto::hash, crypto::secret_key>::const_iterator i = m_tx_keys.find(txid); if (i == m_tx_keys.end()) return false; tx_key = i->second; + const auto j = m_additional_tx_keys.find(txid); + if (j != m_additional_tx_keys.end()) + additional_tx_keys = j->second; return true; } @@ -5190,6 +5579,15 @@ crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::walle // more than one, loop and search const cryptonote::account_keys& keys = m_account.get_keys(); size_t pk_index = 0; + + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); + std::vector<crypto::key_derivation> additional_derivations; + for (size_t i = 0; i < additional_tx_pub_keys.size(); ++i) + { + additional_derivations.push_back({}); + generate_key_derivation(additional_tx_pub_keys[i], keys.m_view_secret_key, additional_derivations.back()); + } + while (find_tx_extra_field_by_type(tx_extra_fields, pub_key_field, pk_index++)) { const crypto::public_key tx_pub_key = pub_key_field.pub_key; crypto::key_derivation derivation; @@ -5198,7 +5596,7 @@ crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::walle for (size_t i = 0; i < td.m_tx.vout.size(); ++i) { tx_scan_info_t tx_scan_info; - check_acc_out_precomp(keys.m_account_address.m_spend_public_key, td.m_tx.vout[i], derivation, i, tx_scan_info); + check_acc_out_precomp(td.m_tx.vout[i], derivation, additional_derivations, i, tx_scan_info); if (!tx_scan_info.error && tx_scan_info.received) return tx_pub_key; } @@ -5210,7 +5608,7 @@ crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::walle return crypto::null_pkey; } -bool wallet2::export_key_images(const std::string filename) +bool wallet2::export_key_images(const std::string &filename) { std::vector<std::pair<crypto::key_image, crypto::signature>> ski = export_key_images(); std::string magic(KEY_IMAGE_EXPORT_FILE_MAGIC, strlen(KEY_IMAGE_EXPORT_FILE_MAGIC)); @@ -5258,11 +5656,12 @@ std::vector<std::pair<crypto::key_image, crypto::signature>> wallet2::export_key } crypto::public_key tx_pub_key = get_tx_pub_key_from_received_outs(td); + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); // generate ephemeral secret key crypto::key_image ki; cryptonote::keypair in_ephemeral; - bool r = cryptonote::generate_key_image_helper(m_account.get_keys(), tx_pub_key, td.m_internal_output_index, in_ephemeral, ki); + bool r = cryptonote::generate_key_image_helper(m_account.get_keys(), m_subaddresses, pkey, tx_pub_key, additional_tx_pub_keys, td.m_internal_output_index, in_ephemeral, ki); THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "Failed to generate key image"); THROW_WALLET_EXCEPTION_IF(td.m_key_image_known && ki != td.m_key_image, @@ -5492,18 +5891,25 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag const crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(spent_tx); crypto::key_derivation derivation; generate_key_derivation(tx_pub_key, keys.m_view_secret_key, derivation); + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(spent_tx); + std::vector<crypto::key_derivation> additional_derivations; + for (size_t i = 0; i < additional_tx_pub_keys.size(); ++i) + { + additional_derivations.push_back({}); + generate_key_derivation(additional_tx_pub_keys[i], keys.m_view_secret_key, additional_derivations.back()); + } size_t output_index = 0; for (const cryptonote::tx_out& out : spent_tx.vout) { tx_scan_info_t tx_scan_info; - check_acc_out_precomp(keys.m_account_address.m_spend_public_key, out, derivation, output_index, tx_scan_info); + check_acc_out_precomp(out, derivation, additional_derivations, output_index, tx_scan_info); THROW_WALLET_EXCEPTION_IF(tx_scan_info.error, error::wallet_internal_error, "check_acc_out_precomp failed"); if (tx_scan_info.received) { if (tx_scan_info.money_transfered == 0) { rct::key mask; - tx_scan_info.money_transfered = tools::decodeRct(spent_tx.rct_signatures, tx_pub_key, keys.m_view_secret_key, output_index, mask); + tx_scan_info.money_transfered = tools::decodeRct(spent_tx.rct_signatures, tx_scan_info.received->derivation, output_index, mask); } tx_money_got_in_outs += tx_scan_info.money_transfered; } @@ -5512,6 +5918,8 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag // get spent amount uint64_t tx_money_spent_in_ins = 0; + uint32_t subaddr_account = (uint32_t)-1; + std::set<uint32_t> subaddr_indices; for (const cryptonote::txin_v& in : spent_tx.vin) { if (in.type() != typeid(cryptonote::txin_to_key)) @@ -5533,12 +5941,16 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag LOG_PRINT_L0("Spent money: " << print_money(amount) << ", with tx: " << *spent_txid); set_spent(it->second, e.block_height); if (m_callback) - m_callback->on_money_spent(e.block_height, *spent_txid, spent_tx, amount, spent_tx); + m_callback->on_money_spent(e.block_height, *spent_txid, spent_tx, amount, spent_tx, td.m_subaddr_index); + if (subaddr_account != (uint32_t)-1 && subaddr_account != td.m_subaddr_index.major) + LOG_PRINT_L0("WARNING: This tx spends outputs received by different subaddress accounts, which isn't supposed to happen"); + subaddr_account = td.m_subaddr_index.major; + subaddr_indices.insert(td.m_subaddr_index.minor); } } // create outgoing payment - process_outgoing(*spent_txid, spent_tx, e.block_height, e.block_timestamp, tx_money_spent_in_ins, tx_money_got_in_outs); + process_outgoing(*spent_txid, spent_tx, e.block_height, e.block_timestamp, tx_money_spent_in_ins, tx_money_got_in_outs, subaddr_account, subaddr_indices); // erase corresponding incoming payment for (auto j = m_payments.begin(); j != m_payments.end(); ++j) @@ -5652,15 +6064,17 @@ size_t wallet2::import_outputs(const std::vector<tools::wallet2::transfer_detail // the hot wallet wouldn't have known about key images (except if we already exported them) cryptonote::keypair in_ephemeral; std::vector<tx_extra_field> tx_extra_fields; - tx_extra_pub_key pub_key_field; THROW_WALLET_EXCEPTION_IF(td.m_tx.vout.empty(), error::wallet_internal_error, "tx with no outputs at index " + boost::lexical_cast<std::string>(i)); THROW_WALLET_EXCEPTION_IF(!parse_tx_extra(td.m_tx.extra, tx_extra_fields), error::wallet_internal_error, "Transaction extra has unsupported format at index " + boost::lexical_cast<std::string>(i)); crypto::public_key tx_pub_key = get_tx_pub_key_from_received_outs(td); + const std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(td.m_tx); - bool r = cryptonote::generate_key_image_helper(m_account.get_keys(), tx_pub_key, td.m_internal_output_index, in_ephemeral, td.m_key_image); + const crypto::public_key& out_key = boost::get<cryptonote::txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key; + bool r = cryptonote::generate_key_image_helper(m_account.get_keys(), m_subaddresses, out_key, tx_pub_key, additional_tx_pub_keys, td.m_internal_output_index, in_ephemeral, td.m_key_image); THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "Failed to generate key image"); + expand_subaddresses(td.m_subaddr_index); td.m_key_image_known = true; THROW_WALLET_EXCEPTION_IF(in_ephemeral.pub != boost::get<cryptonote::txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key, error::wallet_internal_error, "key_image generated ephemeral public key not matched with output_key at index " + boost::lexical_cast<std::string>(i)); @@ -5731,17 +6145,15 @@ std::string wallet2::decrypt_with_view_secret_key(const std::string &ciphertext, //---------------------------------------------------------------------------------------------------- std::string wallet2::make_uri(const std::string &address, const std::string &payment_id, uint64_t amount, const std::string &tx_description, const std::string &recipient_name, std::string &error) { - cryptonote::account_public_address tmp_address; - bool has_payment_id; - crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(tmp_address, has_payment_id, new_payment_id, testnet(), address)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet(), address)) { error = std::string("wrong address: ") + address; return std::string(); } // we want only one payment id - if (has_payment_id && !payment_id.empty()) + if (info.has_payment_id && !payment_id.empty()) { error = "A single payment id is allowed"; return std::string(); @@ -5797,10 +6209,8 @@ bool wallet2::parse_uri(const std::string &uri, std::string &address, std::strin const char *ptr = strchr(remainder.c_str(), '?'); address = ptr ? remainder.substr(0, ptr-remainder.c_str()) : remainder; - cryptonote::account_public_address addr; - bool has_payment_id; - crypto::hash8 new_payment_id; - if(!get_account_integrated_address_from_str(addr, has_payment_id, new_payment_id, testnet(), address)) + cryptonote::address_parse_info info; + if(!get_account_address_from_str(info, testnet(), address)) { error = std::string("URI has wrong address: ") + address; return false; @@ -5841,7 +6251,7 @@ bool wallet2::parse_uri(const std::string &uri, std::string &address, std::strin } else if (kv[0] == "tx_payment_id") { - if (has_payment_id) + if (info.has_payment_id) { error = "Separate payment id given with an integrated address"; return false; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index e31ad5dc8..ff049552e 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -72,9 +72,9 @@ namespace tools { public: virtual void on_new_block(uint64_t height, const cryptonote::block& block) {} - virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) {} - virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount) {} - virtual void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, uint64_t amount, const cryptonote::transaction& spend_tx) {} + virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) {} + virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount, const cryptonote::subaddress_index& subaddr_index) {} + virtual void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx, uint64_t amount, const cryptonote::transaction& spend_tx, const cryptonote::subaddress_index& subaddr_index) {} virtual void on_skip_transaction(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx) {} virtual ~i_wallet2_callback() {} }; @@ -175,9 +175,9 @@ namespace tools uint64_t amount; uint64_t money_transfered; bool error; - bool received; + boost::optional<cryptonote::subaddress_receive_info> received; - tx_scan_info_t(): money_transfered(0), error(true), received(false) {} + tx_scan_info_t(): money_transfered(0), error(true) {} }; struct transfer_details @@ -195,6 +195,7 @@ namespace tools bool m_rct; bool m_key_image_known; size_t m_pk_index; + cryptonote::subaddress_index m_subaddr_index; bool is_rct() const { return m_rct; } uint64_t amount() const { return m_amount; } @@ -214,6 +215,7 @@ namespace tools FIELD(m_rct) FIELD(m_key_image_known) FIELD(m_pk_index) + FIELD(m_subaddr_index) END_SERIALIZE() }; @@ -224,6 +226,7 @@ namespace tools uint64_t m_block_height; uint64_t m_unlock_time; uint64_t m_timestamp; + cryptonote::subaddress_index m_subaddr_index; }; struct unconfirmed_transfer_details @@ -237,6 +240,8 @@ namespace tools crypto::hash m_payment_id; enum { pending, pending_not_in_pool, failed } m_state; uint64_t m_timestamp; + uint32_t m_subaddr_account; // subaddress account of your wallet to be used in this transfer + std::set<uint32_t> m_subaddr_indices; // set of address indices used as inputs in this transfer }; struct confirmed_transfer_details @@ -249,10 +254,12 @@ namespace tools crypto::hash m_payment_id; uint64_t m_timestamp; uint64_t m_unlock_time; + uint32_t m_subaddr_account; // subaddress account of your wallet to be used in this transfer + std::set<uint32_t> m_subaddr_indices; // set of address indices used as inputs in this transfer - confirmed_transfer_details(): m_amount_in(0), m_amount_out(0), m_change((uint64_t)-1), m_block_height(0), m_payment_id(crypto::null_hash), m_timestamp(0), m_unlock_time(0) {} + confirmed_transfer_details(): m_amount_in(0), m_amount_out(0), m_change((uint64_t)-1), m_block_height(0), m_payment_id(crypto::null_hash), m_timestamp(0), m_unlock_time(0), m_subaddr_account((uint32_t)-1) {} confirmed_transfer_details(const unconfirmed_transfer_details &utd, uint64_t height): - m_amount_in(utd.m_amount_in), m_amount_out(utd.m_amount_out), m_change(utd.m_change), m_block_height(height), m_dests(utd.m_dests), m_payment_id(utd.m_payment_id), m_timestamp(utd.m_timestamp), m_unlock_time(utd.m_tx.unlock_time) {} + m_amount_in(utd.m_amount_in), m_amount_out(utd.m_amount_out), m_change(utd.m_change), m_block_height(height), m_dests(utd.m_dests), m_payment_id(utd.m_payment_id), m_timestamp(utd.m_timestamp), m_unlock_time(utd.m_tx.unlock_time), m_subaddr_account(utd.m_subaddr_account), m_subaddr_indices(utd.m_subaddr_indices) {} }; struct tx_construction_data @@ -265,6 +272,8 @@ namespace tools uint64_t unlock_time; bool use_rct; std::vector<cryptonote::tx_destination_entry> dests; // original setup, does not include change + uint32_t subaddr_account; // subaddress account of your wallet to be used in this transfer + std::set<uint32_t> subaddr_indices; // set of address indices used as inputs in this transfer }; typedef std::vector<transfer_details> transfer_container; @@ -282,6 +291,7 @@ namespace tools std::list<size_t> selected_transfers; std::string key_images; crypto::secret_key tx_key; + std::vector<crypto::secret_key> additional_tx_keys; std::vector<cryptonote::tx_destination_entry> dests; tx_construction_data construction_data; @@ -329,6 +339,7 @@ namespace tools cryptonote::account_public_address m_address; crypto::hash m_payment_id; std::string m_description; + bool m_is_subaddress; }; typedef std::tuple<uint64_t, crypto::public_key, rct::key> get_outs_entry; @@ -419,6 +430,21 @@ namespace tools * \brief Sets the seed language */ void set_seed_language(const std::string &language); + + // Subaddress scheme + cryptonote::account_public_address get_subaddress(const cryptonote::subaddress_index& index) const; + cryptonote::account_public_address get_address() const { return get_subaddress({0,0}); } + crypto::public_key get_subaddress_spend_public_key(const cryptonote::subaddress_index& index) const; + std::string get_subaddress_as_str(const cryptonote::subaddress_index& index) const; + std::string get_address_as_str() const { return get_subaddress_as_str({0, 0}); } + std::string get_integrated_address_as_str(const crypto::hash8& payment_id) const; + void add_subaddress_account(const std::string& label); + size_t get_num_subaddress_accounts() const { return m_subaddress_labels.size(); } + size_t get_num_subaddresses(uint32_t index_major) const { return index_major < m_subaddress_labels.size() ? m_subaddress_labels[index_major].size() : 0; } + void add_subaddress(uint32_t index_major, const std::string& label); // throws when index is out of bound + void expand_subaddresses(const cryptonote::subaddress_index& index); + std::string get_subaddress_label(const cryptonote::subaddress_index& index) const; // throws when index is out of bound + void set_subaddress_label(const cryptonote::subaddress_index &index, const std::string &label); // throws when index is out of bound /*! * \brief Tells if the wallet file is deprecated. */ @@ -435,9 +461,15 @@ namespace tools bool restricted() const { return m_restricted; } bool watch_only() const { return m_watch_only; } - uint64_t balance() const; - uint64_t unlocked_balance() const; - uint64_t unlocked_dust_balance(const tx_dust_policy &dust_policy) const; + // locked & unlocked balance of given or current subaddress account + uint64_t balance(uint32_t subaddr_index_major) const; + uint64_t unlocked_balance(uint32_t subaddr_index_major) const; + // locked & unlocked balance per subaddress of given or current subaddress account + std::map<uint32_t, uint64_t> balance_per_subaddress(uint32_t subaddr_index_major) const; + std::map<uint32_t, uint64_t> unlocked_balance_per_subaddress(uint32_t subaddr_index_major) const; + // all locked & unlocked balances of all subaddress accounts + uint64_t balance_all() const; + uint64_t unlocked_balance_all() const; template<typename T> void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, const size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, T destination_split_strategy, const tx_dust_policy& dust_policy, bool trusted_daemon); template<typename T> @@ -445,10 +477,10 @@ namespace tools void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, const size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, bool trusted_daemon); void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, const size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, cryptonote::transaction& tx, pending_tx& ptx, bool trusted_daemon); template<typename T> - void transfer_selected(const std::vector<cryptonote::tx_destination_entry>& dsts, const std::list<size_t> selected_transfers, size_t fake_outputs_count, + void transfer_selected(const std::vector<cryptonote::tx_destination_entry>& dsts, const std::list<size_t>& selected_transfers, size_t fake_outputs_count, std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction& tx, pending_tx &ptx); - void transfer_selected_rct(std::vector<cryptonote::tx_destination_entry> dsts, const std::list<size_t> selected_transfers, size_t fake_outputs_count, + void transfer_selected_rct(std::vector<cryptonote::tx_destination_entry> dsts, const std::list<size_t>& selected_transfers, size_t fake_outputs_count, std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, cryptonote::transaction& tx, pending_tx &ptx); @@ -462,19 +494,19 @@ namespace tools // load unsigned_tx_set from file. bool load_unsigned_tx(const std::string &unsigned_filename, unsigned_tx_set &exported_txs); 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); - std::vector<pending_tx> create_transactions(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon); - std::vector<wallet2::pending_tx> create_transactions_2(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon); - std::vector<wallet2::pending_tx> create_transactions_all(uint64_t below, const cryptonote::account_public_address &address, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon); - std::vector<wallet2::pending_tx> create_transactions_from(const cryptonote::account_public_address &address, std::vector<size_t> unused_transfers_indices, std::vector<size_t> unused_dust_indices, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon); + std::vector<pending_tx> create_transactions(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, bool trusted_daemon); + std::vector<wallet2::pending_tx> create_transactions_2(std::vector<cryptonote::tx_destination_entry> dsts, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, bool trusted_daemon); // 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 fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, uint32_t subaddr_account, std::set<uint32_t> subaddr_indices, bool trusted_daemon); + std::vector<wallet2::pending_tx> create_transactions_from(const cryptonote::account_public_address &address, bool is_subaddress, std::vector<size_t> unused_transfers_indices, std::vector<size_t> unused_dust_indices, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t>& extra, bool trusted_daemon); std::vector<pending_tx> create_unmixable_sweep_transactions(bool trusted_daemon); bool check_connection(uint32_t *version = NULL, uint32_t timeout = 200000); void get_transfers(wallet2::transfer_container& incoming_transfers) const; - void get_payments(const crypto::hash& payment_id, std::list<wallet2::payment_details>& payments, uint64_t min_height = 0) const; - void get_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& payments, uint64_t min_height, uint64_t max_height = (uint64_t)-1) const; + void get_payments(const crypto::hash& payment_id, std::list<wallet2::payment_details>& payments, uint64_t min_height = 0, const boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) const; + void get_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& payments, uint64_t min_height, uint64_t max_height = (uint64_t)-1, const boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) const; void get_payments_out(std::list<std::pair<crypto::hash,wallet2::confirmed_transfer_details>>& confirmed_payments, - uint64_t min_height, uint64_t max_height = (uint64_t)-1) const; - void get_unconfirmed_payments_out(std::list<std::pair<crypto::hash,wallet2::unconfirmed_transfer_details>>& unconfirmed_payments) const; - void get_unconfirmed_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& unconfirmed_payments) const; + uint64_t min_height, uint64_t max_height = (uint64_t)-1, const boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) const; + void get_unconfirmed_payments_out(std::list<std::pair<crypto::hash,wallet2::unconfirmed_transfer_details>>& unconfirmed_payments, const boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) const; + void get_unconfirmed_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& unconfirmed_payments, const boost::optional<uint32_t>& subaddr_account = boost::none, const std::set<uint32_t>& subaddr_indices = {}) const; uint64_t get_blockchain_current_height() const { return m_local_bc_height; } void rescan_spent(); @@ -555,6 +587,12 @@ namespace tools return; a & m_scanned_pool_txs[0]; a & m_scanned_pool_txs[1]; + if (ver < 20) + return; + a & m_subaddresses; + a & m_subaddresses_inv; + a & m_subaddress_labels; + a & m_additional_tx_keys; } /*! @@ -603,13 +641,13 @@ namespace tools void set_confirm_backlog_threshold(uint32_t threshold) { m_confirm_backlog_threshold = threshold; }; uint32_t get_confirm_backlog_threshold() const { return m_confirm_backlog_threshold; }; - bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const; + bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key, std::vector<crypto::secret_key> &additional_tx_keys) const; /*! * \brief GUI Address book get/store */ std::vector<address_book_row> get_address_book() const { return m_address_book; } - bool add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description); + bool add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description, bool is_subaddress); bool delete_address_book_row(std::size_t row_id); uint64_t get_num_rct_outputs(); @@ -651,7 +689,7 @@ namespace tools void import_payments_out(const std::list<std::pair<crypto::hash,wallet2::confirmed_transfer_details>> &confirmed_payments); std::tuple<size_t, crypto::hash, std::vector<crypto::hash>> export_blockchain() const; void import_blockchain(const std::tuple<size_t, crypto::hash, std::vector<crypto::hash>> &bc); - bool export_key_images(const std::string filename); + bool export_key_images(const std::string &filename); std::vector<std::pair<crypto::key_image, crypto::signature>> export_key_images() const; uint64_t import_key_images(const std::vector<std::pair<crypto::key_image, crypto::signature>> &signed_key_images, uint64_t &spent, uint64_t &unspent, bool check_spent = true); uint64_t import_key_images(const std::string &filename, uint64_t &spent, uint64_t &unspent); @@ -704,27 +742,27 @@ namespace tools uint64_t select_transfers(uint64_t needed_money, std::vector<size_t> unused_transfers_indices, std::list<size_t>& selected_transfers, bool trusted_daemon); bool prepare_file_names(const std::string& file_path); void process_unconfirmed(const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t height); - void process_outgoing(const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t height, uint64_t ts, uint64_t spent, uint64_t received); - void add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amount_in, const std::vector<cryptonote::tx_destination_entry> &dests, const crypto::hash &payment_id, uint64_t change_amount); + void process_outgoing(const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t height, uint64_t ts, uint64_t spent, uint64_t received, uint32_t subaddr_account, const std::set<uint32_t>& subaddr_indices); + void add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t amount_in, const std::vector<cryptonote::tx_destination_entry> &dests, const crypto::hash &payment_id, uint64_t change_amount, uint32_t subaddr_account, const std::set<uint32_t>& subaddr_indices); void generate_genesis(cryptonote::block& b); void check_genesis(const crypto::hash& genesis_hash) const; //throws bool generate_chacha8_key_from_secret_keys(crypto::chacha8_key &key) const; crypto::hash get_payment_id(const pending_tx &ptx) const; crypto::hash8 get_short_payment_id(const pending_tx &ptx) const; - void check_acc_out_precomp(const crypto::public_key &spend_public_key, const cryptonote::tx_out &o, const crypto::key_derivation &derivation, size_t i, tx_scan_info_t &tx_scan_info) const; + void check_acc_out_precomp(const cryptonote::tx_out &o, const crypto::key_derivation &derivation, const std::vector<crypto::key_derivation> &additional_derivations, size_t i, tx_scan_info_t &tx_scan_info) const; void parse_block_round(const cryptonote::blobdata &blob, cryptonote::block &bl, crypto::hash &bl_id, bool &error) const; uint64_t get_upper_transaction_size_limit(); std::vector<uint64_t> get_unspent_amounts_vector(); uint64_t get_dynamic_per_kb_fee_estimate(); float get_output_relatedness(const transfer_details &td0, const transfer_details &td1) const; - std::vector<size_t> pick_preferred_rct_inputs(uint64_t needed_money) const; + std::vector<size_t> pick_preferred_rct_inputs(uint64_t needed_money, uint32_t subaddr_account, const std::set<uint32_t> &subaddr_indices) const; void set_spent(size_t idx, uint64_t height); void set_unspent(size_t idx); void get_outs(std::vector<std::vector<get_outs_entry>> &outs, const std::list<size_t> &selected_transfers, size_t fake_outputs_count); crypto::public_key get_tx_pub_key_from_received_outs(const tools::wallet2::transfer_details &td) const; bool should_pick_a_second_output(bool use_rct, size_t n_transfers, const std::vector<size_t> &unused_transfers_indices, const std::vector<size_t> &unused_dust_indices) const; std::vector<size_t> get_only_rct(const std::vector<size_t> &unused_dust_indices, const std::vector<size_t> &unused_transfers_indices) const; - void scan_output(const cryptonote::account_keys &keys, const cryptonote::transaction &tx, const crypto::public_key &tx_pub_key, size_t i, tx_scan_info_t &tx_scan_info, int &num_vouts_received, uint64_t &tx_money_got_in_outs, std::vector<size_t> &outs); + void scan_output(const cryptonote::account_keys &keys, const cryptonote::transaction &tx, const crypto::public_key &tx_pub_key, size_t i, tx_scan_info_t &tx_scan_info, int &num_vouts_received, std::unordered_map<cryptonote::subaddress_index, uint64_t> &tx_money_got_in_outs, std::vector<size_t> &outs); void trim_hashchain(); cryptonote::account_base m_account; @@ -740,12 +778,16 @@ namespace tools std::unordered_multimap<crypto::hash, payment_details> m_unconfirmed_payments; std::unordered_map<crypto::hash, crypto::secret_key> m_tx_keys; cryptonote::checkpoints m_checkpoints; + std::unordered_map<crypto::hash, std::vector<crypto::secret_key>> m_additional_tx_keys; transfer_container m_transfers; payment_container m_payments; std::unordered_map<crypto::key_image, size_t> m_key_images; std::unordered_map<crypto::public_key, size_t> m_pub_keys; cryptonote::account_public_address m_account_public_address; + std::unordered_map<crypto::public_key, cryptonote::subaddress_index> m_subaddresses; + std::unordered_map<cryptonote::subaddress_index, crypto::public_key> m_subaddresses_inv; + std::vector<std::vector<std::string>> m_subaddress_labels; std::unordered_map<crypto::hash, std::string> m_tx_notes; std::vector<tools::wallet2::address_book_row> m_address_book; uint64_t m_upper_transaction_size_limit; //TODO: auto-calc this value or request from daemon, now use some fixed value @@ -780,16 +822,16 @@ namespace tools std::unordered_set<crypto::hash> m_scanned_pool_txs[2]; }; } -BOOST_CLASS_VERSION(tools::wallet2, 19) -BOOST_CLASS_VERSION(tools::wallet2::transfer_details, 7) -BOOST_CLASS_VERSION(tools::wallet2::payment_details, 1) -BOOST_CLASS_VERSION(tools::wallet2::unconfirmed_transfer_details, 6) -BOOST_CLASS_VERSION(tools::wallet2::confirmed_transfer_details, 4) -BOOST_CLASS_VERSION(tools::wallet2::address_book_row, 16) +BOOST_CLASS_VERSION(tools::wallet2, 20) +BOOST_CLASS_VERSION(tools::wallet2::transfer_details, 8) +BOOST_CLASS_VERSION(tools::wallet2::payment_details, 2) +BOOST_CLASS_VERSION(tools::wallet2::unconfirmed_transfer_details, 7) +BOOST_CLASS_VERSION(tools::wallet2::confirmed_transfer_details, 5) +BOOST_CLASS_VERSION(tools::wallet2::address_book_row, 17) BOOST_CLASS_VERSION(tools::wallet2::unsigned_tx_set, 0) BOOST_CLASS_VERSION(tools::wallet2::signed_tx_set, 0) -BOOST_CLASS_VERSION(tools::wallet2::tx_construction_data, 0) -BOOST_CLASS_VERSION(tools::wallet2::pending_tx, 0) +BOOST_CLASS_VERSION(tools::wallet2::tx_construction_data, 1) +BOOST_CLASS_VERSION(tools::wallet2::pending_tx, 1) namespace boost { @@ -823,6 +865,10 @@ namespace boost { x.m_pk_index = 0; } + if (ver < 8) + { + x.m_subaddr_index = {}; + } } template <class Archive> @@ -890,6 +936,12 @@ namespace boost return; } a & x.m_pk_index; + if (ver < 8) + { + initialize_transfer_details(a, x, ver); + return; + } + a & x.m_subaddr_index; } template <class Archive> @@ -929,6 +981,10 @@ namespace boost if (!typename Archive::is_saving() && x.m_change != (uint64_t)-1) x.m_amount_out += x.m_change; } + if (ver < 7) + return; + a & x.m_subaddr_account; + a & x.m_subaddr_indices; } template <class Archive> @@ -966,6 +1022,10 @@ namespace boost return; } a & x.m_unlock_time; + if (ver < 5) + return; + a & x.m_subaddr_account; + a & x.m_subaddr_indices; } template <class Archive> @@ -978,13 +1038,9 @@ namespace boost if (ver < 1) return; a & x.m_timestamp; - } - - template <class Archive> - inline void serialize(Archive& a, cryptonote::tx_destination_entry& x, const boost::serialization::version_type ver) - { - a & x.amount; - a & x.addr; + if (ver < 2) + return; + a & x.m_subaddr_index; } template <class Archive> @@ -993,6 +1049,9 @@ namespace boost a & x.m_address; a & x.m_payment_id; a & x.m_description; + if (ver < 17) + return; + a & x.m_is_subaddress; } template <class Archive> @@ -1020,6 +1079,10 @@ namespace boost a & x.unlock_time; a & x.use_rct; a & x.dests; + if (ver < 1) + return; + a & x.subaddr_account; + a & x.subaddr_indices; } template <class Archive> @@ -1035,6 +1098,9 @@ namespace boost a & x.tx_key; a & x.dests; a & x.construction_data; + if (ver < 1) + return; + a & x.additional_tx_keys; } } } @@ -1055,18 +1121,18 @@ namespace tools for(auto& de: dsts) { cryptonote::decompose_amount_into_digits(de.amount, 0, - [&](uint64_t chunk) { splitted_dsts.push_back(cryptonote::tx_destination_entry(chunk, de.addr)); }, - [&](uint64_t a_dust) { splitted_dsts.push_back(cryptonote::tx_destination_entry(a_dust, de.addr)); } ); + [&](uint64_t chunk) { splitted_dsts.push_back(cryptonote::tx_destination_entry(chunk, de.addr, de.is_subaddress)); }, + [&](uint64_t a_dust) { splitted_dsts.push_back(cryptonote::tx_destination_entry(a_dust, de.addr, de.is_subaddress)); } ); } cryptonote::decompose_amount_into_digits(change_dst.amount, 0, [&](uint64_t chunk) { if (chunk <= dust_threshold) - dust_dsts.push_back(cryptonote::tx_destination_entry(chunk, change_dst.addr)); + dust_dsts.push_back(cryptonote::tx_destination_entry(chunk, change_dst.addr, false)); else - splitted_dsts.push_back(cryptonote::tx_destination_entry(chunk, change_dst.addr)); + splitted_dsts.push_back(cryptonote::tx_destination_entry(chunk, change_dst.addr, false)); }, - [&](uint64_t a_dust) { dust_dsts.push_back(cryptonote::tx_destination_entry(a_dust, change_dst.addr)); } ); + [&](uint64_t a_dust) { dust_dsts.push_back(cryptonote::tx_destination_entry(a_dust, change_dst.addr, false)); } ); } //---------------------------------------------------------------------------------------------------- inline void null_split_strategy(const std::vector<cryptonote::tx_destination_entry>& dsts, @@ -1080,7 +1146,7 @@ namespace tools if (0 != change) { - splitted_dsts.push_back(cryptonote::tx_destination_entry(change, change_dst.addr)); + splitted_dsts.push_back(cryptonote::tx_destination_entry(change, change_dst.addr, false)); } } //---------------------------------------------------------------------------------------------------- @@ -1103,7 +1169,7 @@ namespace tools } template<typename T> - void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, + void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, const size_t fake_outputs_count, const std::vector<size_t> &unused_transfers_indices, uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction& tx, pending_tx &ptx, bool trusted_daemon) { using namespace cryptonote; @@ -1128,6 +1194,10 @@ namespace tools uint64_t found_money = select_transfers(needed_money, unused_transfers_indices, selected_transfers, trusted_daemon); THROW_WALLET_EXCEPTION_IF(found_money < needed_money, error::not_enough_money, found_money, needed_money - fee, fee); + uint32_t subaddr_account = m_transfers[*selected_transfers.begin()].m_subaddr_index.major; + for (auto i = ++selected_transfers.begin(); i != selected_transfers.end(); ++i) + THROW_WALLET_EXCEPTION_IF(subaddr_account != *i, error::wallet_internal_error, "the tx uses funds from multiple accounts"); + typedef COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::out_entry out_entry; typedef cryptonote::tx_source_entry::output_entry tx_output_entry; @@ -1215,7 +1285,7 @@ namespace tools cryptonote::tx_destination_entry change_dts = AUTO_VAL_INIT(change_dts); if (needed_money < found_money) { - change_dts.addr = m_account.get_keys().m_account_address; + change_dts.addr = get_subaddress({subaddr_account, 0}); change_dts.amount = found_money - needed_money; } @@ -1228,12 +1298,13 @@ namespace tools } for(auto& d: dust_dsts) { if (!dust_policy.add_to_fee) - splitted_dsts.push_back(cryptonote::tx_destination_entry(d.amount, dust_policy.addr_for_dust)); + splitted_dsts.push_back(cryptonote::tx_destination_entry(d.amount, dust_policy.addr_for_dust, d.is_subaddress)); dust += d.amount; } crypto::secret_key tx_key; - bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time, tx_key); + std::vector<crypto::secret_key> additional_tx_keys; + bool r = cryptonote::construct_tx_and_get_tx_key(m_account.get_keys(), m_subaddresses, sources, splitted_dsts, change_dts.addr, extra, tx, unlock_time, tx_key, additional_tx_keys); THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time, m_testnet); THROW_WALLET_EXCEPTION_IF(upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, upper_transaction_size_limit); @@ -1259,6 +1330,7 @@ namespace tools ptx.change_dts = change_dts; ptx.selected_transfers = selected_transfers; ptx.tx_key = tx_key; + ptx.additional_tx_keys = additional_tx_keys; ptx.dests = dsts; ptx.construction_data.sources = sources; ptx.construction_data.change_dts = change_dts; @@ -1268,6 +1340,11 @@ namespace tools ptx.construction_data.unlock_time = unlock_time; ptx.construction_data.use_rct = false; ptx.construction_data.dests = dsts; + // record which subaddress indices are being used as inputs + ptx.construction_data.subaddr_account = subaddr_account; + ptx.construction_data.subaddr_indices.clear(); + for (size_t idx: selected_transfers) + ptx.construction_data.subaddr_indices.insert(m_transfers[idx].m_subaddr_index.minor); } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 7a5e01af7..6f612bbb3 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -33,6 +33,7 @@ #include <string> #include <vector> +#include <set> #include <ctime> #include <iostream> @@ -88,6 +89,8 @@ struct PendingTransaction * \return */ virtual uint64_t txCount() const = 0; + virtual std::vector<uint32_t> subaddrAccount() const = 0; + virtual std::vector<std::set<uint32_t>> subaddrIndices() const = 0; }; /** @@ -155,6 +158,9 @@ struct TransactionInfo virtual uint64_t amount() const = 0; virtual uint64_t fee() const = 0; virtual uint64_t blockHeight() const = 0; + virtual std::set<uint32_t> subaddrIndex() const = 0; + virtual uint32_t subaddrAccount() const = 0; + virtual std::string label() const = 0; virtual uint64_t confirmations() const = 0; virtual uint64_t unlockTime() const = 0; //! transaction_id @@ -223,6 +229,66 @@ struct AddressBook virtual int lookupPaymentID(const std::string &payment_id) const = 0; }; +struct SubaddressRow { +public: + SubaddressRow(std::size_t _rowId, const std::string &_address, const std::string &_label): + m_rowId(_rowId), + m_address(_address), + m_label(_label) {} + +private: + std::size_t m_rowId; + std::string m_address; + std::string m_label; +public: + std::string extra; + std::string getAddress() const {return m_address;} + std::string getLabel() const {return m_label;} + std::size_t getRowId() const {return m_rowId;} +}; + +struct Subaddress +{ + virtual ~Subaddress() = 0; + virtual std::vector<SubaddressRow*> getAll() const = 0; + virtual void addRow(uint32_t accountIndex, const std::string &label) = 0; + virtual void setLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label) = 0; + virtual void refresh(uint32_t accountIndex) = 0; +}; + +struct SubaddressAccountRow { +public: + SubaddressAccountRow(std::size_t _rowId, const std::string &_address, const std::string &_label, const std::string &_balance, const std::string &_unlockedBalance): + m_rowId(_rowId), + m_address(_address), + m_label(_label), + m_balance(_balance), + m_unlockedBalance(_unlockedBalance) {} + +private: + std::size_t m_rowId; + std::string m_address; + std::string m_label; + std::string m_balance; + std::string m_unlockedBalance; +public: + std::string extra; + std::string getAddress() const {return m_address;} + std::string getLabel() const {return m_label;} + std::string getBalance() const {return m_balance;} + std::string getUnlockedBalance() const {return m_unlockedBalance;} + std::size_t getRowId() const {return m_rowId;} +}; + +struct SubaddressAccount +{ + virtual ~SubaddressAccount() = 0; + virtual std::vector<SubaddressAccountRow*> getAll() const = 0; + virtual void addRow(const std::string &label) = 0; + virtual void setLabel(uint32_t accountIndex, const std::string &label) = 0; + virtual void refresh() = 0; +}; + struct WalletListener { virtual ~WalletListener() = 0; @@ -294,7 +360,8 @@ struct Wallet //! in case error status, returns error string virtual std::string errorString() const = 0; virtual bool setPassword(const std::string &password) = 0; - virtual std::string address() const = 0; + virtual std::string address(uint32_t accountIndex, uint32_t addressIndex) const = 0; + std::string mainAddress() const { return address(0, 0); } virtual std::string path() const = 0; virtual bool testnet() const = 0; //! returns current hard fork info @@ -406,8 +473,20 @@ struct Wallet virtual ConnectionStatus connected() const = 0; virtual void setTrustedDaemon(bool arg) = 0; virtual bool trustedDaemon() const = 0; - virtual uint64_t balance() const = 0; - virtual uint64_t unlockedBalance() const = 0; + virtual uint64_t balance(uint32_t accountIndex) const = 0; + uint64_t balanceAll() const { + uint64_t result = 0; + for (uint32_t i = 0; i < numSubaddressAccounts(); ++i) + result += balance(i); + return result; + } + virtual uint64_t unlockedBalance(uint32_t accountIndex) const = 0; + uint64_t unlockedBalanceAll() const { + uint64_t result = 0; + for (uint32_t i = 0; i < numSubaddressAccounts(); ++i) + result += unlockedBalance(i); + return result; + } /** * @brief watchOnly - checks if wallet is watch only @@ -492,6 +571,39 @@ struct Wallet */ virtual int autoRefreshInterval() const = 0; + /** + * @brief addSubaddressAccount - appends a new subaddress account at the end of the last major index of existing subaddress accounts + * @param label - the label for the new account (which is the as the label of the primary address (accountIndex,0)) + */ + virtual void addSubaddressAccount(const std::string& label) = 0; + /** + * @brief numSubaddressAccounts - returns the number of existing subaddress accounts + */ + virtual size_t numSubaddressAccounts() const = 0; + /** + * @brief numSubaddresses - returns the number of existing subaddresses associated with the specified subaddress account + * @param accountIndex - the major index specifying the subaddress account + */ + virtual size_t numSubaddresses(uint32_t accountIndex) const = 0; + /** + * @brief addSubaddress - appends a new subaddress at the end of the last minor index of the specified subaddress account + * @param accountIndex - the major index specifying the subaddress account + * @param label - the label for the new subaddress + */ + virtual void addSubaddress(uint32_t accountIndex, const std::string& label) = 0; + /** + * @brief getSubaddressLabel - gets the label of the specified subaddress + * @param accountIndex - the major index specifying the subaddress account + * @param addressIndex - the minor index specifying the subaddress + */ + virtual std::string getSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex) const = 0; + /** + * @brief setSubaddressLabel - sets the label of the specified subaddress + * @param accountIndex - the major index specifying the subaddress account + * @param addressIndex - the minor index specifying the subaddress + * @param label - the new label for the specified subaddress + */ + virtual void setSubaddressLabel(uint32_t accountIndex, uint32_t addressIndex, const std::string &label) = 0; /*! * \brief createTransaction creates transaction. if dst_addr is an integrated address, payment_id is ignored @@ -499,6 +611,8 @@ struct Wallet * \param payment_id optional payment_id, can be empty string * \param amount amount * \param mixin_count mixin count. if 0 passed, wallet will use default value + * \param subaddr_account subaddress account from which the input funds are taken + * \param subaddr_indices set of subaddress indices to use for transfer or sweeping. if set empty, all are chosen when sweeping, and one or more are automatically chosen when transferring. after execution, returns the set of actually used indices * \param priority * \return PendingTransaction object. caller is responsible to check PendingTransaction::status() * after object returned @@ -506,6 +620,8 @@ struct Wallet virtual PendingTransaction * createTransaction(const std::string &dst_addr, const std::string &payment_id, optional<uint64_t> amount, uint32_t mixin_count, + uint32_t subaddr_account, + std::set<uint32_t> subaddr_indices, PendingTransaction::Priority = PendingTransaction::Priority_Low) = 0; /*! @@ -551,8 +667,10 @@ struct Wallet virtual bool importKeyImages(const std::string &filename) = 0; - virtual TransactionHistory * history() const = 0; - virtual AddressBook * addressBook() const = 0; + virtual TransactionHistory * history() = 0; + virtual AddressBook * addressBook() = 0; + virtual Subaddress * subaddress() = 0; + virtual SubaddressAccount * subaddressAccount() = 0; virtual void setListener(WalletListener *) = 0; /*! * \brief defaultMixin - returns number of mixins used in transactions diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h index 2e5acc8cb..d1f4a796d 100644 --- a/src/wallet/wallet_errors.h +++ b/src/wallet/wallet_errors.h @@ -492,7 +492,7 @@ namespace tools for (size_t i = 0; i < m_destinations.size(); ++i) { const cryptonote::tx_destination_entry& dst = m_destinations[i]; - ss << "\n " << i << ": " << cryptonote::get_account_address_as_str(m_testnet, dst.addr) << " " << + ss << "\n " << i << ": " << cryptonote::get_account_address_as_str(m_testnet, dst.is_subaddress, dst.addr) << " " << cryptonote::print_money(dst.amount); } @@ -567,7 +567,7 @@ namespace tools ", destinations:"; for (const auto& dst : m_destinations) { - ss << '\n' << cryptonote::print_money(dst.amount) << " -> " << cryptonote::get_account_address_as_str(m_testnet, dst.addr); + ss << '\n' << cryptonote::print_money(dst.amount) << " -> " << cryptonote::get_account_address_as_str(m_testnet, dst.is_subaddress, dst.addr); } return ss.str(); } diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 46b092376..25f780134 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -253,6 +253,7 @@ namespace tools entry.fee = 0; // TODO entry.note = m_wallet->get_tx_note(pd.m_tx_hash); entry.type = "in"; + entry.subaddr_index = pd.m_subaddr_index; } //------------------------------------------------------------------------------------------------------------------------------ void wallet_rpc_server::fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const tools::wallet2::confirmed_transfer_details &pd) @@ -273,10 +274,11 @@ namespace tools entry.destinations.push_back(wallet_rpc::transfer_destination()); wallet_rpc::transfer_destination &td = entry.destinations.back(); td.amount = d.amount; - td.address = get_account_address_as_str(m_wallet->testnet(), d.addr); + td.address = get_account_address_as_str(m_wallet->testnet(), d.is_subaddress, d.addr); } entry.type = "out"; + entry.subaddr_index = { pd.m_subaddr_account, 0 }; } //------------------------------------------------------------------------------------------------------------------------------ void wallet_rpc_server::fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const tools::wallet2::unconfirmed_transfer_details &pd) @@ -294,6 +296,7 @@ namespace tools entry.unlock_time = pd.m_tx.unlock_time; entry.note = m_wallet->get_tx_note(txid); entry.type = is_failed ? "failed" : "pending"; + entry.subaddr_index = { pd.m_subaddr_account, 0 }; } //------------------------------------------------------------------------------------------------------------------------------ void wallet_rpc_server::fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &payment_id, const tools::wallet2::payment_details &pd) @@ -309,6 +312,7 @@ namespace tools entry.fee = 0; // TODO entry.note = m_wallet->get_tx_note(pd.m_tx_hash); entry.type = "pool"; + entry.subaddr_index = pd.m_subaddr_index; } //------------------------------------------------------------------------------------------------------------------------------ bool wallet_rpc_server::on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er) @@ -316,8 +320,24 @@ namespace tools if (!m_wallet) return not_open(er); try { - res.balance = m_wallet->balance(); - res.unlocked_balance = m_wallet->unlocked_balance(); + res.balance = m_wallet->balance(req.account_index); + res.unlocked_balance = m_wallet->unlocked_balance(req.account_index); + std::map<uint32_t, uint64_t> balance_per_subaddress = m_wallet->balance_per_subaddress(req.account_index); + std::map<uint32_t, uint64_t> unlocked_balance_per_subaddress = m_wallet->unlocked_balance_per_subaddress(req.account_index); + std::vector<tools::wallet2::transfer_details> transfers; + m_wallet->get_transfers(transfers); + for (const auto& i : balance_per_subaddress) + { + wallet_rpc::COMMAND_RPC_GET_BALANCE::per_subaddress_info info; + info.address_index = i.first; + cryptonote::subaddress_index index = {req.account_index, info.address_index}; + info.address = m_wallet->get_subaddress_as_str(index); + info.balance = i.second; + info.unlocked_balance = unlocked_balance_per_subaddress[i.first]; + info.label = m_wallet->get_subaddress_label(index); + info.num_unspent_outputs = std::count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; }); + res.per_subaddress.push_back(info); + } } catch (const std::exception& e) { @@ -333,7 +353,126 @@ namespace tools if (!m_wallet) return not_open(er); try { - res.address = m_wallet->get_account().get_public_address_str(m_wallet->testnet()); + res.addresses.resize(m_wallet->get_num_subaddresses(req.account_index)); + tools::wallet2::transfer_container transfers; + m_wallet->get_transfers(transfers); + cryptonote::subaddress_index index = {req.account_index, 0}; + for (; index.minor < m_wallet->get_num_subaddresses(req.account_index); ++index.minor) + { + auto& info = res.addresses[index.minor]; + info.address = m_wallet->get_subaddress_as_str(index); + info.label = m_wallet->get_subaddress_label(index); + info.address_index = index.minor; + info.used = std::find_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return td.m_subaddr_index == index; }) != transfers.end(); + } + res.address = m_wallet->get_subaddress_as_str({req.account_index, 0}); + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = e.what(); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_create_address(const wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::response& res, epee::json_rpc::error& er) + { + if (!m_wallet) return not_open(er); + m_wallet->add_subaddress(req.account_index, req.label); + res.address_index = m_wallet->get_num_subaddresses(req.account_index) - 1; + res.address = m_wallet->get_subaddress_as_str({req.account_index, res.address_index}); + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_label_address(const wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::response& res, epee::json_rpc::error& er) + { + if (!m_wallet) return not_open(er); + if (req.index.major >= m_wallet->get_num_subaddress_accounts()) + { + er.code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUTOFBOUND; + er.message = "Account index is out of bound"; + return false; + } + if (req.index.minor >= m_wallet->get_num_subaddresses(req.index.major)) + { + er.code = WALLET_RPC_ERROR_CODE_ADDRESS_INDEX_OUTOFBOUND; + er.message = "Address index is out of bound"; + return false; + } + try + { + m_wallet->set_subaddress_label(req.index, req.label); + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = e.what(); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_get_accounts(const wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::request& req, wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::response& res, epee::json_rpc::error& er) + { + if (!m_wallet) return not_open(er); + try + { + res.total_balance = 0; + res.total_unlocked_balance = 0; + cryptonote::subaddress_index subaddr_index = {0,0}; + for (; subaddr_index.major < m_wallet->get_num_subaddress_accounts(); ++subaddr_index.major) + { + wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::subaddress_account_info info; + info.account_index = subaddr_index.major; + info.base_address = m_wallet->get_subaddress_as_str(subaddr_index); + info.balance = m_wallet->balance(subaddr_index.major); + info.unlocked_balance = m_wallet->unlocked_balance(subaddr_index.major); + info.label = m_wallet->get_subaddress_label(subaddr_index); + res.subaddress_accounts.push_back(info); + res.total_balance += info.balance; + res.total_unlocked_balance += info.unlocked_balance; + } + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = e.what(); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_create_account(const wallet_rpc::COMMAND_RPC_CREATE_ACCOUNT::request& req, wallet_rpc::COMMAND_RPC_CREATE_ACCOUNT::response& res, epee::json_rpc::error& er) + { + if (!m_wallet) return not_open(er); + try + { + m_wallet->add_subaddress_account(req.label); + res.account_index = m_wallet->get_num_subaddress_accounts() - 1; + res.address = m_wallet->get_subaddress_as_str({res.account_index, 0}); + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = e.what(); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_label_account(const wallet_rpc::COMMAND_RPC_LABEL_ACCOUNT::request& req, wallet_rpc::COMMAND_RPC_LABEL_ACCOUNT::response& res, epee::json_rpc::error& er) + { + if (!m_wallet) return not_open(er); + if (req.account_index >= m_wallet->get_num_subaddress_accounts()) + { + er.code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUTOFBOUND; + er.message = "Account index is out of bound"; + return false; + } + try + { + m_wallet->set_subaddress_label({req.account_index, 0}, req.label); } catch (const std::exception& e) { @@ -360,17 +499,16 @@ namespace tools return true; } //------------------------------------------------------------------------------------------------------------------------------ - bool wallet_rpc_server::validate_transfer(const std::list<wallet_rpc::transfer_destination> destinations, std::string payment_id, std::vector<cryptonote::tx_destination_entry>& dsts, std::vector<uint8_t>& extra, epee::json_rpc::error& er) + bool wallet_rpc_server::validate_transfer(const std::list<wallet_rpc::transfer_destination>& destinations, const std::string& payment_id, std::vector<cryptonote::tx_destination_entry>& dsts, std::vector<uint8_t>& extra, epee::json_rpc::error& er) { crypto::hash8 integrated_payment_id = crypto::null_hash8; std::string extra_nonce; for (auto it = destinations.begin(); it != destinations.end(); it++) { + cryptonote::address_parse_info info; cryptonote::tx_destination_entry de; - bool has_payment_id; - crypto::hash8 new_payment_id; er.message = ""; - if(!get_account_address_from_str_or_url(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), it->address, + if(!get_account_address_from_str_or_url(info, m_wallet->testnet(), it->address, [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { if (!dnssec_valid) { @@ -390,10 +528,13 @@ namespace tools er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + it->address; return false; } + + de.addr = info.address; + de.is_subaddress = info.is_subaddress; de.amount = it->amount; dsts.push_back(de); - if (has_payment_id) + if (info.has_payment_id) { if (!payment_id.empty() || integrated_payment_id != crypto::null_hash8) { @@ -401,7 +542,7 @@ namespace tools er.message = "A single payment id is allowed per transaction"; return false; } - integrated_payment_id = new_payment_id; + integrated_payment_id = info.payment_id; cryptonote::set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce, integrated_payment_id); /* Append Payment ID data into extra */ @@ -472,7 +613,7 @@ namespace tools try { uint64_t mixin = adjust_mixin(req.mixin); - std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, m_trusted_daemon); + std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, req.account_index, req.subaddr_indices, m_trusted_daemon); // reject proposed transactions if there are more than one. see on_transfer_split below. if (ptx_vector.size() != 1) @@ -548,7 +689,7 @@ namespace tools uint64_t ptx_amount; std::vector<wallet2::pending_tx> ptx_vector; LOG_PRINT_L2("on_transfer_split calling create_transactions_2"); - ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, m_trusted_daemon); + ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, req.account_index, req.subaddr_indices, m_trusted_daemon); LOG_PRINT_L2("on_transfer_split called create_transactions_2"); if (!req.do_not_relay) @@ -688,7 +829,7 @@ namespace tools try { uint64_t mixin = adjust_mixin(req.mixin); - std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_all(req.below_amount, dsts[0].addr, mixin, req.unlock_time, req.priority, extra, m_trusted_daemon); + std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_all(req.below_amount, dsts[0].addr, dsts[0].is_subaddress, mixin, req.unlock_time, req.priority, extra, req.account_index, req.subaddr_indices, m_trusted_daemon); if (!req.do_not_relay) m_wallet->commit_tx(ptx_vector); @@ -752,7 +893,7 @@ namespace tools } } - res.integrated_address = m_wallet->get_account().get_public_integrated_address_str(payment_id, m_wallet->testnet()); + res.integrated_address = m_wallet->get_integrated_address_as_str(payment_id); res.payment_id = epee::string_tools::pod_to_hex(payment_id); return true; } @@ -770,24 +911,22 @@ namespace tools if (!m_wallet) return not_open(er); try { - cryptonote::account_public_address address; - crypto::hash8 payment_id; - bool has_payment_id; + cryptonote::address_parse_info info; - if(!get_account_integrated_address_from_str(address, has_payment_id, payment_id, m_wallet->testnet(), req.integrated_address)) + if(!get_account_address_from_str(info, m_wallet->testnet(), req.integrated_address)) { er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; er.message = "Invalid address"; return false; } - if(!has_payment_id) + if(!info.has_payment_id) { er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; er.message = "Address is not an integrated address"; return false; } - res.standard_address = get_account_address_as_str(m_wallet->testnet(),address); - res.payment_id = epee::string_tools::pod_to_hex(payment_id); + res.standard_address = get_account_address_as_str(m_wallet->testnet(), info.is_subaddress, info.address); + res.payment_id = epee::string_tools::pod_to_hex(info.payment_id); return true; } catch (const std::exception &e) @@ -863,6 +1002,7 @@ namespace tools rpc_payment.amount = payment.m_amount; rpc_payment.block_height = payment.m_block_height; rpc_payment.unlock_time = payment.m_unlock_time; + rpc_payment.subaddr_index = payment.m_subaddr_index; res.payments.push_back(rpc_payment); } @@ -888,6 +1028,7 @@ namespace tools rpc_payment.amount = payment.second.m_amount; rpc_payment.block_height = payment.second.m_block_height; rpc_payment.unlock_time = payment.second.m_unlock_time; + rpc_payment.subaddr_index = payment.second.m_subaddr_index; res.payments.push_back(std::move(rpc_payment)); } @@ -937,6 +1078,7 @@ namespace tools rpc_payment.amount = payment.m_amount; rpc_payment.block_height = payment.m_block_height; rpc_payment.unlock_time = payment.m_unlock_time; + rpc_payment.subaddr_index = payment.m_subaddr_index; res.payments.push_back(std::move(rpc_payment)); } } @@ -975,6 +1117,8 @@ namespace tools { if (!filter || available != td.m_spent) { + if (req.account_index != td.m_subaddr_index.major || (!req.subaddr_indices.empty() && req.subaddr_indices.count(td.m_subaddr_index.minor) == 0)) + continue; if (!transfers_found) { transfers_found = true; @@ -986,6 +1130,7 @@ namespace tools rpc_transfers.global_index = td.m_global_output_index; rpc_transfers.tx_hash = epee::string_tools::pod_to_hex(td.m_txid); rpc_transfers.tx_size = txBlob.size(); + rpc_transfers.subaddr_index = td.m_subaddr_index.minor; res.transfers.push_back(rpc_transfers); } } @@ -1071,11 +1216,9 @@ namespace tools return false; } - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id; + cryptonote::address_parse_info info; er.message = ""; - if(!get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), req.address, + if(!get_account_address_from_str_or_url(info, m_wallet->testnet(), req.address, [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { if (!dnssec_valid) { @@ -1094,7 +1237,7 @@ namespace tools return false; } - res.good = m_wallet->verify(req.data, address, req.signature); + res.good = m_wallet->verify(req.data, info.address, req.signature); return true; } //------------------------------------------------------------------------------------------------------------------------------ @@ -1214,7 +1357,7 @@ namespace tools if (req.in) { std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; - m_wallet->get_payments(payments, min_height, max_height); + m_wallet->get_payments(payments, min_height, max_height, req.account_index, req.subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { res.in.push_back(wallet_rpc::transfer_entry()); fill_transfer_entry(res.in.back(), i->second.m_tx_hash, i->first, i->second); @@ -1224,7 +1367,7 @@ namespace tools if (req.out) { std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> payments; - m_wallet->get_payments_out(payments, min_height, max_height); + m_wallet->get_payments_out(payments, min_height, max_height, req.account_index, req.subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { res.out.push_back(wallet_rpc::transfer_entry()); fill_transfer_entry(res.out.back(), i->first, i->second); @@ -1233,7 +1376,7 @@ namespace tools if (req.pending || req.failed) { std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>> upayments; - m_wallet->get_unconfirmed_payments_out(upayments); + m_wallet->get_unconfirmed_payments_out(upayments, req.account_index, req.subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) { const tools::wallet2::unconfirmed_transfer_details &pd = i->second; bool is_failed = pd.m_state == tools::wallet2::unconfirmed_transfer_details::failed; @@ -1250,7 +1393,7 @@ namespace tools m_wallet->update_pool_state(); std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments; - m_wallet->get_unconfirmed_payments(payments); + m_wallet->get_unconfirmed_payments(payments, req.account_index, req.subaddr_indices); for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) { res.pool.push_back(wallet_rpc::transfer_entry()); fill_transfer_entry(res.pool.back(), i->first, i->second); @@ -1453,7 +1596,7 @@ namespace tools { uint64_t idx = 0; for (const auto &entry: ab) - res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx++, get_account_address_as_str(m_wallet->testnet(), entry.m_address), epee::string_tools::pod_to_hex(entry.m_payment_id), entry.m_description}); + res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx++, get_account_address_as_str(m_wallet->testnet(), entry.m_is_subaddress, entry.m_address), epee::string_tools::pod_to_hex(entry.m_payment_id), entry.m_description}); } else { @@ -1466,7 +1609,7 @@ namespace tools return false; } const auto &entry = ab[idx]; - res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx, get_account_address_as_str(m_wallet->testnet(), entry.m_address), epee::string_tools::pod_to_hex(entry.m_payment_id), entry.m_description}); + res.entries.push_back(wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::entry{idx, get_account_address_as_str(m_wallet->testnet(), entry.m_is_subaddress, entry.m_address), epee::string_tools::pod_to_hex(entry.m_payment_id), entry.m_description}); } } return true; @@ -1482,12 +1625,10 @@ namespace tools return false; } - cryptonote::account_public_address address; - bool has_payment_id; - crypto::hash8 payment_id8; + cryptonote::address_parse_info info; crypto::hash payment_id = crypto::null_hash; er.message = ""; - if(!get_account_address_from_str_or_url(address, has_payment_id, payment_id8, m_wallet->testnet(), req.address, + if(!get_account_address_from_str_or_url(info, m_wallet->testnet(), req.address, [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string { if (!dnssec_valid) { @@ -1507,14 +1648,14 @@ namespace tools er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + req.address; return false; } - if (has_payment_id) + if (info.has_payment_id) { - memcpy(payment_id.data, payment_id8.data, 8); + memcpy(payment_id.data, info.payment_id.data, 8); memset(payment_id.data + 8, 0, 24); } if (!req.payment_id.empty()) { - if (has_payment_id) + if (info.has_payment_id) { er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; er.message = "Separate payment ID given with integrated address"; @@ -1526,7 +1667,7 @@ namespace tools if (!wallet2::parse_long_payment_id(req.payment_id, payment_id)) { - if (!wallet2::parse_short_payment_id(req.payment_id, payment_id8)) + if (!wallet2::parse_short_payment_id(req.payment_id, info.payment_id)) { er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; er.message = "Payment id has invalid format: \"" + req.payment_id + "\", expected 16 or 64 character string"; @@ -1534,12 +1675,12 @@ namespace tools } else { - memcpy(payment_id.data, payment_id8.data, 8); + memcpy(payment_id.data, info.payment_id.data, 8); memset(payment_id.data + 8, 0, 24); } } } - if (!m_wallet->add_address_book_row(address, payment_id, req.description)) + if (!m_wallet->add_address_book_row(info.address, payment_id, req.description, info.is_subaddress)) { er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; er.message = "Failed to add address book entry"; diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h index e5ed0a846..7f4c412e4 100644 --- a/src/wallet/wallet_rpc_server.h +++ b/src/wallet/wallet_rpc_server.h @@ -69,6 +69,11 @@ namespace tools BEGIN_JSON_RPC_MAP("/json_rpc") MAP_JON_RPC_WE("getbalance", on_getbalance, wallet_rpc::COMMAND_RPC_GET_BALANCE) MAP_JON_RPC_WE("getaddress", on_getaddress, wallet_rpc::COMMAND_RPC_GET_ADDRESS) + MAP_JON_RPC_WE("create_address", on_create_address, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS) + MAP_JON_RPC_WE("label_address", on_label_address, wallet_rpc::COMMAND_RPC_LABEL_ADDRESS) + MAP_JON_RPC_WE("get_accounts", on_get_accounts, wallet_rpc::COMMAND_RPC_GET_ACCOUNTS) + MAP_JON_RPC_WE("create_account", on_create_account, wallet_rpc::COMMAND_RPC_CREATE_ACCOUNT) + MAP_JON_RPC_WE("label_account", on_label_account, wallet_rpc::COMMAND_RPC_LABEL_ACCOUNT) MAP_JON_RPC_WE("getheight", on_getheight, wallet_rpc::COMMAND_RPC_GET_HEIGHT) MAP_JON_RPC_WE("transfer", on_transfer, wallet_rpc::COMMAND_RPC_TRANSFER) MAP_JON_RPC_WE("transfer_split", on_transfer_split, wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT) @@ -108,8 +113,13 @@ namespace tools //json_rpc bool on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er); bool on_getaddress(const wallet_rpc::COMMAND_RPC_GET_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS::response& res, epee::json_rpc::error& er); + bool on_create_address(const wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::response& res, epee::json_rpc::error& er); + bool on_label_address(const wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::response& res, epee::json_rpc::error& er); + bool on_get_accounts(const wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::request& req, wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::response& res, epee::json_rpc::error& er); + bool on_create_account(const wallet_rpc::COMMAND_RPC_CREATE_ACCOUNT::request& req, wallet_rpc::COMMAND_RPC_CREATE_ACCOUNT::response& res, epee::json_rpc::error& er); + bool on_label_account(const wallet_rpc::COMMAND_RPC_LABEL_ACCOUNT::request& req, wallet_rpc::COMMAND_RPC_LABEL_ACCOUNT::response& res, epee::json_rpc::error& er); bool on_getheight(const wallet_rpc::COMMAND_RPC_GET_HEIGHT::request& req, wallet_rpc::COMMAND_RPC_GET_HEIGHT::response& res, epee::json_rpc::error& er); - bool validate_transfer(const std::list<wallet_rpc::transfer_destination> destinations, const std::string payment_id, std::vector<cryptonote::tx_destination_entry>& dsts, std::vector<uint8_t>& extra, epee::json_rpc::error& er); + bool validate_transfer(const std::list<wallet_rpc::transfer_destination>& destinations, const std::string& payment_id, std::vector<cryptonote::tx_destination_entry>& dsts, std::vector<uint8_t>& extra, epee::json_rpc::error& er); bool on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res, epee::json_rpc::error& er); bool on_transfer_split(const wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::request& req, wallet_rpc::COMMAND_RPC_TRANSFER_SPLIT::response& res, epee::json_rpc::error& er); bool on_sweep_dust(const wallet_rpc::COMMAND_RPC_SWEEP_DUST::request& req, wallet_rpc::COMMAND_RPC_SWEEP_DUST::response& res, epee::json_rpc::error& er); diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h index fa5c154de..f652fa7ff 100644 --- a/src/wallet/wallet_rpc_server_commands_defs.h +++ b/src/wallet/wallet_rpc_server_commands_defs.h @@ -31,6 +31,7 @@ #pragma once #include "cryptonote_protocol/cryptonote_protocol_defs.h" #include "cryptonote_basic/cryptonote_basic.h" +#include "cryptonote_basic/subaddress_index.h" #include "crypto/hash.h" #include "wallet_rpc_server_error_codes.h" @@ -48,7 +49,28 @@ namespace wallet_rpc { struct request { + uint32_t account_index; BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + END_KV_SERIALIZE_MAP() + }; + + struct per_subaddress_info + { + uint32_t address_index; + std::string address; + uint64_t balance; + uint64_t unlocked_balance; + std::string label; + uint64_t num_unspent_outputs; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address_index) + KV_SERIALIZE(address) + KV_SERIALIZE(balance) + KV_SERIALIZE(unlocked_balance) + KV_SERIALIZE(label) + KV_SERIALIZE(num_unspent_outputs) END_KV_SERIALIZE_MAP() }; @@ -56,10 +78,12 @@ namespace wallet_rpc { uint64_t balance; uint64_t unlocked_balance; + std::vector<per_subaddress_info> per_subaddress; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(balance) KV_SERIALIZE(unlocked_balance) + KV_SERIALIZE(per_subaddress) END_KV_SERIALIZE_MAP() }; }; @@ -68,20 +92,164 @@ namespace wallet_rpc { struct request { + uint32_t account_index; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + END_KV_SERIALIZE_MAP() + }; + + struct address_info + { + std::string address; + std::string label; + uint32_t address_index; + bool used; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(label) + KV_SERIALIZE(address_index) + KV_SERIALIZE(used) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + std::string address; // to remain compatible with older RPC format + std::vector<address_info> addresses; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(addresses) + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_CREATE_ADDRESS + { + struct request + { + uint32_t account_index; + std::string label; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + KV_SERIALIZE(label) END_KV_SERIALIZE_MAP() }; struct response { std::string address; + uint32_t address_index; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address) + KV_SERIALIZE(address_index) + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_LABEL_ADDRESS + { + struct request + { + cryptonote::subaddress_index index; + std::string label; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(index) + KV_SERIALIZE(label) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_GET_ACCOUNTS + { + struct request + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + + struct subaddress_account_info + { + uint32_t account_index; + std::string base_address; + uint64_t balance; + uint64_t unlocked_balance; + std::string label; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + KV_SERIALIZE(base_address) + KV_SERIALIZE(balance) + KV_SERIALIZE(unlocked_balance) + KV_SERIALIZE(label) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + uint64_t total_balance; + uint64_t total_unlocked_balance; + std::vector<subaddress_account_info> subaddress_accounts; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(total_balance) + KV_SERIALIZE(total_unlocked_balance) + KV_SERIALIZE(subaddress_accounts) + END_KV_SERIALIZE_MAP() + }; + }; + struct COMMAND_RPC_CREATE_ACCOUNT + { + struct request + { + std::string label; BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(label) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + uint32_t account_index; + std::string address; // the 0-th address for convenience + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) KV_SERIALIZE(address) END_KV_SERIALIZE_MAP() }; }; + struct COMMAND_RPC_LABEL_ACCOUNT + { + struct request + { + uint32_t account_index; + std::string label; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(account_index) + KV_SERIALIZE(label) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + struct COMMAND_RPC_GET_HEIGHT { struct request @@ -114,6 +282,8 @@ namespace wallet_rpc struct request { std::list<transfer_destination> destinations; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; uint32_t priority; uint64_t mixin; uint64_t unlock_time; @@ -124,6 +294,8 @@ namespace wallet_rpc BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(destinations) + KV_SERIALIZE(account_index) + KV_SERIALIZE(subaddr_indices) KV_SERIALIZE(priority) KV_SERIALIZE(mixin) KV_SERIALIZE(unlock_time) @@ -157,6 +329,8 @@ namespace wallet_rpc struct request { std::list<transfer_destination> destinations; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; uint32_t priority; uint64_t mixin; uint64_t unlock_time; @@ -167,6 +341,8 @@ namespace wallet_rpc BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(destinations) + KV_SERIALIZE(account_index) + KV_SERIALIZE(subaddr_indices) KV_SERIALIZE(priority) KV_SERIALIZE(mixin) KV_SERIALIZE(unlock_time) @@ -249,6 +425,8 @@ namespace wallet_rpc struct request { std::string address; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; uint32_t priority; uint64_t mixin; uint64_t unlock_time; @@ -260,6 +438,8 @@ namespace wallet_rpc BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(address) + KV_SERIALIZE(account_index) + KV_SERIALIZE(subaddr_indices) KV_SERIALIZE(priority) KV_SERIALIZE(mixin) KV_SERIALIZE(unlock_time) @@ -318,6 +498,7 @@ namespace wallet_rpc uint64_t amount; uint64_t block_height; uint64_t unlock_time; + cryptonote::subaddress_index subaddr_index; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(payment_id) @@ -325,6 +506,7 @@ namespace wallet_rpc KV_SERIALIZE(amount) KV_SERIALIZE(block_height) KV_SERIALIZE(unlock_time) + KV_SERIALIZE(subaddr_index) END_KV_SERIALIZE_MAP() }; @@ -379,6 +561,7 @@ namespace wallet_rpc uint64_t global_index; std::string tx_hash; uint64_t tx_size; + uint32_t subaddr_index; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(amount) @@ -386,6 +569,7 @@ namespace wallet_rpc KV_SERIALIZE(global_index) KV_SERIALIZE(tx_hash) KV_SERIALIZE(tx_size) + KV_SERIALIZE(subaddr_index) END_KV_SERIALIZE_MAP() }; @@ -394,9 +578,13 @@ namespace wallet_rpc struct request { std::string transfer_type; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(transfer_type) + KV_SERIALIZE(account_index) + KV_SERIALIZE(subaddr_indices) END_KV_SERIALIZE_MAP() }; @@ -470,10 +658,12 @@ namespace wallet_rpc { std::string standard_address; std::string payment_id; + bool is_subaddress; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(standard_address) KV_SERIALIZE(payment_id) + KV_SERIALIZE(is_subaddress) END_KV_SERIALIZE_MAP() }; }; @@ -561,6 +751,7 @@ namespace wallet_rpc std::list<transfer_destination> destinations; std::string type; uint64_t unlock_time; + cryptonote::subaddress_index subaddr_index; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(txid); @@ -573,6 +764,7 @@ namespace wallet_rpc KV_SERIALIZE(destinations); KV_SERIALIZE(type); KV_SERIALIZE(unlock_time) + KV_SERIALIZE(subaddr_index); END_KV_SERIALIZE_MAP() }; @@ -589,6 +781,8 @@ namespace wallet_rpc bool filter_by_height; uint64_t min_height; uint64_t max_height; + uint32_t account_index; + std::set<uint32_t> subaddr_indices; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(in); @@ -599,6 +793,8 @@ namespace wallet_rpc KV_SERIALIZE(filter_by_height); KV_SERIALIZE(min_height); KV_SERIALIZE(max_height); + KV_SERIALIZE(account_index); + KV_SERIALIZE(subaddr_indices); END_KV_SERIALIZE_MAP() }; diff --git a/src/wallet/wallet_rpc_server_error_codes.h b/src/wallet/wallet_rpc_server_error_codes.h index 3c79c0ac3..cc9fd3856 100644 --- a/src/wallet/wallet_rpc_server_error_codes.h +++ b/src/wallet/wallet_rpc_server_error_codes.h @@ -44,3 +44,5 @@ #define WALLET_RPC_ERROR_CODE_WRONG_URI -11 #define WALLET_RPC_ERROR_CODE_WRONG_INDEX -12 #define WALLET_RPC_ERROR_CODE_NOT_OPEN -13 +#define WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUTOFBOUND -14 +#define WALLET_RPC_ERROR_CODE_ADDRESS_INDEX_OUTOFBOUND -15 |