Skip to content

Commit

Permalink
Add comments to "utils/eth_transaction" (kkrt-labs#636)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

Adding comments for utils function on eth_transaction.cairo file

<!-- Give an estimate of the time you spent on this PR in terms of work
days. Did you spend 0.5 days on this PR or rather 2 days? -->

Time spent on this PR: 0.1

## Pull request type

<!-- Please try to limit your pull request to one type, submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:

- [ ] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying, or
link to a relevant issue. -->

Resolves #<Issue number>

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

-
-
-

## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->
  • Loading branch information
danilowhk authored Jul 25, 2023
1 parent 9d2992d commit 525b2ad
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/utils/eth_transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ from utils.utils import Helpers
// @notice This file contains utils for decoding eth transactions
// @custom:namespace EthTransaction
namespace EthTransaction {
// @notice Decode a legacy Ethereum transaction
// @dev This function decodes a legacy Ethereum transaction in accordance with EIP-155.
// It returns transaction details including nonce, gas price, gas limit, destination address, amount, payload,
// transaction hash, and signature (v, r, s). The transaction hash is computed by keccak hashing the signed
// transaction data, which includes the chain ID in accordance with EIP-155.
// @param tx_data_len The length of the raw transaction data
// @param tx_data The raw transaction data
func decode_legacy_tx{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
Expand Down Expand Up @@ -125,6 +133,13 @@ namespace EthTransaction {
);
}

// @notice Decode a modern Ethereum transaction
// @dev This function decodes a modern Ethereum transaction in accordance with EIP-2718.
// It returns transaction details including nonce, gas price, gas limit, destination address, amount, payload,
// transaction hash, and signature (v, r, s). The transaction hash is computed by keccak hashing the signed
// transaction data, which includes the chain ID as part of the transaction data itself.
// @param tx_data_len The length of the raw transaction data
// @param tx_data The raw transaction data
func decode_tx{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
Expand All @@ -143,7 +158,7 @@ namespace EthTransaction {
r: Uint256,
s: Uint256,
) {
// see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md#specification
// see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2718.md#specification
alloc_locals;
tempvar tx_type = [tx_data];

Expand Down Expand Up @@ -232,12 +247,22 @@ namespace EthTransaction {
);
}

// @notice Check if a raw transaction is a legacy Ethereum transaction
// @dev This function checks if a raw transaction is a legacy Ethereum transaction by checking the transaction type
// according to EIP-2718. If the transaction type is less than or equal to 0xc0, it's a legacy transaction.
// @param tx_data The raw transaction data
func is_legacy_tx{range_check_ptr}(tx_data: felt*) -> felt {
// See https://eips.ethereum.org/EIPS/eip-2718#transactiontype-only-goes-up-to-0x7f
tempvar type = [tx_data];
return is_le(0xc0, type);
}

// @notice Decode a raw Ethereum transaction
// @dev This function decodes a raw Ethereum transaction. It checks if the transaction
// is a legacy transaction or a modern transaction, and calls the appropriate decode function
// (decode_legacy_tx or decode_tx) based on the result.
// @param tx_data_len The length of the raw transaction data
// @param tx_data The raw transaction data
func decode{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
Expand All @@ -264,6 +289,15 @@ namespace EthTransaction {
}
}

// @notice Validate an Ethereum transaction
// @dev This function validates an Ethereum transaction by checking if the transaction
// is correctly signed by the given address, and if the nonce in the transaction
// matches the nonce of the account. It decodes the transaction using the decode function,
// and then verifies the Ethereum signature on the transaction hash.
// @param address The address that is supposed to have signed the transaction
// @param account_nonce The nonce of the account
// @param tx_data_len The length of the raw transaction data
// @param tx_data The raw transaction data
func validate{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
Expand Down

0 comments on commit 525b2ad

Please sign in to comment.