def test_roundtrip(self): longdescription = ( 'One piece of chocolate cake, one icecream cone, one' ' pickle, one slice of swiss cheese, one slice of salami,' ' one lollypop, one piece of cherry pie, one sausage, one' ' cupcake, and one slice of watermelon') tests = [ LnAddr(RHASH, tags=[('d', '')]), LnAddr(RHASH, amount=Decimal('0.001'), tags=[('d', '1 cup coffee'), ('x', 60)]), LnAddr(RHASH, amount=Decimal('1'), tags=[('h', longdescription)]), LnAddr(RHASH, currency='tb', tags=[('f', 'mk2QpYatsKicvFVuTAQLBryyccRXMUaGHP'), ('h', longdescription)]), LnAddr( RHASH, amount=24, tags= [('r', [(unhexlify( '029e03a901b85534ff1e92c43c74431f7ce72046060fcf7a95c37e148f78c77255' ), unhexlify('0102030405060708'), 1, 20, 3), (unhexlify( '039e03a901b85534ff1e92c43c74431f7ce72046060fcf7a95c37e148f78c77255' ), unhexlify('030405060708090a'), 2, 30, 4)]), ('f', '1RustyRX2oai4EYYDpQGWvEL62BBGqN9T'), ('h', longdescription)]), LnAddr(RHASH, amount=24, tags=[('f', '3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQX'), ('h', longdescription)]), LnAddr(RHASH, amount=24, tags=[('f', 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'), ('h', longdescription)]), LnAddr( RHASH, amount=24, tags= [('f', 'bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3' ), ('h', longdescription)]), LnAddr(RHASH, amount=24, tags=[('n', PUBKEY), ('h', longdescription)]), ] # Roundtrip for t in tests: o = lndecode(lnencode(t, PRIVKEY), expected_hrp=t.currency) self.compare(t, o)
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)), verbose=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)), verbose=True) assert lnaddr.pubkey.serialize() == PUBKEY
def set_ln_invoice(self, invoice): try: lnaddr = lndecode(invoice, expected_hrp=constants.net.SEGWIT_HRP) except Exception as e: self.app.show_info(invoice + _(" is not a valid Lightning invoice: ") + repr(e)) # repr because str(Exception()) == '' return self.screen.address = invoice self.screen.message = dict(lnaddr.tags).get('d', None) self.screen.amount = self.app.format_amount_and_units( lnaddr.amount * bitcoin.COIN) if lnaddr.amount else '' self.payment_request = None self.screen.is_lightning = True
def make_model(self, htlcs) -> QtGui.QStandardItemModel: model = QtGui.QStandardItemModel(0, 2) model.setHorizontalHeaderLabels(['HTLC', 'Property value']) parentItem = model.invisibleRootItem() folder_types = {'settled': _('Fulfilled HTLCs'), 'inflight': _('HTLCs in current commitment transaction')} self.folders = {} self.keyname_rows = {} for keyname, i in folder_types.items(): myFont=QtGui.QFont() myFont.setBold(True) folder = HTLCItem(i) folder.setFont(myFont) parentItem.appendRow(folder) self.folders[keyname] = folder mapping = {} num = 0 invoices = dict(self.window.wallet.lnworker.invoices) for pay_hash, item in htlcs.items(): chan_id, i, direction, status = item lnaddr = None if pay_hash in invoices: invoice = invoices[pay_hash][0] lnaddr = lndecode(invoice) if status == 'inflight': if lnaddr is not None: it = self.make_inflight(lnaddr, i, direction) else: it = self.make_htlc_item(i, direction) elif status == 'settled': it = self.make_htlc_item(i, direction) # if we made the invoice and still have it, we can show more info if lnaddr is not None: self.append_lnaddr(it, lnaddr) self.folders[status].appendRow(it) mapping[i.payment_hash] = num num += 1 self.keyname_rows[keyname] = mapping return model
def test_min_final_cltv_expiry_roundtrip(self): lnaddr = LnAddr(RHASH, amount=Decimal('0.001'), tags=[('d', '1 cup coffee'), ('x', 60), ('c', 150)]) invoice = lnencode(lnaddr, PRIVKEY) self.assertEqual(150, lndecode(invoice).get_min_final_cltv_expiry())
def test_min_final_cltv_expiry_decoding(self): self.assertEqual( 144, lndecode( "lnsb500u1pdsgyf3pp5nmrqejdsdgs4n9ukgxcp2kcq265yhrxd4k5dyue58rxtp5y83s3qdqqcqzystrggccm9yvkr5yqx83jxll0qjpmgfg9ywmcd8g33msfgmqgyfyvqhku80qmqm8q6v35zvck2y5ccxsz5avtrauz8hgjj3uahppyq20qp6dvwxe", expected_hrp="sb").get_min_final_cltv_expiry())