You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Infura调用eth_sendRawTransaction时遇'invalid sender'错误及后续问题

问题:Polygon链代币转账签名后发送报错"invalid sender"及解决过程

使用ethereumjs-tx(^2.1.2)对Polygon网络代币合约Transfer函数交易签名,通过PHP发送到Infura的eth_sendRawTransaction接口时,始终返回{"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"invalid sender"}}(已确认私钥正确)。后续改用Infura的Web3示例代码时,因伊朗IP触发countryBlocked错误,改用Chainstack节点并添加硬编码gasPrice后代码正常运行。各阶段代码如下:

初始签名代码

const Tx = require('ethereumjs-tx').Transaction
const argv = require('minimist')(process.argv.slice(2));
const privateKey = Buffer.from('mykey');

var rawTx = {
  chainId: 0x89,
  nonce: argv.nonce,
  gasPrice: '0x174876E800', 
  gasLimit: '0x186A0',
  to: '0x666CBED29287F0dee02a1A925F432f07CFbFECD5',
  value: '0x0', 
  data: argv.data
}

const tx = new Tx(rawTx)
tx.sign(privateKey)

const serializedTx = tx.serialize()
console.log(serializedTx.toString('hex'))

PHP发送代码

//gettxcount() works properly
$nonce = "0x" . dechex(hexdec(json_decode(gettxcount())->result) + 1);
echo "nonce: " . $nonce . "\n";

//Transfer function ABI, plus the parameters sent
$txdata = "0xa9059cbb000000000000000000000000".
substr($address, 2).
str_pad(base_convert(number_format($amount * 10**18, 0, "", ""), 10, 16), 64, '0', STR_PAD_LEFT);

echo "txdata: " . $txdata . "\n";
$RLP = "0x" . exec("node signer/signTransaction.js --nonce=$nonce --data=$txdata");

echo "RLP: " . $RLP . "\n";
$curld = curl_init();
curl_setopt_array($curld, [
    CURLOPT_URL => "https://polygon-mainnet.infura.io/v3/$API_KEY",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode([
      'id' => 1,
      'jsonrpc' => '2.0',
      'method' => 'eth_sendRawTransaction',
      'params' => [
        $RLP
      ]
      ]),
        CURLOPT_HTTPHEADER => [
          "accept: application/json",
          "content-type: application/json"
          ],
      ]);
$output = curl_exec($curld);
curl_close($curld);
return $output;

更新后的可行Web3代码

const { Web3 } = require("web3");
const argv = require('minimist')(process.argv.slice(2));

async function main() {
  // Configuring the connection to the Polygon node
  const web3 = new Web3(
    new Web3.providers.HttpProvider(
      `https://polygon-mainnet.core.chainstack.com/APIKEY`, //Fill in the API KEY
    ),
  );
  // Creating a signing account from a private key
  const privateKey = Buffer.from('PRIVATE_KEY', 'hex'); //Fill in the PRIVATE KEY
  const signer = web3.eth.accounts.privateKeyToAccount(
    privateKey,
  );
  web3.eth.accounts.wallet.add(signer);
  // Creating the transaction object
  const tx = {
    gasPrice: '0x174876E800', //Manually set gasPrice
    from: signer.address,
    to: "SMART CONTRACT", //Fill in the smart contract address
    value: "0x0",
    data: argv.data,
  };
  // Assigning the right amount of gas
  tx.gas = await web3.eth.estimateGas(tx);
  console.log(tx.gas);
  // Sending the transaction to the network
  const receipt = await web3.eth
    .sendTransaction(tx)
    .once("transactionHash", (txhash) => {
      console.log(`Mining transaction ...`);
      console.log(`Transaction hash: ${txhash}`);
    });
  // The transaction is now on chain!
  console.log(`Mined in block ${receipt.blockNumber}`);
}

main();

注:Infura原示例代码未包含gasPrice,导致'replacement transaction underpriced'错误,添加硬编码gasPrice后问题解决。

内容的提问来源于stack exchange,提问作者Amirreza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 18:17:46