amashukov/eip1559-tx-signer-php
Installation:
composer require amashukov/eip1559-tx-signer-php
Ensure your project meets the requirements: PHP 8.3+ and ext-gmp.
First Use Case: Sign a basic EIP-1559 transaction:
use Amashukov\Eip1559TxSigner\Eip1559Signer;
$signer = new Eip1559Signer('0xYOUR_PRIVATE_KEY', chainId: 1);
$rawTx = $signer->sign(
to: '0xRECIPIENT_ADDRESS',
valueWei: '0',
data: '0x',
nonce: 0,
gasLimit: 21000,
maxFeePerGas: '30000000000',
maxPriorityFeePerGas: '1500000000'
);
Where to Look First:
Eip1559Signer class: Core class for signing transactions.address() method: Derive the sender address from the private key.sign() method: Assemble and sign the transaction.Transaction Assembly:
Use the sign() method to create and sign transactions with EIP-1559 parameters:
$rawTx = $signer->sign(
to: '0xRecipientAddress',
valueWei: '1000000000000000000', // 1 ETH in wei
data: '0xFunctionSelectorAndArgs',
nonce: $nonceFromBlockchain,
gasLimit: 200000,
maxFeePerGas: '50000000000', // 50 gwei
maxPriorityFeePerGas: '2000000000' // 2 gwei
);
Deterministic Signing: Leverage RFC 6979 for reproducible signatures:
$samePrivateKeyAndTx = $signer->sign(...); // Always produces the same rawTx
Address Derivation: Derive the sender address from the private key:
$address = $signer->address(); // Returns the Ethereum address
Placeholder Mode: Use an empty private key for testing/dev environments:
$signer = new Eip1559Signer(''); // Placeholder mode
Integration with Laravel:
config or environment variables).Eip1559Signer instance to services:
$this->app->bind(Eip1559Signer::class, function ($app) {
return new Eip1559Signer(config('ethereum.private_key'), chainId: config('ethereum.chain_id'));
});
Batch Transactions: Loop through transactions and sign them sequentially:
foreach ($transactions as $tx) {
$rawTx = $signer->sign($tx);
$this->broadcast($rawTx); // Custom broadcast logic
}
Gas Estimation:
Fetch nonce and gasLimit from the blockchain before signing:
$nonce = $this->ethRpcClient->getTransactionCount($address);
$gasLimit = $this->estimateGas($to, $data);
Error Handling: Validate inputs before signing:
if (!filter_var($to, FILTER_VALIDATE_ADDRESS)) {
throw new \InvalidArgumentException("Invalid recipient address");
}
Logging: Log raw transactions for debugging:
\Log::debug('Signed transaction:', ['rawTx' => $rawTx]);
Testing: Use placeholder mode in tests:
$signer = new Eip1559Signer(''); // Placeholder for tests
Private Key Security:
.env
ETHEREUM_PRIVATE_KEY=your_private_key_here
Nonce Management:
nonce from the blockchain before signing to avoid race conditions.$nonce = $ethRpcClient->getTransactionCount($signer->address());
Gas Limits:
gasLimit or maxFeePerGas/maxPriorityFeePerGas, and your transaction may fail or get stuck.Data Format:
valueWei, maxFeePerGas, and maxPriorityFeePerGas are passed as decimal strings (e.g., '1000000000000000000'), not integers, to avoid overflow.Chain ID:
chainId will result in invalid transactions. Double-check the chain ID matches the network you're targeting.Invalid Signatures:
chainId matches the network.nonce, gasLimit) are accurate.Placeholder Mode:
RLP Encoding Issues:
to, data) are properly formatted (e.g., 0x-prefixed hex strings).Dependencies:
ext-gmp. Ensure it’s enabled in your PHP environment:
php -m | grep gmp
sudo apt-get install php8.3-gmp on Ubuntu).Composite Packages:
amashukov/eth-php, which includes this package as a dependency.Custom Transaction Fields:
accessList (assumes empty). If you need access lists, extend the Eip1559Signer class or use a composite package like amashukov/eth-php.Additional Signing Logic:
sign() method to add pre-signing logic (e.g., gas price adjustments):
class CustomSigner extends Eip1559Signer {
public function sign(array $tx): string {
$tx['maxFeePerGas'] = $this->adjustGasPrice($tx['maxFeePerGas']);
return parent::sign($tx);
}
}
Integration with Laravel Queues:
SignTransactionJob::dispatch($signer, $txParams)->onQueue('ethereum');
Event Dispatching:
event(new TransactionSigned($rawTx));
How can I help you explore Laravel packages today?