Make class methods static on Wallet

This commit is contained in:
Neil Booth 2015-12-31 16:05:05 +09:00
parent 441b695815
commit e6dbe621c6
1 changed files with 66 additions and 70 deletions

View File

@ -1945,19 +1945,12 @@ class Wallet(object):
return wallet
@classmethod
def is_seed(self, seed):
if not seed:
return False
elif is_old_seed(seed):
return True
elif is_new_seed(seed):
return True
else:
return False
@staticmethod
def is_seed(seed):
return is_old_seed(seed) or is_new_seed(seed)
@classmethod
def is_old_mpk(self, mpk):
@staticmethod
def is_old_mpk(mpk):
try:
int(mpk, 16)
assert len(mpk) == 128
@ -1965,8 +1958,8 @@ class Wallet(object):
except:
return False
@classmethod
def is_xpub(self, text):
@staticmethod
def is_xpub(text):
try:
assert text[0:4] == 'xpub'
deserialize_xkey(text)
@ -1974,8 +1967,8 @@ class Wallet(object):
except:
return False
@classmethod
def is_xprv(self, text):
@staticmethod
def is_xprv(text):
try:
assert text[0:4] == 'xprv'
deserialize_xkey(text)
@ -1983,26 +1976,29 @@ class Wallet(object):
except:
return False
@classmethod
def is_address(self, text):
if not text:
return False
for x in text.split():
if not bitcoin.is_address(x):
return False
return True
@staticmethod
def is_address(text):
parts = text.split()
return bool(parts) and all(bitcoin.is_address(x) for x in parts)
@classmethod
def is_private_key(self, text):
if not text:
return False
for x in text.split():
if not bitcoin.is_private_key(x):
return False
return True
@staticmethod
def is_private_key(text):
parts = text.split()
return bool(parts) and all(bitcoin.is_private_key(x) for x in parts)
@classmethod
def from_seed(self, seed, password, storage):
@staticmethod
def is_any(text):
return (Wallet.is_seed(text) or Wallet.is_old_mpk(text)
or Wallet.is_xprv(text) or Wallet.is_xpub(text)
or Wallet.is_address(text) or Wallet.is_private_key(text))
@staticmethod
def should_encrypt(text):
return (Wallet.is_seed(text) or Wallet.is_xprv(text)
or Wallet.is_private_key(text))
@staticmethod
def from_seed(seed, password, storage):
if is_old_seed(seed):
klass = OldWallet
elif is_new_seed(seed):
@ -2013,80 +2009,80 @@ class Wallet(object):
w.create_main_account(password)
return w
@classmethod
def from_address(self, text, storage):
@staticmethod
def from_address(text, storage):
w = Imported_Wallet(storage)
for x in text.split():
w.accounts[IMPORTED_ACCOUNT].add(x, None, None, None)
w.save_accounts()
return w
@classmethod
def from_private_key(self, text, password, storage):
@staticmethod
def from_private_key(text, password, storage):
w = Imported_Wallet(storage)
w.update_password(None, password)
for x in text.split():
w.import_key(x, password)
return w
@classmethod
def from_old_mpk(self, mpk, storage):
@staticmethod
def from_old_mpk(mpk, storage):
w = OldWallet(storage)
w.seed = ''
w.create_watching_only_wallet(mpk)
return w
@classmethod
def from_xpub(self, xpub, storage):
@staticmethod
def from_xpub(xpub, storage):
w = BIP32_Simple_Wallet(storage)
w.create_xpub_wallet(xpub)
return w
@classmethod
def from_xprv(self, xprv, password, storage):
@staticmethod
def from_xprv(xprv, password, storage):
w = BIP32_Simple_Wallet(storage)
w.create_xprv_wallet(xprv, password)
return w
@classmethod
def from_multisig(klass, key_list, password, storage, wallet_type):
@staticmethod
def from_multisig(key_list, password, storage, wallet_type):
storage.put('wallet_type', wallet_type)
self = Multisig_Wallet(storage)
key_list = sorted(key_list, key = lambda x: klass.is_xpub(x))
wallet = Multisig_Wallet(storage)
key_list = sorted(key_list, key = lambda x: Wallet.is_xpub(x))
for i, text in enumerate(key_list):
assert klass.is_seed(text) or klass.is_xprv(text) or klass.is_xpub(text)
assert Wallet.is_seed(text) or Wallet.is_xprv(text) or Wallet.is_xpub(text)
name = "x%d/"%(i+1)
if klass.is_xprv(text):
if Wallet.is_xprv(text):
xpub = bitcoin.xpub_from_xprv(text)
self.add_master_public_key(name, xpub)
self.add_master_private_key(name, text, password)
elif klass.is_xpub(text):
self.add_master_public_key(name, text)
elif klass.is_seed(text):
wallet.add_master_public_key(name, xpub)
wallet.add_master_private_key(name, text, password)
elif Wallet.is_xpub(text):
wallet.add_master_public_key(name, text)
elif Wallet.is_seed(text):
if name == 'x1/':
self.add_seed(text, password)
self.create_master_keys(password)
wallet.add_seed(text, password)
wallet.create_master_keys(password)
else:
self.add_cosigner_seed(text, name, password)
self.use_encryption = (password != None)
self.storage.put('use_encryption', self.use_encryption)
self.create_main_account(password)
return self
wallet.add_cosigner_seed(text, name, password)
wallet.use_encryption = (password != None)
wallet.storage.put('use_encryption', wallet.use_encryption)
wallet.create_main_account(password)
return wallet
@classmethod
def from_text(klass, text, password, storage):
@staticmethod
def from_text(text, password, storage):
if Wallet.is_xprv(text):
wallet = klass.from_xprv(text, password, storage)
wallet = Wallet.from_xprv(text, password, storage)
elif Wallet.is_old_mpk(text):
wallet = klass.from_old_mpk(text, storage)
wallet = Wallet.from_old_mpk(text, storage)
elif Wallet.is_xpub(text):
wallet = klass.from_xpub(text, storage)
wallet = Wallet.from_xpub(text, storage)
elif Wallet.is_address(text):
wallet = klass.from_address(text, storage)
wallet = Wallet.from_address(text, storage)
elif Wallet.is_private_key(text):
wallet = klass.from_private_key(text, password, storage)
wallet = Wallet.from_private_key(text, password, storage)
elif Wallet.is_seed(text):
wallet = klass.from_seed(text, password, storage)
wallet = Wallet.from_seed(text, password, storage)
else:
raise BaseException('Invalid seedphrase or key')
return wallet