-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathprivate_key.py
More file actions
99 lines (69 loc) · 2.58 KB
/
private_key.py
File metadata and controls
99 lines (69 loc) · 2.58 KB
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
from binascii import hexlify
from hashlib import sha256
from coincurve import PrivateKey as PvtKey
from Cryptodome.Hash import keccak
from base58 import b58decode
from crypto.configuration.network import Network
class PrivateKey(object):
def __init__(self, private_key: str):
self.private_key = PvtKey.from_hex(private_key)
self.public_key = hexlify(self.private_key.public_key.format()).decode()
def sign(self, message: bytes) -> bytes:
"""Sign a message with this private key object
Args:
message (bytes): bytes data you want to sign
Returns:
bytes: signature of the signed message
"""
message_hash = bytes.fromhex(keccak.new(data=message, digest_bits=256).hexdigest())
der = self.private_key.sign_recoverable(message_hash, hasher=None)
return bytes([der[64]]) + der[0:64]
def sign_to_ecdsa(self, message: bytes) -> bytes:
"""Sign a message with this private key object in ECDSA format
Args:
message (bytes): bytes data you want to sign
Returns:
bytes: signature of the signed message
"""
message_hash = bytes.fromhex(keccak.new(data=message, digest_bits=256).hexdigest())
der = self.private_key.sign_recoverable(message_hash, hasher=None)
return der[0:64] + bytes([der[64]])
def to_hex(self):
"""Returns a private key in hex format
Returns:
str: private key in hex format
"""
return self.private_key.to_hex()
@classmethod
def from_passphrase(cls, passphrase: str):
"""Create PrivateKey object from a given passphrase
Args:
passphrase (str):
Returns:
PrivateKey: Private key object
"""
private_key = sha256(passphrase.encode()).hexdigest()
return cls(private_key)
@classmethod
def from_hex(cls, private_key: str):
"""Create PrivateKey object from a given hex private key
Args:
private_key (str):
Returns:
PrivateKey: Private key object
"""
return cls(private_key)
@classmethod
def from_wif(cls, wif: str):
"""Create PrivateKey object from a given wif
Args:
wif (str):
Returns:
PrivateKey: Private key object
"""
wif = b58decode(wif).hex()
version = wif[0:2]
if version != Network.get_network().wif():
raise ValueError(f"Invalid network version: {version}")
private_key = wif[2:66]
return cls(private_key)