Skip to content

Commit b2204ef

Browse files
committed
Correct handling of cryptopaths in gui
1 parent 058416c commit b2204ef

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

slip39/api.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,19 @@ def supported( cls, crypto ):
411411
for it, or raises an a ValueError. Eg. "ETH"/"Ethereum" --> "ETH"
412412
413413
"""
414-
validated = cls.CRYPTO_NAMES.get(
415-
crypto.lower(),
416-
crypto.upper() if crypto.upper() in cls.CRYPTO_SYMBOLS else None
417-
)
418-
if validated:
419-
return validated
414+
try:
415+
validated = cls.CRYPTO_NAMES.get(
416+
crypto.lower(),
417+
crypto.upper() if crypto.upper() in cls.CRYPTO_SYMBOLS else None
418+
)
419+
if validated:
420+
return validated
421+
except Exception as exc:
422+
validated = exc
423+
raise
424+
finally:
425+
log.debug( f"Validating {crypto!r} yields: {validated!r}" )
426+
420427
raise ValueError( f"{crypto} not presently supported; specify {commas( cls.CRYPTOCURRENCIES )}" )
421428

422429
def __str__( self ):
@@ -865,39 +872,38 @@ def path_hardened( path ):
865872

866873

867874
def cryptopaths_parser(
868-
cryptocurrency,
875+
cryptocurrency, # Or a cryptopaths list, eg. ["XRP",(ETH,"m/.."),(BTC,NONE,"segwit")]
869876
edit = None,
870877
hardened_defaults = False,
871878
format = None,
872879
):
873-
"""Generate a standard cryptopaths list, from the given sequnce of (<crypto>,<paths>) or
874-
"<crypto>[:<paths>]" cryptocurrencies (default: CRYPTO_PATHS, optionally w/ only the hardened
875-
portion of the path, eg. omitting the trailing ../0/0).
880+
"""Generate a standard cryptopaths list, from the given sequnce of "<crypto>",
881+
(<crypto>,<paths>), (<crypto>,<paths>,<format>), or "<crypto>[:<paths>[:<format>:]"
882+
cryptocurrencies (default: CRYPTO_PATHS, optionally w/ only the hardened portion of the path,
883+
eg. omitting the trailing ../0/0).
876884
877885
Adjusts the provided derivation paths by an optional eg. "../-" path adjustment.
878886
879887
A non-default format may be specified, which may change the default HD derivation path. This
880888
must also be passed back, as it also affects the crypto's account's address format.
881889
882890
"""
883-
cryptopaths = []
884891
for crypto in cryptocurrency or CRYPTO_PATHS:
885-
try:
886-
if type(crypto) is str:
887-
crypto,paths = crypto.split( ':' ) # A sequence of str
888-
else:
889-
crypto,paths = crypto # A sequence of tuples
890-
except ValueError:
891-
crypto,paths = crypto,None
892-
crypto = Account.supported( crypto )
893-
if paths is None:
894-
paths = Account.path_default( crypto, format )
892+
if type(crypto) is str:
893+
crypto = crypto.split( ':' )
894+
895+
cry,*pth = crypto
896+
pth,*fmt = pth or (None,)
897+
fmt, = fmt or (None,)
898+
899+
cry = Account.supported( cry )
900+
if not pth:
901+
pth = Account.path_default( cry, fmt or format )
895902
if hardened_defaults:
896-
paths,_ = path_hardened( paths )
903+
pth,_ = path_hardened( pth )
897904
if edit:
898-
paths = path_edit( paths, edit )
899-
cryptopaths.append( (crypto,paths,format) )
900-
return cryptopaths
905+
pth = path_edit( pth, edit )
906+
yield (cry,pth,fmt)
901907

902908

903909
def random_secret(
@@ -1217,9 +1223,9 @@ def accountgroups(
12171223
master_secret: Union[str,bytes],
12181224
cryptopaths: Optional[Sequence[Union[str,Tuple[str,str],Tuple[str,str,str]]]] = None, # default: ETH, BTC at default path, format
12191225
allow_unbounded: bool = True,
1220-
passphrase: Optional[Union[bytes,str]] = None, # If mnemonic(s) provided, then passphrase/using_bip39 optional
1226+
passphrase: Optional[Union[bytes,str]] = None, # If mnemonic(s) provided, then passphrase/using_bip39 optional
12211227
using_bip39: bool = False,
1222-
format: Optional[str] = None,
1228+
format: Optional[str] = None, # If the default format for every cryptopath isn't desired
12231229
edit: Optional[str] = None,
12241230
hardened_defaults: bool = False,
12251231
) -> Sequence[Sequence[Account]]:

slip39/generator/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ def healthy_reset( file ): # noaq: F811
290290
# --xpub --crypto BTC
291291
#
292292
# is specified, emit BTC "zpub..." xpubkeys at m/84'/0'/0', m/84'/0'/1', ...
293-
cryptopaths = cryptopaths_parser(
293+
cryptopaths = list( cryptopaths_parser(
294294
args.cryptocurrency,
295295
edit = args.path,
296296
hardened_defaults = args.xpub,
297297
format = format,
298-
)
298+
))
299299

300300
#
301301
# Set up serial device, if desired. We will attempt to send each record using hardware flow

slip39/gui/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ def deficiency( *deficiencies ):
14221422
# See what Cryptocurrencies and Paper Wallets are desired, by what checkboxes are clicked.
14231423
# If none are, then the default (BTC, ETH) will be defaulted.
14241424
cs_rec = set( c for c in Account.CRYPTOCURRENCIES if values.get( f"-CRYPTO-{c}-" ) )
1425-
cps_rec = cryptopaths_parser( cryptocurrency=cs_rec, edit=edit )
1425+
cps_rec = list( cryptopaths_parser( cryptocurrency=cs_rec, edit=edit ))
14261426
if cs_rec != cryptocurrency or cps_rec != cryptopaths:
14271427
log.info( "Updating SLIP-39 due to changing Cryptocurrencies" )
14281428
cryptocurrency = cs_rec

slip39/layout/pdf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def write_pdfs(
417417

418418
group_threshold = int( group_threshold ) if group_threshold else math.ceil( len( groups ) * GROUP_THRESHOLD_RATIO )
419419

420-
cryptopaths = cryptopaths_parser( cryptocurrency, edit=edit )
420+
cryptopaths = list( cryptopaths_parser( cryptocurrency, edit=edit ))
421421

422422
# If account details not provided in names, generate them. If using_bip39 is specified, this is
423423
# where we use BIP-39 Seed generation to produce the wallet Seed, instead of SLIP-39 which uses
@@ -615,7 +615,7 @@ def write_pdfs(
615615
assert eth_account, \
616616
"The optional eth-account package is required to support output of encrypted JSON wallets\n" \
617617
" python3 -m pip install eth-account"
618-
assert any( 'ETH' == crypto for crypto,paths in cryptopaths ), \
618+
assert any( 'ETH' == crypto for crypto,*_ in cryptopaths ), \
619619
"--json is only valid if '--crypto ETH' wallets are specified"
620620

621621
for eth in (

0 commit comments

Comments
 (0)