From 43de54e45af82ebc5295515017a43f0a9e0bf34d Mon Sep 17 00:00:00 2001 From: "alexey.stratulat" <alexey.stratulat@demlabs.net> Date: Thu, 17 Oct 2019 21:06:12 +0700 Subject: [PATCH] [+] Added wrapping dap_chain_datum_tx. --- include/wrapping_dap_chain_datum_tx.h | 213 ++++++++++++++++++++++++++ src/wrapping_dap_chain_datum_tx.c | 152 ++++++++++++++++++ 2 files changed, 365 insertions(+) create mode 100644 include/wrapping_dap_chain_datum_tx.h create mode 100644 src/wrapping_dap_chain_datum_tx.c diff --git a/include/wrapping_dap_chain_datum_tx.h b/include/wrapping_dap_chain_datum_tx.h new file mode 100644 index 00000000..e6504429 --- /dev/null +++ b/include/wrapping_dap_chain_datum_tx.h @@ -0,0 +1,213 @@ +#ifndef _WRAPPING_DAP_CHAIN_DATUM_TX_ +#define _WRAPPING_DAP_CHAIN_DATUM_TX_ + +#include "Python.h" +#include "dap_chain_datum_tx.h" +#include "wrapping_dap_chain_common.h" +#include "libdap_crypto_key_python.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* DAP chain tx iter type */ +typedef struct PyDapChainTxItemType{ + PyObject_HEAD +}PyDapChainTxItemTypeObject; + +PyObject *TX_ITEM_TYPE_IN_PY(void); +PyObject *TX_ITEM_TYPE_OUT_PY(void); +PyObject *TX_ITEM_TYPE_PKEY_PY(void); +PyObject *TX_ITEM_TYPE_SIG_PY(void); +PyObject *TX_ITEM_TYPE_TOKEN_PY(void); +PyObject *TX_ITEM_TYPE_IN_COND_PY(void); +PyObject *TX_ITEM_TYPE_OUT_COND_PY(void); +PyObject *TX_ITEM_TYPE_OUT_SERVICE_RECEIPT_PY(void); + +static PyMethodDef PyDapChainTxItemTypeObjectMethods[] ={ + {"TX_ITEM_TYPE_IN", (PyCFunction)TX_ITEM_TYPE_IN_PY, METH_NOARGS | METH_STATIC, ""}, + {"TX_ITEM_TYPE_OUT", (PyCFunction)TX_ITEM_TYPE_OUT_PY, METH_NOARGS | METH_STATIC, ""}, + {"TX_ITEM_TYPE_PKEY", (PyCFunction)TX_ITEM_TYPE_PKEY_PY, METH_NOARGS | METH_STATIC, ""}, + {"TX_ITEM_TYPE_SIG_PY", (PyCFunction)TX_ITEM_TYPE_SIG_PY, METH_NOARGS | METH_STATIC, ""}, + {"TX_ITEM_TYPE_TOKEN", (PyCFunction)TX_ITEM_TYPE_TOKEN_PY, METH_NOARGS | METH_STATIC, ""}, + {"TX_ITEM_TYPE_IN_COND", (PyCFunction)TX_ITEM_TYPE_IN_COND_PY, METH_NOARGS | METH_STATIC, ""}, + {"TX_ITEM_TYPE_OUT_COND", (PyCFunction)TX_ITEM_TYPE_OUT_COND_PY, METH_NOARGS | METH_STATIC, ""}, + {"TX_ITEM_TYPE_OUT_SERVICE_RECEIPT_PY", (PyCFunction)TX_ITEM_TYPE_OUT_SERVICE_RECEIPT_PY, + METH_NOARGS | METH_STATIC, ""}, + {NULL, NULL, 0, NULL} +}; + +static PyTypeObject DapChainTxItemObject_DapChainTxItemTypeObjectType = { + PyVarObject_HEAD_INIT(NULL, 0) + "CellFrame.ChainTxItemType", /* tp_name */ + sizeof(PyDapChainTxItemTypeObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + 0, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + "Chain tx item type object", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + PyDapChainTxItemTypeObjectMethods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ +}; + +/* -------------------------------------- */ + +/* DAP chain tx cond*/ +typedef struct PyDapChainTxCondType{ + PyObject_HEAD + dap_chain_tx_cond_type_t *tx_cond_type_t; +}PyDapChainTxCondTypeObject; + +static PyTypeObject DapChainTxType_DapChainTxCondTypeObject = { + PyVarObject_HEAD_INIT(NULL, 0) + "CellFrame.ChainTxCondType", /* tp_name */ + sizeof(PyDapChainTxCondTypeObject),/* tp_basicsize */ + 0, /* tp_itemsize */ + 0, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + "Chain tx cond type object", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ +}; + +/* -------------------------------------- */ + +/* DAP chain datum tx */ +typedef struct PyDapChainDatumTx{ + PyObject_HEAD + dap_chain_datum_tx_t *datum_tx; +}PyDapChainDatumTxObject; + +PyObject *PyDapChainDatumTxObject_create(PyTypeObject *type_object, PyObject *args, PyObject *kwds); +void PyDapChainDatumTxObject_delete(PyDapChainDatumTxObject* datumTx); +PyObject *dap_chain_datum_tx_get_size_py(PyObject *self, PyObject *args); +PyObject *dap_chain_datum_tx_add_item_py(PyObject *self, PyObject *args); +PyObject *dap_chain_datum_tx_add_in_item_py(PyObject *self, PyObject *args); +PyObject *dap_chain_datum_tx_add_out_item_py(PyObject *self, PyObject *args); +PyObject *dap_chain_datum_tx_add_out_cond_item_py(PyObject *self, PyObject *args); +PyObject *dap_chain_datum_tx_add_sign_item_py(PyObject *self, PyObject *args); +PyObject *dap_chain_datum_tx_verify_sign_py(PyObject *self, PyObject *args); + +static dap_chain_datum_tx_t **PyListToDapChainDatumTxArray(PyObject *a_in_obj); +static PyObject* DapChainDatumTxArrayToPyList(dap_chain_datum_tx_t** datum_txs); + + +static PyMethodDef PyDapChainDatumTxObjectMethods[] ={ + {"getSize", (PyCFunction)dap_chain_datum_tx_get_size_py, METH_VARARGS, ""}, + {"addItem", (PyCFunction)dap_chain_datum_tx_add_item_py, METH_VARARGS | METH_STATIC, ""}, + {"addInItem", (PyCFunction)dap_chain_datum_tx_add_in_item_py, METH_VARARGS | METH_STATIC, ""}, + {"addOutItem", (PyCFunction)dap_chain_datum_tx_add_out_item_py, METH_VARARGS | METH_STATIC, ""}, + {"addOutCond", (PyCFunction)dap_chain_datum_tx_add_out_cond_item_py, METH_VARARGS | METH_STATIC, ""}, + {"addSignItem", (PyCFunction)dap_chain_datum_tx_add_sign_item_py, METH_VARARGS | METH_STATIC, ""}, + {"verifySign", (PyCFunction)dap_chain_datum_tx_verify_sign_py, METH_VARARGS, ""}, + {NULL, NULL, 0, NULL} +}; + +static PyTypeObject DapChainDatumTx_DapChainDatumTxObjectType = { + PyVarObject_HEAD_INIT(NULL, 0) + "CellFrame.ChainDatumTx", /* tp_name */ + sizeof(PyDapChainDatumTxObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)PyDapChainDatumTxObject_delete, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + "Chain datum tx object", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + PyDapChainDatumTxObjectMethods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PyDapChainDatumTxObject_create, /* tp_new */ +}; + +/* -------------------------------------- */ + +#ifdef __cplusplus +} +#endif + +#endif //_WRAPPING_DAP_CHAIN_DATUM_TX_ diff --git a/src/wrapping_dap_chain_datum_tx.c b/src/wrapping_dap_chain_datum_tx.c new file mode 100644 index 00000000..03e4a9e4 --- /dev/null +++ b/src/wrapping_dap_chain_datum_tx.c @@ -0,0 +1,152 @@ +#include "wrapping_dap_chain_datum_tx.h" + +/* DAP chain tx iter type */ + +PyObject *TX_ITEM_TYPE_IN_PY(void){ + return PyLong_FromLong(TX_ITEM_TYPE_IN); +} +PyObject *TX_ITEM_TYPE_OUT_PY(void){ + return PyLong_FromLong(TX_ITEM_TYPE_OUT); +} +PyObject *TX_ITEM_TYPE_PKEY_PY(void){ + return PyLong_FromLong(TX_ITEM_TYPE_PKEY); +} +PyObject *TX_ITEM_TYPE_SIG_PY(void){ + return PyLong_FromLong(TX_ITEM_TYPE_SIG); +} +PyObject *TX_ITEM_TYPE_TOKEN_PY(void){ + return PyLong_FromLong(TX_ITEM_TYPE_TOKEN); +} +PyObject *TX_ITEM_TYPE_IN_COND_PY(void){ + return PyLong_FromLong(TX_ITEM_TYPE_IN_COND); +} +PyObject *TX_ITEM_TYPE_OUT_COND_PY(void){ + return PyLong_FromLong(TX_ITEM_TYPE_OUT_COND); +} +PyObject *TX_ITEM_TYPE_OUT_SERVICE_RECEIPT_PY(void){ + return PyLong_FromLong(TX_ITEM_TYPE_OUT_SERVICE_RECEIPT); +} + +/* -------------------------------------- */ + +/* DAP chain datum tx */ +PyObject *PyDapChainDatumTxObject_create(PyTypeObject *type_object, PyObject *args, PyObject *kwds){ + PyDapChainDatumTxObject *obj = (PyDapChainDatumTxObject*)PyType_GenericNew(type_object, args, kwds); + obj->datum_tx = dap_chain_datum_tx_create(); + return (PyObject *)obj; +} +void PyDapChainDatumTxObject_delete(PyDapChainDatumTxObject* datumTx){ + dap_chain_datum_tx_delete(datumTx->datum_tx); + Py_TYPE(datumTx)->tp_free((PyObject*)datumTx); +} + +PyObject *dap_chain_datum_tx_get_size_py(PyObject *self, PyObject *args){ + size_t size = dap_chain_datum_tx_get_size(((PyDapChainDatumTxObject*)self)->datum_tx); + return PyLong_FromSize_t(size); +} +PyObject *dap_chain_datum_tx_add_item_py(PyObject *self, PyObject *args){ + PyObject *in_obj; + u_int8_t *a_item; + if (!PyArg_ParseTuple(args, "O|b", &in_obj, &a_item)) + return NULL; + dap_chain_datum_tx_t **txs = PyListToDapChainDatumTxArray(in_obj); + int res = dap_chain_datum_tx_add_item(txs, a_item); + if (res == -1){ + return NULL; + } else { + PyObject *out = DapChainDatumTxArrayToPyList(txs); + return Py_BuildValue("O", out); + } +} +PyObject *dap_chain_datum_tx_add_in_item_py(PyObject *self, PyObject *args){ + PyObject *in_obj_datum_txs; + PyObject *in_obj_hash_fast; + uint32_t in_tx_out_pref_idx; + if (!PyArg_ParseTuple(args, "O|O|I", &in_obj_datum_txs, &in_obj_hash_fast, &in_tx_out_pref_idx)) + return NULL; + dap_chain_datum_tx_t **a_txs = PyListToDapChainDatumTxArray(in_obj_datum_txs); + int res = dap_chain_datum_tx_add_in_item(a_txs, ((PyDapChainHashFastObject*)in_obj_hash_fast)->hash_fast, + in_tx_out_pref_idx); + if (res == -1){ + return NULL; + } else { + PyObject *out = DapChainDatumTxArrayToPyList(a_txs); + return Py_BuildValue("O", out); + } +} +PyObject *dap_chain_datum_tx_add_out_item_py(PyObject *self, PyObject *args){ + PyObject *in_obj_datum_txs; + PyObject *in_addr; + uint64_t value; + if (!PyArg_ParseTuple(args, "O|O|k", &in_obj_datum_txs, &in_addr, value)) + return NULL; + dap_chain_datum_tx_t **txs = PyListToDapChainDatumTxArray(in_obj_datum_txs); + int res = dap_chain_datum_tx_add_out_item(txs, ((PyDapChainAddrObject*)in_addr)->addr, value); + if (res == -1){ + return NULL; + } else { + PyObject *out = DapChainDatumTxArrayToPyList(txs); + return Py_BuildValue("O", out); + } +} +PyObject *dap_chain_datum_tx_add_out_cond_item_py(PyObject *self, PyObject *args){ + PyObject *obj_txs; + PyObject *obj_key; + PyObject *obj_addr; + uint64_t value; + PyBytesObject *bytes_cond; + size_t cond_size; + if (!PyArg_ParseTuple(args, "O|O|O|k|S|n", &obj_txs, &obj_key, &obj_addr, &value, &bytes_cond, &cond_size)) + return NULL; + dap_chain_datum_tx_t **txs = PyListToDapChainDatumTxArray(obj_txs); + void *cond = PyBytes_AsString((PyObject *)bytes_cond); + + int res = dap_chain_datum_tx_add_out_cond_item(txs, ((PyCryptoKeyObject*)obj_key)->key, + ((PyDapChainAddrObject*)obj_addr)->addr, value, cond, cond_size); + if (res == -1){ + return NULL; + } else { + PyObject *out = DapChainDatumTxArrayToPyList(txs); + return Py_BuildValue("O", out); + } +} +PyObject *dap_chain_datum_tx_add_sign_item_py(PyObject *self, PyObject *args){ + PyObject *obj_txs; + PyObject *obj_key; + if (!PyArg_ParseTuple(args, "O|O", &obj_txs, &obj_key)) + return NULL; + dap_chain_datum_tx_t **txs = PyListToDapChainDatumTxArray(obj_txs); + int res = dap_chain_datum_tx_add_sign_item(txs, ((PyCryptoKeyObject*)obj_key)->key); + if (res == -1){ + return NULL; + } else { + PyObject *out = DapChainDatumTxArrayToPyList(txs); + return Py_BuildValue("O", out); + } +} +PyObject *dap_chain_datum_tx_verify_sign_py(PyObject *self, PyObject *args){ + int res = dap_chain_datum_tx_verify_sign(((PyDapChainDatumTxObject*)self)->datum_tx); + return PyLong_FromLong(res); +} + +static dap_chain_datum_tx_t **PyListToDapChainDatumTxArray(PyObject *a_in_obj){ + Py_ssize_t size = PyList_Size(a_in_obj); + dap_chain_datum_tx_t **txs = calloc(sizeof(dap_chain_datum_tx_t), (size_t)size); + for (Py_ssize_t i = 0; i < size;i++){ + txs[i] = ((PyDapChainDatumTxObject*)PyList_GetItem(a_in_obj, i))->datum_tx; + } + return txs; +} + +static PyObject* DapChainDatumTxArrayToPyList(dap_chain_datum_tx_t** datum_txs){ + size_t len = sizeof(datum_txs) / sizeof(datum_txs[0]); + PyObject* list = PyList_New((Py_ssize_t)len); + for (size_t i=0; i < len; i++){ + PyObject *obj = _PyObject_New(&DapChainDatumTx_DapChainDatumTxObjectType); + ((PyDapChainDatumTxObject*)obj)->datum_tx = datum_txs[i]; + PyList_Append(list, obj); + } + return list; +} + +/* -------------------------------------- */ -- GitLab