1 from electrum.util import print_error
5 from urlparse import urlparse, parse_qs
9 sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")
11 from PyQt4.QtGui import *
12 from PyQt4.QtCore import *
13 import PyQt4.QtCore as QtCore
14 import PyQt4.QtGui as QtGui
16 target_host = 'labelectrum.herokuapp.com'
17 auth_token = 'naFniLDwQpHzoMkpwB8H'
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()
25 def wallet_id(wallet):
26 return hashlib.sha256(str(wallet.get_master_public_key())).digest().encode('hex')
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'})
37 response = connection.getresponse()
38 if response.reason == httplib.responses[httplib.NOT_FOUND]:
40 response = json.loads(response.read())
42 def add_settings_tab(gui, tabs):
44 layout = QGridLayout(cloud_tab)
45 layout.addWidget(QLabel("API Key: "),0,0)
46 layout.addWidget(QLineEdit("jEnsNBb5fAR5rYSBNYnR"), 0,2)
48 layout.addWidget(QLabel("Label sync options: "),1,0)
50 upload = QPushButton("Force upload")
51 upload.clicked.connect(lambda: full_push(gui.wallet))
52 layout.addWidget(upload, 1,1)
54 download = QPushButton("Force download")
55 download.clicked.connect(lambda: full_pull(gui.wallet))
56 layout.addWidget(download, 1,2)
58 tabs.addTab(cloud_tab, "Label cloud")
60 def full_push(wallet):
61 cloud_wallet = CloudWallet(wallet)
62 cloud_wallet.full_push()
65 def full_pull(wallet):
66 cloud_wallet = CloudWallet(wallet)
67 cloud_wallet.full_pull()
68 print "Labels pulled, please restart your client"
74 return 'Label sync', "Syncs your labels with LabElectrum. Labels are not encrypted, transactions and addresses are however."
82 # This can probably be refactored into plain top level methods instead of a class
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
90 for k, account in wallet.accounts.items():
91 for address in account[0]:
92 addresses.append(address)
94 self.addresses = addresses
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]:
106 response = json.loads(response.read())
107 except ValueError as e:
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"]
123 global target_host, auth_token
125 bundle = {"labels": {}}
126 for key, value in self.labels.iteritems():
127 hashed = hashlib.sha256(key).digest().encode('hex')
128 bundle["labels"][hashed] = value
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'})
134 response = connection.getresponse()
135 if response.reason == httplib.responses[httplib.NOT_FOUND]:
137 response = json.loads(response.read())