• 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

95.31
/electrum/bitcoin.py
1
# -*- coding: utf-8 -*-
2
#
3
# Electrum - lightweight Bitcoin client
4
# Copyright (C) 2011 thomasv@gitorious
5
#
6
# Permission is hereby granted, free of charge, to any person
7
# obtaining a copy of this software and associated documentation files
8
# (the "Software"), to deal in the Software without restriction,
9
# including without limitation the rights to use, copy, modify, merge,
10
# publish, distribute, sublicense, and/or sell copies of the Software,
11
# and to permit persons to whom the Software is furnished to do so,
12
# subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be
15
# included in all copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
# SOFTWARE.
25

26
from typing import Tuple, TYPE_CHECKING, Optional, Union, Sequence
5✔
27
import enum
5✔
28
from enum import IntEnum, Enum
5✔
29

30
import electrum_ecc as ecc
5✔
31

32
from .util import bfh, BitcoinException, assert_bytes, to_bytes, inv_dict, is_hex_str, classproperty
5✔
33
from . import segwit_addr
5✔
34
from . import constants
5✔
35
from .crypto import sha256d, sha256, hash_160
5✔
36

37
if TYPE_CHECKING:
5✔
UNCOV
38
    from .network import Network
×
39

40

41
################################## transactions
42

43
COINBASE_MATURITY = 100
5✔
44
COIN = 100000000
5✔
45
TOTAL_COIN_SUPPLY_LIMIT_IN_BTC = 21000000
5✔
46

47
NLOCKTIME_MIN = 0
5✔
48
NLOCKTIME_BLOCKHEIGHT_MAX = 500_000_000 - 1
5✔
49
NLOCKTIME_MAX = 2 ** 32 - 1
5✔
50

51
# supported types of transaction outputs
52
# TODO kill these with fire
53
TYPE_ADDRESS = 0
5✔
54
TYPE_PUBKEY  = 1
5✔
55
TYPE_SCRIPT  = 2
5✔
56

57

58
class opcodes(IntEnum):
5✔
59
    # push value
60
    OP_0 = 0x00
5✔
61
    OP_FALSE = OP_0
5✔
62
    OP_PUSHDATA1 = 0x4c
5✔
63
    OP_PUSHDATA2 = 0x4d
5✔
64
    OP_PUSHDATA4 = 0x4e
5✔
65
    OP_1NEGATE = 0x4f
5✔
66
    OP_RESERVED = 0x50
5✔
67
    OP_1 = 0x51
5✔
68
    OP_TRUE = OP_1
5✔
69
    OP_2 = 0x52
5✔
70
    OP_3 = 0x53
5✔
71
    OP_4 = 0x54
5✔
72
    OP_5 = 0x55
5✔
73
    OP_6 = 0x56
5✔
74
    OP_7 = 0x57
5✔
75
    OP_8 = 0x58
5✔
76
    OP_9 = 0x59
5✔
77
    OP_10 = 0x5a
5✔
78
    OP_11 = 0x5b
5✔
79
    OP_12 = 0x5c
5✔
80
    OP_13 = 0x5d
5✔
81
    OP_14 = 0x5e
5✔
82
    OP_15 = 0x5f
5✔
83
    OP_16 = 0x60
5✔
84

85
    # control
86
    OP_NOP = 0x61
5✔
87
    OP_VER = 0x62
5✔
88
    OP_IF = 0x63
5✔
89
    OP_NOTIF = 0x64
5✔
90
    OP_VERIF = 0x65
5✔
91
    OP_VERNOTIF = 0x66
5✔
92
    OP_ELSE = 0x67
5✔
93
    OP_ENDIF = 0x68
5✔
94
    OP_VERIFY = 0x69
5✔
95
    OP_RETURN = 0x6a
5✔
96

97
    # stack ops
98
    OP_TOALTSTACK = 0x6b
5✔
99
    OP_FROMALTSTACK = 0x6c
5✔
100
    OP_2DROP = 0x6d
5✔
101
    OP_2DUP = 0x6e
5✔
102
    OP_3DUP = 0x6f
5✔
103
    OP_2OVER = 0x70
5✔
104
    OP_2ROT = 0x71
5✔
105
    OP_2SWAP = 0x72
5✔
106
    OP_IFDUP = 0x73
5✔
107
    OP_DEPTH = 0x74
5✔
108
    OP_DROP = 0x75
5✔
109
    OP_DUP = 0x76
5✔
110
    OP_NIP = 0x77
5✔
111
    OP_OVER = 0x78
5✔
112
    OP_PICK = 0x79
5✔
113
    OP_ROLL = 0x7a
5✔
114
    OP_ROT = 0x7b
5✔
115
    OP_SWAP = 0x7c
5✔
116
    OP_TUCK = 0x7d
5✔
117

118
    # splice ops
119
    OP_CAT = 0x7e
5✔
120
    OP_SUBSTR = 0x7f
5✔
121
    OP_LEFT = 0x80
5✔
122
    OP_RIGHT = 0x81
5✔
123
    OP_SIZE = 0x82
5✔
124

125
    # bit logic
126
    OP_INVERT = 0x83
5✔
127
    OP_AND = 0x84
5✔
128
    OP_OR = 0x85
5✔
129
    OP_XOR = 0x86
5✔
130
    OP_EQUAL = 0x87
5✔
131
    OP_EQUALVERIFY = 0x88
5✔
132
    OP_RESERVED1 = 0x89
5✔
133
    OP_RESERVED2 = 0x8a
5✔
134

