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:
@@ -36,6 +36,7 @@ from enum import IntEnum
|
|||||||
import itertools
|
import itertools
|
||||||
import binascii
|
import binascii
|
||||||
import copy
|
import copy
|
||||||
|
import re
|
||||||
|
|
||||||
import electrum_ecc as ecc
|
import electrum_ecc as ecc
|
||||||
from electrum_ecc.util import bip340_tagged_hash
|
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:
|
if not raw:
|
||||||
raise ValueError("empty string")
|
raise ValueError("empty string")
|
||||||
raw_unstripped = raw
|
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 hex
|
||||||
try:
|
try:
|
||||||
return binascii.unhexlify(raw).hex()
|
return binascii.unhexlify(raw).hex()
|
||||||
|
|||||||
@@ -276,10 +276,11 @@ class TestTransaction(ElectrumTestCase):
|
|||||||
data = raw_tx.data
|
data = raw_tx.data
|
||||||
tx_from_any(data) # test if raises (should not)
|
tx_from_any(data) # test if raises (should not)
|
||||||
else:
|
else:
|
||||||
|
mid = len(raw_tx.data) // 2
|
||||||
if isinstance(raw_tx.data, str):
|
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:
|
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:
|
if raw_tx.is_whitespace_allowed:
|
||||||
tx_from_any(data) # test if raises (should not)
|
tx_from_any(data) # test if raises (should not)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user