electrum

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

commit 55e0e1a72e7b57ef8a91722a060b6d09056e0879
parent c2432aa1251a4f2775d90ff67c1896997b564e1a
Author: ThomasV <thomasv@electrum.org>
Date:   Fri, 21 Feb 2020 12:18:28 +0100

Generate channel keys from random seed

Diffstat:
Melectrum/lnpeer.py | 7+++++--
Melectrum/lnutil.py | 10+++++++---
Melectrum/lnworker.py | 3+--
3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py @@ -20,6 +20,7 @@ import aiorpcx from .crypto import sha256, sha256d from . import bitcoin +from .bip32 import BIP32Node from . import ecc from .ecc import sig_string_from_r_and_s, get_r_and_s_from_sig_string, der_sig_from_sig_string from . import constants @@ -455,8 +456,10 @@ class Peer(Logger): def make_local_config(self, funding_sat: int, push_msat: int, initiator: HTLCOwner) -> LocalConfig: # key derivation - channel_counter = self.lnworker.get_and_inc_counter_for_channel_keys() - keypair_generator = lambda family: generate_keypair(self.lnworker.ln_keystore, family, channel_counter) + seed = os.urandom(32) + node = BIP32Node.from_rootseed(seed, xtype='standard') + keypair_generator = lambda family: generate_keypair(node, family) + if initiator == LOCAL: initial_msat = funding_sat * 1000 - push_msat else: diff --git a/electrum/lnutil.py b/electrum/lnutil.py @@ -22,7 +22,7 @@ from .bitcoin import push_script, redeem_script_to_address, address_to_script from . import segwit_addr from .i18n import _ from .lnaddr import lndecode -from .keystore import BIP32_KeyStore +from .bip32 import BIP32Node if TYPE_CHECKING: from .lnchannel import Channel @@ -791,8 +791,12 @@ class LnKeyFamily(IntEnum): NODE_KEY = 6 -def generate_keypair(ln_keystore: BIP32_KeyStore, key_family: LnKeyFamily, index: int) -> Keypair: - return Keypair(*ln_keystore.get_keypair([key_family, 0, index], None)) +def generate_keypair(node: BIP32Node, key_family: LnKeyFamily) -> Keypair: + node2 = node.subkey_at_private_derivation([key_family]) + k = node2.eckey.get_secret_bytes() + cK = ecc.ECPrivkey(k).get_public_key_bytes() + return Keypair(cK, k) + NUM_MAX_HOPS_IN_PAYMENT_PATH = 20 diff --git a/electrum/lnworker.py b/electrum/lnworker.py @@ -124,7 +124,7 @@ class LNWorker(Logger): def __init__(self, xprv): Logger.__init__(self) - self.node_keypair = generate_keypair(keystore.from_xprv(xprv), LnKeyFamily.NODE_KEY, 0) + self.node_keypair = generate_keypair(BIP32Node.from_xkey(xprv), LnKeyFamily.NODE_KEY) self.peers = {} # type: Dict[bytes, Peer] # pubkey -> Peer # set some feature flags as baseline for both LNWallet and LNGossip # note that e.g. DATA_LOSS_PROTECT is needed for LNGossip as many peers require it @@ -358,7 +358,6 @@ class LNWallet(LNWorker): self.db = wallet.db self.config = wallet.config LNWorker.__init__(self, xprv) - self.ln_keystore = keystore.from_xprv(xprv) self.localfeatures |= LnLocalFeatures.OPTION_DATA_LOSS_PROTECT_REQ self.payments = self.db.get_dict('lightning_payments') # RHASH -> amount, direction, is_paid self.preimages = self.db.get_dict('lightning_preimages') # RHASH -> preimage