135
    # numeric
136
    OP_1ADD = 0x8b
5✔
137
    OP_1SUB = 0x8c
5✔
138
    OP_2MUL = 0x8d
5✔
139
    OP_2DIV = 0x8e
5✔
140
    OP_NEGATE = 0x8f
5✔
141
    OP_ABS = 0x90
5✔
142
    OP_NOT = 0x91
5✔
143
    OP_0NOTEQUAL = 0x92
5✔
144

145
    OP_ADD = 0x93
5✔
146
    OP_SUB = 0x94
5✔
147
    OP_MUL = 0x95
5✔
148
    OP_DIV = 0x96
5✔
149
    OP_MOD = 0x97
5✔
150
    OP_LSHIFT = 0x98
5✔
151
    OP_RSHIFT = 0x99
5✔
152

153
    OP_BOOLAND = 0x9a
5✔
154
    OP_BOOLOR = 0x9b
5✔
155
    OP_NUMEQUAL = 0x9c
5✔
156
    OP_NUMEQUALVERIFY = 0x9d
5✔
157
    OP_NUMNOTEQUAL = 0x9e
5✔
158
    OP_LESSTHAN = 0x9f
5✔
159
    OP_GREATERTHAN = 0xa0
5✔
160
    OP_LESSTHANOREQUAL = 0xa1
5✔
161
    OP_GREATERTHANOREQUAL = 0xa2
5✔
162
    OP_MIN = 0xa3
5✔
163
    OP_MAX = 0xa4
5✔
164

165
    OP_WITHIN = 0xa5
5✔
166

167
    # crypto
168
    OP_RIPEMD160 = 0xa6
5✔
169
    OP_SHA1 = 0xa7
5✔
170
    OP_SHA256 = 0xa8
5✔
171
    OP_HASH160 = 0xa9
5✔
172
    OP_HASH256 = 0xaa
5✔
173
    OP_CODESEPARATOR = 0xab
5✔
174
    OP_CHECKSIG = 0xac
5✔
175
    OP_CHECKSIGVERIFY = 0xad
5✔
176
    OP_CHECKMULTISIG = 0xae
5✔
177
    OP_CHECKMULTISIGVERIFY = 0xaf
5✔
178

179
    # expansion
180
    OP_NOP1 = 0xb0
5✔
181
    OP_CHECKLOCKTIMEVERIFY = 0xb1
5✔
182
    OP_NOP2 = OP_CHECKLOCKTIMEVERIFY
5✔
183
    OP_CHECKSEQUENCEVERIFY = 0xb2
5✔
184
    OP_NOP3 = OP_CHECKSEQUENCEVERIFY
5✔
185
    OP_NOP4 = 0xb3
5✔
186
    OP_NOP5 = 0xb4
5✔
187
    OP_NOP6 = 0xb5
5✔
188
    OP_NOP7 = 0xb6
5✔
189
    OP_NOP8 = 0xb7
5✔
190
    OP_NOP9 = 0xb8
5✔
191
    OP_NOP10 = 0xb9
5✔
192

193
    OP_INVALIDOPCODE = 0xff
5✔
194

195
    def hex(self) -> str:
5✔
UNCOV
196
        return bytes([self]).hex()
×
197

198

199
def script_num_to_bytes(i: int) -> bytes:
5✔
200
    """See CScriptNum in Bitcoin Core.
201
    Encodes an integer as bytes, to be used in script.
202

203
    ported from https://github.com/bitcoin/bitcoin/blob/8cbc5c4be4be22aca228074f087a374a7ec38be8/src/script/script.h#L326
204
    """
205
    if i == 0:
5✔
206
        return b""
5✔
207

208
    result = bytearray()
5✔
209
    neg = i < 0
5✔
210
    absvalue = abs(i)
5✔
211
    while absvalue > 0:
5✔
212
        result.append(absvalue & 0xff)
5✔
213
        absvalue >>= 8
5✔
214

215
    if result[-1] & 0x80:
5✔
216
        result.append(0x80 if neg else 0x00)
5✔
217
    elif neg:
5✔
218
        result[-1] |= 0x80
5✔
219

220
    return bytes(result)
5✔
221

222

223
def var_int(i: int) -> bytes:
5✔
224
    # https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer
225
    # https://github.com/bitcoin/bitcoin/blob/efe1ee0d8d7f82150789f1f6840f139289628a2b/src/serialize.h#L247
226
    # "CompactSize"
227
    assert i >= 0, i
5✔
228
    if i < 0xfd:
5✔
229
        return int.to_bytes(i, length=1, byteorder="little", signed=False)
5✔
230
    elif i <= 0xffff:
5✔
231
        return b"\xfd" + int.to_bytes(i, length=2, byteorder="little", signed=False)
5✔
232
    elif i <= 0xffffffff:
5✔
233
        return b"\xfe" + int.to_bytes(i, length=4, byteorder="little", signed=False)
5✔
234
    else:
235
        return b"\xff" + int.to_bytes(i, length=8, byteorder="little", signed=False)
5✔
236

237

238
def witness_push(item: bytes) -> bytes:
5✔
239
    """Returns data in the form it should be present in the witness."""
240
    return var_int(len(item)) + item
5✔
241

242

243
def _op_push(i: int) -> bytes:
5✔
244
    if i < opcodes.OP_PUSHDATA1:
5✔
245
        return int.to_bytes(i, length=1, byteorder="little", signed=False)
5✔
246
    elif i <= 0xff:
