diff --git a/CMakeLists.txt b/CMakeLists.txt index ff79fc6..d4bef73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,22 +3,16 @@ list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake") cmake_minimum_required(VERSION 3.4.1) if (WIN32) -# "'sleep_for' is not a member of 'std::this_thread'" in gcc 4.7/4.8 -add_definitions( "-D_GLIBCXX_USE_NANOSLEEP=1" ) -add_definitions( "-DWIN32_LEAN_AND_MEAN" ) -endif() - -#target_compile_options(gmock-matchers_test PRIVATE "-Wa,-mbig-obj") - - -#SET(CMAKE_C_COMPILER /path/to/c/compiler) -#SET(CMAKE_CXX_COMPILER /path/to/cpp/compiler) -if (WIN32) + add_definitions( "-D_GLIBCXX_USE_NANOSLEEP=1" ) # "'sleep_for' is not a member of 'std::this_thread'" in gcc 4.7/4.8 + add_definitions( "-DWIN32_LEAN_AND_MEAN" ) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj -O2 -fPIC -std=c++14 -F/Library/Frameworks -pthread -lcrypto -lcrypt32") else() SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++14 -F/Library/Frameworks -pthread") endif() -#SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-fsanitize=address") + +#SET(CMAKE_C_COMPILER /path/to/c/compiler) +#SET(CMAKE_CXX_COMPILER /path/to/cpp/compiler) +#SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address") #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++14 -F/Library/Frameworks -pthread -fsanitize=address") # TODO: way to enable sanitize? get runtime error with this on project(MoneroCppLibrary) @@ -115,7 +109,6 @@ find_package(OpenSSL REQUIRED) message(STATUS "Using OpenSSL include dir at ${OPENSSL_INCLUDE_DIR}") include_directories(${OPENSSL_INCLUDE_DIR}) - if(STATIC AND NOT IOS) if(UNIX) set(OPENSSL_LIBRARIES "${OPENSSL_LIBRARIES};${CMAKE_DL_LIBS};${CMAKE_THREAD_LIBS_INIT}") @@ -126,7 +119,6 @@ if (WIN32) list(APPEND OPENSSL_LIBRARIES ws2_32 crypt32) endif() - ############ # libsodium ############ @@ -146,16 +138,6 @@ if(APPLE) endif() endif() -if (APPLE AND NOT IOS) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=x86-64 -fvisibility=default -std=c++11") - if (NOT OPENSSL_ROOT_DIR) - EXECUTE_PROCESS(COMMAND brew --prefix openssl - OUTPUT_VARIABLE OPENSSL_ROOT_DIR - OUTPUT_STRIP_TRAILING_WHITESPACE) - message(STATUS "Using OpenSSL found at ${OPENSSL_ROOT_DIR}") - endif() -endif() - find_package(HIDAPI REQUIRED) message(STATUS "Using HIDAPI include dir at ${HIDAPI_INCLUDE_DIR}") add_definitions(-DHAVE_HIDAPI) @@ -192,7 +174,6 @@ set_target_properties(epee PROPERTIES IMPORTED_LOCATION # Unbound find_package(Unbound) - if(NOT UNBOUND_INCLUDE_DIR) die("Could not find libunbound") else() @@ -201,9 +182,9 @@ else() message(STATUS "Found libunbound library") include_directories(${UNBOUND_INCLUDE_DIR}) if (WIN32) - add_library(unbound STATIC IMPORTED) + add_library(unbound STATIC IMPORTED) else() - add_library(unbound SHARED IMPORTED) + add_library(unbound SHARED IMPORTED) endif() set_target_properties(unbound PROPERTIES IMPORTED_LOCATION ${UNBOUND_LIBRARIES}) else() @@ -211,7 +192,6 @@ else() endif() endif() - add_library(rpc_base STATIC IMPORTED) set_target_properties(rpc_base PROPERTIES IMPORTED_LOCATION ${MONERO_PROJECT_BUILD}/src/rpc/librpc_base.a) @@ -312,62 +292,62 @@ set( ) if (BUILD_LIBRARY) - add_library(monero-cpp SHARED ${LIBRARY_SRC_FILES}) - - target_link_libraries(monero-cpp - wallet - rpc_base - net - lmdb - unbound - easylogging - cryptonote_core - cryptonote_protocol - cryptonote_basic - cryptonote_format_utils_basic - mnemonics - ringct - ringct_basic - common - cncrypto - blockchain_db - blocks - checkpoints - device - device_trezor - multisig - version - randomx - epee - hardforks - cncrypto - ${wallet_crypto} - - ${UNBOUND_LIBRARIES} - ${Boost_LIBRARIES} - ${Protobuf_LIBRARY} - ${LibUSB_LIBRARIES} - ${OPENSSL_LIBRARIES} - ${SODIUM_LIBRARY} - ${HIDAPI_LIBRARIES} - ${EXTRA_LIBRARIES} - ) + add_library(monero-cpp SHARED ${LIBRARY_SRC_FILES}) + + target_link_libraries(monero-cpp + wallet + rpc_base + net + lmdb + unbound + easylogging + cryptonote_core + cryptonote_protocol + cryptonote_basic + cryptonote_format_utils_basic + mnemonics + ringct + ringct_basic + common + cncrypto + blockchain_db + blocks + checkpoints + device + device_trezor + multisig + version + randomx + epee + hardforks + cncrypto + ${wallet_crypto} + + ${UNBOUND_LIBRARIES} + ${Boost_LIBRARIES} + ${Protobuf_LIBRARY} + ${LibUSB_LIBRARIES} + ${OPENSSL_LIBRARIES} + ${SODIUM_LIBRARY} + ${HIDAPI_LIBRARIES} + ${EXTRA_LIBRARIES} + ) - INSTALL(FILES src/daemon/monero_daemon.h - src/daemon/monero_daemon_model.h - DESTINATION include/daemon) - INSTALL(FILES src/utils/gen_utils.h - src/utils/monero_utils.h - DESTINATION include/utils) - INSTALL(FILES src/wallet/monero_wallet_full.h - src/wallet/monero_wallet.h - src/wallet/monero_wallet_keys.h - src/wallet/monero_wallet_model.h - DESTINATION include/wallet) - INSTALL(TARGETS monero-cpp - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT Runtime - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Runtime - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Development) + INSTALL(FILES src/daemon/monero_daemon.h + src/daemon/monero_daemon_model.h + DESTINATION include/daemon) + INSTALL(FILES src/utils/gen_utils.h + src/utils/monero_utils.h + DESTINATION include/utils) + INSTALL(FILES src/wallet/monero_wallet_full.h + src/wallet/monero_wallet.h + src/wallet/monero_wallet_keys.h + src/wallet/monero_wallet_model.h + DESTINATION include/wallet) + INSTALL(TARGETS monero-cpp + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT Runtime + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Runtime + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Development) endif() ######################## @@ -375,51 +355,50 @@ endif() ######################## if (BUILD_SAMPLE) - set(SAMPLE_CODE_SRC_FILES test/sample_code.cpp) - - add_executable(sample_code ${LIBRARY_SRC_FILES} ${SAMPLE_CODE_SRC_FILES}) - - target_link_libraries(sample_code - wallet - rpc_base - net - lmdb - easylogging - cryptonote_core - cryptonote_protocol - cryptonote_basic - cryptonote_format_utils_basic - mnemonics - ringct - ringct_basic - common - cncrypto - blockchain_db - blocks - checkpoints - device - device_trezor - multisig - version - randomx - epee - hardforks - cncrypto - ${wallet_crypto} + set(SAMPLE_CODE_SRC_FILES test/sample_code.cpp) + + add_executable(sample_code ${LIBRARY_SRC_FILES} ${SAMPLE_CODE_SRC_FILES}) + + target_link_libraries(sample_code + wallet + rpc_base + net + lmdb + easylogging + cryptonote_core + cryptonote_protocol + cryptonote_basic + cryptonote_format_utils_basic + mnemonics + ringct + ringct_basic + common + cncrypto + blockchain_db + blocks + checkpoints + device + device_trezor + multisig + version + randomx + epee + hardforks + cncrypto + ${wallet_crypto} - ${UNBOUND_LIBRARIES} - ${Boost_LIBRARIES} - ${Protobuf_LIBRARY} - ${LibUSB_LIBRARIES} - ${OPENSSL_LIBRARIES} - ${SODIUM_LIBRARY} - ${HIDAPI_LIBRARIES} - ${EXTRA_LIBRARIES} - - ) - if (NOT WIN32) - target_link_libraries(sample_code dl) - endif() + ${UNBOUND_LIBRARIES} + ${Boost_LIBRARIES} + ${Protobuf_LIBRARY} + ${LibUSB_LIBRARIES} + ${OPENSSL_LIBRARIES} + ${SODIUM_LIBRARY} + ${HIDAPI_LIBRARIES} + ${EXTRA_LIBRARIES} + ) + if (NOT WIN32) + target_link_libraries(sample_code dl) + endif() endif() ######################## @@ -427,49 +406,48 @@ endif() ######################## if (BUILD_SCRATCHPAD) - set(SCRATCHPAD_SRC_FILES test/scratchpad.cpp) - - add_executable(scratchpad ${LIBRARY_SRC_FILES} ${SCRATCHPAD_SRC_FILES}) - - target_link_libraries(scratchpad - wallet - rpc_base - net - lmdb - easylogging - cryptonote_core - cryptonote_protocol - cryptonote_basic - cryptonote_format_utils_basic - mnemonics - ringct - ringct_basic - common - cncrypto - blockchain_db - blocks - checkpoints - device - device_trezor - multisig - version - randomx - epee - hardforks - cncrypto - ${wallet_crypto} - - ${UNBOUND_LIBRARIES} - ${Boost_LIBRARIES} - ${Protobuf_LIBRARY} - ${LibUSB_LIBRARIES} - ${OPENSSL_LIBRARIES} - ${SODIUM_LIBRARY} - ${HIDAPI_LIBRARIES} - ${EXTRA_LIBRARIES} - ) - if (NOT WIN32) - target_link_libraries(sample_code dl) - endif() - + set(SCRATCHPAD_SRC_FILES test/scratchpad.cpp) + + add_executable(scratchpad ${LIBRARY_SRC_FILES} ${SCRATCHPAD_SRC_FILES}) + + target_link_libraries(scratchpad + wallet + rpc_base + net + lmdb + easylogging + cryptonote_core + cryptonote_protocol + cryptonote_basic + cryptonote_format_utils_basic + mnemonics + ringct + ringct_basic + common + cncrypto + blockchain_db + blocks + checkpoints + device + device_trezor + multisig + version + randomx + epee + hardforks + cncrypto + ${wallet_crypto} + + ${UNBOUND_LIBRARIES} + ${Boost_LIBRARIES} + ${Protobuf_LIBRARY} + ${LibUSB_LIBRARIES} + ${OPENSSL_LIBRARIES} + ${SODIUM_LIBRARY} + ${HIDAPI_LIBRARIES} + ${EXTRA_LIBRARIES} + ) + if (NOT WIN32) + target_link_libraries(scratchpad dl) + endif() endif() diff --git a/README.md b/README.md index 499a700..3552855 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,8 @@ This project may be compiled as part of another application or built as a shared For example, [monero-java](https://github.com/monero-ecosystem/monero-java) compiles this project to a shared library to support Java JNI bindings, while [monero-javascript](https://github.com/monero-ecosystem/monero-javascript) compiles this project to WebAssembly binaries. +### Building monero-cpp on Mac & Linux + 1. If building this library standalone instead of as a submodule in another project, clone the project repository and update its submodules: 1. `git clone --recurse-submodules https://github.com/monero-ecosystem/monero-cpp.git` 2. `cd ./monero-cpp && ./bin/update_submodules.sh` @@ -173,75 +175,67 @@ For example, [monero-java](https://github.com/monero-ecosystem/monero-java) comp 1. `sudo apt update && sudo apt install build-essential cmake pkg-config libssl-dev libzmq3-dev libsodium-dev libunwind8-dev liblzma-dev libreadline6-dev libpgm-dev qttools5-dev-tools libhidapi-dev libusb-1.0-0-dev libprotobuf-dev protobuf-compiler libudev-dev libboost-chrono-dev libboost-date-time-dev libboost-filesystem-dev libboost-locale-dev libboost-program-options-dev libboost-regex-dev libboost-serialization-dev libboost-system-dev libboost-thread-dev python3 ccache` 2. Install expat (dependency of unbound): - ``` - cd ~ - wget https://github.com/libexpat/libexpat/releases/download/R_2_4_8/expat-2.4.8.tar.bz2 - tar -xf expat-2.4.8.tar.bz2 - rm expat-2.4.8.tar.bz2 - cd expat-2.4.8 - ./configure --enable-static --disable-shared - make - sudo make install - cd ../ - ``` - 3. Install unbound: + ``` + cd ~ + wget https://github.com/libexpat/libexpat/releases/download/R_2_4_8/expat-2.4.8.tar.bz2 + tar -xf expat-2.4.8.tar.bz2 + sudo rm expat-2.4.8.tar.bz2 + cd expat-2.4.8 + ./configure --enable-static --disable-shared + make + sudo make install + cd ../ + ``` + 3. Install unbound: - ``` - cd ~ - wget https://www.nlnetlabs.nl/downloads/unbound/unbound-1.17.0.tar.gz - tar xzf unbound-1.17.0.tar.gz - sudo apt update - sudo apt install -y build-essential - sudo apt install -y libssl-dev - sudo apt install -y libexpat1-dev - sudo apt-get install -y bison - sudo apt-get install -y flex - cd unbound-1.17.0 - ./configure --with-libexpat=/usr --with-ssl=/usr - make - sudo make install - cd ../ - ``` + ``` + cd ~ + wget https://www.nlnetlabs.nl/downloads/unbound/unbound-1.17.0.tar.gz + tar xzf unbound-1.17.0.tar.gz + sudo apt update + sudo apt install -y build-essential + sudo apt install -y libssl-dev + sudo apt install -y libexpat1-dev + sudo apt-get install -y bison + sudo apt-get install -y flex + cd unbound-1.17.0 + ./configure --with-libexpat=/usr --with-ssl=/usr + make + sudo make install + cd ../ + ``` 3. `export MONERO_CPP=path/to/monero-cpp` 4. `cd $MONERO_CPP/external/monero-project` 5. Build monero-project to create .a libraries, e.g.: `make release-static -j8` 6. Link to this library's source files in your application or build as a shared library in ./build/: `cd $MONERO_CPP && ./bin/build_libmonero_cpp.sh` -## Instructions for building libmonero-cpp.dll on Windows +### Building libmonero-cpp.dll on Windows 1. Download and install [MSYS2](https://www.msys2.org/). -2. Press windows button and type ```MSYS2 MINGW64``` for 64 bit systems or ```MSYS2 MINGW32``` for 32 bit. -3. Update packages: - ```pacman -Syu``` -4. Install dependencies - For 64 bit - ``` - pacman -S mingw-w64-x86_64-toolchain make mingw-w64-x86_64-cmake mingw-w64-x86_64-boost mingw-w64-x86_64-openssl mingw-w64-x86_64-zeromq mingw-w64-x86_64-libsodium mingw-w64-x86_64-hidapi mingw-w64-x86_64-unbound mingw-w64-x86_64-protobuf git mingw-w64-x86_64-libusb - ``` - For 32 bit - ``` - pacman -S mingw-w64-i686-toolchain make mingw-w64-i686-cmake mingw-w64-i686-boost mingw-w64-i686-openssl mingw-w64-i686-zeromq mingw-w64-i686-libsodium mingw-w64-i686-hidapi mingw-w64-i686-unbound mingw-w64-i686-protobuf git mingw-w64-i686-libusb - ``` -5. Clone repo - ``` - git clone --recurse-submodules https://github.com/monero-ecosystem/monero-cpp.git - ``` -6. Update submodules - ``` - cd ./monero-cpp && ./bin/update_submodules.sh - ``` -7. ```export MONERO_CPP=path/to/monero-cpp``` -8. ```cd $MONERO_CPP/external/monero-project``` -9. Build monero-project for 64 bit - ``` - make release-static-win64 - ``` - or for 32 bit - ``` - make release-static-win32 - ``` -10. Build as a shared library in ./build/: -```cd $MONERO_CPP && ./bin/build_libmonero_cpp_dll.sh``` +2. Press the Windows button and launch `MSYS2 MINGW64` for 64 bit systems or `MSYS2 MINGW32` for 32 bit. +3. Update packages: `pacman -Syu` and `pacman -Syy` +4. Relaunch MSYS2 (if necessary) and install dependencies: + + For 64 bit: + + ``` + pacman -S mingw-w64-x86_64-toolchain make mingw-w64-x86_64-cmake mingw-w64-x86_64-boost mingw-w64-x86_64-openssl mingw-w64-x86_64-zeromq mingw-w64-x86_64-libsodium mingw-w64-x86_64-hidapi mingw-w64-x86_64-unbound mingw-w64-x86_64-protobuf git mingw-w64-x86_64-libusb make gettext base-devel + ``` + + For 32 bit: + + ``` + pacman -S mingw-w64-i686-toolchain make mingw-w64-i686-cmake mingw-w64-i686-boost mingw-w64-i686-openssl mingw-w64-i686-zeromq mingw-w64-i686-libsodium mingw-w64-i686-hidapi mingw-w64-i686-unbound mingw-w64-i686-protobuf git mingw-w64-i686-libusb make gettext base-devel + ``` +5. Clone repo if installing standalone (skip if building as part of another repo like monero-java or monero-javascript): `git clone --recursive https://github.com/monero-ecosystem/monero-cpp.git` +6. Update submodules: `cd && ./bin/update_submodules.sh` +7. `cd ./external/monero-project` +9. Build monero-project: + + For 64 bit: `make release-static-win64` + + For 32 bit: `make release-static-win32` +10. Build as a shared library in ./build/: `cd ../../ && ./bin/build_libmonero_cpp_dll.sh` ## Related projects diff --git a/src/wallet/monero_wallet_full.cpp b/src/wallet/monero_wallet_full.cpp index e09f29b..c554a72 100644 --- a/src/wallet/monero_wallet_full.cpp +++ b/src/wallet/monero_wallet_full.cpp @@ -44,7 +44,7 @@ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * strict LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers @@ -78,7 +78,7 @@ namespace monero { // ------------------------- INITIALIZE CONSTANTS --------------------------- static const int DEFAULT_CONNECTION_TIMEOUT_MILLIS = 1000 * 30; // default connection timeout 30 sec - static const bool strict = false; // relies exclusively on blockchain data if true, includes local wallet data if false TODO: good use case to expose externally? + static const bool STRICT_ = false; // relies exclusively on blockchain data if true, includes local wallet data if false TODO: good use case to expose externally? (note: cannot use `STRICT` due to namespace collision on Windows) // ----------------------- INTERNAL PRIVATE HELPERS ----------------------- @@ -1590,29 +1590,29 @@ namespace monero { // isMultisigImportNeeded uint64_t monero_wallet_full::get_balance() const { - return m_w2->balance_all(strict); + return m_w2->balance_all(STRICT_); } uint64_t monero_wallet_full::get_balance(uint32_t account_idx) const { - return m_w2->balance(account_idx, strict); + return m_w2->balance(account_idx, STRICT_); } uint64_t monero_wallet_full::get_balance(uint32_t account_idx, uint32_t subaddress_idx) const { - std::map balance_per_subaddress = m_w2->balance_per_subaddress(account_idx, strict); + std::map balance_per_subaddress = m_w2->balance_per_subaddress(account_idx, STRICT_); auto iter = balance_per_subaddress.find(subaddress_idx); return iter == balance_per_subaddress.end() ? 0 : iter->second; } uint64_t monero_wallet_full::get_unlocked_balance() const { - return m_w2->unlocked_balance_all(strict); + return m_w2->unlocked_balance_all(STRICT_); } uint64_t monero_wallet_full::get_unlocked_balance(uint32_t account_idx) const { - return m_w2->unlocked_balance(account_idx, strict); + return m_w2->unlocked_balance(account_idx, STRICT_); } uint64_t monero_wallet_full::get_unlocked_balance(uint32_t account_idx, uint32_t subaddress_idx) const { - std::map>> unlocked_balance_per_subaddress = m_w2->unlocked_balance_per_subaddress(account_idx, strict); + std::map>> unlocked_balance_per_subaddress = m_w2->unlocked_balance_per_subaddress(account_idx, STRICT_); auto iter = unlocked_balance_per_subaddress.find(subaddress_idx); return iter == unlocked_balance_per_subaddress.end() ? 0 : iter->second.first; } @@ -1630,8 +1630,8 @@ namespace monero { monero_account account; account.m_index = account_idx; account.m_primary_address = get_address(account_idx, 0); - account.m_balance = m_w2->balance(account_idx, strict); - account.m_unlocked_balance = m_w2->unlocked_balance(account_idx, strict); + account.m_balance = m_w2->balance(account_idx, STRICT_); + account.m_unlocked_balance = m_w2->unlocked_balance(account_idx, STRICT_); if (include_subaddresses) account.m_subaddresses = get_subaddresses_aux(account_idx, std::vector(), transfers); accounts.push_back(account); } @@ -1650,8 +1650,8 @@ namespace monero { monero_account account; account.m_index = account_idx; account.m_primary_address = get_address(account_idx, 0); - account.m_balance = m_w2->balance(account_idx, strict); - account.m_unlocked_balance = m_w2->unlocked_balance(account_idx, strict); + account.m_balance = m_w2->balance(account_idx, STRICT_); + account.m_unlocked_balance = m_w2->unlocked_balance(account_idx, STRICT_); if (include_subaddresses) account.m_subaddresses = get_subaddresses_aux(account_idx, std::vector(), transfers); return account; } @@ -3697,8 +3697,8 @@ namespace monero { std::vector subaddresses; // get balances per subaddress as maps - std::map balance_per_subaddress = m_w2->balance_per_subaddress(account_idx, strict); - std::map>> unlocked_balance_per_subaddress = m_w2->unlocked_balance_per_subaddress(account_idx, strict); + std::map balance_per_subaddress = m_w2->balance_per_subaddress(account_idx, STRICT_); + std::map>> unlocked_balance_per_subaddress = m_w2->unlocked_balance_per_subaddress(account_idx, STRICT_); // get all indices if no indices given std::vector subaddress_indices_req;