Repository: spesmilo/electrum
Issue: error: short format requires (-0x7fff - 1) <= number <= 0x7fff


Crash Report
============

This crash report was reported through the automatic crash reporting system 🤖

Traceback
--------------

```Python traceback
Traceback (most recent call last):
  File "/home/gastech367/Electrum-4.3.2/electrum/gui/qt/channels_list.py", line 263, in <lambda>
    menu.addAction(_("Export backup"), lambda: self.export_channel_backup(channel_id))
  File "/home/gastech367/Electrum-4.3.2/electrum/gui/qt/channels_list.py", line 171, in export_channel_backup
    data = self.lnworker.export_channel_backup(channel_id)
  File "/home/gastech367/Electrum-4.3.2/electrum/lnworker.py", line 2424, in export_channel_backup
    backup_bytes = self.create_channel_backup(channel_id).to_bytes()
  File "/home/gastech367/Electrum-4.3.2/electrum/lnutil.py", line 276, in to_bytes
    vds.write_int16(self.port)
  File "/home/gastech367/Electrum-4.3.2/electrum/transaction.py", line 346, in write_int16
    def write_int16(self, val): return self._write_num('<h', val)
  File "/home/gastech367/Electrum-4.3.2/electrum/transaction.py", line 393, in _write_num
    s = struct.pack(format, num)
struct.error: short format requires (-0x7fff - 1) <= number <= 0x7fff

error: short format requires (-0x7fff - 1) <= number <= 0x7fff
```

Reporter
------------

This issue was reported by 2 user(s):

| Electrum Version  | Python Version | Operating System  | Wallet Type  | Locale | Stack |
|---|---|---|---|---|---|
| 4.3.2  | 3.10.9 (main, Dec 19 2022, 17:35:49) [GCC 12.2.0] | Linux-5.15.102-1-MANJARO-x86_64-with-glibc2.37 | standard | es_ES | [ℹ](https://crashhub.electrum.org//crash/27566) | 
| 4.3.2  | 3.9.11 (tags/v3.9.11:2de452f, Mar 16 2022, 14:21:37) [MSC v.1929 32 bit (Intel)] | Windows-10-10.0.19045-SP0 | standard | es_VE | [ℹ](https://crashhub.electrum.org//crash/28799) | 


Additional Information
------------------------

The reporting user(s) did not provide additional information.

Changed files:
electrum/lnutil.py

diff --git a/electrum/lnutil.py b/electrum/lnutil.py
index 8cea30275..4ffb77101 100644
--- a/electrum/lnutil.py
+++ b/electrum/lnutil.py
@@ -264,27 +264,27 @@ class ImportedChannelBackupStorage(ChannelBackupStorage):
 
     def to_bytes(self) -> bytes:
         vds = BCDataStream()
-        vds.write_int16(CHANNEL_BACKUP_VERSION)
+        vds.write_uint16(CHANNEL_BACKUP_VERSION)
         vds.write_boolean(self.is_initiator)
         vds.write_bytes(self.privkey, 32)
         vds.write_bytes(self.channel_seed, 32)
         vds.write_bytes(self.node_id, 33)
         vds.write_bytes(bfh(self.funding_txid), 32)
-        vds.write_int16(self.funding_index)
+        vds.write_uint16(self.funding_index)
         vds.write_string(self.funding_address)
         vds.write_bytes(self.remote_payment_pubkey, 33)
         vds.write_bytes(self.remote_revocation_pubkey, 33)
-        vds.write_int16(self.local_delay)
-        vds.write_int16(self.remote_delay)
+        vds.write_uint16(self.local_delay)
+        vds.write_uint16(self.remote_delay)
         vds.write_string(self.host)
-        vds.write_int16(self.port)
+        vds.write_uint16(self.port)
         return bytes(vds.input)
 
     @staticmethod
     def from_bytes(s: bytes) -> "ImportedChannelBackupStorage":
         vds = BCDataStream()
         vds.write(s)
-        version = vds.read_int16()
+        version = vds.read_uint16()
         if version != CHANNEL_BACKUP_VERSION:
             raise Exception(f"unknown version for channel backup: {version}")
         return ImportedChannelBackupStorage(
@@ -293,14 +293,14 @@ class ImportedChannelBackupStorage(ChannelBackupStorage):
             channel_seed=vds.read_bytes(32),
             node_id=vds.read_bytes(33),
             funding_txid=vds.read_bytes(32).hex(),
-            funding_index=vds.read_int16(),
+            funding_index=vds.read_uint16(),
             funding_address=vds.read_string(),
             remote_payment_pubkey=vds.read_bytes(33),
             remote_revocation_pubkey=vds.read_bytes(33),
-            local_delay=vds.read_int16(),
-            remote_delay=vds.read_int16(),
+            local_delay=vds.read_uint16(),
+            remote_delay=vds.read_uint16(),
             host=vds.read_string(),
-            port=vds.read_int16(),
+            port=vds.read_uint16(),
         )
 
     @staticmethod