forked from trezor/python-mnemonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_vectors.py
executable file
·52 lines (40 loc) · 1.42 KB
/
generate_vectors.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
import json
from binascii import hexlify, unhexlify
from random import choice, seed
from bip32utils import BIP32Key
from mnemonic import Mnemonic
def b2h(b):
h = hexlify(b)
return h.decode("utf8")
def process(data, lst):
code = mnemo.to_mnemonic(unhexlify(data))
seed = Mnemonic.to_seed(code, passphrase="TREZOR")
xprv = BIP32Key.fromEntropy(seed).ExtendedKey()
seed = b2h(seed)
print("input : %s (%d bits)" % (data, len(data) * 4))
print("mnemonic : %s (%d words)" % (code, len(code.split(" "))))
print("seed : %s (%d bits)" % (seed, len(seed) * 4))
print("xprv : %s" % xprv)
print()
lst.append((data, code, seed, xprv))
if __name__ == "__main__":
out = {}
seed(1337)
for lang in ["english"]: # Mnemonic.list_languages():
mnemo = Mnemonic(lang)
out[lang] = []
# Generate corner cases
data = []
for l in range(16, 32 + 1, 8):
for b in ["00", "7f", "80", "ff"]:
process(b * l, out[lang])
# Generate random seeds
for i in range(12):
data = "".join(chr(choice(range(0, 256))) for _ in range(8 * (i % 3 + 2)))
data = data.encode("latin1")
process(b2h(data), out[lang])
with open("vectors.json", "w") as f:
json.dump(
out, f, sort_keys=True, indent=4, separators=(",", ": "), ensure_ascii=False
)