-WIP-electrum-btcp/plugins/labels.py

119 lines
4.2 KiB
Python
Raw Normal View History

2013-03-10 09:04:00 -07:00
from electrum.util import print_error
import httplib, urllib
import hashlib
import json
from urlparse import urlparse, parse_qs
try:
import PyQt4
except:
sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
target_host = 'localhost:3000'
auth_token = 'jEnsNBb5fAR5rYSBNYnR'
def init(gui):
cloud_wallet = CloudWallet(gui.wallet)
gui.set_hook('create_settings_tab', add_settings_tab)
gui.set_hook('label_changed', label_changed)
cloud_wallet.full_pull()
def wallet_id(wallet):
return hashlib.sha256(str(wallet.get_master_public_key())).digest().encode('hex')
def label_changed(gui,item,label):
print "Label changed! Item: %s Label: %s label" % ( item, label)
global auth_token, target_host
hashed = hashlib.sha256(item).digest().encode('hex')
bundle = {"label": {"external_id": hashed, "text": label}}
params = json.dumps(bundle)
connection = httplib.HTTPConnection(target_host)
connection.request("POST", ("/api/wallets/%s/labels.json?auth_token=%s" % (wallet_id(gui.wallet), auth_token)), params, {'Content-Type': 'application/json'})
response = connection.getresponse()
if response.reason == httplib.responses[httplib.NOT_FOUND]:
return
response = json.loads(response.read())
def add_settings_tab(gui, tabs):
cloud_tab = QWidget()
layout = QGridLayout(cloud_tab)
layout.addWidget(QLabel("API Key: "),0,0)
layout.addWidget(QLineEdit(), 0,2)
layout.addWidget(QLabel("Label sync options: "),1,0)
layout.addWidget(QPushButton("Force upload"), 1,1)
layout.addWidget(QPushButton("Force download"), 1,2)
tabs.addTab(cloud_tab, "Label cloud")
def show():
print 'showing'
def get_info():
return 'Label sync', "Syncs your labels with LabElectrum. Labels are not encrypted, transactions and addresses are however."
def is_enabled():
return True
def toggle(gui):
return is_enabled()
class CloudWallet():
def __init__(self, wallet):
self.mpk = hashlib.sha256(str(wallet.get_master_public_key())).digest().encode('hex')
self.labels = wallet.labels
self.transactions = wallet.transactions
addresses = []
for k, account in wallet.accounts.items():
for address in account[0]:
addresses.append(address)
self.addresses = addresses
def full_pull(self):
global target_host, auth_token
connection = httplib.HTTPConnection(target_host)
connection.request("GET", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.mpk, auth_token)),"", {'Content-Type': 'application/json'})
response = connection.getresponse()
if response.reason == httplib.responses[httplib.NOT_FOUND]:
return
response = json.loads(response.read())
for label in response:
for key in self.addresses:
target_hashed = hashlib.sha256(key).digest().encode('hex')
if label["external_id"] == target_hashed:
if not self.labels.get(key):
self.labels[key] = label["text"]
for key, value in self.transactions.iteritems():
target_hashed = hashlib.sha256(key).digest().encode('hex')
if label["external_id"] == target_hashed:
if not self.labels.get(key):
self.labels[key] = label["text"]
def full_push(self):
global target_host, auth_token
bundle = {"labels": {}}
for key, value in self.labels.iteritems():
hashed = hashlib.sha256(key).digest().encode('hex')
bundle["labels"][hashed] = value
params = json.dumps(bundle)
connection = httplib.HTTPConnection(target_host)
connection.request("POST", ("/api/wallets/%s/labels/batch.json?auth_token=%s" % (self.mpk, auth_token)), params, {'Content-Type': 'application/json'})
response = connection.getresponse()
if response.reason == httplib.responses[httplib.NOT_FOUND]:
return
response = json.loads(response.read())