def private_key_to_public_key(self, private_key: _PrivateKey) -> _PublicKey: if not isinstance(private_key, PrivateKey): raise ValidationError( "The `private_key` must be an instance of `platon_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 `platon_keys.datatypes.PublicKey`") return public_key
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 `platon_keys.datatypes.PublicKey`" ) if not isinstance(signature, BaseSignature): raise ValidationError( "The `signature` must be an instance of `platon_keys.datatypes.BaseSignature`" ) return self.backend.ecdsa_verify(message_hash, signature, public_key)
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 `platon_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 `platon_keys.datatypes.PublicKey`") return public_key
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 `platon_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 `platon_keys.datatypes.Signature`") return signature
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), ))
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, ))
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, ))
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, ))
def validate_bytes(value: Any) -> None: if not is_bytes(value): raise ValidationError("Value must be a byte string. Got: {0}".format( type(value)))
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)))