Implement buttons and move to prod server
[electrum-nvc.git] / plugins / labels.py
1 from electrum.util import print_error
2 import httplib, urllib
3 import hashlib
4 import json
5 from urlparse import urlparse, parse_qs
6 try:
7     import PyQt4
8 except:
9     sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")
10
11 from PyQt4.QtGui import *
12 from PyQt4.QtCore import *
13 import PyQt4.QtCore as QtCore
14 import PyQt4.QtGui as QtGui
15
16 target_host = 'labelectrum.herokuapp.com'
17 auth_token = 'naFniLDwQpHzoMkpwB8H'
18
19 def init(gui):
20     cloud_wallet = CloudWallet(gui.wallet)
21     gui.set_hook('create_settings_tab', add_settings_tab)
22     gui.set_hook('label_changed', label_changed)
23     cloud_wallet.full_pull()
24
25 def wallet_id(wallet):
26     return hashlib.sha256(str(wallet.get_master_public_key())).digest().encode('hex')
27
28 def label_changed(gui,item,label):
29     print "Label changed! Item: %s Label: %s label" % ( item, label)
30     global auth_token, target_host
31     hashed = hashlib.sha256(item).digest().encode('hex')
32     bundle = {"label": {"external_id": hashed, "text": label}}
33     params = json.dumps(bundle)
34     connection = httplib.HTTPConnection(target_host)
35     connection.request("POST", ("/api/wallets/%s/labels.json?auth_token=%s" % (wallet_id(gui.wallet), auth_token)), params, {'Content-Type': 'application/json'})
36
37     response = connection.getresponse()
38     if response.reason == httplib.responses[httplib.NOT_FOUND]:
39         return
40     response = json.loads(response.read())
41
42 def add_settings_tab(gui, tabs):
43       cloud_tab = QWidget()
44       layout = QGridLayout(cloud_tab)
45       layout.addWidget(QLabel("API Key: "),0,0)
46       layout.addWidget(QLineEdit("jEnsNBb5fAR5rYSBNYnR"), 0,2)
47
48       layout.addWidget(QLabel("Label sync options: "),1,0)
49
50       upload = QPushButton("Force upload")
51       upload.clicked.connect(lambda: full_push(gui.wallet))
52       layout.addWidget(upload, 1,1)
53
54       download = QPushButton("Force download")
55       download.clicked.connect(lambda: full_pull(gui.wallet))
56       layout.addWidget(download, 1,2)
57
58       tabs.addTab(cloud_tab, "Label cloud")
59
60 def full_push(wallet):
61     cloud_wallet = CloudWallet(wallet)
62     cloud_wallet.full_push()
63     print "Labels pushed"
64
65 def full_pull(wallet):
66     cloud_wallet = CloudWallet(wallet)
67     cloud_wallet.full_pull()
68     print "Labels pulled, please restart your client"
69
70 def show():
71     print 'showing'
72
73 def get_info():
74     return 'Label sync', "Syncs your labels with LabElectrum. Labels are not encrypted, transactions and addresses are however."
75
76 def is_enabled():
77     return True
78
79 def toggle(gui):
80     return is_enabled()
81
82 # This can probably be refactored into plain top level methods instead of a class
83 class CloudWallet():
84     def __init__(self, wallet):
85         self.mpk = hashlib.sha256(str(wallet.get_master_public_key())).digest().encode('hex')
86         self.labels = wallet.labels
87         self.transactions = wallet.transactions
88
89         addresses = [] 
90         for k, account in wallet.accounts.items():
91             for address in account[0]:
92                 addresses.append(address)
93
94         self.addresses = addresses
95
96
97     def full_pull(self):
98         global target_host, auth_token
99         connection = httplib.HTTPConnection(target_host)
100         connection.request("GET", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.mpk, auth_token)),"", {'Content-Type': 'application/json'})
101         response = connection.getresponse()
102         if response.reason == httplib.responses[httplib.NOT_FOUND]:
103             return
104
105         try:
106             response = json.loads(response.read())
107         except ValueError as e:
108             return
109
110         for label in response:
111             for key in self.addresses:
112                 target_hashed = hashlib.sha256(key).digest().encode('hex')
113                 if label["external_id"] == target_hashed:
114                    if not self.labels.get(key):
115                        self.labels[key] = label["text"] 
116             for key, value in self.transactions.iteritems():
117                 target_hashed = hashlib.sha256(key).digest().encode('hex')
118                 if label["external_id"] == target_hashed:
119                    if not self.labels.get(key):
120                        self.labels[key] = label["text"] 
121
122     def full_push(self):
123         global target_host, auth_token
124
125         bundle = {"labels": {}}
126         for key, value in self.labels.iteritems():
127             hashed = hashlib.sha256(key).digest().encode('hex')
128             bundle["labels"][hashed] = value
129
130         params = json.dumps(bundle)
131         connection = httplib.HTTPConnection(target_host)
132         connection.request("POST", ("/api/wallets/%s/labels/batch.json?auth_token=%s" % (self.mpk, auth_token)), params, {'Content-Type': 'application/json'})
133
134         response = connection.getresponse()
135         if response.reason == httplib.responses[httplib.NOT_FOUND]:
136             return
137         response = json.loads(response.read())
138         print response