forked from xxxxfanmade/Plutus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdKeyGen.py
47 lines (42 loc) · 1.57 KB
/
oldKeyGen.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
import os
import hashlib
import binascii
from ellipticcurve.privateKey import PrivateKey
def generate_private_key():
"""
Generate a random 32-byte hex integer which serves as a randomly
generated Bitcoin private key.
Average Time: 0.0000061659 seconds
"""
return binascii.hexlify(os.urandom(32)).decode('utf-8').upper()
def private_key_to_public_key(private_key):
"""
Accept a hex private key and convert it to its respective public key.
Because converting a private key to a public key requires SECP256k1 ECDSA
signing, this function is the most time consuming and is a bottleneck in
the overall speed of the program.
Average Time: 0.0031567731 seconds
"""
pk = PrivateKey().fromString(bytes.fromhex(private_key))
return '04' + pk.publicKey().toString().hex().upper()
def public_key_to_address(public_key):
"""
Accept a public key and convert it to its resepective P2PKH wallet address.
Average Time: 0.0000801390 seconds
"""
output = []
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
var = hashlib.new('ripemd160')
encoding = binascii.unhexlify(public_key.encode())
var.update(hashlib.sha256(encoding).digest())
var_encoded = ('00' + var.hexdigest()).encode()
digest = hashlib.sha256(binascii.unhexlify(var_encoded)).digest()
var_hex = '00' + var.hexdigest() + hashlib.sha256(digest).hexdigest()[0:8]
count = [char != '0' for char in var_hex].index(True) // 2
n = int(var_hex, 16)
while n > 0:
n, remainder = divmod(n, 58)
output.append(alphabet[remainder])
for i in range(count):
output.append(alphabet[0])
return ''.join(output[::-1])