@eosrio/node-abieos 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +4 -1
  2. package/dist/abieos.node +0 -0
  3. package/lib/abieos.d.ts +19 -0
  4. package/lib/abieos.d.ts.map +1 -0
  5. package/lib/abieos.js +100 -0
  6. package/lib/abieos.ts +107 -0
  7. package/package.json +13 -8
  8. package/.idea/modules.xml +0 -8
  9. package/.idea/node-abieos.iml +0 -12
  10. package/.idea/vcs.xml +0 -9
  11. package/.idea/workspace.xml +0 -66
  12. package/CMakeLists.txt +0 -35
  13. package/abieos.d.ts +0 -21
  14. package/abieos.js +0 -59
  15. package/eosio.json +0 -1596
  16. package/include/.clang-format +0 -76
  17. package/include/eosio/abi.hpp +0 -393
  18. package/include/eosio/asset.hpp +0 -458
  19. package/include/eosio/bytes.hpp +0 -27
  20. package/include/eosio/chain_conversions.hpp +0 -450
  21. package/include/eosio/chain_types.hpp +0 -14
  22. package/include/eosio/check.hpp +0 -178
  23. package/include/eosio/convert.hpp +0 -95
  24. package/include/eosio/crypto.hpp +0 -148
  25. package/include/eosio/eosio_outcome.hpp +0 -41
  26. package/include/eosio/fixed_bytes.hpp +0 -250
  27. package/include/eosio/float.hpp +0 -35
  28. package/include/eosio/for_each_field.hpp +0 -48
  29. package/include/eosio/fpconv.c +0 -336
  30. package/include/eosio/fpconv.h +0 -41
  31. package/include/eosio/fpconv.license +0 -23
  32. package/include/eosio/from_bin.hpp +0 -272
  33. package/include/eosio/from_json.hpp +0 -749
  34. package/include/eosio/from_string.hpp +0 -28
  35. package/include/eosio/map_macro.h +0 -64
  36. package/include/eosio/murmur.hpp +0 -55
  37. package/include/eosio/name.hpp +0 -178
  38. package/include/eosio/opaque.hpp +0 -184
  39. package/include/eosio/operators.hpp +0 -71
  40. package/include/eosio/powers.h +0 -76
  41. package/include/eosio/reflection.hpp +0 -61
  42. package/include/eosio/ship_protocol.hpp +0 -971
  43. package/include/eosio/stream.hpp +0 -235
  44. package/include/eosio/symbol.hpp +0 -300
  45. package/include/eosio/time.hpp +0 -264
  46. package/include/eosio/to_bin.hpp +0 -189
  47. package/include/eosio/to_json.hpp +0 -334
  48. package/include/eosio/to_key.hpp +0 -305
  49. package/include/eosio/types.hpp +0 -103
  50. package/include/eosio/varint.hpp +0 -452
  51. package/include/outcome-basic.hpp +0 -6836
  52. package/include/rapidjson/allocators.h +0 -284
  53. package/include/rapidjson/cursorstreamwrapper.h +0 -78
  54. package/include/rapidjson/document.h +0 -2646
  55. package/include/rapidjson/encodedstream.h +0 -299
  56. package/include/rapidjson/encodings.h +0 -716
  57. package/include/rapidjson/error/en.h +0 -74
  58. package/include/rapidjson/error/error.h +0 -161
  59. package/include/rapidjson/filereadstream.h +0 -99
  60. package/include/rapidjson/filewritestream.h +0 -104
  61. package/include/rapidjson/fwd.h +0 -151
  62. package/include/rapidjson/internal/biginteger.h +0 -290
  63. package/include/rapidjson/internal/diyfp.h +0 -271
  64. package/include/rapidjson/internal/dtoa.h +0 -245
  65. package/include/rapidjson/internal/ieee754.h +0 -78
  66. package/include/rapidjson/internal/itoa.h +0 -308
  67. package/include/rapidjson/internal/meta.h +0 -186
  68. package/include/rapidjson/internal/pow10.h +0 -55
  69. package/include/rapidjson/internal/regex.h +0 -732
  70. package/include/rapidjson/internal/stack.h +0 -231
  71. package/include/rapidjson/internal/strfunc.h +0 -69
  72. package/include/rapidjson/internal/strtod.h +0 -290
  73. package/include/rapidjson/internal/swap.h +0 -46
  74. package/include/rapidjson/istreamwrapper.h +0 -113
  75. package/include/rapidjson/memorybuffer.h +0 -70
  76. package/include/rapidjson/memorystream.h +0 -71
  77. package/include/rapidjson/msinttypes/inttypes.h +0 -316
  78. package/include/rapidjson/msinttypes/stdint.h +0 -300
  79. package/include/rapidjson/ostreamwrapper.h +0 -81
  80. package/include/rapidjson/pointer.h +0 -1357
  81. package/include/rapidjson/prettywriter.h +0 -277
  82. package/include/rapidjson/rapidjson.h +0 -654
  83. package/include/rapidjson/reader.h +0 -2230
  84. package/include/rapidjson/schema.h +0 -2494
  85. package/include/rapidjson/stream.h +0 -223
  86. package/include/rapidjson/stringbuffer.h +0 -121
  87. package/include/rapidjson/writer.h +0 -710
  88. package/src/abi.cpp +0 -274
  89. package/src/abieos.cpp +0 -334
  90. package/src/abieos.h +0 -91
  91. package/src/abieos.hpp +0 -1011
  92. package/src/abieos_exception.hpp +0 -89
  93. package/src/abieos_numeric.hpp +0 -94
  94. package/src/abieos_ripemd160.hpp +0 -417
  95. package/src/crypto.cpp +0 -215
  96. package/src/main.cpp +0 -242
  97. package/src/ship.abi.cpp +0 -695
  98. package/tools/CMakeLists.txt +0 -9
  99. package/tools/name.cpp +0 -86
