简体   繁体   English

如何从特定地址调用合同功能

[英]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.sendRawTransactionweb3.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.encryptweb3.py因为web3.js尚缺乏此功能)来创建以太坊客户端可识别的密钥文件,并在生成密钥文件后将其放在正确的文件夹中并尝试简单地解锁帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Node.js调用特定的python函数? - How to call a specific python function from nodejs? 调用Web3合约发送功能节点 - Call Web3 contract send function node 如何在使用 infura 部署在 ropsten 测试网上的智能合约中调用 setter 函数 - How to call a setter function in a smart contract which is deployed on ropsten testnet using infura 如何用WalletConnect调用智能合约function? (React.js, Node.js) - How to call a smart contract function with WalletConnect? (React.js, Node.js) 使用web3.js部署时如何获取智能合约地址 - How to get Smart Contract address when it is deployed with web3.js 如何在同一个 javascript 程序中部署和获取智能合约的地址 - How to deploy and get address of smart contract in same javascript program 如何在来自 Node 的 Axios 调用中检索远程 IP 地址 - How to retrieve remote IP address in an Axios call from Node 如何从ipify函数返回IP地址以用于其他功能? - How to return ip address from ipify function for use in other function? 如何从另一个函数调用Express函数? - How to call Express function from another function? 如何在 JavaScript 中以特定的偏移量/间隔持续时间调用函数/通知? - How to call a function/notification at specific offset/interval durations in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM