electrum

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

commit 444610452e4c3344f262680e779b208647d134d5
parent df15042cee9fc7ca68ad6d0034567aa8024907c3
Author: ThomasV <thomasv@electrum.org>
Date:   Tue, 17 Mar 2020 11:04:49 +0100

wallet_db: encapsulate type conversions with attr.s converter

Diffstat:
Melectrum/lnutil.py | 29+++++++++++++++++------------
Melectrum/wallet_db.py | 12------------
2 files changed, 17 insertions(+), 24 deletions(-)

diff --git a/electrum/lnutil.py b/electrum/lnutil.py @@ -41,22 +41,27 @@ def ln_dummy_address(): from .json_db import StoredObject + +hex_to_bytes = lambda v: v if isinstance(v, bytes) else bytes.fromhex(v) if v is not None else None +json_to_keypair = lambda v: v if isinstance(v, OnlyPubkeyKeypair) else Keypair(**v) if len(v)==2 else OnlyPubkeyKeypair(**v) + + @attr.s class OnlyPubkeyKeypair(StoredObject): - pubkey = attr.ib(type=bytes) + pubkey = attr.ib(type=bytes, converter=hex_to_bytes) @attr.s class Keypair(OnlyPubkeyKeypair): - privkey = attr.ib(type=bytes) + privkey = attr.ib(type=bytes, converter=hex_to_bytes) @attr.s class Config(StoredObject): # shared channel config fields - payment_basepoint = attr.ib(type=OnlyPubkeyKeypair) - multisig_key = attr.ib(type=OnlyPubkeyKeypair) - htlc_basepoint = attr.ib(type=OnlyPubkeyKeypair) - delayed_basepoint = attr.ib(type=OnlyPubkeyKeypair) - revocation_basepoint = attr.ib(type=OnlyPubkeyKeypair) + payment_basepoint = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair) + multisig_key = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair) + htlc_basepoint = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair) + delayed_basepoint = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair) + revocation_basepoint = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair) to_self_delay = attr.ib(type=int) dust_limit_sat = attr.ib(type=int) max_htlc_value_in_flight_msat = attr.ib(type=int) @@ -66,17 +71,17 @@ class Config(StoredObject): @attr.s class LocalConfig(Config): - per_commitment_secret_seed = attr.ib(type=bytes) + per_commitment_secret_seed = attr.ib(type=bytes, converter=hex_to_bytes) funding_locked_received = attr.ib(type=bool) was_announced = attr.ib(type=bool) - current_commitment_signature = attr.ib(type=bytes) - current_htlc_signatures = attr.ib(type=bytes) + current_commitment_signature = attr.ib(type=bytes, converter=hex_to_bytes) + current_htlc_signatures = attr.ib(type=bytes, converter=hex_to_bytes) @attr.s class RemoteConfig(Config): htlc_minimum_msat = attr.ib(type=int) - next_per_commitment_point = attr.ib(type=bytes) - current_per_commitment_point = attr.ib(default=None, type=bytes) + next_per_commitment_point = attr.ib(type=bytes, converter=hex_to_bytes) + current_per_commitment_point = attr.ib(default=None, type=bytes, converter=hex_to_bytes) @attr.s class FeeUpdate(StoredObject): diff --git a/electrum/wallet_db.py b/electrum/wallet_db.py @@ -1101,18 +1101,6 @@ class WalletDB(JsonDB): v = ChannelConstraints(**v) elif key == 'funding_outpoint': v = Outpoint(**v) - elif key.endswith("_basepoint") or key.endswith("_key"): - v = Keypair(**v) if len(v)==2 else OnlyPubkeyKeypair(**v) - elif key in [ - "short_channel_id", - "current_per_commitment_point", - "next_per_commitment_point", - "per_commitment_secret_seed", - "current_commitment_signature", - "current_htlc_signatures"]: - v = binascii.unhexlify(v) if v is not None else None - elif len(path) > 2 and path[-2] in ['local_config', 'remote_config'] and key in ["pubkey", "privkey"]: - v = binascii.unhexlify(v) if v is not None else None return v def write(self, storage: 'WalletStorage'):