下面是由levelup.gitconnected编写的题为“面向Python程序员的levelup.gitconnected开发”的教程。网址是https://levelup.gitconnected.com/dapps-development-for-python-developers-f52b32b54f28。在本教程中,有一个名为deploy.py的python文件,如下所示
*deploy.py
import json
from web3 import Web3, HTTPProvider
from web3.contract import ConciseContract
# web3.py instance
w3 = Web3(HTTPProvider("https://ropsten.infura.io/v3/<API key>"))
print(w3.isConnected())
key="<Private Key here with 0x prefix>"
acct = w3.eth.account.privateKeyToAccount(key)
# compile your smart contract with truffle first
truffleFile = json.load(open('./build/contracts/greeter.json'))
abi = truffleFile['abi']
bytecode = truffleFile['bytecode']
contract= w3.eth.contract(bytecode=bytecode, abi=abi)
#building transaction
construct_txn = contract.constructor().buildTransaction({
'from': acct.address,
'nonce': w3.eth.getTransactionCount(acct.address),
'gas': 1728712,
'gasPrice': w3.toWei('21', 'gwei')})
signed = acct.signTransaction(construct_txn)
tx_hash=w3.eth.sendRawTransaction(signed.rawTransaction)
print(tx_hash.hex())
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
print("Contract Deployed At:", tx_receipt['contractAddress'])在第10行(key=“")上,当我输入带有0x前缀的秘密密钥时,会收到下面发布的错误消息:
ValueError:私钥必须是32字节,而不是34字节
我尝试不使用0x前缀,得到了错误:
ValueError:私钥必须是32字节,而不是32字节。
我遗漏了什么?提前谢谢你。
发布于 2021-09-01 19:50:10
不久前,我遇到了一个类似的问题。此错误很可能是由于您应该使用ETH并链接已资助的钱包私钥时意外地使用Infura“项目机密”作为私钥造成的。一定要先把获取测试ETH和链接令牌放进钱包。
发布于 2021-10-01 20:41:12
我在混淆私钥和其他密钥和地址时遇到了这个问题。使用MetaMask,可以在“帐户详细信息>导出私钥”下面找到正确的私钥。
发布于 2022-02-10 02:46:56
确保您来自恩弗拉的测试器在您正在使用的网络上,即: ropsten。
https://ethereum.stackexchange.com/questions/106455
复制相似问题