Skip to content

Commit e0de790

Browse files
committed
commands: add decode_bolt12 command
1 parent 0fbf865 commit e0de790

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

electrum/commands.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
from .lnworker import LN_P2P_NETWORK_TIMEOUT
4949
from .logging import Logger
5050
from .onion_message import create_blinded_path, send_onion_message_to
51-
from .segwit_addr import bech32_encode, Encoding, convertbits
51+
from .segwit_addr import bech32_encode, Encoding, convertbits, INVALID_BECH32
5252
from .submarine_swaps import NostrTransport
5353
from .util import (
5454
bfh, json_decode, json_normalize, is_hash256_str, is_hex_str, to_bytes, parse_max_spend, to_decimal,
55-
UserFacingException, InvalidPassword
55+
UserFacingException, InvalidPassword, json_encode
5656
)
5757
from . import bitcoin
5858
from .bitcoin import is_address, hash_160, COIN
@@ -2275,6 +2275,18 @@ async def get_blinded_path_via(self, node_id: str, dummy_hops: int = 0, wallet:
22752275

22762276
return encoded_blinded_path.hex()
22772277

2278+
@command('')
2279+
async def decode_bolt12(self, bech32: str):
2280+
dec = bolt12.bech32_decode(bech32, ignore_long_length=True, with_checksum=False)
2281+
if dec == INVALID_BECH32:
2282+
raise Exception('invalid bech32')
2283+
d = {
2284+
'lni': bolt12.decode_invoice,
2285+
'lno': bolt12.decode_offer,
2286+
'lnr': bolt12.decode_invoice_request,
2287+
}[dec.hrp](bech32)
2288+
return json_encode(d)
2289+
22782290

22792291
def plugin_command(s, plugin_name):
22802292
"""Decorator to register a cli command inside a plugin. To be used within a commands.py file

0 commit comments

Comments
 (0)