Electroneum
Loading...
Searching...
No Matches
messages_map.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-Present, Electroneum
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
30#include "messages_map.hpp"
31#include "messages/messages.pb.h"
32#include "messages/messages-common.pb.h"
33#include "messages/messages-management.pb.h"
34#include "messages/messages-electroneum.pb.h"
35
36#ifdef WITH_TREZOR_DEBUGGING
37#include "messages/messages-debug.pb.h"
38#endif
39
40using namespace std;
41using namespace hw::trezor;
42
43namespace hw{
44namespace trezor
45{
46
47 const char * TYPE_PREFIX = "MessageType_";
48 const char * PACKAGES[] = {
49 "hw.trezor.messages.",
50 "hw.trezor.messages.common.",
51 "hw.trezor.messages.management.",
52#ifdef WITH_TREZOR_DEBUGGING
53 "hw.trezor.messages.debug.",
54#endif
55 "hw.trezor.messages.electroneum."
56 };
57
58 google::protobuf::Message * MessageMapper::get_message(int wire_number) {
59 return MessageMapper::get_message(static_cast<messages::MessageType>(wire_number));
60 }
61
62 google::protobuf::Message * MessageMapper::get_message(messages::MessageType wire_number) {
63 const string &messageTypeName = hw::trezor::messages::MessageType_Name(wire_number);
64 if (messageTypeName.empty()) {
65 throw exc::EncodingException(std::string("Message descriptor not found: ") + std::to_string(wire_number));
66 }
67
68 string messageName = messageTypeName.substr(strlen(TYPE_PREFIX));
69 return MessageMapper::get_message(messageName);
70 }
71
72 google::protobuf::Message * MessageMapper::get_message(const std::string & msg_name) {
73 // Each package instantiation so lookup works
74 hw::trezor::messages::common::Success::default_instance();
75 hw::trezor::messages::management::Cancel::default_instance();
76 hw::trezor::messages::Electroneum::ElectroneumGetAddress::default_instance();
77
78#ifdef WITH_TREZOR_DEBUGGING
79 hw::trezor::messages::debug::DebugLinkDecision::default_instance();
80#endif
81
82 google::protobuf::Descriptor const * desc = nullptr;
83 for(const string &text : PACKAGES){
84 desc = google::protobuf::DescriptorPool::generated_pool()
85 ->FindMessageTypeByName(text + msg_name);
86 if (desc != nullptr){
87 break;
88 }
89 }
90
91 if (desc == nullptr){
92 throw exc::EncodingException(std::string("Message not found: ") + msg_name);
93 }
94
95 google::protobuf::Message* message =
96 google::protobuf::MessageFactory::generated_factory()
97 ->GetPrototype(desc)->New();
98
99 return message;
100
101// // CODEGEN way, fast
102// switch(wire_number){
103// case 501:
104// return new messages::Electroneum::ElectroneumTransactionSignRequest();
105// default:
106// throw std::runtime_error("not implemented");
107// }
108//
109// // CODEGEN message -> number: specification
110// // messages::MessageType get_message_wire_number(const messages::Electroneum::ElectroneumTransactionSignRequest * msg) { return 501; }
111// // messages::MessageType get_message_wire_number(const messages::management::ping * msg)
112//
113 }
114
115 messages::MessageType MessageMapper::get_message_wire_number(const google::protobuf::Message * msg){
116 return MessageMapper::get_message_wire_number(msg->GetDescriptor()->name());
117 }
118
119 messages::MessageType MessageMapper::get_message_wire_number(const google::protobuf::Message & msg){
120 return MessageMapper::get_message_wire_number(msg.GetDescriptor()->name());
121 }
122
123 messages::MessageType MessageMapper::get_message_wire_number(const std::string & msg_name){
124 string enumMessageName = std::string(TYPE_PREFIX) + msg_name;
125
126 messages::MessageType res;
127 bool r = hw::trezor::messages::MessageType_Parse(enumMessageName, &res);
128 if (!r){
129 throw exc::EncodingException(std::string("Message ") + msg_name + " not found");
130 }
131
132 return res;
133 }
134
135}
136}
static messages::MessageType get_message_wire_number()
::google::protobuf::Message * get_message(int wire_number)
std::string message("Message requiring signing")
const char * res
const char * PACKAGES[]
const char * TYPE_PREFIX
Definition device.cpp:38
STL namespace.