Skip to content

Commit

Permalink
Add signature & signature check
Browse files Browse the repository at this point in the history
  • Loading branch information
tvorogme committed May 10, 2024
1 parent b9ab021 commit 2daf86a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions tvm-python/PyCell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 5 additions & 1 deletion tvm-python/PyCell.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright 2023 Disintar LLP / [email protected]


#include "third-party/pybind11/include/pybind11/pybind11.h"
#include "vm/vm.h"

namespace py = pybind11;


#ifndef TON_PYCELL_H
#define TON_PYCELL_H

Expand All @@ -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;

Expand Down
22 changes: 22 additions & 0 deletions tvm-python/PyKeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, std::string> 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<bool, std::string>(valid, err_msg);
};

std::string PyPublicKey::get_public_key_hex() {
return public_buffer_to_hex(key.as_octet_string());
}
Expand All @@ -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());
}
Expand Down
5 changes: 5 additions & 0 deletions tvm-python/PyKeys.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright 2023 Disintar LLP / [email protected]

#include "third-party/pybind11/include/pybind11/pybind11.h"
#include <vector>
#include <string.h>
#include "tonlib/tonlib/keys/Mnemonic.h"

namespace py = pybind11;

#ifndef TON_PYKEYS_H
#define TON_PYKEYS_H

Expand All @@ -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<bool, std::string> verify_signature(char *data, char *signature);

PyPublicKey(const PyPublicKey& other) : key(td::Ed25519::PublicKey(other.key.as_octet_string())){};
};
Expand All @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions tvm-python/python_ton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_<PyCellBuilder>(m, "PyCellBuilder", py::module_local())
Expand Down Expand Up @@ -349,13 +350,15 @@ PYBIND11_MODULE(python_ton, m) {

py::class_<PyPublicKey>(m, "PyPublicKey", py::module_local())
.def(py::init<std::string>(), 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_<PyPrivateKey>(m, "PyPrivateKey", py::module_local())
.def(py::init<>())
.def(py::init<std::string>(), 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_<PyMnemonic>(m, "PyMnemonic", py::module_local())
.def(py::init<std::vector<std::string>, std::string>(), py::arg("mnemonic"), py::arg("mnemonic_password"))
Expand Down

0 comments on commit 2daf86a

Please sign in to comment.