From bba593180e36f7356fa52cd64366eabf3363165a Mon Sep 17 00:00:00 2001 From: Cheng Date: Thu, 28 Sep 2023 22:14:29 +0000 Subject: [PATCH] displays names in alphabetic order sanity test of pubkey mysteriously fails in display_wallet, yet identical test with same values succeeds in unit_test need to create a view once sanity test passes. then need to refresh display on edit/add name need to make a second try at integrating release v3.2.2.1 --- src/ILog.cpp | 7 +++ src/ILog.h | 3 + src/display_wallet.cpp | 140 +++++++++++++++++++++++------------------ src/unit_test.cpp | 12 ++-- 4 files changed, 96 insertions(+), 66 deletions(-) diff --git a/src/ILog.cpp b/src/ILog.cpp index 6ef3132..c7ddca7 100644 --- a/src/ILog.cpp +++ b/src/ILog.cpp @@ -49,3 +49,10 @@ MyException::MyException(const char* sz, int i, const char* func__, const char* err = std::format(R"|({} line {}, function {}, file {})|", sz, i, func__, FILE__); } + +MyException::MyException(MyException e, int i, const char* func, const char* file) noexcept : + err(std::format(R"|({} +line {}, function {}, file {})|", e.what(), i, func, file)), err_number(e.what_num()) {} +MyException::MyException(std::exception e, int i, const char* func, const char* file) noexcept: + err(std::format(R"|({} +line {}, function {}, file {})|", e.what(), i, func, file)), err_number(i) {} \ No newline at end of file diff --git a/src/ILog.h b/src/ILog.h index 01bdc7d..7aea7cc 100644 --- a/src/ILog.h +++ b/src/ILog.h @@ -32,6 +32,9 @@ public: virtual const int what_num() const { return err_number; } + explicit MyException(MyException e, std::string str)noexcept : err(e.what() + str), err_number(e.what_num()) {} + explicit MyException(MyException e, int i, const char* func, const char* file) noexcept; + explicit MyException(std::exception e, int i, const char* func, const char* file) noexcept; }; class FatalException : public MyException { diff --git a/src/display_wallet.cpp b/src/display_wallet.cpp index 11a6efa..b10a09b 100644 --- a/src/display_wallet.cpp +++ b/src/display_wallet.cpp @@ -1,72 +1,92 @@ #include "stdafx.h" using ro::base58; +static constexpr char SrcFilename[]{ "src/display_wallet.cpp" }; display_wallet::display_wallet(wxWindow* parent, wxFileName& walletfile) : wxPanel(parent, myID_WALLET_UI, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, wxT("Wallet")), m_db(nullptr), m_menuitem_add_name(this, &display_wallet::add_name_event_handler) { wxLogMessage(wxT("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() != WALLET_FILE_IDENTIFIER)throw MyException(sz_unrecognizable_wallet_file_format); - if (!read_from_misc(2) || read_from_misc.value() != 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. - /* ro::sql sql_read_names( - m_db, - R"|(SELECT Names.name, Keys.pubkey FROM Names INNER JOIN Keys ON Names.ROWID=Keys.id AND Keys.use=1 ORDER BY Names.name;)|"){} */ - // m_db.reset(nullptr);// Force error of premature destruction of Isqlite3 - while (read_keys.step() == Icompiled_sql::ROW) { - auto pubkey = read_keys.column(1); - auto id = read_keys.column(2); - auto use = read_keys.column(3); - 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); + try { + if (!walletfile.IsOk() || !walletfile.HasName() || !walletfile.HasExt()) throw MyException("unexpected file name", __LINE__, __func__, SrcFilename); + if (!walletfile.FileExists())throw MyException( + walletfile.GetFullPath().append(" does not exist.").ToUTF8(), + __LINE__, __func__, SrcFilename); + 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() != WALLET_FILE_IDENTIFIER)throw MyException(sz_unrecognizable_wallet_file_format); + if (!read_from_misc(2) || read_from_misc.value() != WALLET_FILE_SCHEMA_VERSION_0_0 || !read_from_misc(4))throw MyException(sz_unrecognized_wallet_schema); + m_MasterSecret= *read_from_misc.value(); + ILogMessage(std::format("\t\tmaster secret: #{}", ro::base58(m_MasterSecret).operator const char* ()).c_str()); + if(!m_MasterSecret.valid()) throw MyException(sz_cold_wallets_not_yet_implemented, __LINE__, __func__, SrcFilename); + 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); + try { + ro::sql read_names_and_keys(m_db, + R"|(SELECT Names.name AS name, Keys.pubkey AS pubkey FROM Names INNER JOIN Keys ON Names.ROWID=Keys.id AND Keys.use=1 ORDER BY LOWER(name), name COLLATE NOCASE;)|"); + while (read_names_and_keys.step() == Icompiled_sql::ROW) { + std::string name = read_names_and_keys.column(0); + auto pubkey = *read_names_and_keys.column(1); +// 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); + } + } + catch (const MyException& e) { + throw MyException(e, __LINE__, __func__, SrcFilename); + // sql exceptions tend to be unintelligible and excessively generic + // because unclear what statement provoked them. + } + catch (const std::exception& e) { + throw MyException(e, __LINE__, __func__, SrcFilename); + } + catch (...) { + szError = sz_unknown_error; + throw MyException(sz_unknown_error, __LINE__, __func__, SrcFilename); + } + Bind(wxEVT_CLOSE_WINDOW, &display_wallet::OnClose, this); + this->SetSize(this->GetParent()->GetClientSize()); + singletonFrame->m_LastUsedSqlite.Assign(walletfile); - wxMenu* menuFile{ singletonFrame->GetMenuBar()->GetMenu(0) }; - 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"); + wxMenu* menuFile{ singletonFrame->GetMenuBar()->GetMenu(0) }; + 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"); + } + catch (const MyException&) { + throw; + } + catch (const std::exception& e) { + throw MyException(e, __LINE__, __func__, SrcFilename); + } + catch (...) { + szError = sz_unknown_error; + throw MyException(sz_unknown_error, __LINE__, __func__, SrcFilename); + } } diff --git a/src/unit_test.cpp b/src/unit_test.cpp index 989106c..8387083 100644 --- a/src/unit_test.cpp +++ b/src/unit_test.cpp @@ -241,9 +241,8 @@ static bool checkDataConversionsProduceExpected(void){ hash<256> hash_b{ hash_a,str_hash_a,scl_a,str_sclr_a,pt_a,str_pt_a,33, 66ull }; if (base58(hash_b).operator std::string() != "i22EVNPsKRjdxYTZrPPu9mx6vnrBjosFix5F4gn2mb2kF" - ){ - throw MyException("unexpected hash of transformations", __LINE__, __func__, SrcFilename); - } + ){ throw MyException("unexpected hash of transformations", __LINE__, __func__, SrcFilename); + } } catch (const MyException& e) { errorCode = e.what_num(); @@ -408,18 +407,19 @@ static bool OpenWallet(void) { if(!read_from_misc(2) || read_from_misc.value() != WALLET_FILE_SCHEMA_VERSION_0_0)throw MyException("Unrecognized wallet schema", __LINE__, __func__, SrcFilename); if (!read_from_misc(4)) throw MyException("Mastersecret missing", __LINE__, __func__, SrcFilename); ristretto255::CMasterSecret MasterSecret(*read_from_misc.value()); + ILogMessage(std::format("\t\tmaster secret: #{}", ro::base58(MasterSecret).operator const char *()).c_str()); ro::sql read_keys(db.get(), R"|(SELECT * FROM "Keys" LIMIT 5;)|"); sql_read_name read_name(db.get()); // db.reset(nullptr);// Force error of premature destruction of Isqlite3 while (read_keys.step() == Icompiled_sql::ROW) { - auto pubkey = read_keys.column(1); + auto pubkey = *read_keys.column(1); auto id = read_keys.column(2); auto use = read_keys.column(3); if (use != 1)throw MyException(sz_unknown_secret_key_algorithm, __LINE__, __func__, SrcFilename); if (!read_name(id)) throw MyException(sz_no_corresponding_entry, __LINE__, __func__, SrcFilename); const char* name = read_name.name(); - if(MasterSecret(name).timesBase()!=*pubkey)throw MyException(R"|(Public key of name fails to correspond)|", __LINE__, __func__, SrcFilename); - wxLogMessage(wxT("\t\t\"%s\" has expected public key 0x%s"), name, (wxString)(bin2hex(*pubkey))); + if(MasterSecret(name).timesBase()!=pubkey)throw MyException(R"|(Public key of name fails to correspond)|", __LINE__, __func__, SrcFilename); + wxLogMessage(wxT("\t\t\"%s\" has expected public key #%s"), name, (wxString)(ro::base58(pubkey).operator const char* ())); } } else {