2019-08-14 09:17:08 +08:00
|
|
|
import coincurve
|
2019-08-15 18:35:43 +08:00
|
|
|
|
2019-08-14 11:23:07 +08:00
|
|
|
from libp2p.crypto.keys import KeyPair, KeyType, PrivateKey, PublicKey
|
2019-08-14 09:17:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Secp256k1PublicKey(PublicKey):
|
|
|
|
def __init__(self, impl: coincurve.PublicKey) -> None:
|
|
|
|
self.impl = impl
|
|
|
|
|
|
|
|
def to_bytes(self) -> bytes:
|
|
|
|
return self.impl.format()
|
|
|
|
|
2019-08-20 23:54:33 +08:00
|
|
|
@classmethod
|
2019-08-24 04:12:13 +08:00
|
|
|
def from_bytes(cls, data: bytes) -> "Secp256k1PublicKey":
|
|
|
|
impl = coincurve.PublicKey(data)
|
|
|
|
return cls(impl)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def deserialize(cls, data: bytes) -> "Secp256k1PublicKey":
|
|
|
|
protobuf_key = cls.deserialize_from_protobuf(data)
|
|
|
|
return cls.from_bytes(protobuf_key.data)
|
2019-08-20 23:54:33 +08:00
|
|
|
|
2019-08-14 09:17:08 +08:00
|
|
|
def get_type(self) -> KeyType:
|
|
|
|
return KeyType.Secp256k1
|
|
|
|
|
|
|
|
def verify(self, data: bytes, signature: bytes) -> bool:
|
2019-08-23 22:54:31 +08:00
|
|
|
return self.impl.verify(signature, data)
|
2019-08-14 09:17:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Secp256k1PrivateKey(PrivateKey):
|
|
|
|
def __init__(self, impl: coincurve.PrivateKey) -> None:
|
|
|
|
self.impl = impl
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def new(cls, secret: bytes = None) -> "Secp256k1PrivateKey":
|
2019-08-14 23:47:16 +08:00
|
|
|
private_key_impl = coincurve.PrivateKey(secret)
|
2019-08-14 09:17:08 +08:00
|
|
|
return cls(private_key_impl)
|
|
|
|
|
|
|
|
def to_bytes(self) -> bytes:
|
|
|
|
return self.impl.secret
|
|
|
|
|
2019-08-24 04:12:13 +08:00
|
|
|
@classmethod
|
|
|
|
def from_bytes(cls, data: bytes) -> "Secp256k1PrivateKey":
|
|
|
|
impl = coincurve.PrivateKey(data)
|
|
|
|
return cls(impl)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def deserialize(cls, data: bytes) -> "Secp256k1PrivateKey":
|
|
|
|
protobuf_key = cls.deserialize_from_protobuf(data)
|
|
|
|
return cls.from_bytes(protobuf_key.data)
|
|
|
|
|
2019-08-14 09:17:08 +08:00
|
|
|
def get_type(self) -> KeyType:
|
|
|
|
return KeyType.Secp256k1
|
|
|
|
|
|
|
|
def sign(self, data: bytes) -> bytes:
|
2019-08-23 22:54:31 +08:00
|
|
|
return self.impl.sign(data)
|
2019-08-14 09:17:08 +08:00
|
|
|
|
|
|
|
def get_public_key(self) -> PublicKey:
|
|
|
|
public_key_impl = coincurve.PublicKey.from_secret(self.impl.secret)
|
|
|
|
return Secp256k1PublicKey(public_key_impl)
|
|
|
|
|
|
|
|
|
2019-08-14 11:23:07 +08:00
|
|
|
def create_new_key_pair(secret: bytes = None) -> KeyPair:
|
2019-08-14 09:17:08 +08:00
|
|
|
"""
|
|
|
|
Returns a new Secp256k1 keypair derived from the provided ``secret``,
|
|
|
|
a sequence of bytes corresponding to some integer between 0 and the group order.
|
|
|
|
|
|
|
|
A valid secret is created if ``None`` is passed.
|
|
|
|
"""
|
2019-08-14 23:47:42 +08:00
|
|
|
private_key = Secp256k1PrivateKey.new(secret)
|
2019-08-14 09:17:08 +08:00
|
|
|
public_key = private_key.get_public_key()
|
2019-08-14 11:23:07 +08:00
|
|
|
return KeyPair(private_key, public_key)
|