Пример #1
0
 def get_offer_reply(self, squeak: CSqueak, peer_address: PeerAddress,
                     price_msat: int) -> Optional[OfferReply]:
     sent_offer = self.get_sent_offer_for_peer(
         squeak,
         peer_address,
         price_msat,
     )
     if sent_offer is None:
         return None
     lnd_external_address: Optional[LightningAddressHostPort] = None
     if self.config.lnd.external_host:
         lnd_external_address = LightningAddressHostPort(
             host=self.config.lnd.external_host,
             port=self.config.lnd.port,
         )
     try:
         offer = self.squeak_core.package_offer(
             sent_offer,
             lnd_external_address,
         )
         return OfferReply(
             squeak_hash=get_hash(squeak),
             offer=offer,
         )
     except Exception:
         return None
Пример #2
0
    def unpack_offer(
        self,
        squeak: CSqueak,
        offer: Offer,
        peer_address: PeerAddress,
        check_payment_point: bool = False,
    ) -> ReceivedOffer:
        """Get the offer details from the message that the buyer
        receives from the seller.

        Args:
            squeak: The squeak that will be unlocked upon payment.
            offer: The offer details received from the seller.
            peer: The peer that sent the offer.

        Returns:
            ReceivedOffer: A record of the details of the offer for the buyer.
        """
        # Get the squeak hash
        squeak_hash = get_hash(squeak)
        # Check if squeak hash matches squeak_hash in buy_offer.
        if squeak_hash != offer.squeak_hash:
            raise Exception(
                "Squeak hash in offer {!r} does not match squeak hash {!r}.".
                format(offer.squeak_hash, squeak_hash))
        # Decode the payment request
        pay_req = self.lightning_client.decode_pay_req(offer.payment_request)
        squeak_payment_point = squeak.paymentPoint
        payment_hash = pay_req.payment_hash
        price_msat = pay_req.num_msat
        destination = pay_req.destination
        invoice_timestamp = pay_req.timestamp
        invoice_expiry = pay_req.expiry
        lightning_address = LightningAddressHostPort(
            host=offer.host or peer_address.host,
            port=offer.port,
        )
        payment_point = pay_req.payment_point
        expected_payment_point = squeak.paymentPoint
        if check_payment_point:
            if payment_point != expected_payment_point:
                raise Exception("Invalid payment point.")
        return ReceivedOffer(
            received_offer_id=None,
            squeak_hash=squeak_hash,
            price_msat=price_msat,
            payment_hash=payment_hash,
            nonce=offer.nonce,
            payment_point=squeak_payment_point,
            invoice_timestamp=invoice_timestamp,
            invoice_expiry=invoice_expiry,
            payment_request=offer.payment_request,
            destination=destination,
            lightning_address=lightning_address,
            peer_address=peer_address,
            paid=False,
        )
Пример #3
0
    def get_lnd_external_address(self) -> Optional[LightningAddressHostPort]:
        """Get the external address of the local lightning node.

        Returns:
            Optional[LightningAddressHostPort]: The host and port of the lnd node if possible.
        """
        info = self.lightning_client.get_info()
        for uri in info.uris:
            pubkey, address = uri.split("@")
            host, port_str = address.split(":")
            port = int(port_str)
            return LightningAddressHostPort(
                host=host,
                port=port,
            )
        return None
Пример #4
0
 def get_packaged_offer(
         self,
         squeak_hash: bytes,
         peer_address: PeerAddress,
 ) -> Optional[Offer]:
     lnd_external_address: Optional[LightningAddressHostPort] = None
     if self.config.lightning.external_host:
         lnd_external_address = LightningAddressHostPort(
             host=self.config.lightning.external_host,
             port=self.config.lightning.external_port,
         )
     price_msat = self.get_sell_price_msat()
     if price_msat == 0:
         return None
     return self.squeak_store.get_packaged_offer(
         squeak_hash,
         peer_address,
         price_msat,
         lnd_external_address,
     )
Пример #5
0
def lightning_host_port():
    return LightningAddressHostPort(host="my_lightning_host", port=8765)
Пример #6
0
def external_lightning_address():
    return LightningAddressHostPort(host="my_external_lightning_host",
                                    port=13579)