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_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 AttributeDict({ '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), )
def sign(self, message) -> bytes: if isinstance(message, str): message = bytes(message, 'utf-8') if not isinstance(message, bytes): raise ValueError if self._mode == EcdsaModes.SECP256K1_KECCAK_256_ETHEREUM: message_hash = defunct_hash_message(message) (_, _, _, signature) = sign_message_hash(self._private_key_object, message_hash) else: hash_function = '_'.join(self._mode.split('_')[1:]).lower() message_hash = hash_message(message, hash_function) signature = self._private_key_object.sign( message_hash, ec.ECDSA(getattr(hashes, hash_function.upper())()), ) return signature