1515from shamir_mnemonic import generate_mnemonics
1616
1717import hdwallet
18- import scrypt
18+ try :
19+ import scrypt
20+ except ImportError :
21+ scrypt = None
1922try :
2023 import eth_account
2124except ImportError :
@@ -78,7 +81,7 @@ class Account( hdwallet.HDWallet ):
7881 CRYPTOCURRENCIES = set ( CRYPTO_NAMES .values () )
7982
8083 ETHJS_ENCRYPT = set ( ('ETH' ,) ) # Can be encrypted w/ Ethereum JSON wallet
81- BIP38_ENCRYPT = CRYPTOCURRENCIES - ETHJS_ENCRYPT # Can be encrypted w/ BIP-38
84+ BIP38_ENCRYPT = CRYPTOCURRENCIES - ETHJS_ENCRYPT # Can be encrypted w/ BIP-38
8285
8386 CRYPTO_FORMAT = dict (
8487 ETH = "legacy" ,
@@ -225,7 +228,7 @@ def encrypted( self, passphrase ):
225228 encrypted JSON wallet standard, Bitcoin et.al. use BIP-38 encrypted private keys."""
226229 if self .crypto in self .ETHJS_ENCRYPT :
227230 if not eth_account :
228- raise NotImplementedError ( "The eth-account package is required to support Ethereum JSON wallet encryption" )
231+ raise NotImplementedError ( "The eth-account module is required to support Ethereum JSON wallet encryption; pip install slip39[wallet] " )
229232 wallet_dict = eth_account .Account .encrypt ( self .key , passphrase )
230233 return json .dumps ( wallet_dict , separators = (',' ,':' ) )
231234 return self .bip38 ( passphrase )
@@ -234,14 +237,16 @@ def from_encrypted( self, encrypted_privkey, passphrase, strict=True ):
234237 """Import the appropriately decrypted private key for this cryptocurrency."""
235238 if self .crypto in self .ETHJS_ENCRYPT :
236239 if not eth_account :
237- raise NotImplementedError ( "The eth-account package is required to support Ethereum JSON wallet decryption" )
240+ raise NotImplementedError ( "The eth-account module is required to support Ethereum JSON wallet decryption; pip install slip39[wallet] " )
238241 private_hex = bytes ( eth_account .Account .decrypt ( encrypted_privkey , passphrase )).hex ()
239242 self .from_private_key ( private_hex )
240243 return self
241244 return self .from_bip38 ( encrypted_privkey , passphrase = passphrase , strict = strict )
242245
243246 def bip38 ( self , passphrase , flagbyte = b'\xe0 ' ):
244247 """BIP-38 encrypt the private key"""
248+ if not scrypt :
249+ raise NotImplementedError ( "The scrypt module is required to support BIP-38 encryption; pip install slip39[wallet]" )
245250 if self .crypto not in self .BIP38_ENCRYPT :
246251 raise NotImplementedError ( f"{ self .crypto } does not support BIP-38 private key encryption" )
247252 private_hex = self .key
@@ -262,6 +267,8 @@ def bip38( self, passphrase, flagbyte=b'\xe0' ):
262267
263268 def from_bip38 ( self , encrypted_privkey , passphrase , strict = True ):
264269 """Bip-38 decrypt and import the private key."""
270+ if not scrypt :
271+ raise NotImplementedError ( "The scrypt module is required to support BIP-38 decryption; pip install slip39[wallet]" )
265272 if self .crypto not in self .BIP38_ENCRYPT :
266273 raise NotImplementedError ( f"{ self .crypto } does not support BIP-38 private key decryption" )
267274 # Decode the encrypted private key from base58, discarding the 4-byte base58 check suffix
@@ -290,7 +297,7 @@ def from_bip38( self, encrypted_privkey, passphrase, strict=True ):
290297 addr = self .legacy_address ().encode ( 'UTF-8' ) # Eg. b"184xW5g..."
291298 ahash_confirm = hashlib .sha256 ( hashlib .sha256 ( addr ).digest () ).digest ()[0 :4 ]
292299 if ahash_confirm != ahash :
293- warning = f"BIP-38 address hash verification failed ({ ahash_confirm .hex ()} != { ahash .hex ()} ); password may be incorrect."
300+ warning = f"BIP-38 address hash verification failed ({ ahash_confirm .hex ()} != { ahash .hex ()} ); password may be incorrect."
294301 if strict :
295302 raise AssertionError ( warning )
296303 else :
0 commit comments