-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyutils.py
75 lines (49 loc) · 2.35 KB
/
keyutils.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
from multiprocessing.managers import public_methods
__author__ = 'rs'
import random
import ecdsa
import base58
import hashlib
def privatekey_to_wif(key):
# return utils.base58CheckEncode(0x80, key_hex.decode('hex'))
x = '80' + key
return base58.b58encode_check(x.decode('hex'))
def privatekey_to_publickey(s):
sk = ecdsa.SigningKey.from_string(s.decode('hex'), curve=ecdsa.SECP256k1)
vk = sk.verifying_key
return ('\04' + sk.verifying_key.to_string()).encode('hex')
def publickey_to_address(public_key):
sha256_of_pubkey = hashlib.sha256(public_key.decode('hex')).digest()
#print 'sha256 of public key -> ', sha256_of_pubkey.encode('hex')
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(sha256_of_pubkey)
ripemd160_of_sha256 = ripemd160.digest()
#print 'ripemd160 of sha256 -> ', ripemd160_of_sha256.encode('hex')
ripemd160_of_sha256_with_network = '00' + ripemd160_of_sha256.encode('hex')
#print 'ripemd160 of sha256 with network -> ', ripemd160_of_sha256_with_network
#double_sha256 = hashlib.sha256(hashlib.sha256(ripemd160_of_sha256_with_network.decode('hex')).digest())
#print 'double sha256 of previous -> ', double_sha256.digest().encode('hex')
#checksum = double_sha256.digest().encode('hex')[:8]
#print 'checksum -> ', checksum
#with_checksum = ripemd160_of_sha256_with_network + checksum
#print 'with checksum -> ', with_checksum
address = base58.b58encode_check(ripemd160_of_sha256_with_network.decode('hex'))
#address = base58.b58encode(with_checksum.decode('hex'))
#print 'address -> ', address
return address
def privatekey_to_address(s):
return publickey_to_address(privatekey_to_publickey(s))
# Warning: this random function is not cryptographically strong and is just for example
print ''
#private_key = ''.join(['{:x}'.format(random.randrange(16)) for x in range(64)])
private_key = 'f19c523315891e6e15ae0608a35eec2e00ebd6d1984cf167f46336dabd9b2de4'
print 'private key -> ', private_key
#print 'private key -> ', private_key_ken
wif_private_key = privatekey_to_wif(private_key)
print 'WIF private key -> ', wif_private_key
print ''
public_key = privatekey_to_publickey(private_key)
print 'public key -> ', public_key
address = publickey_to_address(public_key)
print 'Bitcoin Address -> ', address
print 'Bitcoin Address -> ', privatekey_to_address(private_key)