Ethereum Transaction From Scratch In Rust

To use this crate first create Transaction struct

```rust use ethereum_types::H160;

let tx = Transaction { // Nonce of the transaction nonce: 225,

// To Address
to: Some(
H160::from_str(&"70997970C51812dc3A010C7d01b50e0d17dc79C6")
    .unwrap()
    .to_fixed_bytes(),
),

// Value
value: 10000000000,

// Chain ID
chain_id: 988242,

// Rest is default
..Default::default()

}; ```

You can also specify data if you want to call or deploy a smart contract:

```rust use ethereum_types::H160;

let data = vec![0, 0, 0, 0];

let tx = Transaction { // Nonce of the transaction nonce: 225,

// To Address
to: Some(
H160::from_str(&"70997970C51812dc3A010C7d01b50e0d17dc79C6")
    .unwrap()
    .to_fixed_bytes(),
),

// Value
value: 10000000000,

// Chain ID
chain_id: 988242,

data,

// Rest is default
..Default::default()

} ```

After creating the struct you can just call the sign method with your private key:

```rust use ethereum_types::H256;

// Add your private key // This is a know private key from hardhat test accounts let privatekey = H256::fromstr("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80").unwrap();

// Sign the transaction let txbytes = tx.sign(privatekey.as_bytes()); ```

If you want to send your signed transaction you will need to crate a json object from the signed bytes:

```rust use web3::types::Bytes;

// Convert Vec to Bytes so it can be serialized let txbytes = Bytes::from(txbytes);

// Convert it to JSON value let signedtx = serdejson::tovalue(txbytes).unwrap(); ```

You can find detailed example in examples

Acknowledgments

Credit goes to synlestidae creator of ethereum-tx-sign. His crate helped me to understand how are ethereum transactions encoded and signed.

This crate is just simpler implementation of ethereum-tx-sign.

Disclaimer

This is untested, unaudited software don't use in production or with real crypto!!!