Browse Source

lnutil: test ImportedChannelBackupStorage.from_bytes

regression test - we should not inadvertently break deserialising existing backups
master
SomberNight 3 years ago
parent
commit
a30cda4ebd
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 11
      electrum/lnutil.py
  2. 5
      electrum/lnworker.py
  3. 33
      electrum/tests/test_lnutil.py

11
electrum/lnutil.py

@ -16,7 +16,7 @@ from .util import list_enabled_bits
from .util import ShortID as ShortChannelID from .util import ShortID as ShortChannelID
from .util import format_short_id as format_short_channel_id from .util import format_short_id as format_short_channel_id
from .crypto import sha256 from .crypto import sha256, pw_decode_with_version_and_mac
from .transaction import (Transaction, PartialTransaction, PartialTxInput, TxOutpoint, from .transaction import (Transaction, PartialTransaction, PartialTxInput, TxOutpoint,
PartialTxOutput, opcodes, TxOutput) PartialTxOutput, opcodes, TxOutput)
from .ecc import CURVE_ORDER, sig_string_from_der_sig, ECPubkey, string_to_number from .ecc import CURVE_ORDER, sig_string_from_der_sig, ECPubkey, string_to_number
@ -281,7 +281,7 @@ class ImportedChannelBackupStorage(ChannelBackupStorage):
return bytes(vds.input) return bytes(vds.input)
@staticmethod @staticmethod
def from_bytes(s): def from_bytes(s: bytes) -> "ImportedChannelBackupStorage":
vds = BCDataStream() vds = BCDataStream()
vds.write(s) vds.write(s)
version = vds.read_int16() version = vds.read_int16()
@ -302,6 +302,13 @@ class ImportedChannelBackupStorage(ChannelBackupStorage):
host = vds.read_string(), host = vds.read_string(),
port = vds.read_int16()) port = vds.read_int16())
@staticmethod
def from_encrypted_str(data: str, *, password: str) -> "ImportedChannelBackupStorage":
if not data.startswith('channel_backup:'):
raise ValueError("missing or invalid magic bytes")
encrypted = data[15:]
decrypted = pw_decode_with_version_and_mac(encrypted, password)
return ImportedChannelBackupStorage.from_bytes(decrypted)
class ScriptHtlc(NamedTuple): class ScriptHtlc(NamedTuple):

5
electrum/lnworker.py

@ -2452,11 +2452,8 @@ class LNWallet(LNWorker):
raise Exception(f'Unknown channel {channel_id.hex()}') raise Exception(f'Unknown channel {channel_id.hex()}')
def import_channel_backup(self, data): def import_channel_backup(self, data):
assert data.startswith('channel_backup:')
encrypted = data[15:]
xpub = self.wallet.get_fingerprint() xpub = self.wallet.get_fingerprint()
decrypted = pw_decode_with_version_and_mac(encrypted, xpub) cb_storage = ImportedChannelBackupStorage.from_encrypted_str(data, password=xpub)
cb_storage = ImportedChannelBackupStorage.from_bytes(decrypted)
channel_id = cb_storage.channel_id() channel_id = cb_storage.channel_id()
if channel_id.hex() in self.db.get_dict("channels"): if channel_id.hex() in self.db.get_dict("channels"):
raise Exception('Channel already in wallet') raise Exception('Channel already in wallet')

33
electrum/tests/test_lnutil.py

