forked from OpenBazaar/OpenBazaar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket.py
172 lines (127 loc) · 6.45 KB
/
market.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
from protocol import shout, proto_page, query_page
from reputation import Reputation
from orders import Orders
import protocol
import sys
import json
import lookup
from pymongo import MongoClient
import logging
class Market(object):
def __init__(self, transport):
self._log = logging.getLogger(self.__class__.__name__)
self._log.info("Initializing")
# for now we have the id in the transport
self._myself = transport._myself
self._peers = transport._peers
self._transport = transport
self.query_ident = None
self.reputation = Reputation(self._transport)
self.orders = Orders(self._transport)
self.order_entries = self.orders._orders
# TODO: Persistent storage of nicknames and pages
self.nicks = {}
self.pages = {}
# Connect to database
MONGODB_URI = 'mongodb://localhost:27017'
_dbclient = MongoClient()
self._db = _dbclient.openbazaar
# Register callbacks for incoming events
transport.add_callback('query_myorders', self.on_query_myorders)
transport.add_callback('peer', self.on_peer)
transport.add_callback('query_page', self.on_query_page)
transport.add_callback('page', self.on_page)
transport.add_callback('negotiate_pubkey', self.on_negotiate_pubkey)
transport.add_callback('proto_response_pubkey', self.on_response_pubkey)
self.load_page()
# Send Market Shout
transport.send(shout({'text': 'Market Initialized'}))
def lookup(self, msg):
if self.query_ident is None:
self._log.info("Initializing identity query")
self.query_ident = lookup.QueryIdent()
nickname = str(msg["text"])
key = self.query_ident.lookup(nickname)
if key is None:
self._log.info("Key not found for this nickname")
return ("Key not found for this nickname", None)
self._log.info("Found key: %s " % key.encode("hex"))
if self._transport.nick_mapping.has_key(nickname):
self._log.info("Already have a cached mapping, just adding key there.")
response = {'nickname': nickname, 'pubkey': self._transport.nick_mapping[nickname][1].encode('hex'), 'signature': self._transport.nick_mapping[nickname][0].encode('hex'), 'type': 'response_pubkey', 'signature': 'unknown'}
self._transport.nick_mapping[nickname][0] = key
return (None, response)
self._transport.nick_mapping[nickname] = [key, None]
self._transport.send(protocol.negotiate_pubkey(nickname, key))
# Load default information for your market from your file
def load_page(self):
self._log.info("Loading market config from " + sys.argv[1])
with open(sys.argv[1]) as f:
data = json.loads(f.read())
self._log.info("Configuration data: " + json.dumps(data))
assert "desc" in data
nickname = data["nickname"]
desc = data["desc"]
tagline = "%s: %s" % (nickname, desc)
self.mypage = tagline
self.nickname = nickname
self.signature = self._transport._myself.sign(tagline)
self._log.info("Tagline signature: " + self.signature.encode("hex"))
# SETTINGS
def save_settings(self, msg):
self._db.settings.update({}, msg, True)
def get_settings(self):
settings = self._db.settings.find_one()
if settings:
return { "bitmessage": settings['bitmessage'] if settings.has_key("bitmessage") else "",
"email": settings['email'] if settings.has_key("email") else "",
"PGPPubKey": settings['PGPPubKey'] if settings.has_key("PGPPubKey") else "",
"pubkey": settings['pubkey'] if settings.has_key("pubkey") else "",
"nickname": settings['nickname'] if settings.has_key("nickname") else "",
"secret": settings['secret'] if settings.has_key("secret") else "",
}
# PAGE QUERYING
def query_page(self, pubkey):
self._transport.send(query_page(pubkey))
def on_page(self, page):
self._log.info("Page returned: " + str(page))
pubkey = page.get('pubkey')
page = page.get('text')
if pubkey and page:
self.pages[pubkey] = page
# Return your page info if someone requests it on the network
def on_query_page(self, peer):
self._log.info("Someone is querying for your page")
self._transport.send(proto_page(self._transport._myself.get_pubkey(), self.mypage, self.signature, self.nickname))
def on_query_myorders(self, peer):
self._log.info("Someone is querying for your page")
self._transport.send(proto_page(self._transport._myself.get_pubkey(), self.mypage, self.signature, self.nickname))
def on_peer(self, peer):
self._log.info("New peer")
def on_negotiate_pubkey(self, ident_pubkey):
self._log.info("Someone is asking for your real pubKey")
assert "nickname" in ident_pubkey
assert "ident_pubkey" in ident_pubkey
nickname = ident_pubkey['nickname']
ident_pubkey = ident_pubkey['ident_pubkey'].decode("hex")
self._transport.respond_pubkey_if_mine(nickname, ident_pubkey)
def on_response_pubkey(self, response):
self._log.info("got a pubkey!")
assert "pubkey" in response
assert "nickname" in response
assert "signature" in response
pubkey = response["pubkey"].decode("hex")
signature = response["signature"].decode("hex")
nickname = response["nickname"]
# Cache mapping for later.
if not self._transport.nick_mapping.has_key(nickname):
self._transport.nick_mapping[nickname] = [None, pubkey]
# Verify signature here...
# Add to our dict.
self._transport.nick_mapping[nickname][1] = pubkey
self._log.info("[market] mappings: ###############")
for k, v in self._transport.nick_mapping.iteritems():
self._log.info("'%s' -> '%s' (%s)" % (
k, v[1].encode("hex") if v[1] is not None else v[1],
v[0].encode("hex") if v[0] is not None else v[0]))
self._log.info("##################################")