This repository has been archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathmoderation.py
183 lines (154 loc) · 8.5 KB
/
moderation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import json
import nacl.signing
import os
import time
from binascii import unhexlify
from collections import OrderedDict
from constants import DATA_FOLDER
from dht.utils import digest
from keys.keychain import KeyChain
from market.contracts import Contract
from protos.objects import PlaintextMessage
def process_dispute(contract, db, message_listener, notification_listener, testnet):
"""
This function processes a dispute message received from another node. It checks the
contract to see if this a dispute for a purchase we made, a dispute for one of our
sales, or a new case if we are the moderator.
If it's a purchase or sale it will
update the order status to disputed and push a notification to the listener.
If it's a new case it will validate the contract, create a new case in the db,
and push a notification to the listener.
Args:
contract: a json contract of the current order state. Should have a "dispute"
object attached with dispute info.
db: a `Database` object.
message_listener: a `MessageListenerImpl` object.
notification_listener: a `NotificationListenerImpl` object.
testnet: `bool` of whether we're on testnet or not.
"""
tmp_contract = contract
if "vendor_order_confirmation" in tmp_contract:
del tmp_contract["vendor_order_confirmation"]
if "buyer_receipt" in tmp_contract:
del tmp_contract["buyer_receipt"]
order_id = digest(json.dumps(tmp_contract, indent=4)).encode("hex")
own_guid = KeyChain(db).guid.encode("hex")
if contract["dispute"]["info"]["guid"] == contract["vendor_offer"]["listing"]["id"]["guid"]:
guid = unhexlify(contract["vendor_offer"]["listing"]["id"]["guid"])
signing_key = unhexlify(contract["vendor_offer"]["listing"]["id"]["pubkeys"]["guid"])
if "blockchain_id" in contract["vendor_offer"]["listing"]["id"]:
handle = contract["vendor_offer"]["listing"]["id"]["blockchain_id"]
else:
handle = ""
encryption_key = unhexlify(contract["vendor_offer"]["listing"]["id"]["pubkeys"]["encryption"])
proof_sig = contract["dispute"]["info"]["proof_sig"]
elif contract["dispute"]["info"]["guid"] == contract["buyer_order"]["order"]["id"]["guid"]:
guid = unhexlify(contract["buyer_order"]["order"]["id"]["guid"])
signing_key = unhexlify(contract["buyer_order"]["order"]["id"]["pubkeys"]["guid"])
if "blockchain_id" in contract["buyer_order"]["order"]["id"]:
handle = contract["buyer_order"]["order"]["id"]["blockchain_id"]
else:
handle = ""
encryption_key = unhexlify(contract["buyer_order"]["order"]["id"]["pubkeys"]["encryption"])
proof_sig = None
else:
raise Exception("Dispute guid not in contract")
verify_key = nacl.signing.VerifyKey(signing_key)
verify_key.verify(json.dumps(contract["dispute"]["info"], indent=4),
contract["dispute"]["signature"])
p = PlaintextMessage()
p.sender_guid = guid
p.handle = handle
p.signed_pubkey = signing_key
p.encryption_pubkey = encryption_key
p.subject = order_id
p.type = PlaintextMessage.Type.Value("DISPUTE_OPEN")
p.message = contract["dispute"]["info"]["claim"]
p.timestamp = time.time()
p.avatar_hash = contract["dispute"]["info"]["avatar_hash"]
if db.Purchases().get_purchase(order_id) is not None:
db.Purchases().update_status(order_id, 4)
elif db.Sales().get_sale(order_id) is not None:
db.Sales().update_status(order_id, 4)
elif "moderators" in contract["vendor_offer"]["listing"]:
# TODO: make sure a case isn't already open in the db
is_selected = False
for moderator in contract["vendor_offer"]["listing"]["moderators"]:
if moderator["guid"] == own_guid and contract["buyer_order"]["order"]["moderator"] == own_guid:
is_selected = True
if not is_selected:
raise Exception("Not a moderator for this contract")
else:
if "blockchain_id" in contract["vendor_offer"]["listing"]["id"]:
vendor = contract["vendor_offer"]["listing"]["id"]["blockchain_id"]
else:
vendor = contract["vendor_offer"]["listing"]["id"]["guid"]
if "blockchain_id" in contract["buyer_order"]["order"]["id"]:
buyer = contract["buyer_order"]["order"]["id"]["blockchain_id"]
else:
buyer = contract["buyer_order"]["order"]["id"]["guid"]
c = Contract(db, contract=contract, testnet=testnet)
validation_failures = c.validate_for_moderation(proof_sig)
db.Cases().new_case(order_id,
contract["vendor_offer"]["listing"]["item"]["title"],
time.time(),
contract["buyer_order"]["order"]["date"],
contract["buyer_order"]["order"],
float(contract["buyer_order"]["order"]["payment"]["amount"]),
contract["vendor_offer"]["listing"]["item"]["image_hashes"][0],
buyer, vendor, json.dumps(validation_failures),
contract["dispute"]["info"]["claim"])
with open(DATA_FOLDER + "cases/" + order_id + ".json", 'wb') as outfile:
outfile.write(json.dumps(contract, indent=4))
else:
raise Exception("Order ID for dispute not found")
message_listener.notify(p, "")
notification_listener.notify(guid, handle, "dispute_open", order_id,
contract["vendor_offer"]["listing"]["item"]["title"],
contract["vendor_offer"]["listing"]["item"]["image_hashes"][0])
def close_dispute(resolution_json, db, message_listener, notification_listener, testnet):
"""
This function processes a dispute close message received from the moderator. It will
store the resolution in the contract and send a notification to the listeners telling
them that the dispute is resolved.
Args:
resolution_json: The `dispute_resolution` portion of the contract received from the moderator.
db: a `Database` object.
message_listener: a `MessageListenerImpl` object.
notification_listener: a `NotificationListenerImpl` object.
testnet: `bool` of whether we're on testnet or not.
"""
order_id = resolution_json["dispute_resolution"]["resolution"]["order_id"]
if os.path.exists(DATA_FOLDER + "purchases/in progress/" + order_id + ".json"):
file_path = DATA_FOLDER + "purchases/trade receipts/" + order_id + ".json"
elif os.path.exists(DATA_FOLDER + "store/contracts/in progress/" + order_id + ".json"):
file_path = DATA_FOLDER + "store/contracts/in progress/" + order_id + ".json"
with open(file_path, 'r') as filename:
contract = json.load(filename, object_pairs_hook=OrderedDict)
for moderator in contract["vendor_offer"]["listing"]["moderators"]:
if moderator["guid"] == contract["buyer_order"]["order"]["moderator"]:
moderator_guid = unhexlify(moderator["guid"])
moderator_handle = moderator["blockchain_id"]
moderator_pubkey = unhexlify(moderator["pubkeys"]["signing"]["key"])
moderator_enc_key = unhexlify(moderator["pubkeys"]["encryption"]["key"])
moderator_avatar = unhexlify(moderator["avatar"])
verify_key = nacl.signing.VerifyKey(moderator_pubkey)
verify_key.verify(json.dumps(resolution_json["dispute_resolution"]["resolution"], indent=4),
resolution_json["dispute_resolution"]["signature"])
contract["dispute_resolution"] = resolution_json["dispute_resolution"]
with open(file_path, 'wb') as outfile:
outfile.write(json.dumps(contract, indent=4))
p = PlaintextMessage()
p.sender_guid = moderator_guid
p.handle = moderator_handle
p.signed_pubkey = moderator_pubkey
p.encryption_pubkey = moderator_enc_key
p.subject = order_id
p.type = PlaintextMessage.Type.Value("DISPUTE_CLOSE")
p.message = resolution_json["dispute_resolution"]["resolution"]["decision"]
p.timestamp = time.time()
p.avatar_hash = moderator_avatar
message_listener.notify(p, "")
notification_listener.notify(moderator_guid, moderator_handle, "dispute_close", order_id,
contract["vendor_offer"]["listing"]["item"]["title"],
contract["vendor_offer"]["listing"]["item"]["image_hashes"][0])