1111import nacl .utils
1212import obelisk
1313import os .path
14- import pickle
1514import time
1615from binascii import unhexlify
1716from collections import OrderedDict
@@ -197,6 +196,7 @@ def get_result(result):
197196 self .get_image (node_to_ask , p .avatar_hash )
198197 if not os .path .isfile (DATA_FOLDER + 'cache/' + p .header_hash .encode ("hex" )):
199198 self .get_image (node_to_ask , p .header_hash )
199+ self .cache (result [1 ][0 ], node_to_ask .id .encode ("hex" ))
200200 return p
201201 except Exception :
202202 return None
@@ -563,6 +563,12 @@ def parse_messages(messages):
563563 self .db , self .protocol .get_message_listener (),
564564 self .protocol .get_notification_listener (),
565565 self .protocol .multiplexer .testnet )
566+ elif p .type == objects .PlaintextMessage .Type .Value ("REFUND" ):
567+ refund_json = json .loads (p .message , object_pairs_hook = OrderedDict )
568+ c = Contract (self .db , hash_value = unhexlify (refund_json ["refund" ]["order_id" ]),
569+ testnet = self .protocol .multiplexer .testnet )
570+ c .process_refund (refund_json , self .protocol .multiplexer .blockchain ,
571+ self .protocol .get_notification_listener ())
566572 else :
567573 listener .notify (p , signature )
568574 except Exception :
@@ -833,16 +839,20 @@ def history_fetched(ec, history):
833839
834840 outputs .append ({'value' : moderator_fee , 'address' : moderator_address })
835841 dispute_json ["dispute_resolution" ]["resolution" ]["moderator_address" ] = moderator_address
836- dispute_json ["dispute_resolution" ]["resolution" ]["moderator_fee" ] = moderator_fee
837- dispute_json ["dispute_resolution" ]["resolution" ]["transaction_fee" ] = TRANSACTION_FEE
842+ dispute_json ["dispute_resolution" ]["resolution" ]["moderator_fee" ] = \
843+ round (moderator_fee / float (100000000 ), 8 )
844+ dispute_json ["dispute_resolution" ]["resolution" ]["transaction_fee" ] = \
845+ round (TRANSACTION_FEE / float (100000000 ), 8 )
838846 if float (buyer_percentage ) > 0 :
839847 amt = int (float (buyer_percentage ) * satoshis )
840- dispute_json ["dispute_resolution" ]["resolution" ]["buyer_payout" ] = amt
848+ dispute_json ["dispute_resolution" ]["resolution" ]["buyer_payout" ] = \
849+ round (amt / float (100000000 ), 8 )
841850 outputs .append ({'value' : amt ,
842851 'address' : buyer_address })
843852 if float (vendor_percentage ) > 0 :
844853 amt = int (float (vendor_percentage ) * satoshis )
845- dispute_json ["dispute_resolution" ]["resolution" ]["vendor_payout" ] = amt
854+ dispute_json ["dispute_resolution" ]["resolution" ]["vendor_payout" ] = \
855+ round (amt / float (100000000 ), 8 )
846856 outputs .append ({'value' : amt ,
847857 'address' : vendor_address })
848858
@@ -902,32 +912,34 @@ def release_funds(self, order_id):
902912 the moderator has resolved the dispute and provided his signature.
903913 """
904914 if os .path .exists (DATA_FOLDER + "purchases/in progress/" + order_id + ".json" ):
905- file_path = DATA_FOLDER + "purchases/trade receipts /" + order_id + ".json"
906- outpoints = pickle .loads (self .db .purchases .get_outpoint (order_id ))
915+ file_path = DATA_FOLDER + "purchases/in progress /" + order_id + ".json"
916+ outpoints = json .loads (self .db .purchases .get_outpoint (order_id ))
907917 elif os .path .exists (DATA_FOLDER + "store/contracts/in progress/" + order_id + ".json" ):
908918 file_path = DATA_FOLDER + "store/contracts/in progress/" + order_id + ".json"
909- outpoints = pickle .loads (self .db .sales .get_outpoint (order_id ))
919+ outpoints = json .loads (self .db .sales .get_outpoint (order_id ))
910920
911921 with open (file_path , 'r' ) as filename :
912922 contract = json .load (filename , object_pairs_hook = OrderedDict )
913923
914- vendor_address = contract ["vendor_order_confirmation" ]["invoice" ]["payout" ]["address" ]
915- buyer_address = contract ["buyer_order" ]["order" ]["refund_address" ]
916-
917924 outputs = []
918925
919- outputs .append ({'value' : int (contract ["dispute_resolution" ]["resolution" ]["moderator_fee" ]),
926+ outputs .append ({'value' : int (float (contract ["dispute_resolution" ]
927+ ["resolution" ]["moderator_fee" ]) * 100000000 ),
920928 'address' : contract ["dispute_resolution" ]["resolution" ]["moderator_address" ]})
921929
922930 if "buyer_payout" in contract ["dispute_resolution" ]["resolution" ]:
923- outputs .append ({'value' : int (contract ["dispute_resolution" ]["resolution" ]["buyer_payout" ]),
931+ buyer_address = contract ["buyer_order" ]["order" ]["refund_address" ]
932+ outputs .append ({'value' : int (float (contract ["dispute_resolution" ]
933+ ["resolution" ]["buyer_payout" ]) * 100000000 ),
924934 'address' : buyer_address })
925935
926936 if "vendor_payout" in contract ["dispute_resolution" ]["resolution" ]:
927- outputs .append ({'value' : int (contract ["dispute_resolution" ]["resolution" ]["vendor_payout" ]),
937+ vendor_address = contract ["vendor_order_confirmation" ]["invoice" ]["payout" ]["address" ]
938+ outputs .append ({'value' : int (float (contract ["dispute_resolution" ]
939+ ["resolution" ]["vendor_payout" ]) * 100000000 ),
928940 'address' : vendor_address })
929941
930- tx = BitcoinTransaction .make_unsigned (outpoints , outputs )
942+ tx = BitcoinTransaction .make_unsigned (outpoints , outputs , testnet = self . protocol . multiplexer . testnet )
931943 chaincode = contract ["buyer_order" ]["order" ]["payment" ]["chaincode" ]
932944 redeem_script = str (contract ["buyer_order" ]["order" ]["payment" ]["redeem_script" ])
933945 masterkey = bitcointools .bip32_extract_key (KeyChain (self .db ).bitcoin_master_privkey )
@@ -948,7 +960,7 @@ def release_funds(self, order_id):
948960
949961 tx .multisign (signatures , redeem_script )
950962 tx .broadcast (self .protocol .multiplexer .blockchain )
951- self .log .info ("Broadcasting payout tx %s to network" % tx .get_hash ())
963+ self .log .info ("broadcasting payout tx %s to network" % tx .get_hash ())
952964
953965 if self .db .purchases .get_purchase (order_id ) is not None :
954966 self .db .purchases .update_status (order_id , 6 )
@@ -986,8 +998,6 @@ def get_result(result):
986998 pass
987999 return ret
9881000 except Exception :
989- import traceback
990- traceback .print_exc ()
9911001 return None
9921002
9931003 if node_to_ask .ip is None :
@@ -1004,7 +1014,7 @@ def refund(self, order_id):
10041014 to the buyer with contain the signature.
10051015 """
10061016 file_path = DATA_FOLDER + "store/contracts/in progress/" + order_id + ".json"
1007- outpoints = pickle .loads (self .db .sales .get_outpoint (order_id ))
1017+ outpoints = json .loads (self .db .sales .get_outpoint (order_id ))
10081018
10091019 with open (file_path , 'r' ) as filename :
10101020 contract = json .load (filename , object_pairs_hook = OrderedDict )
@@ -1014,7 +1024,6 @@ def refund(self, order_id):
10141024 contract ["buyer_order" ]["order" ]["id" ]["pubkeys" ]["guid" ],
10151025 encoder = nacl .encoding .HexEncoder ).to_curve25519_public_key ()
10161026 refund_address = contract ["buyer_order" ]["order" ]["refund_address" ]
1017- redeem_script = contract ["buyer_order" ]["order" ]["payment" ]["redeem_script" ]
10181027 tx = BitcoinTransaction .make_unsigned (outpoints , refund_address ,
10191028 testnet = self .protocol .multiplexer .testnet )
10201029 chaincode = contract ["buyer_order" ]["order" ]["payment" ]["chaincode" ]
@@ -1024,14 +1033,25 @@ def refund(self, order_id):
10241033 refund_json = {"refund" : {}}
10251034 refund_json ["refund" ]["order_id" ] = order_id
10261035 if "moderator" in contract ["buyer_order" ]["order" ]:
1036+ redeem_script = contract ["buyer_order" ]["order" ]["payment" ]["redeem_script" ]
10271037 sigs = tx .create_signature (vendor_priv , redeem_script )
1028- refund_json ["refund" ]["value" ] = tx .get_out_value ()
1038+ refund_json ["refund" ]["value" ] = round ( tx .get_out_value () / float ( 100000000 ), 8 )
10291039 refund_json ["refund" ]["signature(s)" ] = sigs
10301040 else :
10311041 tx .sign (vendor_priv )
10321042 tx .broadcast (self .protocol .multiplexer .blockchain )
1043+ self .log .info ("broadcasting refund tx %s to network" % tx .get_hash ())
10331044 refund_json ["refund" ]["txid" ] = tx .get_hash ()
10341045
1046+ contract ["refund" ] = refund_json ["refund" ]
1047+ self .db .sales .update_status (order_id , 7 )
1048+ file_path = DATA_FOLDER + "store/contracts/trade receipts/" + order_id + ".json"
1049+ with open (file_path , 'w' ) as outfile :
1050+ outfile .write (json .dumps (contract , indent = 4 ))
1051+ file_path = DATA_FOLDER + "store/contracts/in progress/" + order_id + ".json"
1052+ if os .path .exists (file_path ):
1053+ os .remove (file_path )
1054+
10351055 def get_node (node_to_ask ):
10361056 def parse_response (response ):
10371057 if not response [0 ]:
@@ -1059,8 +1079,7 @@ def parse_response(response):
10591079 @staticmethod
10601080 def cache (file_to_save , filename ):
10611081 """
1062- Saves the file to a cache folder if it doesn't already exist .
1082+ Saves the file to a cache folder override previous versions if any .
10631083 """
1064- if not os .path .isfile (DATA_FOLDER + "cache/" + filename ):
1065- with open (DATA_FOLDER + "cache/" + filename , 'wb' ) as outfile :
1066- outfile .write (file_to_save )
1084+ with open (DATA_FOLDER + "cache/" + filename , 'wb' ) as outfile :
1085+ outfile .write (file_to_save )
0 commit comments