def eth_sign(message: bytes, web3: Web3): assert (isinstance(message, bytes)) assert (isinstance(web3, Web3)) local_account = _registered_accounts.get( (web3, Address(web3.eth.defaultAccount))) if local_account: message_hash = defunct_hash_message(primitive=message) signature = web3.eth.account.signHash( message_hash, private_key=local_account.privateKey).signature.hex() return signature else: signature = bytes_to_hexstring( web3.manager.request_blocking( "eth_sign", [web3.eth.defaultAccount, encode_hex(message)], )) # for `EthereumJS TestRPC/v2.2.1/ethereum-js` if signature.endswith("00"): signature = signature[:-2] + "1b" if signature.endswith("01"): signature = signature[:-2] + "1c" return signature
def eth_sign(message: bytes, web3: Web3): assert(isinstance(message, bytes)) assert(isinstance(web3, Web3)) local_account = _registered_accounts.get((web3, Address(web3.eth.defaultAccount))) if local_account: start_time = time.time() start_clock = time.clock() try: message_hash = defunct_hash_message(primitive=message) signature = web3.eth.account.signHash(message_hash, private_key=local_account.privateKey).signature.hex() finally: end_time = time.time() end_clock = time.clock() logging.debug(f"Local signing took {end_time - start_time:.3f}s time, {end_clock - start_clock:.3f}s clock") return signature else: signature = bytes_to_hexstring(web3.manager.request_blocking( "eth_sign", [web3.eth.defaultAccount, encode_hex(message)], )) # for `EthereumJS TestRPC/v2.2.1/ethereum-js` if signature.endswith("00"): signature = signature[:-2] + "1b" if signature.endswith("01"): signature = signature[:-2] + "1c" return signature