Browse Source

fix transaction parsing from command line

master
ThomasV 10 years ago
parent
commit
9659e8542d
  1. 16
      gui/qt/main_window.py
  2. 15
      lib/commands.py
  3. 18
      lib/transaction.py

16
gui/qt/main_window.py

@ -2244,21 +2244,9 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
def tx_from_text(self, txt): def tx_from_text(self, txt):
"json or raw hexadecimal" from electrum.transaction import tx_from_str
txt = txt.strip()
try: try:
txt.decode('hex') return tx_from_str(txt)
is_hex = True
except:
is_hex = False
try:
if is_hex:
return Transaction(txt)
tx_dict = json.loads(str(txt))
assert "hex" in tx_dict.keys()
tx = Transaction(tx_dict["hex"])
return tx
except: except:
traceback.print_exc(file=sys.stdout) traceback.print_exc(file=sys.stdout)
self.show_critical(_("Electrum was unable to parse your transaction")) self.show_critical(_("Electrum was unable to parse your transaction"))

15
lib/commands.py

@ -216,24 +216,22 @@ class Commands:
@command('wp') @command('wp')
def signtransaction(self, tx, privkey=None): def signtransaction(self, tx, privkey=None):
"""Sign a transaction. The wallet keys will be used unless a private key is provided.""" """Sign a transaction. The wallet keys will be used unless a private key is provided."""
t = Transaction(tx)
if privkey: if privkey:
pubkey = bitcoin.public_key_from_private_key(privkey) pubkey = bitcoin.public_key_from_private_key(privkey)
t.sign({pubkey:privkey}) tx.sign({pubkey:privkey})
else: else:
self.wallet.sign_transaction(t, self._password) self.wallet.sign_transaction(tx, self._password)
return t.as_dict() return tx.as_dict()
@command('') @command('')
def deserialize(self, tx): def deserialize(self, tx):
"""Deserialize a serialized transaction""" """Deserialize a serialized transaction"""
return Transaction(tx).deserialize() return tx.deserialize()
@command('n') @command('n')
def broadcast(self, tx, timeout=10): def broadcast(self, tx, timeout=10):
"""Broadcast a transaction to the network. """ """Broadcast a transaction to the network. """
t = Transaction(tx) return self.network.broadcast(tx, timeout)
return self.network.broadcast(t, timeout)
@command('') @command('')
def createmultisig(self, num, pubkeys): def createmultisig(self, num, pubkeys):
@ -667,12 +665,13 @@ command_options = {
# don't use floats because of rounding errors # don't use floats because of rounding errors
from transaction import tx_from_str
json_loads = lambda x: json.loads(x, parse_float=lambda x: str(Decimal(x))) json_loads = lambda x: json.loads(x, parse_float=lambda x: str(Decimal(x)))
arg_types = { arg_types = {
'num': int, 'num': int,
'nbits': int, 'nbits': int,
'entropy': long, 'entropy': long,
'tx': json_loads, 'tx': tx_from_str,
'pubkeys': json_loads, 'pubkeys': json_loads,
'inputs': json_loads, 'inputs': json_loads,
'outputs': json_loads, 'outputs': json_loads,

18
lib/transaction.py

@ -859,3 +859,21 @@ class Transaction:
print_error(priority, threshold) print_error(priority, threshold)
return priority < threshold return priority < threshold
def tx_from_str(txt):
"json or raw hexadecimal"
import json
txt = txt.strip()
try:
txt.decode('hex')
is_hex = True
except:
is_hex = False
if is_hex:
return Transaction(txt)
tx_dict = json.loads(str(txt))
assert "hex" in tx_dict.keys()
tx = Transaction(tx_dict["hex"])
return tx

Loading…
Cancel
Save