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.plugins import BasePlugin
20 from electrum.i18n import _
22 from electrum_gui.qt import HelpButton, EnterButton
24 class Plugin(BasePlugin):
27 return _('Label Sync')
29 def description(self):
30 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."))
35 def encode(self, message):
36 encrypted = aes.encryptData(self.encode_password, unicode(message))
37 encoded_message = base64.b64encode(encrypted)
39 return encoded_message
41 def decode(self, message):
42 decoded_message = aes.decryptData(self.encode_password, base64.b64decode(unicode(message)) )
44 return decoded_message
48 self.target_host = 'labelectrum.herokuapp.com'
49 self.window = self.gui.main_window
51 def load_wallet(self, wallet):
53 if self.wallet.get_master_public_key():
54 mpk = self.wallet.get_master_public_key()
56 mpk = self.wallet.master_public_keys["m/0'/"][1]
57 self.encode_password = hashlib.sha1(mpk).digest().encode('hex')[:32]
58 self.wallet_id = hashlib.sha256(mpk).digest().encode('hex')
61 for account in self.wallet.accounts.values():
62 for address in account.get_addresses(0):
63 addresses.append(address)
65 self.addresses = addresses
68 # If there is an auth token we can try to actually start syncing
72 return self.config.get("plugin_label_api_key")
74 def is_available(self):
77 def requires_settings(self):
80 def set_label(self, item,label, changed):
84 bundle = {"label": {"external_id": self.encode(item), "text": self.encode(label)}}
85 params = json.dumps(bundle)
86 connection = httplib.HTTPConnection(self.target_host)
87 connection.request("POST", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.wallet_id, self.auth_token())), params, {'Content-Type': 'application/json'})
89 response = connection.getresponse()
90 if response.reason == httplib.responses[httplib.NOT_FOUND]:
92 response = json.loads(response.read())
93 except socket.gaierror as e:
94 print_error('Error connecting to service: %s ' % e)
97 def settings_widget(self, window):
98 return EnterButton(_('Settings'), self.settings_dialog)
100 def settings_dialog(self):
101 def check_for_api_key(api_key):
102 if api_key and len(api_key) > 12:
103 self.config.set_key("plugin_label_api_key", str(self.auth_token_edit.text()))
104 self.upload.setEnabled(True)
105 self.download.setEnabled(True)
106 self.accept.setEnabled(True)
108 self.upload.setEnabled(False)
109 self.download.setEnabled(False)
110 self.accept.setEnabled(False)
113 layout = QGridLayout(d)
114 layout.addWidget(QLabel("API Key: "),0,0)
116 self.auth_token_edit = QLineEdit(self.auth_token())
117 self.auth_token_edit.textChanged.connect(check_for_api_key)
119 layout.addWidget(QLabel("Label sync options: "),2,0)
120 layout.addWidget(self.auth_token_edit, 0,1,1,2)
122 decrypt_key_text = QLineEdit(self.encode_password)
123 decrypt_key_text.setReadOnly(True)
124 layout.addWidget(decrypt_key_text, 1,1)
125 layout.addWidget(QLabel("Decryption key: "),1,0)
126 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)
128 self.upload = QPushButton("Force upload")
129 self.upload.clicked.connect(self.full_push)
130 layout.addWidget(self.upload, 2,1)
132 self.download = QPushButton("Force download")
133 self.download.clicked.connect(lambda: self.full_pull(True))
134 layout.addWidget(self.download, 2,2)
136 c = QPushButton(_("Cancel"))
137 c.clicked.connect(d.reject)
139 self.accept = QPushButton(_("Done"))
140 self.accept.clicked.connect(d.accept)
142 layout.addWidget(c,3,1)
143 layout.addWidget(self.accept,3,2)
145 check_for_api_key(self.auth_token())
153 if not self.auth_token(): # First run, throw plugin settings in your face
155 self.load_wallet(self.gui.main_window.wallet)
156 if self.settings_dialog():
157 self.set_enabled(True)
160 self.set_enabled(False)
163 self.set_enabled(True)
168 if self.do_full_push():
169 QMessageBox.information(None, _("Labels uploaded"), _("Your labels have been uploaded."))
171 def full_pull(self, force = False):
172 if self.do_full_pull(force) and force:
173 QMessageBox.information(None, _("Labels synchronized"), _("Your labels have been synchronized."))
174 self.window.update_history_tab()
175 self.window.update_completions()
176 self.window.update_receive_tab()
177 self.window.update_contacts_tab()
179 def do_full_push(self):
181 bundle = {"labels": {}}
182 for key, value in self.wallet.labels.iteritems():
183 encoded = self.encode(key)
184 bundle["labels"][encoded] = self.encode(value)
186 params = json.dumps(bundle)
187 connection = httplib.HTTPConnection(self.target_host)
188 connection.request("POST", ("/api/wallets/%s/labels/batch.json?auth_token=%s" % (self.wallet_id, self.auth_token())), params, {'Content-Type': 'application/json'})
190 response = connection.getresponse()
191 if response.reason == httplib.responses[httplib.NOT_FOUND]:
194 response = json.loads(response.read())
195 except ValueError as e:
198 if "error" in response:
199 QMessageBox.warning(None, _("Error"),_("Could not sync labels: %s" % response["error"]))
203 except socket.gaierror as e:
204 print_error('Error connecting to service: %s ' % e)
207 def do_full_pull(self, force = False):
209 connection = httplib.HTTPConnection(self.target_host)
210 connection.request("GET", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.wallet_id, self.auth_token())),"", {'Content-Type': 'application/json'})
211 response = connection.getresponse()
212 if response.reason == httplib.responses[httplib.NOT_FOUND]:
215 response = json.loads(response.read())
216 except ValueError as e:
219 if "error" in response:
220 QMessageBox.warning(None, _("Error"),_("Could not sync labels: %s" % response["error"]))
223 for label in response:
224 decoded_key = self.decode(label["external_id"])
225 decoded_label = self.decode(label["text"])
226 if force or not self.wallet.labels.get(decoded_key):
227 self.wallet.labels[decoded_key] = decoded_label
229 except socket.gaierror as e:
230 print_error('Error connecting to service: %s ' % e)