Signature

生成原始 trx 並將其發送到網路顯示“無效發件人”錯誤

  • September 2, 2018

我正在嘗試通過php和這些庫進行乙太坊原始交易:

https://github.com/simplito/elliptic-php

https://github.com/kornrunner/php-keccak

https://github.com/web3p/rlp

這是我的程式碼:

use Elliptic\EC;
use kornrunner\Keccak;
use Web3p\RLP\RLP;

$privateKeyHex = '.....'; // wallet private key
$toWallet = '118086be6247fBDa3BC64B4A11F07F3894aA1fAF';

$ec = new EC('secp256k1');
$key = $ec->keyFromPrivate($privateKeyHex );
$publicKeyHex = $key->getPublic('hex');
// $publicKeyHex => 0445e2caf0f227247dfa10440765812492e4d4c9df7b4e74d0d5cd3279fa80f5ef987a70e061ca20c06f09690957c9ba365cf06541181d1291e14c847d0d826583

$nonce= 0;
$gasPrice = 1e10;
$gasLimit = 21000;
$to = hex2bin($toWallet);
$value = 1e15-($gasLimit*$gasPrice)-1;
$inputData = 0;
//*********** EIP_155 *********
$chain_id = 1;
$r = 0;
$s = 0;
//*****************************

$SignData = [$nonce,$gasPrice,$gasLimit,$to,$value,$inputData,$chain_id,$r,$s];
$SignRlpData = rlpEncode($SignData);
$signHash = Keccak::hash(hex2bin($SignRlpData), 256);

$signature = $ec->sign($signHash ,$key);
$r = $signature->r->toString('hex');
$s = $signature->s->toString('hex');
$v = $chain_id*2 + ($signature->recoveryParam +35);

$trxData = [$nonce,$gasPrice,$gasLimit,$to,$value,$inputData,$v,hex2bin($r),hex2bin($s)];
$trxRlpData = rlpEncode($trxData );
// trxRlpData => f86b808502540be40082520894118086be6247fbda3bc64b4a11f07f3894aa1faf8702ce80355f5fff8026a0d879bd4319788f5e75fba039f879f7b38f25a80d1b6768f8c80d2e0dec7dc11aa0d952e26360e25f95821fc7f92d231925cd0cd1c412fc1d4b5bbdb439fbf0f19b




function rlpEncode($a){
   $rlp = new RLP;
   $encodedBuffer = $rlp->encode($a);
   return $encodedBuffer->toString('hex');
}

現在,在我通過https://etherscan.io/pushTx$trxRlpData向乙太坊網路發送值後,顯示以下錯誤消息:

Error! Unable to broadcast Tx : {"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"invalid sender"}}

但問題出在哪裡?

最後我發現問題出在 $gasPrice$gasLimit和*$value*變數的錯誤值上。

進行這些更改後,目前問題已得到修復:

$gasPrice = 1e9;
$value = 1e14-($gasLimit*$gasPrice);

但我遇到了另一個錯誤,稱為交易低估。最後我通過增加一點*$gasLimit*解決了這個問題。

$gasLimit = 21001;

引用自:https://ethereum.stackexchange.com/questions/57519