Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit cd643c7

Browse files
committed
Fix refunds
1 parent 2d61ca3 commit cd643c7

File tree

3 files changed

+63
-62
lines changed

3 files changed

+63
-62
lines changed

market/contracts.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,60 @@ def save(self):
967967
# save the `ListingMetadata` protobuf to the database as well
968968
self.db.listings.add_listing(data)
969969

970+
def process_refund(self, refund_json, blockchain, notification_listener):
971+
self.contract["refund"] = refund_json["refund"]
972+
order_id = refund_json["refund"]["order_id"]
973+
974+
if "txid" not in refund_json["refund"]:
975+
outpoints = json.loads(self.db.purchases.get_outpoint(order_id))
976+
refund_address = self.contract["buyer_order"]["order"]["refund_address"]
977+
redeem_script = self.contract["buyer_order"]["order"]["payment"]["redeem_script"]
978+
value = int(float(refund_json["refund"]["value"]) * 100000000)
979+
tx = BitcoinTransaction.make_unsigned(outpoints, refund_address,
980+
testnet=self.testnet,
981+
out_value=value)
982+
chaincode = self.contract["buyer_order"]["order"]["payment"]["chaincode"]
983+
masterkey_b = bitcointools.bip32_extract_key(KeyChain(self.db).bitcoin_master_privkey)
984+
buyer_priv = derive_childkey(masterkey_b, chaincode, bitcointools.MAINNET_PRIVATE)
985+
buyer_sigs = tx.create_signature(buyer_priv, redeem_script)
986+
vendor_sigs = refund_json["refund"]["signature(s)"]
987+
988+
signatures = []
989+
for i in range(len(outpoints)):
990+
for vendor_sig in vendor_sigs:
991+
if vendor_sig["index"] == i:
992+
v_signature = vendor_sig["signature"]
993+
for buyer_sig in buyer_sigs:
994+
if buyer_sig["index"] == i:
995+
b_signature = buyer_sig["signature"]
996+
signature_obj = {"index": i, "signatures": [b_signature, v_signature]}
997+
signatures.append(signature_obj)
998+
999+
tx.multisign(signatures, redeem_script)
1000+
tx.broadcast(blockchain)
1001+
self.log.info("broadcasting refund tx %s to network" % tx.get_hash())
1002+
1003+
self.db.sales.update_status(order_id, 7)
1004+
file_path = DATA_FOLDER + "purchases/trade receipts/" + order_id + ".json"
1005+
with open(file_path, 'w') as outfile:
1006+
outfile.write(json.dumps(self.contract, indent=4))
1007+
file_path = DATA_FOLDER + "purchases/in progress/" + order_id + ".json"
1008+
if os.path.exists(file_path):
1009+
os.remove(file_path)
1010+
1011+
title = self.contract["vendor_offer"]["listing"]["item"]["title"]
1012+
if "image_hashes" in self.contract["vendor_offer"]["listing"]["item"]:
1013+
image_hash = unhexlify(self.contract["vendor_offer"]["listing"]["item"]["image_hashes"][0])
1014+
else:
1015+
image_hash = ""
1016+
buyer_guid = self.contract["buyer_order"]["order"]["id"]["guid"]
1017+
if "blockchain_id" in self.contract["buyer_order"]["order"]["id"]:
1018+
handle = self.contract["buyer_order"]["order"]["id"]["blockchain_id"]
1019+
else:
1020+
handle = ""
1021+
notification_listener.notify(buyer_guid, handle, "refund", order_id, title, image_hash)
1022+
1023+
9701024
def verify(self, sender_key):
9711025
"""
9721026
Validate that an order sent over by a buyer is filled out correctly.

market/network.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,12 @@ def parse_messages(messages):
562562
self.db, self.protocol.get_message_listener(),
563563
self.protocol.get_notification_listener(),
564564
self.protocol.multiplexer.testnet)
565+
elif p.type == objects.PlaintextMessage.Type.Value("REFUND"):
566+
refund_json = json.loads(p.message, object_pairs_hook=OrderedDict)
567+
c = Contract(self.db, hash_value=unhexlify(refund_json["refund"]["order_id"]),
568+
testnet=self.protocol.multiplexer.testnet)
569+
c.process_refund(refund_json, self.protocol.multiplexer.blockchain,
570+
self.protocol.get_notification_listener())
565571
else:
566572
listener.notify(p, signature)
567573
except Exception:

market/protocol.py

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
__author__ = 'chris'
22

3-
import bitcointools
43
import json
5-
import os
6-
import pickle
74
import nacl.signing
85
import nacl.utils
96
import nacl.encoding
107
import nacl.hash
118
from binascii import unhexlify
129
from collections import OrderedDict
13-
from config import DATA_FOLDER
1410
from interfaces import MessageProcessor, BroadcastListener, MessageListener, NotificationListener
1511
from keys.bip32utils import derive_childkey
16-
from keys.keychain import KeyChain
1712
from log import Logger
1813
from market.contracts import Contract
1914
from market.moderation import process_dispute, close_dispute
2015
from market.profile import Profile
21-
from market.transactions import BitcoinTransaction
2216
from nacl.public import PublicKey, Box
2317
from net.rpcudp import RPCProtocol
2418
from protos.message import GET_CONTRACT, GET_IMAGE, GET_PROFILE, GET_LISTINGS, GET_USER_METADATA,\
@@ -358,62 +352,9 @@ def rpc_refund(self, sender, pubkey, encrypted):
358352
box = Box(self.signing_key.to_curve25519_private_key(), PublicKey(pubkey))
359353
refund = box.decrypt(encrypted)
360354
refund_json = json.loads(refund, object_pairs_hook=OrderedDict)
361-
order_id = refund_json["order_id"]
362-
363-
file_path = DATA_FOLDER + "purchases/in progress/" + order_id + ".json"
364-
with open(file_path, 'r') as filename:
365-
order = json.load(filename, object_pairs_hook=OrderedDict)
366-
order["refund"] = refund_json["refund"]
367-
368-
if "txid" not in refund_json:
369-
outpoints = pickle.loads(self.db.sales.get_outpoint(order_id))
370-
refund_address = order["buyer_order"]["order"]["refund_address"]
371-
redeem_script = order["buyer_order"]["order"]["payment"]["redeem_script"]
372-
value = int(float(refund_json["refund"]["value"]) * 100000000)
373-
tx = BitcoinTransaction.make_unsigned(outpoints, refund_address,
374-
testnet=self.multiplexer.testnet,
375-
out_value=value)
376-
chaincode = order["buyer_order"]["order"]["payment"]["chaincode"]
377-
masterkey_b = bitcointools.bip32_extract_key(KeyChain(self.db).bitcoin_master_privkey)
378-
buyer_priv = derive_childkey(masterkey_b, chaincode, bitcointools.MAINNET_PRIVATE)
379-
buyer_sigs = tx.create_signature(buyer_priv, redeem_script)
380-
vendor_sigs = refund_json["refund"]["signature(s)"]
381-
382-
signatures = []
383-
for i in range(len(outpoints)):
384-
for vendor_sig in vendor_sigs:
385-
if vendor_sig["index"] == i:
386-
v_signature = vendor_sig["signature"]
387-
for buyer_sig in buyer_sigs:
388-
if buyer_sig["index"] == i:
389-
b_signature = buyer_sig["signature"]
390-
signature_obj = {"index": i, "signatures": [b_signature, v_signature]}
391-
signatures.append(signature_obj)
392-
393-
tx.multisign(signatures, redeem_script)
394-
tx.broadcast(self.multiplexer.blockchain)
395-
self.log.info("broadcasting refund tx %s to network" % tx.get_hash())
396-
397-
self.db.sales.update_status(order_id, 7)
398-
file_path = DATA_FOLDER + "purchases/trade receipts/" + order_id + ".json"
399-
with open(file_path, 'w') as outfile:
400-
outfile.write(json.dumps(order, indent=4))
401-
file_path = DATA_FOLDER + "purchases/in progress/" + order_id + ".json"
402-
if os.path.exists(file_path):
403-
os.remove(file_path)
404-
405-
title = order["vendor_offer"]["listing"]["item"]["title"]
406-
if "image_hashes" in order["vendor_offer"]["listing"]["item"]:
407-
image_hash = unhexlify(order["vendor_offer"]["listing"]["item"]["image_hashes"][0])
408-
else:
409-
image_hash = ""
410-
buyer_guid = self.contract["buyer_order"]["order"]["id"]["guid"]
411-
if "blockchain_id" in self.contract["buyer_order"]["order"]["id"]:
412-
handle = self.contract["buyer_order"]["order"]["id"]["blockchain_id"]
413-
else:
414-
handle = ""
415-
self.get_notification_listener().notify(buyer_guid, handle, "refund", order_id, title, image_hash)
416-
355+
c = Contract(self.db, hash_value=unhexlify(refund_json["refund"]["order_id"]),
356+
testnet=self.multiplexer.testnet)
357+
c.process_refund(refund_json, self.multiplexer.blockchain, self.get_notification_listener())
417358
self.router.addContact(sender)
418359
self.log.info("order %s refunded by vendor" % refund_json["refund"]["order_id"])
419360
return ["True"]

0 commit comments

Comments
 (0)