Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
Refactor/eoa new param (#839)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->


Time spent on this PR:1d

## 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)
- [x] 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 #829 

## What is the new behavior?

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

- change eoa to now take an unsigned payload as calldata and signature
in tx_info
  • Loading branch information
Eikix authored Dec 7, 2023
1 parent 649c2d9 commit f361ff5
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 227 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
GITHUB_TOKEN=

# Starkent Network
STARKNET_NETWORK=starknet-devnet
STARKNET_NETWORK=katana

# An infura RPC key for mainnet/testnet
INFURA_KEY=
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ jobs:
uses: actions/checkout@v3
with:
repository: kkrt-labs/ef-tests
ref: feat/move-signature-location
- name: Rust cache
uses: Swatinem/rust-cache@v2
with:
Expand Down
6 changes: 3 additions & 3 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 0.1
cli:
version: 1.16.2
version: 1.17.2
plugins:
sources:
- id: trunk
Expand Down Expand Up @@ -45,8 +45,8 @@ lint:
- [email protected]
- [email protected]
disabled:
- trufflehog@3.56.0
- bandit@1.7.5
- trufflehog
- bandit
ignore:
- linters: [bandit]
paths: [tests, docker]
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[tool.poetry]
name = "kakarot"
version = "0.1.0"
description = "EVM interpreter written in Cairo, a sort of ZK-EVM emulator, leveraging STARK proof system."
description = "EVM implementation written in Cairo, a sort of ZK-EVM emulator, leveraging STARK proof system."
authors = []
readme = "README.md"
packages = [{ include = "scripts" }]
packages = [{ include = "scripts" }, { include = "tests" }]

[tool.poetry.dependencies]
python = ">=3.9,<3.10"
Expand Down
65 changes: 44 additions & 21 deletions scripts/utils/kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from starknet_py.net.account.account import Account
from starknet_py.net.client_errors import ClientError
from starknet_py.net.client_models import Call, Event
from starknet_py.net.models.transaction import Invoke
from starknet_py.net.signer.stark_curve_signer import KeyPair
from starkware.starknet.public.abi import starknet_keccak
from web3 import Web3
Expand All @@ -40,6 +41,8 @@
from scripts.utils.starknet import get_deployments
from scripts.utils.starknet import invoke as _invoke_starknet
from scripts.utils.starknet import wait_for_transaction
from tests.utils.helpers import rlp_encode_signed_data
from tests.utils.uint256 import int_to_uint256

logging.basicConfig()
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -287,32 +290,52 @@ async def eth_send_transaction(
):
"""Execute the data at the EVM contract to on Kakarot."""
evm_account = caller_eoa or await get_eoa()
nonce = await evm_account.get_nonce()

payload = {
"type": 0x2,
"chainId": KAKAROT_CHAIN_ID,
"nonce": nonce,
"gas": gas,
"maxPriorityFeePerGas": int(1e19),
"maxFeePerGas": int(1e19),
"to": to_checksum_address(to) if to else None,
"value": value,
"data": data,
}

typed_transaction = TypedTransaction.from_dict(payload)

typed_transaction = TypedTransaction.from_dict(
{
"type": 0x2,
"chainId": KAKAROT_CHAIN_ID,
"nonce": await evm_account.get_nonce(),
"gas": gas,
"maxPriorityFeePerGas": int(1e19),
"maxFeePerGas": int(1e19),
"to": to_checksum_address(to) if to else None,
"value": value,
"data": data,
}
)
evm_tx = EvmAccount.sign_transaction(
typed_transaction.as_dict(),
hex(evm_account.signer.private_key),
)
response = await evm_account.execute(
calls=Call(
to_addr=0xDEAD, # unused in current EOA implementation
selector=0xDEAD, # unused in current EOA implementation
calldata=evm_tx.rawTransaction,
),

encoded_unsigned_tx = rlp_encode_signed_data(typed_transaction.as_dict())

prepared_invoke = await evm_account._prepare_invoke(
calls=[
Call(
to_addr=0xDEAD, # unused in current EOA implementation
selector=0xDEAD, # unused in current EOA implementation
calldata=encoded_unsigned_tx,
)
],
max_fee=int(5e17) if max_fee is None else max_fee,
)
# We need to reconstruct the prepared_invoke with the new signature
# And Invoke.signature is Frozen
prepared_invoke = Invoke(
version=prepared_invoke.version,
max_fee=prepared_invoke.max_fee,
signature=[*int_to_uint256(evm_tx.r), *int_to_uint256(evm_tx.s), evm_tx.v],
nonce=prepared_invoke.nonce,
sender_address=prepared_invoke.sender_address,
calldata=prepared_invoke.calldata,
)

response = await evm_account.client.send_transaction(prepared_invoke)

await wait_for_transaction(tx_hash=response.transaction_hash)
receipt = await CLIENT.get_transaction_receipt(response.transaction_hash)
transaction_events = [
Expand All @@ -324,8 +347,8 @@ async def eth_send_transaction(
if len(transaction_events) != 1:
raise ValueError("Cannot locate the single event giving the actual tx status")
(
msg_hash_low,
msg_hash_high,
_msg_hash_low,
_msg_hash_high,
response_len,
*response,
success,
Expand Down
21 changes: 15 additions & 6 deletions src/kakarot/accounts/eoa/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,20 @@ namespace ExternallyOwnedAccount {
let (address) = evm_address.read();
let (tx_info) = get_tx_info();

// Assert signature field is of length 5: r_low, r_high, s_low, s_high, v
assert tx_info.signature_len = 5;
let r = Uint256(tx_info.signature[0], tx_info.signature[1]);
let s = Uint256(tx_info.signature[2], tx_info.signature[3]);
let v = tx_info.signature[4];

EthTransaction.validate(
address, tx_info.nonce, [call_array].data_len, calldata + [call_array].data_offset
address,
tx_info.nonce,
r,
s,
v,
[call_array].data_len,
calldata + [call_array].data_offset,
);

return validate(
Expand Down Expand Up @@ -134,17 +146,15 @@ namespace ExternallyOwnedAccount {
}

let (
msg_hash,
nonce,
gas_price,
gas_limit,
destination,
amount,
_chain_id,
payload_len,
payload,
msg_hash,
v,
r,
s,
) = EthTransaction.decode([call_array].data_len, calldata + [call_array].data_offset);

let (_kakarot_address) = kakarot_address.read();
Expand All @@ -161,7 +171,6 @@ namespace ExternallyOwnedAccount {

// See Argent account
// https://github.com/argentlabs/argent-contracts-starknet/blob/c6d3ee5e05f0f4b8a5c707b4094446c3bc822427/contracts/account/ArgentAccount.cairo#L132
// Using here the EVM msg_hash instead of the Starknet tx.hash
transaction_executed.emit(
msg_hash=msg_hash, response_len=return_data_len, response=return_data, success=success
);
Expand Down
Loading

0 comments on commit f361ff5

Please sign in to comment.