From c1ea0ef1fcfe503b37a6a61a96472fa3634231dc Mon Sep 17 00:00:00 2001 From: Neozaru Date: Sun, 15 Jun 2014 23:45:44 +0200 Subject: [PATCH] High level library draft for Wallets : Open, Generate --- CMakeLists.txt | 4 +- src/CMakeLists.txt | 12 ++- src/monero_wallet_wrapper/MoneroWallet.cc | 98 +++++++++++++++++++++++ src/monero_wallet_wrapper/MoneroWallet.hh | 74 +++++++++++++++++ src/wallet/wallet2.h | 1 - 5 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 src/monero_wallet_wrapper/MoneroWallet.cc create mode 100644 src/monero_wallet_wrapper/MoneroWallet.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f084ba22..cf4ee6bd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,8 +69,8 @@ else() else() set(STATIC_ASSERT_FLAG "-Dstatic_assert=_Static_assert") endif() - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_FLAG} ${WARNINGS} ${C_WARNINGS} ${ARCH_FLAG} -maes") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -D_GNU_SOURCE ${MINGW_FLAG} ${WARNINGS} ${CXX_WARNINGS} ${ARCH_FLAG} -maes") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_FLAG} ${WARNINGS} ${C_WARNINGS} ${ARCH_FLAG} -maes -fPIC") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -D_GNU_SOURCE ${MINGW_FLAG} ${WARNINGS} ${CXX_WARNINGS} ${ARCH_FLAG} -maes -fPIC") if(APPLE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGTEST_HAS_TR1_TUPLE=0") endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e4e682b99..f5075233b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,6 +11,7 @@ file(GLOB_RECURSE SIMPLEWALLET simplewallet/*) file(GLOB_RECURSE CONN_TOOL connectivity_tool/*) file(GLOB_RECURSE WALLET wallet/*) file(GLOB_RECURSE MINER miner/*) +file(GLOB_RECURSE MONERO_WALLET_WRAPPER monero_wallet_wrapper/*) source_group(common FILES ${COMMON}) source_group(crypto FILES ${CRYPTO}) @@ -23,6 +24,7 @@ source_group(simplewallet FILES ${SIMPLEWALLET}) source_group(connectivity-tool FILES ${CONN_TOOL}) source_group(wallet FILES ${WALLET}) source_group(simpleminer FILES ${MINER}) +source_group(monero_wallet_wrapper FILES ${MONERO_WALLET_WRAPPER}) add_library(common ${COMMON}) add_library(crypto ${CRYPTO}) @@ -41,6 +43,14 @@ add_dependencies(daemon version) add_dependencies(rpc version) add_dependencies(simplewallet version) -set_property(TARGET common crypto cryptonote_core rpc wallet PROPERTY FOLDER "libs") + +add_library(monerowallet SHARED ${MONERO_WALLET_WRAPPER}) +add_dependencies(monerowallet wallet) +target_link_libraries(monerowallet wallet cryptonote_core crypto common upnpc-static ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) + +#target_link_libraries(monerowallet wallet rpc cryptonote_core crypto common upnpc-static ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES}) + +set_property(TARGET common crypto cryptonote_core rpc wallet monerowallet PROPERTY FOLDER "libs") set_property(TARGET daemon simplewallet connectivity_tool simpleminer PROPERTY FOLDER "prog") set_property(TARGET daemon PROPERTY OUTPUT_NAME "bitmonerod") + diff --git a/src/monero_wallet_wrapper/MoneroWallet.cc b/src/monero_wallet_wrapper/MoneroWallet.cc new file mode 100644 index 000000000..f2621a857 --- /dev/null +++ b/src/monero_wallet_wrapper/MoneroWallet.cc @@ -0,0 +1,98 @@ +#include "MoneroWallet.hh" + + +#include + + +#include "wallet/wallet2.h" + +using namespace Monero; + +amount_t fromMini(amount_mini_t pAmountMini) { + return pAmountMini * pow(10,-12); +} + +amount_mini_t toMini(amount_t pAmount) { + return pAmount * pow(10,12); +} + +Wallet::Wallet(const std::string& pWalletFile, const std::string& pWalletPassword) + : wallet_impl(new tools::wallet2()) +{ + + try { + wallet_impl->load(pWalletFile, pWalletPassword); + } + catch(tools::error::invalid_password) { + throw(Errors::iInvalidPassword); + } + catch(tools::error::file_error_base<1>) { + throw(Errors::iInvalidFile); + } + // catch(tools::error::file_read_error) { + // throw(Errors::iInvalidFile); + // } + + +} + +Wallet::Wallet(tools::wallet2* pWalletImpl) + : wallet_impl(pWalletImpl) +{ + +} + +Wallet::~Wallet() { + wallet_impl->store(); + delete wallet_impl; +} + +const std::string Wallet::getAddress() const { + + return wallet_impl->get_account().get_public_address_str(); + +} + +amount_mini_t Wallet::getBalanceMini() const { + return wallet_impl->balance(); +} + +amount_mini_t Wallet::getUnlockedBalanceMini() const { + return wallet_impl->unlocked_balance(); + +} + +amount_t Wallet::getBalance() const { + return fromMini(getBalanceMini()); +} + +amount_t Wallet::getUnlockedBalance() const { + return fromMini(getUnlockedBalanceMini()); +} + + +bool Wallet::walletExists(const std::string pWalletFile, bool& oWallet_data_exists, bool& oWallet_keys_exist) { + + bool lDataExists = false; + bool lKeysExists = false; + tools::wallet2::wallet_exists(pWalletFile, lDataExists, lKeysExists); + + return lKeysExists; + +} + +Wallet Wallet::generateWallet(const std::string pWalletFile, const std::string& pWalletPassword) { + + try { + tools::wallet2* lWalletImpl = new tools::wallet2(); + lWalletImpl->generate(pWalletFile, pWalletPassword); + return Wallet(lWalletImpl); + } + catch(tools::error::file_save_error) { + throw(Errors::iNotWritableFile); + } + catch(tools::error::file_exists) { + throw(Errors::iNotWritableFile); + } + +} \ No newline at end of file diff --git a/src/monero_wallet_wrapper/MoneroWallet.hh b/src/monero_wallet_wrapper/MoneroWallet.hh new file mode 100644 index 000000000..dc36f71b0 --- /dev/null +++ b/src/monero_wallet_wrapper/MoneroWallet.hh @@ -0,0 +1,74 @@ +#pragma once + +#include +// #include + +namespace tools { class wallet2; } + +namespace Monero { + + + +typedef unsigned long long amount_mini_t; +typedef long double amount_t; + +namespace Errors { + + class InvalidPassword: public std::exception + { + public: + virtual const char* what() const throw() + { + return "Wrong wallet password"; + } + } iInvalidPassword; + + class InvalidFile: public std::exception + { + public: + virtual const char* what() const throw() + { + return "Wallet file not found or invalid"; + } + } iInvalidFile; + + class NotWritableFile: public std::exception + { + public: + virtual const char* what() const throw() + { + return "Unable to write file"; + } + + } iNotWritableFile; + +} + +class Wallet { + +public: + Wallet(const std::string& pWalletFile, const std::string& pWalletPassword); + ~Wallet(); + + /* Offline methods */ + const std::string getAddress() const; + + amount_mini_t getBalanceMini() const; + amount_mini_t getUnlockedBalanceMini() const; + + amount_t getBalance() const; + amount_t getUnlockedBalance() const; + /**/ + + static bool walletExists(const std::string pWalletFile, bool& oWallet_data_exists, bool& oWallet_keys_exist); + static Wallet generateWallet(const std::string pWalletFile, const std::string& pWalletPassword); + +private: + tools::wallet2* wallet_impl; + + Wallet(tools::wallet2* pWalletImpl); +}; + + + +} \ No newline at end of file diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 534a0517b..ecaa9f6e3 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -9,7 +9,6 @@ #include #include -#include "include_base_utils.h" #include "cryptonote_core/account.h" #include "cryptonote_core/account_boost_serialization.h" #include "cryptonote_core/cryptonote_basic_impl.h"