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