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
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)
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)
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