|
1 | 1 | __author__ = 'chris' |
2 | 2 |
|
3 | | -import bitcointools |
4 | 3 | import json |
5 | | -import os |
6 | | -import pickle |
7 | 4 | import nacl.signing |
8 | 5 | import nacl.utils |
9 | 6 | import nacl.encoding |
10 | 7 | import nacl.hash |
11 | 8 | from binascii import unhexlify |
12 | 9 | from collections import OrderedDict |
13 | | -from config import DATA_FOLDER |
14 | 10 | from interfaces import MessageProcessor, BroadcastListener, MessageListener, NotificationListener |
15 | 11 | from keys.bip32utils import derive_childkey |
16 | | -from keys.keychain import KeyChain |
17 | 12 | from log import Logger |
18 | 13 | from market.contracts import Contract |
19 | 14 | from market.moderation import process_dispute, close_dispute |
20 | 15 | from market.profile import Profile |
21 | | -from market.transactions import BitcoinTransaction |
22 | 16 | from nacl.public import PublicKey, Box |
23 | 17 | from net.rpcudp import RPCProtocol |
24 | 18 | 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): |
358 | 352 | box = Box(self.signing_key.to_curve25519_private_key(), PublicKey(pubkey)) |
359 | 353 | refund = box.decrypt(encrypted) |
360 | 354 | 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()) |
417 | 358 | self.router.addContact(sender) |
418 | 359 | self.log.info("order %s refunded by vendor" % refund_json["refund"]["order_id"]) |
419 | 360 | return ["True"] |
|
0 commit comments