1
1
from coincurve import PrivateKey , PublicKey
2
2
from coincurve .utils import get_valid_secret
3
- from eth_keys import keys
4
3
5
4
from ..config import ECIES_CONFIG , Config
6
5
from .hex import decode_hex
@@ -30,8 +29,9 @@ def generate_eth_key():
30
29
eth_keys.keys.PrivateKey
31
30
An ethereum key
32
31
33
- >>> k = generate_eth_key()
34
32
"""
33
+ from eth_keys import keys
34
+
35
35
return keys .PrivateKey (get_valid_secret ())
36
36
37
37
@@ -51,23 +51,14 @@ def hex2pk(pk_hex: str) -> PublicKey:
51
51
coincurve.PublicKey
52
52
A secp256k1 public key
53
53
54
- >>> from ecies.utils import sha256
55
- >>> data = b'0' * 32
56
- >>> data_hash = sha256(data)
57
- >>> eth_sk = generate_eth_key()
58
- >>> cc_sk = hex2sk(eth_sk.to_hex())
59
- >>> eth_sk.sign_msg_hash(data_hash).to_bytes() == cc_sk.sign_recoverable(data)
60
- True
61
- >>> pk_hex = eth_sk.public_key.to_hex()
62
- >>> computed_pk = hex2pk(pk_hex)
63
- >>> computed_pk == cc_sk.public_key
64
- True
65
54
"""
66
- uncompressed = decode_hex (pk_hex )
67
- if len (uncompressed ) == 64 : # eth public key format
68
- uncompressed = b"\x04 " + uncompressed
55
+ return PublicKey (compat_eth_public_key (decode_hex (pk_hex )))
56
+
69
57
70
- return PublicKey (uncompressed )
58
+ def compat_eth_public_key (data : bytes ):
59
+ if len (data ) == 64 : # eth public key format
60
+ data = b"\x04 " + data
61
+ return data
71
62
72
63
73
64
def hex2sk (sk_hex : str ) -> PrivateKey :
@@ -84,18 +75,14 @@ def hex2sk(sk_hex: str) -> PrivateKey:
84
75
coincurve.PrivateKey
85
76
A secp256k1 private key
86
77
87
- >>> k = generate_eth_key()
88
- >>> sk_hex = k.to_hex()
89
- >>> pk_hex = k.public_key.to_hex()
90
- >>> computed_sk = hex2sk(sk_hex)
91
- >>> computed_sk.to_int() == int(k.to_hex(), 16)
92
- True
93
78
"""
94
79
return PrivateKey (decode_hex (sk_hex ))
95
80
96
81
97
82
# private below
98
- def encapsulate (private_key : PrivateKey , peer_public_key : PublicKey , config : Config = ECIES_CONFIG ) -> bytes :
83
+ def encapsulate (
84
+ private_key : PrivateKey , peer_public_key : PublicKey , config : Config = ECIES_CONFIG
85
+ ) -> bytes :
99
86
is_compressed = config .is_hkdf_key_compressed
100
87
shared_point = peer_public_key .multiply (private_key .secret )
101
88
master = private_key .public_key .format (is_compressed ) + shared_point .format (
@@ -104,7 +91,9 @@ def encapsulate(private_key: PrivateKey, peer_public_key: PublicKey, config: Con
104
91
return derive_key (master )
105
92
106
93
107
- def decapsulate (public_key : PublicKey , peer_private_key : PrivateKey , config : Config = ECIES_CONFIG ) -> bytes :
94
+ def decapsulate (
95
+ public_key : PublicKey , peer_private_key : PrivateKey , config : Config = ECIES_CONFIG
96
+ ) -> bytes :
108
97
is_compressed = config .is_hkdf_key_compressed
109
98
shared_point = public_key .multiply (peer_private_key .secret )
110
99
master = public_key .format (is_compressed ) + shared_point .format (is_compressed )
0 commit comments