mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-31 01:11:35 +00:00
trezor restore from seed
This commit is contained in:
parent
1f9598e1c7
commit
36a5e09532
1 changed files with 34 additions and 12 deletions
|
@ -99,22 +99,31 @@ class Plugin(BasePlugin):
|
||||||
@hook
|
@hook
|
||||||
def load_wallet(self, wallet):
|
def load_wallet(self, wallet):
|
||||||
self.wallet = wallet
|
self.wallet = wallet
|
||||||
|
if self.wallet.has_seed():
|
||||||
|
return
|
||||||
if self.trezor_is_connected():
|
if self.trezor_is_connected():
|
||||||
if not self.wallet.check_proper_device():
|
if not self.wallet.check_proper_device():
|
||||||
QMessageBox.information(self.window, _('Error'), _("This wallet does not match your Trezor device"), _('OK'))
|
QMessageBox.information(self.window, _('Error'), _("This wallet does not match your Trezor device"), _('OK'))
|
||||||
|
self.wallet.force_watching_only = True
|
||||||
else:
|
else:
|
||||||
QMessageBox.information(self.window, _('Error'), _("Trezor device not detected.\nContinuing in watching-only mode."), _('OK'))
|
QMessageBox.information(self.window, _('Error'), _("Trezor device not detected.\nContinuing in watching-only mode."), _('OK'))
|
||||||
|
self.wallet.force_watching_only = True
|
||||||
|
|
||||||
@hook
|
@hook
|
||||||
def installwizard_restore(self, wizard, storage):
|
def installwizard_restore(self, wizard, storage):
|
||||||
if storage.get('wallet_type') != 'trezor':
|
if storage.get('wallet_type') != 'trezor':
|
||||||
return
|
return
|
||||||
wallet = TrezorWallet(storage)
|
seed = wizard.enter_seed_dialog("Enter your Trezor seed", None, func=lambda x:True)
|
||||||
try:
|
if not seed:
|
||||||
wallet.create_main_account(None)
|
|
||||||
except BaseException as e:
|
|
||||||
QMessageBox.information(None, _('Error'), str(e), _('OK'))
|
|
||||||
return
|
return
|
||||||
|
wallet = TrezorWallet(storage)
|
||||||
|
self.wallet = wallet
|
||||||
|
password = wizard.password_dialog()
|
||||||
|
wallet.add_seed(seed, password)
|
||||||
|
wallet.add_cosigner_seed(' '.join(seed.split()), 'x/', password)
|
||||||
|
wallet.create_main_account(password)
|
||||||
|
# disable trezor plugin
|
||||||
|
self.set_enabled(False)
|
||||||
return wallet
|
return wallet
|
||||||
|
|
||||||
@hook
|
@hook
|
||||||
|
@ -161,6 +170,8 @@ class Plugin(BasePlugin):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
from electrum.wallet import pw_decode, bip32_private_derivation, bip32_root
|
||||||
|
|
||||||
class TrezorWallet(BIP32_HD_Wallet):
|
class TrezorWallet(BIP32_HD_Wallet):
|
||||||
wallet_type = 'trezor'
|
wallet_type = 'trezor'
|
||||||
root_derivation = "m/44'/0'"
|
root_derivation = "m/44'/0'"
|
||||||
|
@ -171,6 +182,7 @@ class TrezorWallet(BIP32_HD_Wallet):
|
||||||
self.client = None
|
self.client = None
|
||||||
self.mpk = None
|
self.mpk = None
|
||||||
self.device_checked = False
|
self.device_checked = False
|
||||||
|
self.force_watching_only = False
|
||||||
|
|
||||||
def get_action(self):
|
def get_action(self):
|
||||||
if not self.accounts:
|
if not self.accounts:
|
||||||
|
@ -188,11 +200,8 @@ class TrezorWallet(BIP32_HD_Wallet):
|
||||||
def can_change_password(self):
|
def can_change_password(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def has_seed(self):
|
|
||||||
return False
|
|
||||||
|
|
||||||
def is_watching_only(self):
|
def is_watching_only(self):
|
||||||
return False
|
return self.force_watching_only
|
||||||
|
|
||||||
def get_client(self):
|
def get_client(self):
|
||||||
if not TREZOR:
|
if not TREZOR:
|
||||||
|
@ -221,10 +230,23 @@ class TrezorWallet(BIP32_HD_Wallet):
|
||||||
def create_main_account(self, password):
|
def create_main_account(self, password):
|
||||||
self.create_account('Main account', None) #name, empty password
|
self.create_account('Main account', None) #name, empty password
|
||||||
|
|
||||||
|
def mnemonic_to_seed(self, mnemonic, passphrase):
|
||||||
|
# trezor uses bip39
|
||||||
|
import pbkdf2, hashlib, hmac
|
||||||
|
PBKDF2_ROUNDS = 2048
|
||||||
|
mnemonic = ' '.join(mnemonic.split())
|
||||||
|
return pbkdf2.PBKDF2(mnemonic, 'mnemonic' + passphrase, iterations = PBKDF2_ROUNDS, macmodule = hmac, digestmodule = hashlib.sha512).read(64)
|
||||||
|
|
||||||
def derive_xkeys(self, root, derivation, password):
|
def derive_xkeys(self, root, derivation, password):
|
||||||
derivation = derivation.replace(self.root_name,"44'/0'/")
|
x = self.master_private_keys.get(root)
|
||||||
xpub = self.get_public_key(derivation)
|
if x:
|
||||||
return xpub, None
|
root_xprv = pw_decode(x, password)
|
||||||
|
xprv, xpub = bip32_private_derivation(root_xprv, root, derivation)
|
||||||
|
return xpub, xprv
|
||||||
|
else:
|
||||||
|
derivation = derivation.replace(self.root_name,"44'/0'/")
|
||||||
|
xpub = self.get_public_key(derivation)
|
||||||
|
return xpub, None
|
||||||
|
|
||||||
def get_public_key(self, bip32_path):
|
def get_public_key(self, bip32_path):
|
||||||
address_n = self.get_client().expand_path(bip32_path)
|
address_n = self.get_client().expand_path(bip32_path)
|
||||||
|
|
Loading…
Add table
Reference in a new issue