Skip to content

Commit

Permalink
application 'mnemonic' (not 'words') for bip39 from bip85
Browse files Browse the repository at this point in the history
  • Loading branch information
akarve committed Jun 5, 2024
1 parent 8241a9e commit 272b553
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/bipsea/bip85.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dice": "89101'",
"drng": "0'",
"hex": "128169'",
"words": "39'",
"mnemonic": "39'",
"wif": "2'",
"xprv": "32'",
}
Expand Down Expand Up @@ -69,7 +69,7 @@ def apply_85(derived_key: ExtendedKey, path: str) -> Dict[str, Union[bytes, str]

entropy = to_entropy(derived_key.data[1:])

if app == APPLICATIONS["words"]:
if app == APPLICATIONS["mnemonic"]:
language_index, n_words = indexes[:2]
n_words = int(n_words[:-1]) # chop ' from hardened derivation
if n_words not in N_WORDS_META.keys():
Expand Down
20 changes: 10 additions & 10 deletions src/bipsea/bipsea.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ def bip39_cmd(from_, to, input, number, passphrase, pretty):
if (from_ == "random" and input) or (from_ != "random" and not input):
raise click.BadOptionUsage(
option_name="--from",
message="`--from words` requires `--input STRING`, `--from rand` forbids `--input`",
message="`--from [anything but 'rand']` requires `--input`, `--from rand` forbids `--input`",
)
language = ISO_TO_LANGUAGE.get(from_)
if language or from_ == "any":
if to == "words":
if to == "mnemonic":
raise click.BadOptionUsage(
option_name="--to",
message="`--to words` incompatible with `--from LANGUAGE`",
message="f`--to {from_}` incompatible with `--from [anything but 'rand']`",
)
words = normalize_list(re.split(r"\s+", input), lower=True)
if language and not verify_seed_words(words, language):
raise click.BadParameter(
f"Unexpected {language} words from `--input` ({' '.join(words)}) or bad checksum.",
f"--input mnemonic not in {from_} or has bad checksum.",
param_hint="--input",
)
if from_ == "any":
Expand Down Expand Up @@ -176,15 +176,15 @@ def bip39_cmd(from_, to, input, number, passphrase, pretty):
@click.option(
"-a",
"--application",
default="words",
default="mnemonic",
required=True,
type=click.Choice(APPLICATIONS.keys()),
)
@click.option(
"-n",
"--number",
type=int,
help="Length of output in bytes, chars, or words, depending on the application.",
help="Length of output in bytes, chars, or words, depending on --application.",
)
@click.option(
"-i",
Expand All @@ -209,7 +209,7 @@ def bip39_cmd(from_, to, input, number, passphrase, pretty):
"-t",
"--to",
type=click.Choice(ENTROPY_TO_VALUES),
help="Mnemonic language for `--application words`.",
help="Output language for `--application mnemonic`.",
)
def bip85_cmd(application, number, index, special, input, to):
if not input:
Expand Down Expand Up @@ -250,15 +250,15 @@ def bip85_cmd(application, number, index, special, input, to):
path += f"/{app_code}"

if to:
if application != "words":
if application != "mnemonic":
raise click.BadOptionUsage(
option_name="--to",
message="--to requires `--application words`",
message="--to requires `--application mnemonic`",
)
else:
to = "eng"

if application == "words":
if application == "mnemonic":
language = ISO_TO_LANGUAGE[to]
code_85 = next(i for i, l in INDEX_TO_LANGUAGE.items() if l == language)
path += f"/{code_85}/{number}'/{index}'"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_seed_option_sensitivity(runner, language, vectors):
result = runner.invoke(cli, cmd)
if suffix == ".":
assert result.exit_code != 0
assert "Unexpected" in result.output
assert lang_code in result.output
else:
assert result.exit_code == 0
result_xprv = result.output.strip().split("\n")[-1]
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_entropy_bip39(runner, vector):
xprv = vector["master"]
n_words = vector["mnemonic_length"]
result = runner.invoke(
cli, ["entropy", "-a", "words", "--input", xprv, "-n", n_words]
cli, ["entropy", "-a", "mnemonic", "--input", xprv, "-n", n_words]
)
assert result.exit_code == 0
words = result.output.strip()
Expand All @@ -210,7 +210,7 @@ def test_entropy_bip39(runner, vector):
def test_entropy_bip39_languages(runner, iso):
xprv = MNEMONIC_12["xprv"]
result = runner.invoke(
cli, ["entropy", "-a", "words", "--input", xprv, "-n", 12, "-t", iso]
cli, ["entropy", "-a", "mnemonic", "--input", xprv, "-n", 12, "-t", iso]
)
assert result.exit_code == 0
words = result.output.strip()
Expand Down

0 comments on commit 272b553

Please sign in to comment.