-
Notifications
You must be signed in to change notification settings - Fork 3
/
vanitymnem.py
executable file
·194 lines (168 loc) · 9.9 KB
/
vanitymnem.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# '##::::'##::::'###::::'##::: ##:'####:'########:'##:::'##:'##::::'##:'##::: ##:'########:'##::::'##:
# ##:::: ##:::'## ##::: ###:: ##:. ##::... ##..::. ##:'##:: ###::'###: ###:: ##: ##.....:: ###::'###:
# ##:::: ##::'##:. ##:: ####: ##:: ##::::: ##:::::. ####::: ####'####: ####: ##: ##::::::: ####'####:
# ##:::: ##:'##:::. ##: ## ## ##:: ##::::: ##::::::. ##:::: ## ### ##: ## ## ##: ######::: ## ### ##:
# . ##:: ##:: #########: ##. ####:: ##::::: ##::::::: ##:::: ##. #: ##: ##. ####: ##...:::: ##. #: ##:
# :. ## ##::: ##.... ##: ##:. ###:: ##::::: ##::::::: ##:::: ##:.:: ##: ##:. ###: ##::::::: ##:.:: ##:
# ::. ###:::: ##:::: ##: ##::. ##:'####:::: ##::::::: ##:::: ##:::: ##: ##::. ##: ########: ##:::: ##:
# :::...:::::..:::::..::..::::..::....:::::..::::::::..:::::..:::::..::..::::..::........::..:::::..::
# VanityMnem - create your vanity mnemonics - 2020-2022 Valerio Vaccaro
# https://github.com/valerio-vaccaro/vanitymnem
import argparse
import os
import wallycore as wally
import re
import time
from colorama import init
from termcolor import colored
banner = """
'##::::'##::::'###::::'##::: ##:'####:'########:'##:::'##:'##::::'##:'##::: ##:'########:'##::::'##:
##:::: ##:::'## ##::: ###:: ##:. ##::... ##..::. ##:'##:: ###::'###: ###:: ##: ##.....:: ###::'###:
##:::: ##::'##:. ##:: ####: ##:: ##::::: ##:::::. ####::: ####'####: ####: ##: ##::::::: ####'####:
##:::: ##:'##:::. ##: ## ## ##:: ##::::: ##::::::. ##:::: ## ### ##: ## ## ##: ######::: ## ### ##:
. ##:: ##:: #########: ##. ####:: ##::::: ##::::::: ##:::: ##. #: ##: ##. ####: ##...:::: ##. #: ##:
:. ## ##::: ##.... ##: ##:. ###:: ##::::: ##::::::: ##:::: ##:.:: ##: ##:. ###: ##::::::: ##:.:: ##:
::. ###:::: ##:::: ##: ##::. ##:'####:::: ##::::::: ##:::: ##:::: ##: ##::. ##: ########: ##:::: ##:
:::...:::::..:::::..::..::::..::....:::::..::::::::..:::::..:::::..::..::::..::........::..:::::..::
VanityMnem - create your vanity mnemonics - 2020-2022 Valerio Vaccaro
https://github.com/valerio-vaccaro/vanitymnem"""
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def main():
init()
parser = argparse.ArgumentParser(description='Create a valid Bitcoin mnemonic with a vanity address in a specific derivation.', epilog='MIT License - Copyright (c) 2020 Valerio Vaccaro')
parser.add_argument('-v', '--verbose', action='count', default=0, help='Be more verbose. Can be used multiple times.')
parser.add_argument('-n', '--network', help=' main, test (default=test)', default='test')
parser.add_argument('-p', '--pattern', help='Regex for pattern', default='^.*[vV][aA][lL][eE]')
parser.add_argument('-P', '--passphrase', help='Passphrase', default='')
parser.add_argument('-t', '--twelve', help='Twelve words (if false a twentyfour words mnemonic will be returned)', type=bool, default=False)
parser.add_argument('-c', '--children', help='Check in children derivations from 0 to this value (default=100).', type=int, default=100)
parser.add_argument('-a', '--address', help='native_segwit, nested_segwit or legacy (default=native_segwit).', default='native_segwit')
args = parser.parse_args()
print(colored(banner, 'green'))
# check net
if args.network == 'main':
master_key_flags = wally.BIP32_VER_MAIN_PRIVATE
native_segwit_address_flags = 'bc'
nested_segwit_address_flags = wally.WALLY_ADDRESS_VERSION_P2SH_MAINNET
legacy_address_flags = wally.WALLY_ADDRESS_VERSION_P2PKH_MAINNET
elif args.network == 'test':
master_key_flags = wally.BIP32_VER_TEST_PRIVATE
native_segwit_address_flags = 'bc'
nested_segwit_address_flags = wally.WALLY_ADDRESS_VERSION_P2SH_TESTNET
legacy_address_flags = wally.WALLY_ADDRESS_VERSION_P2PKH_TESTNET
else:
print(colored('Wrong network type, choose between main or test.', 'red'))
exit(1)
# check address
if args.address == 'native_segwit':
derivation = "m/84'/0'/0'"
elif args.address == 'nested_segwit':
derivation = "m/49'/0'/0'"
elif args.address == 'legacy':
derivation = "m/44'/0'/0'"
else:
print(colored('Wrong address type, choose between native_segwit or nested_segwit or legacy.', 'red'))
exit(1)
# check external addresses, not change ones
derivation = derivation + '/0'
path = []
for c in derivation.split('/'):
der = c.split("'")
if (der[0] == 'm'):
continue
if len(der) == 2:
path = path + [0x80000000 + int(der[0])]
else:
path = path + [int(der[0])]
pattern = re.compile(args.pattern)
i = 0
start = time.time()
while(True):
i = i + 1
# get entropy
if args.twelve:
entropy = os.urandom(16)
else:
entropy = os.urandom(32)
# calculate mnemonic
mnemonic = wally.bip39_mnemonic_from_bytes(None, entropy)
# calculate the seed
seed = bytearray(64)
wally.bip39_mnemonic_to_seed(mnemonic, args.passphrase, seed)
# calculate master key
master_key = wally.bip32_key_from_seed(seed, master_key_flags, wally.BIP32_FLAG_SKIP_HASH)
if args.verbose > 1:
print(colored('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', 'yellow'))
print(colored('Seed: {}'.format(seed.hex()), 'red'))
print(colored('Mnemonic: {}'.format(mnemonic), 'red'))
print(colored('Passphrase: {}'.format(args.passphrase), 'red'))
print(colored('Master key: {}'.format(wally.bip32_key_to_base58(master_key, 0)), 'yellow'))
print(colored('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', 'yellow'))
# derive a children
found = False
for x in range(0, args.children + 1):
child = x
derived = wally.bip32_key_from_parent_path(master_key, path + [child], wally.BIP32_FLAG_KEY_PRIVATE);
if args.verbose > 1:
print(colored('Derivation: {}/{}'.format(derivation, x), 'yellow'))
if args.address == 'native_segwit':
# calculate native segwit address
native_segwit = wally.bip32_key_to_addr_segwit(derived, native_segwit_address_flags, 0);
if args.verbose > 1:
print(colored('Native segwit address: {}'.format(native_segwit), 'yellow'))
if pattern.match(native_segwit):
found = True
if args.address == 'nested_segwit':
# calculate nested segwit address - base_58
nested_segwit = wally.bip32_key_to_address(derived, wally.WALLY_ADDRESS_TYPE_P2SH_P2WPKH, nested_segwit_address_flags);
if args.verbose > 1:
print(colored('Nested segwit addres: {}'.format(nested_segwit), 'yellow'))
if pattern.match(nested_segwit):
found = True
if args.address == 'legacy':
# calculate legacy address - base_58
legacy_address = wally.bip32_key_to_address(derived, wally.WALLY_ADDRESS_TYPE_P2PKH, legacy_address_flags);
if args.verbose > 1:
print(colored('Legacy address: {}'.format(legacy_address), 'yellow'))
if pattern.match(legacy_address):
found = True
if args.verbose > 1:
print(colored('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', 'yellow'))
if found:
break
if found:
break
if i%1000 == 0:
if args.verbose > 0:
end = time.time()
print(colored('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', 'yellow'))
print(' Processed {} mnemonics in {} seconds ({} mnemonics per second).'.format(i, round(end-start), round(i/(end-start))))
end = time.time()
if args.verbose > 0:
print(colored('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', 'yellow'))
print(colored(' Processed {} mnemonics in {} seconds ({} mnemonics per second).'.format(i, round(end-start), round(i/(end-start))), 'yellow'))
print(colored('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', 'yellow'))
print(colored('Tested mnemonics: {}'.format(i), 'yellow'))
print(colored('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', 'yellow'))
print(colored('Seed: {}'.format(seed.hex()), 'red'))
print(colored('Mnemonic: {}'.format(mnemonic), 'red'))
print(colored('Passphrase: {}'.format(args.passphrase), 'red'))
print(colored('Master key: {}'.format(wally.bip32_key_to_base58(master_key, 0)), 'yellow'))
print(colored('Derivation: {}/{}'.format(derivation, x), 'yellow'))
if args.address == 'native_segwit':
print(colored('Native segwit address: {}'.format(native_segwit), 'yellow'))
if args.address == 'nested_segwit':
print(colored('Nested segwit addres: {}'.format(nested_segwit), 'yellow'))
if args.address == 'legacy':
print(colored('Legacy address: {}'.format(legacy_address), 'yellow'))
print(colored('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', 'yellow'))
if __name__== "__main__" :
main()