5✔
247
        return bytes([opcodes.OP_PUSHDATA1]) + int.to_bytes(i, length=1, byteorder="little", signed=False)
5✔
248
    elif i <= 0xffff:
5✔
249
        return bytes([opcodes.OP_PUSHDATA2]) + int.to_bytes(i, length=2, byteorder="little", signed=False)
5✔
250
    else:
251
        return bytes([opcodes.OP_PUSHDATA4]) + int.to_bytes(i, length=4, byteorder="little", signed=False)
5✔
252

253

254
def push_script(data: bytes) -> bytes:
5✔
255
    """Returns pushed data to the script, automatically
256
    choosing canonical opcodes depending on the length of the data.
257

258
    ported from https://github.com/btcsuite/btcd/blob/fdc2bc867bda6b351191b5872d2da8270df00d13/txscript/scriptbuilder.go#L128
259
    """
260
    data_len = len(data)
5✔
261

262
    # "small integer" opcodes
263
    if data_len == 0 or data_len == 1 and data[0] == 0:
5✔
264
        return bytes([opcodes.OP_0])
5✔
265
    elif data_len == 1 and data[0] <= 16:
5✔
266
        return bytes([opcodes.OP_1 - 1 + data[0]])
5✔
267
    elif data_len == 1 and data[0] == 0x81:
5✔
268
        return bytes([opcodes.OP_1NEGATE])
5✔
269

270
    return _op_push(data_len) + data
5✔
271

272

273
def make_op_return(x: bytes) -> bytes:
5✔
UNCOV
274
    return bytes([opcodes.OP_RETURN]) + push_script(x)
×
275

276

277
def add_number_to_script(i: int) -> bytes:
5✔
278
    return push_script(script_num_to_bytes(i))
5✔
279

280

281
def construct_witness(items: Sequence[Union[str, int, bytes]]) -> bytes:
5✔
282
    """Constructs a witness from the given stack items."""
283
    witness = bytearray()
5✔
284
    witness += var_int(len(items))
5✔
285
    for item in items:
5✔
286
        if type(item) is int:
5✔
287
            item = script_num_to_bytes(item)
5✔
288
        elif isinstance(item, (bytes, bytearray)):
5✔
289
            pass  # use as-is
5✔
290
        else:
UNCOV
291
            assert is_hex_str(item), repr(item)
×
292
            item = bfh(item)
×
293
        witness += witness_push(item)
5✔
294
    return bytes(witness)
5✔
295

296

297
def construct_script(items: Sequence[Union[str, int, bytes, opcodes]], values=None) -> bytes:
5✔
298
    """Constructs bitcoin script from given items."""
299
    script = bytearray()
5✔
300
    values = values or {}
5✔
301
    for i, item in enumerate(items):
5✔
302
        if i in values:
5✔
UNCOV
303
            item = values[i]
×
304
        if isinstance(item, opcodes):
5✔
305
            script += bytes([item])
5✔
306
        elif type(item) is int:
5✔
307
            script += add_number_to_script(item)
5✔
308
        elif isinstance(item, (bytes, bytearray)):
5✔
309
            script += push_script(item)
5✔
310
        elif isinstance(item, str):
5✔
311
            assert is_hex_str(item)
5✔
312
            script += push_script(bfh(item))
5✔
313
        else:
UNCOV
314
            raise Exception(f'unexpected item for script: {item!r}')
×
315
    return bytes(script)
5✔
316

317

318
def relayfee(network: 'Network' = None) -> int:
5✔
319
    """Returns feerate in sat/kbyte."""
320
    from .simple_config import FEERATE_DEFAULT_RELAY, FEERATE_MAX_RELAY
5✔
321
    if network and network.relay_fee is not None:
5✔
322
        fee = network.relay_fee
5✔
323
    else:
324
        fee = FEERATE_DEFAULT_RELAY
5✔
325
    # sanity safeguards, as network.relay_fee is coming from a server:
326
    fee = min(fee, FEERATE_MAX_RELAY)
5✔
327
    fee = max(fee, FEERATE_DEFAULT_RELAY)
5✔
328
    return fee
5✔
329

330

331
# see https://github.com/bitcoin/bitcoin/blob/a62f0ed64f8bbbdfe6467ac5ce92ef5b5222d1bd/src/policy/policy.cpp#L14
332
# and https://github.com/lightningnetwork/lightning-rfc/blob/7e3dce42cbe4fa4592320db6a4e06c26bb99122b/03-transactions.md#dust-limits
333
DUST_LIMIT_P2PKH = 546
5✔
334
DUST_LIMIT_P2SH = 540
5✔
335
DUST_LIMIT_UNKNOWN_SEGWIT = 354
5✔
336
DUST_LIMIT_P2WSH = 330
5✔
337
DUST_LIMIT_P2WPKH = 294
5✔
338

339

340
def dust_threshold(network: 'Network' = None) -> int:
5✔
341
    """Returns the dust limit in satoshis."""
342
    # Change <= dust threshold is added to the tx fee
343
    dust_lim = 182 * 3 * relayfee(network)  # in msat
5✔
344
    # convert to sat, but round up:
