1
0

tx: replace whitespace chars in raw tx string

Removes all whitespace characters from a raw transaction string.
This is useful for example when loading raw transactions from text input
as it happens that there are some newline characters in the text.
I noticed this when copying-pasting from a timelock recovery pdf.
This commit is contained in:
f321x
2025-12-10 15:19:18 +01:00
parent 0eefcbae9c
commit 7d307048a0
2 changed files with 9 additions and 3 deletions

View File

@@ -36,6 +36,7 @@ from enum import IntEnum
import itertools
import binascii
import copy
import re
import electrum_ecc as ecc
from electrum_ecc.util import bip340_tagged_hash
@@ -1482,7 +1483,11 @@ def convert_raw_tx_to_hex(raw: Union[str, bytes]) -> str:
if not raw:
raise ValueError("empty string")
raw_unstripped = raw
raw = raw.strip()
# remove all whitespace characters
if isinstance(raw, str):
raw = re.sub(r'\s', '', raw)
else:
raw = re.sub(rb'\s', b'', raw)
# try hex
try:
return binascii.unhexlify(raw).hex()

View File

@@ -276,10 +276,11 @@ class TestTransaction(ElectrumTestCase):
data = raw_tx.data
tx_from_any(data) # test if raises (should not)
else:
mid = len(raw_tx.data) // 2
if isinstance(raw_tx.data, str):
data = whitespace_str + raw_tx.data + whitespace_str
data = whitespace_str + raw_tx.data[:mid] + whitespace_str + raw_tx.data[mid:] + whitespace_str
else:
data = whitespace_bytes + raw_tx.data + whitespace_bytes
data = whitespace_bytes + raw_tx.data[:mid] + whitespace_bytes + raw_tx.data[mid:] + whitespace_bytes
if raw_tx.is_whitespace_allowed:
tx_from_any(data) # test if raises (should not)
else: