forked from sarchar/addressgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genaddress.py
457 lines (358 loc) · 16.2 KB
/
genaddress.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/env python3
import argparse
import ctypes
import hashlib
import hmac
import sys
import base58
################################################################################
################################################################################
DEBUG = False
COINS = {
'BTC' : {
'main': {
'prefix' : 0,
'private_prefix': 0x80,
'bip32_public' : bytes([0x04, 0x88, 0xb2, 0x1e]),
'bip32_private' : bytes([0x04, 0x88, 0xad, 0xe4]),
},
'test': {
'prefix' : 0x6f,
'private_prefix': 0x6f+0x80,
'bip32_public' : bytes([0x04, 0x35, 0x87, 0xcf]),
'bip32_private' : bytes([0x04, 0x35, 0x83, 0x94]),
}
},
'DOGE' : {
'main': {
'prefix' : 0x1e,
'private_prefix': 0x1e+0x80,
'bip32_public' : bytes([0x02, 0xfa, 0xca, 0xfd]),
'bip32_private' : bytes([0x02, 0xfa, 0xc3, 0x98]),
},
'test': {
'prefix' : 0x71,
'private_prefix': 0x71+0x80,
'bip32_public' : bytes([0x04, 0x32, 0xa9, 0xa8]),
'bip32_private' : bytes([0x04, 0x32, 0xa2, 0x43]),
}
},
'LTC' : {
'main': {
'prefix' : 0x30,
'private_prefix': 0x30+0x80,
'bip32_public' : bytes([0x01, 0x9d, 0xa4, 0x62]),
'bip32_private' : bytes([0x01, 0x9d, 0x9c, 0xfe]),
},
'test': {
'prefix' : 0x6f,
'private_prefix': 0x6f+0x80,
'bip32_public' : bytes([0x04, 0x36, 0xf6, 0xe1]),
'bip32_private' : bytes([0x04, 0x36, 0xef, 0x7d]),
}
},
'PPC' : {
'main': {
'prefix' : 0x37,
'private_prefix': 0x38+0x80,
},
'test': {
'prefix' : 0x6f,
'private_prefix': 0x6f+0x80,
}
}
}
################################################################################
################################################################################
try:
ssl_library = ctypes.cdll.LoadLibrary('libeay32.dll')
except:
ssl_library = ctypes.cdll.LoadLibrary('libssl.so')
if DEBUG:
print("ssl_library is {}".format(ssl_library))
def bytes2hex(data):
return ''.join([ '{:02x}'.format(v) for v in data ])
def hex2bytes(data):
return bytes([ int(data[i:i+2], 16) for i in range(0, len(data), 2) ])
NID_secp160k1 = 708
NID_secp256k1 = 714
BIP32_PRIVATE_KEY_BYTES = set([COINS[coin_name][network]['bip32_private'] for coin_name in COINS.keys() for network in ('main', 'test') if 'bip32_private' in COINS[coin_name][network]])
BIP32_PUBLIC_KEY_BYTES = set([COINS[coin_name][network]['bip32_public'] for coin_name in COINS.keys() for network in ('main', 'test') if 'bip32_public' in COINS[coin_name][network]])
def gen_key_pair(curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
if ssl_library.EC_KEY_generate_key(k) != 1:
raise Exception("internal error")
bignum_private_key = ssl_library.EC_KEY_get0_private_key(k)
size = (ssl_library.BN_num_bits(bignum_private_key)+7)//8
if DEBUG:
print("Private key size is {} bytes".format(size))
storage = ctypes.create_string_buffer(size)
ssl_library.BN_bn2bin(bignum_private_key, storage)
private_key = storage.raw
if (len(private_key) == size) and size < 32:
private_key = bytes([0] * (32 - size)) + private_key
size = ssl_library.i2o_ECPublicKey(k, 0)
if DEBUG:
print("Public key size is {} bytes".format(size))
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_KEY_free(k)
return public_key, private_key
def get_public_key(private_key, curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
storage = ctypes.create_string_buffer(private_key)
bignum_private_key = ssl_library.BN_new()
ssl_library.BN_bin2bn(storage, 32, bignum_private_key)
group = ssl_library.EC_KEY_get0_group(k)
point = ssl_library.EC_POINT_new(group)
ssl_library.EC_POINT_mul(group, point, bignum_private_key, None, None, None)
ssl_library.EC_KEY_set_private_key(k, bignum_private_key)
ssl_library.EC_KEY_set_public_key(k, point)
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
pstorage = ctypes.pointer(storage)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(pstorage))
public_key = storage.raw
ssl_library.EC_POINT_free(point)
ssl_library.BN_free(bignum_private_key)
ssl_library.EC_KEY_free(k)
return public_key
def compress(public_key):
x_coord = public_key[1:33]
if public_key[64] & 0x01:
c = bytes([0x03]) + x_coord
else:
c = bytes([0x02]) + x_coord
return c
def decompress(public_key):
raise Exception("TODO")
def singlehash256(data):
hasher = hashlib.sha256()
hasher.update(data)
return hasher.digest()
def hash256(data):
hasher = hashlib.sha256()
hasher.update(data)
r = hasher.digest()
hasher2 = hashlib.sha256()
hasher2.update(r)
return hasher2.digest()
def hash160(data):
hasher = hashlib.sha256()
hasher.update(data)
r = hasher.digest()
hasher2 = hashlib.new('ripemd160')
hasher2.update(r)
return hasher2.digest()
def is_public_key(public_key):
return len(public_key) > 0 and \
((public_key[0] == 0x04 and len(public_key) == 65) or \
(public_key[0] in (0x02, 0x03) and len(public_key) == 3))
def address_from_data(data, version_bytes=0):
assert isinstance(data, bytes)
return base58_check(hash160(data), version_bytes=version_bytes)
def base58_check(src, version_bytes=0):
if isinstance(version_bytes, int):
version_bytes = bytes([version_bytes])
src = version_bytes + src
r = hash256(src)
if DEBUG:
print('SHA256(SHA256(0x{} + src)):'.format(bytes2hex(version_bytes)), bytes2hex(r))
checksum = r[:4]
s = src + checksum
if DEBUG:
print('src + checksum:', bytes2hex(s))
e = base58.encode(int.from_bytes(s, 'big'))
if version_bytes == bytes([0]):
lz = 0
while lz < len(src) and src[lz] == 0:
lz += 1
return ('1' * lz) + e
return e
def decode_base58_private_key(src):
decoded = base58.decode(src)
try:
# version + private_key + checksum
decoded_bytes = decoded.to_bytes(37, 'big')
version_byte = decoded_bytes[0]
private_key = decoded_bytes[1:33]
compressed_byte = 0
checksum = decoded_bytes[33:]
src = bytes([version_byte]) + private_key
except OverflowError:
# version + private_key + compression + checksum
decoded_bytes = decoded.to_bytes(38, 'big')
version_byte = decoded_bytes[0]
private_key = decoded_bytes[1:33]
compressed_byte = decoded_bytes[33]
checksum = decoded_bytes[34:]
src = bytes([version_byte]) + private_key + bytes([compressed_byte])
s = hash256(src)
if s[0:4] != checksum:
raise Exception("invalid private key")
return version_byte, compressed_byte == 0x01, private_key
def bip32(private_key, coin):
m = hmac.new('Bitcoin seed'.encode('ascii'), digestmod=hashlib.sha512)
m.update(private_key)
s = m.digest()
il, ir = s[:32], s[32:]
# Generate a master private key:
# depth + parent_fingerprint + child_index + chain_code + 0 + private_key
r = bytes([0]) + bytes([0, 0, 0, 0]) + bytes([0, 0, 0, 0]) + ir + bytes([0]) + il
bip32_private_key = base58_check(r, version_bytes=coin['bip32_private'])
# Generate a master public key:
# depth + parent_fingerprint + child_index + chain_code + compressed_public_key
r = bytes([0]) + bytes([0, 0, 0, 0]) + bytes([0, 0, 0, 0]) + ir + compress(get_public_key(il))
bip32_public_key = base58_check(r, version_bytes=coin['bip32_public'])
return bip32_public_key, bip32_private_key
def bip32_get_public_key(bip32_private_key):
decoded = base58.decode(bip32_private_key)
decoded_bytes = decoded.to_bytes(82, 'big')
if len(decoded_bytes) != 82:
raise Exception("invalid bip32 key")
c = hash256(decoded_bytes[0:78])
if c[0:4] != decoded_bytes[78:]:
raise Exception("invalid bip32 key")
if decoded_bytes[0:4] not in BIP32_PRIVATE_KEY_BYTES:
raise Exception("invalid bip32 key")
if decoded_bytes[-4-33] != 0:
raise Exception("invalid bip32 key")
private_key = decoded_bytes[-4-32:-4]
r = decoded_bytes[4:-4-33] + compress(get_public_key(private_key))
coin = None
for coin_name in COINS:
for network in ('main', 'test'):
if 'bip32_private' in COINS[coin_name][network] and decoded_bytes[0:4] == COINS[coin_name][network]['bip32_private']:
coin = COINS[coin_name][network]
break
if coin is None:
raise Exception("unknown extended private key")
return base58_check(r, version_bytes=coin['bip32_public'])
def bip32_extract_private_key(bip32_private_key):
decoded = base58.decode(bip32_private_key)
decoded_bytes = decoded.to_bytes(82, 'big')
if len(decoded_bytes) != 82:
raise Exception("invalid bip32 key")
c = hash256(decoded_bytes[0:78])
if c[0:4] != decoded_bytes[78:]:
raise Exception("invalid bip32 key")
if decoded_bytes[0:4] not in BIP32_PRIVATE_KEY_BYTES:
raise Exception("invalid bip32 key")
if decoded_bytes[-4-33] != 0:
raise Exception("invalid bip32 key")
return decoded_bytes[-4-32:-4]
def bip32_extract_public_key(bip32_public_key):
decoded = base58.decode(bip32_public_key)
decoded_bytes = decoded.to_bytes(82, 'big')
if len(decoded_bytes) != 82:
raise Exception("invalid bip32 key")
c = hash256(decoded_bytes[0:78])
if c[0:4] != decoded_bytes[78:]:
raise Exception("invalid bip32 key")
if decoded_bytes[0:4] not in BIP32_PUBLIC_KEY_BYTES:
raise Exception("invalid bip32 key")
if decoded_bytes[-4-33] not in (0x02, 0x03):
raise Exception("invalid bip32 key")
return decoded_bytes[-4-33:-4]
def parse_args():
parser = argparse.ArgumentParser(description="Generate Bitcoin Private Keys and Addresses")
parser.add_argument("-p", "--passphrase", default=None, help="Use PASSPHRASE as the seed to a hash, the result of the hash is used as the private key")
parser.add_argument("-t", "--testnet", default=False, action='store_true', help="Generate testnet address")
parser.add_argument("-c", "--compressed", default=False, action='store_true', help="Generate address using compressed private key")
parser.add_argument("-a", "--address-only", metavar="STR", default=None, help="Hash160 STR and produce a Bitcoin address; no corresponding private key is generated")
parser.add_argument("-H", "--hash-type", metavar="HASH", default='SHA256', help="For -p only, specify the hash type to use [scrypt, SHA256] (default: SHA-256)")
parser.add_argument("-k", "--private-key", metavar="KEY", default=None, help="Generate the public key and address from the given Bitcoin private key")
parser.add_argument("-e", "--bip32-private-key", metavar="KEY", default=None, help="Generate the BIP32 public key and show information from the Bitcoin extended private key")
parser.add_argument("-n", "--coin", metavar="COIN", default="BTC", help="Generate an address for the given coin (BTC, DOGE, LTC, PPC) [default: BTC]")
args = parser.parse_args()
c = 0
if args.passphrase is not None: c += 1
if args.address_only is not None: c += 1
if args.private_key is not None: c += 1
if args.bip32_private_key is not None: c += 1
if c > 1:
raise Exception("you can only specify one of -p, -a, -k, or -e")
if (args.address_only is not None or args.private_key is not None) and args.compressed:
raise Exception("you can not use -c with -a or -k")
if args.coin.upper() not in COINS:
raise Exception("unknown coin {}".format(args.coin))
args.coin = COINS[args.coin.upper()]['test' if args.testnet else 'main']
return args
def main():
args = parse_args()
coin = args.coin
bip32_private_key = None
bip32_public_key = None
if args.passphrase is not None:
if args.hash_type == "SHA256":
private_key = singlehash256(args.passphrase.encode("utf8"))
else:
raise Exception("TODO")
public_key = get_public_key(private_key)
elif args.address_only is not None:
if args.hash_type == "SHA256":
public_key = args.address_only.encode("utf8")
else:
raise Exception("TODO")
private_key = None
elif args.private_key is not None:
version_byte, compressed, private_key = decode_base58_private_key(args.private_key)
print(version_byte, '{:04x}'.format(version_byte))
public_key = get_public_key(private_key)
coin = None
for coin_name in COINS.keys():
for network in ('main', 'test'):
if network not in COINS[coin_name]:
continue
print(version_byte, coin_name, network, COINS[coin_name][network]['private_prefix'], version_byte == COINS[coin_name][network]['private_prefix'])
if version_byte == COINS[coin_name][network]['private_prefix']:
args.testnet = (network == 'test')
coin = COINS[coin_name][network]
break
if coin is not None:
break
if coin is None:
raise Exception("invalid version byte in private key")
args.compressed = compressed
elif args.bip32_private_key is not None:
bip32_private_key = args.bip32_private_key
bip32_public_key = bip32_get_public_key(bip32_private_key)
public_key = None
private_key = None
else:
public_key, private_key = gen_key_pair()
if private_key is not None:
assert len(private_key) == 32
print("ECDSA private key (random number / secret exponent)\n {}".format(bytes2hex(private_key)))
if args.compressed:
print("Bitcoin private key (Base58Check, compressed)\n {}".format(base58_check(private_key + bytes([0x01]), version_bytes=coin['private_prefix'])))
else:
print("Bitcoin private key (Base58Check, uncompressed)\n {}".format(base58_check(private_key, version_bytes=coin['private_prefix'])))
if 'bip32_private' in coin:
bip32_public_key, bip32_private_key = bip32(private_key, coin)
if bip32_private_key is not None:
print("Bitcoin extended private key (Base58Check)\n {}".format(bip32_private_key))
print(" (embedded private key) -> {}".format(base58_check(bip32_extract_private_key(bip32_private_key) + bytes([0x01]), version_bytes=239 if args.testnet else 128)))
if public_key is not None or bip32_public_key is not None:
print('------')
if public_key is not None:
assert len(public_key) in (33, 65)
if args.compressed:
compressed_public_key = compress(public_key)
print("ECDSA public key (compressed)\n {}".format(bytes2hex(compressed_public_key)))
addr = base58_check(hash160(compressed_public_key), version_bytes=coin['prefix'])
print("Bitcoin Address (compressed, length={}):\n {}".format(len(addr), addr))
else:
if args.address_only:
print("Public key source\n {}".format(bytes2hex(public_key)))
else:
print("ECDSA public key (uncompressed)\n {}".format(bytes2hex(public_key)))
addr = address_from_data(public_key, version_bytes=coin['prefix'])
print("Bitcoin Address (uncompressed, length={}):\n {}".format(len(addr), addr))
if bip32_public_key is not None:
print("Bitcoin extended public key\n {}".format(bip32_public_key))
print(" (embedded public key) -> {}".format(bytes2hex(bip32_extract_public_key(bip32_public_key))))
print(" (bitcoin address) -> {}".format(base58_check(hash160(bip32_extract_public_key(bip32_public_key)), version_bytes=coin['prefix'])))
if __name__ == "__main__":
main()