Skip to content

Commit

Permalink
feat: v3 transaction support
Browse files Browse the repository at this point in the history
Users can now opt in to sending v3 transactions, paying transaction
fees in STRK instead of ETH. To user v3 transactions, simply use any
of `--fee-token STRK`, `--strk`, `--strk-fee` in transaction-sending
commands.
  • Loading branch information
xJonathanLEI committed May 1, 2024
1 parent 53ffb43 commit 3624201
Show file tree
Hide file tree
Showing 11 changed files with 798 additions and 344 deletions.
77 changes: 39 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ serde = { version = "1.0.164", features = ["derive"] }
serde_json = { version = "1.0.99", features = ["preserve_order"] }
serde_with = "2.3.3"
shellexpand = "3.1.0"
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "d980869d44ef86249b4077f4ec809165f470678d" }
starknet-crypto = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "d980869d44ef86249b4077f4ec809165f470678d" }
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "805b57b0258445c01a167cdc3cb43449ef078936" }
starknet-crypto = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "805b57b0258445c01a167cdc3cb43449ef078936" }
tempfile = "3.8.0"
thiserror = "1.0.40"
tokio = { version = "1.28.2", default-features = false, features = ["macros", "rt-multi-thread"] }
Expand Down
57 changes: 53 additions & 4 deletions src/account_factory/braavos.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use async_trait::async_trait;
use starknet::{
accounts::{AccountFactory, PreparedAccountDeployment, RawAccountDeployment},
accounts::{
AccountFactory, PreparedAccountDeploymentV1, PreparedAccountDeploymentV3,
RawAccountDeploymentV1, RawAccountDeploymentV3,
},
core::types::{BlockId, BlockTag, FieldElement},
providers::Provider,
signers::Signer,
Expand Down Expand Up @@ -77,12 +80,58 @@ where
self.block_id
}

async fn sign_deployment(
async fn sign_deployment_v1(
&self,
deployment: &RawAccountDeployment,
deployment: &RawAccountDeploymentV1,
) -> Result<Vec<FieldElement>, Self::SignError> {
let tx_hash =
PreparedAccountDeployment::from_raw(deployment.clone(), self).transaction_hash();
PreparedAccountDeploymentV1::from_raw(deployment.clone(), self).transaction_hash();

let signature = self.signer.sign_hash(&tx_hash).await?;

let mut aux_data = vec![
// account_implementation
self.class_hash,
// signer_type
FieldElement::ZERO,
// secp256r1_signer.x.low
FieldElement::ZERO,
// secp256r1_signer.x.high
FieldElement::ZERO,
// secp256r1_signer.y.low
FieldElement::ZERO,
// secp256r1_signer.y.high
FieldElement::ZERO,
// multisig_threshold
FieldElement::ZERO,
// withdrawal_limit_low
FieldElement::ZERO,
// fee_rate
FieldElement::ZERO,
// stark_fee_rate
FieldElement::ZERO,
// chain_id
self.chain_id,
];

let aux_hash = poseidon_hash_many(&aux_data);

let aux_signature = self.signer.sign_hash(&aux_hash).await?;

let mut full_signature_payload = vec![signature.r, signature.s];
full_signature_payload.append(&mut aux_data);
full_signature_payload.push(aux_signature.r);
full_signature_payload.push(aux_signature.s);

Ok(full_signature_payload)
}

async fn sign_deployment_v3(
&self,
deployment: &RawAccountDeploymentV3,
) -> Result<Vec<FieldElement>, Self::SignError> {
let tx_hash =
PreparedAccountDeploymentV3::from_raw(deployment.clone(), self).transaction_hash();

let signature = self.signer.sign_hash(&tx_hash).await?;

Expand Down
24 changes: 18 additions & 6 deletions src/account_factory/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use async_trait::async_trait;
use starknet::{
accounts::{
AccountFactory, ArgentAccountFactory, OpenZeppelinAccountFactory, RawAccountDeployment,
AccountFactory, ArgentAccountFactory, OpenZeppelinAccountFactory, RawAccountDeploymentV1,
RawAccountDeploymentV3,
},
core::types::{BlockId, FieldElement},
providers::Provider,
Expand Down Expand Up @@ -67,14 +68,25 @@ where
}
}

async fn sign_deployment(
async fn sign_deployment_v1(
&self,
deployment: &RawAccountDeployment,
deployment: &RawAccountDeploymentV1,
) -> Result<Vec<FieldElement>, Self::SignError> {
match self {
AnyAccountFactory::OpenZeppelin(inner) => inner.sign_deployment(deployment).await,
AnyAccountFactory::Argent(inner) => inner.sign_deployment(deployment).await,
AnyAccountFactory::Braavos(inner) => inner.sign_deployment(deployment).await,
AnyAccountFactory::OpenZeppelin(inner) => inner.sign_deployment_v1(deployment).await,
AnyAccountFactory::Argent(inner) => inner.sign_deployment_v1(deployment).await,
AnyAccountFactory::Braavos(inner) => inner.sign_deployment_v1(deployment).await,
}
}

async fn sign_deployment_v3(
&self,
deployment: &RawAccountDeploymentV3,
) -> Result<Vec<FieldElement>, Self::SignError> {
match self {
AnyAccountFactory::OpenZeppelin(inner) => inner.sign_deployment_v3(deployment).await,
AnyAccountFactory::Argent(inner) => inner.sign_deployment_v3(deployment).await,
AnyAccountFactory::Braavos(inner) => inner.sign_deployment_v3(deployment).await,
}
}
}

0 comments on commit 3624201

Please sign in to comment.