Skip to content

Commit

Permalink
Merge pull request #5 from ermvrs/main
Browse files Browse the repository at this point in the history
rlp and eip1559
  • Loading branch information
ermvrs authored Oct 30, 2024
2 parents 9e6f2a0 + cb99c66 commit f5d4f22
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 17 deletions.
1 change: 0 additions & 1 deletion .snfoundry_cache/.prev_tests_failed
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
rosettacontracts::utils::serialization::tests::test_chunks_deserialize
tests::factory_tests::deploy_check_initials
8 changes: 8 additions & 0 deletions src/accounts/base.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub mod RosettaAccount {
};
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use rosettacontracts::accounts::utils::{is_valid_eth_signature, Secp256k1PointStorePacking, pubkey_to_eth_address};
use rosettacontracts::utils::serialization::{deserialize_bytes};
use rosettacontracts::utils::rlp::{RLPType, RLPTrait, RLPItem, RLPHelpersTrait};
use rosettacontracts::utils::transaction::eip1559::{Eip1559, Eip1559Trait};

pub mod Errors {
pub const INVALID_CALLER: felt252 = 'Rosetta: invalid caller';
Expand Down Expand Up @@ -142,6 +145,11 @@ pub mod RosettaAccount {
// TODO verify transaction with eth address not pub key
// Kakarot calldata ile transactionu bir daha olusturup verify etmeye calismis
let public_key: EthPublicKey = self.ethereum_public_key.read();

let deserialized_signature = deserialize_bytes(signature).unwrap();
let mut rlp_decoded_transaction = RLPTrait::decode(deserialized_signature.span()).unwrap();
let eip1559_transaction = Eip1559Trait::decode_fields(ref rlp_decoded_transaction).unwrap();

is_valid_eth_signature(hash, public_key, signature)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod traits;
pub mod eth_address;

pub mod transaction {
pub mod eip1559;
pub mod eip2930;
pub mod common;
}
Expand Down
20 changes: 4 additions & 16 deletions src/utils/serialization.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,16 @@ fn compute_y_parity(v: u128, chain_id: u64) -> Option<bool> {
return Option::None;
}

use crate::utils::helpers::{u256_to_bytes_array, split_word};
// TODO complete word splitting
pub fn deserialize_and_split_bytes(self: Span<felt252>) -> Array<u8> {
let mut bytes: Array<u8> = Default::default();

for item in self {
split_word((*item).into(), 16, ref bytes);

};

bytes
}

#[cfg(test)]
mod tests {
use crate::utils::serialization::{deserialize_and_split_bytes};
use crate::utils::serialization::{deserialize_bytes};

#[test]
fn test_chunks_deserialize() {
let arr = array![0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 0xABC];
fn test_bytes_deserialize() {
let arr = array![0xFF, 0xAB];

let deserialized_bytes = deserialize_and_split_bytes(arr.span());
let deserialized_bytes = deserialize_bytes(arr.span()).unwrap();

assert_eq!(*deserialized_bytes.at(0), 0xFF);
}
Expand Down
80 changes: 80 additions & 0 deletions src/utils/transaction/eip1559.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::utils::transaction::common::{TxKind};
use crate::utils::transaction::eip2930::{AccessListItem};
use crate::utils::traits::SpanDefault;
use crate::utils::rlp::{RLPItem, RLPHelpersTrait};
use crate::errors::{EthTransactionError, RLPError, RLPErrorTrait};

#[derive(Copy, Drop, Debug, Default, PartialEq, Serde)]
pub struct Eip1559 {
pub chain_id: u64,
pub nonce: u64,
pub gas_limit: u64,
pub max_fee_per_gas: u128,
pub max_priority_fee_per_gas: u128,
pub to: TxKind,
pub value: u256,
pub access_list: Span<AccessListItem>,
pub input: Span<u8>,
}

#[generate_trait]
pub impl _impl of Eip1559Trait {
/// Decodes the RLP-encoded fields into a TxEip1559 struct.
///
/// # Arguments
///
/// * `data` - A span of RLPItems containing the encoded transaction fields
///
/// # Returns
///
/// A Result containing either the decoded TxEip1559 struct or an EthTransactionError
fn decode_fields(ref data: Span<RLPItem>) -> Result<Eip1559, EthTransactionError> {
let boxed_fields = data
.multi_pop_front::<9>()
.ok_or(EthTransactionError::RLPError(RLPError::InputTooShort))?;
let [
chain_id_encoded,
nonce_encoded,
max_priority_fee_per_gas_encoded,
max_fee_per_gas_encoded,
gas_limit_encoded,
to_encoded,
value_encoded,
input_encoded,
access_list_encoded
] =
(*boxed_fields)
.unbox();

let chain_id = chain_id_encoded.parse_u64_from_string().map_err()?;
let nonce = nonce_encoded.parse_u64_from_string().map_err()?;
let max_priority_fee_per_gas = max_priority_fee_per_gas_encoded
.parse_u128_from_string()
.map_err()?;
let max_fee_per_gas = max_fee_per_gas_encoded.parse_u128_from_string().map_err()?;
let gas_limit = gas_limit_encoded.parse_u64_from_string().map_err()?;
let to = to_encoded.try_parse_address_from_string().map_err()?;
let value = value_encoded.parse_u256_from_string().map_err()?;
let input = input_encoded.parse_bytes_from_string().map_err()?;
let access_list = access_list_encoded.parse_access_list().map_err()?;

let txkind_to = match to {
Option::Some(to) => { TxKind::Call(to) },
Option::None => { TxKind::Create }
};

Result::Ok(
Eip1559 {
chain_id,
nonce,
max_priority_fee_per_gas,
max_fee_per_gas,
gas_limit,
to: txkind_to,
value,
access_list,
input,
}
)
}
}

0 comments on commit f5d4f22

Please sign in to comment.