store contacts and invoices in wallet file. fix #1482
This commit is contained in:
@@ -93,7 +93,6 @@ class Commands:
|
||||
self._callback = callback
|
||||
self._password = password
|
||||
self.new_password = new_password
|
||||
self.contacts = contacts.Contacts(self.config)
|
||||
|
||||
def _run(self, method, args, password_getter):
|
||||
cmd = known_commands[method]
|
||||
@@ -371,7 +370,7 @@ class Commands:
|
||||
def _resolver(self, x):
|
||||
if x is None:
|
||||
return None
|
||||
out = self.contacts.resolve(x)
|
||||
out = self.wallet.contacts.resolve(x)
|
||||
if out.get('type') == 'openalias' and self.nocheck is False and out.get('validated') is False:
|
||||
raise BaseException('cannot verify alias', x)
|
||||
return out['address']
|
||||
@@ -464,21 +463,21 @@ class Commands:
|
||||
transaction ID"""
|
||||
self.wallet.set_label(key, label)
|
||||
|
||||
@command('')
|
||||
@command('w')
|
||||
def listcontacts(self):
|
||||
"""Show your list of contacts"""
|
||||
return self.contacts
|
||||
return self.wallet.contacts
|
||||
|
||||
@command('')
|
||||
@command('w')
|
||||
def getalias(self, key):
|
||||
"""Retrieve alias. Lookup in your list of contacts, and for an OpenAlias DNS record."""
|
||||
return self.contacts.resolve(key)
|
||||
return self.wallet.contacts.resolve(key)
|
||||
|
||||
@command('')
|
||||
@command('w')
|
||||
def searchcontacts(self, query):
|
||||
"""Search through contacts, return matching entries. """
|
||||
results = {}
|
||||
for key, value in self.contacts.items():
|
||||
for key, value in self.wallet.contacts.items():
|
||||
if query.lower() in key.lower():
|
||||
results[key] = value
|
||||
return results
|
||||
@@ -603,7 +602,7 @@ class Commands:
|
||||
alias = self.config.get('alias')
|
||||
if not alias:
|
||||
raise BaseException('No alias in your configuration')
|
||||
alias_addr = self.contacts.resolve(alias)['address']
|
||||
alias_addr = self.wallet.contacts.resolve(alias)['address']
|
||||
self.wallet.sign_payment_request(address, alias, alias_addr, self._password)
|
||||
|
||||
@command('w')
|
||||
|
||||
@@ -24,17 +24,21 @@
|
||||
import sys
|
||||
import re
|
||||
import dns
|
||||
import os
|
||||
import json
|
||||
|
||||
import bitcoin
|
||||
import dnssec
|
||||
from util import StoreDict, print_error
|
||||
from util import print_error
|
||||
from i18n import _
|
||||
|
||||
|
||||
class Contacts(StoreDict):
|
||||
class Contacts(dict):
|
||||
|
||||
def __init__(self, config):
|
||||
StoreDict.__init__(self, config, 'contacts')
|
||||
def __init__(self, storage):
|
||||
self.storage = storage
|
||||
d = self.storage.get('contacts', {})
|
||||
self.update(d)
|
||||
# backward compatibility
|
||||
for k, v in self.items():
|
||||
_type, n = v
|
||||
@@ -42,6 +46,26 @@ class Contacts(StoreDict):
|
||||
self.pop(k)
|
||||
self[n] = ('address', k)
|
||||
|
||||
def save(self):
|
||||
self.storage.put('contacts', dict(self))
|
||||
|
||||
def import_file(self, path):
|
||||
try:
|
||||
with open(path, 'r') as f:
|
||||
d = json.loads(f.read())
|
||||
except:
|
||||
return
|
||||
self.update(d)
|
||||
self.save()
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
dict.__setitem__(self, key, value)
|
||||
self.save()
|
||||
|
||||
def pop(self, key):
|
||||
if key in self.keys():
|
||||
dict.pop(self, key)
|
||||
self.save()
|
||||
|
||||
def resolve(self, k):
|
||||
if bitcoin.is_address(k):
|
||||
|
||||
@@ -457,18 +457,13 @@ def make_request(config, req):
|
||||
|
||||
class InvoiceStore(object):
|
||||
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
def __init__(self, storage):
|
||||
self.storage = storage
|
||||
self.invoices = {}
|
||||
self.load_invoices()
|
||||
d = self.storage.get('invoices', {})
|
||||
self.load(d)
|
||||
|
||||
def load_invoices(self):
|
||||
path = os.path.join(self.config.path, 'invoices')
|
||||
try:
|
||||
with open(path, 'r') as f:
|
||||
d = json.loads(f.read())
|
||||
except:
|
||||
return
|
||||
def load(self, d):
|
||||
for k, v in d.items():
|
||||
try:
|
||||
pr = PaymentRequest(v.get('hex').decode('hex'))
|
||||
@@ -478,6 +473,15 @@ class InvoiceStore(object):
|
||||
except:
|
||||
continue
|
||||
|
||||
def import_file(self, path):
|
||||
try:
|
||||
with open(path, 'r') as f:
|
||||
d = json.loads(f.read())
|
||||
self.load(d)
|
||||
except:
|
||||
return
|
||||
self.save()
|
||||
|
||||
def save(self):
|
||||
l = {}
|
||||
for k, pr in self.invoices.items():
|
||||
@@ -486,10 +490,7 @@ class InvoiceStore(object):
|
||||
'requestor': pr.requestor,
|
||||
'txid': pr.tx
|
||||
}
|
||||
path = os.path.join(self.config.path, 'invoices')
|
||||
with open(path, 'w') as f:
|
||||
s = json.dumps(l, indent=4, sort_keys=True)
|
||||
r = f.write(s)
|
||||
self.storage.put('invoices', l)
|
||||
|
||||
def get_status(self, key):
|
||||
pr = self.get(key)
|
||||
|
||||
31
lib/util.py
31
lib/util.py
@@ -622,37 +622,6 @@ class QueuePipe:
|
||||
|
||||
|
||||
|
||||
class StoreDict(dict):
|
||||
|
||||
def __init__(self, config, name):
|
||||
self.config = config
|
||||
self.path = os.path.join(self.config.path, name)
|
||||
self.load()
|
||||
|
||||
def load(self):
|
||||
try:
|
||||
with open(self.path, 'r') as f:
|
||||
self.update(json.loads(f.read()))
|
||||
except:
|
||||
pass
|
||||
|
||||
def save(self):
|
||||
with open(self.path, 'w') as f:
|
||||
s = json.dumps(self, indent=4, sort_keys=True)
|
||||
r = f.write(s)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
dict.__setitem__(self, key, value)
|
||||
self.save()
|
||||
|
||||
def pop(self, key):
|
||||
if key in self.keys():
|
||||
dict.pop(self, key)
|
||||
self.save()
|
||||
|
||||
|
||||
|
||||
|
||||
def check_www_dir(rdir):
|
||||
import urllib, urlparse, shutil, os
|
||||
if not os.path.exists(rdir):
|
||||
|
||||
@@ -62,6 +62,8 @@ from verifier import SPV
|
||||
from mnemonic import Mnemonic
|
||||
|
||||
import paymentrequest
|
||||
from paymentrequest import InvoiceStore
|
||||
from contacts import Contacts
|
||||
|
||||
|
||||
TX_STATUS = [
|
||||
@@ -127,6 +129,11 @@ class Abstract_Wallet(PrintError):
|
||||
if self.storage.get('wallet_type') is None:
|
||||
self.storage.put('wallet_type', self.wallet_type)
|
||||
|
||||
# invoices and contacts
|
||||
self.invoices = InvoiceStore(self.storage)
|
||||
self.contacts = Contacts(self.storage)
|
||||
|
||||
|
||||
def diagnostic_name(self):
|
||||
return self.basename()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user