5 Java调用合约方法时出现invalid sender错误,但是无法定位到具体原因是什么,麻烦各位大佬帮忙瞅瞅

合约功能没有问题,通过remix可以正常执行,但是Java调用的时候出现了invalid sender错误 1:合约部署在https://sepolia.etherscan.io/ ETH测试链上 这个测试链的ID我找的是:11155111 RPC URL是:https://eth.getblock.io/d83c4ddb-23e8-4487-b8d7-8de29c7a3f1b/sepolia/

2:合约地址:0x09466b1d1161c870C6E48791519ac1F20C8737Ee 出现的问题如下图:

a13119bb982458116ce8c39a9572410.png

Java代码如下:

package com.block.chain.utils;

import com.blockchain.tools.eth.codec.EthAbiCodecTool;
import com.blockchain.tools.eth.contract.util.EthContractUtil;
import com.blockchain.tools.eth.contract.util.model.SendResultModel;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Numeric;

import java.math.BigInteger;
import java.util.List;

public class ContractUtils {
    private Web3j web3j;

    public static void main(String args[]) throws Exception {
        String privateKey = ""; // 私钥
        Web3j web3j = Web3j.build(new HttpService("https://eth.getblock.io/d83c4ddb-23e8-4487-b8d7-8de29c7a3f1b/sepolia/")); // 链的RPC地址
        String contractAddress = "0x09466b1d1161c870C6E48791519ac1F20C8737Ee";
        String senderAddress = "0xb147533b5953EF2Ae351A1237Ab75279265db499";

        SendResultModel sendResultModel = sendRawTransaction(
                senderAddress, // 调用者的地址
                contractAddress, // 合约地址
                privateKey, // senderAddress的私钥
                new BigInteger("3000000"), // gasPrice,如果想用默认值 可以直接传null,或者不传这个参数
                new BigInteger("3000000"), // gasLimit,如果想用默认值 可以直接传null,或者不传这个参数
                EthAbiCodecTool.getInputData(
                        "addAddress", // 要调用的方法名称
                        new Address("0x2D85B9F92a2774779C37c2f8b5E5c7367a36052f")
                ) // 要调用的方法的inputData
        );
        sendResultModel.getEthSendTransaction(); // 发送交易后的结果
        sendResultModel.getEthGetTransactionReceipt(); // 交易成功上链后的结果
        String s="";

    }

    private static SendResultModel sendRawTransaction(String fromAddress, String toAddress, String privateKey, BigInteger gasPrice, BigInteger gasLimit, String inputData) throws Exception {
        Web3j web3j = Web3j.build(new HttpService("https://eth.getblock.io/d83c4ddb-23e8-4487-b8d7-8de29c7a3f1b/sepolia/")); // 链的RPC地址
        EthGetTransactionCount ethGetTransactionCount = (EthGetTransactionCount)web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST).send();
        BigInteger nonce = ethGetTransactionCount.getTransactionCount();
        if (gasPrice == null) {
            gasPrice = ((EthGasPrice)web3j.ethGasPrice().send()).getGasPrice();
        }

        if (gasLimit == null) {
            gasLimit = new BigInteger("8000000");
        }

//        RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, toAddress, inputData);
        RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, toAddress, inputData);

        // 通过私钥创建credentials对象
        Credentials credentials = Credentials.create(privateKey);
        String send = credentials.getAddress();
        int chainId = 11155111;
        // 使用TransactionEncoder类的signMessage方法签名交易,并设置EIP155为true
        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, (byte) chainId, credentials);

//        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, Credentials.create(privateKey));
        String hexValue = Numeric.toHexString(signedMessage);

        EthSendTransaction ethSendTransaction = (EthSendTransaction)web3j.ethSendRawTransaction(hexValue).send();
        if (ethSendTransaction.hasError()) {
            throw new Exception(ethSendTransaction.getError().getMessage());
        } else {
            SendResultModel sendResultModel = new SendResultModel();
            sendResultModel.setEthSendTransaction(ethSendTransaction);

            for(int i = 0; i < 60; ++i) {
                EthGetTransactionReceipt ethGetTransactionReceipt = (EthGetTransactionReceipt)web3j.ethGetTransactionReceipt(ethSendTransaction.getTransactionHash()).send();
                sendResultModel.setEthGetTransactionReceipt(ethGetTransactionReceipt);
                if (ethGetTransactionReceipt != null && ethGetTransactionReceipt.getResult() != null) {
                    break;
                }

                Thread.sleep(3000L);
            }

            return sendResultModel;
        }
}}
请先 登录 后评论

4 个回答

Tiny熊
  擅长:智能合约,以太坊
请先 登录 后评论
用户_14664
请先 登录 后评论
路人乙
请先 登录 后评论
杨大敏 - 架构师
请先 登录 后评论
  • 4 关注
  • 0 收藏,2199 浏览
  • 醒梦无痕 提出于 2023-08-03 19:35