From a41422528fb9d5ed56676b4296f34c6bbf046da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=80=D0=B5=D0=BF=D0=BE=D0=B4=D0=BE=D0=B1=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BD?= Date: Mon, 25 Apr 2022 13:42:44 +0300 Subject: [PATCH] Added Bitcoin client support. --- CMakeLists.txt | 2 +- cmake-modules/FindBitcoin-Client.cmake | 60 +++++++++++++++++++ src/modules/Workers/WebService/WebService.cpp | 51 ++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 cmake-modules/FindBitcoin-Client.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 44a325f..8404182 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ set(INSTALL_AS_ROOT ON CACHE BOOL "Install as root") set(WITH_POSTGRESQL OFF CACHE BOOL "Build with PostgreSQL") set(WITH_SQLITE3 OFF CACHE BOOL "Build with Sqlite3") set(WITH_CURL OFF CACHE BOOL "Build with cURL") -set(WITH_BITCOIN_CLIENT OFF CACHE BOOL "Build with libbitcoin-client") +set(WITH_BITCOIN_CLIENT ON CACHE BOOL "Build with libbitcoin-client") # ---------------------------------------------------------------------------------------------------------------------- if (INSTALL_AS_ROOT) diff --git a/cmake-modules/FindBitcoin-Client.cmake b/cmake-modules/FindBitcoin-Client.cmake new file mode 100644 index 0000000..0916194 --- /dev/null +++ b/cmake-modules/FindBitcoin-Client.cmake @@ -0,0 +1,60 @@ +############################################################################### +# Copyright (c) 2014-2019 libbitcoin-client developers (see COPYING). +# +# GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY +# +############################################################################### +# FindBitcoin-Client +# +# Use this module by invoking find_package with the form:: +# +# find_package( Bitcoin-Client +# [version] # Minimum version +# [REQUIRED] # Fail with error if bitcoin-client is not found +# ) +# +# Defines the following for use: +# +# bitcoin_client_FOUND - true if headers and requested libraries were found +# bitcoin_client_INCLUDE_DIRS - include directories for bitcoin-client libraries +# bitcoin_client_LIBRARY_DIRS - link directories for bitcoin-client libraries +# bitcoin_client_LIBRARIES - bitcoin-client libraries to be linked +# bitcoin_client_PKG - bitcoin-client pkg-config package specification. +# + +if (MSVC) + if ( Bitcoin-Client_FIND_REQUIRED ) + set( _bitcoin_client_MSG_STATUS "SEND_ERROR" ) + else () + set( _bitcoin_client_MSG_STATUS "STATUS" ) + endif() + + set( bitcoin_client_FOUND false ) + message( ${_bitcoin_client_MSG_STATUS} "MSVC environment detection for 'bitcoin-client' not currently supported." ) +else () + # required + if ( Bitcoin-Client_FIND_REQUIRED ) + set( _bitcoin_client_REQUIRED "REQUIRED" ) + endif() + + # quiet + if ( Bitcoin-Client_FIND_QUIETLY ) + set( _bitcoin_client_QUIET "QUIET" ) + endif() + + # modulespec + if ( Bitcoin-Client_FIND_VERSION_COUNT EQUAL 0 ) + set( _bitcoin_client_MODULE_SPEC "libbitcoin-client" ) + else () + if ( Bitcoin-Client_FIND_VERSION_EXACT ) + set( _bitcoin_client_MODULE_SPEC_OP "=" ) + else () + set( _bitcoin_client_MODULE_SPEC_OP ">=" ) + endif() + + set( _bitcoin_client_MODULE_SPEC "libbitcoin-client ${_bitcoin_client_MODULE_SPEC_OP} ${Bitcoin-Client_FIND_VERSION}" ) + endif() + + pkg_check_modules( bitcoin_client ${_bitcoin_client_REQUIRED} ${_bitcoin_client_QUIET} "${_bitcoin_client_MODULE_SPEC}" ) + set( bitcoin_client_PKG "${_bitcoin_client_MODULE_SPEC}" ) +endif() diff --git a/src/modules/Workers/WebService/WebService.cpp b/src/modules/Workers/WebService/WebService.cpp index 11ee955..1c48f0d 100644 --- a/src/modules/Workers/WebService/WebService.cpp +++ b/src/modules/Workers/WebService/WebService.cpp @@ -1384,6 +1384,57 @@ namespace Apostol { RouteDeal(AConnection, "GET", sRoute, caAction); + } else if (caCommand == "bc" && caAction == "history") { + + const auto& caAccount = pRequest->Params["account"]; + + if (caAccount.IsEmpty()) { + AConnection->SendStockReply(CHTTPReply::bad_request); + return; + } + + try { + const wallet::payment_address address(std::string(caAccount.c_str())); + + CJSON history; + fetch_history(address, history); + + pReply->Content = history.ToString(); + } catch (Delphi::Exception::Exception &E) { + ExceptionToJson(CHTTPReply::bad_request, E, pReply->Content); + Log()->Error(APP_LOG_EMERG, 0, E.what()); + } + + AConnection->SendReply(CHTTPReply::ok, nullptr, true); + + } else if (caCommand == "bc" && caAction == "header") { + + const auto& LHeight = pRequest->Params["height"]; + const auto& LHash = pRequest->Params["hash"]; + + if (LHeight.IsEmpty() && LHash.IsEmpty()) { + AConnection->SendStockReply(CHTTPReply::bad_request); + return; + } + + try { + CJSON header; + + if (!LHash.IsEmpty()) { + fetch_header(hash256(std::string(LHash.c_str())), header); + } else { + uint32_t height = StrToInt(LHeight.c_str()); + fetch_header(height, header); + } + + pReply->Content = header.ToString(); + } catch (Delphi::Exception::Exception &E) { + ExceptionToJson(CHTTPReply::bad_request, E, pReply->Content); + Log()->Error(APP_LOG_EMERG, 0, E.what()); + } + + AConnection->SendReply(CHTTPReply::ok, nullptr, true); + } else { AConnection->SendStockReply(CHTTPReply::not_found);