web3p / ethereum-tx

Ethereum transaction library in PHP.
https://www.web3p.xyz/ethereumtx.html
MIT License
183 stars 63 forks source link

Transaction(EIP1559Transaction) hash doesn't match the real one from server #41

Closed sajadkhosravani1 closed 2 years ago

sajadkhosravani1 commented 3 years ago

Hello and thanks for developing this library. I noticed that an EIP1559Transaction unlike legacy Transaction objects has the same hash before and after signing the transaction. For legacy transactions I recreate the transaction object by parsing the signed raw transaction using the "Transaction(string $raw)" constructor and then I call hash() function so I get the final hash of the transaction. But for EIP1559Transaction there's no way to get final transaction hash. In this case calling signed and unsigned transactions hash() function have the same response and it's of course not the same as what server computes and returns as response of "eth_sendRawTransaction". With best regards.

sajadkhosravani1 commented 3 years ago

Code appended to previous comment:

require "lib.php";
use Web3p\EthereumTx\Transaction;
use Web3p\EthereumTx\EIP1559Transaction;
$res = rpcInvoke("eth_getTransactionCount",["0x1c82bf96916743bce021d60d24095c2b1a888687", 'pending']);
// unset($res['transactions']);
$transaction = new Transaction([
    'nonce' => $res ,
    'from' => '0x1c82bf96916743bce021d60d24095c2b1a888687',
    'to' => '0xcDA0D6adCD0f1CCeA6795F9b1F23a27ae643FE7C',
    'gas' => '0x5208',
    'gasPrice' => '0x' . dechex(70 * 10**9),
    'chainId'   => 3,
    'value' => '0x' . dechex(0.01 * 10**18),
    'data' => ''
]);

$transaction = new EIP1559Transaction([
    'nonce' => $res,
    'from' => '0x1c82bf96916743bce021d60d24095c2b1a888687',
    'to' => '0xcDA0D6adCD0f1CCeA6795F9b1F23a27ae643FE7C',
    'maxPriorityFeePerGas' => '0x' . dechex(70 * 10**9),
    'maxFeePerGas' => '0x' . dechex(100 * 10**9),
    'gas' => '0x5208',
    'value' =>  '0x' . dechex(0.01 * 10**18),
    'chainId' => 3,
    'accessList' => [],
    'data' => ''
]);
echo "before sign: ";
echo '0x' . $transaction->hash() . PHP_EOL;
$signed = $transaction->sign("0x1a56ee986126833163dbc7335b74ec8d1b03c1f9f2ff4acb5d438a012a967d76");
$transaction = new EIP1559Transaction("0x$signed");
echo "after sign: ";
echo '0x' . $transaction->hash() . PHP_EOL;
$res = rpcInvoke("eth_sendRawTransaction",["0x$signed"]);
echo "Server response result: " ;
var_dump($res);  

Result:

before sign: 0x27a16bc2e5ed47d9fc4840cced8f83407d835bb9127681b7a5ecc994fdeb52fb
after sign: 0x27a16bc2e5ed47d9fc4840cced8f83407d835bb9127681b7a5ecc994fdeb52fb
Server response result: string(66) "0xdba32a4a8df5cd515151145bf0103a102a770fac9f45959057ce096911fb222e"
sc0Vu commented 3 years ago

@sajadkhosravani1 Thanks, you're right! I didn't hash the signature together.

I've push fix in PR.