version 0.2.0, new lib for bitcoin ecc, dont display or track notify errors, dont reload again within 1 sec, null peer ip fix, signingmoved to ContentManager, content.json include support, content.json multisig ready, content.json proper bitcoincore compatible signing, content.json include permissions, multithreaded publish, publish timeout 60s, no exception on invalid bitcoin address, testcase for new lib, bip32 based persite privatekey generation, multiuser ready, simple json database query command, websocket api fileGet, wrapper loading title stuck bugfix
2015-02-09 02:09:02 +01:00
|
|
|
import logging, json, time
|
|
|
|
from Crypt import CryptBitcoin
|
|
|
|
|
|
|
|
class User:
|
|
|
|
def __init__(self, master_address=None):
|
|
|
|
if master_address:
|
|
|
|
self.master_address = master_address
|
|
|
|
self.master_seed = None
|
|
|
|
else:
|
|
|
|
self.master_seed = CryptBitcoin.newSeed()
|
|
|
|
self.master_address = CryptBitcoin.privatekeyToAddress(self.master_seed)
|
|
|
|
self.sites = {}
|
|
|
|
self.log = logging.getLogger("User:%s" % self.master_address)
|
|
|
|
|
|
|
|
|
|
|
|
# Save to data/users.json
|
|
|
|
def save(self):
|
|
|
|
users = json.load(open("data/users.json"))
|
|
|
|
if not self.master_address in users: users[self.master_address] = {} # Create if not exits
|
|
|
|
|
|
|
|
user_data = users[self.master_address]
|
|
|
|
if self.master_seed: user_data["master_seed"] = self.master_seed
|
|
|
|
user_data["sites"] = self.sites
|
|
|
|
open("data/users.json", "w").write(json.dumps(users, indent=2, sort_keys=True))
|
|
|
|
self.log.debug("Saved")
|
|
|
|
|
|
|
|
|
2015-02-17 01:48:15 +01:00
|
|
|
# Get user site data
|
|
|
|
# Return: {"auth_address": "xxx", "auth_privatekey": "xxx"}
|
|
|
|
def getSiteData(self, address):
|
version 0.2.0, new lib for bitcoin ecc, dont display or track notify errors, dont reload again within 1 sec, null peer ip fix, signingmoved to ContentManager, content.json include support, content.json multisig ready, content.json proper bitcoincore compatible signing, content.json include permissions, multithreaded publish, publish timeout 60s, no exception on invalid bitcoin address, testcase for new lib, bip32 based persite privatekey generation, multiuser ready, simple json database query command, websocket api fileGet, wrapper loading title stuck bugfix
2015-02-09 02:09:02 +01:00
|
|
|
if not address in self.sites: # Genreate new BIP32 child key based on site address
|
|
|
|
s = time.time()
|
|
|
|
address_id = int(address.encode("hex"), 16) # Convert site address to int
|
|
|
|
auth_privatekey = CryptBitcoin.hdPrivatekey(self.master_seed, address_id)
|
|
|
|
self.sites[address] = {
|
|
|
|
"auth_address": CryptBitcoin.privatekeyToAddress(auth_privatekey),
|
|
|
|
"auth_privatekey": auth_privatekey
|
|
|
|
}
|
|
|
|
self.save()
|
|
|
|
self.log.debug("Added new site: %s in %.3fs" % (address, time.time()-s))
|
2015-02-17 01:48:15 +01:00
|
|
|
return self.sites[address]
|
|
|
|
|
version 0.2.0, new lib for bitcoin ecc, dont display or track notify errors, dont reload again within 1 sec, null peer ip fix, signingmoved to ContentManager, content.json include support, content.json multisig ready, content.json proper bitcoincore compatible signing, content.json include permissions, multithreaded publish, publish timeout 60s, no exception on invalid bitcoin address, testcase for new lib, bip32 based persite privatekey generation, multiuser ready, simple json database query command, websocket api fileGet, wrapper loading title stuck bugfix
2015-02-09 02:09:02 +01:00
|
|
|
|
2015-02-17 01:48:15 +01:00
|
|
|
# Get BIP32 address from site address
|
|
|
|
# Return: BIP32 auth address
|
|
|
|
def getAuthAddress(self, address):
|
|
|
|
return self.getSiteData(address)["auth_address"]
|
version 0.2.0, new lib for bitcoin ecc, dont display or track notify errors, dont reload again within 1 sec, null peer ip fix, signingmoved to ContentManager, content.json include support, content.json multisig ready, content.json proper bitcoincore compatible signing, content.json include permissions, multithreaded publish, publish timeout 60s, no exception on invalid bitcoin address, testcase for new lib, bip32 based persite privatekey generation, multiuser ready, simple json database query command, websocket api fileGet, wrapper loading title stuck bugfix
2015-02-09 02:09:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def getAuthPrivatekey(self, address):
|
2015-02-17 01:48:15 +01:00
|
|
|
return self.getSiteData(address)["auth_privatekey"]
|
version 0.2.0, new lib for bitcoin ecc, dont display or track notify errors, dont reload again within 1 sec, null peer ip fix, signingmoved to ContentManager, content.json include support, content.json multisig ready, content.json proper bitcoincore compatible signing, content.json include permissions, multithreaded publish, publish timeout 60s, no exception on invalid bitcoin address, testcase for new lib, bip32 based persite privatekey generation, multiuser ready, simple json database query command, websocket api fileGet, wrapper loading title stuck bugfix
2015-02-09 02:09:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Set user attributes from dict
|
|
|
|
def setData(self, data):
|
|
|
|
for key, val in data.items():
|
|
|
|
setattr(self, key, val)
|
2015-02-17 01:48:15 +01:00
|
|
|
|