8cfe5d5eb636c8c2f6dd106bd6b34a9c514df64d
[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
18 def init(gui):
19     """If you want to give this a spin create a account at the target_host url and put it in your user dir config
20     file with the label_api_key."""
21
22     global auth_token
23     auth_token = gui.config.get("label_api_key")
24     if not auth_token:
25       return 
26
27     cloud_wallet = CloudWallet(gui.wallet)
28     gui.set_hook('create_settings_tab', add_settings_tab)
29     gui.set_hook('label_changed', label_changed)
30     cloud_wallet.full_pull()
31
32 def wallet_id(wallet):
33     return hashlib.sha256(str(wallet.get_master_public_key())).digest().encode('hex')
34
35 def label_changed(gui,item,label):
36     print "Label changed! Item: %s Label: %s label" % ( item, label)
37     global auth_token, target_host
38     hashed = hashlib.sha256(item).digest().encode('hex')
39     bundle = {"label": {"external_id": hashed, "text": label}}
40     params = json.dumps(bundle)
41     connection = httplib.HTTPConnection(target_host)
42     wallet = wallet_id(gui.wallet)
43     connection.request("POST", ("/api/wallets/%s/labels.json?auth_token=%s" % (wallet, auth_token)), params, {'Content-Type': 'application/json'})
44
45     response = connection.getresponse()
46     if response.reason == httplib.responses[httplib.NOT_FOUND]:
47         return
48     response = json.loads(response.read())
49
50 def add_settings_tab(gui, tabs):
51       cloud_tab = QWidget()
52       layout = QGridLayout(cloud_tab)
53       layout.addWidget(QLabel("API Key: "),0,0)
54       layout.addWidget(QLineEdit(auth_token), 0,2)
55
56       layout.addWidget(QLabel("Label sync options: "),1,0)
57
58       upload = QPushButton("Force upload")
59       upload.clicked.connect(lambda: full_push(gui.wallet))
60       layout.addWidget(upload, 1,1)
61
62       download = QPushButton("Force download")
63       download.clicked.connect(lambda: full_pull(gui.wallet))
64       layout.addWidget(download, 1,2)
65
66       tabs.addTab(cloud_tab, "Label cloud")
67
68 def full_push(wallet):
69     cloud_wallet = CloudWallet(wallet)
70     cloud_wallet.full_push()
71     print "Labels pushed"
72
73 def full_pull(wallet):
74     cloud_wallet = CloudWallet(wallet)
75     cloud_wallet.full_pull(True)
76     print "Labels pulled, please restart your client"
77
78 def show():
79     print 'showing'
80
81 def get_info():
82     return 'Label sync', "Syncs your labels with LabElectrum. Labels are not encrypted, transactions and addresses are however."
83
84 def is_enabled():
85     return True
86
87 def toggle(gui):
88     return is_enabled()
89
90 # This can probably be refactored into plain top level methods instead of a class
91 class CloudWallet():
92     def __init__(self, wallet):
93         self.mpk = hashlib.sha256(str(wallet.get_master_public_key())).digest().encode('hex')
94         self.labels = wallet.labels
95         self.transactions = wallet.transactions
96
97         addresses = [] 
98         for k, account in wallet.accounts.items():
99             for address in account[0]:
100                 addresses.append(address)
101
102         self.addresses = addresses
103
104
105     def full_pull(self, force = False):
106         global target_host, auth_token
107         connection = httplib.HTTPConnection(target_host)
108         connection.request("GET", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.mpk, auth_token)),"", {'Content-Type': 'application/json'})
109         response = connection.getresponse()
110         if response.reason == httplib.responses[httplib.NOT_FOUND]:
111             return
112         try:
113             response = json.loads(response.read())
114         except ValueError as e:
115             return
116
117         for label in response:
118             for key in self.addresses:
119                 target_hashed = hashlib.sha256(key).digest().encode('hex')
120                 if label["external_id"] == target_hashed:
121                    if force or not self.labels.get(key):
122                        self.labels[key] = label["text"] 
123             for key, value in self.transactions.iteritems():
124                 target_hashed = hashlib.sha256(key).digest().encode('hex')
125                 if label["external_id"] == target_hashed:
126                    if force or not self.labels.get(key):
127                        self.labels[key] = label["text"] 
128
129     def full_push(self):
130         global target_host, auth_token
131
132         bundle = {"labels": {}}
133         for key, value in self.labels.iteritems():
134             hashed = hashlib.sha256(key).digest().encode('hex')
135             bundle["labels"][hashed] = value
136
137         params = json.dumps(bundle)
138         connection = httplib.HTTPConnection(target_host)
139         connection.request("POST", ("/api/wallets/%s/labels/batch.json?auth_token=%s" % (self.mpk, auth_token)), params, {'Content-Type': 'application/json'})
140
141         response = connection.getresponse()
142         if response.reason == httplib.responses[httplib.NOT_FOUND]:
143             return
144         response = json.loads(response.read())
145         print response