Exemple #1
0
 def private_key_to_public_key(self, private_key):
     if not isinstance(private_key, PrivateKey):
         raise ValidationError(
             "The `private_key` must be an instance of `eth_keys.datatypes.PrivateKey`"
         )
     public_key = self.backend.private_key_to_public_key(private_key)
     if not isinstance(public_key, PublicKey):
         raise ValidationError(
             "Backend returned an invalid public_key.  Return value must be "
             "an instance of `eth_keys.datatypes.PublicKey`")
     return public_key
Exemple #2
0
 def ecdsa_verify(self, message_hash: bytes, signature: BaseSignature,
                  public_key: _PublicKey) -> bool:
     validate_message_hash(message_hash)
     if not isinstance(public_key, PublicKey):
         raise ValidationError(
             "The `public_key` must be an instance of `eth_keys.datatypes.PublicKey`"
         )
     if not isinstance(signature, BaseSignature):
         raise ValidationError(
             "The `signature` must be an instance of `eth_keys.datatypes.BaseSignature`"
         )
     return self.backend.ecdsa_verify(message_hash, signature, public_key)
Exemple #3
0
 def ecdsa_sign(self, message_hash: bytes,
                private_key: _PrivateKey) -> _Signature:
     validate_message_hash(message_hash)
     if not isinstance(private_key, PrivateKey):
         raise ValidationError(
             "The `private_key` must be an instance of `eth_keys.datatypes.PrivateKey`"
         )
     signature = self.backend.ecdsa_sign(message_hash, private_key)
     if not isinstance(signature, Signature):
         raise ValidationError(
             "Backend returned an invalid signature.  Return value must be "
             "an instance of `eth_keys.datatypes.Signature`")
     return signature
Exemple #4
0
 def ecdsa_recover(self, message_hash: bytes,
                   signature: _Signature) -> _PublicKey:
     validate_message_hash(message_hash)
     if not isinstance(signature, Signature):
         raise ValidationError(
             "The `signature` must be an instance of `eth_keys.datatypes.Signature`"
         )
     public_key = self.backend.ecdsa_recover(message_hash, signature)
     if not isinstance(public_key, _PublicKey):
         raise ValidationError(
             "Backend returned an invalid public_key.  Return value must be "
             "an instance of `eth_keys.datatypes.PublicKey`")
     return public_key
Exemple #5
0
 def ecdsa_verify(self, message_hash: bytes, signature: _Signature,
                  public_key: _PublicKey) -> bool:
     if not isinstance(public_key, PublicKey):
         raise ValidationError(
             "The `public_key` must be an instance of `eth_keys.datatypes.PublicKey`"
         )
     return self.ecdsa_recover(message_hash, signature) == public_key
Exemple #6
0
def validate_compressed_public_key_bytes(value: Any) -> None:
    validate_bytes(value)
    validate_bytes_length(value, 33, "compressed public key")
    first_byte = value[0:1]
    if first_byte not in (b"\x02", b"\x03"):
        raise ValidationError(
            "Unexpected compressed public key format: Must start with 0x02 or 0x03, but starts "
            "with {first_byte}".format(first_byte=encode_hex(first_byte), ))
Exemple #7
0
 def ecdsa_sign(
         self,
         message_hash,  # type: bytes
         private_key  # type: Union[PrivateKey, bytes]
 ):
     # type: (...) -> Optional[Signature]
     validate_message_hash(message_hash)
     if not isinstance(private_key, PrivateKey):
         raise ValidationError(
             "The `private_key` must be an instance of `eth_keys.datatypes.PrivateKey`"
         )
     signature = self.backend.ecdsa_sign(message_hash, private_key)
     if not isinstance(signature, Signature):
         raise ValidationError(
             "Backend returned an invalid signature.  Return value must be "
             "an instance of `eth_keys.datatypes.Signature`")
     return signature
Exemple #8
0
 def ecdsa_recover(
         self,
         message_hash,  # type: bytes
         signature  # type: Union[Signature, bytes]
 ):
     # type: (...) -> Optional[PublicKey]
     validate_message_hash(message_hash)
     if not isinstance(signature, Signature):
         raise ValidationError(
             "The `signature` must be an instance of `eth_keys.datatypes.Signature`"
         )
     public_key = self.backend.ecdsa_recover(message_hash, signature)
     if not isinstance(public_key, PublicKey):
         raise ValidationError(
             "Backend returned an invalid public_key.  Return value must be "
             "an instance of `eth_keys.datatypes.PublicKey`")
     return public_key
Exemple #9
0
def validate_lte(value: Any, maximum: int) -> None:
    validate_integer(value)
    if value > maximum:
        raise ValidationError(
            "Value {0} is not less than or equal to {1}".format(
                value,
                maximum,
            ))
Exemple #10
0
def validate_gte(value: Any, minimum: int) -> None:
    validate_integer(value)
    if value < minimum:
        raise ValidationError(
            "Value {0} is not greater than or equal to {1}".format(
                value,
                minimum,
            ))
Exemple #11
0
def validate_bytes_length(value: bytes, expected_length: int,
                          name: str) -> None:
    actual_length = len(value)
    if actual_length != expected_length:
        raise ValidationError(
            "Unexpected {name} length: Expected {expected_length}, but got {actual_length} "
            "bytes".format(
                name=name,
                expected_length=expected_length,
                actual_length=actual_length,
            ))
Exemple #12
0
 def ecdsa_verify(
     self,
     message_hash,  # type: bytes
     signature,  # type: Union[Signature, bytes]
     public_key  # type: Union[PublicKey, bytes]
 ):
     # type: (...) -> Optional[bool]
     if not isinstance(public_key, PublicKey):
         raise ValidationError(
             "The `public_key` must be an instance of `eth_keys.datatypes.PublicKey`"
         )
     return self.ecdsa_recover(message_hash, signature) == public_key
Exemple #13
0
def validate_bytes(value: Any) -> None:
    if not is_bytes(value):
        raise ValidationError("Value must be a byte string.  Got: {0}".format(
            type(value)))
Exemple #14
0
def validate_signature_bytes(value: Any) -> None:
    validate_bytes(value)
    if len(value) != 65:
        raise ValidationError(
            "Unexpected signature format.  Must be length 65 byte string")
Exemple #15
0
def validate_private_key_bytes(value: Any) -> None:
    validate_bytes(value)
    if len(value) != 32:
        raise ValidationError(
            "Unexpected private key format.  Must be length 32 byte string")
Exemple #16
0
def validate_public_key_bytes(value: Any) -> None:
    validate_bytes(value)
    if len(value) != 64:
        raise ValidationError(
            "Unexpected public key format.  Must be length 64 byte string")
Exemple #17
0
def validate_message_hash(value):
    validate_bytes(value)
    if len(value) != 32:
        raise ValidationError(
            "Unexpected signature format.  Must be length 65 byte string")
Exemple #18
0
def validate_integer(value: Any) -> None:
    if not is_integer(value) or isinstance(value, bool):
        raise ValidationError("Value must be a an integer.  Got: {0}".format(
            type(value)))