Skip to content

Commit

Permalink
account_info_t refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
madMAx43v3r committed Jun 28, 2024
1 parent 4ad6271 commit bc52c8c
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 75 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ add_library(mmx_iface SHARED
src/wordlist_en.cpp
src/mnemonic.cpp
src/offer_data_t.cpp
src/account_info_t.cpp
)

add_library(mmx_vm SHARED
Expand Down
8 changes: 8 additions & 0 deletions include/mmx/ECDSA_Wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ class ECDSA_Wallet {
return farmer_key;
}

vnx::optional<addr_t> find_address(const uint32_t index) const
{
if(index >= addresses.size()) {
return nullptr;
}
return addresses[index];
}

addr_t get_address(const uint32_t index) const
{
if(index >= addresses.size()) {
Expand Down
4 changes: 2 additions & 2 deletions include/mmx/Wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ class Wallet : public WalletBase {

std::vector<std::pair<skey_t, pubkey_t>> get_all_farmer_keys() const override;

account_t get_account(const uint32_t& index) const override;
account_info_t get_account(const uint32_t& index) const override;

std::map<uint32_t, account_t> get_all_accounts() const override;
std::vector<account_info_t> get_all_accounts() const override;

bool is_locked(const uint32_t& index) const override;

Expand Down
12 changes: 12 additions & 0 deletions interface/account_info_t.vni
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mmx;

struct account_info_t extends account_t {

optional<addr_t> address;

uint account;


static account_info_t make(uint account, optional<addr_t> address, account_t config);

}
4 changes: 2 additions & 2 deletions modules/Wallet.vni
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ module Wallet implements vnx.addons.HttpComponent {

vector<addr_t> get_all_addresses(int index) const; // (index == -1) -> all wallets

account_t get_account(uint index) const;
account_info_t get_account(uint index) const;

map<uint, account_t> get_all_accounts() const;
vector<account_info_t> get_all_accounts() const;

bool is_locked(uint index) const;

Expand Down
33 changes: 16 additions & 17 deletions src/Farmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ void Farmer::main()
{
if(reward_addr) {
if(*reward_addr == addr_t()) {
throw std::logic_error("reward_addr == zero");
reward_addr = nullptr;
} else {
log(INFO) << "Reward address: " << reward_addr->to_string();
}
log(INFO) << "Reward address: " << reward_addr->to_string();
}
params = get_params();

Expand Down Expand Up @@ -107,22 +108,20 @@ void Farmer::update()

if(!reward_addr) {
wallet->get_all_accounts(
[this](const std::map<uint32_t, account_t>& accounts) {
if(accounts.empty()) {
log(WARN) << "Failed to get reward address from wallet: no wallet available";
return;
}
wallet->get_address(accounts.begin()->first, 0,
[this](const addr_t& address) {
if(!reward_addr) {
log(INFO) << "Reward address: " << address.to_string();
[this](const std::vector<account_info_t>& accounts) {
if(!reward_addr) {
for(const auto& entry : accounts) {
if(entry.address) {
reward_addr = entry.address;
break;
}
reward_addr = address;
},
[this](const vnx::exception& ex) {
log(WARN) << "Failed to get reward address from wallet: " << ex.what();
});

}
if(reward_addr) {
log(INFO) << "Reward address: " << reward_addr->to_string();
} else {
log(WARN) << "Failed to get reward address from wallet: no wallet available";
}
}
},
[this](const vnx::exception& ex) {
log(WARN) << "Failed to get reward address from wallet: " << ex.what();
Expand Down
14 changes: 9 additions & 5 deletions src/TimeLord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ void TimeLord::main()
if(!reward_addr) {
try {
WalletClient wallet(wallet_server);
const auto accounts = wallet.get_all_accounts();
if(accounts.empty()) {
for(const auto& entry : wallet.get_all_accounts()) {
if(entry.address) {
reward_addr = entry.address;
break;
}
}
if(!reward_addr) {
throw std::logic_error("no wallet available");
}
reward_addr = wallet.get_address(accounts.begin()->first, 0);
}
catch(const std::exception& ex) {
log(WARN) << "Failed to get reward address from wallet: " << ex.what();
Expand All @@ -52,7 +56,7 @@ void TimeLord::main()
enable_reward = false;
}
} else {
log(INFO) << "Rewards not enabled";
log(INFO) << "Reward is disabled";
}
{
vnx::File file(storage_path + "timelord_sk.dat");
Expand All @@ -78,7 +82,7 @@ void TimeLord::main()
}
}
timelord_key = pubkey_t::from_skey(timelord_sk);
log(INFO) << "Our Timelord Key: " << timelord_key;
log(DEBUG) << "Timelord Key: " << timelord_key;
}

set_timer_millis(10000, std::bind(&TimeLord::print_info, this));
Expand Down
11 changes: 6 additions & 5 deletions src/Wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,17 +822,18 @@ std::vector<std::pair<skey_t, pubkey_t>> Wallet::get_all_farmer_keys() const
return res;
}

account_t Wallet::get_account(const uint32_t& index) const
account_info_t Wallet::get_account(const uint32_t& index) const
{
return get_wallet(index)->config;
const auto wallet = get_wallet(index);
return account_info_t::make(index, wallet->find_address(0), wallet->config);
}

std::map<uint32_t, account_t> Wallet::get_all_accounts() const
std::vector<account_info_t> Wallet::get_all_accounts() const
{
std::map<uint32_t, account_t> res;
std::vector<account_info_t> res;
for(size_t i = 0; i < wallets.size(); ++i) {
if(auto wallet = wallets[i]) {
res[i] = wallet->config;
res.push_back(account_info_t::make(i, wallet->find_address(0), wallet->config));
}
}
return res;
Expand Down
31 changes: 30 additions & 1 deletion src/WebAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,15 @@ vnx::Variant render_value(const T& value, std::shared_ptr<const RenderContext> c
return std::move(visitor.result);
}

template<typename T>
vnx::Variant render_list(const std::vector<T>& list, std::shared_ptr<const RenderContext> context = nullptr) {
std::vector<vnx::Object> tmp;
for(const auto& entry : list) {
tmp.push_back(render(entry, context));
}
return vnx::Variant(tmp);
}

template<typename T>
vnx::Object render_object(const T& value, std::shared_ptr<const RenderContext> context = nullptr) {
return render_value(value, context).to_object();
Expand Down Expand Up @@ -1654,6 +1663,26 @@ void WebAPI::http_request_async(std::shared_ptr<const vnx::addons::HttpRequest>
respond_status(request_id, 404, "wallet/seed?index");
}
}
else if(sub_path == "/wallet/accounts") {
wallet->get_all_accounts(
[this, request_id](const std::vector<account_info_t>& list) {
respond(request_id, render_list(list));
},
std::bind(&WebAPI::respond_ex, this, request_id, std::placeholders::_1));
}
else if(sub_path == "/wallet/account") {
const auto iter_index = query.find("index");
if(iter_index != query.end()) {
const uint32_t index = vnx::from_string<int64_t>(iter_index->second);
wallet->get_account(index,
[this, request_id](const account_info_t& info) {
respond(request_id, render(info));
},
std::bind(&WebAPI::respond_ex, this, request_id, std::placeholders::_1));
} else {
respond_status(request_id, 404, "wallet/account?index");
}
}
else if(sub_path == "/wallet/balance") {
const auto iter_index = query.find("index");
const auto iter_currency = query.find("currency");
Expand Down Expand Up @@ -2551,7 +2580,7 @@ void WebAPI::http_request_async(std::shared_ptr<const vnx::addons::HttpRequest>
"address/history", "wallet/balance", "wallet/contracts", "wallet/address", "wallet/coins",
"wallet/history", "wallet/history/memo", "wallet/send", "wallet/cancel_offer", "wallet/accept_offer", "wallet/offer_withdraw", "wallet/offer_trade",
"wallet/swap/liquid", "wallet/swap/trade", "wallet/swap/add_liquid", "wallet/swap/rem_liquid", "wallet/swap/payout",
"wallet/swap/switch_pool", "wallet/swap/rem_all_liquid",
"wallet/swap/switch_pool", "wallet/swap/rem_all_liquid", "wallet/accounts", "wallet/account",
"swap/list", "swap/info", "swap/user_info", "swap/trade_estimate",
"farmer/info", "farmer/blocks", "farmer/blocks/summary", "farmer/proofs",
"node/offers", "node/offer", "node/trade_history",
Expand Down
23 changes: 23 additions & 0 deletions src/account_info_t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* account_info_t.cpp
*
* Created on: Jun 28, 2024
* Author: mad
*/

#include <mmx/account_info_t.hxx>


namespace mmx {

account_info_t account_info_t::make(const uint32_t& account, const vnx::optional<addr_t>& address, const account_t& config)
{
account_info_t out;
static_cast<account_t&>(out) = config;
out.account = account;
out.address = address;
return out;
}


} // mmx
15 changes: 9 additions & 6 deletions src/mmx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,11 @@ int main(int argc, char** argv)
} catch(...) {
// ignore
}
const auto accounts = wallet.get_all_accounts();
if(index == 0 && !accounts.empty() && accounts.find(index) == accounts.end()) {
index = accounts.begin()->first;
if(index == 0) {
const auto accounts = wallet.get_all_accounts();
if(!accounts.empty()) {
index = accounts[0].account;
}
}
}

Expand Down Expand Up @@ -452,9 +454,10 @@ int main(int argc, char** argv)
else if(command == "accounts")
{
for(const auto& entry : wallet.get_all_accounts()) {
const auto& config = entry.second;
std::cout << "[" << entry.first << "] name = '" << config.name << "', index = " << config.index
<< ", num_addresses = " << config.num_addresses << ", key_file = '" << config.key_file << "'" << std::endl;
std::cout << "[" << entry.account << "] name = '" << entry.name << "', index = " << entry.index
<< ", passphrase = " << (entry.with_passphrase ? "yes" : "no")
<< ", num_addresses = " << entry.num_addresses << ", key_file = " << entry.key_file
<< " (" << vnx::to_string(entry.address) << ")" << std::endl;
}
}
else if(command == "keys")
Expand Down
10 changes: 5 additions & 5 deletions www/web-gui/public/market.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Vue.component('market-menu', {
methods: {
update() {
this.loading = true;
fetch('/api/wallet/get_all_accounts')
fetch('/wapi/wallet/accounts')
.then(response => response.json())
.then(data => {
this.loading = false;
Expand All @@ -27,7 +27,7 @@ Vue.component('market-menu', {
this.wallet = this.wallet_;
}
else if(data.length > 0) {
this.wallet = data[0][0];
this.wallet = data[0].account;
}
})
.catch(error => {
Expand Down Expand Up @@ -108,10 +108,10 @@ Vue.component('market-menu', {
v-model="wallet"
:items="wallets"
:lablel="$t('market_menu.wallet')"
item-text="[0]"
item-value="[0]">
item-text="account"
item-value="account">
<template v-for="slotName in ['item', 'selection']" v-slot:[slotName]="{ item }">
{{ $t('market_menu.wallet') }} #{{item[0]}}
{{ $t('market_menu.wallet') }} #{{item.account}} ({{item.address}})
</template>
</v-select>
Expand Down
Loading

0 comments on commit bc52c8c

Please sign in to comment.