1
0
forked from cheng/wallet
wallet/display_wallet.cpp
Cheng 5a00115c56
construction of new wallet files is now working
albeit they are dud wallet files, delete is not
recording the delete.

Something incomprehensible is happening with tracking the last file.

ui is useless for any useful purpose, it exists only so that
we have some ui that can be fixed up later to actually work
ui fails to track added names,
names are wrongly displayed in order added.
2022-05-13 15:31:48 +10:00

106 lines
4.5 KiB
C++

#include "stdafx.h"
using ro::base58;
display_wallet::display_wallet(wxWindow* parent, wxFileName& walletfile) :
wxPanel(parent, myID_WALLET_UI, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("Wallet")),
m_db(nullptr),
m_menuitem_close(this, &display_wallet::close_menu_event_handler),
m_menuitem_add_name(this, &display_wallet::add_name_event_handler)
{
wxLogMessage(_T("Loading %s"), walletfile.GetFullPath());
if (!walletfile.IsOk() || !walletfile.HasName() || !walletfile.HasExt()) throw MyException("unexpected file name");
if (!walletfile.FileExists())throw MyException(
walletfile.GetFullPath().append(" does not exist.").ToUTF8()
);
m_db.reset(Sqlite3_open(walletfile.GetFullPath().ToUTF8()));
sql_read_from_misc read_from_misc(m_db);
if (!read_from_misc(1) || read_from_misc.value<int64_t>() != WALLET_FILE_IDENTIFIER)throw MyException(sz_unrecognizable_wallet_file_format);
if (!read_from_misc(2) || read_from_misc.value<int64_t>() != WALLET_FILE_SCHEMA_VERSION_0_0 || !read_from_misc(4))throw MyException(sz_unrecognized_wallet_schema);
read_from_misc.read(m_MasterSecret);
if (!m_MasterSecret.valid()) throw MyException(sz_cold_wallets_not_yet_implemented);
auto sizer = new wxBoxSizer(wxHORIZONTAL);
m_lSizer = new wxBoxSizer(wxVERTICAL);
m_rSizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(m_lSizer,0, wxGROW, 4);
sizer->Add(m_rSizer, 50, wxGROW, 4);
SetSizer(sizer);
ro::sql read_keys(m_db, R"|(SELECT * FROM "Keys";)|");
sql_read_name read_name(m_db); //*It would be better to have a select statement goes through the name table, in name order. This is unit test code wrongly repurposed.
// m_db.reset(nullptr);// Force error of premature destruction of Isqlite3
while (read_keys.step() == Icompiled_sql::ROW) {
auto pubkey = read_keys.column<ristretto255::point>(0);
auto id = read_keys.column<int>(1);
auto use = read_keys.column<int>(2);
if (use != 1)throw MyException(sz_unknown_secret_key_algorithm);
if (!read_name(id)) throw MyException(sz_no_corresponding_entry);
const char* name = read_name.name();
if (m_MasterSecret(name).timesBase() != *pubkey)throw MyException(std::string(sz_public_key_of) + name + sz_fails_to_correspond);
m_lSizer->Add(
new wxStaticText(
this,
wxID_ANY,
name,
wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT|wxST_ELLIPSIZE_END
),
10,
wxEXPAND | // make horizontally stretchable
wxALL, // and make border all around
2);
m_rSizer->Add(
new wxStaticText(
this,
wxID_ANY,
"#" + base58(*pubkey).operator std::string(),
wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT | wxST_ELLIPSIZE_END
),
10,
wxEXPAND | // make horizontally stretchable
wxALL, // and make border all around
2);
}
Bind(wxEVT_CLOSE_WINDOW, &display_wallet::OnClose, this);
this->SetSize(this->GetParent()->GetClientSize());
singletonFrame->m_LastUsedSqlite.Assign(walletfile);
wxMenu* menuFile{ singletonFrame->GetMenuBar()->GetMenu(0) };
m_menuitem_close.Insert(menuFile, 1, "close", "close wallet");
singletonFrame->GetMenuBar()->EnableTop(1, true); //enable edit menu.
wxMenu* menuEdit{ singletonFrame->GetMenuBar()->GetMenu(1) };
m_menuitem_add_name.Insert(menuEdit, 0, "add name", "create new Zooko identity");
}
display_wallet::~display_wallet() {
assert(true);
singletonFrame->GetMenuBar()->EnableTop(1, false); //disable edit menu.
}
void display_wallet::close_menu_event_handler(wxCommandEvent& event) {
Close(true);
}
void display_wallet::add_name_event_handler(wxCommandEvent& event) {
wxTextEntryDialog dialog(this,
"This is a small sample\n"
"A long, long string to test out the text entrybox",
"Please enter a string",
"Default value",
wxOK | wxCANCEL);
if (dialog.ShowModal() == wxID_OK)
{
wxMessageBox(dialog.GetValue(), "Got string", wxOK | wxICON_INFORMATION, this);
}
sql_insert_name insert_name(m_db);
insert_name(dialog.GetValue().ToUTF8(), m_MasterSecret(dialog.GetValue().ToUTF8()).timesBase());
}
void display_wallet::OnClose(wxCloseEvent& event) {
// This event gives you the opportunity to clean up anything that needs explicit cleanup, albeit if you have done your work right nothing should need explicit cleanup,
// and to object to the closing in a "file not saved" type situation.
// https://docs.wxwidgets.org/trunk/classwx_close_event.html
DestroyChildren();
Destroy(); //Default handler will destroy the window. This is our handler for the user calling close,
// replacing the default handler.'
if (singletonFrame->m_panel ==this)singletonFrame->m_panel = nullptr;
}