Exemple #1
0
    def test_n_decoding(self):
        # We flip the signature recovery bit, which would normally give a different
        # pubkey.
        _, hrp, data = bech32_decode(lnencode(
            LnAddr(paymenthash=RHASH, amount=24, tags=[('d', '')]), PRIVKEY),
                                     ignore_long_length=True)
        databits = u5_to_bitarray(data)
        databits.invert(-1)
        lnaddr = lndecode(bech32_encode(segwit_addr.Encoding.BECH32, hrp,
                                        bitarray_to_u5(databits)),
                          verbose=True)
        assert lnaddr.pubkey.serialize() != PUBKEY

        # But not if we supply expliciy `n` specifier!
        _, hrp, data = bech32_decode(lnencode(
            LnAddr(paymenthash=RHASH,
                   amount=24,
                   tags=[('d', ''), ('n', PUBKEY)]), PRIVKEY),
                                     ignore_long_length=True)
        databits = u5_to_bitarray(data)
        databits.invert(-1)
        lnaddr = lndecode(bech32_encode(segwit_addr.Encoding.BECH32, hrp,
                                        bitarray_to_u5(databits)),
                          verbose=True)
        assert lnaddr.pubkey.serialize() == PUBKEY
Exemple #2
0
def convert_bech32(inp: str, new_hrp: str) -> str:
    """Converts a bech32 input to another HRP"""

    _, data = segwit_addr.bech32_decode(inp)
    if data is None:
        raise AssertionError(f"Invalid bech32 for conversion: {inp}")

    return segwit_addr.bech32_encode(new_hrp, data)
Exemple #3
0
def convert_ln_bech32(inp: str, new_base_hrp: str) -> str:
    """Converts a Lightning address in bech32 format to another base HRP"""

    old_hrp, data = segwit_addr.bech32_decode(inp, ignore_long_length=True)
    if data is None:
        raise AssertionError(f"Invalid bech32 for conversion: {inp}")

    new_hrp = "ln" + new_base_hrp + old_hrp[4:]
    return segwit_addr.bech32_encode(new_hrp, data)
Exemple #4
0
    def test_n_decoding(self):
        # We flip the signature recovery bit, which would normally give a different
        # pubkey.
        hrp, data = bech32_decode(lnencode(LnAddr(RHASH, amount=24, tags=[('d', '')]), PRIVKEY), True)
        databits = u5_to_bitarray(data)
        databits.invert(-1)
        lnaddr = lndecode(bech32_encode(hrp, bitarray_to_u5(databits)), True)
        assert lnaddr.pubkey.serialize() != PUBKEY

        # But not if we supply expliciy `n` specifier!
        hrp, data = bech32_decode(lnencode(LnAddr(RHASH, amount=24,
                                                  tags=[('d', ''),
                                                        ('n', PUBKEY)]),
                                           PRIVKEY), True)
        databits = u5_to_bitarray(data)
        databits.invert(-1)
        lnaddr = lndecode(bech32_encode(hrp, bitarray_to_u5(databits)), True)
        assert lnaddr.pubkey.serialize() == PUBKEY
Exemple #5
0
def decode_lnurl(lnurl: str) -> str:
    """Converts bech32 encoded lnurl to url."""
    decoded_bech32 = bech32_decode(lnurl, ignore_long_length=True)
    hrp = decoded_bech32.hrp
    data = decoded_bech32.data
    if decoded_bech32.encoding is None:
        raise LnDecodeException("Bad bech32 checksum")
    if decoded_bech32.encoding != Encoding.BECH32:
        raise LnDecodeException(
            "Bad bech32 encoding: must be using vanilla BECH32")
    if not hrp.startswith("lnurl"):
        raise LnDecodeException("Does not start with lnurl")
    data = convertbits(data, 5, 8, False)
    url = bytes(data).decode("utf-8")
    return url