Skip to content

Commit 7dbbe60

Browse files
committed
Simplify bitcoin RPC parameter handling with pure passthrough approach
1 parent 881dfab commit 7dbbe60

File tree

1 file changed

+5
-44
lines changed

1 file changed

+5
-44
lines changed

src/warnet/bitcoin.py

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import json
21
import os
32
import re
3+
import shlex
44
import sys
55
from datetime import datetime
66
from io import BytesIO
@@ -39,54 +39,15 @@ def rpc(tank: str, method: str, params: list[str], namespace: Optional[str]):
3939

4040

4141
def _rpc(tank: str, method: str, params: list[str], namespace: Optional[str] = None):
42-
# bitcoin-cli should be able to read bitcoin.conf inside the container
43-
# so no extra args like port, chain, username or password are needed
4442
namespace = get_default_namespace_or(namespace)
4543

4644
if params:
47-
# Process parameters to ensure proper shell escaping
48-
processed_params = []
49-
for param in params:
50-
# If the parameter looks like JSON (starts with [ or {), fix malformed patterns
51-
if param.startswith("[") or param.startswith("{"):
52-
# Fix common malformed JSON patterns
53-
if '\\"' in param:
54-
# Convert [\"value\"] to ["value"]
55-
param = param.replace('\\"', '"')
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)
84-
else:
85-
processed_params.append(param)
86-
87-
cmd = f"kubectl -n {namespace} exec {tank} --container {BITCOINCORE_CONTAINER} -- bitcoin-cli {method} {' '.join(processed_params)}"
45+
# Shell-escape each param to preserve quotes and special characters
46+
bitcoin_cli_args = " ".join(shlex.quote(p) for p in params)
47+
cmd = f"kubectl -n {namespace} exec {tank} --container {BITCOINCORE_CONTAINER} -- bitcoin-cli {method} {bitcoin_cli_args}"
8848
else:
8949
cmd = f"kubectl -n {namespace} exec {tank} --container {BITCOINCORE_CONTAINER} -- bitcoin-cli {method}"
50+
9051
return run_command(cmd)
9152

9253

0 commit comments

Comments
 (0)