package/src/abi.cpp DELETED
@@ -1,274 +0,0 @@
1
- #include <eosio/abi.hpp>
2
- #include "abieos.hpp"
3
-
4
- using namespace eosio;
5
-
6
- namespace {
7
-
8
- template <int i>
9
- bool ends_with(const std::string& s, const char (&suffix)[i]) {
10
- return s.size() >= i - 1 && !strcmp(s.c_str() + s.size() - (i - 1), suffix);
11
- }
12
-
13
- template <typename T>
14
- struct abi_serializer_impl : abi_serializer {
15
- void json_to_bin(::abieos::jvalue_to_bin_state& state, bool allow_extensions, const abi_type* type,
16
- bool start) const override {
17
- return ::abieos::json_to_bin((T*)nullptr, state, allow_extensions, type, start);
18
- }
19
- void json_to_bin(::abieos::json_to_bin_state& state, bool allow_extensions, const abi_type* type,
20
- bool start) const override {
21
- return ::abieos::json_to_bin((T*)nullptr, state, allow_extensions, type, start);
22
- }
23
- void bin_to_json(::abieos::bin_to_json_state& state, bool allow_extensions, const abi_type* type,
24
- bool start) const override {
25
- return ::abieos::bin_to_json((T*)nullptr, state, allow_extensions, type, start);
26
- }
27
- };
28
-
29
- template <typename T>
30
- constexpr auto abi_serializer_for = abi_serializer_impl<T>{};
31
-
32
- abi_type::alias resolve(std::map<std::string, abi_type>& abi_types, const abi_type::alias_def* type, int depth);
33
-
34
- template<typename... T, typename... A>
35
- bool holds_any_alternative(const std::variant<A...>& v) {
36
- return (... || std::holds_alternative<T>(v));
37
- }
38
-
39
- template <typename F>
40
- constexpr void for_each_abi_type(F f) {
41
- static_assert(sizeof(float) == 4);
42
- static_assert(sizeof(double) == 8);
43
- std::apply([&f](auto&& ...t) { (f(&t), ...); }, basic_abi_types{});
44
- }
45
-
46
- abi_type* get_type(std::map<std::string, abi_type>& abi_types,
47
- const std::string& name, int depth) {
48
- eosio::check(depth < 32,
49
- eosio::convert_abi_error(abi_error::recursion_limit_reached));
50
- auto it = abi_types.find(name);
51
- if (it == abi_types.end()) {
52
- if (ends_with(name, "?")) {
53
- auto base = get_type(abi_types, name.substr(0, name.size() - 1), depth + 1);
54
- eosio::check(!holds_any_alternative<abi_type::optional, abi_type::array, abi_type::extension>(base->_data),
55
- eosio::convert_abi_error(abi_error::invalid_nesting));
56
- auto [iter, success] = abi_types.try_emplace(name, name, abi_type::optional{base}, &abi_serializer_for< ::abieos::pseudo_optional>);
57
- return &iter->second;
58
- } else if (ends_with(name, "[]")) {
59
- auto element = get_type(abi_types, name.substr(0, name.size() - 2), depth + 1);
60
- eosio::check(!holds_any_alternative<abi_type::optional, abi_type::array, abi_type::extension>(element->_data),
61
- eosio::convert_abi_error(abi_error::invalid_nesting));
62
- auto [iter, success] = abi_types.try_emplace(name, name, abi_type::array{element}, &abi_serializer_for< ::abieos::pseudo_array>);
63
- return &iter->second;
64
- } else if (ends_with(name, "$")) {
65
- auto base = get_type(abi_types, name.substr(0, name.size() - 1), depth + 1);
66
- eosio::check(!std::holds_alternative<abi_type::extension>(base->_data),
67
- eosio::convert_abi_error(abi_error::invalid_nesting));
68
- auto [iter, success] = abi_types.try_emplace(name, name, abi_type::extension{base}, &abi_serializer_for< ::abieos::pseudo_extension>);
69
- return &iter->second;
70
- } else
71
- eosio::check(false, eosio::convert_abi_error(abi_error::unknown_type));
72
- }
73
-
74
- // resolve aliases
75
- if (auto* alias = std::get_if<abi_type::alias>(&it->second._data)) {
76
- return alias->type;
77
- } else if(auto* alias = std::get_if<const abi_type::alias_def*>(&it->second._data)) {
78
- auto base = resolve(abi_types, *alias, depth);
79
- it->second._data = base;
80
- return base.type;
81
- }
82
-
83
- return &it->second;
84
- }
85
-
86
- abi_type::struct_ resolve(std::map<std::string, abi_type>& abi_types, const struct_def* type, int depth) {
87
- eosio::check(depth < 32,
88
- eosio::convert_abi_error(abi_error::recursion_limit_reached));
89
- abi_type::struct_ result;
90
- if (!type->base.empty()) {
91
- auto base = get_type(abi_types, type->base, depth + 1);
92
-
93
- if(auto* base_def = std::get_if<const struct_def*>(&base->_data)) {
94
- auto b = resolve(abi_types, *base_def, depth + 1);
95
- base->_data = std::move(b);
96
- }
97
- if(auto* b = std::get_if<abi_type::struct_>(&base->_data)) {
98
- result.fields = b->fields;
99
- } else {
100
- eosio::check(false, eosio::convert_abi_error(abi_error::base_not_a_struct));
101
- }
102
- }
103
- for (auto& field : type->fields) {
104
- auto t = get_type(abi_types, field.type, depth + 1);
105
- result.fields.push_back(abi_field{field.name, t});
106
- }
107
- return result;
108
- }
109
-
110
-
111
- abi_type::variant resolve(std::map<std::string, abi_type>& abi_types, const variant_def* type, int depth) {
112
- eosio::check(depth < 32,
113
- eosio::convert_abi_error(abi_error::recursion_limit_reached));
114
- abi_type::variant result;
115
- for (const std::string& field : type->types) {
116
- auto t = get_type(abi_types, field, depth + 1);
117
- result.push_back({field, t});
118
- }
119
- return result;
120
- }
121
-
122
- abi_type::alias resolve(std::map<std::string, abi_type>& abi_types, const abi_type::alias_def* type, int depth) {
123
- auto t = get_type(abi_types, *type, depth + 1);
124
- eosio::check(!std::holds_alternative<abi_type::extension>(t->_data),
125
- eosio::convert_abi_error(abi_error::extension_typedef));
126
- return abi_type::alias{t};
127
- }
128
-
129
- struct fill_t {
130
- std::map<std::string, abi_type>& abi_types;
131
- abi_type& type;
132
- int depth;
133
- template<typename T>
134
- auto operator()(T& t) -> std::void_t<decltype(resolve(abi_types, t, depth))> {
135
- auto x = resolve(abi_types, t, depth);
136
- type._data = std::move(x);
137
- }
138
- template<typename T>
139
- auto operator()(const T& t) {
140
- }
141
- };
142
-
143
- void fill(std::map<std::string, abi_type>& abi_types, abi_type& type, int depth) {
144
- return std::visit(fill_t{abi_types, type, depth}, type._data);
145
- }
146
-
147
- }
148
-
149
-
150
- const abi_type* eosio::abi::get_type(const std::string& name) {
151
- return ::get_type(abi_types, name, 0);
152
- }
153
-
154
- void eosio::convert(const abi_def& abi, eosio::abi& c) {
155
- for (auto& a : abi.actions)
156
- c.action_types[a.name] = a.type;
157
- for (auto& t : abi.tables)
158
- c.table_types[t.name] = t.type;
159
- for (auto& r : abi.action_results.value)
160
- c.action_result_types[r.name] = r.result_type;
161
- for_each_abi_type([&](auto* p) {
162
- const char* name = get_type_name(p);
163
- c.abi_types.try_emplace(name, name, abi_type::builtin{}, &abi_serializer_for<std::decay_t<decltype(*p)>>);
164
- });
165
- {
166
- c.abi_types.try_emplace("extended_asset", "extended_asset",
167
- abi_type::struct_{nullptr, {{"quantity", &c.abi_types.find("asset")->second},
168
- {"contract", &c.abi_types.find("name")->second}}},
169
- &abi_serializer_for<::abieos::pseudo_object>);
170
- }
171
-
172
- for (auto& t : abi.types) {
173
- eosio::check(!t.new_type_name.empty(),
174
- eosio::convert_abi_error(abi_error::missing_name));
175
- auto [_, inserted] = c.abi_types.try_emplace(t.new_type_name, t.new_type_name, &t.type, nullptr);
176
- eosio::check(inserted,
177
- eosio::convert_abi_error(abi_error::redefined_type));
178
- }
179
- for (auto& s : abi.structs) {
180
- eosio::check(!s.name.empty(),
181
- eosio::convert_abi_error(abi_error::missing_name));
182
- auto [it, inserted] = c.abi_types.try_emplace(s.name, s.name, &s, &abi_serializer_for<::abieos::pseudo_object>);
183
- eosio::check(inserted,
184
- eosio::convert_abi_error(abi_error::redefined_type));
185
- }
186
- for (auto& v : abi.variants.value) {
187
- eosio::check(!v.name.empty(),
188
- eosio::convert_abi_error(abi_error::missing_name));
189
- auto [it, inserted] = c.abi_types.try_emplace(v.name, v.name, &v, &abi_serializer_for<::abieos::pseudo_variant>);
190
- eosio::check(inserted,
191
- eosio::convert_abi_error(abi_error::redefined_type));
192
- }
193
- for (auto& [_, t] : c.abi_types) {
194
- fill(c.abi_types, t, 0);
195
- }
196
-
197
- for (const auto& [key, val] : abi.kv_tables.value) {
198
- std::vector<char> bytes;
199
- eosio::vector_stream strm(bytes);
200
- to_json(val, strm);
201
- c.kv_tables.try_emplace(key, bytes.begin(), bytes.end());
202
- }
203
- }
204
-
205
- void to_abi_def(abi_def& def, const std::string& name, const abi_type::builtin&) {}
206
- void to_abi_def(abi_def& def, const std::string& name, const abi_type::optional&) {}
207
- void to_abi_def(abi_def& def, const std::string& name, const abi_type::array&) {}
208
- void to_abi_def(abi_def& def, const std::string& name, const abi_type::extension&) {}
209
-
210
- template<typename T>
211
- void to_abi_def(abi_def& def, const std::string& name, const T*) {
212
- eosio::check(false, eosio::convert_abi_error(eosio::abi_error::bad_abi));
213
- }
214
-
215
- void to_abi_def(abi_def& def, const std::string& name, const abi_type::alias& alias) {
216
- def.types.push_back({name, alias.type->name});
217
- }
218
-
219
- void to_abi_def(abi_def& def, const std::string& name, const abi_type::struct_& struct_) {
220
- if(name == "extended_asset") return;
221
- std::size_t field_offset = 0;
222
- std::string base;
223
- std::vector<field_def> fields;
224
- if(struct_.base) {
225
- field_offset = struct_.base->as_struct()->fields.size();
226
- base = struct_.base->name;
227
- }
228
- for(std::size_t i = field_offset; i < struct_.fields.size(); ++i) {
229
- const abi_field& field = struct_.fields[i];
230
- fields.push_back({field.name, field.type->name});
231
- }
232
- def.structs.push_back({name, std::move(base), std::move(fields)});
233
- }
234
-
235
- void to_abi_def(abi_def& def, const std::string& name, const abi_type::variant& variant) {
236
- std::vector<std::string> types;
237
- for(const auto& [name, type] : variant) {
238
- types.push_back(type->name);
239
- }
240
- def.variants.value.push_back({name, std::move(types)});
241
- }
242
-
243
- void eosio::convert(const eosio::abi& abi, eosio::abi_def& def) {
244
- def.version = "eosio::abi/1.0";
245
- for(auto& [name, type] : abi.abi_types) {
246
- std::visit([&name = type.name, &def](const auto& t){ return to_abi_def(def, name, t); }, type._data);
247
- }
248
- }
249
-
250
- const abi_serializer* const eosio::object_abi_serializer = &abi_serializer_for< ::abieos::pseudo_object>;
251
- const abi_serializer* const eosio::variant_abi_serializer = &abi_serializer_for< ::abieos::pseudo_variant>;
252
- const abi_serializer* const eosio::array_abi_serializer = &abi_serializer_for< ::abieos::pseudo_array>;
253
- const abi_serializer* const eosio::extension_abi_serializer = &abi_serializer_for< ::abieos::pseudo_extension>;
254
- const abi_serializer* const eosio::optional_abi_serializer = &abi_serializer_for< ::abieos::pseudo_optional>;
255
-
256
- std::vector<char> eosio::abi_type::json_to_bin_reorderable(std::string_view json, std::function<void()> f) const {
257
- abieos::jvalue tmp;
258
- abieos::json_to_jvalue(tmp, json, f);
259
- std::vector<char> result;
260
- abieos::json_to_bin(result, this, tmp, f);
261
- return result;
262
- }
263
-
264
- std::vector<char> eosio::abi_type::json_to_bin(std::string_view json, std::function<void()> f) const {
265
- std::vector<char> result;
266
- abieos::json_to_bin(result, this, json, f);
267
- return result;
268
- }
269
-
270
- std::string eosio::abi_type::bin_to_json(input_stream& bin, std::function<void()> f) const {
271
- std::string result;
272
- abieos::bin_to_json(bin, this, result, f);
273
- return result;
274
- }
package/src/abieos.cpp DELETED
@@ -1,334 +0,0 @@
1
- // copyright defined in abieos/LICENSE.txt
2
-
3
- #include "abieos.h"
4
- #include "abieos.hpp"
5
-
6
- #include <memory>
7
-
8
- inline const bool catch_all = true;
9
-
10
- using namespace abieos;
11
-
12
- struct abieos_context_s {
13
- const char* last_error = "";
14
- std::string last_error_buffer{};
15
- std::string result_str{};
16
- std::vector<char> result_bin{};
17
-
18
- std::map<name, abi> contracts{};
19
- };
20
-
21
- void fix_null_str(const char*& s) {
22
- if (!s)
23
- s = "";
24
- }
25
-
26
- bool set_error(abieos_context* context, std::string error) noexcept {
27
- context->last_error_buffer = std::move(error);
28
- context->last_error = context->last_error_buffer.c_str();
29
- return false;
30
- }
31
-
32
- template <typename T, typename F>
33
- auto handle_exceptions(abieos_context* context, T errval, F f) noexcept -> decltype(f()) {
34
- if (!context)
35
- return errval;
36
- try {
37
- return f();
38
- } catch (std::exception& e) {
39
- if (!catch_all)
40
- throw;
41
- set_error(context, e.what());
42
- return errval;
43
- } catch (...) {
44
- if (!catch_all)
45
- throw;
46
- set_error(context, "unknown exception");
47
- return errval;
48
- }
49
- }
50
-
51
- extern "C" abieos_context* abieos_create() {
52
- try {
53
- return new abieos_context{};
54
- } catch (...) {
55
- if (!catch_all)
56
- throw;
57
- return nullptr;
58
- }
59
- }
60
-
61
- extern "C" abieos_bool abieos_delete_contract(abieos_context* context, uint64_t contract) {
62
- auto itr = context->contracts.find(::abieos::name{contract});
63
- if(itr == context->contracts.end()) {
64
- return false;
65
- } else {
66
- context->contracts.erase(itr);
67
- return true;
68
- }
69
- }
70
-
71
- extern "C" void abieos_destroy(abieos_context* context) { delete context; }
72
-
73
- extern "C" const char* abieos_get_error(abieos_context* context) {
74
- if (!context)
75
- return "context is null";
76
- return context->last_error;
77
- }
78
-
79
- extern "C" int abieos_get_bin_size(abieos_context* context) {
80
- if (!context)
81
- return 0;
82
- return context->result_bin.size();
83
- }
84
-
85
- extern "C" const char* abieos_get_bin_data(abieos_context* context) {
86
- if (!context)
87
- return nullptr;
88
- return context->result_bin.data();
89
- }
90
-
91
- extern "C" const char* abieos_get_bin_hex(abieos_context* context) {
92
- return handle_exceptions(context, nullptr, [&] {
93
- context->result_str.clear();
94
- hex(context->result_bin.begin(), context->result_bin.end(), std::back_inserter(context->result_str));
95
- return context->result_str.c_str();
96
- });
97
- }
98
-
99
- extern "C" uint64_t abieos_string_to_name(abieos_context* context, const char* str) {
100
- fix_null_str(str);
101
- return eosio::string_to_name(str);
102
- }
103
-
104
- extern "C" const char* abieos_name_to_string(abieos_context* context, uint64_t name) {
105
- return handle_exceptions(context, nullptr, [&] {
106
- context->result_str = eosio::name_to_string(name);
107
- return context->result_str.c_str();
108
- });
109
- }
110
-
111
- extern "C" abieos_bool abieos_set_abi(abieos_context* context, uint64_t contract, const char* abi) {
112
- fix_null_str(abi);
113
- return handle_exceptions(context, false, [&]() {
114
- context->last_error = "abi parse error";
115
- abi_def def{};
116
- std::string error;
117
- std::string abi_copy{abi};
118
- eosio::json_token_stream stream(abi_copy.data());
119
- from_json(def, stream);
120
- if (!check_abi_version(def.version, error))
121
- return set_error(context, std::move(error));
122
- abieos::abi c;
123
- convert(def, c);
124
- context->contracts.insert({name{contract}, std::move(c)});
125
- return true;
126
- });
127
- }
128
-
129
- extern "C" abieos_bool abieos_set_abi_bin(abieos_context* context, uint64_t contract, const char* data, size_t size) {
130
- return handle_exceptions(context, false, [&] {
131
- context->last_error = "abi parse error";
132
- if (!data || !size)
133
- return set_error(context, "no data");
134
- std::string error;
135
- eosio::input_stream stream{data, size};
136
- std::string version;
137
- from_bin(version, stream);
138
- if (!check_abi_version(version, error))
139
- return set_error(context, std::move(error));
140
- abi_def def{};
141
- stream = {data, size};
142
- from_bin(def, stream);
143
- abieos::abi c;
144
- convert(def, c);
145
- context->contracts.insert({name{contract}, std::move(c)});
146
- return true;
147
- });
148
- }
149
-
150
- extern "C" abieos_bool abieos_set_abi_hex(abieos_context* context, uint64_t contract, const char* hex) {
151
- fix_null_str(hex);
152
- return handle_exceptions(context, false, [&]() -> abieos_bool {
153
- std::vector<char> data;
154
- std::string error;
155
- if (!unhex(error, hex, hex + strlen(hex), std::back_inserter(data))) {
156
- if (!error.empty())
157
- set_error(context, std::move(error));
158
- return false;
159
- }
160
- return abieos_set_abi_bin(context, contract, data.data(), data.size());
161
- });
162
- }
163
-
164
- extern "C" const char* abieos_get_type_for_action(abieos_context* context, uint64_t contract, uint64_t action) {
165
- return handle_exceptions(context, nullptr, [&] {
166
- auto contract_it = context->contracts.find(::abieos::name{contract});
167
- if (contract_it == context->contracts.end())
168
- throw std::runtime_error("contract \"" + eosio::name_to_string(contract) + "\" is not loaded");
169
- auto& c = contract_it->second;
170
-
171
- auto action_it = c.action_types.find(name{action});
172
- if (action_it == c.action_types.end())
173
- throw std::runtime_error("contract \"" + eosio::name_to_string(contract) + "\" does not have action \"" +
174
- eosio::name_to_string(action) + "\"");
175
- return action_it->second.c_str();
176
- });
177
- }
178
-
179
- extern "C" const char* abieos_get_type_for_table(abieos_context* context, uint64_t contract, uint64_t table) {
180
- return handle_exceptions(context, nullptr, [&] {
181
- auto contract_it = context->contracts.find(::abieos::name{contract});
182
- if (contract_it == context->contracts.end())
183
- throw std::runtime_error("contract \"" + eosio::name_to_string(contract) + "\" is not loaded");
184
- auto& c = contract_it->second;
185
-
186
- auto table_it = c.table_types.find(name{table});
187
- if (table_it == c.table_types.end())
188
- throw std::runtime_error("contract \"" + eosio::name_to_string(contract) + "\" does not have table \"" +
189
- eosio::name_to_string(table) + "\"");
190
- return table_it->second.c_str();
191
- });
192
- }
193
-
194
- extern "C" const char* abieos_get_kv_table_def(abieos_context* context, uint64_t contract, uint64_t table) {
195
- return handle_exceptions(context, nullptr, [&] {
196
- auto contract_it = context->contracts.find(::abieos::name{contract});
197
- if (contract_it == context->contracts.end())
198
- throw std::runtime_error("contract \"" + eosio::name_to_string(contract) + "\" is not loaded");
199
- auto& c = contract_it->second;
200
-
201
- auto table_it = c.kv_tables.find(name{table});
202
- if (table_it == c.kv_tables.end())
203
- throw std::runtime_error("contract \"" + eosio::name_to_string(contract) + "\" does not have kv table \"" +
204
- eosio::name_to_string(table) + "\"");
205
- return table_it->second.c_str();
206
- });
207
- }
208
-
209
- extern "C" const char* abieos_get_type_for_action_result(abieos_context* context, uint64_t contract,
210
- uint64_t action_result) {
211
- return handle_exceptions(context, nullptr, [&] {
212
- auto contract_it = context->contracts.find(::abieos::name{contract});
213
- if (contract_it == context->contracts.end())
214
- throw std::runtime_error("contract \"" + eosio::name_to_string(contract) + "\" is not loaded");
215
- auto& c = contract_it->second;
216
-
217
- auto action_result_it = c.action_result_types.find(name{action_result});
218
- if (action_result_it == c.action_result_types.end())
219
- throw std::runtime_error("contract \"" + eosio::name_to_string(contract) +
220
- "\" does not have action_result \"" + eosio::name_to_string(action_result) + "\"");
221
- return action_result_it->second.c_str();
222
- });
223
- }
224
-
225
- extern "C" abieos_bool abieos_json_to_bin(abieos_context* context, uint64_t contract, const char* type,
226
- const char* json) {
227
- fix_null_str(type);
228
- fix_null_str(json);
229
- return handle_exceptions(context, false, [&] {
230
- context->last_error = "json parse error";
231
- auto contract_it = context->contracts.find(::abieos::name{contract});
232
- if (contract_it == context->contracts.end())
233
- return set_error(context, "contract \"" + eosio::name_to_string(contract) + "\" is not loaded");
234
- std::string error;
235
- auto t = contract_it->second.get_type(type);
236
- context->result_bin.clear();
237
- context->result_bin = t->json_to_bin(json);
238
- return true;
239
- });
240
- }
241
-
242
- extern "C" abieos_bool abieos_json_to_bin_reorderable(abieos_context* context, uint64_t contract, const char* type,
243
- const char* json) {
244
- fix_null_str(type);
245
- fix_null_str(json);
246
- return handle_exceptions(context, false, [&] {
247
- context->last_error = "json parse error";
248
- auto contract_it = context->contracts.find(::abieos::name{contract});
249
- if (contract_it == context->contracts.end())
250
- return set_error(context, "contract \"" + eosio::name_to_string(contract) + "\" is not loaded");
251
- std::string error;
252
- auto t = contract_it->second.get_type(type);
253
- context->result_bin.clear();
254
- context->result_bin = t->json_to_bin_reorderable(json);
255
- return true;
256
- });
257
- }
258
-
259
- extern "C" const char* abieos_bin_to_json(abieos_context* context, uint64_t contract, const char* type,
260
- const char* data, size_t size) {
261
- fix_null_str(type);
262
- return handle_exceptions(context, nullptr, [&]() -> const char* {
263
- if (!data)
264
- size = 0;
265
- context->last_error = "binary decode error";
266
- auto contract_it = context->contracts.find(::abieos::name{contract});
267
- std::string error;
268
- if (contract_it == context->contracts.end()) {
269
- (void)set_error(error, "contract \"" + eosio::name_to_string(contract) + "\" is not loaded");
270
- return nullptr;
271
- }
272
- auto t = contract_it->second.get_type(type);
273
- eosio::input_stream bin{data, size};
274
- context->result_str = t->bin_to_json(bin);
275
- if (bin.pos != bin.end)
276
- throw std::runtime_error("Extra data");
277
- return context->result_str.c_str();
278
- });
279
- }
280
-
281
- extern "C" const char* abieos_hex_to_json(abieos_context* context, uint64_t contract, const char* type,
282
- const char* hex) {
283
- fix_null_str(hex);
284
- return handle_exceptions(context, nullptr, [&]() -> const char* {
285
- std::vector<char> data;
286
- std::string error;
287
- if (!unhex(error, hex, hex + strlen(hex), std::back_inserter(data))) {
288
- if (!error.empty())
289
- set_error(context, std::move(error));
290
- return nullptr;
291
- }
292
- return abieos_bin_to_json(context, contract, type, data.data(), data.size());
293
- });
294
- }
295
-
296
- extern "C" abieos_bool abieos_abi_json_to_bin(abieos_context* context, const char* abi_json) {
297
- fix_null_str(abi_json);
298
- return handle_exceptions(context, false, [&] {
299
- std::string abi_copy{abi_json};
300
- eosio::json_token_stream json_stream(abi_copy.data());
301
- abi_def def{};
302
- std::string error;
303
- from_json(def, json_stream);
304
- if (!check_abi_version(def.version, error)) {
305
- return set_error(context, std::move(error));
306
- }
307
- context->result_bin = convert_to_bin(def);
308
- return true;
309
- });
310
- }
311
-
312
- extern "C" const char* abieos_abi_bin_to_json(abieos_context* context, const char* abi_bin_data,
313
- const size_t abi_bin_data_size) {
314
- return handle_exceptions(context, nullptr, [&]() -> const char* {
315
- if (!abi_bin_data || abi_bin_data_size == 0) {
316
- set_error(context, "no data");
317
- return nullptr;
318
- }
319
- eosio::input_stream bin_stream{abi_bin_data, abi_bin_data_size};
320
- abi_def def{};
321
- from_bin(def, bin_stream);
322
- std::string error;
323
- if (!check_abi_version(def.version, error)) {
324
- set_error(context, std::move(error));
325
- return nullptr;
326
- }
327
- std::vector<char> bytes;
328
- eosio::vector_stream byte_stream(bytes);
329
- to_json(def, byte_stream);
330
-
331
- context->result_str.assign(bytes.begin(), bytes.end());
332
- return context->result_str.c_str();
333
- });
334
- }