diff --git a/tvm-python/PyCell.cpp b/tvm-python/PyCell.cpp index 6fcbca294..dff431b17 100644 --- a/tvm-python/PyCell.cpp +++ b/tvm-python/PyCell.cpp @@ -33,6 +33,10 @@ std::string PyCell::toString() const { } } +py::bytes PyCell::to_slice() const { + return std_boc_serialize(my_cell, 31).move_as_ok().data(); +} + std::string PyCell::dump() const { if (my_cell.is_null()) { throw std::invalid_argument("Cell is null"); diff --git a/tvm-python/PyCell.h b/tvm-python/PyCell.h index 5e7ffc7ef..579e0fed9 100644 --- a/tvm-python/PyCell.h +++ b/tvm-python/PyCell.h @@ -1,8 +1,11 @@ // Copyright 2023 Disintar LLP / andrey@head-labs.com - +#include "third-party/pybind11/include/pybind11/pybind11.h" #include "vm/vm.h" +namespace py = pybind11; + + #ifndef TON_PYCELL_H #define TON_PYCELL_H @@ -22,6 +25,7 @@ class PyCell { std::string dump() const; std::string dump_as_tlb(std::string tlb_type) const; std::string to_boc() const; + py::bytes to_slice() const; PyCell copy() const; bool is_null() const; diff --git a/tvm-python/PyKeys.cpp b/tvm-python/PyKeys.cpp index ffe2d361b..5e354a87b 100644 --- a/tvm-python/PyKeys.cpp +++ b/tvm-python/PyKeys.cpp @@ -65,6 +65,19 @@ std::string public_buffer_to_hex(td::Slice buffer) { PyPublicKey::PyPublicKey(std::string key_int) : key(td::Ed25519::PublicKey{td::SecureString{hex_to_bytes(key_int)}}) { } +std::tuple PyPublicKey::verify_signature(char *data, char *signature) { + auto R = key.verify_signature(td::Slice(data), td::Slice(signature)); + std::string err_msg; + bool valid = true; + + if (R.is_error()) { + valid = false; + err_msg = R.move_as_error().to_string(); + } + + return std::tuple(valid, err_msg); +}; + std::string PyPublicKey::get_public_key_hex() { return public_buffer_to_hex(key.as_octet_string()); } @@ -77,6 +90,15 @@ std::string PyPrivateKey::get_private_key_hex() { return to_hex(key.as_octet_string()); } +py::bytes PyPrivateKey::sign(char *data) { + auto R = key.sign(td::Slice(data)); + if (R.is_error()) { + throw std::invalid_argument(R.move_as_error().to_string()); + } + + return R.move_as_ok().as_slice().data(); +}; + PyPublicKey PyPrivateKey::get_public_key() { return PyPublicKey(key.get_public_key().move_as_ok()); } diff --git a/tvm-python/PyKeys.h b/tvm-python/PyKeys.h index 0e2adbcd5..15838536a 100644 --- a/tvm-python/PyKeys.h +++ b/tvm-python/PyKeys.h @@ -1,9 +1,12 @@ // Copyright 2023 Disintar LLP / andrey@head-labs.com +#include "third-party/pybind11/include/pybind11/pybind11.h" #include #include #include "tonlib/tonlib/keys/Mnemonic.h" +namespace py = pybind11; + #ifndef TON_PYKEYS_H #define TON_PYKEYS_H @@ -13,6 +16,7 @@ class PyPublicKey { PyPublicKey(std::string key_int); PyPublicKey(td::Ed25519::PublicKey key_) : key(std::move(key_)){}; std::string get_public_key_hex(); + std::tuple verify_signature(char *data, char *signature); PyPublicKey(const PyPublicKey& other) : key(td::Ed25519::PublicKey(other.key.as_octet_string())){}; }; @@ -25,6 +29,7 @@ class PyPrivateKey { PyPrivateKey(td::Ed25519::PrivateKey key_) : key(td::Ed25519::PrivateKey(key_.as_octet_string())){}; std::string get_private_key_hex(); PyPublicKey get_public_key(); + py::bytes sign(char* data); }; class PyMnemonic { diff --git a/tvm-python/python_ton.cpp b/tvm-python/python_ton.cpp index 6995b3e26..6ac6a37de 100644 --- a/tvm-python/python_ton.cpp +++ b/tvm-python/python_ton.cpp @@ -164,6 +164,7 @@ PYBIND11_MODULE(python_ton, m) { .def("to_boc", &PyCell::to_boc) .def("__repr__", &PyCell::toString) .def("copy", &PyCell::copy) + .def("to_slice", &PyCell::to_slice) .def("is_null", &PyCell::is_null); py::class_(m, "PyCellBuilder", py::module_local()) @@ -349,13 +350,15 @@ PYBIND11_MODULE(python_ton, m) { py::class_(m, "PyPublicKey", py::module_local()) .def(py::init(), py::arg("key_hex")) - .def("get_public_key_hex", &PyPublicKey::get_public_key_hex); + .def("get_public_key_hex", &PyPublicKey::get_public_key_hex) + .def("verify_signature", &PyPublicKey::verify_signature); py::class_(m, "PyPrivateKey", py::module_local()) .def(py::init<>()) .def(py::init(), py::arg("key_hex")) .def("get_private_key_hex", &PyPrivateKey::get_private_key_hex) - .def("get_public_key", &PyPrivateKey::get_public_key); + .def("get_public_key", &PyPrivateKey::get_public_key) + .def("sign", &PyPrivateKey::sign); py::class_(m, "PyMnemonic", py::module_local()) .def(py::init, std::string>(), py::arg("mnemonic"), py::arg("mnemonic_password"))