/
bip39.py
executable file
·37 lines (28 loc) · 987 Bytes
/
bip39.py
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
#!./venv/bin/python
# We use BIP39 from electrum instead of finding a smaller lib, because:
# - we also have electrum_mnemonic.py, so we need electrum anyway for this
# - other libraries force enforce the checksum check
from electrum import keystore
import argparse
import sys
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--no-check', action='store_true', default=False)
args = arg_parser.parse_args()
lines = [x.strip() for x in sys.stdin.readlines()]
words = ""
passw = ""
if len(lines) == 2:
passw = lines[1]
if not (len(lines) in [1, 2]):
print("wrong input", file=sys.stderr)
sys.exit(1)
words = lines[0]
if not args.no_check:
(checksum_ok, wordlist_ok) = keystore.bip39_is_checksum_valid(words)
if not wordlist_ok:
print("Unknown words!", file=sys.stderr)
sys.exit(1)
if not checksum_ok:
print("Checksum NOT OK!", file=sys.stderr)
sys.exit(1)
print(keystore.bip39_to_seed(words, passw).hex())