[英]How to call a contract function from a specific address
I want to use web3 to call a contract function(sendTransaction not call) from a specific address that I hold the private key to. 我想使用web3从我持有私钥的特定地址调用合同功能(不调用sendTransaction)。 I also have the contract instance connected.
我还连接了合同实例。 I know I have to dio something like web3.personal.unlockAccount(address,password,duration) And then specify the from address.
我知道我必须像web3.personal.unlockAccount(address,password,duration)之类的东西,然后指定发件人地址。 So my question is, is "password" where I enter my private key?
所以我的问题是,“密码”是我输入私钥的地方吗? And how do I pass that address into fromAddress once I unlock it.
解锁后如何将该地址传递给fromAddress。 Or is there a more streamlined way to go about it?
还是有更简化的方法来解决这个问题?
is "password" where I enter my private key?
输入私钥的“密码”在哪里?
No , it's not. 不 ,不是。 Actually it is a password, which you entered when you were creating account and which was used to generate your keystore file.
实际上,它是一个密码,您在创建帐户时输入该密码,并用于生成密钥库文件。
If you use web3.personal.unlockAccount(address,password,duration)
you need to have your keystore file in the keystore
folder near chaindata of geth. 如果使用
web3.personal.unlockAccount(address,password,duration)
,则需要将密钥keystore
文件保存在geth的chaindata附近的keystore
文件夹中。
Another way will be to use web3.eth.sendRawTransaction
from web3.py
, but it will be a little bit clunky to call contract method from it. 另一种方法使用起来会
web3.eth.sendRawTransaction
从web3.py
,但是这将是笨重从中调用合同法一点点。
key = '0x' + 'YOUR-PRIVATE-KEY
transaction = {
'to': _to,
'value': 0,
'gas': 300000,
'gasPrice': 23000000000,
'nonce': web3.eth.getTransactionCount(_from),
'chainId': int(web3.admin.nodeInfo['protocols']['eth']['network']),
'data': b''
}
signed = web3.eth.account.signTransaction(transaction, key)
print("Transaction Hash: " + web3.toHex(web3.eth.sendRawTransaction(signed.rawTransaction)))
My suggestion will be to create keyfile recognisable by Ethereum clients by using web3.eth.account.encrypt
( web3.py
because web3.js
yet lack this functionality) and after you generate it put it in the right folder and try simply to unlock account. 我的建议是通过使用
web3.eth.account.encrypt
( web3.py
因为web3.js
尚缺乏此功能)来创建以太坊客户端可识别的密钥文件,并在生成密钥文件后将其放在正确的文件夹中并尝试简单地解锁帐户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.