electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit f957837e218770531502d995556bc34925a6b733
parent 72d7d0b07ae49785591c571137f5090b38729b36
Author: ThomasV <thomasv@gitorious>
Date:   Sat, 14 Sep 2013 21:53:56 +0200

custom json encoder for transactions

Diffstat:
Mlib/commands.py | 6+++---
Mlib/transaction.py | 4----
Mlib/util.py | 13++++++++++---
3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/lib/commands.py b/lib/commands.py @@ -115,12 +115,12 @@ class Commands: i['index'] = i['vout'] outputs = map(lambda x: (x[0],int(1e8*x[1])), outputs.items()) tx = Transaction.from_io(inputs, outputs) - return tx.as_dict() + return tx def signrawtransaction(self, raw_tx, input_info, private_keys): tx = Transaction(raw_tx) self.wallet.signrawtransaction(tx, input_info, private_keys, self.password) - return tx.as_dict() + return tx def decoderawtransaction(self, raw): tx = Transaction(raw) @@ -251,7 +251,7 @@ class Commands: def mksendmanytx(self, outputs, fee = None, change_addr = None, domain = None): tx = self._mktx(outputs, fee, change_addr, domain) - return tx.as_dict() + return tx def payto(self, to_address, amount, fee = None, change_addr = None, domain = None): diff --git a/lib/transaction.py b/lib/transaction.py @@ -379,10 +379,6 @@ class Transaction: self.input_info = None self.is_complete = True - - def __repr__(self): - return "Transaction('"+self.raw+"')" - def __str__(self): return self.raw diff --git a/lib/util.py b/lib/util.py @@ -1,4 +1,4 @@ -import os, sys, re +import os, sys, re, json import platform import shutil from datetime import datetime @@ -6,6 +6,14 @@ is_verbose = True +class MyEncoder(json.JSONEncoder): + def default(self, obj): + from transaction import Transaction + if isinstance(obj, Transaction): + return obj.as_dict() + return super(MyEncoder, self).default(obj) + + def set_verbosity(b): global is_verbose is_verbose = b @@ -23,9 +31,8 @@ def print_msg(*args): sys.stdout.flush() def print_json(obj): - import json try: - s = json.dumps(obj,sort_keys = True, indent = 4) + s = json.dumps(obj, sort_keys = True, indent = 4, cls=MyEncoder) except TypeError: s = repr(obj) sys.stdout.write(s + "\n")