-
Notifications
You must be signed in to change notification settings - Fork 7
/
mnemonics_utils.py
136 lines (105 loc) · 4.65 KB
/
mnemonics_utils.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
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
# -*- coding: utf-8 -*-
import sys, mnemonic, bip32utils
from six.moves import input
from iota.crypto.kerl import Kerl, conv
#===============================================================================
def MnemonicsToIotaSeed(recovery_words,
passphrase='',
bip44_account=0x00000000,
bip44_page_index=0x00000000):
"""Recover an IOTA seed from the ledger Nano S recovery phrase
Keyword arguments:
recovery_words -- a list of 24 words (your ledger recovery phrase)
passphrase -- a string containing the passphrase (only if set in ledger, not your pin number!)
bip44_account -- an integer containing BIP44 path 'Account'
bip44_page_index -- an integer containing BIP44 path 'Page index'
"""
master_seed = mnemonic.Mnemonic.to_seed(mnemonic=' '.join(recovery_words), passphrase=passphrase)
bip32_root_key = bip32utils.BIP32Key.fromEntropy(master_seed)
bip44_purpose_key = bip32_root_key.ChildKey(0x8000002C) # Purpose
bip44_coin_type_key = bip44_purpose_key.ChildKey(0x8000107A) # CoinType
bip44_account_key = bip44_coin_type_key.ChildKey(0x80000000+bip44_account) # Account
bip44_page_index_key = bip44_account_key.ChildKey(0x80000000+bip44_page_index) # Page index
if (sys.version_info.major >= 3):
priv_key = bytearray(bip44_page_index_key.PrivateKey())
chain_code = bytearray(bip44_page_index_key.C)
else:
priv_key = bytearray.fromhex(bip44_page_index_key.PrivateKey().encode('hex'))
chain_code = bytearray.fromhex(bip44_page_index_key.C.encode('hex'))
trits_out = []
kerl = Kerl()
kerl.k.update(priv_key[0:32] + chain_code[0:16] + priv_key[16:32] + chain_code[0:32])
kerl.squeeze(trits_out)
iota_seed = conv.trits_to_trytes(trits_out)
return iota_seed
#===============================================================================
def InputRecoveryWords():
print("\nPlease enter your Ledger Nano S recovery phrase now:")
m = mnemonic.Mnemonic(language='english')
recovery_words = []
for i in range(24):
while True:
try:
word = input(" word #%d: " % (i+1))
except KeyboardInterrupt:
return None
if word == '':
print("\nERROR: word #%d was empty!" % (i+1))
continue
try:
m.wordlist.index(word)
except ValueError:
print("\nERROR: word '%s' is not in the BIP44 words list!" % (word))
continue
recovery_words.append(word)
break
return recovery_words
#===============================================================================
def InputPassphrase():
print("\nPlease enter your Ledger Nano S passphrase now (only if set in the ledger, not your pin number!):")
try:
return input(" Ledger passphrase: ")
except KeyboardInterrupt:
return None
#===============================================================================
def InputLedgerStartIndex():
print("\nPlease enter ledger start index (Trinity default 0):")
try:
return input(" Ledger start index: ")
except KeyboardInterrupt:
return None
#===============================================================================
def InputLedgerEndIndex():
print("\nPlease enter ledger end index (Trinity default 0):")
try:
return input(" Ledger end index: ")
except KeyboardInterrupt:
return None
#===============================================================================
def InputLedgerStartPage():
print("\nPlease enter ledger start page (Trinity default 0):")
try:
return input(" Ledger start page: ")
except KeyboardInterrupt:
return None
#===============================================================================
def InputLedgerEndPage():
print("\nPlease enter ledger end page (Trinity default 0):")
try:
return input(" Ledger end page: ")
except KeyboardInterrupt:
return None
#===============================================================================
def InputAddressAmount():
print("\nPlease enter the amount of addresses to generate:")
try:
return input(" Address amount: ")
except KeyboardInterrupt:
return None
#===============================================================================
def InputTargetAddress():
print("\nPlease enter a target address with checksum:")
try:
return input(" Address: ")
except KeyboardInterrupt:
return None