def sign_message(self, message: EIP712Message) -> SignedMessage: """Signs an `EIP712Message` using this account's private key. Args: message: An `EIP712Message` instance. Returns: An eth_account `SignedMessage` instance. """ # some of this code is from: # https://github.com/ethereum/eth-account/blob/00e7b10/eth_account/account.py#L577 # https://github.com/ethereum/eth-account/blob/00e7b10/eth_account/account.py#L502 msg_hash_bytes = HexBytes( _hash_eip191_message(message.signable_message)) assert len( msg_hash_bytes) == 32, "The message hash must be exactly 32-bytes" eth_private_key = eth_keys.keys.PrivateKey(HexBytes(self.private_key)) (v, r, s, eth_signature_bytes) = sign_message_hash(eth_private_key, msg_hash_bytes) return SignedMessage( messageHash=msg_hash_bytes, r=r, s=s, v=v, signature=HexBytes(eth_signature_bytes), )
def _sign_hash(self, message_hash, private_key): msg_hash_bytes = HexBytes(message_hash) if len(msg_hash_bytes) != 32: raise ValueError("The message hash must be exactly 32-bytes") key = self._parsePrivateKey(private_key) (v, r, s, eth_signature_bytes) = sign_message_hash(key, msg_hash_bytes) return SignedMessage( messageHash=msg_hash_bytes, r=r, s=s, v=v, signature=HexBytes(eth_signature_bytes), )
def sign_defunct_message(self, message: str) -> SignedMessage: """Signs an `EIP-191` using this account's private key. Args: message: An text Returns: An eth_account `SignedMessage` instance. """ msg_hash_bytes = defunct_hash_message(text=message) eth_private_key = eth_keys.keys.PrivateKey(HexBytes(self.private_key)) (v, r, s, eth_signature_bytes) = sign_message_hash(eth_private_key, msg_hash_bytes) return SignedMessage( messageHash=msg_hash_bytes, r=r, s=s, v=v, signature=HexBytes(eth_signature_bytes), )