From f99223f08a697b82eb62f41a8addb82072d46f8f Mon Sep 17 00:00:00 2001
From: "alexey.stratulat" <alexey.stratulat@demlabs.net>
Date: Fri, 17 Apr 2020 21:28:18 +0700
Subject: [PATCH] [+] Added functions getPath, openFile, open, save,
 getCertsNumber and destructor close for ChainWallet object.

---
 include/dap_chain_wallet_python.h | 13 +++++++---
 src/dap_chain_wallet_python.c     | 43 +++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/include/dap_chain_wallet_python.h b/include/dap_chain_wallet_python.h
index 889ecf0..be59c12 100644
--- a/include/dap_chain_wallet_python.h
+++ b/include/dap_chain_wallet_python.h
@@ -28,7 +28,7 @@ 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);
 
-PyObject *dap_chain_wallet_close_py(PyObject *self, PyObject *argv);
+void dap_chain_wallet_close_py(PyDapChainWalletObject *self);
 
 PyObject *dap_cert_to_addr_py(PyObject *self, PyObject *argv);
 
@@ -37,10 +37,15 @@ PyObject *dap_chain_wallet_get_certs_number_py(PyObject *self, PyObject *argv);
 PyObject *dap_chain_wallet_get_pkey_py(PyObject *self, PyObject *argv);
 PyObject *dap_chain_wallet_get_key_p(PyObject *self, PyObject *argv);
 
-PyObject *dap_chain_wallet_save_file_py(PyObject *self, PyObject *argv);
+//PyObject *dap_chain_wallet_save_file_py(PyObject *self, PyObject *argv);
 
 static PyMethodDef ChainWalletMethods[] = {
-        {NULL, NULL, 0, NULL}
+    {"getPath", (PyCFunction)dap_chain_wallet_get_path_py, METH_VARARGS | METH_STATIC, ""},
+    {"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, ""},
+    {"getCertsNumber", (PyCFunction)dap_chain_wallet_get_certs_number_py, METH_NOARGS, ""},
+    {NULL, NULL, 0, NULL}
 };
 
 static PyTypeObject DapChainWallet_dapChainWalletType = {
@@ -48,7 +53,7 @@ static PyTypeObject DapChainWallet_dapChainWalletType = {
     "ChainWallet",             /* tp_name */
     sizeof(PyDapChainWalletObject),         /* tp_basicsize */
     0,                         /* tp_itemsize */
-    0,                         /* tp_dealloc */
+    (destructor)dap_chain_wallet_close_py, /* tp_dealloc */
     0,                         /* tp_print */
     0,                         /* tp_getattr */
     0,                         /* tp_setattr */
diff --git a/src/dap_chain_wallet_python.c b/src/dap_chain_wallet_python.c
index 40b7f6f..22a1cfd 100644
--- a/src/dap_chain_wallet_python.c
+++ b/src/dap_chain_wallet_python.c
@@ -6,3 +6,46 @@ int dap_chain_wallet_init_py(void){
 void dap_chain_wallet_deinit_py(void){
     dap_chain_wallet_deinit();
 }
+
+PyObject *dap_chain_wallet_get_path_py(PyObject *self, PyObject *argv){
+    (void)self;
+    (void)argv;
+    const char *path = dap_chain_wallet_get_path(g_config);
+    return Py_BuildValue("(s)", path);
+}
+
+PyObject *dap_chain_wallet_open_file_py(PyObject *self, PyObject *argv){
+    (void)self;
+    const char *file_path;
+    if (!PyArg_ParseTuple(argv, "s", &file_path))
+        return NULL;
+    PyObject *obj_wallet = _PyObject_New(&DapChainWallet_dapChainWalletType);
+    ((PyDapChainWalletObject*)obj_wallet)->wallet = dap_chain_wallet_open_file(file_path);
+    return Py_BuildValue("(O)", obj_wallet);
+}
+PyObject *dap_chain_wallet_open_py(PyObject *self, PyObject *argv){
+    (void)self;
+    const char *wallet_name;
+    const char *wallet_path;
+    if (!PyArg_ParseTuple(argv, "ss", &wallet_name, &wallet_path))
+        return NULL;
+    PyObject *obj_wallet = _PyObject_New(&DapChainWallet_dapChainWalletType);
+    ((PyDapChainWalletObject*)obj_wallet)->wallet = dap_chain_wallet_open(wallet_name, wallet_path);
+    return Py_BuildValue("(O)", obj_wallet);
+}
+PyObject *dap_chain_wallet_save_py(PyObject *self, PyObject *argv){
+    (void)argv;
+    int result = dap_chain_wallet_save(((PyDapChainWalletObject*)self)->wallet);
+    return PyLong_FromLong(result);
+}
+
+void dap_chain_wallet_close_py(PyDapChainWalletObject *self){
+    dap_chain_wallet_close(self->wallet);
+    Py_TYPE(self)->tp_free((PyObject*)self);
+}
+
+PyObject *dap_chain_wallet_get_certs_number_py(PyObject *self, PyObject *argv){
+    (void)argv;
+    size_t result = dap_chain_wallet_get_certs_number(((PyDapChainWalletObject*)self)->wallet);
+    return PyLong_FromLong(result);
+}
-- 
GitLab