-
-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathcli.py
More file actions
168 lines (149 loc) · 4.41 KB
/
cli.py
File metadata and controls
168 lines (149 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import sys
import click
from mnemonic import Mnemonic
from mnemonic.mnemonic import ConfigurationError
@click.group()
def cli() -> None:
"""BIP-39 mnemonic phrase generator and validator."""
pass
@cli.command()
@click.option(
"-l",
"--language",
default="english",
type=str,
help="Language for the mnemonic wordlist.",
)
@click.option(
"-s",
"--strength",
default="128",
type=click.Choice(["128", "160", "192", "224", "256"]),
help="Entropy strength in bits.",
)
@click.option(
"-p",
"--passphrase",
default="",
envvar="MNEMONIC_PASSPHRASE",
type=str,
help="Passphrase for seed derivation. Can also be set via MNEMONIC_PASSPHRASE env var.",
)
@click.option(
"-P",
"--prompt-passphrase",
is_flag=True,
default=False,
help="Prompt for passphrase with hidden input (secure).",
)
@click.option(
"--hide-seed",
is_flag=True,
default=False,
help="Do not display the derived seed.",
)
def create(
language: str,
passphrase: str,
prompt_passphrase: bool,
strength: str,
hide_seed: bool,
) -> None:
"""Generate a new mnemonic phrase and its derived seed."""
if prompt_passphrase:
if passphrase:
click.secho(
"Warning: --prompt-passphrase overrides -p/MNEMONIC_PASSPHRASE.",
fg="yellow",
err=True,
)
passphrase = click.prompt("Passphrase", default="", hide_input=True)
try:
mnemo = Mnemonic(language)
words = mnemo.generate(int(strength))
click.echo(f"Mnemonic: {words}")
if not hide_seed:
seed = mnemo.to_seed(words, passphrase)
click.echo(f"Seed: {seed.hex()}")
except ConfigurationError as e:
raise click.ClickException(str(e))
@cli.command()
@click.option(
"-l",
"--language",
default=None,
type=str,
help="Language for the mnemonic wordlist. Auto-detected if not specified.",
)
@click.argument("words", nargs=-1)
def check(language: str | None, words: tuple[str, ...]) -> None:
"""Validate a mnemonic phrase's checksum.
WORDS can be provided as arguments or piped via stdin.
"""
if words:
mnemonic = " ".join(words)
else:
mnemonic = sys.stdin.read().strip()
if not mnemonic:
raise click.ClickException("No mnemonic provided.")
try:
if language is None:
language = Mnemonic.detect_language(mnemonic)
mnemo = Mnemonic(language)
if mnemo.check(mnemonic):
click.secho("Valid mnemonic.", fg="green")
else:
raise click.ClickException("Invalid mnemonic checksum.")
except ConfigurationError as e:
raise click.ClickException(str(e))
except (ValueError, LookupError) as e:
raise click.ClickException(str(e))
@cli.command("to-seed")
@click.option(
"-p",
"--passphrase",
default="",
envvar="MNEMONIC_PASSPHRASE",
type=str,
help="Passphrase for seed derivation. Can also be set via MNEMONIC_PASSPHRASE env var.",
)
@click.option(
"-P",
"--prompt-passphrase",
is_flag=True,
default=False,
help="Prompt for passphrase with hidden input (secure).",
)
@click.argument("words", nargs=-1)
def to_seed(passphrase: str, prompt_passphrase: bool, words: tuple[str, ...]) -> None:
"""Derive a seed from a mnemonic phrase.
WORDS can be provided as arguments or piped via stdin.
Outputs the 64-byte seed in hexadecimal format.
"""
if words:
mnemonic = " ".join(words)
else:
mnemonic = sys.stdin.read().strip()
if not mnemonic:
raise click.ClickException("No mnemonic provided.")
if prompt_passphrase:
if passphrase:
click.secho(
"Warning: --prompt-passphrase overrides -p/MNEMONIC_PASSPHRASE.",
fg="yellow",
err=True,
)
passphrase = click.prompt("Passphrase", default="", hide_input=True)
try:
language = Mnemonic.detect_language(mnemonic)
mnemo = Mnemonic(language)
if not mnemo.check(mnemonic):
raise click.ClickException("Invalid mnemonic checksum.")
seed = mnemo.to_seed(mnemonic, passphrase)
click.echo(seed.hex())
except ConfigurationError as e:
raise click.ClickException(str(e))
except (ValueError, LookupError) as e:
raise click.ClickException(str(e))
if __name__ == "__main__":
cli()