Skip to content

Commit 6a4e4e4

Browse files
committed
feat: sessions
1 parent ba4b461 commit 6a4e4e4

File tree

123 files changed

+11753
-993
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+11753
-993
lines changed

packages/sdk-platforms/react-native/react-native-zksync-sso/example/src/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
},
99
"nodeUrl": "http://localhost:8011/",
1010
"deployWallet": {
11-
"privateKeyHex": "2544c43e10c09cd9bc1ecd30b931545eaf08b01811156714cae8062cbaaa0186"
11+
"privateKeyHex": "f1010e4119b26f6ceccb5a42b8dc6b2a10650d4f5943e4eb8285951aba264b6c"
1212
}
1313
}

packages/sdk-platforms/rust/zksync-sso/Cargo.toml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ resolver = "2"
55
[workspace.package]
66
version = "0.0.1"
77
edition = "2024"
8-
rust-version = "1.86"
8+
rust-version = "1.87"
99
license = "Apache-2.0"
1010

1111
[profile.release]
@@ -33,6 +33,9 @@ alloy-zksync = { version = "0.12.5" }
3333
openssl = "0.10.71"
3434
openssl-sys = "0.9.106"
3535

36+
# Signer
37+
k256 = { version = "0.13", default-features = false, features = ["ecdsa"] }
38+
3639
# Passkey support
3740
passkey-types = { git = "https://github.com/jackpooleyml/passkey-rs", branch = "validate-credential", features = [
3841
"testable",
@@ -59,8 +62,7 @@ async-trait = "0.1"
5962
futures = "0.3"
6063

6164
# Uniffi
62-
# uniffi = { version = "0.29" }
63-
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "6c3f9b2376cb4b622247e4ae94eabe8d426cc5c5" }
65+
uniffi = { version = "0.29" }
6466

6567
# Error handling
6668
eyre = "0.6"
@@ -69,13 +71,26 @@ thiserror = "2.0.11"
6971
# Randomness
7072
rand = "0.9"
7173

72-
# Hex
73-
hex = "0.4"
74+
# Hashing
75+
sha2 = "0.10"
76+
sha3 = "0.10.8"
77+
78+
# passkey support
79+
coset = "0.3"
80+
81+
# CBOR
82+
serde_cbor = "0.11"
83+
ciborium = { version = "^0.2.1", default-features = false }
84+
ciborium-io = { version = "^0.2.0", features = ["alloc"] }
7485

7586
# Base64
7687
base64-url = "3.0.0"
7788
base64 = "0.21"
7889

90+
# DER
91+
der = { version = "0.7", features = ["alloc"] }
92+
der_derive = "0.7"
93+
7994
# i18n
8095
icu = { version = "1.5.0", features = ["default", "serde", "std"] }
8196
icu_decimal = { version = "1.5.0", features = ["default"] }
@@ -89,3 +104,7 @@ log = "0.4"
89104
android_logger = { version = "0.15" }
90105
# Logging for Apple platforms
91106
oslog = "0.2"
107+
108+
# Mocking
109+
mockall = { version = "0.13.1" }
110+
scopeguard = { version = "1.2.0" }
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
# TODOs
22

33
[ ] Move `fund` method to seperate module
4-
[ ] Consider using the `log` crate for debug logging
5-
[ ] Change passkey attestation to `direct`

packages/sdk-platforms/rust/zksync-sso/crates/cli/src/deploy_contracts.rs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use sdk::{
33
api::utils::deploy_contracts,
44
config::{Config, deploy_wallet::DeployWallet},
55
};
6-
use std::{fs, path::PathBuf};
6+
use std::{fs, path::PathBuf, process::Command};
77
use url::Url;
88

99
pub async fn deploy_contracts_and_update_example_configs(
1010
node_url: Url,
1111
config_paths: Vec<PathBuf>,
1212
) -> Result<()> {
13-
println!("Deploying contracts to node: {}", node_url);
13+
println!("Deploying contracts to node: {node_url}");
1414

1515
let contracts = deploy_contracts(node_url.clone()).await?;
1616
println!("Contracts deployed successfully:");
@@ -20,9 +20,8 @@ pub async fn deploy_contracts_and_update_example_configs(
2020
println!(" ExampleAuthServerPaymaster: {}", contracts.account_paymaster);
2121
println!(" Recovery: {}", contracts.recovery);
2222

23-
let deploy_wallet = DeployWallet::random();
24-
let config =
25-
Config::new(contracts.clone(), node_url.clone(), deploy_wallet);
23+
let deploy_wallet = Some(DeployWallet::random());
24+
let config = Config::new(contracts, node_url.clone(), deploy_wallet);
2625

2726
for path in config_paths {
2827
let platform_name = path
@@ -40,15 +39,15 @@ fn write_and_verify_config(
4039
config_path: &PathBuf,
4140
platform_name: &str,
4241
) -> Result<()> {
43-
println!("\nWriting {} config to path: {:?}", platform_name, config_path);
42+
println!("\nWriting {platform_name} config to path: {config_path:?}");
4443
config.write_json(config_path)?;
4544

46-
println!("\nVerifying written {} config:", platform_name);
45+
println!("\nVerifying written {platform_name} config:");
4746
let written_json = fs::read_to_string(config_path)?;
48-
println!("Written {} JSON content:\n{}", platform_name, written_json);
47+
println!("Written {platform_name} JSON content:\n{written_json}");
4948

5049
let written_config: Config = serde_json::from_str(&written_json)?;
51-
println!("\nParsed {} config verification:", platform_name);
50+
println!("\nParsed {platform_name} config verification:");
5251
println!(" Node URL: {}", written_config.node_url);
5352
println!(" AAFactory: {}", written_config.contracts.account_factory);
5453
println!(" WebAuthValidator: {}", written_config.contracts.passkey);
@@ -59,8 +58,46 @@ fn write_and_verify_config(
5958
);
6059
println!(" Recovery: {}", written_config.contracts.recovery);
6160
println!(
62-
"\nSuccessfully updated and verified {} config values",
63-
platform_name
61+
"\nSuccessfully updated and verified {platform_name} config values"
6462
);
6563
Ok(())
6664
}
65+
66+
pub async fn build_and_deploy_contracts_and_update_example_configs(
67+
node_url: Url,
68+
config_paths: Option<Vec<PathBuf>>,
69+
) -> Result<()> {
70+
println!("Building contracts...");
71+
72+
let contracts_dir = std::env::current_dir()?
73+
.parent()
74+
.and_then(|p| p.parent())
75+
.and_then(|p| p.parent())
76+
.and_then(|p| p.parent())
77+
.map(|p| p.join("packages").join("contracts"))
78+
.ok_or_else(|| eyre::eyre!("Could not find contracts directory"))?;
79+
80+
let output = Command::new("pnpm")
81+
.arg("build")
82+
.current_dir(&contracts_dir)
83+
.output()?;
84+
85+
if !output.status.success() {
86+
return Err(eyre::eyre!(
87+
"Failed to build contracts: {}",
88+
String::from_utf8_lossy(&output.stderr)
89+
));
90+
}
91+
92+
println!("Contracts built successfully!");
93+
94+
let paths_to_write = match config_paths {
95+
Some(paths) if !paths.is_empty() => paths,
96+
_ => vec![
97+
Config::get_default_swift_config_path(),
98+
Config::get_default_react_native_config_path(),
99+
],
100+
};
101+
102+
deploy_contracts_and_update_example_configs(node_url, paths_to_write).await
103+
}

packages/sdk-platforms/rust/zksync-sso/crates/cli/src/handle_cli.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::handle_cli::Commands::DeployContracts;
1+
use crate::handle_cli::Commands::{BuildAndDeployContracts, DeployContracts};
22
use clap::{Parser, Subcommand};
33
use sdk::config::Config;
44
use std::path::PathBuf;
@@ -19,6 +19,16 @@ enum Commands {
1919
#[arg(long, default_value = "http://localhost:8011/")]
2020
node_url: Url,
2121

22+
/// Paths to write the config files to. If not provided or if empty, defaults to Swift and React Native example paths.
23+
#[arg(long)]
24+
config_paths: Option<Vec<PathBuf>>,
25+
},
26+
/// Build contracts and then deploy them with config updates
27+
BuildAndDeployContracts {
28+
/// The URL of the zkSync node (defaults to http://localhost:8011/)
29+
#[arg(long, default_value = "http://localhost:8011/")]
30+
node_url: Url,
31+
2232
/// Paths to write the config files to. If not provided or if empty, defaults to Swift and React Native example paths.
2333
#[arg(long)]
2434
config_paths: Option<Vec<PathBuf>>,
@@ -43,6 +53,13 @@ pub async fn handle_cli() -> eyre::Result<()> {
4353
)
4454
.await?;
4555
}
56+
BuildAndDeployContracts { node_url, config_paths } => {
57+
super::deploy_contracts::build_and_deploy_contracts_and_update_example_configs(
58+
node_url,
59+
config_paths,
60+
)
61+
.await?;
62+
}
4663
}
4764

4865
Ok(())

packages/sdk-platforms/rust/zksync-sso/crates/ffi/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ log.workspace = true
3232
rand.workspace = true
3333
base64.workspace = true
3434

35-
# Hex encoding/decoding
36-
hex.workspace = true
37-
3835
# Target-specific dependencies - Android
3936
[target.'cfg(target_os = "android")'.dependencies]
4037
android_logger.workspace = true

packages/sdk-platforms/rust/zksync-sso/crates/ffi/build-swift-framework-ios-ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,6 @@ sed -i '' 's/filenonisolated(unsafe) private let uniffiContinuationHandleMap = U
130130
cat <<EOT >> "$WRAPPER_PATH/$FRAMEWORK_LIBRARY_NAME.swift"
131131
132132
extension Config: Codable {}
133-
extension PasskeyContracts: Codable {}
133+
extension SsoContracts: Codable {}
134134
extension DeployWallet: Codable {}
135135
EOT

packages/sdk-platforms/rust/zksync-sso/crates/ffi/src/account.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ mod deployment;
33
mod fetch;
44
mod fund;
55
mod send;
6+
mod session;
7+
mod transaction;
68

79
#[derive(Debug, Clone, uniffi::Record)]
810
pub struct Account {

packages/sdk-platforms/rust/zksync-sso/crates/ffi/src/account/send.rs

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
use crate::{
2+
account::transaction::Transaction,
23
config,
34
native_apis::{PasskeyAuthenticator, PasskeyAuthenticatorAsync},
45
};
56
use futures::future::BoxFuture;
67
use log::debug;
7-
use sdk::api::utils::parse_address;
88
use std::sync::Arc;
99

1010
pub mod prepare;
1111

12-
#[derive(Debug, uniffi::Record)]
13-
pub struct Transaction {
14-
pub from: String,
15-
pub to: Option<String>,
16-
pub value: Option<String>,
17-
pub input: Option<String>,
18-
}
19-
2012
#[derive(Debug, uniffi::Record)]
2113
pub struct SendTransactionResult {
2214
pub tx_hash: String,
@@ -31,27 +23,6 @@ pub enum SendTransactionError {
3123
InvalidAddress(String),
3224
}
3325

34-
impl TryFrom<Transaction> for sdk::api::account::transaction::Transaction {
35-
type Error = SendTransactionError;
36-
37-
fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
38-
Ok(Self {
39-
from: parse_address(&tx.from).map_err(|e| {
40-
SendTransactionError::InvalidAddress(e.to_string())
41-
})?,
42-
to: tx.to.and_then(|to| {
43-
parse_address(&to)
44-
.map_err(|e| {
45-
SendTransactionError::InvalidAddress(e.to_string())
46-
})
47-
.ok()
48-
}),
49-
value: tx.value,
50-
input: tx.input,
51-
})
52-
}
53-
}
54-
5526
impl From<sdk::api::account::send::SendTransactionResult>
5627
for SendTransactionResult
5728
{
@@ -73,20 +44,23 @@ pub async fn send_transaction(
7344
authenticator: Arc<dyn PasskeyAuthenticator + 'static>,
7445
config: config::Config,
7546
) -> Result<SendTransactionResult, SendTransactionError> {
76-
debug!("XDB send_transaction - transaction: {:?}", transaction);
47+
debug!("XDB send_transaction - transaction: {transaction:?}");
7748
let tx: sdk::api::account::transaction::Transaction =
78-
transaction.try_into()?;
49+
transaction.try_into().map_err(
50+
|e: crate::account::transaction::TransactionConversionError| {
51+
SendTransactionError::SendTransaction(e.to_string())
52+
},
53+
)?;
7954

80-
debug!("XDB send_transaction - tx: {:?}", tx);
55+
debug!("XDB send_transaction - tx: {tx:?}");
8156

8257
let authenticator = authenticator.clone();
8358
let sign_message: SignFn = Box::new(
8459
move |message: &[u8]| -> BoxFuture<'static, Result<Vec<u8>, String>> {
8560
let message_owned = message.to_vec();
8661
let auth = authenticator.clone();
8762
debug!(
88-
"XDB send_transaction - sign_message - message_owned: {:?}",
89-
message_owned
63+
"XDB send_transaction - sign_message - message_owned: {message_owned:?}"
9064
);
9165
Box::pin(async move {
9266
debug!(
@@ -117,14 +91,15 @@ pub async fn send_transaction_async_signer(
11791
authenticator: Arc<dyn PasskeyAuthenticatorAsync + 'static>,
11892
config: config::Config,
11993
) -> Result<SendTransactionResult, SendTransactionError> {
120-
debug!(
121-
"XDB send_transaction_async_signer - transaction: {:?}",
122-
transaction
123-
);
94+
debug!("XDB send_transaction_async_signer - transaction: {transaction:?}");
12495
let tx: sdk::api::account::transaction::Transaction =
125-
transaction.try_into()?;
96+
transaction.try_into().map_err(
97+
|e: crate::account::transaction::TransactionConversionError| {
98+
SendTransactionError::SendTransaction(e.to_string())
99+
},
100+
)?;
126101

127-
debug!("XDB send_transaction_async_signer - tx: {:?}", tx);
102+
debug!("XDB send_transaction_async_signer - tx: {tx:?}");
128103

129104
let authenticator = authenticator.clone();
130105
let sign_message: SignFn = Box::new(

packages/sdk-platforms/rust/zksync-sso/crates/ffi/src/account/send/prepare.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use super::SendTransactionError::{
2-
InvalidAddress as SendInvalidAddress,
3-
SendTransaction as SendSendTransaction,
4-
};
51
use crate::{
6-
account::send::prepare::PrepareTransactionError::{
7-
InvalidAddress, PrepareTransaction,
2+
account::{
3+
send::prepare::PrepareTransactionError::{
4+
InvalidAddress, PrepareTransaction,
5+
},
6+
transaction::Transaction,
87
},
98
config,
109
};
@@ -54,14 +53,13 @@ pub enum PrepareTransactionError {
5453

5554
#[uniffi::export(async_runtime = "tokio")]
5655
pub async fn prepare_send_transaction(
57-
transaction: super::Transaction,
56+
transaction: Transaction,
5857
config: config::Config,
5958
) -> Result<PreparedTransaction, PrepareTransactionError> {
6059
let transaction: sdk::api::account::transaction::Transaction =
61-
transaction.try_into().map_err(|e| -> PrepareTransactionError {
60+
transaction.try_into().map_err(|e: crate::account::transaction::TransactionConversionError| -> PrepareTransactionError {
6261
match e {
63-
SendInvalidAddress(e) => InvalidAddress(e),
64-
SendSendTransaction(e) => PrepareTransaction(e),
62+
crate::account::transaction::TransactionConversionError::InvalidAddress(e) => InvalidAddress(e),
6563
}
6664
})?;
6765

0 commit comments

Comments
 (0)