【问题标题】:How to call a contract function from a specific address如何从特定地址调用合约函数
【发布时间】:2018-07-12 14:50:26
【问题描述】:

我想使用 web3 从我持有私钥的特定地址调用合约函数(sendTransaction 不调用)。我还连接了合同实例。我知道我必须 dio 类似 web3.personal.unlockAccount(address,password,duration) 然后指定发件人地址。 所以我的问题是,我输入私钥的地方是“密码”吗?以及解锁后如何将该地址传递给 fromAddress 。 还是有更精简的方法来解决它?

【问题讨论】:

    标签: node.js ethereum solidity


    【解决方案1】:

    是我输入私钥的“密码”吗?

    ,不是。实际上它是一个密码,您在创建帐户时输入并用于生成您的密钥库文件。


    如果您使用web3.personal.unlockAccount(address,password,duration),您需要将您的密钥库文件放在geth 的chaindata 附近的keystore 文件夹中。


    另一种方法是使用web3.py 中的web3.eth.sendRawTransaction,但从中调用合约方法会有点笨拙。

    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)))
    

    我的建议是使用web3.eth.account.encryptweb3.py 因为web3.js 缺少此功能)创建以太坊客户端可识别的密钥文件,并在生成后将其放入正确的文件夹并尝试简单地解锁帐户。

    【讨论】:

      猜你喜欢
      • 2020-12-31
      • 2021-01-20
      • 1970-01-01
      • 2022-01-04
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      相关资源
      最近更新 更多