-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added sbbs methods to the wallet api
- Loading branch information
Showing
10 changed files
with
302 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} | ||
}); | ||
} | ||
} | ||
} |