-
Notifications
You must be signed in to change notification settings - Fork 4
/
KeyPair.py
174 lines (141 loc) · 5.47 KB
/
KeyPair.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
#Generate Public Key from Private Key
# Optimised for speed by reducing amount of library bloat needed
# WIF: Human Readable private key e.g. 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ
# PublicKeyReadable: Human readable public key
import os
import bitcoin
import base58
import hashlib # comes with standard python implementation
import binascii # comes with standard python implementation
from ECCurve import ECDSA
from UInt160 import UInt160 # useful representation used in bitcoin / NEO
def random_bytes(nBytes):
return os.urandom(nBytes)
def setup_curve():
"""
Setup the Elliptic curve parameters.
"""
bitcoin.change_curve(
115792089210356248762697446949407573530086143415290314195533631308867097853951,
115792089210356248762697446949407573529996955224135760342422259061068512044369,
115792089210356248762697446949407573530086143415290314195533631308867097853948,
41058363725152142129326129780047268409114441015993725554835256314039467401291,
48439561293906451759052585252797914202762949526041747995844080717082404635286,
36134250956749795798585127919587881956611106672985015071877198253568414405109
)
setup_curve()
class KeyPair():
PublicKey = None
PrivateKey = None
def __str__(self):
s = "Address: {}\n".format(self.GetAddress())
s += "WIF: {}\n".format(self.Export())
return s
@staticmethod
def scripthash_to_address(scripthash):
"""
Convert a script hash to a public address.
Args:
scripthash (bytes):
Returns:
str: base58 encoded string representing the wallet address.
"""
sb = bytearray(23) + scripthash
c256 = hashlib.sha256(hashlib.sha256(scripthash).digest()).digest()[0:4]
outb = sb + bytearray(c256)
return base58.b58encode(bytes(outb))
@property
def WIF(self):
"""
Export this KeyPair's private key in WIF format.
Returns:
str: The key in wif format
"""
data = bytearray(38)
data[0] = 0x80
data[1:33] = self.PrivateKey[0:32]
data[33] = 0x01
checksum = KeyPair.checksum_generator(data[0:34])
data[34:38] = checksum[0:4]
b58 = base58.b58encode(bytes(data))
return b58
@staticmethod
def checksum_generator(msg):
return hashlib.sha256(hashlib.sha256(msg).digest()).digest()
@staticmethod
def PrivateKeyFromWIF(wif):
"""
Get the private key from a WIF key
Args:
wif (str): The wif key
Returns:
bytes: The private key
"""
if wif is None or len(wif) is not 52:
raise ValueError('Please provide a wif with a length of 52 bytes (LEN: {0:d})'.format(len(wif)))
data = base58.b58decode(wif)
checksum = hashlib.sha256(hashlib.sha256((data[0:34])).digest()).digest()[0:4]
if checksum != data[34:]:
raise ValueError("Invalid WIF Checksum!")
return data[1:33]
@staticmethod
def ToScriptHash(data):
"""
Get a script hash of the data.
Args:
data (bytes): data to hash.
unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'
Returns:
UInt160: script hash.
"""
if len(data) > 1:
data = binascii.unhexlify(data)
return UInt160(data=binascii.unhexlify(bytes(KeyPair.hash160(data), encoding='utf-8')))
@staticmethod
def hash160(string):
intermed = hashlib.sha256(string).digest()
return hashlib.new('ripemd160', intermed).hexdigest()
@staticmethod
def scripthash_to_address(scripthash):
"""
Convert a script hash to a public address.
Args:
scripthash (bytes):
Returns:
str: base58 encoded string representing the wallet address.
"""
sb = bytearray([23]) + scripthash
c256 = hashlib.sha256(hashlib.sha256(sb).digest()).digest()[0:4]
outb = sb + bytearray(c256)
return base58.b58encode(bytes(outb))
def GetAddress(self):
"""
Returns the public NEO address for this KeyPair
Returns:
str: The human readable Public Address from the public key
"""
script = b'21' + self.PublicKey.encode_point(True) + b'ac'
script_hash = KeyPair.ToScriptHash(script)
address = KeyPair.scripthash_to_address(script_hash.Data)
return address
def __init__(self, priv_key=None):
"""
Create an instance
:param priv_key: a private key
"""
if priv_key is None:
priv_key = bytes(random_bytes(32))
length = len(priv_key)
if length != 32: # Just handle 32 byte version
raise ValueError("Invalid private key length")
self.PrivateKey = bytearray(priv_key[-32:]) #Get last 32 bytes
try:
pubkey_encoded_not_compressed = bitcoin.privkey_to_pubkey(priv_key)
except Exception as e:
raise Exception("Could not determine public key")
if pubkey_encoded_not_compressed:
pubkey_points = bitcoin.decode_pubkey(pubkey_encoded_not_compressed, 'bin')
pubx = pubkey_points[0]
puby = pubkey_points[1]
edcsa = ECDSA.secp256r1()
self.PublicKey = edcsa.Curve.point(pubx, puby)