示例#1
0
def message_to_sent_payment(msg: squeak_admin_pb2.SentPayment) -> SentPayment:
    return SentPayment(
        sent_payment_id=(msg.sent_payment_id
                         if msg.sent_payment_id > 0 else None),
        created_time_ms=(msg.time_ms if msg.time_ms > 0 else None),
        peer_address=message_to_peer_address(msg.peer_address),
        squeak_hash=bytes.fromhex(msg.squeak_hash),
        payment_hash=bytes.fromhex(msg.payment_hash),
        secret_key=b'',  # TODO: why does this field exist?
        price_msat=msg.price_msat,
        node_pubkey=msg.node_pubkey,
        valid=msg.valid,
    )
示例#2
0
def gen_sent_payment(peer_address, squeak_hash, secret_key, price_msat, seller_pubkey):
    payment_hash = gen_random_hash()
    return SentPayment(
        sent_payment_id=None,
        created_time_ms=None,
        peer_address=peer_address,
        squeak_hash=squeak_hash,
        payment_hash=payment_hash,
        secret_key=secret_key,
        price_msat=price_msat,
        node_pubkey=seller_pubkey,
        valid=True,
    )
示例#3
0
def sent_payment(
    squeak_hash,
    payment_hash,
    secret_key,
    price_msat,
    seller_pubkey,
    peer_address,
):
    yield SentPayment(
        sent_payment_id=None,
        created_time_ms=None,
        peer_address=peer_address,
        squeak_hash=squeak_hash,
        payment_hash=payment_hash,
        secret_key=secret_key,
        price_msat=price_msat,
        node_pubkey=seller_pubkey,
        valid=True,
    )
示例#4
0
    def pay_offer(self, received_offer: ReceivedOffer) -> SentPayment:
        """Pay the offer that the buyer received from the seller.

        Args:
            received_offer: The details of the offer received by the buyer.

        Returns:
            SentPayment: A record of the sent payment.
        """
        # Pay the invoice
        payment = self.lightning_client.pay_invoice(
            received_offer.payment_request)
        preimage = payment.payment_preimage
        if not preimage:
            raise Exception("Payment failed with error: {}".format(
                payment.payment_error))
        # Calculate the secret key
        nonce = received_offer.nonce
        secret_key = subtract_tweak(preimage, nonce)
        # Check if the secret key is valid for the preimage
        point = get_payment_point_of_secret_key(secret_key)
        valid = point == received_offer.payment_point
        # Save the preimage of the sent payment
        # peer_address = PeerAddress(
        #     network=received_offer.peer_address.network,
        #     host=received_offer.peer_address.host,
        #     port=received_offer.peer_address.port,
        # )
        return SentPayment(
            sent_payment_id=None,
            created_time_ms=None,
            peer_address=received_offer.peer_address,
            squeak_hash=received_offer.squeak_hash,
            payment_hash=received_offer.payment_hash,
            secret_key=secret_key,
            price_msat=received_offer.price_msat,
            node_pubkey=received_offer.destination,
            valid=valid,
        )