This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
135 lines (104 loc) · 3.74 KB
/
main.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
#!/usr/bin/python3
import binascii
# import csv
import hashlib
import argparse
def load_worddict():
worddict = {}
with open('Dice List.csv') as file:
for line in file:
worddict[line[:5]] = line[6:].strip()
return worddict
def split(str, num):
return [str[start:start + num] for start in range(0, len(str), num)]
def phrase_to_privkey(phrase):
privkey = phrase.encode('ascii')
privkey = hashlib.sha256(privkey).digest()
privkey = binascii.hexlify(privkey)
return privkey.decode('utf-8')
# By Filioo Valsorda
# https://filippo.io/brainwallets-from-the-password-to-the-address/
def encode_wif(private_key):
# Prepend the 0x80 version/application byte
private_key = b'\x80' + binascii.unhexlify(private_key)
# Append the first 4 bytes of SHA256(SHA256(private_key)) as a checksum
private_key += hashlib.sha256(
hashlib.sha256(private_key).digest()).digest()[:4]
# Convert to Base58 encoding
code_string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
value = int(private_key.encode('hex'), 16)
output = ""
while value:
value, remainder = divmod(value, 58)
output = code_string[remainder] + output
return output
def dicerolls_type(rolls):
try:
int(rolls)
except:
msg = "Dicerolls must only include integers."
raise argparse.ArgumentTypeError(msg)
for r in rolls:
if int(r) < 1 or int(r) > 6:
msg = "Each die roll must be a number 1-6."
raise argparse.ArgumentTypeError(msg)
remainder = len(str(rolls)) % 5
if not remainder == 0:
msg = "Each word must be five dicerolls. \
Roll a die {0} more times.".format(5 - remainder)
raise argparse.ArgumentTypeError(msg)
return rolls
def numaddrs_type(num):
try:
x = int(num)
except:
msg = "Number of addresses must be an integer."
raise argparse.ArgumentTypeError(msg)
if x < 1:
msg = "Number of addresses must be greater than 0."
raise argparse.ArgumentTypeError(msg)
return x
def print_addr(phrase, key):
print
print("Back up phrase: '" + phrase + "'")
print("Private key: " + key)
print("Private key (WIF): " + encode_wif(key))
def get_parser():
parser = argparse.ArgumentParser(
description='Generate diceware addresses.')
parser.add_argument('dicerolls',
help='dice rolls - no spaces',
type=dicerolls_type)
parser.add_argument('-n',
'--numaddrs',
nargs='?',
help="Number of diceware addresses (>= 1)",
type=numaddrs_type)
parser.add_argument('-s',
'--salt',
nargs='?',
help="Add a salt (quotation marks are optional, \
unless salt includes spaces; use escape character \
for quotation marks in salt)")
return parser.parse_args()
def main():
worddict = load_worddict()
args = get_parser()
dicerolls = args.dicerolls
dicerolls = split(dicerolls, 5)
dicewords = ''
for roll in dicerolls:
dicewords = ' '.join((dicewords, worddict[roll]))
dicewords = dicewords.strip()
if args.salt:
dicewords += ' ' + args.salt.strip()
private_key = phrase_to_privkey(dicewords)
print_addr(dicewords, private_key)
if (args.numaddrs and args.numaddrs > 1):
for i in range(1, args.numaddrs):
dicewordsnum = dicewords + str(i)
private_key = phrase_to_privkey(dicewordsnum)
print_addr(dicewordsnum, private_key)
print
if __name__ == '__main__':
main()