Skip to content

Commit 881dfab

Browse files
committed
fix: handle JSON array parameters intelligently for different RPC methods
- Fix getblock '[blockhash]' issue by extracting first element from JSON arrays - Maintain backward compatibility for plain string parameters - Support JSON arrays as-is for methods like logging and importdescriptors - Extract only first element for single-parameter methods (getblock, getblockhash, gettransaction) - Extract all elements for multi-parameter methods - Fix malformed JSON patterns with escaped quotes
1 parent 8449337 commit 881dfab

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/warnet/bitcoin.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,34 @@ def _rpc(tank: str, method: str, params: list[str], namespace: Optional[str] = N
5353
if '\\"' in param:
5454
# Convert [\"value\"] to ["value"]
5555
param = param.replace('\\"', '"')
56-
# Wrap JSON in single quotes to preserve it as a single argument
57-
processed_params.append(f"'{param}'")
56+
57+
# Try to parse as JSON to determine how to handle it
58+
try:
59+
parsed_json = json.loads(param)
60+
if isinstance(parsed_json, list):
61+
# For JSON arrays, extract elements for methods that expect individual parameters
62+
# (like getblock, gettransaction, etc.)
63+
# But keep as JSON for methods that expect JSON arrays (like logging)
64+
if method in ["logging", "importdescriptors", "importmulti"]:
65+
# These methods expect JSON arrays as-is
66+
processed_params.append(f"'{param}'")
67+
else:
68+
# For single-parameter methods, only extract the first element
69+
# For multi-parameter methods, extract all elements
70+
if method in ["getblockhash", "getblock", "gettransaction"]:
71+
# These methods expect a single parameter, so only take the first element
72+
if parsed_json:
73+
processed_params.append(str(parsed_json[0]))
74+
else:
75+
# Extract all array elements for other methods
76+
for element in parsed_json:
77+
processed_params.append(str(element))
78+
else:
79+
# For JSON objects, pass as-is
80+
processed_params.append(f"'{param}'")
81+
except json.JSONDecodeError:
82+
# If it's not valid JSON, pass as-is
83+
processed_params.append(param)
5884
else:
5985
processed_params.append(param)
6086

0 commit comments

Comments
 (0)