Skip to content

Commit

Permalink
added sbbs methods to the wallet api
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolse committed Jul 10, 2023
1 parent 54614fe commit ef0eb95
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 4 deletions.
3 changes: 3 additions & 0 deletions wallet/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions wallet/api/base/parse_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ namespace beam::wallet
return ValidHexBuffer(beam::from_hex(type_get<std::string>(j)));
}

BOOST_STRONG_TYPEDEF(json, JsonObject)
inline void to_json(json& j, const JsonObject& p) {
j = p.t;
}

template<>
inline const char* type_name<JsonObject>()
{
return "json object";
}

template<>
inline bool type_check<JsonObject>(const json& j)
{
return j.is_object();
}

template<>
inline JsonObject type_get<JsonObject>(const json& j)
{
return JsonObject(j);
}

BOOST_STRONG_TYPEDEF(json, JsonArray)
inline void to_json(json& j, const JsonArray& p) {
j = p.t;
Expand Down
8 changes: 8 additions & 0 deletions wallet/api/i_wallet_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions wallet/api/i_wallet_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions wallet/api/v7_4/api7_4.rest
Original file line number Diff line number Diff line change
@@ -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
}
}


24 changes: 24 additions & 0 deletions wallet/api/v7_4/v7_4_api.cpp
Original file line number Diff line number Diff line change
@@ -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)
}
}
31 changes: 31 additions & 0 deletions wallet/api/v7_4/v7_4_api.h
Original file line number Diff line number Diff line change
@@ -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)
};
}
47 changes: 47 additions & 0 deletions wallet/api/v7_4/v7_4_api_defs.h
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <vector>
#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<InstantMessage> messages;
};
};
}
29 changes: 29 additions & 0 deletions wallet/api/v7_4/v7_4_api_handle.cpp
Original file line number Diff line number Diff line change
@@ -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)});
}
}
89 changes: 89 additions & 0 deletions wallet/api/v7_4/v7_4_api_parse.cpp
Original file line number Diff line number Diff line change
@@ -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<SendSbbsMessage, IWalletApi::MethodInfo> V74Api::onParseSendSbbsMessage(const JsonRpcId& id, const nlohmann::json& params)
{
SendSbbsMessage message;
message.receiver.FromBuf(getMandatoryParam<ValidHexBuffer>(params, "receiver"));
if (auto sender = getOptionalParam<ValidHexBuffer>(params, "sender"))
{
message.sender.FromBuf(*sender);
}
else
{
WalletAddress wa;
getWalletDB()->getDefaultAddressAlways(wa);
message.sender = wa.m_BbsAddr;
}

json jsonMessage = getMandatoryParam<JsonObject>(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<ReadSbbsMessages, IWalletApi::MethodInfo> V74Api::onParseReadSbbsMessages(const JsonRpcId& id, const nlohmann::json& params)
{
ReadSbbsMessages message;
if (auto all = getOptionalParam<bool>(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)}
});
}
}
}

0 comments on commit ef0eb95

Please sign in to comment.