Decode payment request on get offer (#235)

* Decode payment request after getting offer

* Decode amount and node_pubkey from payment request string

* Rename fields in offer database table

* Include invoice time and expiry time in offer table

* Fix block time display in squeak detail component
This commit is contained in:
Jonathan Zernik 2020-08-07 17:50:06 -07:00 committed by GitHub
parent a365128b40
commit 686edbcd77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 90 additions and 22 deletions

View file

@ -166,7 +166,7 @@ export default function SqueakDetailItem({
alignItems="flex-start"
>
<Grid item>
<Box color="secondary.main">
<Box color="secondary.main" fontWeight="fontWeightBold">
{new Date(squeak.getBlockTime()*1000).toString()} (Block # {squeak.getBlockHeight()})
</Box>
</Grid>

View file

@ -49,10 +49,12 @@ CREATE TABLE IF NOT EXISTS offer (
squeak_hash CHAR(64) NOT NULL,
key_cipher bytea NOT NULL,
iv bytea NOT NULL,
preimage_hash CHAR(64) NOT NULL,
amount INTEGER NOT NULL,
payment_hash CHAR(64) NOT NULL,
invoice_timestamp INTEGER NOT NULL,
invoice_expiry INTEGER NOT NULL,
price_msat INTEGER NOT NULL,
payment_request TEXT NOT NULL,
node_pubkey VARCHAR(66) NOT NULL,
destination VARCHAR(66) NOT NULL,
node_host VARCHAR(256) NOT NULL,
node_port INTEGER NOT NULL,
peer_id INTEGER NOT NULL

View file

@ -294,8 +294,8 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
return squeak_admin_pb2.OfferDisplayEntry(
offer_id=offer.offer_id,
squeak_hash=offer.squeak_hash,
amount=offer.amount,
node_pubkey=offer.node_pubkey,
amount=offer.price_msat,
node_pubkey=offer.destination,
node_host=offer.node_host,
node_port=offer.node_port,
peer=None,

View file

@ -156,3 +156,16 @@ class LNDLightningClient:
return self.stub.CloseChannel(
close_channel_request, metadata=[("macaroon", self.macaroon)]
)
def decode_pay_req(self, payment_request):
""" Decode a payment request
args:
pay_req (str) -- The payment request string
"""
decode_pay_req_request = self.ln_module.PayReqString(
pay_req=payment_request,
)
return self.stub.DecodePayReq(
decode_pay_req_request, metadata=[("macaroon", self.macaroon)]
)

View file

@ -5,10 +5,12 @@ Offer = namedtuple("Offer", [
"squeak_hash",
"key_cipher",
"iv",
"amount",
"preimage_hash",
"price_msat",
"payment_hash",
"invoice_timestamp",
"invoice_expiry",
"payment_request",
"node_pubkey",
"destination",
"node_host",
"node_port",
"proof",

View file

@ -17,11 +17,13 @@ class PeerGetOffer:
squeak_hash,
squeak_store,
postgres_db,
lightning_client
):
self.peer = peer
self.squeak_hash = squeak_hash
self.squeak_store = squeak_store
self.postgres_db = postgres_db
self.lightning_client = lightning_client
self.peer_client = PeerClient(
self.peer.host,
@ -58,8 +60,11 @@ class PeerGetOffer:
challenge_proof.hex(),
))
# Get the decoded offer from the payment request string
decoded_offer = self._get_decoded_offer(offer)
# Save the offer
self._save_offer(offer)
self._save_offer(decoded_offer)
def stop(self):
self._stop_event.set()
@ -95,12 +100,51 @@ class PeerGetOffer:
squeak_hash=offer_msg.squeak_hash,
key_cipher=offer_msg.key_cipher,
iv=offer_msg.iv,
amount=offer_msg.amount,
preimage_hash=offer_msg.preimage_hash,
price_msat=None,
payment_hash=offer_msg.preimage_hash,
invoice_timestamp=None,
invoice_expiry=None,
payment_request=offer_msg.payment_request,
node_pubkey=offer_msg.pubkey,
destination=None,
node_host=offer_msg.host,
node_port=offer_msg.port,
proof=offer_msg.proof,
peer_id=self.peer.peer_id,
)
def _decode_payment_request(self, payment_request):
return self.lightning_client.decode_pay_req(payment_request)
def _get_decoded_offer(self, offer):
pay_req = self._decode_payment_request(offer.payment_request)
logger.info("Decoded payment request: {}".format(pay_req))
# TODO create a new offer object with decoded fields
price_msat = pay_req.num_msat
destination = pay_req.destination
invoice_timestamp = pay_req.timestamp
invoice_expiry = pay_req.expiry
logger.info("price_msat: {}".format(price_msat))
logger.info("destination: {}".format(destination))
logger.info("invoice_timestamp: {}".format(invoice_timestamp))
logger.info("invoice_expiry: {}".format(invoice_expiry))
decoded_offer = Offer(
offer_id=offer.offer_id,
squeak_hash=offer.squeak_hash,
key_cipher=offer.key_cipher,
iv=offer.iv,
price_msat=price_msat,
payment_hash=offer.payment_hash,
invoice_timestamp=invoice_timestamp,
invoice_expiry=invoice_expiry,
payment_request=offer.payment_request,
destination=destination,
node_host=offer.node_host,
node_port=offer.node_port,
proof=offer.proof,
peer_id=offer.peer_id,
)
return decoded_offer

View file

@ -32,10 +32,11 @@ class SqueakGetOfferStatus:
class SqueakGetOfferController:
def __init__(self, squeak_store, postgres_db):
def __init__(self, squeak_store, postgres_db, lightning_client):
self.squeak_get_offer_status = SqueakGetOfferStatus()
self.squeak_store = squeak_store
self.postgres_db = postgres_db
self.lightning_client = lightning_client
def get_offers(self, peers, squeak_hash):
self._download_from_peers(peers, squeak_hash)
@ -55,6 +56,7 @@ class SqueakGetOfferController:
squeak_hash,
self.squeak_store,
self.postgres_db,
self.lightning_client,
)
try:
logger.debug("Trying to get offer from peer: {} for squeak: {}".format(peer.peer_id, squeak_hash))

View file

@ -68,6 +68,7 @@ class SqueakNode:
self.squeak_get_offer_controller = SqueakGetOfferController(
self.squeak_store,
self.postgres_db,
self.lightning_client,
)
def start_running(self):

View file

@ -478,8 +478,8 @@ class PostgresDb:
def insert_offer(self, offer):
""" Insert a new offer. """
sql = """
INSERT INTO offer(squeak_hash, key_cipher, iv, amount, preimage_hash, payment_request, node_pubkey, node_host, node_port, peer_id)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
INSERT INTO offer(squeak_hash, key_cipher, iv, price_msat, payment_hash, invoice_timestamp, invoice_expiry, payment_request, destination, node_host, node_port, peer_id)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING offer_id;
"""
with self.get_cursor() as curs:
@ -490,10 +490,12 @@ class PostgresDb:
offer.squeak_hash.hex(),
offer.key_cipher,
offer.iv,
offer.amount,
offer.preimage_hash.hex(),
offer.price_msat,
offer.payment_hash.hex(),
offer.invoice_timestamp,
offer.invoice_expiry,
offer.payment_request,
offer.node_pubkey,
offer.destination,
offer.node_host,
offer.node_port,
offer.peer_id,
@ -591,10 +593,12 @@ class PostgresDb:
squeak_hash=row["squeak_hash"],
key_cipher=row["key_cipher"],
iv=row["iv"],
amount=row["amount"],
preimage_hash=row["preimage_hash"],
price_msat=row["price_msat"],
payment_hash=row["payment_hash"],
invoice_timestamp=row["invoice_timestamp"],
invoice_expiry=row["invoice_expiry"],
payment_request=row["payment_request"],
node_pubkey=row["node_pubkey"],
destination=row["destination"],
node_host=row["node_host"],
node_port=row["node_port"],
proof=None,