1 from electrum.util import print_error
7 from urlparse import urlparse, parse_qs
11 sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")
13 from PyQt4.QtGui import *
14 from PyQt4.QtCore import *
15 import PyQt4.QtCore as QtCore
16 import PyQt4.QtGui as QtGui
19 from electrum import bmp, pyqrnative
20 from electrum.plugins import BasePlugin
21 from electrum.i18n import _
23 from electrum_gui.qt import HelpButton
25 class Plugin(BasePlugin):
28 return _('Label Sync')
30 def description(self):
31 return '%s\n\n%s%s%s' % (_("This plugin can sync your labels across multiple Electrum installs by using a remote database to save your data. Labels, transactions and addresses are all sent and stored encrypted on the remote server. This code might increase the load of your wallet with a few microseconds as it will sync labels on each startup."), _("To get started visit"), " http://labelectrum.herokuapp.com/ ", _(" to sign up for an account."))
36 def encode(self, message):
37 encrypted = aes.encryptData(self.encode_password, unicode(message))
38 encoded_message = base64.b64encode(encrypted)
40 return encoded_message
42 def decode(self, message):
43 decoded_message = aes.decryptData(self.encode_password, base64.b64decode(unicode(message)) )
45 return decoded_message
49 self.target_host = 'labelectrum.herokuapp.com'
50 self.window = self.gui.main_window
52 def load_wallet(self, wallet):
54 mpk = self.wallet.master_public_keys["m/0'/"][1]
55 self.encode_password = hashlib.sha1(mpk).digest().encode('hex')[:32]
56 self.wallet_id = hashlib.sha256(mpk).digest().encode('hex')
59 for account in self.wallet.accounts.values():
60 for address in account.get_addresses(0):
61 addresses.append(address)
63 self.addresses = addresses
66 # If there is an auth token we can try to actually start syncing
70 return self.config.get("plugin_label_api_key")
72 def is_available(self):
75 def requires_settings(self):
78 def set_label(self, item,label, changed):
82 bundle = {"label": {"external_id": self.encode(item), "text": self.encode(label)}}
83 params = json.dumps(bundle)
84 connection = httplib.HTTPConnection(self.target_host)
85 connection.request("POST", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.wallet_id, self.auth_token())), params, {'Content-Type': 'application/json'})
87 response = connection.getresponse()
88 if response.reason == httplib.responses[httplib.NOT_FOUND]:
90 response = json.loads(response.read())
91 except socket.gaierror as e:
92 print_error('Error connecting to service: %s ' % e)
95 def settings_dialog(self):
96 def check_for_api_key(api_key):
97 if api_key and len(api_key) > 12:
98 self.config.set_key("plugin_label_api_key", str(self.auth_token_edit.text()))
99 self.upload.setEnabled(True)
100 self.download.setEnabled(True)
101 self.accept.setEnabled(True)
103 self.upload.setEnabled(False)
104 self.download.setEnabled(False)
105 self.accept.setEnabled(False)
107 d = QDialog(self.window)
108 layout = QGridLayout(d)
109 layout.addWidget(QLabel("API Key: "),0,0)
111 self.auth_token_edit = QLineEdit(self.auth_token())
112 self.auth_token_edit.textChanged.connect(check_for_api_key)
114 layout.addWidget(QLabel("Label sync options: "),2,0)
115 layout.addWidget(self.auth_token_edit, 0,1,1,2)
117 decrypt_key_text = QLineEdit(self.encode_password)
118 decrypt_key_text.setReadOnly(True)
119 layout.addWidget(decrypt_key_text, 1,1)
120 layout.addWidget(QLabel("Decryption key: "),1,0)
121 layout.addWidget(HelpButton("This key can be used on the LabElectrum website to decrypt your data in case you want to review it online."),1,2)
123 self.upload = QPushButton("Force upload")
124 self.upload.clicked.connect(self.full_push)
125 layout.addWidget(self.upload, 2,1)
127 self.download = QPushButton("Force download")
128 self.download.clicked.connect(lambda: self.full_pull(True))
129 layout.addWidget(self.download, 2,2)
131 c = QPushButton(_("Cancel"))
132 c.clicked.connect(d.reject)
134 self.accept = QPushButton(_("Done"))
135 self.accept.clicked.connect(d.accept)
137 layout.addWidget(c,3,1)
138 layout.addWidget(self.accept,3,2)
140 check_for_api_key(self.auth_token())
148 if not self.auth_token(): # First run, throw plugin settings in your face
150 if self.settings_dialog():
151 self.set_enabled(True)
154 self.set_enabled(False)
157 self.set_enabled(True)
162 if self.do_full_push():
163 QMessageBox.information(None, _("Labels uploaded"), _("Your labels have been uploaded."))
165 def full_pull(self, force = False):
166 if self.do_full_pull(force) and force:
167 QMessageBox.information(None, _("Labels synchronized"), _("Your labels have been synchronized."))
168 self.window.update_history_tab()
169 self.window.update_completions()
170 self.window.update_receive_tab()
171 self.window.update_contacts_tab()
173 def do_full_push(self):
175 bundle = {"labels": {}}
176 for key, value in self.wallet.labels.iteritems():
177 encoded = self.encode(key)
178 bundle["labels"][encoded] = self.encode(value)
180 params = json.dumps(bundle)
181 connection = httplib.HTTPConnection(self.target_host)
182 connection.request("POST", ("/api/wallets/%s/labels/batch.json?auth_token=%s" % (self.wallet_id, self.auth_token())), params, {'Content-Type': 'application/json'})
184 response = connection.getresponse()
185 if response.reason == httplib.responses[httplib.NOT_FOUND]:
188 response = json.loads(response.read())
189 except ValueError as e:
192 if "error" in response:
193 QMessageBox.warning(None, _("Error"),_("Could not sync labels: %s" % response["error"]))
197 except socket.gaierror as e:
198 print_error('Error connecting to service: %s ' % e)
201 def do_full_pull(self, force = False):
203 connection = httplib.HTTPConnection(self.target_host)
204 connection.request("GET", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.wallet_id, self.auth_token())),"", {'Content-Type': 'application/json'})
205 response = connection.getresponse()
206 if response.reason == httplib.responses[httplib.NOT_FOUND]:
209 response = json.loads(response.read())
210 except ValueError as e:
213 if "error" in response:
214 QMessageBox.warning(None, _("Error"),_("Could not sync labels: %s" % response["error"]))
217 for label in response:
218 decoded_key = self.decode(label["external_id"])
219 decoded_label = self.decode(label["text"])
220 if force or not self.wallet.labels.get(decoded_key):
221 self.wallet.labels[decoded_key] = decoded_label
223 except socket.gaierror as e:
224 print_error('Error connecting to service: %s ' % e)