| #include <chrono> |
| 6 | #include <condition_variable> |
| 7 | #include <cstdint> |
| 8 | #include <exception> |
| 9 | #include <functional> |
| 10 | #include <iostream> |
| 11 | #include <map> |
| 12 | #include <memory> |
| 13 | #include <mutex> |
| 14 | #include <optional> |
| 15 | #include <stdexcept> |
| 16 | #include <streambuf> |
| 17 | #include <type_traits> |
| 18 | #include <variant> |
| 19 | #include <vector> |
| 20 |
| 21 | #include "gram_wallet_scaffolding.hpp" |
| 22 |
| 23 | #ifndef UNIFFI_CPP_RUST_STREAM |
| 24 | #define UNIFFI_CPP_RUST_STREAM |
| 25 | namespace uniffi { |
| 26 | struct RustStreamBuffer: std::basic_streambuf<char> { |
| 27 | RustStreamBuffer(RustBuffer *buf) { |
| 28 | char* data = reinterpret_cast<char*>(buf->data); |
| 29 | this->setg(data, data, data + buf->len); |
| 30 | this->setp(data, data + buf->capacity); |
| 31 | } |
| 32 | ~RustStreamBuffer() = default; |
| 33 |
| 34 | private: |
| 35 | RustStreamBuffer() = delete; |
| 36 | RustStreamBuffer(const RustStreamBuffer &) = delete; |
| 37 | RustStreamBuffer(RustStreamBuffer &&) = delete; |
| 38 |
| 39 | RustStreamBuffer &operator=(const RustStreamBuffer &) = delete; |
| 40 | RustStreamBuffer &operator=(RustStreamBuffer &&) = delete; |
| 41 | }; |
| 42 |
| 43 | struct RustStream: std::basic_iostream<char> { |
| 44 | RustStream(RustBuffer *buf): |
| 45 | std::basic_iostream<char>(&streambuf), streambuf(RustStreamBuffer(buf)) { } |
| 46 |
| 47 | template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 48 | RustStream &operator>>(T &val) { |
| 49 | read(reinterpret_cast<char *>(&val), sizeof(T)); |
| 50 |
| 51 | if (std::endian::native != std::endian::big) { |
| 52 | auto bytes = reinterpret_cast<char *>(&val); |
| 53 |
| 54 | std::reverse(bytes, bytes + sizeof(T)); |
| 55 | } |
| 56 |
| 57 | return *this; |
| 58 | } |
| 59 |
| 60 | template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 61 | RustStream &operator<<(T val) { |
| 62 | if (std::endian::native != std::endian::big) { |
| 63 | auto bytes = reinterpret_cast<char *>(&val); |
| 64 |
| 65 | std::reverse(bytes, bytes + sizeof(T)); |
| 66 | } |
| 67 |
| 68 | write(reinterpret_cast<char *>(&val), sizeof(T)); |
| 69 |
| 70 | return *this; |
| 71 | } |
| 72 | private: |
| 73 | RustStreamBuffer streambuf; |
| 74 | }; |
| 75 |
| 76 | } |
| 77 | #endif |
| 78 |
| 79 | namespace gram_wallet { |
| 80 | struct GramWalletError; |
| 81 |
| 82 | namespace uniffi { |
| 83 | struct FfiConverterGramWalletError; |
| 84 | } // namespace uniffi |
| 85 |
| 86 | struct GramWalletError: std::runtime_error { |
| 87 | friend uniffi::FfiConverterGramWalletError; |
| 88 |
| 89 | GramWalletError() : std::runtime_error("") {} |
| 90 | GramWalletError(const std::string &what_arg) : std::runtime_error(what_arg) {} |
| 91 |
| 92 | virtual ~GramWalletError() = default; |
| 93 |
| 94 | virtual void throw_underlying() { |
| 95 | throw *this; |
| 96 | } |
| 97 |
| 98 | protected: |
| 99 | virtual int32_t get_variant_idx() const { |
| 100 | return 0; |
| 101 | }; |
| 102 | }; |
| 103 | /** |
| 104 | * Contains variants of GramWalletError |
| 105 | */ |
| 106 | namespace gram_wallet_error { |
| 107 |
| 108 | struct InvalidData: GramWalletError { |
| 109 |
| 110 | InvalidData() : GramWalletError("") {} |
| 111 | InvalidData(const std::string &what_arg) : GramWalletError(what_arg) {} |
| 112 |
| 113 | void throw_underlying() override { |
| 114 | throw *this; |
| 115 | } |
| 116 |
| 117 | protected: |
| 118 | int32_t get_variant_idx() const override { |
| 119 | return 1; |
| 120 | } |
| 121 | }; |
| 122 |
| 123 | struct Protocol: GramWalletError { |
| 124 | std::string message; |
| 125 |
| 126 | Protocol() : GramWalletError("") {} |
| 127 | Protocol(const std::string &what_arg) : GramWalletError(what_arg) {} |
| 128 |
| 129 | void throw_underlying() override { |
| 130 | throw *this; |
| 131 | } |
| 132 |
| 133 | protected: |
| 134 | int32_t get_variant_idx() const override { |
| 135 | return 2; |
| 136 | } |
| 137 | }; |
| 138 | } // namespace gram_wallet_error |
| 139 |
| 140 | namespace uniffi { |
| 141 | using ::uniffi::RustStream; |
| 142 | using ::uniffi::RustStreamBuffer; |
| 143 |
| 144 | RustBuffer rustbuffer_alloc(uint64_t); |
| 145 | RustBuffer rustbuffer_from_bytes(const ForeignBytes &); |
| 146 | void rustbuffer_free(RustBuffer); |
| 147 | template <typename T> struct HandleMap { |
| 148 | HandleMap() = default; |
| 149 |
| 150 | std::shared_ptr<T> at(uint64_t handle) { |
| 151 | std::lock_guard<std::mutex> guard(this->mutex); |
| 152 |
| 153 | return this->map.at(handle); |
| 154 | } |
| 155 |
| 156 | uint64_t insert(std::shared_ptr<T> impl) { |
| 157 | std::lock_guard<std::mutex> guard(this->mutex); |
| 158 |
| 159 | auto handle = this->cur_handle; |
| 160 |
| 161 | this->map.insert({ handle, impl }); |
| 162 | this->cur_handle += 1; |
| 163 |
| 164 | return handle; |
| 165 | } |
| 166 |
| 167 | void erase(uint64_t handle) { |
| 168 | // We store the object here to avoid re-entrant locking |
| 169 | std::shared_ptr<T> cleanup; |
| 170 | { |
| 171 | std::lock_guard<std::mutex> guard(this->mutex); |
| 172 | auto it = this->map.find(handle); |
| 173 | if (it != this->map.end()) { |
| 174 | cleanup = it->second; |
| 175 | this->map.erase(it); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | private: |
| 180 | HandleMap(const HandleMap<T> &) = delete; |
| 181 | HandleMap(HandleMap<T> &&) = delete; |
| 182 |
| 183 | HandleMap<T> &operator=(const HandleMap<T> &) = delete; |
| 184 | HandleMap<T> &operator=(HandleMap<T> &&) = delete; |
| 185 |
| 186 | std::mutex mutex; |
| 187 | uint64_t cur_handle = 0; |
| 188 | std::map<uint64_t, std::shared_ptr<T>> map; |
| 189 | }; |
| 190 | struct FfiConverterBool { |
| 191 | static bool lift(uint8_t); |
| 192 | static uint8_t lower(bool); |
| 193 | static bool read(RustStream &); |
| 194 | static void write(RustStream &, bool); |
| 195 | static uint64_t allocation_size(bool); |
| 196 | }; |
| 197 | struct FfiConverterString { |
| 198 | static std::string lift(RustBuffer buf); |
| 199 | static RustBuffer lower(const std::string &); |
| 200 | static std::string read(RustStream &); |
| 201 | static void write(RustStream &, const std::string &); |
| 202 | static uint64_t allocation_size(const std::string &); |
| 203 | }; |
| 204 |
| 205 | struct FfiConverterGramWalletError { |
| 206 | static std::shared_ptr<GramWalletError> lift(RustBuffer buf); |
| 207 | static RustBuffer lower(const GramWalletError &); |
| 208 | static std::shared_ptr<GramWalletError> read(RustStream &stream); |
| 209 | static void write(RustStream &stream, const GramWalletError &); |
| 210 | static uint64_t allocation_size(const GramWalletError &); |
| 211 | }; |
| 212 | } // namespace uniffi |
| 213 |
| 214 | /** |
| 215 | * Validates the single-root BoC envelope before passing untrusted cells to the wallet. |
| 216 | */ |
| 217 | bool is_valid_cell_boc(const std::string &boc); |
| 218 | /** |
| 219 | * Hash of the message body, as used by the MTProto transaction matcher. |
| 220 | * This deliberately differs from the hash of the complete external message. |
| 221 | */ |
| 222 | std::string message_body_hash(const std::string &boc); |
| 223 | /** |
| 224 | * Stateless cryptographic adapter. Java owns TON Connect envelopes, methods, |
| 225 | * capabilities and MTProto workflow; secret keys never leave this call. |
| 226 | */ |
| 227 | std::string gram_ton_connect_crypto(const std::string &phrase, const std::string ¶meters); |
| 228 | } // namespace gram_wallet |