@ -9,12 +9,15 @@ from electrum.lnutil import (RevocationStore, get_per_commitment_secret_from_see
derive_pubkey, make_htlc_tx, extract_ctn_from_tx, UnableToDeriveSecret, derive_pubkey, make_htlc_tx, extract_ctn_from_tx, UnableToDeriveSecret,
get_compressed_pubkey_from_bech32, split_host_port, ConnStringFormatError, get_compressed_pubkey_from_bech32, split_host_port, ConnStringFormatError,
ScriptHtlc, extract_nodeid, calc_fees_for_commitment_tx, UpdateAddHtlc, LnFeatures, ScriptHtlc, extract_nodeid, calc_fees_for_commitment_tx, UpdateAddHtlc, LnFeatures,
ln_compare_features, IncompatibleLightningFeatures, ChannelType) ln_compare_features, IncompatibleLightningFeatures, ChannelType,
ImportedChannelBackupStorage)
from electrum.util import bfh, MyEncoder from electrum.util import bfh, MyEncoder
from electrum.transaction import Transaction, PartialTransaction, Sighash from electrum.transaction import Transaction, PartialTransaction, Sighash
from electrum.lnworker import LNWallet from electrum.lnworker import LNWallet
from electrum.wallet import restore_wallet_from_text, Standard_Wallet
from electrum.simple_config import SimpleConfig
from . import ElectrumTestCase from . import ElectrumTestCase, as_testnet
funding_tx_id = '8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be' funding_tx_id = '8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be'
@ -903,3 +906,29 @@ class TestLNUtil(ElectrumTestCase):
# ignore unknown channel types # ignore unknown channel types
channel_type = ChannelType(0b10000000001000000000010).discard_unknown_and_check() channel_type = ChannelType(0b10000000001000000000010).discard_unknown_and_check()
self.assertEqual(ChannelType(0b10000000001000000000000), channel_type) self.assertEqual(ChannelType(0b10000000001000000000000), channel_type)
@as_testnet
async def test_decode_imported_channel_backup(self):
encrypted_cb = "channel_backup:Adn87xcGIs9H2kfp4VpsOaNKWCHX08wBoqq37l1cLYKGlJamTeoaLEwpJA81l1BXF3GP/mRxqkY+whZG9l51G8izIY/kmMSvnh0DOiZEdwaaT/1/MwEHfsEomruFqs+iW24SFJPHbMM7f80bDtIxcLfZkKmgcKBAOlcqtq+dL3U3yH74S8BDDe2L4snaxxpCjF0JjDMBx1UR/28D+QlIi+lbvv1JMaCGXf+AF1+3jLQf8+lVI+rvFdyArws6Ocsvjf+ANQeSGUwW6Nb2xICQcMRgr1DO7bO4pgGu408eYRr2v3ayJBVtnKwSwd49gF5SDSjTDAO4CCM0uj9H5RxyzH7fqotkd9J80MBr84RiBXAeXKz+Ap8608/FVqgQ9BOcn6LhuAQdE5zXpmbQyw5jUGkPvHuseR+rzthzncy01odUceqTNg=="
config = SimpleConfig({'electrum_path': self.electrum_path})
d = restore_wallet_from_text("9dk", path=None, gap_limit=2, config=config)
wallet1 = d['wallet'] # type: Standard_Wallet
decoded_cb = ImportedChannelBackupStorage.from_encrypted_str(encrypted_cb, password=wallet1.get_fingerprint())
self.assertEqual(
ImportedChannelBackupStorage(
funding_txid='97767fdefef3152319363b772914d71e5eb70e793b835c13dce20037d3ac13fe',
funding_index=1,
funding_address='tb1qfsxllwl2edccpar9jas9wsxd4vhcewlxqwmn0w27kurkme3jvkdqn4msdp',
is_initiator=True,
node_id=bfh('02bf82e22f99dcd7ac1de4aad5152ce48f0694c46ec582567f379e0adbf81e2d0f'),
privkey=bfh('7e634853dc47f0bc2f2e0d1054b302fcb414371ddbd889f29ba8aa4e8b62c772'),
host='lightning.electrum.org',
port=9739,
channel_seed=bfh('ce9bad44ff8521d9f57fd202ad7cdedceb934f0056f42d0f3aa7a576b505332a'),
local_delay=1008,
remote_delay=720,
remote_payment_pubkey=bfh('02a1bbc818e2e88847016a93c223eb4adef7bb8becb3709c75c556b6beb3afe7bd'),
remote_revocation_pubkey=bfh('022f28b7d8d1f05768ada3df1b0966083b8058e1e7197c57393e302ec118d7f0ae'),
),
decoded_cb,
)

Loading…
Cancel
Save