Skip to content

Commit e290632

Browse files
authored
Merge pull request #111 from erickcestari/embit-descriptor-parse
add embit descriptor parsing support and fix embit miniscript_parse
2 parents 840d500 + abef12f commit e290632

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

.github/workflows/workflow.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ jobs:
128128
- name: Test - descriptor_parse
129129
timeout-minutes: 5
130130
run: |
131-
export CXXFLAGS="-DBITCOIN_CORE -DRUST_MINISCRIPT"
131+
export CXXFLAGS="-DBITCOIN_CORE -DRUST_MINISCRIPT -DEMBIT"
132132
make
133133
FUZZ=descriptor_parse ./bitcoinfuzz -runs=100
134134

modules/embit/embit_lib/embit_lib.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
#include <iostream>
44
#include <cstring>
55

6-
extern "C" bool embit_miniscript_miniscript_parse(const char* input) {
6+
static bool call_python_parser(const char* input, const char* function_name) {
77
if (!Py_IsInitialized()) {
88
Py_Initialize();
9-
109
PyRun_SimpleString("import sys; sys.path.append('.')");
1110
}
1211

@@ -15,20 +14,20 @@ extern "C" bool embit_miniscript_miniscript_parse(const char* input) {
1514

1615
bool success = false;
1716
PyObject* main_module = NULL;
18-
PyObject* miniscript_parse_func = NULL;
17+
PyObject* parse_func = NULL;
1918
PyObject* args = NULL;
2019
PyObject* result = NULL;
2120
PyObject* input_str = NULL;
2221

23-
// Import the main Python module containing miniscript_parse
22+
// Import the main Python module containing the parser function
2423
main_module = PyImport_ImportModule("main");
2524
if (!main_module) {
2625
goto cleanup;
2726
}
2827

29-
// Get the miniscript_parse function from the module
30-
miniscript_parse_func = PyObject_GetAttrString(main_module, "miniscript_parse");
31-
if (!miniscript_parse_func || !PyCallable_Check(miniscript_parse_func)) {
28+
// Get the parser function from the module
29+
parse_func = PyObject_GetAttrString(main_module, function_name);
30+
if (!parse_func || !PyCallable_Check(parse_func)) {
3231
goto cleanup;
3332
}
3433

@@ -45,14 +44,13 @@ extern "C" bool embit_miniscript_miniscript_parse(const char* input) {
4544
}
4645

4746
// Add the input string to the tuple
48-
// PyTuple_SetItem steals a reference, so we don't need to decref input_str after
4947
if (PyTuple_SetItem(args, 0, input_str) < 0) {
5048
goto cleanup;
5149
}
5250
input_str = NULL; // Ownership transferred to args
5351

5452
// Call the Python function
55-
result = PyObject_CallObject(miniscript_parse_func, args);
53+
result = PyObject_CallObject(parse_func, args);
5654
if (!result) {
5755
goto cleanup;
5856
}
@@ -65,11 +63,19 @@ extern "C" bool embit_miniscript_miniscript_parse(const char* input) {
6563
cleanup:
6664
Py_XDECREF(result);
6765
Py_XDECREF(args);
68-
Py_XDECREF(miniscript_parse_func);
66+
Py_XDECREF(parse_func);
6967
Py_XDECREF(main_module);
7068
Py_XDECREF(input_str);
7169

7270
PyGILState_Release(gstate);
7371

7472
return success;
73+
}
74+
75+
extern "C" bool embit_miniscript_miniscript_parse(const char* input) {
76+
return call_python_parser(input, "miniscript_parse");
77+
}
78+
79+
extern "C" bool embit_descriptor_parse(const char* input) {
80+
return call_python_parser(input, "descriptor_parse");
7581
}

modules/embit/embit_lib/embit_lib.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#include <cstdint>
22

3-
extern "C" bool embit_miniscript_miniscript_parse(const char* input);
3+
extern "C" bool embit_miniscript_miniscript_parse(const char* input);
4+
5+
extern "C" bool embit_miniscript_descriptor_parse(const char* input);

modules/embit/embit_lib/embit_lib.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
21
from io import BytesIO
32
from embit.descriptor.miniscript import Miniscript
3+
from embit.descriptor import Descriptor
44

55
def miniscript_parse(input):
66
try:
7-
ms = Miniscript.read_from(BytesIO(input.encode()), taproot=False)
7+
ms = Miniscript.from_string(input, taproot=False)
88
ms.verify()
99
return True
1010
except Exception as _:
1111
try:
12-
ms = Miniscript.read_from(BytesIO(input.encode()), taproot=True)
12+
ms = Miniscript.from_string(input, taproot=True)
1313
ms.verify()
1414
return True
1515
except Exception as _:
16-
return False
16+
return False
17+
18+
def descriptor_parse(input):
19+
try:
20+
desc = Descriptor.from_string(input)
21+
return True
22+
except Exception as _:
23+
return False

0 commit comments

Comments
 (0)