mirror of
https://github.com/yzernik/squeaknode.git
synced 2026-08-15 12:50:47 +02:00
Add buy page (#231)
* Add todo comments to handle exceptions in upload/download * Log the proof and expected proof in get offer task * Convert offer message to namedtuple * Successfully save offer in database
This commit is contained in:
parent
901b257b37
commit
be36252131
6 changed files with 71 additions and 2 deletions
15
squeakserver/core/offer.py
Normal file
15
squeakserver/core/offer.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from collections import namedtuple
|
||||
|
||||
Offer = namedtuple("Offer", [
|
||||
"squeak_hash",
|
||||
"key_cipher",
|
||||
"iv",
|
||||
"amount",
|
||||
"preimage_hash",
|
||||
"payment_request",
|
||||
"node_pubkey",
|
||||
"node_host",
|
||||
"node_port",
|
||||
"proof",
|
||||
"peer_id",
|
||||
])
|
||||
|
|
@ -70,7 +70,8 @@ class PeerClient:
|
|||
challenge=challenge,
|
||||
)
|
||||
)
|
||||
return buy_response.offer
|
||||
offer_msg = buy_response.offer
|
||||
return offer_msg
|
||||
|
||||
def _get_hash(self, squeak):
|
||||
""" Needs to be reversed because hash is stored as little-endian """
|
||||
|
|
|
|||
|
|
@ -73,6 +73,7 @@ class PeerDownload:
|
|||
logger.info("hash to download: {}".format(hash.hex()))
|
||||
|
||||
# Download squeaks for the hashes
|
||||
# TODO: catch exception downloading individual squeak
|
||||
for hash in hashes_to_download:
|
||||
if self.stopped():
|
||||
return
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import threading
|
|||
|
||||
from squeak.core.encryption import generate_data_key
|
||||
|
||||
from squeakserver.core.offer import Offer
|
||||
from squeakserver.server.util import get_hash
|
||||
from squeakserver.network.peer_client import PeerClient
|
||||
|
||||
|
|
@ -49,6 +50,8 @@ class PeerGetOffer:
|
|||
|
||||
# Check the proof
|
||||
proof = offer.proof
|
||||
logger.info("Proof: {}".format(proof.hex()))
|
||||
logger.info("Expected proof: {}".format(challenge_proof.hex()))
|
||||
if (proof != challenge_proof):
|
||||
raise Exception("Invalid offer proof: {}, expected: {}".format(
|
||||
proof.hex(),
|
||||
|
|
@ -76,7 +79,27 @@ class PeerGetOffer:
|
|||
|
||||
def _download_buy_offer(self, challenge):
|
||||
logger.info("Downloading buy offer for squeak hash: {}".format(self.squeak_hash.hex()))
|
||||
return self.peer_client.buy_squeak(self.squeak_hash, challenge)
|
||||
offer_msg = self.peer_client.buy_squeak(self.squeak_hash, challenge)
|
||||
offer = self._offer_from_msg(offer_msg)
|
||||
return offer
|
||||
|
||||
def _save_offer(self, offer):
|
||||
logger.info("Saving offer: {}".format(offer))
|
||||
self.postgres_db.insert_offer(offer)
|
||||
|
||||
def _offer_from_msg(self, offer_msg):
|
||||
if not offer_msg:
|
||||
return None
|
||||
return Offer(
|
||||
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,
|
||||
payment_request=offer_msg.payment_request,
|
||||
node_pubkey=offer_msg.pubkey,
|
||||
node_host=offer_msg.host,
|
||||
node_port=offer_msg.port,
|
||||
proof=offer_msg.proof,
|
||||
peer_id=self.peer.peer_id,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -61,6 +61,7 @@ class PeerUpload:
|
|||
logger.debug("Hashes to upload: {}".format(hashes_to_upload))
|
||||
|
||||
# Upload squeaks for the hashes
|
||||
# TODO: catch exception downloading individual squeak
|
||||
for hash in hashes_to_upload:
|
||||
if self.stopped():
|
||||
return
|
||||
|
|
|
|||
|
|
@ -474,6 +474,34 @@ class PostgresDb:
|
|||
with self.get_cursor() as curs:
|
||||
curs.execute(sql, (peer_id,))
|
||||
|
||||
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)
|
||||
RETURNING offer_id;
|
||||
"""
|
||||
with self.get_cursor() as curs:
|
||||
# execute the INSERT statement
|
||||
curs.execute(
|
||||
sql,
|
||||
(
|
||||
offer.squeak_hash.hex(),
|
||||
offer.key_cipher,
|
||||
offer.iv,
|
||||
offer.amount,
|
||||
offer.preimage_hash.hex(),
|
||||
offer.payment_request,
|
||||
offer.node_pubkey,
|
||||
offer.node_host,
|
||||
offer.node_port,
|
||||
offer.peer_id,
|
||||
),
|
||||
)
|
||||
# get the new offer id back
|
||||
row = curs.fetchone()
|
||||
return row["offer_id"]
|
||||
|
||||
def _parse_squeak_entry(self, row):
|
||||
if row is None:
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue