Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Eip1559 Tx Signer Php Laravel Package

amashukov/eip1559-tx-signer-php

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require amashukov/eip1559-tx-signer-php
    

    Ensure your project meets the requirements: PHP 8.3+ and ext-gmp.

  2. 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'
    );
    
  3. 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.
    • README.md: For detailed usage examples and features.

Implementation Patterns

Usage Patterns

  1. 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
    );
    
  2. Deterministic Signing: Leverage RFC 6979 for reproducible signatures:

    $samePrivateKeyAndTx = $signer->sign(...); // Always produces the same rawTx
    
  3. Address Derivation: Derive the sender address from the private key:

    $address = $signer->address(); // Returns the Ethereum address
    
  4. Placeholder Mode: Use an empty private key for testing/dev environments:

    $signer = new Eip1559Signer(''); // Placeholder mode
    

Workflows

  1. Integration with Laravel:

    • Store private keys securely (e.g., Laravel's config or environment variables).
    • Use dependency injection to pass the Eip1559Signer instance to services:
      $this->app->bind(Eip1559Signer::class, function ($app) {
          return new Eip1559Signer(config('ethereum.private_key'), chainId: config('ethereum.chain_id'));
      });
      
  2. Batch Transactions: Loop through transactions and sign them sequentially:

    foreach ($transactions as $tx) {
        $rawTx = $signer->sign($tx);
        $this->broadcast($rawTx); // Custom broadcast logic
    }
    
  3. Gas Estimation: Fetch nonce and gasLimit from the blockchain before signing:

    $nonce = $this->ethRpcClient->getTransactionCount($address);
    $gasLimit = $this->estimateGas($to, $data);
    

Integration Tips

  1. Error Handling: Validate inputs before signing:

    if (!filter_var($to, FILTER_VALIDATE_ADDRESS)) {
        throw new \InvalidArgumentException("Invalid recipient address");
    }
    
  2. Logging: Log raw transactions for debugging:

    \Log::debug('Signed transaction:', ['rawTx' => $rawTx]);
    
  3. Testing: Use placeholder mode in tests:

    $signer = new Eip1559Signer(''); // Placeholder for tests
    

Gotchas and Tips

Pitfalls

  1. Private Key Security:

    • Never hardcode private keys in your codebase. Use environment variables or secure vaults.
    • Example: .env
      ETHEREUM_PRIVATE_KEY=your_private_key_here
      
  2. Nonce Management:

    • Always fetch the latest nonce from the blockchain before signing to avoid race conditions.
    • Example:
      $nonce = $ethRpcClient->getTransactionCount($signer->address());
      
  3. Gas Limits:

    • Underestimate gasLimit or maxFeePerGas/maxPriorityFeePerGas, and your transaction may fail or get stuck.
    • Use tools like Etherscan or custom gas estimation logic to set appropriate values.
  4. Data Format:

    • Ensure valueWei, maxFeePerGas, and maxPriorityFeePerGas are passed as decimal strings (e.g., '1000000000000000000'), not integers, to avoid overflow.
  5. Chain ID:

    • Incorrect chainId will result in invalid transactions. Double-check the chain ID matches the network you're targeting.

Debugging

  1. Invalid Signatures:

    • If a transaction fails with an "invalid signature" error, verify:
      • The private key is correct.
      • The chainId matches the network.
      • The transaction parameters (e.g., nonce, gasLimit) are accurate.
  2. Placeholder Mode:

    • Transactions signed with a placeholder key will fail on the blockchain. Use this mode only for testing.
  3. RLP Encoding Issues:

    • If the raw transaction appears malformed, ensure all fields (e.g., to, data) are properly formatted (e.g., 0x-prefixed hex strings).

Config Quirks

  1. Dependencies:

    • The package relies on ext-gmp. Ensure it’s enabled in your PHP environment:
      php -m | grep gmp
      
    • If missing, install it via your system package manager (e.g., sudo apt-get install php8.3-gmp on Ubuntu).
  2. Composite Packages:

    • For broader Ethereum functionality, consider using amashukov/eth-php, which includes this package as a dependency.

Extension Points

  1. Custom Transaction Fields:

    • The package currently omits accessList (assumes empty). If you need access lists, extend the Eip1559Signer class or use a composite package like amashukov/eth-php.
  2. Additional Signing Logic:

    • Extend the 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);
          }
      }
      
  3. Integration with Laravel Queues:

    • Dispatch signing jobs to a queue for async processing:
      SignTransactionJob::dispatch($signer, $txParams)->onQueue('ethereum');
      
  4. Event Dispatching:

    • Trigger events before/after signing for observability:
      event(new TransactionSigned($rawTx));
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity