• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

spesmilo / electrum / 4878529344569344

04 Mar 2025 10:05AM UTC coverage: 60.716% (-0.02%) from 60.731%
4878529344569344

Pull #9587

CirrusCI

f321x
disable mpp flags in invoice creation if jit channel is required, check against available liquidity if we need a jit channel
Pull Request #9587: Disable mpp flags in invoice creation if jit channel is required and consider available liquidity

5 of 15 new or added lines in 2 files covered. (33.33%)

847 existing lines in 6 files now uncovered.

20678 of 34057 relevant lines covered (60.72%)

3.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.35
/electrum/lnutil.py
1
# Copyright (C) 2018 The Electrum developers
2
# Distributed under the MIT software license, see the accompanying
3
# file LICENCE or http://www.opensource.org/licenses/mit-license.php
4

5
from enum import IntFlag, IntEnum
5✔
6
import enum
5✔
7
import json
5✔
8
from collections import namedtuple, defaultdict
5✔
9
from typing import NamedTuple, List, Tuple, Mapping, Optional, TYPE_CHECKING, Union, Dict, Set, Sequence
5✔
10
import re
5✔
11
import sys
5✔
12

13
import electrum_ecc as ecc
5✔
14
from electrum_ecc import CURVE_ORDER, ecdsa_sig64_from_der_sig, ECPubkey, string_to_number
5✔
15
import attr
5✔
16

17
from .util import bfh, inv_dict, UserFacingException
5✔
18
from .util import list_enabled_bits
5✔
19
from .util import ShortID as ShortChannelID
5✔
20
from .util import format_short_id as format_short_channel_id
5✔
21

22
from .crypto import sha256, pw_decode_with_version_and_mac
5✔
23
from .transaction import (Transaction, PartialTransaction, PartialTxInput, TxOutpoint,
5✔
24
                          PartialTxOutput, opcodes, TxOutput, OPPushDataPubkey)
25
from . import bitcoin, crypto, transaction
5✔
26
from . import descriptor
5✔
27
from .bitcoin import (redeem_script_to_address, address_to_script,
5✔
28
                      construct_witness, construct_script)
29
from . import segwit_addr
5✔
30
from .i18n import _
5✔
31
from .lnaddr import lndecode
5✔
32
from .bip32 import BIP32Node, BIP32_PRIME
5✔
33
from .transaction import BCDataStream, OPPushDataGeneric
5✔
34
from .logging import get_logger
5✔
35

36

37
if TYPE_CHECKING:
5✔
UNCOV
38
    from .lnchannel import Channel, AbstractChannel
×
39
    from .lnrouter import LNPaymentRoute
×
40
    from .lnonion import OnionRoutingFailure
×
41
    from .simple_config import SimpleConfig
×
42

43

44
_logger = get_logger(__name__)
5✔
45

46

47
# defined in BOLT-03:
48
HTLC_TIMEOUT_WEIGHT = 663
5✔
49
HTLC_TIMEOUT_WEIGHT_ANCHORS = 666
5✔
50
HTLC_SUCCESS_WEIGHT = 703
5✔
51
HTLC_SUCCESS_WEIGHT_ANCHORS = 706
5✔
52
COMMITMENT_TX_WEIGHT = 724
5✔
53
COMMITMENT_TX_WEIGHT_ANCHORS = 1124
5✔
54
HTLC_OUTPUT_WEIGHT = 172
5✔
55
FIXED_ANCHOR_SAT = 330
5✔
56

57
LN_MAX_FUNDING_SAT_LEGACY = pow(2, 24) - 1
5✔
58
DUST_LIMIT_MAX = 1000
5✔
59

60
SCRIPT_TEMPLATE_FUNDING = [opcodes.OP_2, OPPushDataPubkey, OPPushDataPubkey, opcodes.OP_2, opcodes.OP_CHECKMULTISIG]
5✔
61

62

63
from .json_db import StoredObject, stored_in, stored_as
5✔
64

65

66
def channel_id_from_funding_tx(funding_txid: str, funding_index: int) -> Tuple[bytes, bytes]:
5✔
67
    funding_txid_bytes = bytes.fromhex(funding_txid)[::-1]
5✔
68
    i = int.from_bytes(funding_txid_bytes, 'big') ^ funding_index
5✔
69
    return i.to_bytes(32, 'big'), funding_txid_bytes
5✔
70

71
hex_to_bytes = lambda v: v if isinstance(v, bytes) else bytes.fromhex(v) if v is not None else None
5✔
72
bytes_to_hex = lambda v: repr(v.hex()) if v is not None else None
5✔
73
json_to_keypair = lambda v: v if isinstance(v, OnlyPubkeyKeypair) else Keypair(**v) if len(v)==2 else OnlyPubkeyKeypair(**v)
5✔
74

75

76
def serialize_htlc_key(scid: bytes, htlc_id: int) -> str:
5✔
77
    return scid.hex() + ':%d'%htlc_id
5✔
78

79

80
def deserialize_htlc_key(htlc_key: str) -> Tuple[bytes, int]:
5✔
81
    scid, htlc_id = htlc_key.split(':')
5✔
82
    return bytes.fromhex(scid), int(htlc_id)
5✔
83

84

85
@attr.s
5✔
86
class OnlyPubkeyKeypair(StoredObject):
5✔
87
    pubkey = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
88

89
@attr.s
5✔
90
class Keypair(OnlyPubkeyKeypair):
5✔
91
    privkey = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
92

93
@attr.s
5✔
94
class ChannelConfig(StoredObject):
5✔
95
    # shared channel config fields
96
    payment_basepoint = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair)
5✔
97
    multisig_key = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair)
5✔
98
    htlc_basepoint = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair)
5✔
99
    delayed_basepoint = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair)
5✔
100
    revocation_basepoint = attr.ib(type=OnlyPubkeyKeypair, converter=json_to_keypair)
5✔
101
    to_self_delay = attr.ib(type=int)  # applies to OTHER ctx
5✔
102
    dust_limit_sat = attr.ib(type=int)  # applies to SAME ctx
5✔
103
    max_htlc_value_in_flight_msat = attr.ib(type=int)  # max val of INCOMING htlcs
5✔
104
    max_accepted_htlcs = attr.ib(type=int)  # max num of INCOMING htlcs
5✔
105
    initial_msat = attr.ib(type=int)
5✔
106
    reserve_sat = attr.ib(type=int)  # applies to OTHER ctx
5✔
107
    htlc_minimum_msat = attr.ib(type=int)  # smallest value for INCOMING htlc
5✔
108
    upfront_shutdown_script = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
109
    announcement_node_sig = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
110
    announcement_bitcoin_sig = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
111

112
    def validate_params(self, *, funding_sat: int, config: 'SimpleConfig', peer_features: 'LnFeatures') -> None:
5✔
UNCOV
113
        conf_name = type(self).__name__
×
114
        for key in (
×
115
                self.payment_basepoint,
116
                self.multisig_key,
117
                self.htlc_basepoint,
118
                self.delayed_basepoint,
119
                self.revocation_basepoint
120
        ):
UNCOV
121
            if not (len(key.pubkey) == 33 and ecc.ECPubkey.is_pubkey_bytes(key.pubkey)):
×
122
                raise Exception(f"{conf_name}. invalid pubkey in channel config")
×
123
        if funding_sat < MIN_FUNDING_SAT:
×
124
            raise Exception(f"funding_sat too low: {funding_sat} sat < {MIN_FUNDING_SAT}")
×
125
        if not peer_features.supports(LnFeatures.OPTION_SUPPORT_LARGE_CHANNEL_OPT):
×
126
            # MUST set funding_satoshis to less than 2^24 satoshi
UNCOV
127
            if funding_sat > LN_MAX_FUNDING_SAT_LEGACY:
×
128
                raise Exception(f"funding_sat too high: {funding_sat} sat > {LN_MAX_FUNDING_SAT_LEGACY} (legacy limit)")
×
129
        if funding_sat > config.LIGHTNING_MAX_FUNDING_SAT:
×
130
            raise Exception(f"funding_sat too high: {funding_sat} sat > {config.LIGHTNING_MAX_FUNDING_SAT} (config setting)")
×
131
        # MUST set push_msat to equal or less than 1000 * funding_satoshis
UNCOV
132
        if not (0 <= self.initial_msat <= 1000 * funding_sat):
×
133
            raise Exception(f"{conf_name}. insane initial_msat={self.initial_msat}. (funding_sat={funding_sat})")
×
134
        if self.reserve_sat < self.dust_limit_sat:
×
135
            raise Exception(f"{conf_name}. MUST set channel_reserve_satoshis greater than or equal to dust_limit_satoshis")
×
136
        if self.dust_limit_sat < bitcoin.DUST_LIMIT_UNKNOWN_SEGWIT:
×
137
            raise Exception(f"{conf_name}. dust limit too low: {self.dust_limit_sat} sat")
×
138
        if self.dust_limit_sat > DUST_LIMIT_MAX:
×
139
            raise Exception(f"{conf_name}. dust limit too high: {self.dust_limit_sat} sat")
×
140
        if self.reserve_sat > funding_sat // 100:
×
141
            raise Exception(f"{conf_name}. reserve too high: {self.reserve_sat}, funding_sat: {funding_sat}")
×
142
        if self.htlc_minimum_msat > 1_000:
×
143
            raise Exception(f"{conf_name}. htlc_minimum_msat too high: {self.htlc_minimum_msat} msat")
×
144
        HTLC_MINIMUM_MSAT_MIN = 0  # should be at least 1 really, but apparently some nodes are sending zero...
×
145
        if self.htlc_minimum_msat < HTLC_MINIMUM_MSAT_MIN:
×
146
            raise Exception(f"{conf_name}. htlc_minimum_msat too low: {self.htlc_minimum_msat} msat < {HTLC_MINIMUM_MSAT_MIN}")
×
147
        if self.max_accepted_htlcs < 5:
×
148
            raise Exception(f"{conf_name}. max_accepted_htlcs too low: {self.max_accepted_htlcs}")
×
149
        if self.max_accepted_htlcs > 483:
×
150
            raise Exception(f"{conf_name}. max_accepted_htlcs too high: {self.max_accepted_htlcs}")
×
151
        if self.to_self_delay > MAXIMUM_REMOTE_TO_SELF_DELAY_ACCEPTED:
×
152
            raise Exception(f"{conf_name}. to_self_delay too high: {self.to_self_delay} > {MAXIMUM_REMOTE_TO_SELF_DELAY_ACCEPTED}")
×
153
        if self.max_htlc_value_in_flight_msat < min(1000 * funding_sat, 100_000_000):
×
154
            raise Exception(f"{conf_name}. max_htlc_value_in_flight_msat is too small: {self.max_htlc_value_in_flight_msat}")
×
155

156
    @classmethod
5✔
157
    def cross_validate_params(
5✔
158
            cls,
159
            *,
160
            local_config: 'LocalConfig',
161
            remote_config: 'RemoteConfig',
162
            funding_sat: int,
163
            is_local_initiator: bool,  # whether we are the funder
164
            initial_feerate_per_kw: int,
165
            config: 'SimpleConfig',
166
            peer_features: 'LnFeatures',
167
            has_anchors: bool,
168
    ) -> None:
169
        # first we validate the configs separately
UNCOV
170
        local_config.validate_params(funding_sat=funding_sat, config=config, peer_features=peer_features)
×
171
        remote_config.validate_params(funding_sat=funding_sat, config=config, peer_features=peer_features)
×
172
        # now do tests that need access to both configs
UNCOV
173
        if is_local_initiator:
×
174
            funder, fundee = LOCAL, REMOTE
×
175
            funder_config, fundee_config = local_config, remote_config
×
176
        else:
UNCOV
177
            funder, fundee = REMOTE, LOCAL
×
178
            funder_config, fundee_config = remote_config, local_config
×
179
        # if channel_reserve_satoshis is less than dust_limit_satoshis within the open_channel message:
180
        #     MUST reject the channel.
UNCOV
181
        if remote_config.reserve_sat < local_config.dust_limit_sat:
×
182
            raise Exception("violated constraint: remote_config.reserve_sat < local_config.dust_limit_sat")
×
183
        # if channel_reserve_satoshis from the open_channel message is less than dust_limit_satoshis:
184
        #     MUST reject the channel.
UNCOV
185
        if local_config.reserve_sat < remote_config.dust_limit_sat:
×
186
            raise Exception("violated constraint: local_config.reserve_sat < remote_config.dust_limit_sat")
×
187
        # The receiving node MUST fail the channel if:
188
        #     the funder's amount for the initial commitment transaction is not
189
        #     sufficient for full fee payment.
UNCOV
190
        if funder_config.initial_msat < calc_fees_for_commitment_tx(
×
191
                num_htlcs=0,
192
                feerate=initial_feerate_per_kw,
193
                is_local_initiator=is_local_initiator,
194
                has_anchors=has_anchors,
195
        )[funder]:
UNCOV
196
            raise Exception(
×
197
                "the funder's amount for the initial commitment transaction "
198
                "is not sufficient for full fee payment")
199
        # The receiving node MUST fail the channel if:
200
        #     both to_local and to_remote amounts for the initial commitment transaction are
201
        #     less than or equal to channel_reserve_satoshis (see BOLT 3).
UNCOV
202
        if (max(local_config.initial_msat, remote_config.initial_msat)
×
203
                <= 1000 * max(local_config.reserve_sat, remote_config.reserve_sat)):
UNCOV
204
            raise Exception(
×
205
                "both to_local and to_remote amounts for the initial commitment "
206
                "transaction are less than or equal to channel_reserve_satoshis")
UNCOV
207
        from .simple_config import FEERATE_PER_KW_MIN_RELAY_LIGHTNING
×
208
        if initial_feerate_per_kw < FEERATE_PER_KW_MIN_RELAY_LIGHTNING:
×
209
            raise Exception(f"feerate lower than min relay fee. {initial_feerate_per_kw} sat/kw.")
×
210

211

212
@stored_as('local_config')
5✔
213
@attr.s
5✔
214
class LocalConfig(ChannelConfig):
5✔
215
    channel_seed = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)  # type: Optional[bytes]
5✔
216
    funding_locked_received = attr.ib(type=bool)
5✔
217
    current_commitment_signature = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
218
    current_htlc_signatures = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
219
    per_commitment_secret_seed = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
220

221
    @classmethod
5✔
222
    def from_seed(cls, **kwargs):
5✔
UNCOV
223
        channel_seed = kwargs['channel_seed']
×
224
        node = BIP32Node.from_rootseed(channel_seed, xtype='standard')
×
225
        keypair_generator = lambda family: generate_keypair(node, family)
×
226
        kwargs['per_commitment_secret_seed'] = keypair_generator(LnKeyFamily.REVOCATION_ROOT).privkey
×
227
        if kwargs['multisig_key'] is None:
×
228
            kwargs['multisig_key'] = keypair_generator(LnKeyFamily.MULTISIG)
×
229
        kwargs['htlc_basepoint'] = keypair_generator(LnKeyFamily.HTLC_BASE)
×
230
        kwargs['delayed_basepoint'] = keypair_generator(LnKeyFamily.DELAY_BASE)
×
231
        kwargs['revocation_basepoint'] = keypair_generator(LnKeyFamily.REVOCATION_BASE)
×
232
        static_remotekey = kwargs.pop('static_remotekey')
×
233
        static_payment_key = kwargs.pop('static_payment_key')
×
234
        if static_payment_key:
×
235
            # We derive the payment_basepoint from a static secret (derived from
236
            # the wallet seed) and a public nonce that is revealed
237
            # when the funding transaction is spent. This way we can restore the
238
            # payment_basepoint, needed for sweeping in the event of a force close.
UNCOV
239
            kwargs['payment_basepoint'] = derive_payment_basepoint(
×
240
                static_payment_secret=static_payment_key.privkey,
241
                funding_pubkey=kwargs['multisig_key'].pubkey
242
            )
UNCOV
243
        elif static_remotekey:  # we automatically sweep to a wallet address
×
244
            kwargs['payment_basepoint'] = OnlyPubkeyKeypair(static_remotekey)
×
245
        else:
246
            # we expect all our channels to use option_static_remotekey, so ending up here likely indicates an issue...
UNCOV
247
            kwargs['payment_basepoint'] = keypair_generator(LnKeyFamily.PAYMENT_BASE)
×
248

UNCOV
249
        return LocalConfig(**kwargs)
×
250

251
    def validate_params(self, *, funding_sat: int, config: 'SimpleConfig', peer_features: 'LnFeatures') -> None:
5✔
UNCOV
252
        conf_name = type(self).__name__
×
253
        # run base checks regardless whether LOCAL/REMOTE config
UNCOV
254
        super().validate_params(funding_sat=funding_sat, config=config, peer_features=peer_features)
×
255
        # run some stricter checks on LOCAL config (make sure we ourselves do the sane thing,
256
        # even if we are lenient with REMOTE for compatibility reasons)
UNCOV
257
        HTLC_MINIMUM_MSAT_MIN = 1
×
258
        if self.htlc_minimum_msat < HTLC_MINIMUM_MSAT_MIN:
×
259
            raise Exception(f"{conf_name}. htlc_minimum_msat too low: {self.htlc_minimum_msat} msat < {HTLC_MINIMUM_MSAT_MIN}")
×
260

261
@stored_as('remote_config')
5✔
262
@attr.s
5✔
263
class RemoteConfig(ChannelConfig):
5✔
264
    next_per_commitment_point = attr.ib(type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
265
    current_per_commitment_point = attr.ib(default=None, type=bytes, converter=hex_to_bytes, repr=bytes_to_hex)
5✔
266

267
@stored_in('fee_updates')
5✔
268
@attr.s
5✔
269
class FeeUpdate(StoredObject):
5✔
270
    rate = attr.ib(type=int)  # in sat/kw
5✔
271
    ctn_local = attr.ib(default=None, type=int)
5✔
272
    ctn_remote = attr.ib(default=None, type=int)
5✔
273

274
@stored_as('constraints')
5✔
275
@attr.s
5✔
276
class ChannelConstraints(StoredObject):
5✔
277
    flags = attr.ib(type=int, converter=int)
5✔
278
    capacity = attr.ib(type=int)  # in sat
5✔
279
    is_initiator = attr.ib(type=bool)  # note: sometimes also called "funder"
5✔
280
    funding_txn_minimum_depth = attr.ib(type=int)
5✔
281

282

283
CHANNEL_BACKUP_VERSION_LATEST = 2
5✔
284
KNOWN_CHANNEL_BACKUP_VERSIONS = (0, 1, 2, )
5✔
285
assert CHANNEL_BACKUP_VERSION_LATEST in KNOWN_CHANNEL_BACKUP_VERSIONS
5✔
286

287
@attr.s
5✔
288
class ChannelBackupStorage(StoredObject):
5✔
289
    funding_txid = attr.ib(type=str)
5✔
290
    funding_index = attr.ib(type=int, converter=int)
5✔
291
    funding_address = attr.ib(type=str)
5✔
292
    is_initiator = attr.ib(type=bool)
5✔
293

294
    def funding_outpoint(self):
5✔
UNCOV
295
        return Outpoint(self.funding_txid, self.funding_index)
×
296

297
    def channel_id(self):
5✔
UNCOV
298
        chan_id, _ = channel_id_from_funding_tx(self.funding_txid, self.funding_index)
×
299
        return chan_id
×
300

301
@stored_in('onchain_channel_backups')
5✔
302
@attr.s
5✔
303
class OnchainChannelBackupStorage(ChannelBackupStorage):
5✔
304
    node_id_prefix = attr.ib(type=bytes, converter=hex_to_bytes)  # remote node pubkey
5✔
305

306
@stored_in('imported_channel_backups')
5✔
307
@attr.s
5✔
308
class ImportedChannelBackupStorage(ChannelBackupStorage):
5✔
309
    node_id = attr.ib(type=bytes, converter=hex_to_bytes)  # remote node pubkey
5✔
310
    privkey = attr.ib(type=bytes, converter=hex_to_bytes)  # local node privkey
5✔
311
    host = attr.ib(type=str)
5✔
312
    port = attr.ib(type=int, converter=int)
5✔
313
    channel_seed = attr.ib(type=bytes, converter=hex_to_bytes)
5✔
314
    local_delay = attr.ib(type=int, converter=int)
5✔
315
    remote_delay = attr.ib(type=int, converter=int)
5✔
316
    remote_payment_pubkey = attr.ib(type=bytes, converter=hex_to_bytes)
5✔
317
    remote_revocation_pubkey = attr.ib(type=bytes, converter=hex_to_bytes)
5✔
318
    local_payment_pubkey = attr.ib(type=bytes, converter=hex_to_bytes)  # type: Optional[bytes]
5✔
319
    multisig_funding_privkey = attr.ib(type=bytes, converter=hex_to_bytes)  # type: Optional[bytes]
5✔
320

321
    def to_bytes(self) -> bytes:
5✔
UNCOV
322
        vds = BCDataStream()
×
323
        vds.write_uint16(CHANNEL_BACKUP_VERSION_LATEST)
×
324
        vds.write_boolean(self.is_initiator)
×
325
        vds.write_bytes(self.privkey, 32)
×
326
        vds.write_bytes(self.channel_seed, 32)
×
327
        vds.write_bytes(self.node_id, 33)
×
328
        vds.write_bytes(bfh(self.funding_txid), 32)
×
329
        vds.write_uint16(self.funding_index)
×
330
        vds.write_string(self.funding_address)
×
331
        vds.write_bytes(self.remote_payment_pubkey, 33)
×
332
        vds.write_bytes(self.remote_revocation_pubkey, 33)
×
333
        vds.write_uint16(self.local_delay)
×
334
        vds.write_uint16(self.remote_delay)
×
335
        vds.write_string(self.host)
×
336
        vds.write_uint16(self.port)
×
337
        vds.write_bytes(self.local_payment_pubkey, 33)
×
338
        vds.write_bytes(self.multisig_funding_privkey, 32)
×
339
        return bytes(vds.input)
×
340

341
    @staticmethod
5✔
342
    def from_bytes(s: bytes) -> "ImportedChannelBackupStorage":
5✔
343
        vds = BCDataStream()
5✔
344
        vds.write(s)
5✔
345
        version = vds.read_uint16()
5✔
346
        if version not in KNOWN_CHANNEL_BACKUP_VERSIONS:
5✔
UNCOV
347
            raise Exception(f"unknown version for channel backup: {version}")
×
348
        is_initiator = vds.read_boolean()
5✔
349
        privkey = vds.read_bytes(32)
5✔
350
        channel_seed = vds.read_bytes(32)
5✔
351
        node_id = vds.read_bytes(33)
5✔
352
        funding_txid = vds.read_bytes(32).hex()
5✔
353
        funding_index = vds.read_uint16()
5✔
354
        funding_address = vds.read_string()
5✔
355
        remote_payment_pubkey = vds.read_bytes(33)
5✔
356
        remote_revocation_pubkey = vds.read_bytes(33)
5✔
357
        local_delay = vds.read_uint16()
5✔
358
        remote_delay = vds.read_uint16()
5✔
359
        host = vds.read_string()
5✔
360
        port = vds.read_uint16()
5✔
361
        if version >= 1:
5✔
362
            local_payment_pubkey = vds.read_bytes(33)
5✔
363
        else:
364
            local_payment_pubkey = None
5✔
365
        if version >= 2:
5✔
UNCOV
366
            multisig_funding_privkey = vds.read_bytes(32)
×
367
        else:
368
            multisig_funding_privkey = None
5✔
369
        return ImportedChannelBackupStorage(
5✔
370
            is_initiator=is_initiator,
371
            privkey=privkey,
372
            channel_seed=channel_seed,
373
            node_id=node_id,
374
            funding_txid=funding_txid,
375
            funding_index=funding_index,
376
            funding_address=funding_address,
377
            remote_payment_pubkey=remote_payment_pubkey,
378
            remote_revocation_pubkey=remote_revocation_pubkey,
379
            local_delay=local_delay,
380
            remote_delay=remote_delay,
381
            host=host,
382
            port=port,
383
            local_payment_pubkey=local_payment_pubkey,
384
            multisig_funding_privkey=multisig_funding_privkey,
385
        )
386

387
    @staticmethod
5✔
388
    def from_encrypted_str(data: str, *, password: str) -> "ImportedChannelBackupStorage":
5✔
389
        if not data.startswith('channel_backup:'):
5✔
UNCOV
390
            raise ValueError("missing or invalid magic bytes")
×
391
        encrypted = data[15:]
5✔
392
        decrypted = pw_decode_with_version_and_mac(encrypted, password)
5✔
393
        return ImportedChannelBackupStorage.from_bytes(decrypted)
5✔
394

395

396
class ScriptHtlc(NamedTuple):
5✔
397
    redeem_script: bytes
5✔
398
    htlc: 'UpdateAddHtlc'
5✔
399

400

401
# FIXME duplicate of TxOutpoint in transaction.py??
402
@stored_as('funding_outpoint')
5✔
403
@attr.s
5✔
404
class Outpoint(StoredObject):
5✔
405
    txid = attr.ib(type=str)
5✔
406
    output_index = attr.ib(type=int)
5✔
407

408
    def to_str(self):
5✔
UNCOV
409
        return "{}:{}".format(self.txid, self.output_index)
×
410

411

412
class HtlcLog(NamedTuple):
5✔
413
    success: bool
5✔
414
    amount_msat: int  # amount for receiver (e.g. from invoice)
5✔
415
    route: Optional['LNPaymentRoute'] = None
5✔
416
    preimage: Optional[bytes] = None
5✔
417
    error_bytes: Optional[bytes] = None
5✔
418
    failure_msg: Optional['OnionRoutingFailure'] = None
5✔
419
    sender_idx: Optional[int] = None
5✔
420
    trampoline_fee_level: Optional[int] = None
5✔
421

422
    def formatted_tuple(self):
5✔
UNCOV
423
        route = self.route
×
424
        route_str = '%d'%len(route)
×
425
        short_channel_id = None
×
426
        if not self.success:
×
427
            sender_idx = self.sender_idx
×
428
            failure_msg = self.failure_msg
×
429
            if sender_idx is not None:
×
430
                try:
×
431
                    short_channel_id = route[sender_idx + 1].short_channel_id
×
432
                except IndexError:
×
433
                    # payment destination reported error
UNCOV
434
                    short_channel_id = _("Destination node")
×
435
            message = failure_msg.code_name()
×
436
        else:
UNCOV
437
            short_channel_id = route[-1].short_channel_id
×
438
            message = _('Success')
×
439
        chan_str = str(short_channel_id) if short_channel_id else _("Unknown")
×
440
        return route_str, chan_str, message
×
441

442

443
class LightningError(Exception): pass
5✔
444
class UnableToDeriveSecret(LightningError): pass
5✔
445
class RemoteMisbehaving(LightningError): pass
5✔
446

447
class NotFoundChanAnnouncementForUpdate(Exception): pass
5✔
448
class InvalidGossipMsg(Exception):
5✔
449
    """e.g. signature check failed"""
450

451
class PaymentFailure(UserFacingException): pass
5✔
452
class NoPathFound(PaymentFailure):
5✔
453
    def __str__(self):
5✔
454
        return _('No path found')
5✔
455
class FeeBudgetExceeded(PaymentFailure):
5✔
456
    def __str__(self):
5✔
UNCOV
457
        return _('Fee budget exceeded')
×
458

459

460
class LNProtocolError(Exception):
5✔
461
    """Raised in peer methods to trigger an error message."""
462

463

464
class LNProtocolWarning(Exception):
5✔
465
    """Raised in peer methods to trigger a warning message."""
466

467

468

469
# TODO make some of these values configurable?
470
REDEEM_AFTER_DOUBLE_SPENT_DELAY = 30
5✔
471

472
CHANNEL_OPENING_TIMEOUT = 24*60*60
5✔
473

474
# Small capacity channels are problematic for many reasons. As the onchain fees start to become
475
# significant compared to the capacity, things start to break down. e.g. the counterparty
476
# force-closing the channel costs much of the funds in the channel.
477
# Closing a channel uses ~200 vbytes onchain, feerates could spike to 100 sat/vbyte or even higher;
478
# that in itself is already 20_000 sats. This mining fee is reserved and cannot be used for payments.
479
# The value below is chosen arbitrarily to be one order of magnitude higher than that.
480
MIN_FUNDING_SAT = 200_000
5✔
481

482
##### CLTV-expiry-delta-related values
483
# see https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md#cltv_expiry_delta-selection
484

485
# the minimum cltv_expiry accepted for newly received HTLCs
486
# note: when changing, consider Blockchain.is_tip_stale()
487
MIN_FINAL_CLTV_DELTA_ACCEPTED = 144
5✔
488
# set it a tiny bit higher for invoices as blocks could get mined
489
# during forward path of payment
490
MIN_FINAL_CLTV_DELTA_FOR_INVOICE = MIN_FINAL_CLTV_DELTA_ACCEPTED + 3
5✔
491

492
# the deadline for offered HTLCs:
493
# the deadline after which the channel has to be failed and timed out on-chain
494
NBLOCK_DEADLINE_DELTA_AFTER_EXPIRY_FOR_OFFERED_HTLCS = 1
5✔
495

496
# the deadline for received HTLCs this node has fulfilled:
497
# the deadline after which the channel has to be failed and the HTLC fulfilled on-chain before its cltv_expiry
498
NBLOCK_DEADLINE_DELTA_BEFORE_EXPIRY_FOR_RECEIVED_HTLCS = 72
5✔
499

500
NBLOCK_CLTV_DELTA_TOO_FAR_INTO_FUTURE = 28 * 144
5✔
501

502
MAXIMUM_REMOTE_TO_SELF_DELAY_ACCEPTED = 2016
5✔
503

504
class RevocationStore:
5✔
505
    # closely based on code in lightningnetwork/lnd
506

507
    START_INDEX = 2 ** 48 - 1
5✔
508

509
    def __init__(self, storage):
5✔
510
        if len(storage) == 0:
5✔
511
            storage['index'] = self.START_INDEX
5✔
512
            storage['buckets'] = {}
5✔
513
        self.storage = storage
5✔
514
        self.buckets = storage['buckets']
5✔
515

516
    def add_next_entry(self, hsh):
5✔
517
        index = self.storage['index']
5✔
518
        new_element = ShachainElement(index=index, secret=hsh)
5✔
519
        bucket = count_trailing_zeros(index)
5✔
520
        for i in range(0, bucket):
5✔
521
            this_bucket = self.buckets[i]
5✔
522
            e = shachain_derive(new_element, this_bucket.index)
5✔
523
            if e != this_bucket:
5✔
524
                raise Exception("hash is not derivable: {} {} {}".format(e.secret.hex(), this_bucket.secret.hex(), this_bucket.index))
5✔
525
        self.buckets[bucket] = new_element
5✔
526
        self.storage['index'] = index - 1
5✔
527

528
    def retrieve_secret(self, index: int) -> bytes:
5✔
529
        assert index <= self.START_INDEX, index
5✔
530
        for i in range(0, 49):
5✔
531
            bucket = self.buckets.get(i)
5✔
532
            if bucket is None:
5✔
UNCOV
533
                raise UnableToDeriveSecret()
×
534
            try:
5✔
535
                element = shachain_derive(bucket, index)
5✔
536
            except UnableToDeriveSecret:
5✔
537
                continue
5✔
538
            return element.secret
5✔
UNCOV
539
        raise UnableToDeriveSecret()
×
540

541
    def __eq__(self, o):
5✔
UNCOV
542
        return type(o) is RevocationStore and self.serialize() == o.serialize()
×
543

544
    def __hash__(self):
5✔
UNCOV
545
        return hash(json.dumps(self.serialize(), sort_keys=True))
×
546

547

548
def count_trailing_zeros(index):
5✔
549
    """ BOLT-03 (where_to_put_secret) """
550
    try:
5✔
551
        return list(reversed(bin(index)[2:])).index("1")
5✔
UNCOV
552
    except ValueError:
×
553
        return 48
×
554

555
def shachain_derive(element, to_index):
5✔
556
    def get_prefix(index, pos):
5✔
557
        mask = (1 << 64) - 1 - ((1 << pos) - 1)
5✔
558
        return index & mask
5✔
559
    from_index = element.index
5✔
560
    zeros = count_trailing_zeros(from_index)
5✔
561
    if from_index != get_prefix(to_index, zeros):
5✔
562
        raise UnableToDeriveSecret("prefixes are different; index not derivable")
5✔
563
    return ShachainElement(
5✔
564
        get_per_commitment_secret_from_seed(element.secret, to_index, zeros),
565
        to_index)
566

567
class ShachainElement(NamedTuple):
5✔
568
    secret: bytes
5✔
569
    index: int
5✔
570

571
    def __str__(self):
5✔
UNCOV
572
        return "ShachainElement(" + self.secret.hex() + "," + str(self.index) + ")"
×
573

574
    @stored_in('buckets', tuple)
5✔
575
    def read(*x):
5✔
576
        return ShachainElement(bfh(x[0]), int(x[1]))
5✔
577

578

579
def get_per_commitment_secret_from_seed(seed: bytes, i: int, bits: int = 48) -> bytes:
5✔
580
    """Generate per commitment secret."""
581
    per_commitment_secret = bytearray(seed)
5✔
582
    for bitindex in range(bits - 1, -1, -1):
5✔
583
        mask = 1 << bitindex
5✔
584
        if i & mask:
5✔
585
            per_commitment_secret[bitindex // 8] ^= 1 << (bitindex % 8)
5✔
586
            per_commitment_secret = bytearray(sha256(per_commitment_secret))
5✔
587
    bajts = bytes(per_commitment_secret)
5✔
588
    return bajts
5✔
589

590
def secret_to_pubkey(secret: int) -> bytes:
5✔
591
    assert type(secret) is int
5✔
592
    return ecc.ECPrivkey.from_secret_scalar(secret).get_public_key_bytes(compressed=True)
5✔
593

594

595
def derive_pubkey(basepoint: bytes, per_commitment_point: bytes) -> bytes:
5✔
596
    p = ecc.ECPubkey(basepoint) + ecc.GENERATOR * ecc.string_to_number(sha256(per_commitment_point + basepoint))
5✔
597
    return p.get_public_key_bytes()
5✔
598

599
def derive_privkey(secret: int, per_commitment_point: bytes) -> int:
5✔
600
    assert type(secret) is int
5✔
601
    basepoint_bytes = secret_to_pubkey(secret)
5✔
602
    basepoint = secret + ecc.string_to_number(sha256(per_commitment_point + basepoint_bytes))
5✔
603
    basepoint %= CURVE_ORDER
5✔
604
    return basepoint
5✔
605

606
def derive_blinded_pubkey(basepoint: bytes, per_commitment_point: bytes) -> bytes:
5✔
607
    k1 = ecc.ECPubkey(basepoint) * ecc.string_to_number(sha256(basepoint + per_commitment_point))
5✔
608
    k2 = ecc.ECPubkey(per_commitment_point) * ecc.string_to_number(sha256(per_commitment_point + basepoint))
5✔
609
    return (k1 + k2).get_public_key_bytes()
5✔
610

611
def derive_blinded_privkey(basepoint_secret: bytes, per_commitment_secret: bytes) -> bytes:
5✔
UNCOV
612
    basepoint = ecc.ECPrivkey(basepoint_secret).get_public_key_bytes(compressed=True)
×
613
    per_commitment_point = ecc.ECPrivkey(per_commitment_secret).get_public_key_bytes(compressed=True)
×
614
    k1 = ecc.string_to_number(basepoint_secret) * ecc.string_to_number(sha256(basepoint + per_commitment_point))
×
615
    k2 = ecc.string_to_number(per_commitment_secret) * ecc.string_to_number(sha256(per_commitment_point + basepoint))
×
616
    sum = (k1 + k2) % ecc.CURVE_ORDER
×
617
    return int.to_bytes(sum, length=32, byteorder='big', signed=False)
×
618

619

620
def derive_payment_basepoint(static_payment_secret: bytes, funding_pubkey: bytes) -> Keypair:
5✔
UNCOV
621
    assert isinstance(static_payment_secret, bytes)
×
622
    assert isinstance(funding_pubkey, bytes)
×
623
    payment_basepoint = ecc.ECPrivkey(sha256(static_payment_secret + funding_pubkey))
×
624
    return Keypair(
×
625
        pubkey=payment_basepoint.get_public_key_bytes(),
626
        privkey=payment_basepoint.get_secret_bytes()
627
    )
628

629

630
def derive_multisig_funding_key_if_we_opened(
5✔
631
    *,
632
    funding_root_secret: bytes,
633
    remote_node_id_or_prefix: bytes,
634
    nlocktime: int,
635
) -> Keypair:
UNCOV
636
    from .lnworker import NODE_ID_PREFIX_LEN
×
637
    assert isinstance(funding_root_secret, bytes)
×
638
    assert len(funding_root_secret) == 32
×
639
    assert isinstance(remote_node_id_or_prefix, bytes)
×
640
    assert len(remote_node_id_or_prefix) in (NODE_ID_PREFIX_LEN, 33)
×
641
    assert isinstance(nlocktime, int)
×
642
    nlocktime_bytes = int.to_bytes(nlocktime, length=4, byteorder="little", signed=False)
×
643
    node_id_prefix = remote_node_id_or_prefix[0:NODE_ID_PREFIX_LEN]
×
644
    funding_key = ecc.ECPrivkey(bitcoin.bip340_tagged_hash(
×
645
        tag=b"electrum/ln_multisig_funding_key/we_opened",
646
        msg=funding_root_secret + node_id_prefix + nlocktime_bytes,
647
    ))
UNCOV
648
    return Keypair(
×
649
        pubkey=funding_key.get_public_key_bytes(),
650
        privkey=funding_key.get_secret_bytes(),
651
    )
652

653

654
def derive_multisig_funding_key_if_they_opened(
5✔
655
    *,
656
    funding_root_secret: bytes,
657
    remote_node_id_or_prefix: bytes,
658
    remote_funding_pubkey: bytes,
659
) -> Keypair:
UNCOV
660
    from .lnworker import NODE_ID_PREFIX_LEN
×
661
    assert isinstance(funding_root_secret, bytes)
×
662
    assert len(funding_root_secret) == 32
×
663
    assert isinstance(remote_node_id_or_prefix, bytes)
×
664
    assert len(remote_node_id_or_prefix) in (NODE_ID_PREFIX_LEN, 33)
×
665
    assert isinstance(remote_funding_pubkey, bytes)
×
666
    assert len(remote_funding_pubkey) == 33
×
667
    node_id_prefix = remote_node_id_or_prefix[0:NODE_ID_PREFIX_LEN]
×
668
    funding_key = ecc.ECPrivkey(bitcoin.bip340_tagged_hash(
×
669
        tag=b"electrum/ln_multisig_funding_key/they_opened",
670
        msg=funding_root_secret + node_id_prefix + remote_funding_pubkey,
671
    ))
UNCOV
672
    return Keypair(
×
673
        pubkey=funding_key.get_public_key_bytes(),
674
        privkey=funding_key.get_secret_bytes(),
675
    )
676

677

678
def make_htlc_tx_output(
5✔
679
    amount_msat,
680
    local_feerate,
681
    revocationpubkey,
682
    local_delayedpubkey,
683
    success,
684
    to_self_delay,
685
    has_anchors: bool
686
) -> Tuple[bytes, PartialTxOutput]:
687
    assert type(amount_msat) is int
5✔
688
    assert type(local_feerate) is int
5✔
689
    script = make_commitment_output_to_local_witness_script(
5✔
690
        revocation_pubkey=revocationpubkey,
691
        to_self_delay=to_self_delay,
692
        delayed_pubkey=local_delayedpubkey,
693
    )
694

695
    p2wsh = bitcoin.redeem_script_to_address('p2wsh', script)
5✔
696
    weight = effective_htlc_tx_weight(success=success, has_anchors=has_anchors)
5✔
697
    fee = local_feerate * weight
5✔
698
    fee = fee // 1000 * 1000
5✔
699
    final_amount_sat = (amount_msat - fee) // 1000
5✔
700
    assert final_amount_sat > 0, final_amount_sat
5✔
701
    output = PartialTxOutput.from_address_and_value(p2wsh, final_amount_sat)
5✔
702
    return script, output
5✔
703

704
def make_htlc_tx_witness(remotehtlcsig: bytes, localhtlcsig: bytes,
5✔
705
                         payment_preimage: bytes, witness_script: bytes) -> bytes:
706
    assert type(remotehtlcsig) is bytes
5✔
707
    assert type(localhtlcsig) is bytes
5✔
708
    assert type(payment_preimage) is bytes
5✔
709
    assert type(witness_script) is bytes
5✔
710
    return construct_witness([0, remotehtlcsig, localhtlcsig, payment_preimage, witness_script])
5✔
711

712
def make_htlc_tx_inputs(htlc_output_txid: str, htlc_output_index: int,
5✔
713
                        amount_msat: int, witness_script: bytes) -> List[PartialTxInput]:
714
    assert type(htlc_output_txid) is str
5✔
715
    assert type(htlc_output_index) is int
5✔
716
    assert type(amount_msat) is int
5✔
717
    assert type(witness_script) is bytes
5✔
718
    txin = PartialTxInput(prevout=TxOutpoint(txid=bfh(htlc_output_txid), out_idx=htlc_output_index),
5✔
719
                          nsequence=0)
720
    txin.witness_script = witness_script
5✔
721
    txin.script_sig = b''
5✔
722
    txin._trusted_value_sats = amount_msat // 1000
5✔
723
    c_inputs = [txin]
5✔
724
    return c_inputs
5✔
725

726
def make_htlc_tx(*, cltv_abs: int, inputs: List[PartialTxInput], output: PartialTxOutput) -> PartialTransaction:
5✔
727
    assert type(cltv_abs) is int
5✔
728
    c_outputs = [output]
5✔
729
    tx = PartialTransaction.from_io(inputs, c_outputs, locktime=cltv_abs, version=2)
5✔
730
    return tx
5✔
731

732
def make_offered_htlc(
5✔
733
    *,
734
    revocation_pubkey: bytes,
735
    remote_htlcpubkey: bytes,
736
    local_htlcpubkey: bytes,
737
    payment_hash: bytes,
738
    has_anchors: bool,
739
) -> bytes:
740
    assert type(revocation_pubkey) is bytes
5✔
741
    assert type(remote_htlcpubkey) is bytes
5✔
742
    assert type(local_htlcpubkey) is bytes
5✔
743
    assert type(payment_hash) is bytes
5✔
744
    script_opcodes = [
5✔
745
        opcodes.OP_DUP,
746
        opcodes.OP_HASH160,
747
        bitcoin.hash_160(revocation_pubkey),
748
        opcodes.OP_EQUAL,
749
        opcodes.OP_IF,
750
        opcodes.OP_CHECKSIG,
751
        opcodes.OP_ELSE,
752
        remote_htlcpubkey,
753
        opcodes.OP_SWAP,
754
        opcodes.OP_SIZE,
755
        32,
756
        opcodes.OP_EQUAL,
757
        opcodes.OP_NOTIF,
758
        opcodes.OP_DROP,
759
        2,
760
        opcodes.OP_SWAP,
761
        local_htlcpubkey,
762
        2,
763
        opcodes.OP_CHECKMULTISIG,
764
        opcodes.OP_ELSE,
765
        opcodes.OP_HASH160,
766
        crypto.ripemd(payment_hash),
767
        opcodes.OP_EQUALVERIFY,
768
        opcodes.OP_CHECKSIG,
769
        opcodes.OP_ENDIF,
770
    ]
771
    if has_anchors:
5✔
772
        script_opcodes.extend([1, opcodes.OP_CHECKSEQUENCEVERIFY, opcodes.OP_DROP])
5✔
773
    script_opcodes.append(opcodes.OP_ENDIF)
5✔
774
    script = construct_script(script_opcodes)
5✔
775
    return script
5✔
776

777
def make_received_htlc(
5✔
778
    *,
779
    revocation_pubkey: bytes,
780
    remote_htlcpubkey: bytes,
781
    local_htlcpubkey: bytes,
782
    payment_hash: bytes,
783
    cltv_abs: int,
784
    has_anchors: bool,
785
) -> bytes:
786
    for i in [revocation_pubkey, remote_htlcpubkey, local_htlcpubkey, payment_hash]:
5✔
787
        assert type(i) is bytes
5✔
788
    assert type(cltv_abs) is int
5✔
789

790
    script_opcodes = [
5✔
791
        opcodes.OP_DUP,
792
        opcodes.OP_HASH160,
793
        bitcoin.hash_160(revocation_pubkey),
794
        opcodes.OP_EQUAL,
795
        opcodes.OP_IF,
796
        opcodes.OP_CHECKSIG,
797
        opcodes.OP_ELSE,
798
        remote_htlcpubkey,
799
        opcodes.OP_SWAP,
800
        opcodes.OP_SIZE,
801
        32,
802
        opcodes.OP_EQUAL,
803
        opcodes.OP_IF,
804
        opcodes.OP_HASH160,
805
        crypto.ripemd(payment_hash),
806
        opcodes.OP_EQUALVERIFY,
807
        2,
808
        opcodes.OP_SWAP,
809
        local_htlcpubkey,
810
        2,
811
        opcodes.OP_CHECKMULTISIG,
812
        opcodes.OP_ELSE,
813
        opcodes.OP_DROP,
814
        cltv_abs,
815
        opcodes.OP_CHECKLOCKTIMEVERIFY,
816
        opcodes.OP_DROP,
817
        opcodes.OP_CHECKSIG,
818
        opcodes.OP_ENDIF,
819
    ]
820
    if has_anchors:
5✔
821
        script_opcodes.extend([1, opcodes.OP_CHECKSEQUENCEVERIFY, opcodes.OP_DROP])
5✔
822
    script_opcodes.append(opcodes.OP_ENDIF)
5✔
823
    script = construct_script(script_opcodes)
5✔
824
    return script
5✔
825

826
WITNESS_TEMPLATE_OFFERED_HTLC = [
5✔
827
    opcodes.OP_DUP,
828
    opcodes.OP_HASH160,
829
    OPPushDataGeneric(None),
830
    opcodes.OP_EQUAL,
831
    opcodes.OP_IF,
832
    opcodes.OP_CHECKSIG,
833
    opcodes.OP_ELSE,
834
    OPPushDataGeneric(None),
835
    opcodes.OP_SWAP,
836
    opcodes.OP_SIZE,
837
    OPPushDataGeneric(lambda x: x==1),
838
    opcodes.OP_EQUAL,
839
    opcodes.OP_NOTIF,
840
    opcodes.OP_DROP,
841
    opcodes.OP_2,
842
    opcodes.OP_SWAP,
843
    OPPushDataGeneric(None),
844
    opcodes.OP_2,
845
    opcodes.OP_CHECKMULTISIG,
846
    opcodes.OP_ELSE,
847
    opcodes.OP_HASH160,
848
    OPPushDataGeneric(None),
849
    opcodes.OP_EQUALVERIFY,
850
    opcodes.OP_CHECKSIG,
851
    opcodes.OP_ENDIF,
852
    opcodes.OP_ENDIF,
853
]
854

855
WITNESS_TEMPLATE_RECEIVED_HTLC = [
5✔
856
    opcodes.OP_DUP,
857
    opcodes.OP_HASH160,
858
    OPPushDataGeneric(None),
859
    opcodes.OP_EQUAL,
860
    opcodes.OP_IF,
861
    opcodes.OP_CHECKSIG,
862
    opcodes.OP_ELSE,
863
    OPPushDataGeneric(None),
864
    opcodes.OP_SWAP,
865
    opcodes.OP_SIZE,
866
    OPPushDataGeneric(lambda x: x==1),
867
    opcodes.OP_EQUAL,
868
    opcodes.OP_IF,
869
    opcodes.OP_HASH160,
870
    OPPushDataGeneric(None),
871
    opcodes.OP_EQUALVERIFY,
872
    opcodes.OP_2,
873
    opcodes.OP_SWAP,
874
    OPPushDataGeneric(None),
875
    opcodes.OP_2,
876
    opcodes.OP_CHECKMULTISIG,
877
    opcodes.OP_ELSE,
878
    opcodes.OP_DROP,
879
    OPPushDataGeneric(None),
880
    opcodes.OP_CHECKLOCKTIMEVERIFY,
881
    opcodes.OP_DROP,
882
    opcodes.OP_CHECKSIG,
883
    opcodes.OP_ENDIF,
884
    opcodes.OP_ENDIF,
885
]
886

887

888
def make_htlc_output_witness_script(
5✔
889
    *,
890
    is_received_htlc: bool,
891
    remote_revocation_pubkey: bytes,
892
    remote_htlc_pubkey: bytes,
893
    local_htlc_pubkey: bytes,
894
    payment_hash: bytes,
895
    cltv_abs: Optional[int],
896
    has_anchors: bool,
897
) -> bytes:
898
    if is_received_htlc:
5✔
899
        return make_received_htlc(
5✔
900
            revocation_pubkey=remote_revocation_pubkey,
901
            remote_htlcpubkey=remote_htlc_pubkey,
902
            local_htlcpubkey=local_htlc_pubkey,
903
            payment_hash=payment_hash,
904
            cltv_abs=cltv_abs,
905
            has_anchors=has_anchors,
906
        )
907
    else:
908
        return make_offered_htlc(
5✔
909
            revocation_pubkey=remote_revocation_pubkey,
910
            remote_htlcpubkey=remote_htlc_pubkey,
911
            local_htlcpubkey=local_htlc_pubkey,
912
            payment_hash=payment_hash,
913
            has_anchors=has_anchors,
914
        )
915

916

917
def get_ordered_channel_configs(chan: 'AbstractChannel', for_us: bool) -> Tuple[Union[LocalConfig, RemoteConfig],
5✔
918
                                                                                Union[LocalConfig, RemoteConfig]]:
919
    conf =       chan.config[LOCAL] if     for_us else chan.config[REMOTE]
5✔
920
    other_conf = chan.config[LOCAL] if not for_us else chan.config[REMOTE]
5✔
921
    return conf, other_conf
5✔
922

923

924
def possible_output_idxs_of_htlc_in_ctx(*, chan: 'Channel', pcp: bytes, subject: 'HTLCOwner',
5✔
925
                                        htlc_direction: 'Direction', ctx: Transaction,
926
                                        htlc: 'UpdateAddHtlc') -> Set[int]:
927
    amount_msat, cltv_abs, payment_hash = htlc.amount_msat, htlc.cltv_abs, htlc.payment_hash
5✔
928
    for_us = subject == LOCAL
5✔
929
    conf, other_conf = get_ordered_channel_configs(chan=chan, for_us=for_us)
5✔
930

931
    other_revocation_pubkey = derive_blinded_pubkey(other_conf.revocation_basepoint.pubkey, pcp)
5✔
932
    other_htlc_pubkey = derive_pubkey(other_conf.htlc_basepoint.pubkey, pcp)
5✔
933
    htlc_pubkey = derive_pubkey(conf.htlc_basepoint.pubkey, pcp)
5✔
934
    witness_script = make_htlc_output_witness_script(
5✔
935
        is_received_htlc=htlc_direction == RECEIVED,
936
        remote_revocation_pubkey=other_revocation_pubkey,
937
        remote_htlc_pubkey=other_htlc_pubkey,
938
        local_htlc_pubkey=htlc_pubkey,
939
        payment_hash=payment_hash,
940
        cltv_abs=cltv_abs,
941
        has_anchors=chan.has_anchors(),
942
    )
943
    htlc_address = redeem_script_to_address('p2wsh', witness_script)
5✔
944
    candidates = ctx.get_output_idxs_from_address(htlc_address)
5✔
945
    return {output_idx for output_idx in candidates
5✔
946
            if ctx.outputs()[output_idx].value == htlc.amount_msat // 1000}
947

948

949
def map_htlcs_to_ctx_output_idxs(*, chan: 'Channel', ctx: Transaction, pcp: bytes,
5✔
950
                                 subject: 'HTLCOwner', ctn: int) -> Dict[Tuple['Direction', 'UpdateAddHtlc'], Tuple[int, int]]:
951
    """Returns a dict from (htlc_dir, htlc) to (ctx_output_idx, htlc_relative_idx)"""
952
    htlc_to_ctx_output_idx_map = {}  # type: Dict[Tuple[Direction, UpdateAddHtlc], int]
5✔
953
    unclaimed_ctx_output_idxs = set(range(len(ctx.outputs())))
5✔
954
    offered_htlcs = chan.included_htlcs(subject, SENT, ctn=ctn)
5✔
955
    offered_htlcs.sort(key=lambda htlc: htlc.cltv_abs)
5✔
956
    received_htlcs = chan.included_htlcs(subject, RECEIVED, ctn=ctn)
5✔
957
    received_htlcs.sort(key=lambda htlc: htlc.cltv_abs)
5✔
958
    for direction, htlcs in zip([SENT, RECEIVED], [offered_htlcs, received_htlcs]):
5✔
959
        for htlc in htlcs:
5✔
960
            cands = sorted(possible_output_idxs_of_htlc_in_ctx(chan=chan,
5✔
961
                                                               pcp=pcp,
962
                                                               subject=subject,
963
                                                               htlc_direction=direction,
964
                                                               ctx=ctx,
965
                                                               htlc=htlc))
966
            for ctx_output_idx in cands:
5✔
967
                if ctx_output_idx in unclaimed_ctx_output_idxs:
5✔
968
                    unclaimed_ctx_output_idxs.discard(ctx_output_idx)
5✔
969
                    htlc_to_ctx_output_idx_map[(direction, htlc)] = ctx_output_idx
5✔
970
                    break
5✔
971
    # calc htlc_relative_idx
972
    inverse_map = {ctx_output_idx: (direction, htlc)
5✔
973
                   for ((direction, htlc), ctx_output_idx) in htlc_to_ctx_output_idx_map.items()}
974

975
    return {inverse_map[ctx_output_idx]: (ctx_output_idx, htlc_relative_idx)
5✔
976
            for htlc_relative_idx, ctx_output_idx in enumerate(sorted(inverse_map))}
977

978

979
def make_htlc_tx_with_open_channel(*, chan: 'Channel', pcp: bytes, subject: 'HTLCOwner', ctn: int,
5✔
980
                                   htlc_direction: 'Direction', commit: Transaction, ctx_output_idx: int,
981
                                   htlc: 'UpdateAddHtlc', name: str = None) -> Tuple[bytes, PartialTransaction]:
982
    amount_msat, cltv_abs, payment_hash = htlc.amount_msat, htlc.cltv_abs, htlc.payment_hash
5✔
983
    for_us = subject == LOCAL
5✔
984
    conf, other_conf = get_ordered_channel_configs(chan=chan, for_us=for_us)
5✔
985

986
    delayedpubkey = derive_pubkey(conf.delayed_basepoint.pubkey, pcp)
5✔
987
    other_revocation_pubkey = derive_blinded_pubkey(other_conf.revocation_basepoint.pubkey, pcp)
5✔
988
    other_htlc_pubkey = derive_pubkey(other_conf.htlc_basepoint.pubkey, pcp)
5✔
989
    htlc_pubkey = derive_pubkey(conf.htlc_basepoint.pubkey, pcp)
5✔
990
    # HTLC-success for the HTLC spending from a received HTLC output
991
    # if we do not receive, and the commitment tx is not for us, they receive, so it is also an HTLC-success
992
    is_htlc_success = htlc_direction == RECEIVED
5✔
993
    witness_script_of_htlc_tx_output, htlc_tx_output = make_htlc_tx_output(
5✔
994
        amount_msat=amount_msat,
995
        local_feerate=chan.get_feerate(subject, ctn=ctn),
996
        revocationpubkey=other_revocation_pubkey,
997
        local_delayedpubkey=delayedpubkey,
998
        success=is_htlc_success,
999
        to_self_delay=other_conf.to_self_delay,
1000
        has_anchors=chan.has_anchors(),
1001
    )
1002
    witness_script_in = make_htlc_output_witness_script(
5✔
1003
        is_received_htlc=is_htlc_success,
1004
        remote_revocation_pubkey=other_revocation_pubkey,
1005
        remote_htlc_pubkey=other_htlc_pubkey,
1006
        local_htlc_pubkey=htlc_pubkey,
1007
        payment_hash=payment_hash,
1008
        cltv_abs=cltv_abs,
1009
        has_anchors=chan.has_anchors(),
1010
    )
1011
    htlc_tx_inputs = make_htlc_tx_inputs(
5✔
1012
        commit.txid(), ctx_output_idx,
1013
        amount_msat=amount_msat,
1014
        witness_script=witness_script_in)
1015
    if chan.has_anchors():
5✔
1016
        htlc_tx_inputs[0].nsequence = 1
5✔
1017
    if is_htlc_success:
5✔
1018
        cltv_abs = 0
5✔
1019
    htlc_tx = make_htlc_tx(cltv_abs=cltv_abs, inputs=htlc_tx_inputs, output=htlc_tx_output)
5✔
1020
    return witness_script_of_htlc_tx_output, htlc_tx
5✔
1021

1022
def make_funding_input(local_funding_pubkey: bytes, remote_funding_pubkey: bytes,
5✔
1023
        funding_pos: int, funding_txid: str, funding_sat: int) -> PartialTxInput:
1024
    pubkeys = sorted([local_funding_pubkey.hex(), remote_funding_pubkey.hex()])
5✔
1025
    # commitment tx input
1026
    prevout = TxOutpoint(txid=bfh(funding_txid), out_idx=funding_pos)
5✔
1027
    c_input = PartialTxInput(prevout=prevout)
5✔
1028

1029
    ppubkeys = [descriptor.PubkeyProvider.parse(pk) for pk in pubkeys]
5✔
1030
    multi = descriptor.MultisigDescriptor(pubkeys=ppubkeys, thresh=2, is_sorted=True)
5✔
1031
    c_input.script_descriptor = descriptor.WSHDescriptor(subdescriptor=multi)
5✔
1032
    c_input._trusted_value_sats = funding_sat
5✔
1033
    return c_input
5✔
1034

1035

1036
class HTLCOwner(IntEnum):
5✔
1037
    LOCAL = 1
5✔
1038
    REMOTE = -LOCAL
5✔
1039

1040
    def inverted(self) -> 'HTLCOwner':
5✔
1041
        return -self
5✔
1042

1043
    def __neg__(self) -> 'HTLCOwner':
5✔
1044
        return HTLCOwner(super().__neg__())
5✔
1045

1046

1047
class Direction(IntEnum):
5✔
1048
    SENT = -1     # in the context of HTLCs: "offered" HTLCs
5✔
1049
    RECEIVED = 1  # in the context of HTLCs: "received" HTLCs
5✔
1050

1051
SENT = Direction.SENT
5✔
1052
RECEIVED = Direction.RECEIVED
5✔
1053

1054
LOCAL = HTLCOwner.LOCAL
5✔
1055
REMOTE = HTLCOwner.REMOTE
5✔
1056

1057

1058
def make_commitment_outputs(
5✔
1059
    *,
1060
    fees_per_participant: Mapping[HTLCOwner, int],
1061
    local_amount_msat: int,
1062
    remote_amount_msat: int,
1063
    local_script: bytes,
1064
    remote_script: bytes,
1065
    htlcs: List[ScriptHtlc],
1066
    dust_limit_sat: int,
1067
    has_anchors: bool,
1068
    local_anchor_script: Optional[str],
1069
    remote_anchor_script: Optional[str]
1070
) -> Tuple[List[PartialTxOutput], List[PartialTxOutput]]:
1071

1072
    # determine HTLC outputs and trim below dust to know if anchors need to be included
1073
    htlc_outputs = []
5✔
1074
    for script, htlc in htlcs:
5✔
1075
        addr = bitcoin.redeem_script_to_address('p2wsh', script)
5✔
1076
        if htlc.amount_msat // 1000 > dust_limit_sat:
5✔
1077
            htlc_outputs.append(
5✔
1078
                PartialTxOutput(
1079
                    scriptpubkey=address_to_script(addr),
1080
                    value=htlc.amount_msat // 1000
1081
                ))
1082

1083
    # BOLT-03: "Base commitment transaction fees are extracted from the funder's amount;
1084
    #           if that amount is insufficient, the entire amount of the funder's output is used."
1085
    non_htlc_outputs = []
5✔
1086
    to_local_amt_msat = local_amount_msat - fees_per_participant[LOCAL]
5✔
1087
    to_remote_amt_msat = remote_amount_msat - fees_per_participant[REMOTE]
5✔
1088

1089
    anchor_outputs = []
5✔
1090
    # if no anchor scripts are set, we ignore anchor outputs, useful when this
1091
    # function is used to determine outputs for a collaborative close
1092
    if has_anchors and local_anchor_script and remote_anchor_script:
5✔
1093
        local_pays_anchors = bool(fees_per_participant[LOCAL])
5✔
1094
        # we always allocate for two anchor outputs even if they are not added
1095
        if local_pays_anchors:
5✔
1096
            to_local_amt_msat -= 2 * FIXED_ANCHOR_SAT * 1000
5✔
1097
        else:
1098
            to_remote_amt_msat -= 2 * FIXED_ANCHOR_SAT * 1000
5✔
1099

1100
        # include anchors for outputs that materialize, include both if there are HTLCs present
1101
        if to_local_amt_msat // 1000 >= dust_limit_sat or htlc_outputs:
5✔
1102
            anchor_outputs.append(PartialTxOutput(scriptpubkey=local_anchor_script, value=FIXED_ANCHOR_SAT))
5✔
1103
        if to_remote_amt_msat // 1000 >= dust_limit_sat or htlc_outputs:
5✔
1104
            anchor_outputs.append(PartialTxOutput(scriptpubkey=remote_anchor_script, value=FIXED_ANCHOR_SAT))
5✔
1105

1106
    # if funder cannot afford feerate, their output might go negative, so take max(0, x) here
1107
    to_local_amt_msat = max(0, to_local_amt_msat)
5✔
1108
    to_remote_amt_msat = max(0, to_remote_amt_msat)
5✔
1109
    non_htlc_outputs.append(PartialTxOutput(scriptpubkey=local_script, value=to_local_amt_msat // 1000))
5✔
1110
    non_htlc_outputs.append(PartialTxOutput(scriptpubkey=remote_script, value=to_remote_amt_msat // 1000))
5✔
1111

1112
    c_outputs_filtered = list(filter(lambda x: x.value >= dust_limit_sat, non_htlc_outputs + htlc_outputs))
5✔
1113
    c_outputs = c_outputs_filtered + anchor_outputs
5✔
1114
    return htlc_outputs, c_outputs
5✔
1115

1116

1117
def effective_htlc_tx_weight(success: bool, has_anchors: bool):
5✔
1118
    # for anchors-zero-fee-htlc we set an effective weight of zero
1119
    # we only trim htlcs below dust, as in the anchors commitment format,
1120
    # the fees for the hltc transaction don't need to be subtracted from
1121
    # the htlc output, but fees are taken from extra attached inputs
1122
    if has_anchors:
5✔
1123
        return 0 * HTLC_SUCCESS_WEIGHT_ANCHORS if success else 0 * HTLC_TIMEOUT_WEIGHT_ANCHORS
5✔
1124
    else:
1125
        return HTLC_SUCCESS_WEIGHT if success else HTLC_TIMEOUT_WEIGHT
5✔
1126

1127

1128
def offered_htlc_trim_threshold_sat(*, dust_limit_sat: int, feerate: int, has_anchors: bool) -> int:
5✔
1129
    # offered htlcs strictly below this amount will be trimmed (from ctx).
1130
    # feerate is in sat/kw
1131
    # returns value in sat
1132
    weight = effective_htlc_tx_weight(success=False, has_anchors=has_anchors)
5✔
1133
    return dust_limit_sat + weight * feerate // 1000
5✔
1134

1135

1136
def received_htlc_trim_threshold_sat(*, dust_limit_sat: int, feerate: int, has_anchors: bool) -> int:
5✔
1137
    # received htlcs strictly below this amount will be trimmed (from ctx).
1138
    # feerate is in sat/kw
1139
    # returns value in sat
1140
    weight = effective_htlc_tx_weight(success=True, has_anchors=has_anchors)
5✔
1141
    return dust_limit_sat + weight * feerate // 1000
5✔
1142

1143

1144
def fee_for_htlc_output(*, feerate: int) -> int:
5✔
1145
    # feerate is in sat/kw
1146
    # returns fee in msat
1147
    return feerate * HTLC_OUTPUT_WEIGHT
5✔
1148

1149

1150
def calc_fees_for_commitment_tx(*, num_htlcs: int, feerate: int,
5✔
1151
                                is_local_initiator: bool, round_to_sat: bool = True, has_anchors: bool) -> Dict['HTLCOwner', int]:
1152
    # feerate is in sat/kw
1153
    # returns fees in msats
1154
    # note: BOLT-02 specifies that msat fees need to be rounded down to sat.
1155
    #       However, the rounding needs to happen for the total fees, so if the return value
1156
    #       is to be used as part of additional fee calculation then rounding should be done after that.
1157
    if has_anchors:
5✔
1158
        commitment_tx_weight = COMMITMENT_TX_WEIGHT_ANCHORS
5✔
1159
    else:
1160
        commitment_tx_weight = COMMITMENT_TX_WEIGHT
5✔
1161
    overall_weight = commitment_tx_weight + num_htlcs * HTLC_OUTPUT_WEIGHT
5✔
1162
    fee = feerate * overall_weight
5✔
1163
    if round_to_sat:
5✔
1164
        fee = fee // 1000 * 1000
5✔
1165
    return {
5✔
1166
        LOCAL: fee if is_local_initiator else 0,
1167
        REMOTE: fee if not is_local_initiator else 0,
1168
    }
1169

1170

1171
def make_commitment(
5✔
1172
        *,
1173
        ctn: int,
1174
        local_funding_pubkey: bytes,
1175
        remote_funding_pubkey: bytes,
1176
        remote_payment_pubkey: bytes,
1177
        funder_payment_basepoint: bytes,
1178
        fundee_payment_basepoint: bytes,
1179
        revocation_pubkey: bytes,
1180
        delayed_pubkey: bytes,
1181
        to_self_delay: int,
1182
        funding_txid: str,
1183
        funding_pos: int,
1184
        funding_sat: int,
1185
        local_amount: int,
1186
        remote_amount: int,
1187
        dust_limit_sat: int,
1188
        fees_per_participant: Mapping[HTLCOwner, int],
1189
        htlcs: List[ScriptHtlc],
1190
        has_anchors: bool
1191
) -> PartialTransaction:
1192
    c_input = make_funding_input(local_funding_pubkey, remote_funding_pubkey,
5✔
1193
                                 funding_pos, funding_txid, funding_sat)
1194
    obs = get_obscured_ctn(ctn, funder_payment_basepoint, fundee_payment_basepoint)
5✔
1195
    locktime = (0x20 << 24) + (obs & 0xffffff)
5✔
1196
    sequence = (0x80 << 24) + (obs >> 24)
5✔
1197
    c_input.nsequence = sequence
5✔
1198

1199
    c_inputs = [c_input]
5✔
1200

1201
    # commitment tx outputs
1202
    local_address = make_commitment_output_to_local_address(revocation_pubkey, to_self_delay, delayed_pubkey)
5✔
1203
    remote_address = make_commitment_output_to_remote_address(remote_payment_pubkey, has_anchors)
5✔
1204
    local_anchor_address = None
5✔
1205
    remote_anchor_address = None
5✔
1206
    if has_anchors:
5✔
1207
        local_anchor_address = make_commitment_output_to_anchor_address(local_funding_pubkey)
5✔
1208
        remote_anchor_address = make_commitment_output_to_anchor_address(remote_funding_pubkey)
5✔
1209
    # note: it is assumed that the given 'htlcs' are all non-dust (dust htlcs already trimmed)
1210

1211
    # BOLT-03: "Transaction Input and Output Ordering
1212
    #           Lexicographic ordering: see BIP69. In the case of identical HTLC outputs,
1213
    #           the outputs are ordered in increasing cltv_expiry order."
1214
    # so we sort by cltv_expiry now; and the later BIP69-sort is assumed to be *stable*
1215
    htlcs = list(htlcs)
5✔
1216
    htlcs.sort(key=lambda x: x.htlc.cltv_abs)
5✔
1217

1218
    htlc_outputs, c_outputs_filtered = make_commitment_outputs(
5✔
1219
        fees_per_participant=fees_per_participant,
1220
        local_amount_msat=local_amount,
1221
        remote_amount_msat=remote_amount,
1222
        local_script=address_to_script(local_address),
1223
        remote_script=address_to_script(remote_address),
1224
        htlcs=htlcs,
1225
        dust_limit_sat=dust_limit_sat,
1226
        has_anchors=has_anchors,
1227
        local_anchor_script=address_to_script(local_anchor_address) if local_anchor_address else None,
1228
        remote_anchor_script=address_to_script(remote_anchor_address) if remote_anchor_address else None
1229
    )
1230

1231
    assert sum(x.value for x in c_outputs_filtered) <= funding_sat, (c_outputs_filtered, funding_sat)
5✔
1232

1233
    # create commitment tx
1234
    tx = PartialTransaction.from_io(c_inputs, c_outputs_filtered, locktime=locktime, version=2)
5✔
1235
    return tx
5✔
1236

1237
def make_commitment_output_to_local_witness_script(
5✔
1238
        revocation_pubkey: bytes, to_self_delay: int, delayed_pubkey: bytes,
1239
) -> bytes:
1240
    assert type(revocation_pubkey) is bytes
5✔
1241
    assert type(to_self_delay) is int
5✔
1242
    assert type(delayed_pubkey) is bytes
5✔
1243
    script = construct_script([
5✔
1244
        opcodes.OP_IF,
1245
        revocation_pubkey,
1246
        opcodes.OP_ELSE,
1247
        to_self_delay,
1248
        opcodes.OP_CHECKSEQUENCEVERIFY,
1249
        opcodes.OP_DROP,
1250
        delayed_pubkey,
1251
        opcodes.OP_ENDIF,
1252
        opcodes.OP_CHECKSIG,
1253
    ])
1254
    return script
5✔
1255

1256
def make_commitment_output_to_local_address(
5✔
1257
        revocation_pubkey: bytes, to_self_delay: int, delayed_pubkey: bytes) -> str:
1258
    local_script = make_commitment_output_to_local_witness_script(revocation_pubkey, to_self_delay, delayed_pubkey)
5✔
1259
    return bitcoin.redeem_script_to_address('p2wsh', local_script)
5✔
1260

1261
def make_commitment_output_to_remote_witness_script(remote_payment_pubkey: bytes) -> bytes:
5✔
1262
    assert isinstance(remote_payment_pubkey, bytes)
5✔
1263
    script = construct_script([
5✔
1264
        remote_payment_pubkey,
1265
        opcodes.OP_CHECKSIGVERIFY,
1266
        opcodes.OP_1,
1267
        opcodes.OP_CHECKSEQUENCEVERIFY,
1268
    ])
1269
    return script
5✔
1270

1271
def make_commitment_output_to_remote_address(remote_payment_pubkey: bytes, has_anchors: bool) -> str:
5✔
1272
    if has_anchors:
5✔
1273
        remote_script = make_commitment_output_to_remote_witness_script(remote_payment_pubkey)
5✔
1274
        return bitcoin.redeem_script_to_address('p2wsh', remote_script)
5✔
1275
    else:
1276
        return bitcoin.pubkey_to_address('p2wpkh', remote_payment_pubkey.hex())
5✔
1277

1278
def make_commitment_output_to_anchor_witness_script(funding_pubkey: bytes) -> bytes:
5✔
1279
    assert isinstance(funding_pubkey, bytes)
5✔
1280
    script = construct_script([
5✔
1281
        funding_pubkey,
1282
        opcodes.OP_CHECKSIG,
1283
        opcodes.OP_IFDUP,
1284
        opcodes.OP_NOTIF,
1285
        opcodes.OP_16,
1286
        opcodes.OP_CHECKSEQUENCEVERIFY,
1287
        opcodes.OP_ENDIF,
1288
    ])
1289
    return script
5✔
1290

1291
def make_commitment_output_to_anchor_address(funding_pubkey: bytes) -> str:
5✔
1292
    script = make_commitment_output_to_anchor_witness_script(funding_pubkey)
5✔
1293
    return bitcoin.redeem_script_to_address('p2wsh', script)
5✔
1294

1295
def sign_and_get_sig_string(tx: PartialTransaction, local_config, remote_config):
5✔
1296
    tx.sign({local_config.multisig_key.pubkey: local_config.multisig_key.privkey})
5✔
1297
    sig = tx.inputs()[0].sigs_ecdsa[local_config.multisig_key.pubkey]
5✔
1298
    sig_64 = ecdsa_sig64_from_der_sig(sig[:-1])
5✔
1299
    return sig_64
5✔
1300

1301
def funding_output_script(local_config: 'LocalConfig', remote_config: 'RemoteConfig') -> bytes:
5✔
UNCOV
1302
    return funding_output_script_from_keys(local_config.multisig_key.pubkey, remote_config.multisig_key.pubkey)
×
1303

1304
def funding_output_script_from_keys(pubkey1: bytes, pubkey2: bytes) -> bytes:
5✔
UNCOV
1305
    pubkeys = sorted([pubkey1.hex(), pubkey2.hex()])
×
1306
    return transaction.multisig_script(pubkeys, 2)
×
1307

1308

1309
def get_obscured_ctn(ctn: int, funder: bytes, fundee: bytes) -> int:
5✔
1310
    mask = int.from_bytes(sha256(funder + fundee)[-6:], 'big')
5✔
1311
    return ctn ^ mask
5✔
1312

1313
def extract_ctn_from_tx(tx: Transaction, txin_index: int, funder_payment_basepoint: bytes,
5✔
1314
                        fundee_payment_basepoint: bytes) -> int:
1315
    tx.deserialize()
5✔
1316
    locktime = tx.locktime
5✔
1317
    sequence = tx.inputs()[txin_index].nsequence
5✔
1318
    obs = ((sequence & 0xffffff) << 24) + (locktime & 0xffffff)
5✔
1319
    return get_obscured_ctn(obs, funder_payment_basepoint, fundee_payment_basepoint)
5✔
1320

1321
def extract_ctn_from_tx_and_chan(tx: Transaction, chan: 'AbstractChannel') -> int:
5✔
1322
    funder_conf = chan.config[LOCAL] if     chan.is_initiator() else chan.config[REMOTE]
5✔
1323
    fundee_conf = chan.config[LOCAL] if not chan.is_initiator() else chan.config[REMOTE]
5✔
1324
    return extract_ctn_from_tx(tx, txin_index=0,
5✔
1325
                               funder_payment_basepoint=funder_conf.payment_basepoint.pubkey,
1326
                               fundee_payment_basepoint=fundee_conf.payment_basepoint.pubkey)
1327

1328
def ctx_has_anchors(tx: Transaction):
5✔
UNCOV
1329
    output_values = [output.value for output in tx.outputs()]
×
1330
    if FIXED_ANCHOR_SAT in output_values:
×
1331
        return True
×
1332
    else:
UNCOV
1333
        return False
×
1334

1335

1336

1337
class LnFeatureContexts(enum.Flag):
5✔
1338
    INIT = enum.auto()
5✔
1339
    NODE_ANN = enum.auto()
5✔
1340
    CHAN_ANN_AS_IS = enum.auto()
5✔
1341
    CHAN_ANN_ALWAYS_ODD = enum.auto()
5✔
1342
    CHAN_ANN_ALWAYS_EVEN = enum.auto()
5✔
1343
    INVOICE = enum.auto()
5✔
1344

1345
LNFC = LnFeatureContexts
5✔
1346

1347
_ln_feature_direct_dependencies = defaultdict(set)  # type: Dict[LnFeatures, Set[LnFeatures]]
5✔
1348
_ln_feature_contexts = {}  # type: Dict[LnFeatures, LnFeatureContexts]
5✔
1349

1350
class LnFeatures(IntFlag):
5✔
1351
    OPTION_DATA_LOSS_PROTECT_REQ = 1 << 0
5✔
1352
    OPTION_DATA_LOSS_PROTECT_OPT = 1 << 1
5✔
1353
    _ln_feature_contexts[OPTION_DATA_LOSS_PROTECT_OPT] = (LNFC.INIT | LnFeatureContexts.NODE_ANN)
5✔
1354
    _ln_feature_contexts[OPTION_DATA_LOSS_PROTECT_REQ] = (LNFC.INIT | LnFeatureContexts.NODE_ANN)
5✔
1355

1356
    INITIAL_ROUTING_SYNC = 1 << 3
5✔
1357
    _ln_feature_contexts[INITIAL_ROUTING_SYNC] = LNFC.INIT
5✔
1358

1359
    OPTION_UPFRONT_SHUTDOWN_SCRIPT_REQ = 1 << 4
5✔
1360
    OPTION_UPFRONT_SHUTDOWN_SCRIPT_OPT = 1 << 5
5✔
1361
    _ln_feature_contexts[OPTION_UPFRONT_SHUTDOWN_SCRIPT_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1362
    _ln_feature_contexts[OPTION_UPFRONT_SHUTDOWN_SCRIPT_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1363

1364
    GOSSIP_QUERIES_REQ = 1 << 6
5✔
1365
    GOSSIP_QUERIES_OPT = 1 << 7
5✔
1366
    _ln_feature_contexts[GOSSIP_QUERIES_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1367
    _ln_feature_contexts[GOSSIP_QUERIES_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1368

1369
    VAR_ONION_REQ = 1 << 8
5✔
1370
    VAR_ONION_OPT = 1 << 9
5✔
1371
    _ln_feature_contexts[VAR_ONION_OPT] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1372
    _ln_feature_contexts[VAR_ONION_REQ] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1373

1374
    GOSSIP_QUERIES_EX_REQ = 1 << 10
5✔
1375
    GOSSIP_QUERIES_EX_OPT = 1 << 11
5✔
1376
    _ln_feature_direct_dependencies[GOSSIP_QUERIES_EX_OPT] = {GOSSIP_QUERIES_OPT}
5✔
1377
    _ln_feature_contexts[GOSSIP_QUERIES_EX_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1378
    _ln_feature_contexts[GOSSIP_QUERIES_EX_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1379

1380
    OPTION_STATIC_REMOTEKEY_REQ = 1 << 12
5✔
1381
    OPTION_STATIC_REMOTEKEY_OPT = 1 << 13
5✔
1382
    _ln_feature_contexts[OPTION_STATIC_REMOTEKEY_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1383
    _ln_feature_contexts[OPTION_STATIC_REMOTEKEY_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1384

1385
    PAYMENT_SECRET_REQ = 1 << 14
5✔
1386
    PAYMENT_SECRET_OPT = 1 << 15
5✔
1387
    _ln_feature_direct_dependencies[PAYMENT_SECRET_OPT] = {VAR_ONION_OPT}
5✔
1388
    _ln_feature_contexts[PAYMENT_SECRET_OPT] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1389
    _ln_feature_contexts[PAYMENT_SECRET_REQ] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1390

1391
    BASIC_MPP_REQ = 1 << 16
5✔
1392
    BASIC_MPP_OPT = 1 << 17
5✔
1393
    _ln_feature_direct_dependencies[BASIC_MPP_OPT] = {PAYMENT_SECRET_OPT}
5✔
1394
    _ln_feature_contexts[BASIC_MPP_OPT] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1395
    _ln_feature_contexts[BASIC_MPP_REQ] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1396

1397
    OPTION_SUPPORT_LARGE_CHANNEL_REQ = 1 << 18
5✔
1398
    OPTION_SUPPORT_LARGE_CHANNEL_OPT = 1 << 19
5✔
1399
    _ln_feature_contexts[OPTION_SUPPORT_LARGE_CHANNEL_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1400
    _ln_feature_contexts[OPTION_SUPPORT_LARGE_CHANNEL_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1401

1402
    OPTION_ANCHOR_OUTPUTS_REQ = 1 << 20
5✔
1403
    OPTION_ANCHOR_OUTPUTS_OPT = 1 << 21
5✔
1404
    _ln_feature_direct_dependencies[OPTION_ANCHOR_OUTPUTS_OPT] = {OPTION_STATIC_REMOTEKEY_OPT}
5✔
1405
    _ln_feature_contexts[OPTION_ANCHOR_OUTPUTS_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1406
    _ln_feature_contexts[OPTION_ANCHOR_OUTPUTS_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1407

1408
    OPTION_ANCHORS_ZERO_FEE_HTLC_REQ = 1 << 22
5✔
1409
    OPTION_ANCHORS_ZERO_FEE_HTLC_OPT = 1 << 23
5✔
1410
    _ln_feature_direct_dependencies[OPTION_ANCHORS_ZERO_FEE_HTLC_OPT] = {OPTION_STATIC_REMOTEKEY_OPT}
5✔
1411
    _ln_feature_contexts[OPTION_ANCHORS_ZERO_FEE_HTLC_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1412
    _ln_feature_contexts[OPTION_ANCHORS_ZERO_FEE_HTLC_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1413

1414
    # Temporary number.
1415
    OPTION_TRAMPOLINE_ROUTING_REQ_ECLAIR = 1 << 148
5✔
1416
    OPTION_TRAMPOLINE_ROUTING_OPT_ECLAIR = 1 << 149
5✔
1417

1418
    _ln_feature_contexts[OPTION_TRAMPOLINE_ROUTING_REQ_ECLAIR] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1419
    _ln_feature_contexts[OPTION_TRAMPOLINE_ROUTING_OPT_ECLAIR] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1420

1421
    # We use a different bit because Phoenix cannot do end-to-end multi-trampoline routes
1422
    OPTION_TRAMPOLINE_ROUTING_REQ_ELECTRUM = 1 << 150
5✔
1423
    OPTION_TRAMPOLINE_ROUTING_OPT_ELECTRUM = 1 << 151
5✔
1424

1425
    _ln_feature_contexts[OPTION_TRAMPOLINE_ROUTING_REQ_ELECTRUM] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1426
    _ln_feature_contexts[OPTION_TRAMPOLINE_ROUTING_OPT_ELECTRUM] = (LNFC.INIT | LNFC.NODE_ANN | LNFC.INVOICE)
5✔
1427

1428
    OPTION_SHUTDOWN_ANYSEGWIT_REQ = 1 << 26
5✔
1429
    OPTION_SHUTDOWN_ANYSEGWIT_OPT = 1 << 27
5✔
1430

1431
    _ln_feature_contexts[OPTION_SHUTDOWN_ANYSEGWIT_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1432
    _ln_feature_contexts[OPTION_SHUTDOWN_ANYSEGWIT_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1433

1434
    OPTION_ONION_MESSAGE_REQ = 1 << 38
5✔
1435
    OPTION_ONION_MESSAGE_OPT = 1 << 39
5✔
1436

1437
    _ln_feature_contexts[OPTION_ONION_MESSAGE_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1438
    _ln_feature_contexts[OPTION_ONION_MESSAGE_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1439

1440
    OPTION_CHANNEL_TYPE_REQ = 1 << 44
5✔
1441
    OPTION_CHANNEL_TYPE_OPT = 1 << 45
5✔
1442

1443
    _ln_feature_contexts[OPTION_CHANNEL_TYPE_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1444
    _ln_feature_contexts[OPTION_CHANNEL_TYPE_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1445

1446
    OPTION_SCID_ALIAS_REQ = 1 << 46
5✔
1447
    OPTION_SCID_ALIAS_OPT = 1 << 47
5✔
1448

1449
    _ln_feature_contexts[OPTION_SCID_ALIAS_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1450
    _ln_feature_contexts[OPTION_SCID_ALIAS_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1451

1452
    OPTION_ZEROCONF_REQ = 1 << 50
5✔
1453
    OPTION_ZEROCONF_OPT = 1 << 51
5✔
1454

1455
    _ln_feature_direct_dependencies[OPTION_ZEROCONF_OPT] = {OPTION_SCID_ALIAS_OPT}
5✔
1456
    _ln_feature_contexts[OPTION_ZEROCONF_REQ] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1457
    _ln_feature_contexts[OPTION_ZEROCONF_OPT] = (LNFC.INIT | LNFC.NODE_ANN)
5✔
1458

1459
    def validate_transitive_dependencies(self) -> bool:
5✔
1460
        # for all even bit set, set corresponding odd bit:
1461
        features = self  # copy
5✔
1462
        flags = list_enabled_bits(features)
5✔
1463
        for flag in flags:
5✔
1464
            if flag % 2 == 0:
5✔
1465
                features |= 1 << get_ln_flag_pair_of_bit(flag)
5✔
1466
        # Check dependencies. We only check that the direct dependencies of each flag set
1467
        # are satisfied: this implies that transitive dependencies are also satisfied.
1468
        flags = list_enabled_bits(features)
5✔
1469
        for flag in flags:
5✔
1470
            for dependency in _ln_feature_direct_dependencies[1 << flag]:
5✔
1471
                if not (dependency & features):
5✔
1472
                    return False
5✔
1473
        return True
5✔
1474

1475
    def for_init_message(self) -> 'LnFeatures':
5✔
1476
        features = LnFeatures(0)
5✔
1477
        for flag in list_enabled_ln_feature_bits(self):
5✔
1478
            if LnFeatureContexts.INIT & _ln_feature_contexts[1 << flag]:
5✔
1479
                features |= (1 << flag)
5✔
1480
        return features
5✔
1481

1482
    def for_node_announcement(self) -> 'LnFeatures':
5✔
UNCOV
1483
        features = LnFeatures(0)
×
1484
        for flag in list_enabled_ln_feature_bits(self):
×
1485
            if LnFeatureContexts.NODE_ANN & _ln_feature_contexts[1 << flag]:
×
1486
                features |= (1 << flag)
×
1487
        return features
×
1488

1489
    def for_invoice(self) -> 'LnFeatures':
5✔
1490
        features = LnFeatures(0)
5✔
1491
        for flag in list_enabled_ln_feature_bits(self):
5✔
1492
            if LnFeatureContexts.INVOICE & _ln_feature_contexts[1 << flag]:
5✔
1493
                features |= (1 << flag)
5✔
1494
        return features
5✔
1495

1496
    def for_channel_announcement(self) -> 'LnFeatures':
5✔
UNCOV
1497
        features = LnFeatures(0)
×
1498
        for flag in list_enabled_ln_feature_bits(self):
×
1499
            ctxs = _ln_feature_contexts[1 << flag]
×
1500
            if LnFeatureContexts.CHAN_ANN_AS_IS & ctxs:
×
1501
                features |= (1 << flag)
×
1502
            elif LnFeatureContexts.CHAN_ANN_ALWAYS_EVEN & ctxs:
×
1503
                if flag % 2 == 0:
×
1504
                    features |= (1 << flag)
×
1505
            elif LnFeatureContexts.CHAN_ANN_ALWAYS_ODD & ctxs:
×
1506
                if flag % 2 == 0:
×
1507
                    flag = get_ln_flag_pair_of_bit(flag)
×
1508
                features |= (1 << flag)
×
1509
        return features
×
1510

1511
    def supports(self, feature: 'LnFeatures') -> bool:
5✔
1512
        """Returns whether given feature is enabled.
1513

1514
        Helper function that tries to hide the complexity of even/odd bits.
1515
        For example, instead of:
1516
          bool(myfeatures & LnFeatures.VAR_ONION_OPT or myfeatures & LnFeatures.VAR_ONION_REQ)
1517
        you can do:
1518
          myfeatures.supports(LnFeatures.VAR_ONION_OPT)
1519
        """
1520
        if (1 << (feature.bit_length() - 1)) != feature:
5✔
1521
            raise ValueError(f"'feature' cannot be a combination of features: {feature}")
5✔
1522
        if feature.bit_length() % 2 == 0:  # feature is OPT
5✔
1523
            feature_other = feature >> 1
5✔
1524
        else:  # feature is REQ
1525
            feature_other = feature << 1
5✔
1526
        return (self & feature != 0) or (self & feature_other != 0)
5✔
1527

1528
    def get_names(self) -> Sequence[str]:
5✔
1529
        r = []
5✔
1530
        for flag in list_enabled_bits(self):
5✔
1531
            feature_name = LnFeatures(1 << flag).name
5✔
1532
            r.append(feature_name or f"bit_{flag}")
5✔
1533
        return r
5✔
1534

1535
    if hasattr(IntFlag, "_numeric_repr_"):  # python 3.11+
5✔
1536
        # performance improvement (avoid base2<->base10), see #8403
1537
        _numeric_repr_ = hex
4✔
1538

1539
    def __repr__(self):
5✔
1540
        # performance improvement (avoid base2<->base10), see #8403
1541
        return f"<{self._name_}: {hex(self._value_)}>"
5✔
1542

1543
    def __str__(self):
5✔
1544
        # performance improvement (avoid base2<->base10), see #8403
1545
        return hex(self._value_)
5✔
1546

1547

1548
@stored_as('channel_type', _type=None)
5✔
1549
class ChannelType(IntFlag):
5✔
1550
    OPTION_LEGACY_CHANNEL = 0
5✔
1551
    OPTION_STATIC_REMOTEKEY = 1 << 12
5✔
1552
    OPTION_ANCHOR_OUTPUTS = 1 << 20
5✔
1553
    OPTION_ANCHORS_ZERO_FEE_HTLC_TX = 1 << 22
5✔
1554
    OPTION_SCID_ALIAS = 1 << 46
5✔
1555
    OPTION_ZEROCONF = 1 << 50
5✔
1556

1557
    def discard_unknown_and_check(self):
5✔
1558
        """Discards unknown flags and checks flag combination."""
1559
        flags = list_enabled_bits(self)
5✔
1560
        known_channel_types = []
5✔
1561
        for flag in flags:
5✔
1562
            channel_type = ChannelType(1 << flag)
5✔
1563
            if channel_type.name:
5✔
1564
                known_channel_types.append(channel_type)
5✔
1565
        final_channel_type = known_channel_types[0]
5✔
1566
        for channel_type in known_channel_types[1:]:
5✔
1567
            final_channel_type |= channel_type
5✔
1568

1569
        final_channel_type.check_combinations()
5✔
1570
        return final_channel_type
5✔
1571

1572
    def check_combinations(self):
5✔
1573
        basic_type = self & ~(ChannelType.OPTION_SCID_ALIAS | ChannelType.OPTION_ZEROCONF)
5✔
1574
        if basic_type not in [
5✔
1575
                ChannelType.OPTION_STATIC_REMOTEKEY,
1576
                ChannelType.OPTION_ANCHOR_OUTPUTS | ChannelType.OPTION_STATIC_REMOTEKEY,
1577
                ChannelType.OPTION_ANCHORS_ZERO_FEE_HTLC_TX | ChannelType.OPTION_STATIC_REMOTEKEY
1578
        ]:
UNCOV
1579
            raise ValueError("Channel type is not a valid flag combination.")
×
1580

1581
    def complies_with_features(self, features: LnFeatures) -> bool:
5✔
1582
        flags = list_enabled_bits(self)
5✔
1583
        complies = True
5✔
1584
        for flag in flags:
5✔
1585
            feature = LnFeatures(1 << flag)
5✔
1586
            complies &= features.supports(feature)
5✔
1587
        return complies
5✔
1588

1589
    def to_bytes_minimal(self):
5✔
1590
        # MUST use the smallest bitmap possible to represent the channel type.
UNCOV
1591
        bit_length =self.value.bit_length()
×
1592
        byte_length = bit_length // 8 + int(bool(bit_length % 8))
×
1593
        return self.to_bytes(byte_length, byteorder='big')
×
1594

1595
    @property
5✔
1596
    def name_minimal(self):
5✔
UNCOV
1597
        if self.name:
×
1598
            return self.name.replace('OPTION_', '')
×
1599
        else:
UNCOV
1600
            return str(self)
×
1601

1602

1603
del LNFC  # name is ambiguous without context
5✔
1604

1605
# features that are actually implemented and understood in our codebase:
1606
# (note: this is not what we send in e.g. init!)
1607
# (note: specify both OPT and REQ here)
1608
LN_FEATURES_IMPLEMENTED = (
5✔
1609
        LnFeatures(0)
1610
        | LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT | LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
1611
        | LnFeatures.GOSSIP_QUERIES_OPT | LnFeatures.GOSSIP_QUERIES_REQ
1612
        | LnFeatures.OPTION_STATIC_REMOTEKEY_OPT | LnFeatures.OPTION_STATIC_REMOTEKEY_REQ
1613
        | LnFeatures.VAR_ONION_OPT | LnFeatures.VAR_ONION_REQ
1614
        | LnFeatures.PAYMENT_SECRET_OPT | LnFeatures.PAYMENT_SECRET_REQ
1615
        | LnFeatures.BASIC_MPP_OPT | LnFeatures.BASIC_MPP_REQ
1616
        | LnFeatures.OPTION_TRAMPOLINE_ROUTING_OPT_ELECTRUM | LnFeatures.OPTION_TRAMPOLINE_ROUTING_REQ_ELECTRUM
1617
        | LnFeatures.OPTION_SHUTDOWN_ANYSEGWIT_OPT | LnFeatures.OPTION_SHUTDOWN_ANYSEGWIT_REQ
1618
        | LnFeatures.OPTION_CHANNEL_TYPE_OPT | LnFeatures.OPTION_CHANNEL_TYPE_REQ
1619
        | LnFeatures.OPTION_SCID_ALIAS_OPT | LnFeatures.OPTION_SCID_ALIAS_REQ
1620
        | LnFeatures.OPTION_ANCHORS_ZERO_FEE_HTLC_OPT | LnFeatures.OPTION_ANCHORS_ZERO_FEE_HTLC_REQ
1621
)
1622

1623

1624
def get_ln_flag_pair_of_bit(flag_bit: int) -> int:
5✔
1625
    """Ln Feature flags are assigned in pairs, one even, one odd. See BOLT-09.
1626
    Return the other flag from the pair.
1627
    e.g. 6 -> 7
1628
    e.g. 7 -> 6
1629
    """
1630
    if flag_bit % 2 == 0:
5✔
1631
        return flag_bit + 1
5✔
1632
    else:
1633
        return flag_bit - 1
5✔
1634

1635

1636
def list_enabled_ln_feature_bits(features: int) -> tuple[int, ...]:
5✔
1637
    """Returns a list of enabled feature bits. If both opt and req are set, only
1638
    req will be included in the result."""
1639
    all_enabled_bits = list_enabled_bits(features)
5✔
1640
    single_feature_bits: set[int] = set()
5✔
1641
    for bit in all_enabled_bits:
5✔
1642
        if bit % 2 == 0:  # even bit, always added
5✔
1643
            single_feature_bits.add(bit)
5✔
1644
        elif bit - 1 not in single_feature_bits:
5✔
1645
            # add if we haven't already added the corresponding req (even) bit
1646
            single_feature_bits.add(bit)
5✔
1647
    return tuple(sorted(single_feature_bits))
5✔
1648

1649

1650
class IncompatibleOrInsaneFeatures(Exception): pass
5✔
1651
class UnknownEvenFeatureBits(IncompatibleOrInsaneFeatures): pass
5✔
1652
class IncompatibleLightningFeatures(IncompatibleOrInsaneFeatures): pass
5✔
1653

1654

1655
def ln_compare_features(our_features: 'LnFeatures', their_features: int) -> 'LnFeatures':
5✔
1656
    """Returns negotiated features.
1657
    Raises IncompatibleLightningFeatures if incompatible.
1658
    """
1659
    our_flags = set(list_enabled_bits(our_features))
5✔
1660
    their_flags = set(list_enabled_bits(their_features))
5✔
1661
    # check that they have our required features, and disable the optional features they don't have
1662
    for flag in our_flags:
5✔
1663
        if flag not in their_flags and get_ln_flag_pair_of_bit(flag) not in their_flags:
5✔
1664
            # they don't have this feature we wanted :(
1665
            if flag % 2 == 0:  # even flags are compulsory
5✔
1666
                raise IncompatibleLightningFeatures(f"remote does not support {LnFeatures(1 << flag)!r}")
5✔
1667
            our_features ^= 1 << flag  # disable flag
5✔
1668
        else:
1669
            # They too have this flag.
1670
            # For easier feature-bit-testing, if this is an even flag, we also
1671
            # set the corresponding odd flag now.
1672
            if flag % 2 == 0 and our_features & (1 << flag):
5✔
1673
                our_features |= 1 << get_ln_flag_pair_of_bit(flag)
5✔
1674
    # check that we have their required features
1675
    for flag in their_flags:
5✔
1676
        if flag not in our_flags and get_ln_flag_pair_of_bit(flag) not in our_flags:
5✔
1677
            # we don't have this feature they wanted :(
1678
            if flag % 2 == 0:  # even flags are compulsory
5✔
1679
                raise IncompatibleLightningFeatures(f"remote wanted feature we don't have: {LnFeatures(1 << flag)!r}")
5✔
1680
    return our_features
5✔
1681

1682

1683
if hasattr(sys, "get_int_max_str_digits"):
5✔
1684
    # check that the user or other library has not lowered the limit (from default)
1685
    assert sys.get_int_max_str_digits() >= 4300, f"sys.get_int_max_str_digits() too low: {sys.get_int_max_str_digits()}"
5✔
1686

1687

1688
def validate_features(features: int) -> LnFeatures:
5✔
1689
    """Raises IncompatibleOrInsaneFeatures if
1690
    - a mandatory feature is listed that we don't recognize, or
1691
    - the features are inconsistent
1692
    For convenience, returns the parsed features.
1693
    """
1694
    if features.bit_length() > 10_000:
5✔
1695
        # This is an implementation-specific limit for how high feature bits we allow.
1696
        # Needed as LnFeatures subclasses IntFlag, and uses ints internally.
1697
        # See https://docs.python.org/3/library/stdtypes.html#integer-string-conversion-length-limitation
UNCOV
1698
        raise IncompatibleOrInsaneFeatures(f"features bitvector too large: {features.bit_length()=} > 10_000")
×
1699
    features = LnFeatures(features)
5✔
1700
    enabled_features = list_enabled_bits(features)
5✔
1701
    for fbit in enabled_features:
5✔
1702
        if (1 << fbit) & LN_FEATURES_IMPLEMENTED == 0 and fbit % 2 == 0:
5✔
1703
            raise UnknownEvenFeatureBits(fbit)
5✔
1704
    if not features.validate_transitive_dependencies():
5✔
1705
        raise IncompatibleOrInsaneFeatures(f"not all transitive dependencies are set. "
5✔
1706
                                           f"features={features}")
1707
    return features
5✔
1708

1709

1710
def derive_payment_secret_from_payment_preimage(payment_preimage: bytes) -> bytes:
5✔
1711
    """Returns secret to be put into invoice.
1712
    Derivation is deterministic, based on the preimage.
1713
    Crucially the payment_hash must be derived in an independent way from this.
1714
    """
1715
    # Note that this could be random data too, but then we would need to store it.
1716
    # We derive it identically to clightning, so that we cannot be distinguished:
1717
    # https://github.com/ElementsProject/lightning/blob/faac4b28adee5221e83787d64cd5d30b16b62097/lightningd/invoice.c#L115
1718
    modified = bytearray(payment_preimage)
5✔
1719
    modified[0] ^= 1
5✔
1720
    return sha256(bytes(modified))
5✔
1721

1722

1723

1724

1725
def get_compressed_pubkey_from_bech32(bech32_pubkey: str) -> bytes:
5✔
1726
    decoded_bech32 = segwit_addr.bech32_decode(bech32_pubkey)
5✔
1727
    hrp = decoded_bech32.hrp
5✔
1728
    data_5bits = decoded_bech32.data
5✔
1729
    if decoded_bech32.encoding is None:
5✔
UNCOV
1730
        raise ValueError("Bad bech32 checksum")
×
1731
    if decoded_bech32.encoding != segwit_addr.Encoding.BECH32:
5✔
UNCOV
1732
        raise ValueError("Bad bech32 encoding: must be using vanilla BECH32")
×
1733
    if hrp != 'ln':
5✔
UNCOV
1734
        raise Exception('unexpected hrp: {}'.format(hrp))
×
1735
    data_8bits = segwit_addr.convertbits(data_5bits, 5, 8, False)
5✔
1736
    # pad with zeroes
1737
    COMPRESSED_PUBKEY_LENGTH = 33
5✔
1738
    data_8bits = data_8bits + ((COMPRESSED_PUBKEY_LENGTH - len(data_8bits)) * [0])
5✔
1739
    return bytes(data_8bits)
5✔
1740

1741

1742
def make_closing_tx(local_funding_pubkey: bytes, remote_funding_pubkey: bytes,
5✔
1743
                    funding_txid: str, funding_pos: int, funding_sat: int,
1744
                    outputs: List[PartialTxOutput]) -> PartialTransaction:
1745
    c_input = make_funding_input(local_funding_pubkey, remote_funding_pubkey,
5✔
1746
        funding_pos, funding_txid, funding_sat)
1747
    c_input.nsequence = 0xFFFF_FFFF
5✔
1748
    tx = PartialTransaction.from_io([c_input], outputs, locktime=0, version=2)
5✔
1749
    return tx
5✔
1750

1751

1752

1753

1754

1755

1756
# key derivation
1757
# originally based on lnd/keychain/derivation.go
1758
# notes:
1759
# - Add a new path for each use case. Do not reuse existing paths.
1760
#   (to avoid having to carefully consider if reuse would be safe)
1761
# - Always prefer to use hardened derivation for new paths you add.
1762
#   (to avoid having to carefully consider if unhardened would be safe)
1763
class LnKeyFamily(IntEnum):
5✔
1764
    MULTISIG = 0 | BIP32_PRIME
5✔
1765
    REVOCATION_BASE = 1 | BIP32_PRIME
5✔
1766
    HTLC_BASE = 2 | BIP32_PRIME
5✔
1767
    PAYMENT_BASE = 3 | BIP32_PRIME
5✔
1768
    DELAY_BASE = 4 | BIP32_PRIME
5✔
1769
    REVOCATION_ROOT = 5 | BIP32_PRIME
5✔
1770
    NODE_KEY = 6
5✔
1771
    BACKUP_CIPHER = 7 | BIP32_PRIME
5✔
1772
    PAYMENT_SECRET_KEY = 8 | BIP32_PRIME
5✔
1773
    NOSTR_KEY = 9 | BIP32_PRIME
5✔
1774
    FUNDING_ROOT_KEY = 10 | BIP32_PRIME
5✔
1775

1776

1777
def generate_keypair(node: BIP32Node, key_family: LnKeyFamily) -> Keypair:
5✔
1778
    node2 = node.subkey_at_private_derivation([key_family, 0, 0])
5✔
1779
    k = node2.eckey.get_secret_bytes()
5✔
1780
    cK = ecc.ECPrivkey(k).get_public_key_bytes()
5✔
1781
    return Keypair(cK, k)
5✔
1782

1783
def generate_random_keypair() -> Keypair:
5✔
UNCOV
1784
    import secrets
×
1785
    k = secrets.token_bytes(32)
×
1786
    cK = ecc.ECPrivkey(k).get_public_key_bytes()
×
1787
    return Keypair(cK, k)
×
1788

1789

1790
NUM_MAX_HOPS_IN_PAYMENT_PATH = 20
5✔
1791
NUM_MAX_EDGES_IN_PAYMENT_PATH = NUM_MAX_HOPS_IN_PAYMENT_PATH
5✔
1792

1793

1794

1795

1796

1797
@attr.s(frozen=True)
5✔
1798
class UpdateAddHtlc:
5✔
1799
    amount_msat = attr.ib(type=int, kw_only=True)
5✔
1800
    payment_hash = attr.ib(type=bytes, kw_only=True, converter=hex_to_bytes, repr=lambda val: val.hex())
5✔
1801
    cltv_abs = attr.ib(type=int, kw_only=True)
5✔
1802
    timestamp = attr.ib(type=int, kw_only=True)
5✔
1803
    htlc_id = attr.ib(type=int, kw_only=True, default=None)
5✔
1804

1805
    @stored_in('adds', tuple)
5✔
1806
    def from_tuple(amount_msat, payment_hash, cltv_abs, htlc_id, timestamp) -> 'UpdateAddHtlc':
5✔
1807
        return UpdateAddHtlc(
5✔
1808
            amount_msat=amount_msat,
1809
            payment_hash=payment_hash,
1810
            cltv_abs=cltv_abs,
1811
            htlc_id=htlc_id,
1812
            timestamp=timestamp)
1813

1814
    def to_json(self):
5✔
UNCOV
1815
        return (self.amount_msat, self.payment_hash, self.cltv_abs, self.htlc_id, self.timestamp)
×
1816

1817

1818
class OnionFailureCodeMetaFlag(IntFlag):
5✔
1819
    BADONION = 0x8000
5✔
1820
    PERM     = 0x4000
5✔
1821
    NODE     = 0x2000
5✔
1822
    UPDATE   = 0x1000
5✔
1823

1824

1825
class PaymentFeeBudget(NamedTuple):
5✔
1826
    fee_msat: int
5✔
1827

1828
    # The cltv budget covers the cost of route to get to the destination, but excluding the
1829
    # cltv-delta the destination wants for itself. (e.g. "min_final_cltv_delta" is excluded)
1830
    cltv: int  # this is cltv-delta-like, no absolute heights here!
5✔
1831

1832
    #num_htlc: int
1833

1834
    @classmethod
5✔
1835
    def default(cls, *, invoice_amount_msat: int, config: 'SimpleConfig') -> 'PaymentFeeBudget':
5✔
1836
        millionths_orig = config.LIGHTNING_PAYMENT_FEE_MAX_MILLIONTHS
5✔
1837
        millionths = min(max(0, millionths_orig), 250_000)  # clamp into [0, 25%]
5✔
1838
        cutoff_orig = config.LIGHTNING_PAYMENT_FEE_CUTOFF_MSAT
5✔
1839
        cutoff = min(max(0, cutoff_orig), 10_000_000)  # clamp into [0, 10k sat]
5✔
1840
        if millionths != millionths_orig:
5✔
UNCOV
1841
            _logger.warning(
×
1842
                f"PaymentFeeBudget. found insane fee millionths in config. "
1843
                f"clamped: {millionths_orig}->{millionths}")
1844
        if cutoff != cutoff_orig:
5✔
UNCOV
1845
            _logger.warning(
×
1846
                f"PaymentFeeBudget. found insane fee cutoff in config. "
1847
                f"clamped: {cutoff_orig}->{cutoff}")
1848
        # for small payments, fees <= constant cutoff are fine
1849
        # for large payments, the max fee is percentage-based
1850
        fee_msat = invoice_amount_msat * millionths // 1_000_000
5✔
1851
        fee_msat = max(fee_msat, cutoff)
5✔
1852
        return PaymentFeeBudget(
5✔
1853
            fee_msat=fee_msat,
1854
            cltv=NBLOCK_CLTV_DELTA_TOO_FAR_INTO_FUTURE,
1855
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc