diff --git a/electrum/lnchan.py b/electrum/lnchan.py index 5fc5e4aba..18bf0b61a 100644 --- a/electrum/lnchan.py +++ b/electrum/lnchan.py @@ -22,6 +22,13 @@ from .lnutil import ScriptHtlc, SENT, RECEIVED, PaymentFailure, calc_onchain_fee from .transaction import Transaction, TxOutput, construct_witness from .simple_config import SimpleConfig, FEERATE_FALLBACK_STATIC_FEE +class ChannelJsonEncoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, bytes): + return binascii.hexlify(o).decode("ascii") + if isinstance(o, RevocationStore): + return o.serialize() + return super(ChannelJsonEncoder, self) RevokeAndAck = namedtuple("RevokeAndAck", ["per_commitment_secret", "next_per_commitment_point"]) @@ -685,14 +692,7 @@ class Channel(PrintError): def serialize(self): namedtuples_to_dict = lambda v: {i: j._asdict() if isinstance(j, tuple) else j for i, j in v._asdict().items()} serialized_channel = {k: namedtuples_to_dict(v) if isinstance(v, tuple) else v for k, v in self.to_save().items()} - class MyJsonEncoder(json.JSONEncoder): - def default(self, o): - if isinstance(o, bytes): - return binascii.hexlify(o).decode("ascii") - if isinstance(o, RevocationStore): - return o.serialize() - return super(MyJsonEncoder, self) - dumped = MyJsonEncoder().encode(serialized_channel) + dumped = ChannelJsonEncoder().encode(serialized_channel) roundtripped = json.loads(dumped) reconstructed = Channel(roundtripped) if reconstructed.to_save() != self.to_save(): diff --git a/electrum/lnworker.py b/electrum/lnworker.py index 9ff10ba72..d35599c35 100644 --- a/electrum/lnworker.py +++ b/electrum/lnworker.py @@ -6,6 +6,7 @@ import time from typing import Optional, Sequence, Tuple, List, Dict, TYPE_CHECKING import threading import socket +import json import dns.resolver import dns.exception @@ -20,7 +21,7 @@ from .lntransport import LNResponderTransport from .lnbase import Peer from .lnaddr import lnencode, LnAddr, lndecode from .ecc import der_sig_from_sig_string -from .lnchan import Channel +from .lnchan import Channel, ChannelJsonEncoder from .lnutil import (Outpoint, calc_short_channel_id, LNPeerAddr, get_compressed_pubkey_from_bech32, extract_nodeid, PaymentFailure, split_host_port, ConnStringFormatError, @@ -405,11 +406,13 @@ class LNWorker(PrintError): self.wallet.storage.write() def list_channels(self): + encoder = ChannelJsonEncoder() with self.lock: # we output the funding_outpoint instead of the channel_id because lnd uses channel_point (funding outpoint) to identify channels for channel_id, chan in self.channels.items(): yield { - 'htlcs': chan.log[LOCAL], + 'local_htlcs': json.loads(encoder.encode(chan.log[LOCAL ])), + 'remote_htlcs': json.loads(encoder.encode(chan.log[REMOTE])), 'channel_id': bh2u(chan.short_channel_id), 'channel_point': chan.funding_outpoint.to_str(), 'state': chan.get_state(),