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 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 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
# 初始化默认chainId web3.thk.defaultChainId = "2" # 测试发送一笔交易 Transfer() # 测试发布合约 # contractAddress = ReleaseContract(cotractName, contractText) # print(contractAddress) # getcontract = web3.thk.getContract(contractAddress) getcontract = web3.thk.compileContract(web3.thk.defaultChainId, contractText) abi = getcontract[cotractName]["info"]["abiDefinition"] contract_bin = remove_0x_prefix(getcontract[cotractName]["code"]) Greeter = web3.thk.contract(abi=abi, bytecode=contract_bin) # 构造函数 tx_hash = Greeter.constructor().transact() print("tx_hash=", tx_hash) # 等待交易执行完成 tx_receipt = web3.thk.waitForTransactionReceipt(web3.thk.defaultChainId, tx_hash["TXhash"]) if tx_receipt["status"] == 1: print("合约:", cotractName, "发布成功") else: print("合约:", cotractName, "发布失败") print("contractAddress=", tx_receipt['contractAddress']) # 初始化合约对象