Skip to content

Commit

Permalink
Merge pull request #138 from victor-tucci/bns-update-in-libwalletapi
Browse files Browse the repository at this point in the history
Bns update in libwalletapi
  • Loading branch information
sanada08 authored Feb 15, 2024
2 parents 4e375a9 + 0293c3e commit 5d46cd3
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 1 deletion.
115 changes: 114 additions & 1 deletion src/wallet/api/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,8 @@ PendingTransaction *WalletImpl::createTransactionMultDest(const std::vector<std:
bool error = false;
auto w = wallet();
for (size_t i = 0; i < dst_addr.size() && !error; i++) {
if(!cryptonote::get_account_address_from_str(info, m_wallet_ptr->nettype(), dst_addr[i])) {
std::optional<std::string> address = w->resolve_address(dst_addr[i]);
if(!cryptonote::get_account_address_from_str(info, m_wallet_ptr->nettype(), *address)) {
// TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982
setStatusError(tr("Invalid destination address"));
error = true;
Expand Down Expand Up @@ -1825,6 +1826,11 @@ PendingTransaction *WalletImpl::createBnsTransaction(std::string& owner, std::st
}
pendingTxPostProcess(transaction);

if (good() && !setBnsRecord(name)) {
LOG_PRINT_L1(__FUNCTION__ << "BNS records are not being cached properly");
break;
}

}catch (const tools::error::daemon_busy&) {
// TODO: make it translatable with "tr"?
setStatusError(tr("daemon is busy. Please try again later."));
Expand Down Expand Up @@ -1939,6 +1945,11 @@ PendingTransaction *WalletImpl::bnsUpdateTransaction(std::string& owner, std::st
}
pendingTxPostProcess(transaction);

if (good() && !setBnsRecord(name)) {
LOG_PRINT_L1(__FUNCTION__ << "BNS records are not being cached properly");
break;
}

}catch (const tools::error::daemon_busy&) {
// TODO: make it translatable with "tr"?
setStatusError(tr("daemon is busy. Please try again later."));
Expand Down Expand Up @@ -2123,6 +2134,108 @@ PendingTransaction *WalletImpl::bnsRenewTransaction(std::string &name,std::strin
return transaction;
}

EXPORT
bool WalletImpl::setBnsRecord(const std::string &name)
{
std::string reason;
auto name_str = tools::lowercase_ascii_string(name);
if (bns::validate_bns_name(name_str, &reason))
{
std::string name_hash_str = bns::name_to_base64_hash(name);
tools::wallet2::bns_detail detail = {
name_str,
name_hash_str};
wallet()->set_bns_cache_record(detail);
return true;
}
setStatusError(tr("Invalid BNS name '" + name_str + "': " + reason));
return false;
}

EXPORT
std::vector<bnsInfo>* WalletImpl::MyBns() const
{
std::vector<bnsInfo>* my_bns = new std::vector<bnsInfo>;

auto w = wallet();

std::vector<std::vector<cryptonote::rpc::BNS_OWNERS_TO_NAMES::response_entry>> rpc_results;
std::vector<cryptonote::rpc::BNS_OWNERS_TO_NAMES::request> requests(1);

std::unordered_map<std::string, tools::wallet2::bns_detail> cache = w->get_bns_cache();

for (uint32_t index = 0; index < w->get_num_subaddresses(0); ++index)
{
if (requests.back().entries.size() >= cryptonote::rpc::BNS_OWNERS_TO_NAMES::MAX_REQUEST_ENTRIES)
requests.emplace_back();
requests.back().entries.push_back(w->get_subaddress_as_str({0, index}));
}

rpc_results.reserve(requests.size());
for (auto const &request : requests)
{
auto [success, result] = w->bns_owners_to_names(request);
if (!success)
{
setStatusError(tr("Connection to daemon failed when requesting BNS names"));
}
rpc_results.emplace_back(std::move(result));
}

auto nettype = w->nettype();
for (size_t i = 0; i < rpc_results.size(); i++)
{
auto const &rpc = rpc_results[i];
for (auto const &entry : rpc)
{
std::string_view name;
std::string value_bchat, value_wallet, value_belnet;
if (auto got = cache.find(entry.name_hash); got != cache.end())
{
name = got->second.name;
//BCHAT
{
bns::mapping_value mv;
const auto type = bns::mapping_type::bchat;
if (bns::mapping_value::validate_encrypted(type, oxenc::from_hex(entry.encrypted_bchat_value), &mv)
&& mv.decrypt(name, type))
value_bchat = mv.to_readable_value(nettype, type);
}
//WALLET
{
bns::mapping_value mv;
const auto type = bns::mapping_type::wallet;
if (bns::mapping_value::validate_encrypted(type, oxenc::from_hex(entry.encrypted_wallet_value), &mv)
&& mv.decrypt(name, type))
value_wallet = mv.to_readable_value(nettype,type);
}
//BELNET
{
bns::mapping_value mv;
const auto type = bns::mapping_type::belnet;
if (bns::mapping_value::validate_encrypted(type, oxenc::from_hex(entry.encrypted_belnet_value), &mv)
&& mv.decrypt(name, type))
value_belnet = mv.to_readable_value(nettype, type);
}
}
auto &info = my_bns->emplace_back();
info.name_hash = entry.name_hash;
info.name = name.empty() ? "(none)" : std::string(name);
info.value_bchat = value_bchat.empty() ? "(none)" : value_bchat;
info.value_wallet = value_wallet.empty() ? "(none)" : value_wallet;
info.value_belnet = value_belnet.empty() ? "(none)" : value_belnet;
info.owner = entry.owner;
info.backup_owner = entry.backup_owner? *entry.backup_owner : "(none)";
info.update_height = entry.update_height;
info.expiration_height = entry.expiration_height ? *entry.expiration_height : 0;
info.encrypted_bchat_value = entry.encrypted_bchat_value.empty() ? "(none)" : entry.encrypted_bchat_value;
info.encrypted_wallet_value = entry.encrypted_wallet_value.empty() ? "(none)" : entry.encrypted_wallet_value;
info.encrypted_belnet_value = entry.encrypted_belnet_value.empty() ? "(none)" : entry.encrypted_belnet_value;
}
}
return my_bns;
}

EXPORT
PendingTransaction *WalletImpl::createSweepUnmixableTransaction()

Expand Down
2 changes: 2 additions & 0 deletions src/wallet/api/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ class WalletImpl : public Wallet
uint32_t priority=0,
uint32_t m_current_subaddress_account = 0,
std::set<uint32_t> subaddr_indices = {}) override;
bool setBnsRecord(const std::string &name) override;
std::vector<bnsInfo>* MyBns() const override;
PendingTransaction* createSweepUnmixableTransaction() override;
bool submitTransaction(std::string_view filename) override;
UnsignedTransaction* loadUnsignedTx(std::string_view unsigned_filename) override;
Expand Down
27 changes: 27 additions & 0 deletions src/wallet/api/wallet2_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,21 @@ struct stakeInfo{
bool decommissioned = false;
};

struct bnsInfo{
uint64_t update_height;
uint64_t expiration_height;
std::string name_hash;
std::string name;
std::string owner;
std::string backup_owner;
std::string value_bchat;
std::string value_wallet;
std::string value_belnet;
std::string encrypted_bchat_value;
std::string encrypted_wallet_value;
std::string encrypted_belnet_value;
};

/**
* @brief Interface for wallet operations.
* TODO: check if /include/IWallet.h is still actual
Expand Down Expand Up @@ -927,6 +942,18 @@ struct Wallet
uint32_t m_current_subaddress_account = 0,
std::set<uint32_t> subaddr_indices = {}) = 0;

/*!
* \brief setBnsRecord - attach an name and namehash to a wallet cache attribute
* \param name - the key
* \return true if successful, false otherwise
*/
virtual bool setBnsRecord(const std::string &name) = 0;

/*!
* \return struct bnsInfo
*/
virtual std::vector<bnsInfo>* MyBns() const = 0;

/*!
* \brief createSweepUnmixableTransaction creates transaction with unmixable outputs.
* \return PendingTransaction object. caller is responsible to check PendingTransaction::status()
Expand Down
24 changes: 24 additions & 0 deletions tests/libwallet_api_tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,30 @@ TEST_F(WalletTest1, statusOfCountBns)
ASSERT_TRUE(wmgr->closeWallet(wallet1));
}

TEST_F(WalletTest1, bnsByOwner)
{
Wallet::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Wallet::NetworkType::TESTNET);
ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0));
std::vector<Wallet::bnsInfo> data = *(wallet1->MyBns());

for (const auto& info : data) {
std::cout << "name_hash :" << info.name_hash << std::endl;
std::cout << "name :" << info.name << std::endl;
std::cout << "value_bchat :" << info.value_bchat << std::endl;
std::cout << "value_wallet :" << info.value_wallet << std::endl;
std::cout << "value_belnet :" << info.value_belnet << std::endl;
std::cout << "owner :" << info.owner << std::endl;
std::cout << "backup_owner :" << info.backup_owner << std::endl;
std::cout << "update_height :" << info.update_height << std::endl;
std::cout << "expiration_height :" << info.expiration_height << std::endl;
std::cout << "encrypted_bchat_value :" << info.encrypted_bchat_value << std::endl;
std::cout << "encrypted_wallet_value :" << info.encrypted_wallet_value << std::endl;
std::cout << "encrypted_belnet_value :" << info.encrypted_belnet_value << std::endl;
}

ASSERT_TRUE(wmgr->closeWallet(wallet1));
}

// TEST_F(WalletTest1, WalletTransactionWithMixin)
// {
// std::vector<int> mixins;
Expand Down

0 comments on commit 5d46cd3

Please sign in to comment.