345
    return (dust_lim // 1000) + (dust_lim % 1000 > 0)
5✔
346

347

348
def hash_encode(x: bytes) -> str:
5✔
349
    return x[::-1].hex()
5✔
350

351

352
def hash_decode(x: str) -> bytes:
5✔
353
    return bfh(x)[::-1]
5✔
354

355

356
############ functions from pywallet #####################
357

358
def hash160_to_b58_address(h160: bytes, addrtype: int) -> str:
5✔
359
    s = bytes([addrtype]) + h160
5✔
360
    s = s + sha256d(s)[0:4]
5✔
361
    return base_encode(s, base=58)
5✔
362

363

364
def b58_address_to_hash160(addr: str) -> Tuple[int, bytes]:
5✔
365
    addr = to_bytes(addr, 'ascii')
5✔
366
    _bytes = DecodeBase58Check(addr)
5✔
367
    if len(_bytes) != 21:
5✔
368
        raise Exception(f'expected 21 payload bytes in base58 address. got: {len(_bytes)}')
5✔
369
    return _bytes[0], _bytes[1:21]
5✔
370

371

372
def hash160_to_p2pkh(h160: bytes, *, net=None) -> str:
5✔
373
    if net is None: net = constants.net
5✔
374
    return hash160_to_b58_address(h160, net.ADDRTYPE_P2PKH)
5✔
375

376

377
def hash160_to_p2sh(h160: bytes, *, net=None) -> str:
5✔
378
    if net is None: net = constants.net
5✔
379
    return hash160_to_b58_address(h160, net.ADDRTYPE_P2SH)
5✔
380

381

382
def public_key_to_p2pkh(public_key: bytes, *, net=None) -> str:
5✔
383
    return hash160_to_p2pkh(hash_160(public_key), net=net)
5✔
384

385

386
def hash_to_segwit_addr(h: bytes, witver: int, *, net=None) -> str:
5✔
387
    if net is None: net = constants.net
5✔
388
    addr = segwit_addr.encode_segwit_address(net.SEGWIT_HRP, witver, h)
5✔
389
    assert addr is not None
5✔
390
    return addr
5✔
391

392

393
def public_key_to_p2wpkh(public_key: bytes, *, net=None) -> str:
5✔
UNCOV
394
    return hash_to_segwit_addr(hash_160(public_key), witver=0, net=net)
×
395

396

397
def script_to_p2wsh(script: bytes, *, net=None) -> str:
5✔
398
    return hash_to_segwit_addr(sha256(script), witver=0, net=net)
5✔
399

400

401
def p2wsh_nested_script(witness_script: bytes) -> bytes:
5✔
402
    wsh = sha256(witness_script)
5✔
403
    return construct_script([0, wsh])
5✔
404

405

406
def pubkey_to_address(txin_type: str, pubkey: str, *, net=None) -> str:
5✔
407
    from . import descriptor
5✔
408
    desc = descriptor.get_singlesig_descriptor_from_legacy_leaf(pubkey=pubkey, script_type=txin_type)
5✔
409
    return desc.expand().address(net=net)
5✔
410

411

412
# TODO this method is confusingly named
413
def redeem_script_to_address(txin_type: str, scriptcode: bytes, *, net=None) -> str:
5✔
414
    assert isinstance(scriptcode, bytes)
5✔
415
    if txin_type == 'p2sh':
5✔
416
        # given scriptcode is a redeem_script
417
        return hash160_to_p2sh(hash_160(scriptcode), net=net)
5✔
418
    elif txin_type == 'p2wsh':
5✔
419
        # given scriptcode is a witness_script
420
        return script_to_p2wsh(scriptcode, net=net)
5✔
421
    elif txin_type == 'p2wsh-p2sh':
5✔
422
        # given scriptcode is a witness_script
423
        redeem_script = p2wsh_nested_script(scriptcode)
5✔
424
        return hash160_to_p2sh(hash_160(redeem_script), net=net)
5✔
425
    else:
UNCOV
426
        raise NotImplementedError(txin_type)
×
427

428

429
def script_to_address(script: bytes, *, net=None) -> Optional[str]:
5✔
430
    from .transaction import get_address_from_output_script
5✔
431
    return get_address_from_output_script(script, net=net)
5✔
432

433

434
def address_to_script(addr: str, *, net=None) -> bytes:
5✔
435
    if net is None: net = constants.net
5✔
436
    if not is_address(addr, net=net):
5✔
UNCOV
437
        raise BitcoinException(f"invalid bitcoin address: {addr}")
×
438
    witver, witprog = segwit_addr.decode_segwit_address(net.SEGWIT_HRP, addr)
5✔
439
    if witprog is not None:
5✔
440
        if not (0 <= witver <= 16):
5✔
UNCOV
441
            raise BitcoinException(f'impossible witness version: {witver}')
×
442
        return construct_script([witver, bytes(witprog)])
5✔
443
    addrtype, hash_160_ = b58_address_to_hash160(addr)
5✔
444
    if addrtype == net.ADDRTYPE_P2PKH:
5✔
445
        script = pubkeyhash_to_p2pkh_script(hash_160_)
5✔
446
    elif addrtype == net.ADDRTYPE_P2SH:
5✔
447
        script = construct_script([opcodes.OP_HASH160, hash_160_, opcodes.OP_EQUAL])
5✔
448
    else:
UNCOV
449
        raise BitcoinException(f'unknown address type: {addrtype}')
×
450
    return script
5✔
451

452

453
class OnchainOutputType(Enum):
5✔
454
    """Opaque types of scriptPubKeys.
455
    In case of p2sh, p2wsh and similar, no knowledge of redeem script, etc.
456
    """
457
    P2PKH = enum.auto()
5✔
458
    P2SH = enum.auto()
5✔
459
    WITVER0_P2WPKH = enum.auto()
5✔
460
    WITVER0_P2WSH = enum.auto()
5✔
461
    WITVER1_P2TR = enum.auto()
5✔
462

463

464
def address_to_payload(addr: str, *, net=None) -> Tuple[OnchainOutputType, bytes]:
5✔
465
    """Return (type, pubkey hash / witness program) for an address."""
466
    if net is None: net = constants.net
5✔
467
    if not is_address(addr, net=net):
5✔
UNCOV
468
        raise BitcoinException(f"invalid bitcoin address: {addr}")
×
469
    witver, witprog = segwit_addr.decode_segwit_address(net.SEGWIT_HRP, addr)
5✔
470
    if witprog is not None:
5✔
471
        if witver == 0:
5✔
472
            if len(witprog) == 20:
5✔
473
                return OnchainOutputType.WITVER0_P2WPKH, bytes(witprog)
5✔
474
            elif len(witprog) == 32:
5✔
475
                return OnchainOutputType.WITVER0_P2WSH, bytes(witprog)
5✔
476
            else:
UNCOV
477
                raise BitcoinException(f"unexpected length for segwit witver=0 witprog: len={len(witprog)}")
×
478
        elif witver == 1:
5✔
479
            if len(witprog) == 32:
5✔
480
                return OnchainOutputType.WITVER1_P2TR, bytes(witprog)
5✔
481
            else:
UNCOV
482
                raise BitcoinException(f"unexpected length for segwit witver=1 witprog: len={len(witprog)}")
×
483
        else:
UNCOV
484
            raise BitcoinException(f"not implemented handling for witver={witver}")
×
485
    addrtype, hash_160_ = b58_address_to_hash160(addr)
5✔
486
    if addrtype == net.ADDRTYPE_P2PKH:
5✔
487
        return OnchainOutputType.P2PKH, hash_160_
5✔
488
    elif addrtype == net.ADDRTYPE_P2SH:
5✔
489
        return OnchainOutputType.P2SH, hash_160_
5✔
UNCOV
490
    raise BitcoinException(f"unknown address type: {addrtype}")
×
491

492

493
def address_to_scripthash(addr: str, *, net=None) -> str:
5✔
494
    script = address_to_script(addr, net=net)
5✔
495
    return script_to_scripthash(script)
5✔
496

497

498
def script_to_scripthash(script: bytes) -> str:
5✔
499
    h = sha256(script)
5✔
500
    return h[::-1].hex()
5✔
501

502

503
def pubkeyhash_to_p2pkh_script(pubkey_hash160: bytes) -> bytes:
5✔
504
    return construct_script([
5✔
505
        opcodes.OP_DUP,
506
        opcodes.OP_HASH160,
507
        pubkey_hash160,
508
        opcodes.OP_EQUALVERIFY,
509
        opcodes.OP_CHECKSIG
510
    ])
511

512

513
__b58chars = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
5✔
514
assert len(__b58chars) == 58
5✔
515
__b58chars_inv = inv_dict(dict(enumerate(__b58chars)))
5✔
516

517
__b43chars = b'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$*+-./:'
5✔
518
assert len(__b43chars) == 43
5✔
519
__b43chars_inv = inv_dict(dict(enumerate(__b43chars)))
5✔
520

521

522
class BaseDecodeError(BitcoinException): pass
5✔
523

524

525
def base_encode(v: bytes, *, base: int) -> str:
5✔
526
    """ encode v, which is a string of bytes, to base58."""
527
    assert_bytes(v)
5✔
528
    if base not in (58, 43):
5✔
UNCOV
529
        raise ValueError('not supported base: {}'.format(base))
×
530
    chars = __b58chars
5✔
531
    if base == 43:
5✔
532
        chars = __b43chars
5✔
533

534
    origlen = len(v)
5✔
535
    v = v.lstrip(b'\x00')
5✔
536
    newlen = len(v)
5✔
537

538
    num = int.from_bytes(v, byteorder='big')
5✔
539
    string = b""
5✔
540
    while num:
5✔
541
        num, idx = divmod(num, base)
5✔
542
        string = chars[idx:idx + 1] + string
5✔
543

544
    result = chars[0:1] * (origlen - newlen) + string
5✔
545
    return result.decode('ascii')
5✔
546

547

548
def base_decode(v: Union[bytes, str], *, base: int) -> Optional[bytes]:
5✔
549
    """ decode v into a string of len bytes.
550

551
    based on the work of David Keijser in https://github.com/keis/base58
552
    """
553
    # assert_bytes(v)
554
    v = to_bytes(v, 'ascii')
5✔
555
    if base not in (58, 43):
5✔
UNCOV
556
        raise ValueError('not supported base: {}'.format(base))
×
557
    chars = __b58chars
5✔
558
    chars_inv = __b58chars_inv
5✔
559
    if base == 43:
5✔
560
        chars = __b43chars
5✔
561
        chars_inv = __b43chars_inv
5✔
562

563
    origlen = len(v)
5✔
564
    v = v.lstrip(chars[0:1])
5✔
565
    newlen = len(v)
5✔
566

567
    num = 0
5✔
568
    try:
5✔
569
        for char in v:
5✔
570
            num = num * base + chars_inv[char]
5✔
571
    except KeyError:
5✔
572
        raise BaseDecodeError('Forbidden character {} for base {}'.format(char, base))
5✔
573

574
    return num.to_bytes(origlen - newlen + (num.bit_length() + 7) // 8, 'big')
5✔
575

576

577
class InvalidChecksum(BaseDecodeError):
5✔
578
    pass
5✔
579

580

581
def EncodeBase58Check(vchIn: bytes) -> str:
5✔
582
    hash = sha256d(vchIn)
5✔
583
    return base_encode(vchIn + hash[0:4], base=58)
5✔
584

585

586
def DecodeBase58Check(psz: Union[bytes, str]) -> bytes:
5✔
587
    vchRet = base_decode(psz, base=58)
5✔
588
    payload = vchRet[0:-4]
5✔
589
    csum_found = vchRet[-4:]
5✔
590
    csum_calculated = sha256d(payload)[0:4]
5✔
591
    if csum_calculated != csum_found:
5✔
592
        raise InvalidChecksum(f'calculated {csum_calculated.hex()}, found {csum_found.hex()}')
5✔
593
    else:
594
        return payload
5✔
595

596

597
# backwards compat
598
# extended WIF for segwit (used in 3.0.x; but still used internally)
599
# the keys in this dict should be a superset of what Imported Wallets can import
600
WIF_SCRIPT_TYPES = {
5✔
601
    'p2pkh': 0,
602
    'p2wpkh': 1,
603
    'p2wpkh-p2sh': 2,
604
    'p2sh': 5,
605
    'p2wsh': 6,
606
    'p2wsh-p2sh': 7
607
}
608
WIF_SCRIPT_TYPES_INV = inv_dict(WIF_SCRIPT_TYPES)
5✔
609

610

611
def is_segwit_script_type(txin_type: str) -> bool:
5✔
612
    return txin_type in ('p2wpkh', 'p2wpkh-p2sh', 'p2wsh', 'p2wsh-p2sh')
5✔
613

614

615
def serialize_privkey(secret: bytes, compressed: bool, txin_type: str, *,
5✔
616
                      internal_use: bool = False) -> str:
617
    # we only export secrets inside curve range
618
    secret = ecc.ECPrivkey.normalize_secret_bytes(secret)
5✔
619
    if internal_use:
5✔
620
        prefix = bytes([(WIF_SCRIPT_TYPES[txin_type] + constants.net.WIF_PREFIX) & 255])
5✔
621
    else:
622
        prefix = bytes([constants.net.WIF_PREFIX])
5✔
623
    suffix = b'\01' if compressed else b''
5✔
624
    vchIn = prefix + secret + suffix
5✔
625
    base58_wif = EncodeBase58Check(vchIn)
5✔
626
    if internal_use:
5✔
627
        return base58_wif
5✔
628
    else:
629
        return '{}:{}'.format(txin_type, base58_wif)
5✔
630

631

632
def deserialize_privkey(key: str) -> Tuple[str, bytes, bool]:
5✔
633
    if is_minikey(key):
5✔
634
        return 'p2pkh', minikey_to_private_key(key), False
5✔
635

636
    txin_type = None
5✔
637
    if ':' in key:
5✔
638
        txin_type, key = key.split(sep=':', maxsplit=1)
5✔
639
        if txin_type not in WIF_SCRIPT_TYPES:
5✔
UNCOV
640
            raise BitcoinException('unknown script type: {}'.format(txin_type))
×
641
    try:
5✔
642
        vch = DecodeBase58Check(key)
5✔
643
    except Exception as e:
5✔
644
        neutered_privkey = str(key)[:3] + '..' + str(key)[-2:]
5✔
645
        raise BaseDecodeError(f"cannot deserialize privkey {neutered_privkey}") from e
5✔
646

647
    if txin_type is None:
5✔
648
        # keys exported in version 3.0.x encoded script type in first byte
649
        prefix_value = vch[0] - constants.net.WIF_PREFIX
5✔
650
        try:
5✔
651
            txin_type = WIF_SCRIPT_TYPES_INV[prefix_value]
5✔
652
        except KeyError as e:
5✔
653
            raise BitcoinException('invalid prefix ({}) for WIF key (1)'.format(vch[0])) from None
5✔
654
    else:
655
        # all other keys must have a fixed first byte
656
        if vch[0] != constants.net.WIF_PREFIX:
5✔
UNCOV
657
            raise BitcoinException('invalid prefix ({}) for WIF key (2)'.format(vch[0]))
×
658

659
    if len(vch) not in [33, 34]:
5✔
UNCOV
660
        raise BitcoinException('invalid vch len for WIF key: {}'.format(len(vch)))
×
661
    compressed = False
5✔
662
    if len(vch) == 34:
5✔
663
        if vch[33] == 0x01:
5✔
664
            compressed = True
5✔
665
        else:
666
            raise BitcoinException(f'invalid WIF key. length suggests compressed pubkey, '
5✔
667
                                   f'but last byte is {vch[33]} != 0x01')
668

669
    if is_segwit_script_type(txin_type) and not compressed:
5✔
670
        raise BitcoinException('only compressed public keys can be used in segwit scripts')
5✔
671

672
    secret_bytes = vch[1:33]
5✔
673
    # we accept secrets outside curve range; cast into range here:
674
    secret_bytes = ecc.ECPrivkey.normalize_secret_bytes(secret_bytes)
5✔
675
    return txin_type, secret_bytes, compressed
5✔
676

677

678
def is_compressed_privkey(sec: str) -> bool:
5✔
679
    return deserialize_privkey(sec)[2]
5✔
680

681

682
def address_from_private_key(sec: str) -> str:
5✔
683
    txin_type, privkey, compressed = deserialize_privkey(sec)
5✔
684
    public_key = ecc.ECPrivkey(privkey).get_public_key_hex(compressed=compressed)
5✔
685
    return pubkey_to_address(txin_type, public_key)
5✔
686

687

688
def is_segwit_address(addr: str, *, net=None) -> bool:
5✔
689
    if net is None: net = constants.net
5✔
690
    try:
5✔
691
        witver, witprog = segwit_addr.decode_segwit_address(net.SEGWIT_HRP, addr)
5✔
692
    except Exception as e:
5✔
693
        return False
5✔
694
    return witprog is not None
5✔
695

696

697
def is_taproot_address(addr: str, *, net=None) -> bool:
5✔
698
    if net is None: net = constants.net
5✔
699
    try:
5✔
700
        witver, witprog = segwit_addr.decode_segwit_address(net.SEGWIT_HRP, addr)
5✔
UNCOV
701
    except Exception as e:
×
702
        return False
×
703
    return witver == 1
5✔
704

705

706
def is_b58_address(addr: str, *, net=None) -> bool:
5✔
707
    if net is None: net = constants.net
5✔
708
    try:
5✔
709
        # test length, checksum, encoding:
710
        addrtype, h = b58_address_to_hash160(addr)
5✔
711
    except Exception as e:
5✔
712
        return False
5✔
713
    if addrtype not in [net.ADDRTYPE_P2PKH, net.ADDRTYPE_P2SH]:
5✔
UNCOV
714
        return False
×
715
    return True
5✔
716

717

718
def is_address(addr: str, *, net=None) -> bool:
5✔
719
    return is_segwit_address(addr, net=net) \
5✔
720
           or is_b58_address(addr, net=net)
721

722

723
def is_private_key(key: str, *, raise_on_error=False) -> bool:
5✔
724
    try:
5✔
725
        deserialize_privkey(key)
5✔
726
        return True
5✔
727
    except BaseException as e:
5✔
728
        if raise_on_error:
5✔
729
            raise
5✔
730
        return False
5✔
731

732

733
########### end pywallet functions #######################
734

735
def is_minikey(text: str) -> bool:
5✔
736
    # Minikeys are typically 22 or 30 characters, but this routine
737
    # permits any length of 20 or more provided the minikey is valid.
738
    # A valid minikey must begin with an 'S', be in base58, and when
739
    # suffixed with '?' have its SHA256 hash begin with a zero byte.
740
    # They are widely used in Casascius physical bitcoins.
741
    return (len(text) >= 20 and text[0] == 'S'
5✔
742
            and all(ord(c) in __b58chars for c in text)
743
            and sha256(text + '?')[0] == 0x00)
744

745

746
def minikey_to_private_key(text: str) -> bytes:
5✔
747
    return sha256(text)
5✔
748

749

750
def _get_dummy_address(purpose: str) -> str:
5✔
751
    return redeem_script_to_address('p2wsh', sha256(bytes(purpose, "utf8")))
5✔
752

753

754
_dummy_addr_funcs = set()
5✔
755

756

757
class DummyAddress:
5✔
758
    """dummy address for fee estimation of funding tx
759
    Use e.g. as: DummyAddress.CHANNEL
760
    """
761
    def purpose(func):
5✔
762
        _dummy_addr_funcs.add(func)
5✔
763
        return classproperty(func)
5✔
764

765
    @purpose
5✔
766
    def CHANNEL(self) -> str:
5✔
767
        return _get_dummy_address("channel")
5✔
768
    @purpose
5✔
769
    def SWAP(self) -> str:
5✔
770
        return _get_dummy_address("swap")
5✔
771

772
    @classmethod
5✔
773
    def is_dummy_address(cls, addr: str) -> bool:
5✔
774
        return addr in (f(cls) for f in _dummy_addr_funcs)
5✔
775

776

777
class DummyAddressUsedInTxException(Exception): pass
5✔
778

779

780
def taproot_tweak_pubkey(pubkey32: bytes, h: bytes) -> Tuple[int, bytes]:
5✔
781
    assert isinstance(pubkey32, bytes), type(pubkey32)
5✔
782
    assert isinstance(h, bytes), type(h)
5✔
783
    assert len(pubkey32) == 32, len(pubkey32)
5✔
784
    int_from_bytes = lambda x: int.from_bytes(x, byteorder="big", signed=False)
5✔
785

786
    tweak = int_from_bytes(bip340_tagged_hash(b"TapTweak", pubkey32 + h))
5✔
787
    if tweak >= ecc.CURVE_ORDER:
5✔
UNCOV
788
        raise ValueError
×
789
    P = ecc.ECPubkey(b"\x02" + pubkey32)
5✔
790
    Q = P + (ecc.GENERATOR * tweak)
5✔
791
    return 0 if Q.has_even_y() else 1, Q.get_public_key_bytes(compressed=True)[1:]
5✔
792

793

794
def taproot_tweak_seckey(seckey0: bytes, h: bytes) -> bytes:
5✔
795
    assert isinstance(seckey0, bytes), type(seckey0)
5✔
796
    assert isinstance(h, bytes), type(h)
5✔
797
    assert len(seckey0) == 32, len(seckey0)
5✔
798
    int_from_bytes = lambda x: int.from_bytes(x, byteorder="big", signed=False)
5✔
799

800
    P = ecc.ECPrivkey(seckey0)
5✔
801
    seckey = P.secret_scalar if P.has_even_y() else ecc.CURVE_ORDER - P.secret_scalar
5✔
802
    pubkey32 = P.get_public_key_bytes(compressed=True)[1:]
5✔
803
    tweak = int_from_bytes(bip340_tagged_hash(b"TapTweak", pubkey32 + h))
5✔
804
    if tweak >= ecc.CURVE_ORDER:
5✔
UNCOV
805
        raise ValueError
×
806
    return int.to_bytes((seckey + tweak) % ecc.CURVE_ORDER, length=32, byteorder="big", signed=False)
5✔
807

808

809
# a TapTree is either:
810
#  - a (leaf_version, script) tuple (leaf_version is 0xc0 for BIP-0342 scripts)
811
#  - a list of two elements, each with the same structure as TapTree itself
812
TapTreeLeaf = Tuple[int, bytes]
5✔
813
TapTree = Union[TapTreeLeaf, Sequence['TapTree']]
5✔
814

815

816
# FIXME just use electrum_ecc.util.bip340_tagged_hash instead
817
def bip340_tagged_hash(tag: bytes, msg: bytes) -> bytes:
5✔
818
    # note: _libsecp256k1.secp256k1_tagged_sha256 benchmarks about 70% slower than this (on my machine)
819
    return sha256(sha256(tag) + sha256(tag) + msg)
5✔
820

821

822
def taproot_tree_helper(script_tree: TapTree):
5✔
823
    if isinstance(script_tree, tuple):
5✔
824
        leaf_version, script = script_tree
5✔
825
        h = bip340_tagged_hash(b"TapLeaf", bytes([leaf_version]) + witness_push(script))
5✔
826
        return [((leaf_version, script), bytes())], h
5✔
827
    left, left_h = taproot_tree_helper(script_tree[0])
5✔
828
    right, right_h = taproot_tree_helper(script_tree[1])
5✔
829
    ret = [(l, c + right_h) for l, c in left] + [(l, c + left_h) for l, c in right]
5✔
830
    if right_h < left_h:
5✔
831
        left_h, right_h = right_h, left_h
5✔
832
    return ret, bip340_tagged_hash(b"TapBranch", left_h + right_h)
5✔
833

834

835
def taproot_output_script(internal_pubkey: bytes, *, script_tree: Optional[TapTree]) -> bytes:
5✔
836
    """Given an internal public key and a tree of scripts, compute the output script."""
837
    assert isinstance(internal_pubkey, bytes), type(internal_pubkey)
5✔
838
    assert len(internal_pubkey) == 32, len(internal_pubkey)
5✔
839
    if script_tree is None:
5✔
840
        merkle_root = bytes()
5✔
841
    else:
842
        _, merkle_root = taproot_tree_helper(script_tree)
5✔
843
    _, output_pubkey = taproot_tweak_pubkey(internal_pubkey, merkle_root)
5✔
844
    return construct_script([1, output_pubkey])
5✔
845

846

847
def control_block_for_taproot_script_spend(
5✔
848
    *, internal_pubkey: bytes, script_tree: TapTree, script_num: int,
849
) -> Tuple[bytes, bytes]:
850
    """Constructs the control block necessary for spending a taproot UTXO using a script.
851
    script_num indicates which script to use, which indexes into (flattened) script_tree.
852
    """
853
    assert isinstance(internal_pubkey, bytes), type(internal_pubkey)
5✔
854
    assert len(internal_pubkey) == 32, len(internal_pubkey)
5✔
855
    info, merkle_root = taproot_tree_helper(script_tree)
5✔
856
    (leaf_version, leaf_script), merkle_path = info[script_num]
5✔
857
    output_pubkey_y_parity, _ = taproot_tweak_pubkey(internal_pubkey, merkle_root)
5✔
858
    pubkey_data = bytes([output_pubkey_y_parity + leaf_version]) + internal_pubkey
5✔
859
    control_block = pubkey_data + merkle_path
5✔
860
    return (leaf_script, control_block)
5✔
861

862

863
# user message signing
864
def usermessage_magic(message: bytes) -> bytes:
5✔
865
    length = var_int(len(message))
5✔
866
    return b"\x18Bitcoin Signed Message:\n" + length + message
5✔
867

868

869
def ecdsa_sign_usermessage(ec_privkey, message: Union[bytes, str], *, is_compressed: bool) -> bytes:
5✔
870
    message = to_bytes(message, 'utf8')
5✔
871
    msg32 = sha256d(usermessage_magic(message))
5✔
872
    return ec_privkey.ecdsa_sign_recoverable(msg32, is_compressed=is_compressed)
5✔
873

874

875
def verify_usermessage_with_address(address: str, sig65: bytes, message: bytes, *, net=None) -> bool:
5✔
876
    from electrum_ecc import ECPubkey
5✔
877
    assert_bytes(sig65, message)
5✔
878
    if net is None: net = constants.net
5✔
879
    h = sha256d(usermessage_magic(message))
5✔
880
    try:
5✔
881
        public_key, compressed, txin_type_guess = ECPubkey.from_ecdsa_sig65(sig65, h)
5✔
882
    except Exception as e:
5✔
883
        return False
5✔
884
    # check public key using the address
885
    pubkey_hex = public_key.get_public_key_hex(compressed)
5✔
886
    txin_types = (txin_type_guess,) if txin_type_guess else ('p2pkh', 'p2wpkh', 'p2wpkh-p2sh')
5✔
887
    for txin_type in txin_types:
5✔
888
        addr = pubkey_to_address(txin_type, pubkey_hex, net=net)
5✔
889
        if address == addr:
5✔
890
            break
5✔
891
    else:
892
        return False
5✔
893
    # check message
894
    # note: `$ bitcoin-cli verifymessage` does NOT enforce the low-S rule for ecdsa sigs
895
    return public_key.ecdsa_verify(sig65[1:], h, enforce_low_s=False)
5✔
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