def signTransaction(self, transaction_dict, private_key): if not isinstance(transaction_dict, Mapping): raise TypeError("transaction_dict must be dict-like, got %r" % transaction_dict) # check and set default if 'useLocal' not in transaction_dict: transaction_dict.setdefault('useLocal', False) if 'extra' not in transaction_dict: transaction_dict.setdefault('extra', "") user_local = "0" if transaction_dict["useLocal"]: user_local = "1" sign_str = transaction_dict["chainId"] + '-' + remove_0x_prefix(transaction_dict["from"].lower()) + '-' + \ remove_0x_prefix(transaction_dict["to"].lower()) + '-' + transaction_dict["nonce"] + '-' + \ user_local + '-' + transaction_dict["value"] + '-' + \ remove_0x_prefix(transaction_dict["input"].lower()) sign_str = sign_str + '-' if transaction_dict["extra"] != '': sign_str = sign_str + transaction_dict["extra"] sign_bytes = to_bytes(text=sign_str) res = eth_utils_keccak(sign_bytes) sign_hash = self.account.signHash(to_hex(res), private_key=private_key) transaction_dict["sig"] = to_hex(sign_hash.signature) pk = keys.PrivateKey(private_key) transaction_dict["pub"] = "0x04" + pk.public_key.to_hex()[2:] return transaction_dict
def test_eth_account_encrypt(acct, web3js_key, web3js_password): encrypted = acct.encrypt(web3js_key, web3js_password) assert encrypted['address'] == '2c7536e3605d9c16a7a3d7b1898e529396a65c23' assert encrypted['version'] == 3 decrypted_key = acct.decrypt(encrypted, web3js_password) assert decrypted_key == to_bytes(hexstr=web3js_key)
def keccak(primitive=None, text=None, hexstr=None): if isinstance(primitive, (bytes, int, type(None))): input_bytes = to_bytes(primitive, hexstr=hexstr, text=text) return eth_utils_keccak(input_bytes) raise TypeError( "You called keccak with first arg %r and keywords %r. You must call it with one of " "these approaches: keccak(text='txt'), keccak(hexstr='0x747874'), " "keccak(b'\\x74\\x78\\x74'), or keccak(0x747874)." % (primitive, { 'text': text, 'hexstr': hexstr }))
def signTransaction(self, transaction_dict, private_key): if not isinstance(transaction_dict, Mapping): raise TypeError("transaction_dict must be dict-like, got %r" % transaction_dict) sign_str = transaction_dict["chainId"] + remove_0x_prefix(transaction_dict["from"].lower()) + \ remove_0x_prefix(transaction_dict["to"].lower()) + transaction_dict["nonce"] + \ transaction_dict["value"] + remove_0x_prefix(transaction_dict["input"].lower()) sign_bytes = to_bytes(text=sign_str) res = eth_utils_keccak(sign_bytes) sign_hash = self.account.signHash(to_hex(res), private_key=private_key) print("sign_hash=", sign_hash) transaction_dict["sig"] = to_hex(sign_hash.signature) pk = keys.PrivateKey(private_key) transaction_dict["pub"] = "0x04" + pk.public_key.to_hex()[2:] return transaction_dict
def signTransaction(transaction_dict, private_key): FULL_NODE_HOSTS = 'http://192.168.1.13:8089' provider = HTTPProvider (FULL_NODE_HOSTS) web3 = Web3 (provider) if not isinstance(transaction_dict, Mapping): raise TypeError("transaction_dict must be dict-like, got %r" % transaction_dict) sign_str = transaction_dict["chainId"] + remove_0x_prefix(transaction_dict["from"].lower()) + \ remove_0x_prefix(transaction_dict["to"].lower()) + transaction_dict["nonce"] + \ transaction_dict["value"] + remove_0x_prefix(transaction_dict["input"].lower()) sign_bytes = to_bytes(text=sign_str) res = eth_utils_keccak(sign_bytes) sign_hash = web3.eth.account.signHash(to_hex(res), private_key=private_key) transaction_dict["sig"] = to_hex(sign_hash.signature) pk = keys.PrivateKey(private_key) transaction_dict["pub"] = "0x04" + pk.public_key.to_hex()[2:] print(transaction_dict) return transaction_dict
def get_public_key_from_address(web3, account): """ :param web3: :param account: :return: """ _hash = web3.keccak(text='verify signature.') signature = web3.sign(_hash, account.address, account.password) signature = split_signature(web3, to_bytes(hexstr=signature)) signature_vrs = Signature(signature.v % 27, big_endian_to_int(signature.r), big_endian_to_int(signature.s)) prefixed_hash = prepare_prefixed_hash(_hash) pub_key = KeyAPI.PublicKey.recover_from_msg_hash( prefixed_hash, KeyAPI.Signature(vrs=signature_vrs)) assert pub_key.to_checksum_address() == account.address, \ 'recovered address does not match signing address.' return pub_key
def signTransaction(transaction_dict, private_key): FULL_NODE_HOSTS = 'http://192.168.1.13:8089' provider = HTTPProvider(FULL_NODE_HOSTS) web3 = Web3(provider) if not isinstance(transaction_dict, Mapping): raise TypeError("transaction_dict must be dict-like, got %r" % transaction_dict) sign_str = transaction_dict["chainId"] + remove_0x_prefix(transaction_dict["from"].lower()) + \ remove_0x_prefix(transaction_dict["to"].lower()) + transaction_dict["nonce"] + \ transaction_dict["value"] + remove_0x_prefix(transaction_dict["input"].lower()) sign_bytes = to_bytes(text=sign_str) res = eth_utils_keccak(sign_bytes) sign_hash = web3.eth.account.signHash(to_hex(res), private_key=private_key) transaction_dict["sig"] = to_hex(sign_hash.signature) # pk = keys.PrivateKey(private_key) # transaction_dict["pub"] = "0x04" + pk.public_key.to_hex()[2:] transaction_dict[ "pub"] = "0x043d85aa2a649fa5fd421988cebff58d7173f7b563b8a9594e92bcf3e9f5e43037c3463121af51aacc8a8cf2d8cfcc6fa717b774fc0aceec04d7185c87e279c1f6" return transaction_dict
def create_account(): from eth_utils import ( keccak, ) from web3._utils.encoding import ( to_bytes, ) from eth_utils import ( to_hex, ) from eth_keys import (keys) # acct = web3.account.Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530') acct = account.Account.privateKeyToAccount(keccak(to_bytes(text="456"))) private_key = to_hex(acct.privateKey) print("privateKey:", private_key) pk = keys.PrivateKey(acct.privateKey) public_key = "0x04" + pk.public_key.to_hex()[2:] print("public_key:", pk.public_key.to_hex()[2:]) print("public_key:", public_key) print(acct.address) print("address:", acct.address)