From ef0eb95a166f2b85fd8d441189718ea1d8ad6445 Mon Sep 17 00:00:00 2001 From: anatol Date: Mon, 10 Jul 2023 15:00:54 +0300 Subject: [PATCH] added sbbs methods to the wallet api --- wallet/api/CMakeLists.txt | 3 + wallet/api/base/parse_utils.h | 23 ++++++++ wallet/api/i_wallet_api.cpp | 8 +++ wallet/api/i_wallet_api.h | 8 +-- wallet/api/v7_4/api7_4.rest | 44 ++++++++++++++ wallet/api/v7_4/v7_4_api.cpp | 24 ++++++++ wallet/api/v7_4/v7_4_api.h | 31 ++++++++++ wallet/api/v7_4/v7_4_api_defs.h | 47 +++++++++++++++ wallet/api/v7_4/v7_4_api_handle.cpp | 29 ++++++++++ wallet/api/v7_4/v7_4_api_parse.cpp | 89 +++++++++++++++++++++++++++++ 10 files changed, 302 insertions(+), 4 deletions(-) create mode 100644 wallet/api/v7_4/api7_4.rest create mode 100644 wallet/api/v7_4/v7_4_api.cpp create mode 100644 wallet/api/v7_4/v7_4_api.h create mode 100644 wallet/api/v7_4/v7_4_api_defs.h create mode 100644 wallet/api/v7_4/v7_4_api_handle.cpp create mode 100644 wallet/api/v7_4/v7_4_api_parse.cpp diff --git a/wallet/api/CMakeLists.txt b/wallet/api/CMakeLists.txt index 018ebfa78..da0c5a848 100644 --- a/wallet/api/CMakeLists.txt +++ b/wallet/api/CMakeLists.txt @@ -23,6 +23,9 @@ set(SOURCES v7_3/v7_3_api.cpp v7_3/v7_3_api_handle.cpp v7_3/v7_3_api_parse.cpp + v7_4/v7_4_api.cpp + v7_4/v7_4_api_handle.cpp + v7_4/v7_4_api_parse.cpp ) add_library(wallet_api STATIC diff --git a/wallet/api/base/parse_utils.h b/wallet/api/base/parse_utils.h index 439ac5f13..34606df63 100644 --- a/wallet/api/base/parse_utils.h +++ b/wallet/api/base/parse_utils.h @@ -262,6 +262,29 @@ namespace beam::wallet return ValidHexBuffer(beam::from_hex(type_get(j))); } + BOOST_STRONG_TYPEDEF(json, JsonObject) + inline void to_json(json& j, const JsonObject& p) { + j = p.t; + } + + template<> + inline const char* type_name() + { + return "json object"; + } + + template<> + inline bool type_check(const json& j) + { + return j.is_object(); + } + + template<> + inline JsonObject type_get(const json& j) + { + return JsonObject(j); + } + BOOST_STRONG_TYPEDEF(json, JsonArray) inline void to_json(json& j, const JsonArray& p) { j = p.t; diff --git a/wallet/api/i_wallet_api.cpp b/wallet/api/i_wallet_api.cpp index f8cdd6ba5..398d004b4 100644 --- a/wallet/api/i_wallet_api.cpp +++ b/wallet/api/i_wallet_api.cpp @@ -19,6 +19,7 @@ #include "v7_1/v7_1_api.h" #include "v7_2/v7_2_api.h" #include "v7_3/v7_3_api.h" +#include "v7_4/v7_4_api.h" namespace beam::wallet { @@ -76,6 +77,13 @@ namespace beam::wallet // MUST BE SAFE TO CALL FROM ANY THREAD switch (version) { + case ApiVer7_4: + { + auto api = new V74Api(handler, 7, 4, data); + auto ptr = IWalletApi::Ptr(api); + api->takeGuardPtr(ptr); + return ptr; + } case ApiVer7_3: { auto api = new V73Api(handler, 7, 3, data); diff --git a/wallet/api/i_wallet_api.h b/wallet/api/i_wallet_api.h index 24745b34d..6c7361ac2 100644 --- a/wallet/api/i_wallet_api.h +++ b/wallet/api/i_wallet_api.h @@ -42,14 +42,14 @@ namespace beam::wallet macro(7, 0) \ macro(7, 1) \ macro(7, 2) \ - macro(7, 3) - + macro(7, 3) \ + macro(7, 4) #define THE_MACRO(major, minor) const uint32_t ApiVer##major##_##minor = major##minor; ApiVersions(THE_MACRO) #undef THE_MACRO - const uint32_t ApiVerCurrent = ApiVer7_3; - const uint32_t ApiVerMax = ApiVer7_3; + const uint32_t ApiVerCurrent = ApiVer7_4; + const uint32_t ApiVerMax = ApiVer7_4; const uint32_t ApiVerMin = ApiVer6_0; class IWalletApiHandler diff --git a/wallet/api/v7_4/api7_4.rest b/wallet/api/v7_4/api7_4.rest new file mode 100644 index 000000000..c8cb5067e --- /dev/null +++ b/wallet/api/v7_4/api7_4.rest @@ -0,0 +1,44 @@ +### send_message +POST http://127.0.0.1:10000/api/wallet HTTP/1.1 +content-type: application/json-rpc + +{ + "jsonrpc": "2.0", + "id": 1236, + "method": "send_message", + "params": { + "receiver": "50614dccd139f398504882567e5246ecd2cbe031085be97806cea7f07510443c69", + "message": { + "random_field1": 257, + "random_field2": "text24" + } + } +} + +### read_messages + +POST http://127.0.0.1:10000/api/wallet HTTP/1.1 +content-type: application/json-rpc + +{ + "jsonrpc": "2.0", + "id": 1236, + "method": "read_messages", + "params": {} +} + +### read_messages + +POST http://127.0.0.1:10000/api/wallet HTTP/1.1 +content-type: application/json-rpc + +{ + "jsonrpc": "2.0", + "id": 1236, + "method": "read_messages", + "params": { + "all" : true + } +} + + diff --git a/wallet/api/v7_4/v7_4_api.cpp b/wallet/api/v7_4/v7_4_api.cpp new file mode 100644 index 000000000..b8ff72619 --- /dev/null +++ b/wallet/api/v7_4/v7_4_api.cpp @@ -0,0 +1,24 @@ +// Copyright 2023 The Beam Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "v7_4_api.h" + +namespace beam::wallet +{ +V74Api::V74Api(IWalletApiHandler& handler, unsigned long avMajor, unsigned long avMinor, const ApiInitData &init) + : V73Api(handler, avMajor, avMinor, init) +{ + // MUST BE SAFE TO CALL FROM ANY THREAD + V7_4_API_METHODS(BEAM_API_REG_METHOD) +} +} diff --git a/wallet/api/v7_4/v7_4_api.h b/wallet/api/v7_4/v7_4_api.h new file mode 100644 index 000000000..d5981e102 --- /dev/null +++ b/wallet/api/v7_4/v7_4_api.h @@ -0,0 +1,31 @@ +// Copyright 2023 The Beam Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#pragma once +#include "v7_4_api_defs.h" +#include "wallet/api/v7_3/v7_3_api.h" + +namespace beam::wallet +{ +class V74Api: public V73Api +{ +public: + // CTOR MUST BE SAFE TO CALL FROM ANY THREAD + V74Api(IWalletApiHandler& handler, unsigned long avMajor, unsigned long avMinor, const ApiInitData& init); + ~V74Api() override = default; + + V7_4_API_METHODS(BEAM_API_PARSE_FUNC) + V7_4_API_METHODS(BEAM_API_RESPONSE_FUNC) + V7_4_API_METHODS(BEAM_API_HANDLE_FUNC) +}; +} diff --git a/wallet/api/v7_4/v7_4_api_defs.h b/wallet/api/v7_4/v7_4_api_defs.h new file mode 100644 index 000000000..7a3493faa --- /dev/null +++ b/wallet/api/v7_4/v7_4_api_defs.h @@ -0,0 +1,47 @@ +// Copyright 2023 The Beam Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#pragma once + +#include +#include +#include "wallet/core/wallet_db.h" + +namespace beam::wallet +{ +#define V7_4_API_METHODS(macro) \ + macro(SendSbbsMessage, "send_message", API_WRITE_ACCESS, API_SYNC, APPS_ALLOWED) \ + macro(ReadSbbsMessages, "read_messages", API_READ_ACCESS, API_SYNC, APPS_ALLOWED) + + struct SendSbbsMessage + { + WalletID receiver = Zero; + WalletID sender = Zero; + ByteBuffer message; + struct Response + { + WalletID sender = Zero; + WalletID receiver = Zero; + size_t bytes = 0; + }; + }; + + struct ReadSbbsMessages + { + bool all = false; + struct Response + { + std::vector messages; + }; + }; +} diff --git a/wallet/api/v7_4/v7_4_api_handle.cpp b/wallet/api/v7_4/v7_4_api_handle.cpp new file mode 100644 index 000000000..2914b0380 --- /dev/null +++ b/wallet/api/v7_4/v7_4_api_handle.cpp @@ -0,0 +1,29 @@ +// Copyright 2023 The Beam Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "v7_4_api.h" + +namespace beam::wallet +{ + void V74Api::onHandleSendSbbsMessage(const JsonRpcId& id, SendSbbsMessage&& req) + { + auto timestamp = getTimestamp(); + getWallet()->sendInstantSbbsMessage(timestamp, req.receiver, req.sender, std::move(req.message)); + doResponse(id, SendSbbsMessage::Response{ req.sender, req.receiver, req.message.size() }); + } + + void V74Api::onHandleReadSbbsMessages(const JsonRpcId& id, ReadSbbsMessages&& req) + { + doResponse(id, ReadSbbsMessages::Response{ getWalletDB()->readIMs(req.all)}); + } +} diff --git a/wallet/api/v7_4/v7_4_api_parse.cpp b/wallet/api/v7_4/v7_4_api_parse.cpp new file mode 100644 index 000000000..d65b3c81c --- /dev/null +++ b/wallet/api/v7_4/v7_4_api_parse.cpp @@ -0,0 +1,89 @@ +// Copyright 2023 The Beam Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "v7_4_api.h" +#include "version.h" +#include "wallet/core/base58.h" + +namespace beam::wallet +{ +std::pair V74Api::onParseSendSbbsMessage(const JsonRpcId& id, const nlohmann::json& params) +{ + SendSbbsMessage message; + message.receiver.FromBuf(getMandatoryParam(params, "receiver")); + if (auto sender = getOptionalParam(params, "sender")) + { + message.sender.FromBuf(*sender); + } + else + { + WalletAddress wa; + getWalletDB()->getDefaultAddressAlways(wa); + message.sender = wa.m_BbsAddr; + } + + json jsonMessage = getMandatoryParam(params, "message"); + + auto messageDump = jsonMessage.dump(); + ByteBuffer messageBuffer(messageDump.begin(), messageDump.end()); + message.message = messageBuffer; + + return std::make_pair(std::move(message), MethodInfo()); +} + +void V74Api::getResponse(const JsonRpcId& id, const SendSbbsMessage::Response& res, json& msg) +{ + msg = json + { + {JsonRpcHeader, JsonRpcVersion}, + {"id", id}, + {"result", + { + {"from", std::to_string(res.sender)}, + {"to", std::to_string(res.receiver)}, + {"bytes", res.bytes} + } + } + }; +} + +std::pair V74Api::onParseReadSbbsMessages(const JsonRpcId& id, const nlohmann::json& params) +{ + ReadSbbsMessages message; + if (auto all = getOptionalParam(params, "all")) + { + message.all = *all; + } + return std::make_pair(std::move(message), MethodInfo()); +} + +void V74Api::getResponse(const JsonRpcId& id, const ReadSbbsMessages::Response& res, json& msg) +{ + msg = json + { + {JsonRpcHeader, JsonRpcVersion}, + {"id", id}, + {"result", json::array()} + }; + for (auto& message : res.messages) + { + msg["result"].push_back( + { + {"id", message.m_id}, + {"timestamp", message.m_timestamp}, + {"sender", std::to_string(message.m_counterpart)}, + {"message", json::parse(message.m_message)} + }); + } +} +}