forked from spesmilo/electrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoinbase_buyback.py
321 lines (274 loc) · 9.7 KB
/
coinbase_buyback.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import PyQt4
import sys
import PyQt4.QtCore as QtCore
import base64
import urllib
import re
import time
import os
import httplib
import datetime
import json
import string
from urllib import urlencode
from PyQt4.QtGui import *
from PyQt4.QtCore import *
try:
from PyQt4.QtWebKit import QWebView
loaded_qweb = True
except ImportError as e:
loaded_qweb = False
from electrum.plugins import BasePlugin, hook
from electrum.i18n import _, set_language
from electrum.util import user_dir
from electrum.util import appdata_dir
from electrum.util import format_satoshis
from electrum_gui.qt import ElectrumGui
SATOSHIS_PER_BTC = float(100000000)
COINBASE_ENDPOINT = 'https://coinbase.com'
SCOPE = 'buy'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
TOKEN_URI = 'https://coinbase.com/oauth/token'
CLIENT_ID = '0a930a48b5a6ea10fb9f7a9fec3d093a6c9062ef8a7eeab20681274feabdab06'
CLIENT_SECRET = 'f515989e8819f1822b3ac7a7ef7e57f755c9b12aee8f22de6b340a99fd0fd617'
# Expiry is stored in RFC3339 UTC format
EXPIRY_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
class Plugin(BasePlugin):
def fullname(self): return 'Coinbase BuyBack'
def description(self): return 'After sending bitcoin, prompt the user with the option to rebuy them via Coinbase.\n\nMarcell Ortutay, 1FNGQvm29tKM7y3niq63RKi7Qbg7oZ3jrB'
def __init__(self, gui, name):
BasePlugin.__init__(self, gui, name)
self._is_available = self._init()
def _init(self):
return loaded_qweb
@hook
def init_qt(self, gui):
self.gui = gui
def is_available(self):
return self._is_available
def enable(self):
return BasePlugin.enable(self)
@hook
def receive_tx(self, tx, wallet):
domain = wallet.get_account_addresses(None)
is_relevant, is_send, v, fee = tx.get_value(domain, wallet.prevout_values)
if isinstance(self.gui, ElectrumGui):
try:
web = propose_rebuy_qt(abs(v))
except OAuth2Exception as e:
rm_local_oauth_credentials()
# TODO(ortutay): android flow
def propose_rebuy_qt(amount):
web = QWebView()
box = QMessageBox()
box.setFixedSize(200, 200)
credentials = read_local_oauth_credentials()
questionText = _('Rebuy ') + format_satoshis(amount) + _(' BTC?')
if credentials:
credentials.refresh()
if credentials and not credentials.invalid:
credentials.store_locally()
totalPrice = get_coinbase_total_price(credentials, amount)
questionText += _('\n(Price: ') + totalPrice + _(')')
if not question(box, questionText):
return
if credentials:
do_buy(credentials, amount)
else:
do_oauth_flow(web, amount)
return web
def do_buy(credentials, amount):
conn = httplib.HTTPSConnection('coinbase.com')
credentials.authorize(conn)
params = {
'qty': float(amount)/SATOSHIS_PER_BTC,
'agree_btc_amount_varies': False
}
resp = conn.auth_request('POST', '/api/v1/buys', urlencode(params), None)
if resp.status != 200:
message(_('Error, could not buy bitcoin'))
return
content = json.loads(resp.read())
if content['success']:
message(_('Success!\n') + content['transfer']['description'])
else:
if content['errors']:
message(_('Error: ') + string.join(content['errors'], '\n'))
else:
message(_('Error, could not buy bitcoin'))
def get_coinbase_total_price(credentials, amount):
conn = httplib.HTTPSConnection('coinbase.com')
params={'qty': amount/SATOSHIS_PER_BTC}
conn.request('GET', '/api/v1/prices/buy?' + urlencode(params))
resp = conn.getresponse()
if resp.status != 200:
return 'unavailable'
content = json.loads(resp.read())
return '$' + content['total']['amount']
def do_oauth_flow(web, amount):
# QT expects un-escaped URL
auth_uri = step1_get_authorize_url()
web.load(QUrl(auth_uri))
web.setFixedSize(500, 700)
web.show()
web.titleChanged.connect(lambda(title): complete_oauth_flow(title, web, amount) if re.search('^[a-z0-9]+$', title) else False)
def complete_oauth_flow(token, web, amount):
web.close()
credentials = step2_exchange(str(token))
credentials.store_locally()
do_buy(credentials, amount)
def token_path():
dir = user_dir() + '/coinbase_buyback'
if not os.access(dir, os.F_OK):
os.mkdir(dir)
return dir + '/token'
def read_local_oauth_credentials():
if not os.access(token_path(), os.F_OK):
return None
f = open(token_path(), 'r')
data = f.read()
f.close()
try:
credentials = Credentials.from_json(data)
return credentials
except Exception as e:
return None
def rm_local_oauth_credentials():
os.remove(token_path())
def step1_get_authorize_url():
return ('https://coinbase.com/oauth/authorize'
+ '?scope=' + SCOPE
+ '&redirect_uri=' + REDIRECT_URI
+ '&response_type=code'
+ '&client_id=' + CLIENT_ID
+ '&access_type=offline')
def step2_exchange(code):
body = urllib.urlencode({
'grant_type': 'authorization_code',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'code': code,
'redirect_uri': REDIRECT_URI,
'scope': SCOPE,
})
headers = {
'content-type': 'application/x-www-form-urlencoded',
}
conn = httplib.HTTPSConnection('coinbase.com')
conn.request('POST', TOKEN_URI, body, headers)
resp = conn.getresponse()
if resp.status == 200:
d = json.loads(resp.read())
access_token = d['access_token']
refresh_token = d.get('refresh_token', None)
token_expiry = None
if 'expires_in' in d:
token_expiry = datetime.datetime.utcnow() + datetime.timedelta(
seconds=int(d['expires_in']))
return Credentials(access_token, refresh_token, token_expiry)
else:
raise OAuth2Exception(content)
class OAuth2Exception(Exception):
"""An error related to OAuth2"""
class Credentials(object):
def __init__(self, access_token, refresh_token, token_expiry):
self.access_token = access_token
self.refresh_token = refresh_token
self.token_expiry = token_expiry
# Indicates a failed refresh
self.invalid = False
def to_json(self):
token_expiry = self.token_expiry
if (token_expiry and isinstance(token_expiry, datetime.datetime)):
token_expiry = token_expiry.strftime(EXPIRY_FORMAT)
d = {
'access_token': self.access_token,
'refresh_token': self.refresh_token,
'token_expiry': token_expiry,
}
return json.dumps(d)
def store_locally(self):
f = open(token_path(), 'w')
f.write(self.to_json())
f.close()
@classmethod
def from_json(cls, s):
data = json.loads(s)
if ('token_expiry' in data
and not isinstance(data['token_expiry'], datetime.datetime)):
try:
data['token_expiry'] = datetime.datetime.strptime(
data['token_expiry'], EXPIRY_FORMAT)
except:
data['token_expiry'] = None
retval = Credentials(
data['access_token'],
data['refresh_token'],
data['token_expiry'])
return retval
def apply(self, headers):
headers['Authorization'] = 'Bearer ' + self.access_token
def authorize(self, conn):
request_orig = conn.request
def new_request(method, uri, params, headers):
if headers == None:
headers = {}
self.apply(headers)
request_orig(method, uri, params, headers)
resp = conn.getresponse()
if resp.status == 401:
# Refresh and try again
self._refresh(request_orig)
self.store_locally()
self.apply(headers)
request_orig(method, uri, params, headers)
return conn.getresponse()
else:
return resp
conn.auth_request = new_request
return conn
def refresh(self):
try:
self._refresh()
except OAuth2Exception as e:
rm_local_oauth_credentials()
self.invalid = True
raise e
def _refresh(self):
conn = httplib.HTTPSConnection('coinbase.com')
body = urllib.urlencode({
'grant_type': 'refresh_token',
'refresh_token': self.refresh_token,
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
})
headers = {
'content-type': 'application/x-www-form-urlencoded',
}
conn.request('POST', TOKEN_URI, body, headers)
resp = conn.getresponse()
if resp.status == 200:
d = json.loads(resp.read())
self.token_response = d
self.access_token = d['access_token']
self.refresh_token = d.get('refresh_token', self.refresh_token)
if 'expires_in' in d:
self.token_expiry = datetime.timedelta(
seconds=int(d['expires_in'])) + datetime.datetime.utcnow()
else:
raise OAuth2Exception('Refresh failed, ' + content)
def message(msg):
box = QMessageBox()
box.setFixedSize(200, 200)
return QMessageBox.information(box, _('Message'), msg)
def question(widget, msg):
return (QMessageBox.question(
widget, _('Message'), msg, QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
== QMessageBox.Yes)
def main():
app = QApplication(sys.argv)
print sys.argv[1]
propose_rebuy_qt(int(sys.argv[1]))
sys.exit(app.exec_())
if __name__ == "__main__":
main()