From 6846a89105badb45b6b1c66ee779f8bf2d5c94c9 Mon Sep 17 00:00:00 2001 From: "alexey.stratulat" <alexey.stratulat@demlabs.net> Date: Tue, 21 Apr 2020 15:54:47 +0700 Subject: [PATCH] [+] Added functions getPKey, certToAddr. Added constructor for object ChainWallet. --- include/dap_chain_wallet_python.h | 9 +++++--- src/dap_chain_wallet_python.c | 38 ++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/dap_chain_wallet_python.h b/include/dap_chain_wallet_python.h index f08c5867..52b033c9 100644 --- a/include/dap_chain_wallet_python.h +++ b/include/dap_chain_wallet_python.h @@ -7,7 +7,8 @@ #include "wrapping_dap_chain_common.h" #include "libdap_crypto_key_python.h" #include "wrapping_dap_sign.h" -//#include "wrapping_dap_sign +#include "wrapping_dap_pkey.h" +#include "wrapping_cert.h" #ifdef __cplusplus extern "C"{ @@ -27,7 +28,7 @@ void dap_chain_wallet_deinit_py(void); PyObject *dap_chain_wallet_get_path_py(PyObject *self, PyObject *argv); PyObject *dap_chain_wallet_create_with_seed_py(PyObject *self, PyObject *argv); -PyObject *dap_chain_wallet_create_py(PyObject *self, PyObject *argv); +PyObject *dap_chain_wallet_create_py(PyTypeObject *type, PyObject *argv, PyObject *kwds); PyObject *dap_chain_wallet_open_file_py(PyObject *self, PyObject *argv); PyObject *dap_chain_wallet_open_py(PyObject *self, PyObject *argv); PyObject *dap_chain_wallet_save_py(PyObject *self, PyObject *argv); @@ -49,8 +50,10 @@ static PyMethodDef ChainWalletMethods[] = { {"openFile", (PyCFunction)dap_chain_wallet_open_file_py, METH_VARARGS | METH_STATIC, ""}, {"open", (PyCFunction)dap_chain_wallet_open_py, METH_VARARGS | METH_STATIC, ""}, {"save", (PyCFunction)dap_chain_wallet_save_py, METH_NOARGS, ""}, + {"certToAddr", (PyCFunction)dap_cert_to_addr_py, METH_VARARGS | METH_STATIC, ""}, {"getAddr", (PyCFunction)dap_chain_wallet_get_addr_py, METH_VARARGS, ""}, {"getCertsNumber", (PyCFunction)dap_chain_wallet_get_certs_number_py, METH_NOARGS, ""}, + {"getPKey", (PyCFunction)dap_chain_wallet_get_pkey_py, METH_VARARGS, ""}, {"getKey", (PyCFunction)dap_chain_wallet_get_key_py, METH_VARARGS, ""}, {NULL, NULL, 0, NULL} }; @@ -94,7 +97,7 @@ static PyTypeObject DapChainWallet_dapChainWalletType = { 0, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ + dap_chain_wallet_create_py,/* tp_new */ 0, /* tp_free */ 0, /* tp_is_gc*/ 0, /* tp_bases*/ diff --git a/src/dap_chain_wallet_python.c b/src/dap_chain_wallet_python.c index 323ac6e9..df71af90 100644 --- a/src/dap_chain_wallet_python.c +++ b/src/dap_chain_wallet_python.c @@ -37,6 +37,24 @@ PyObject *dap_chain_wallet_create_with_seed_py(PyObject *self, PyObject *argv){ seed_size); return Py_BuildValue("(O)", obj_wallet); } +PyObject *dap_chain_wallet_create_py(PyTypeObject *type, PyObject *argv, PyObject *kwds){ + (void)kwds; + PyDapChainWalletObject *self; + const char *wallet_name; + const char *path_wallets; + PyObject *obj_sign_type; + if (!PyArg_ParseTuple(argv, "ssO", &wallet_name, &path_wallets, &obj_sign_type)) + return NULL; + self = (PyDapChainWalletObject*)type->tp_alloc(type, 0); + if (self != NULL){ + self->wallet = dap_chain_wallet_create(wallet_name, path_wallets, *((PyDapSignTypeObject*)obj_sign_type)->sign_type); + if (self->wallet == NULL){ + Py_XDECREF(self); + return NULL; + } + } + return (PyObject*)self; +} PyObject *dap_chain_wallet_open_file_py(PyObject *self, PyObject *argv){ (void)self; const char *file_path; @@ -69,7 +87,16 @@ void dap_chain_wallet_close_py(PyDapChainWalletObject *self){ PyObject *dap_cert_to_addr_py(PyObject *self, PyObject *argv){ (void)self; - return NULL; + PyObject *obj_cert; + PyObject *obj_net_id; + if (!PyArg_ParseTuple(argv, "OO", &obj_cert, &obj_net_id)) + return NULL; + PyObject *obj_addr = _PyObject_New(&DapChainAddrObject_DapChainAddrObjectType); + ((PyDapChainAddrObject*)obj_addr)->addr = dap_cert_to_addr( + ((PyCryptoCertObject*)obj_cert)->cert, + ((PyDapChainNetIdObject*)obj_net_id)->net_id + ); + return Py_BuildValue("(O)", obj_addr); } PyObject *dap_chain_wallet_get_addr_py(PyObject *self, PyObject *argv){ @@ -88,6 +115,15 @@ PyObject *dap_chain_wallet_get_certs_number_py(PyObject *self, PyObject *argv){ size_t result = dap_chain_wallet_get_certs_number(((PyDapChainWalletObject*)self)->wallet); return PyLong_FromLong(result); } +PyObject *dap_chain_wallet_get_pkey_py(PyObject *self, PyObject *argv){ + uint32_t key_idx; + if (!PyArg_ParseTuple(argv, "I", &key_idx)) + return NULL; + PyObject *obj_pkey = _PyObject_New(&DapPkeyObject_DapPkeyObjectType); + ((PyDapPkeyObject*)obj_pkey)->pkey = dap_chain_wallet_get_pkey(((PyDapChainWalletObject*)self)->wallet, + key_idx); + return Py_BuildValue("(O)", obj_pkey); +} PyObject *dap_chain_wallet_get_key_py(PyObject *self, PyObject *argv){ uint32_t key_idx; if (!PyArg_ParseTuple(argv, "I", &key_idx)) -- GitLab